Path: blob/master/drivers/infiniband/hw/mlx4/main.c
15112 views
/*1* Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.2* Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/module.h>34#include <linux/init.h>35#include <linux/slab.h>36#include <linux/errno.h>37#include <linux/netdevice.h>38#include <linux/inetdevice.h>39#include <linux/rtnetlink.h>40#include <linux/if_vlan.h>4142#include <rdma/ib_smi.h>43#include <rdma/ib_user_verbs.h>44#include <rdma/ib_addr.h>4546#include <linux/mlx4/driver.h>47#include <linux/mlx4/cmd.h>4849#include "mlx4_ib.h"50#include "user.h"5152#define DRV_NAME "mlx4_ib"53#define DRV_VERSION "1.0"54#define DRV_RELDATE "April 4, 2008"5556MODULE_AUTHOR("Roland Dreier");57MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");58MODULE_LICENSE("Dual BSD/GPL");59MODULE_VERSION(DRV_VERSION);6061static const char mlx4_ib_version[] =62DRV_NAME ": Mellanox ConnectX InfiniBand driver v"63DRV_VERSION " (" DRV_RELDATE ")\n";6465struct update_gid_work {66struct work_struct work;67union ib_gid gids[128];68struct mlx4_ib_dev *dev;69int port;70};7172static struct workqueue_struct *wq;7374static void init_query_mad(struct ib_smp *mad)75{76mad->base_version = 1;77mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;78mad->class_version = 1;79mad->method = IB_MGMT_METHOD_GET;80}8182static union ib_gid zgid;8384static int mlx4_ib_query_device(struct ib_device *ibdev,85struct ib_device_attr *props)86{87struct mlx4_ib_dev *dev = to_mdev(ibdev);88struct ib_smp *in_mad = NULL;89struct ib_smp *out_mad = NULL;90int err = -ENOMEM;9192in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);93out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);94if (!in_mad || !out_mad)95goto out;9697init_query_mad(in_mad);98in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;99100err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);101if (err)102goto out;103104memset(props, 0, sizeof *props);105106props->fw_ver = dev->dev->caps.fw_ver;107props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |108IB_DEVICE_PORT_ACTIVE_EVENT |109IB_DEVICE_SYS_IMAGE_GUID |110IB_DEVICE_RC_RNR_NAK_GEN |111IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;112if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)113props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;114if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)115props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;116if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)117props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;118if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)119props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;120if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)121props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;122if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)123props->device_cap_flags |= IB_DEVICE_UD_TSO;124if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)125props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;126if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&127(dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&128(dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))129props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;130131props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &1320xffffff;133props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));134props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));135memcpy(&props->sys_image_guid, out_mad->data + 4, 8);136137props->max_mr_size = ~0ull;138props->page_size_cap = dev->dev->caps.page_size_cap;139props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;140props->max_qp_wr = dev->dev->caps.max_wqes;141props->max_sge = min(dev->dev->caps.max_sq_sg,142dev->dev->caps.max_rq_sg);143props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;144props->max_cqe = dev->dev->caps.max_cqes;145props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;146props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;147props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;148props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;149props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;150props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;151props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;152props->max_srq_sge = dev->dev->caps.max_srq_sge;153props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;154props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;155props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?156IB_ATOMIC_HCA : IB_ATOMIC_NONE;157props->masked_atomic_cap = IB_ATOMIC_HCA;158props->max_pkeys = dev->dev->caps.pkey_table_len[1];159props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;160props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;161props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *162props->max_mcast_grp;163props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1;164165out:166kfree(in_mad);167kfree(out_mad);168169return err;170}171172static enum rdma_link_layer173mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)174{175struct mlx4_dev *dev = to_mdev(device)->dev;176177return dev->caps.port_mask & (1 << (port_num - 1)) ?178IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;179}180181static int ib_link_query_port(struct ib_device *ibdev, u8 port,182struct ib_port_attr *props,183struct ib_smp *out_mad)184{185props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));186props->lmc = out_mad->data[34] & 0x7;187props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));188props->sm_sl = out_mad->data[36] & 0xf;189props->state = out_mad->data[32] & 0xf;190props->phys_state = out_mad->data[33] >> 4;191props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));192props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];193props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;194props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];195props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));196props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));197props->active_width = out_mad->data[31] & 0xf;198props->active_speed = out_mad->data[35] >> 4;199props->max_mtu = out_mad->data[41] & 0xf;200props->active_mtu = out_mad->data[36] >> 4;201props->subnet_timeout = out_mad->data[51] & 0x1f;202props->max_vl_num = out_mad->data[37] >> 4;203props->init_type_reply = out_mad->data[41] >> 4;204205return 0;206}207208static u8 state_to_phys_state(enum ib_port_state state)209{210return state == IB_PORT_ACTIVE ? 5 : 3;211}212213static int eth_link_query_port(struct ib_device *ibdev, u8 port,214struct ib_port_attr *props,215struct ib_smp *out_mad)216{217struct mlx4_ib_iboe *iboe = &to_mdev(ibdev)->iboe;218struct net_device *ndev;219enum ib_mtu tmp;220221props->active_width = IB_WIDTH_1X;222props->active_speed = 4;223props->port_cap_flags = IB_PORT_CM_SUP;224props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];225props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;226props->pkey_tbl_len = 1;227props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));228props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));229props->max_mtu = IB_MTU_2048;230props->subnet_timeout = 0;231props->max_vl_num = out_mad->data[37] >> 4;232props->init_type_reply = 0;233props->state = IB_PORT_DOWN;234props->phys_state = state_to_phys_state(props->state);235props->active_mtu = IB_MTU_256;236spin_lock(&iboe->lock);237ndev = iboe->netdevs[port - 1];238if (!ndev)239goto out;240241tmp = iboe_get_mtu(ndev->mtu);242props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;243244props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?245IB_PORT_ACTIVE : IB_PORT_DOWN;246props->phys_state = state_to_phys_state(props->state);247248out:249spin_unlock(&iboe->lock);250return 0;251}252253static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,254struct ib_port_attr *props)255{256struct ib_smp *in_mad = NULL;257struct ib_smp *out_mad = NULL;258int err = -ENOMEM;259260in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);261out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);262if (!in_mad || !out_mad)263goto out;264265memset(props, 0, sizeof *props);266267init_query_mad(in_mad);268in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;269in_mad->attr_mod = cpu_to_be32(port);270271err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);272if (err)273goto out;274275err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?276ib_link_query_port(ibdev, port, props, out_mad) :277eth_link_query_port(ibdev, port, props, out_mad);278279out:280kfree(in_mad);281kfree(out_mad);282283return err;284}285286static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,287union ib_gid *gid)288{289struct ib_smp *in_mad = NULL;290struct ib_smp *out_mad = NULL;291int err = -ENOMEM;292293in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);294out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);295if (!in_mad || !out_mad)296goto out;297298init_query_mad(in_mad);299in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;300in_mad->attr_mod = cpu_to_be32(port);301302err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);303if (err)304goto out;305306memcpy(gid->raw, out_mad->data + 8, 8);307308init_query_mad(in_mad);309in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;310in_mad->attr_mod = cpu_to_be32(index / 8);311312err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);313if (err)314goto out;315316memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);317318out:319kfree(in_mad);320kfree(out_mad);321return err;322}323324static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,325union ib_gid *gid)326{327struct mlx4_ib_dev *dev = to_mdev(ibdev);328329*gid = dev->iboe.gid_table[port - 1][index];330331return 0;332}333334static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,335union ib_gid *gid)336{337if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)338return __mlx4_ib_query_gid(ibdev, port, index, gid);339else340return iboe_query_gid(ibdev, port, index, gid);341}342343static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,344u16 *pkey)345{346struct ib_smp *in_mad = NULL;347struct ib_smp *out_mad = NULL;348int err = -ENOMEM;349350in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);351out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);352if (!in_mad || !out_mad)353goto out;354355init_query_mad(in_mad);356in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;357in_mad->attr_mod = cpu_to_be32(index / 32);358359err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);360if (err)361goto out;362363*pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);364365out:366kfree(in_mad);367kfree(out_mad);368return err;369}370371static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,372struct ib_device_modify *props)373{374struct mlx4_cmd_mailbox *mailbox;375376if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)377return -EOPNOTSUPP;378379if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))380return 0;381382spin_lock(&to_mdev(ibdev)->sm_lock);383memcpy(ibdev->node_desc, props->node_desc, 64);384spin_unlock(&to_mdev(ibdev)->sm_lock);385386/*387* If possible, pass node desc to FW, so it can generate388* a 144 trap. If cmd fails, just ignore.389*/390mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);391if (IS_ERR(mailbox))392return 0;393394memset(mailbox->buf, 0, 256);395memcpy(mailbox->buf, props->node_desc, 64);396mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,397MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A);398399mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);400401return 0;402}403404static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,405u32 cap_mask)406{407struct mlx4_cmd_mailbox *mailbox;408int err;409u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;410411mailbox = mlx4_alloc_cmd_mailbox(dev->dev);412if (IS_ERR(mailbox))413return PTR_ERR(mailbox);414415memset(mailbox->buf, 0, 256);416417if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {418*(u8 *) mailbox->buf = !!reset_qkey_viols << 6;419((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);420} else {421((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;422((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);423}424425err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,426MLX4_CMD_TIME_CLASS_B);427428mlx4_free_cmd_mailbox(dev->dev, mailbox);429return err;430}431432static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,433struct ib_port_modify *props)434{435struct ib_port_attr attr;436u32 cap_mask;437int err;438439mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);440441err = mlx4_ib_query_port(ibdev, port, &attr);442if (err)443goto out;444445cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &446~props->clr_port_cap_mask;447448err = mlx4_SET_PORT(to_mdev(ibdev), port,449!!(mask & IB_PORT_RESET_QKEY_CNTR),450cap_mask);451452out:453mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);454return err;455}456457static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,458struct ib_udata *udata)459{460struct mlx4_ib_dev *dev = to_mdev(ibdev);461struct mlx4_ib_ucontext *context;462struct mlx4_ib_alloc_ucontext_resp resp;463int err;464465if (!dev->ib_active)466return ERR_PTR(-EAGAIN);467468resp.qp_tab_size = dev->dev->caps.num_qps;469resp.bf_reg_size = dev->dev->caps.bf_reg_size;470resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;471472context = kmalloc(sizeof *context, GFP_KERNEL);473if (!context)474return ERR_PTR(-ENOMEM);475476err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);477if (err) {478kfree(context);479return ERR_PTR(err);480}481482INIT_LIST_HEAD(&context->db_page_list);483mutex_init(&context->db_page_mutex);484485err = ib_copy_to_udata(udata, &resp, sizeof resp);486if (err) {487mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);488kfree(context);489return ERR_PTR(-EFAULT);490}491492return &context->ibucontext;493}494495static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)496{497struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);498499mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);500kfree(context);501502return 0;503}504505static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)506{507struct mlx4_ib_dev *dev = to_mdev(context->device);508509if (vma->vm_end - vma->vm_start != PAGE_SIZE)510return -EINVAL;511512if (vma->vm_pgoff == 0) {513vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);514515if (io_remap_pfn_range(vma, vma->vm_start,516to_mucontext(context)->uar.pfn,517PAGE_SIZE, vma->vm_page_prot))518return -EAGAIN;519} else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {520vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);521522if (io_remap_pfn_range(vma, vma->vm_start,523to_mucontext(context)->uar.pfn +524dev->dev->caps.num_uars,525PAGE_SIZE, vma->vm_page_prot))526return -EAGAIN;527} else528return -EINVAL;529530return 0;531}532533static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,534struct ib_ucontext *context,535struct ib_udata *udata)536{537struct mlx4_ib_pd *pd;538int err;539540pd = kmalloc(sizeof *pd, GFP_KERNEL);541if (!pd)542return ERR_PTR(-ENOMEM);543544err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);545if (err) {546kfree(pd);547return ERR_PTR(err);548}549550if (context)551if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {552mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);553kfree(pd);554return ERR_PTR(-EFAULT);555}556557return &pd->ibpd;558}559560static int mlx4_ib_dealloc_pd(struct ib_pd *pd)561{562mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);563kfree(pd);564565return 0;566}567568static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)569{570struct mlx4_ib_qp *mqp = to_mqp(ibqp);571struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);572struct mlx4_ib_gid_entry *ge;573574ge = kzalloc(sizeof *ge, GFP_KERNEL);575if (!ge)576return -ENOMEM;577578ge->gid = *gid;579if (mlx4_ib_add_mc(mdev, mqp, gid)) {580ge->port = mqp->port;581ge->added = 1;582}583584mutex_lock(&mqp->mutex);585list_add_tail(&ge->list, &mqp->gid_list);586mutex_unlock(&mqp->mutex);587588return 0;589}590591int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,592union ib_gid *gid)593{594u8 mac[6];595struct net_device *ndev;596int ret = 0;597598if (!mqp->port)599return 0;600601spin_lock(&mdev->iboe.lock);602ndev = mdev->iboe.netdevs[mqp->port - 1];603if (ndev)604dev_hold(ndev);605spin_unlock(&mdev->iboe.lock);606607if (ndev) {608rdma_get_mcast_mac((struct in6_addr *)gid, mac);609rtnl_lock();610dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);611ret = 1;612rtnl_unlock();613dev_put(ndev);614}615616return ret;617}618619static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)620{621int err;622struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);623struct mlx4_ib_qp *mqp = to_mqp(ibqp);624625err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,626!!(mqp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),627MLX4_PROT_IB_IPV6);628if (err)629return err;630631err = add_gid_entry(ibqp, gid);632if (err)633goto err_add;634635return 0;636637err_add:638mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);639return err;640}641642static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)643{644struct mlx4_ib_gid_entry *ge;645struct mlx4_ib_gid_entry *tmp;646struct mlx4_ib_gid_entry *ret = NULL;647648list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {649if (!memcmp(raw, ge->gid.raw, 16)) {650ret = ge;651break;652}653}654655return ret;656}657658static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)659{660int err;661struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);662struct mlx4_ib_qp *mqp = to_mqp(ibqp);663u8 mac[6];664struct net_device *ndev;665struct mlx4_ib_gid_entry *ge;666667err = mlx4_multicast_detach(mdev->dev,668&mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);669if (err)670return err;671672mutex_lock(&mqp->mutex);673ge = find_gid_entry(mqp, gid->raw);674if (ge) {675spin_lock(&mdev->iboe.lock);676ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;677if (ndev)678dev_hold(ndev);679spin_unlock(&mdev->iboe.lock);680rdma_get_mcast_mac((struct in6_addr *)gid, mac);681if (ndev) {682rtnl_lock();683dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);684rtnl_unlock();685dev_put(ndev);686}687list_del(&ge->list);688kfree(ge);689} else690printk(KERN_WARNING "could not find mgid entry\n");691692mutex_unlock(&mqp->mutex);693694return 0;695}696697static int init_node_data(struct mlx4_ib_dev *dev)698{699struct ib_smp *in_mad = NULL;700struct ib_smp *out_mad = NULL;701int err = -ENOMEM;702703in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);704out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);705if (!in_mad || !out_mad)706goto out;707708init_query_mad(in_mad);709in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;710711err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);712if (err)713goto out;714715memcpy(dev->ib_dev.node_desc, out_mad->data, 64);716717in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;718719err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);720if (err)721goto out;722723memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);724725out:726kfree(in_mad);727kfree(out_mad);728return err;729}730731static ssize_t show_hca(struct device *device, struct device_attribute *attr,732char *buf)733{734struct mlx4_ib_dev *dev =735container_of(device, struct mlx4_ib_dev, ib_dev.dev);736return sprintf(buf, "MT%d\n", dev->dev->pdev->device);737}738739static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,740char *buf)741{742struct mlx4_ib_dev *dev =743container_of(device, struct mlx4_ib_dev, ib_dev.dev);744return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),745(int) (dev->dev->caps.fw_ver >> 16) & 0xffff,746(int) dev->dev->caps.fw_ver & 0xffff);747}748749static ssize_t show_rev(struct device *device, struct device_attribute *attr,750char *buf)751{752struct mlx4_ib_dev *dev =753container_of(device, struct mlx4_ib_dev, ib_dev.dev);754return sprintf(buf, "%x\n", dev->dev->rev_id);755}756757static ssize_t show_board(struct device *device, struct device_attribute *attr,758char *buf)759{760struct mlx4_ib_dev *dev =761container_of(device, struct mlx4_ib_dev, ib_dev.dev);762return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,763dev->dev->board_id);764}765766static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);767static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);768static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);769static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);770771static struct device_attribute *mlx4_class_attributes[] = {772&dev_attr_hw_rev,773&dev_attr_fw_ver,774&dev_attr_hca_type,775&dev_attr_board_id776};777778static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)779{780memcpy(eui, dev->dev_addr, 3);781memcpy(eui + 5, dev->dev_addr + 3, 3);782if (vlan_id < 0x1000) {783eui[3] = vlan_id >> 8;784eui[4] = vlan_id & 0xff;785} else {786eui[3] = 0xff;787eui[4] = 0xfe;788}789eui[0] ^= 2;790}791792static void update_gids_task(struct work_struct *work)793{794struct update_gid_work *gw = container_of(work, struct update_gid_work, work);795struct mlx4_cmd_mailbox *mailbox;796union ib_gid *gids;797int err;798struct mlx4_dev *dev = gw->dev->dev;799struct ib_event event;800801mailbox = mlx4_alloc_cmd_mailbox(dev);802if (IS_ERR(mailbox)) {803printk(KERN_WARNING "update gid table failed %ld\n", PTR_ERR(mailbox));804return;805}806807gids = mailbox->buf;808memcpy(gids, gw->gids, sizeof gw->gids);809810err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,8111, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B);812if (err)813printk(KERN_WARNING "set port command failed\n");814else {815memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);816event.device = &gw->dev->ib_dev;817event.element.port_num = gw->port;818event.event = IB_EVENT_LID_CHANGE;819ib_dispatch_event(&event);820}821822mlx4_free_cmd_mailbox(dev, mailbox);823kfree(gw);824}825826static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)827{828struct net_device *ndev = dev->iboe.netdevs[port - 1];829struct update_gid_work *work;830struct net_device *tmp;831int i;832u8 *hits;833int ret;834union ib_gid gid;835int free;836int found;837int need_update = 0;838u16 vid;839840work = kzalloc(sizeof *work, GFP_ATOMIC);841if (!work)842return -ENOMEM;843844hits = kzalloc(128, GFP_ATOMIC);845if (!hits) {846ret = -ENOMEM;847goto out;848}849850rcu_read_lock();851for_each_netdev_rcu(&init_net, tmp) {852if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {853gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);854vid = rdma_vlan_dev_vlan_id(tmp);855mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);856found = 0;857free = -1;858for (i = 0; i < 128; ++i) {859if (free < 0 &&860!memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))861free = i;862if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {863hits[i] = 1;864found = 1;865break;866}867}868869if (!found) {870if (tmp == ndev &&871(memcmp(&dev->iboe.gid_table[port - 1][0],872&gid, sizeof gid) ||873!memcmp(&dev->iboe.gid_table[port - 1][0],874&zgid, sizeof gid))) {875dev->iboe.gid_table[port - 1][0] = gid;876++need_update;877hits[0] = 1;878} else if (free >= 0) {879dev->iboe.gid_table[port - 1][free] = gid;880hits[free] = 1;881++need_update;882}883}884}885}886rcu_read_unlock();887888for (i = 0; i < 128; ++i)889if (!hits[i]) {890if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))891++need_update;892dev->iboe.gid_table[port - 1][i] = zgid;893}894895if (need_update) {896memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);897INIT_WORK(&work->work, update_gids_task);898work->port = port;899work->dev = dev;900queue_work(wq, &work->work);901} else902kfree(work);903904kfree(hits);905return 0;906907out:908kfree(work);909return ret;910}911912static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)913{914switch (event) {915case NETDEV_UP:916case NETDEV_CHANGEADDR:917update_ipv6_gids(dev, port, 0);918break;919920case NETDEV_DOWN:921update_ipv6_gids(dev, port, 1);922dev->iboe.netdevs[port - 1] = NULL;923}924}925926static void netdev_added(struct mlx4_ib_dev *dev, int port)927{928update_ipv6_gids(dev, port, 0);929}930931static void netdev_removed(struct mlx4_ib_dev *dev, int port)932{933update_ipv6_gids(dev, port, 1);934}935936static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,937void *ptr)938{939struct net_device *dev = ptr;940struct mlx4_ib_dev *ibdev;941struct net_device *oldnd;942struct mlx4_ib_iboe *iboe;943int port;944945if (!net_eq(dev_net(dev), &init_net))946return NOTIFY_DONE;947948ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);949iboe = &ibdev->iboe;950951spin_lock(&iboe->lock);952mlx4_foreach_ib_transport_port(port, ibdev->dev) {953oldnd = iboe->netdevs[port - 1];954iboe->netdevs[port - 1] =955mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);956if (oldnd != iboe->netdevs[port - 1]) {957if (iboe->netdevs[port - 1])958netdev_added(ibdev, port);959else960netdev_removed(ibdev, port);961}962}963964if (dev == iboe->netdevs[0] ||965(iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))966handle_en_event(ibdev, 1, event);967else if (dev == iboe->netdevs[1]968|| (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))969handle_en_event(ibdev, 2, event);970971spin_unlock(&iboe->lock);972973return NOTIFY_DONE;974}975976static void *mlx4_ib_add(struct mlx4_dev *dev)977{978struct mlx4_ib_dev *ibdev;979int num_ports = 0;980int i;981int err;982struct mlx4_ib_iboe *iboe;983984printk_once(KERN_INFO "%s", mlx4_ib_version);985986mlx4_foreach_ib_transport_port(i, dev)987num_ports++;988989/* No point in registering a device with no ports... */990if (num_ports == 0)991return NULL;992993ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);994if (!ibdev) {995dev_err(&dev->pdev->dev, "Device struct alloc failed\n");996return NULL;997}998999iboe = &ibdev->iboe;10001001if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))1002goto err_dealloc;10031004if (mlx4_uar_alloc(dev, &ibdev->priv_uar))1005goto err_pd;10061007ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,1008PAGE_SIZE);1009if (!ibdev->uar_map)1010goto err_uar;1011MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);10121013ibdev->dev = dev;10141015strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);1016ibdev->ib_dev.owner = THIS_MODULE;1017ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;1018ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;1019ibdev->num_ports = num_ports;1020ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;1021ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;1022ibdev->ib_dev.dma_device = &dev->pdev->dev;10231024ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;1025ibdev->ib_dev.uverbs_cmd_mask =1026(1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |1027(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |1028(1ull << IB_USER_VERBS_CMD_QUERY_PORT) |1029(1ull << IB_USER_VERBS_CMD_ALLOC_PD) |1030(1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |1031(1ull << IB_USER_VERBS_CMD_REG_MR) |1032(1ull << IB_USER_VERBS_CMD_DEREG_MR) |1033(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |1034(1ull << IB_USER_VERBS_CMD_CREATE_CQ) |1035(1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |1036(1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |1037(1ull << IB_USER_VERBS_CMD_CREATE_QP) |1038(1ull << IB_USER_VERBS_CMD_MODIFY_QP) |1039(1ull << IB_USER_VERBS_CMD_QUERY_QP) |1040(1ull << IB_USER_VERBS_CMD_DESTROY_QP) |1041(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |1042(1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |1043(1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |1044(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |1045(1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |1046(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);10471048ibdev->ib_dev.query_device = mlx4_ib_query_device;1049ibdev->ib_dev.query_port = mlx4_ib_query_port;1050ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;1051ibdev->ib_dev.query_gid = mlx4_ib_query_gid;1052ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;1053ibdev->ib_dev.modify_device = mlx4_ib_modify_device;1054ibdev->ib_dev.modify_port = mlx4_ib_modify_port;1055ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;1056ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;1057ibdev->ib_dev.mmap = mlx4_ib_mmap;1058ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;1059ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;1060ibdev->ib_dev.create_ah = mlx4_ib_create_ah;1061ibdev->ib_dev.query_ah = mlx4_ib_query_ah;1062ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;1063ibdev->ib_dev.create_srq = mlx4_ib_create_srq;1064ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;1065ibdev->ib_dev.query_srq = mlx4_ib_query_srq;1066ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;1067ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;1068ibdev->ib_dev.create_qp = mlx4_ib_create_qp;1069ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;1070ibdev->ib_dev.query_qp = mlx4_ib_query_qp;1071ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;1072ibdev->ib_dev.post_send = mlx4_ib_post_send;1073ibdev->ib_dev.post_recv = mlx4_ib_post_recv;1074ibdev->ib_dev.create_cq = mlx4_ib_create_cq;1075ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;1076ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;1077ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;1078ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;1079ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;1080ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;1081ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;1082ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;1083ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;1084ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;1085ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;1086ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;1087ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;1088ibdev->ib_dev.process_mad = mlx4_ib_process_mad;10891090ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;1091ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;1092ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;1093ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;10941095spin_lock_init(&iboe->lock);10961097if (init_node_data(ibdev))1098goto err_map;10991100spin_lock_init(&ibdev->sm_lock);1101mutex_init(&ibdev->cap_mask_mutex);11021103if (ib_register_device(&ibdev->ib_dev, NULL))1104goto err_map;11051106if (mlx4_ib_mad_init(ibdev))1107goto err_reg;11081109if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {1110iboe->nb.notifier_call = mlx4_ib_netdev_event;1111err = register_netdevice_notifier(&iboe->nb);1112if (err)1113goto err_reg;1114}11151116for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {1117if (device_create_file(&ibdev->ib_dev.dev,1118mlx4_class_attributes[i]))1119goto err_notif;1120}11211122ibdev->ib_active = true;11231124return ibdev;11251126err_notif:1127if (unregister_netdevice_notifier(&ibdev->iboe.nb))1128printk(KERN_WARNING "failure unregistering notifier\n");1129flush_workqueue(wq);11301131err_reg:1132ib_unregister_device(&ibdev->ib_dev);11331134err_map:1135iounmap(ibdev->uar_map);11361137err_uar:1138mlx4_uar_free(dev, &ibdev->priv_uar);11391140err_pd:1141mlx4_pd_free(dev, ibdev->priv_pdn);11421143err_dealloc:1144ib_dealloc_device(&ibdev->ib_dev);11451146return NULL;1147}11481149static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)1150{1151struct mlx4_ib_dev *ibdev = ibdev_ptr;1152int p;11531154mlx4_ib_mad_cleanup(ibdev);1155ib_unregister_device(&ibdev->ib_dev);1156if (ibdev->iboe.nb.notifier_call) {1157if (unregister_netdevice_notifier(&ibdev->iboe.nb))1158printk(KERN_WARNING "failure unregistering notifier\n");1159ibdev->iboe.nb.notifier_call = NULL;1160}1161iounmap(ibdev->uar_map);11621163mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)1164mlx4_CLOSE_PORT(dev, p);11651166mlx4_uar_free(dev, &ibdev->priv_uar);1167mlx4_pd_free(dev, ibdev->priv_pdn);1168ib_dealloc_device(&ibdev->ib_dev);1169}11701171static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,1172enum mlx4_dev_event event, int port)1173{1174struct ib_event ibev;1175struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);11761177if (port > ibdev->num_ports)1178return;11791180switch (event) {1181case MLX4_DEV_EVENT_PORT_UP:1182ibev.event = IB_EVENT_PORT_ACTIVE;1183break;11841185case MLX4_DEV_EVENT_PORT_DOWN:1186ibev.event = IB_EVENT_PORT_ERR;1187break;11881189case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:1190ibdev->ib_active = false;1191ibev.event = IB_EVENT_DEVICE_FATAL;1192break;11931194default:1195return;1196}11971198ibev.device = ibdev_ptr;1199ibev.element.port_num = port;12001201ib_dispatch_event(&ibev);1202}12031204static struct mlx4_interface mlx4_ib_interface = {1205.add = mlx4_ib_add,1206.remove = mlx4_ib_remove,1207.event = mlx4_ib_event,1208.protocol = MLX4_PROT_IB_IPV61209};12101211static int __init mlx4_ib_init(void)1212{1213int err;12141215wq = create_singlethread_workqueue("mlx4_ib");1216if (!wq)1217return -ENOMEM;12181219err = mlx4_register_interface(&mlx4_ib_interface);1220if (err) {1221destroy_workqueue(wq);1222return err;1223}12241225return 0;1226}12271228static void __exit mlx4_ib_cleanup(void)1229{1230mlx4_unregister_interface(&mlx4_ib_interface);1231destroy_workqueue(wq);1232}12331234module_init(mlx4_ib_init);1235module_exit(mlx4_ib_cleanup);123612371238