Path: blob/master/drivers/infiniband/hw/cxgb4/device.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/debugfs.h>3435#include <rdma/ib_verbs.h>3637#include "iw_cxgb4.h"3839#define DRV_VERSION "0.1"4041MODULE_AUTHOR("Steve Wise");42MODULE_DESCRIPTION("Chelsio T4 RDMA Driver");43MODULE_LICENSE("Dual BSD/GPL");44MODULE_VERSION(DRV_VERSION);4546static LIST_HEAD(uld_ctx_list);47static DEFINE_MUTEX(dev_mutex);4849static struct dentry *c4iw_debugfs_root;5051struct c4iw_debugfs_data {52struct c4iw_dev *devp;53char *buf;54int bufsize;55int pos;56};5758static int count_idrs(int id, void *p, void *data)59{60int *countp = data;6162*countp = *countp + 1;63return 0;64}6566static ssize_t debugfs_read(struct file *file, char __user *buf, size_t count,67loff_t *ppos)68{69struct c4iw_debugfs_data *d = file->private_data;7071return simple_read_from_buffer(buf, count, ppos, d->buf, d->pos);72}7374static int dump_qp(int id, void *p, void *data)75{76struct c4iw_qp *qp = p;77struct c4iw_debugfs_data *qpd = data;78int space;79int cc;8081if (id != qp->wq.sq.qid)82return 0;8384space = qpd->bufsize - qpd->pos - 1;85if (space == 0)86return 1;8788if (qp->ep)89cc = snprintf(qpd->buf + qpd->pos, space,90"qp sq id %u rq id %u state %u onchip %u "91"ep tid %u state %u %pI4:%u->%pI4:%u\n",92qp->wq.sq.qid, qp->wq.rq.qid, (int)qp->attr.state,93qp->wq.sq.flags & T4_SQ_ONCHIP,94qp->ep->hwtid, (int)qp->ep->com.state,95&qp->ep->com.local_addr.sin_addr.s_addr,96ntohs(qp->ep->com.local_addr.sin_port),97&qp->ep->com.remote_addr.sin_addr.s_addr,98ntohs(qp->ep->com.remote_addr.sin_port));99else100cc = snprintf(qpd->buf + qpd->pos, space,101"qp sq id %u rq id %u state %u onchip %u\n",102qp->wq.sq.qid, qp->wq.rq.qid,103(int)qp->attr.state,104qp->wq.sq.flags & T4_SQ_ONCHIP);105if (cc < space)106qpd->pos += cc;107return 0;108}109110static int qp_release(struct inode *inode, struct file *file)111{112struct c4iw_debugfs_data *qpd = file->private_data;113if (!qpd) {114printk(KERN_INFO "%s null qpd?\n", __func__);115return 0;116}117kfree(qpd->buf);118kfree(qpd);119return 0;120}121122static int qp_open(struct inode *inode, struct file *file)123{124struct c4iw_debugfs_data *qpd;125int ret = 0;126int count = 1;127128qpd = kmalloc(sizeof *qpd, GFP_KERNEL);129if (!qpd) {130ret = -ENOMEM;131goto out;132}133qpd->devp = inode->i_private;134qpd->pos = 0;135136spin_lock_irq(&qpd->devp->lock);137idr_for_each(&qpd->devp->qpidr, count_idrs, &count);138spin_unlock_irq(&qpd->devp->lock);139140qpd->bufsize = count * 128;141qpd->buf = kmalloc(qpd->bufsize, GFP_KERNEL);142if (!qpd->buf) {143ret = -ENOMEM;144goto err1;145}146147spin_lock_irq(&qpd->devp->lock);148idr_for_each(&qpd->devp->qpidr, dump_qp, qpd);149spin_unlock_irq(&qpd->devp->lock);150151qpd->buf[qpd->pos++] = 0;152file->private_data = qpd;153goto out;154err1:155kfree(qpd);156out:157return ret;158}159160static const struct file_operations qp_debugfs_fops = {161.owner = THIS_MODULE,162.open = qp_open,163.release = qp_release,164.read = debugfs_read,165.llseek = default_llseek,166};167168static int dump_stag(int id, void *p, void *data)169{170struct c4iw_debugfs_data *stagd = data;171int space;172int cc;173174space = stagd->bufsize - stagd->pos - 1;175if (space == 0)176return 1;177178cc = snprintf(stagd->buf + stagd->pos, space, "0x%x\n", id<<8);179if (cc < space)180stagd->pos += cc;181return 0;182}183184static int stag_release(struct inode *inode, struct file *file)185{186struct c4iw_debugfs_data *stagd = file->private_data;187if (!stagd) {188printk(KERN_INFO "%s null stagd?\n", __func__);189return 0;190}191kfree(stagd->buf);192kfree(stagd);193return 0;194}195196static int stag_open(struct inode *inode, struct file *file)197{198struct c4iw_debugfs_data *stagd;199int ret = 0;200int count = 1;201202stagd = kmalloc(sizeof *stagd, GFP_KERNEL);203if (!stagd) {204ret = -ENOMEM;205goto out;206}207stagd->devp = inode->i_private;208stagd->pos = 0;209210spin_lock_irq(&stagd->devp->lock);211idr_for_each(&stagd->devp->mmidr, count_idrs, &count);212spin_unlock_irq(&stagd->devp->lock);213214stagd->bufsize = count * sizeof("0x12345678\n");215stagd->buf = kmalloc(stagd->bufsize, GFP_KERNEL);216if (!stagd->buf) {217ret = -ENOMEM;218goto err1;219}220221spin_lock_irq(&stagd->devp->lock);222idr_for_each(&stagd->devp->mmidr, dump_stag, stagd);223spin_unlock_irq(&stagd->devp->lock);224225stagd->buf[stagd->pos++] = 0;226file->private_data = stagd;227goto out;228err1:229kfree(stagd);230out:231return ret;232}233234static const struct file_operations stag_debugfs_fops = {235.owner = THIS_MODULE,236.open = stag_open,237.release = stag_release,238.read = debugfs_read,239.llseek = default_llseek,240};241242static int setup_debugfs(struct c4iw_dev *devp)243{244struct dentry *de;245246if (!devp->debugfs_root)247return -1;248249de = debugfs_create_file("qps", S_IWUSR, devp->debugfs_root,250(void *)devp, &qp_debugfs_fops);251if (de && de->d_inode)252de->d_inode->i_size = 4096;253254de = debugfs_create_file("stags", S_IWUSR, devp->debugfs_root,255(void *)devp, &stag_debugfs_fops);256if (de && de->d_inode)257de->d_inode->i_size = 4096;258return 0;259}260261void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,262struct c4iw_dev_ucontext *uctx)263{264struct list_head *pos, *nxt;265struct c4iw_qid_list *entry;266267mutex_lock(&uctx->lock);268list_for_each_safe(pos, nxt, &uctx->qpids) {269entry = list_entry(pos, struct c4iw_qid_list, entry);270list_del_init(&entry->entry);271if (!(entry->qid & rdev->qpmask))272c4iw_put_resource(&rdev->resource.qid_fifo, entry->qid,273&rdev->resource.qid_fifo_lock);274kfree(entry);275}276277list_for_each_safe(pos, nxt, &uctx->qpids) {278entry = list_entry(pos, struct c4iw_qid_list, entry);279list_del_init(&entry->entry);280kfree(entry);281}282mutex_unlock(&uctx->lock);283}284285void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev,286struct c4iw_dev_ucontext *uctx)287{288INIT_LIST_HEAD(&uctx->qpids);289INIT_LIST_HEAD(&uctx->cqids);290mutex_init(&uctx->lock);291}292293/* Caller takes care of locking if needed */294static int c4iw_rdev_open(struct c4iw_rdev *rdev)295{296int err;297298c4iw_init_dev_ucontext(rdev, &rdev->uctx);299300/*301* qpshift is the number of bits to shift the qpid left in order302* to get the correct address of the doorbell for that qp.303*/304rdev->qpshift = PAGE_SHIFT - ilog2(rdev->lldi.udb_density);305rdev->qpmask = rdev->lldi.udb_density - 1;306rdev->cqshift = PAGE_SHIFT - ilog2(rdev->lldi.ucq_density);307rdev->cqmask = rdev->lldi.ucq_density - 1;308PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "309"pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x "310"qp qid start %u size %u cq qid start %u size %u\n",311__func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,312rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),313rdev->lldi.vr->pbl.start,314rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,315rdev->lldi.vr->rq.size,316rdev->lldi.vr->qp.start,317rdev->lldi.vr->qp.size,318rdev->lldi.vr->cq.start,319rdev->lldi.vr->cq.size);320PDBG("udb len 0x%x udb base %p db_reg %p gts_reg %p qpshift %lu "321"qpmask 0x%x cqshift %lu cqmask 0x%x\n",322(unsigned)pci_resource_len(rdev->lldi.pdev, 2),323(void *)pci_resource_start(rdev->lldi.pdev, 2),324rdev->lldi.db_reg,325rdev->lldi.gts_reg,326rdev->qpshift, rdev->qpmask,327rdev->cqshift, rdev->cqmask);328329if (c4iw_num_stags(rdev) == 0) {330err = -EINVAL;331goto err1;332}333334err = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);335if (err) {336printk(KERN_ERR MOD "error %d initializing resources\n", err);337goto err1;338}339err = c4iw_pblpool_create(rdev);340if (err) {341printk(KERN_ERR MOD "error %d initializing pbl pool\n", err);342goto err2;343}344err = c4iw_rqtpool_create(rdev);345if (err) {346printk(KERN_ERR MOD "error %d initializing rqt pool\n", err);347goto err3;348}349err = c4iw_ocqp_pool_create(rdev);350if (err) {351printk(KERN_ERR MOD "error %d initializing ocqp pool\n", err);352goto err4;353}354return 0;355err4:356c4iw_rqtpool_destroy(rdev);357err3:358c4iw_pblpool_destroy(rdev);359err2:360c4iw_destroy_resource(&rdev->resource);361err1:362return err;363}364365static void c4iw_rdev_close(struct c4iw_rdev *rdev)366{367c4iw_pblpool_destroy(rdev);368c4iw_rqtpool_destroy(rdev);369c4iw_destroy_resource(&rdev->resource);370}371372struct uld_ctx {373struct list_head entry;374struct cxgb4_lld_info lldi;375struct c4iw_dev *dev;376};377378static void c4iw_remove(struct uld_ctx *ctx)379{380PDBG("%s c4iw_dev %p\n", __func__, ctx->dev);381c4iw_unregister_device(ctx->dev);382c4iw_rdev_close(&ctx->dev->rdev);383idr_destroy(&ctx->dev->cqidr);384idr_destroy(&ctx->dev->qpidr);385idr_destroy(&ctx->dev->mmidr);386iounmap(ctx->dev->rdev.oc_mw_kva);387ib_dealloc_device(&ctx->dev->ibdev);388ctx->dev = NULL;389}390391static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)392{393struct c4iw_dev *devp;394int ret;395396devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));397if (!devp) {398printk(KERN_ERR MOD "Cannot allocate ib device\n");399return ERR_PTR(-ENOMEM);400}401devp->rdev.lldi = *infop;402403devp->rdev.oc_mw_pa = pci_resource_start(devp->rdev.lldi.pdev, 2) +404(pci_resource_len(devp->rdev.lldi.pdev, 2) -405roundup_pow_of_two(devp->rdev.lldi.vr->ocq.size));406devp->rdev.oc_mw_kva = ioremap_wc(devp->rdev.oc_mw_pa,407devp->rdev.lldi.vr->ocq.size);408409PDBG(KERN_INFO MOD "ocq memory: "410"hw_start 0x%x size %u mw_pa 0x%lx mw_kva %p\n",411devp->rdev.lldi.vr->ocq.start, devp->rdev.lldi.vr->ocq.size,412devp->rdev.oc_mw_pa, devp->rdev.oc_mw_kva);413414ret = c4iw_rdev_open(&devp->rdev);415if (ret) {416mutex_unlock(&dev_mutex);417printk(KERN_ERR MOD "Unable to open CXIO rdev err %d\n", ret);418ib_dealloc_device(&devp->ibdev);419return ERR_PTR(ret);420}421422idr_init(&devp->cqidr);423idr_init(&devp->qpidr);424idr_init(&devp->mmidr);425spin_lock_init(&devp->lock);426427if (c4iw_debugfs_root) {428devp->debugfs_root = debugfs_create_dir(429pci_name(devp->rdev.lldi.pdev),430c4iw_debugfs_root);431setup_debugfs(devp);432}433return devp;434}435436static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)437{438struct uld_ctx *ctx;439static int vers_printed;440int i;441442if (!vers_printed++)443printk(KERN_INFO MOD "Chelsio T4 RDMA Driver - version %s\n",444DRV_VERSION);445446ctx = kzalloc(sizeof *ctx, GFP_KERNEL);447if (!ctx) {448ctx = ERR_PTR(-ENOMEM);449goto out;450}451ctx->lldi = *infop;452453PDBG("%s found device %s nchan %u nrxq %u ntxq %u nports %u\n",454__func__, pci_name(ctx->lldi.pdev),455ctx->lldi.nchan, ctx->lldi.nrxq,456ctx->lldi.ntxq, ctx->lldi.nports);457458mutex_lock(&dev_mutex);459list_add_tail(&ctx->entry, &uld_ctx_list);460mutex_unlock(&dev_mutex);461462for (i = 0; i < ctx->lldi.nrxq; i++)463PDBG("rxqid[%u] %u\n", i, ctx->lldi.rxq_ids[i]);464out:465return ctx;466}467468static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,469const struct pkt_gl *gl)470{471struct uld_ctx *ctx = handle;472struct c4iw_dev *dev = ctx->dev;473struct sk_buff *skb;474const struct cpl_act_establish *rpl;475unsigned int opcode;476477if (gl == NULL) {478/* omit RSS and rsp_ctrl at end of descriptor */479unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;480481skb = alloc_skb(256, GFP_ATOMIC);482if (!skb)483goto nomem;484__skb_put(skb, len);485skb_copy_to_linear_data(skb, &rsp[1], len);486} else if (gl == CXGB4_MSG_AN) {487const struct rsp_ctrl *rc = (void *)rsp;488489u32 qid = be32_to_cpu(rc->pldbuflen_qid);490c4iw_ev_handler(dev, qid);491return 0;492} else {493skb = cxgb4_pktgl_to_skb(gl, 128, 128);494if (unlikely(!skb))495goto nomem;496}497498rpl = cplhdr(skb);499opcode = rpl->ot.opcode;500501if (c4iw_handlers[opcode])502c4iw_handlers[opcode](dev, skb);503else504printk(KERN_INFO "%s no handler opcode 0x%x...\n", __func__,505opcode);506507return 0;508nomem:509return -1;510}511512static int c4iw_uld_state_change(void *handle, enum cxgb4_state new_state)513{514struct uld_ctx *ctx = handle;515516PDBG("%s new_state %u\n", __func__, new_state);517switch (new_state) {518case CXGB4_STATE_UP:519printk(KERN_INFO MOD "%s: Up\n", pci_name(ctx->lldi.pdev));520if (!ctx->dev) {521int ret = 0;522523ctx->dev = c4iw_alloc(&ctx->lldi);524if (!IS_ERR(ctx->dev))525ret = c4iw_register_device(ctx->dev);526if (IS_ERR(ctx->dev) || ret)527printk(KERN_ERR MOD528"%s: RDMA registration failed: %d\n",529pci_name(ctx->lldi.pdev), ret);530}531break;532case CXGB4_STATE_DOWN:533printk(KERN_INFO MOD "%s: Down\n",534pci_name(ctx->lldi.pdev));535if (ctx->dev)536c4iw_remove(ctx);537break;538case CXGB4_STATE_START_RECOVERY:539printk(KERN_INFO MOD "%s: Fatal Error\n",540pci_name(ctx->lldi.pdev));541if (ctx->dev) {542struct ib_event event;543544ctx->dev->rdev.flags |= T4_FATAL_ERROR;545memset(&event, 0, sizeof event);546event.event = IB_EVENT_DEVICE_FATAL;547event.device = &ctx->dev->ibdev;548ib_dispatch_event(&event);549c4iw_remove(ctx);550}551break;552case CXGB4_STATE_DETACH:553printk(KERN_INFO MOD "%s: Detach\n",554pci_name(ctx->lldi.pdev));555if (ctx->dev)556c4iw_remove(ctx);557break;558}559return 0;560}561562static struct cxgb4_uld_info c4iw_uld_info = {563.name = DRV_NAME,564.add = c4iw_uld_add,565.rx_handler = c4iw_uld_rx_handler,566.state_change = c4iw_uld_state_change,567};568569static int __init c4iw_init_module(void)570{571int err;572573err = c4iw_cm_init();574if (err)575return err;576577c4iw_debugfs_root = debugfs_create_dir(DRV_NAME, NULL);578if (!c4iw_debugfs_root)579printk(KERN_WARNING MOD580"could not create debugfs entry, continuing\n");581582cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);583584return 0;585}586587static void __exit c4iw_exit_module(void)588{589struct uld_ctx *ctx, *tmp;590591mutex_lock(&dev_mutex);592list_for_each_entry_safe(ctx, tmp, &uld_ctx_list, entry) {593if (ctx->dev)594c4iw_remove(ctx);595kfree(ctx);596}597mutex_unlock(&dev_mutex);598cxgb4_unregister_uld(CXGB4_ULD_RDMA);599c4iw_cm_term();600debugfs_remove_recursive(c4iw_debugfs_root);601}602603module_init(c4iw_init_module);604module_exit(c4iw_exit_module);605606607