Path: blob/master/drivers/infiniband/hw/cxgb4/provider.c
15112 views
/*1* Copyright (c) 2009-2010 Chelsio, 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*/31#include <linux/module.h>32#include <linux/moduleparam.h>33#include <linux/device.h>34#include <linux/netdevice.h>35#include <linux/etherdevice.h>36#include <linux/delay.h>37#include <linux/errno.h>38#include <linux/list.h>39#include <linux/spinlock.h>40#include <linux/ethtool.h>41#include <linux/rtnetlink.h>42#include <linux/inetdevice.h>43#include <linux/io.h>4445#include <asm/irq.h>46#include <asm/byteorder.h>4748#include <rdma/iw_cm.h>49#include <rdma/ib_verbs.h>50#include <rdma/ib_smi.h>51#include <rdma/ib_umem.h>52#include <rdma/ib_user_verbs.h>5354#include "iw_cxgb4.h"5556static int fastreg_support = 1;57module_param(fastreg_support, int, 0644);58MODULE_PARM_DESC(fastreg_support, "Advertise fastreg support (default=1)");5960static int c4iw_modify_port(struct ib_device *ibdev,61u8 port, int port_modify_mask,62struct ib_port_modify *props)63{64return -ENOSYS;65}6667static struct ib_ah *c4iw_ah_create(struct ib_pd *pd,68struct ib_ah_attr *ah_attr)69{70return ERR_PTR(-ENOSYS);71}7273static int c4iw_ah_destroy(struct ib_ah *ah)74{75return -ENOSYS;76}7778static int c4iw_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)79{80return -ENOSYS;81}8283static int c4iw_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)84{85return -ENOSYS;86}8788static int c4iw_process_mad(struct ib_device *ibdev, int mad_flags,89u8 port_num, struct ib_wc *in_wc,90struct ib_grh *in_grh, struct ib_mad *in_mad,91struct ib_mad *out_mad)92{93return -ENOSYS;94}9596static int c4iw_dealloc_ucontext(struct ib_ucontext *context)97{98struct c4iw_dev *rhp = to_c4iw_dev(context->device);99struct c4iw_ucontext *ucontext = to_c4iw_ucontext(context);100struct c4iw_mm_entry *mm, *tmp;101102PDBG("%s context %p\n", __func__, context);103list_for_each_entry_safe(mm, tmp, &ucontext->mmaps, entry)104kfree(mm);105c4iw_release_dev_ucontext(&rhp->rdev, &ucontext->uctx);106kfree(ucontext);107return 0;108}109110static struct ib_ucontext *c4iw_alloc_ucontext(struct ib_device *ibdev,111struct ib_udata *udata)112{113struct c4iw_ucontext *context;114struct c4iw_dev *rhp = to_c4iw_dev(ibdev);115116PDBG("%s ibdev %p\n", __func__, ibdev);117context = kzalloc(sizeof(*context), GFP_KERNEL);118if (!context)119return ERR_PTR(-ENOMEM);120c4iw_init_dev_ucontext(&rhp->rdev, &context->uctx);121INIT_LIST_HEAD(&context->mmaps);122spin_lock_init(&context->mmap_lock);123return &context->ibucontext;124}125126static int c4iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)127{128int len = vma->vm_end - vma->vm_start;129u32 key = vma->vm_pgoff << PAGE_SHIFT;130struct c4iw_rdev *rdev;131int ret = 0;132struct c4iw_mm_entry *mm;133struct c4iw_ucontext *ucontext;134u64 addr;135136PDBG("%s pgoff 0x%lx key 0x%x len %d\n", __func__, vma->vm_pgoff,137key, len);138139if (vma->vm_start & (PAGE_SIZE-1))140return -EINVAL;141142rdev = &(to_c4iw_dev(context->device)->rdev);143ucontext = to_c4iw_ucontext(context);144145mm = remove_mmap(ucontext, key, len);146if (!mm)147return -EINVAL;148addr = mm->addr;149kfree(mm);150151if ((addr >= pci_resource_start(rdev->lldi.pdev, 0)) &&152(addr < (pci_resource_start(rdev->lldi.pdev, 0) +153pci_resource_len(rdev->lldi.pdev, 0)))) {154155/*156* MA_SYNC register...157*/158vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);159ret = io_remap_pfn_range(vma, vma->vm_start,160addr >> PAGE_SHIFT,161len, vma->vm_page_prot);162} else if ((addr >= pci_resource_start(rdev->lldi.pdev, 2)) &&163(addr < (pci_resource_start(rdev->lldi.pdev, 2) +164pci_resource_len(rdev->lldi.pdev, 2)))) {165166/*167* Map user DB or OCQP memory...168*/169if (addr >= rdev->oc_mw_pa)170vma->vm_page_prot = t4_pgprot_wc(vma->vm_page_prot);171else172vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);173ret = io_remap_pfn_range(vma, vma->vm_start,174addr >> PAGE_SHIFT,175len, vma->vm_page_prot);176} else {177178/*179* Map WQ or CQ contig dma memory...180*/181ret = remap_pfn_range(vma, vma->vm_start,182addr >> PAGE_SHIFT,183len, vma->vm_page_prot);184}185186return ret;187}188189static int c4iw_deallocate_pd(struct ib_pd *pd)190{191struct c4iw_dev *rhp;192struct c4iw_pd *php;193194php = to_c4iw_pd(pd);195rhp = php->rhp;196PDBG("%s ibpd %p pdid 0x%x\n", __func__, pd, php->pdid);197c4iw_put_resource(&rhp->rdev.resource.pdid_fifo, php->pdid,198&rhp->rdev.resource.pdid_fifo_lock);199kfree(php);200return 0;201}202203static struct ib_pd *c4iw_allocate_pd(struct ib_device *ibdev,204struct ib_ucontext *context,205struct ib_udata *udata)206{207struct c4iw_pd *php;208u32 pdid;209struct c4iw_dev *rhp;210211PDBG("%s ibdev %p\n", __func__, ibdev);212rhp = (struct c4iw_dev *) ibdev;213pdid = c4iw_get_resource(&rhp->rdev.resource.pdid_fifo,214&rhp->rdev.resource.pdid_fifo_lock);215if (!pdid)216return ERR_PTR(-EINVAL);217php = kzalloc(sizeof(*php), GFP_KERNEL);218if (!php) {219c4iw_put_resource(&rhp->rdev.resource.pdid_fifo, pdid,220&rhp->rdev.resource.pdid_fifo_lock);221return ERR_PTR(-ENOMEM);222}223php->pdid = pdid;224php->rhp = rhp;225if (context) {226if (ib_copy_to_udata(udata, &php->pdid, sizeof(u32))) {227c4iw_deallocate_pd(&php->ibpd);228return ERR_PTR(-EFAULT);229}230}231PDBG("%s pdid 0x%0x ptr 0x%p\n", __func__, pdid, php);232return &php->ibpd;233}234235static int c4iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,236u16 *pkey)237{238PDBG("%s ibdev %p\n", __func__, ibdev);239*pkey = 0;240return 0;241}242243static int c4iw_query_gid(struct ib_device *ibdev, u8 port, int index,244union ib_gid *gid)245{246struct c4iw_dev *dev;247248PDBG("%s ibdev %p, port %d, index %d, gid %p\n",249__func__, ibdev, port, index, gid);250dev = to_c4iw_dev(ibdev);251BUG_ON(port == 0);252memset(&(gid->raw[0]), 0, sizeof(gid->raw));253memcpy(&(gid->raw[0]), dev->rdev.lldi.ports[port-1]->dev_addr, 6);254return 0;255}256257static int c4iw_query_device(struct ib_device *ibdev,258struct ib_device_attr *props)259{260261struct c4iw_dev *dev;262PDBG("%s ibdev %p\n", __func__, ibdev);263264dev = to_c4iw_dev(ibdev);265memset(props, 0, sizeof *props);266memcpy(&props->sys_image_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);267props->hw_ver = dev->rdev.lldi.adapter_type;268props->fw_ver = dev->rdev.lldi.fw_vers;269props->device_cap_flags = dev->device_cap_flags;270props->page_size_cap = T4_PAGESIZE_MASK;271props->vendor_id = (u32)dev->rdev.lldi.pdev->vendor;272props->vendor_part_id = (u32)dev->rdev.lldi.pdev->device;273props->max_mr_size = T4_MAX_MR_SIZE;274props->max_qp = T4_MAX_NUM_QP;275props->max_qp_wr = T4_MAX_QP_DEPTH;276props->max_sge = T4_MAX_RECV_SGE;277props->max_sge_rd = 1;278props->max_qp_rd_atom = c4iw_max_read_depth;279props->max_qp_init_rd_atom = c4iw_max_read_depth;280props->max_cq = T4_MAX_NUM_CQ;281props->max_cqe = T4_MAX_CQ_DEPTH;282props->max_mr = c4iw_num_stags(&dev->rdev);283props->max_pd = T4_MAX_NUM_PD;284props->local_ca_ack_delay = 0;285props->max_fast_reg_page_list_len = T4_MAX_FR_DEPTH;286287return 0;288}289290static int c4iw_query_port(struct ib_device *ibdev, u8 port,291struct ib_port_attr *props)292{293struct c4iw_dev *dev;294struct net_device *netdev;295struct in_device *inetdev;296297PDBG("%s ibdev %p\n", __func__, ibdev);298299dev = to_c4iw_dev(ibdev);300netdev = dev->rdev.lldi.ports[port-1];301302memset(props, 0, sizeof(struct ib_port_attr));303props->max_mtu = IB_MTU_4096;304if (netdev->mtu >= 4096)305props->active_mtu = IB_MTU_4096;306else if (netdev->mtu >= 2048)307props->active_mtu = IB_MTU_2048;308else if (netdev->mtu >= 1024)309props->active_mtu = IB_MTU_1024;310else if (netdev->mtu >= 512)311props->active_mtu = IB_MTU_512;312else313props->active_mtu = IB_MTU_256;314315if (!netif_carrier_ok(netdev))316props->state = IB_PORT_DOWN;317else {318inetdev = in_dev_get(netdev);319if (inetdev) {320if (inetdev->ifa_list)321props->state = IB_PORT_ACTIVE;322else323props->state = IB_PORT_INIT;324in_dev_put(inetdev);325} else326props->state = IB_PORT_INIT;327}328329props->port_cap_flags =330IB_PORT_CM_SUP |331IB_PORT_SNMP_TUNNEL_SUP |332IB_PORT_REINIT_SUP |333IB_PORT_DEVICE_MGMT_SUP |334IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;335props->gid_tbl_len = 1;336props->pkey_tbl_len = 1;337props->active_width = 2;338props->active_speed = 2;339props->max_msg_sz = -1;340341return 0;342}343344static ssize_t show_rev(struct device *dev, struct device_attribute *attr,345char *buf)346{347struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,348ibdev.dev);349PDBG("%s dev 0x%p\n", __func__, dev);350return sprintf(buf, "%d\n", c4iw_dev->rdev.lldi.adapter_type);351}352353static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,354char *buf)355{356struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,357ibdev.dev);358PDBG("%s dev 0x%p\n", __func__, dev);359360return sprintf(buf, "%u.%u.%u.%u\n",361FW_HDR_FW_VER_MAJOR_GET(c4iw_dev->rdev.lldi.fw_vers),362FW_HDR_FW_VER_MINOR_GET(c4iw_dev->rdev.lldi.fw_vers),363FW_HDR_FW_VER_MICRO_GET(c4iw_dev->rdev.lldi.fw_vers),364FW_HDR_FW_VER_BUILD_GET(c4iw_dev->rdev.lldi.fw_vers));365}366367static ssize_t show_hca(struct device *dev, struct device_attribute *attr,368char *buf)369{370struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,371ibdev.dev);372struct ethtool_drvinfo info;373struct net_device *lldev = c4iw_dev->rdev.lldi.ports[0];374375PDBG("%s dev 0x%p\n", __func__, dev);376lldev->ethtool_ops->get_drvinfo(lldev, &info);377return sprintf(buf, "%s\n", info.driver);378}379380static ssize_t show_board(struct device *dev, struct device_attribute *attr,381char *buf)382{383struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,384ibdev.dev);385PDBG("%s dev 0x%p\n", __func__, dev);386return sprintf(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,387c4iw_dev->rdev.lldi.pdev->device);388}389390static int c4iw_get_mib(struct ib_device *ibdev,391union rdma_protocol_stats *stats)392{393struct tp_tcp_stats v4, v6;394struct c4iw_dev *c4iw_dev = to_c4iw_dev(ibdev);395396cxgb4_get_tcp_stats(c4iw_dev->rdev.lldi.pdev, &v4, &v6);397memset(stats, 0, sizeof *stats);398stats->iw.tcpInSegs = v4.tcpInSegs + v6.tcpInSegs;399stats->iw.tcpOutSegs = v4.tcpOutSegs + v6.tcpOutSegs;400stats->iw.tcpRetransSegs = v4.tcpRetransSegs + v6.tcpRetransSegs;401stats->iw.tcpOutRsts = v4.tcpOutRsts + v6.tcpOutSegs;402403return 0;404}405406static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);407static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);408static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);409static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);410411static struct device_attribute *c4iw_class_attributes[] = {412&dev_attr_hw_rev,413&dev_attr_fw_ver,414&dev_attr_hca_type,415&dev_attr_board_id,416};417418int c4iw_register_device(struct c4iw_dev *dev)419{420int ret;421int i;422423PDBG("%s c4iw_dev %p\n", __func__, dev);424BUG_ON(!dev->rdev.lldi.ports[0]);425strlcpy(dev->ibdev.name, "cxgb4_%d", IB_DEVICE_NAME_MAX);426memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));427memcpy(&dev->ibdev.node_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);428dev->ibdev.owner = THIS_MODULE;429dev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY | IB_DEVICE_MEM_WINDOW;430if (fastreg_support)431dev->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;432dev->ibdev.local_dma_lkey = 0;433dev->ibdev.uverbs_cmd_mask =434(1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |435(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |436(1ull << IB_USER_VERBS_CMD_QUERY_PORT) |437(1ull << IB_USER_VERBS_CMD_ALLOC_PD) |438(1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |439(1ull << IB_USER_VERBS_CMD_REG_MR) |440(1ull << IB_USER_VERBS_CMD_DEREG_MR) |441(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |442(1ull << IB_USER_VERBS_CMD_CREATE_CQ) |443(1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |444(1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |445(1ull << IB_USER_VERBS_CMD_CREATE_QP) |446(1ull << IB_USER_VERBS_CMD_MODIFY_QP) |447(1ull << IB_USER_VERBS_CMD_POLL_CQ) |448(1ull << IB_USER_VERBS_CMD_DESTROY_QP) |449(1ull << IB_USER_VERBS_CMD_POST_SEND) |450(1ull << IB_USER_VERBS_CMD_POST_RECV);451dev->ibdev.node_type = RDMA_NODE_RNIC;452memcpy(dev->ibdev.node_desc, C4IW_NODE_DESC, sizeof(C4IW_NODE_DESC));453dev->ibdev.phys_port_cnt = dev->rdev.lldi.nports;454dev->ibdev.num_comp_vectors = 1;455dev->ibdev.dma_device = &(dev->rdev.lldi.pdev->dev);456dev->ibdev.query_device = c4iw_query_device;457dev->ibdev.query_port = c4iw_query_port;458dev->ibdev.modify_port = c4iw_modify_port;459dev->ibdev.query_pkey = c4iw_query_pkey;460dev->ibdev.query_gid = c4iw_query_gid;461dev->ibdev.alloc_ucontext = c4iw_alloc_ucontext;462dev->ibdev.dealloc_ucontext = c4iw_dealloc_ucontext;463dev->ibdev.mmap = c4iw_mmap;464dev->ibdev.alloc_pd = c4iw_allocate_pd;465dev->ibdev.dealloc_pd = c4iw_deallocate_pd;466dev->ibdev.create_ah = c4iw_ah_create;467dev->ibdev.destroy_ah = c4iw_ah_destroy;468dev->ibdev.create_qp = c4iw_create_qp;469dev->ibdev.modify_qp = c4iw_ib_modify_qp;470dev->ibdev.destroy_qp = c4iw_destroy_qp;471dev->ibdev.create_cq = c4iw_create_cq;472dev->ibdev.destroy_cq = c4iw_destroy_cq;473dev->ibdev.resize_cq = c4iw_resize_cq;474dev->ibdev.poll_cq = c4iw_poll_cq;475dev->ibdev.get_dma_mr = c4iw_get_dma_mr;476dev->ibdev.reg_phys_mr = c4iw_register_phys_mem;477dev->ibdev.rereg_phys_mr = c4iw_reregister_phys_mem;478dev->ibdev.reg_user_mr = c4iw_reg_user_mr;479dev->ibdev.dereg_mr = c4iw_dereg_mr;480dev->ibdev.alloc_mw = c4iw_alloc_mw;481dev->ibdev.bind_mw = c4iw_bind_mw;482dev->ibdev.dealloc_mw = c4iw_dealloc_mw;483dev->ibdev.alloc_fast_reg_mr = c4iw_alloc_fast_reg_mr;484dev->ibdev.alloc_fast_reg_page_list = c4iw_alloc_fastreg_pbl;485dev->ibdev.free_fast_reg_page_list = c4iw_free_fastreg_pbl;486dev->ibdev.attach_mcast = c4iw_multicast_attach;487dev->ibdev.detach_mcast = c4iw_multicast_detach;488dev->ibdev.process_mad = c4iw_process_mad;489dev->ibdev.req_notify_cq = c4iw_arm_cq;490dev->ibdev.post_send = c4iw_post_send;491dev->ibdev.post_recv = c4iw_post_receive;492dev->ibdev.get_protocol_stats = c4iw_get_mib;493dev->ibdev.uverbs_abi_ver = C4IW_UVERBS_ABI_VERSION;494495dev->ibdev.iwcm = kmalloc(sizeof(struct iw_cm_verbs), GFP_KERNEL);496if (!dev->ibdev.iwcm)497return -ENOMEM;498499dev->ibdev.iwcm->connect = c4iw_connect;500dev->ibdev.iwcm->accept = c4iw_accept_cr;501dev->ibdev.iwcm->reject = c4iw_reject_cr;502dev->ibdev.iwcm->create_listen = c4iw_create_listen;503dev->ibdev.iwcm->destroy_listen = c4iw_destroy_listen;504dev->ibdev.iwcm->add_ref = c4iw_qp_add_ref;505dev->ibdev.iwcm->rem_ref = c4iw_qp_rem_ref;506dev->ibdev.iwcm->get_qp = c4iw_get_qp;507508ret = ib_register_device(&dev->ibdev, NULL);509if (ret)510goto bail1;511512for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i) {513ret = device_create_file(&dev->ibdev.dev,514c4iw_class_attributes[i]);515if (ret)516goto bail2;517}518return 0;519bail2:520ib_unregister_device(&dev->ibdev);521bail1:522kfree(dev->ibdev.iwcm);523return ret;524}525526void c4iw_unregister_device(struct c4iw_dev *dev)527{528int i;529530PDBG("%s c4iw_dev %p\n", __func__, dev);531for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i)532device_remove_file(&dev->ibdev.dev,533c4iw_class_attributes[i]);534ib_unregister_device(&dev->ibdev);535kfree(dev->ibdev.iwcm);536return;537}538539540