linux/drivers/infiniband/hw/hns/hns_roce_cmd.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2016 Hisilicon Limited.
   3 *
   4 * This software is available to you under a choice of one of two
   5 * licenses.  You may choose to be licensed under the terms of the GNU
   6 * General Public License (GPL) Version 2, available from the file
   7 * COPYING in the main directory of this source tree, or the
   8 * OpenIB.org BSD license below:
   9 *
  10 *     Redistribution and use in source and binary forms, with or
  11 *     without modification, are permitted provided that the following
  12 *     conditions are met:
  13 *
  14 *      - Redistributions of source code must retain the above
  15 *        copyright notice, this list of conditions and the following
  16 *        disclaimer.
  17 *
  18 *      - Redistributions in binary form must reproduce the above
  19 *        copyright notice, this list of conditions and the following
  20 *        disclaimer in the documentation and/or other materials
  21 *        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 OF
  25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30 * SOFTWARE.
  31 */
  32
  33#include <linux/dmapool.h>
  34#include <linux/platform_device.h>
  35#include "hns_roce_common.h"
  36#include "hns_roce_device.h"
  37#include "hns_roce_cmd.h"
  38
  39#define CMD_POLL_TOKEN 0xffff
  40#define CMD_MAX_NUM 32
  41
  42static int hns_roce_cmd_mbox_post_hw(struct hns_roce_dev *hr_dev, u64 in_param,
  43                                     u64 out_param, u32 in_modifier,
  44                                     u8 op_modifier, u16 op, u16 token,
  45                                     int event)
  46{
  47        return hr_dev->hw->post_mbox(hr_dev, in_param, out_param, in_modifier,
  48                                     op_modifier, op, token, event);
  49}
  50
  51/* this should be called with "poll_sem" */
  52static int __hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
  53                                    u64 out_param, unsigned long in_modifier,
  54                                    u8 op_modifier, u16 op,
  55                                    unsigned int timeout)
  56{
  57        int ret;
  58
  59        ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
  60                                        in_modifier, op_modifier, op,
  61                                        CMD_POLL_TOKEN, 0);
  62        if (ret) {
  63                dev_err_ratelimited(hr_dev->dev,
  64                                    "failed to post mailbox %x in poll mode, ret = %d.\n",
  65                                    op, ret);
  66                return ret;
  67        }
  68
  69        return hr_dev->hw->poll_mbox_done(hr_dev, timeout);
  70}
  71
  72static int hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
  73                                  u64 out_param, unsigned long in_modifier,
  74                                  u8 op_modifier, u16 op, unsigned int timeout)
  75{
  76        int ret;
  77
  78        down(&hr_dev->cmd.poll_sem);
  79        ret = __hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param, in_modifier,
  80                                       op_modifier, op, timeout);
  81        up(&hr_dev->cmd.poll_sem);
  82
  83        return ret;
  84}
  85
  86void hns_roce_cmd_event(struct hns_roce_dev *hr_dev, u16 token, u8 status,
  87                        u64 out_param)
  88{
  89        struct hns_roce_cmd_context *context =
  90                &hr_dev->cmd.context[token % hr_dev->cmd.max_cmds];
  91
  92        if (unlikely(token != context->token)) {
  93                dev_err_ratelimited(hr_dev->dev,
  94                                    "[cmd] invalid ae token %x,context token is %x!\n",
  95                                    token, context->token);
  96                return;
  97        }
  98
  99        context->result = (status == HNS_ROCE_CMD_SUCCESS) ? 0 : (-EIO);
 100        context->out_param = out_param;
 101        complete(&context->done);
 102}
 103
 104static int __hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
 105                                    u64 out_param, unsigned long in_modifier,
 106                                    u8 op_modifier, u16 op,
 107                                    unsigned int timeout)
 108{
 109        struct hns_roce_cmdq *cmd = &hr_dev->cmd;
 110        struct hns_roce_cmd_context *context;
 111        struct device *dev = hr_dev->dev;
 112        int ret;
 113
 114        spin_lock(&cmd->context_lock);
 115
 116        do {
 117                context = &cmd->context[cmd->free_head];
 118                cmd->free_head = context->next;
 119        } while (context->busy);
 120
 121        context->busy = 1;
 122        context->token += cmd->max_cmds;
 123
 124        spin_unlock(&cmd->context_lock);
 125
 126        reinit_completion(&context->done);
 127
 128        ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
 129                                        in_modifier, op_modifier, op,
 130                                        context->token, 1);
 131        if (ret) {
 132                dev_err_ratelimited(dev,
 133                                    "failed to post mailbox %x in event mode, ret = %d.\n",
 134                                    op, ret);
 135                goto out;
 136        }
 137
 138        if (!wait_for_completion_timeout(&context->done,
 139                                         msecs_to_jiffies(timeout))) {
 140                dev_err_ratelimited(dev, "[cmd] token %x mailbox %x timeout.\n",
 141                                    context->token, op);
 142                ret = -EBUSY;
 143                goto out;
 144        }
 145
 146        ret = context->result;
 147        if (ret)
 148                dev_err_ratelimited(dev, "[cmd] token %x mailbox %x error %d\n",
 149                                    context->token, op, ret);
 150
 151out:
 152        context->busy = 0;
 153        return ret;
 154}
 155
 156static int hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
 157                                  u64 out_param, unsigned long in_modifier,
 158                                  u8 op_modifier, u16 op, unsigned int timeout)
 159{
 160        int ret;
 161
 162        down(&hr_dev->cmd.event_sem);
 163        ret = __hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param, in_modifier,
 164                                       op_modifier, op, timeout);
 165        up(&hr_dev->cmd.event_sem);
 166
 167        return ret;
 168}
 169
 170int hns_roce_cmd_mbox(struct hns_roce_dev *hr_dev, u64 in_param, u64 out_param,
 171                      unsigned long in_modifier, u8 op_modifier, u16 op,
 172                      unsigned int timeout)
 173{
 174        bool is_busy;
 175
 176        if (hr_dev->hw->chk_mbox_avail)
 177                if (!hr_dev->hw->chk_mbox_avail(hr_dev, &is_busy))
 178                        return is_busy ? -EBUSY : 0;
 179
 180        if (hr_dev->cmd.use_events)
 181                return hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
 182                                              in_modifier, op_modifier, op,
 183                                              timeout);
 184        else
 185                return hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param,
 186                                              in_modifier, op_modifier, op,
 187                                              timeout);
 188}
 189
 190int hns_roce_cmd_init(struct hns_roce_dev *hr_dev)
 191{
 192        sema_init(&hr_dev->cmd.poll_sem, 1);
 193        hr_dev->cmd.use_events = 0;
 194        hr_dev->cmd.max_cmds = CMD_MAX_NUM;
 195        hr_dev->cmd.pool = dma_pool_create("hns_roce_cmd", hr_dev->dev,
 196                                           HNS_ROCE_MAILBOX_SIZE,
 197                                           HNS_ROCE_MAILBOX_SIZE, 0);
 198        if (!hr_dev->cmd.pool)
 199                return -ENOMEM;
 200
 201        return 0;
 202}
 203
 204void hns_roce_cmd_cleanup(struct hns_roce_dev *hr_dev)
 205{
 206        dma_pool_destroy(hr_dev->cmd.pool);
 207}
 208
 209int hns_roce_cmd_use_events(struct hns_roce_dev *hr_dev)
 210{
 211        struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
 212        int i;
 213
 214        hr_cmd->context =
 215                kcalloc(hr_cmd->max_cmds, sizeof(*hr_cmd->context), GFP_KERNEL);
 216        if (!hr_cmd->context) {
 217                hr_dev->cmd_mod = 0;
 218                return -ENOMEM;
 219        }
 220
 221        for (i = 0; i < hr_cmd->max_cmds; ++i) {
 222                hr_cmd->context[i].token = i;
 223                hr_cmd->context[i].next = i + 1;
 224                init_completion(&hr_cmd->context[i].done);
 225        }
 226        hr_cmd->context[hr_cmd->max_cmds - 1].next = 0;
 227        hr_cmd->free_head = 0;
 228
 229        sema_init(&hr_cmd->event_sem, hr_cmd->max_cmds);
 230        spin_lock_init(&hr_cmd->context_lock);
 231
 232        hr_cmd->use_events = 1;
 233
 234        return 0;
 235}
 236
 237void hns_roce_cmd_use_polling(struct hns_roce_dev *hr_dev)
 238{
 239        struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
 240
 241        kfree(hr_cmd->context);
 242        hr_cmd->use_events = 0;
 243}
 244
 245struct hns_roce_cmd_mailbox *
 246hns_roce_alloc_cmd_mailbox(struct hns_roce_dev *hr_dev)
 247{
 248        struct hns_roce_cmd_mailbox *mailbox;
 249
 250        mailbox = kmalloc(sizeof(*mailbox), GFP_KERNEL);
 251        if (!mailbox)
 252                return ERR_PTR(-ENOMEM);
 253
 254        mailbox->buf =
 255                dma_pool_alloc(hr_dev->cmd.pool, GFP_KERNEL, &mailbox->dma);
 256        if (!mailbox->buf) {
 257                kfree(mailbox);
 258                return ERR_PTR(-ENOMEM);
 259        }
 260
 261        return mailbox;
 262}
 263
 264void hns_roce_free_cmd_mailbox(struct hns_roce_dev *hr_dev,
 265                               struct hns_roce_cmd_mailbox *mailbox)
 266{
 267        if (!mailbox)
 268                return;
 269
 270        dma_pool_free(hr_dev->cmd.pool, mailbox->buf, mailbox->dma);
 271        kfree(mailbox);
 272}
 273