linux/drivers/net/ethernet/hisilicon/hns3/hns3vf/hclgevf_mbx.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2// Copyright (c) 2016-2017 Hisilicon Limited.
   3
   4#include "hclge_mbx.h"
   5#include "hclgevf_main.h"
   6#include "hnae3.h"
   7
   8static void hclgevf_reset_mbx_resp_status(struct hclgevf_dev *hdev)
   9{
  10        /* this function should be called with mbx_resp.mbx_mutex held
  11         * to prtect the received_response from race condition
  12         */
  13        hdev->mbx_resp.received_resp  = false;
  14        hdev->mbx_resp.origin_mbx_msg = 0;
  15        hdev->mbx_resp.resp_status    = 0;
  16        memset(hdev->mbx_resp.additional_info, 0, HCLGE_MBX_MAX_RESP_DATA_SIZE);
  17}
  18
  19/* hclgevf_get_mbx_resp: used to get a response from PF after VF sends a mailbox
  20 * message to PF.
  21 * @hdev: pointer to struct hclgevf_dev
  22 * @resp_msg: pointer to store the original message type and response status
  23 * @len: the resp_msg data array length.
  24 */
  25static int hclgevf_get_mbx_resp(struct hclgevf_dev *hdev, u16 code0, u16 code1,
  26                                u8 *resp_data, u16 resp_len)
  27{
  28#define HCLGEVF_MAX_TRY_TIMES   500
  29#define HCLGEVF_SLEEP_USECOND   1000
  30        struct hclgevf_mbx_resp_status *mbx_resp;
  31        u16 r_code0, r_code1;
  32        int i = 0;
  33
  34        if (resp_len > HCLGE_MBX_MAX_RESP_DATA_SIZE) {
  35                dev_err(&hdev->pdev->dev,
  36                        "VF mbx response len(=%d) exceeds maximum(=%d)\n",
  37                        resp_len,
  38                        HCLGE_MBX_MAX_RESP_DATA_SIZE);
  39                return -EINVAL;
  40        }
  41
  42        while ((!hdev->mbx_resp.received_resp) && (i < HCLGEVF_MAX_TRY_TIMES)) {
  43                if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state))
  44                        return -EIO;
  45
  46                usleep_range(HCLGEVF_SLEEP_USECOND, HCLGEVF_SLEEP_USECOND * 2);
  47                i++;
  48        }
  49
  50        if (i >= HCLGEVF_MAX_TRY_TIMES) {
  51                dev_err(&hdev->pdev->dev,
  52                        "VF could not get mbx resp(=%d) from PF in %d tries\n",
  53                        hdev->mbx_resp.received_resp, i);
  54                return -EIO;
  55        }
  56
  57        mbx_resp = &hdev->mbx_resp;
  58        r_code0 = (u16)(mbx_resp->origin_mbx_msg >> 16);
  59        r_code1 = (u16)(mbx_resp->origin_mbx_msg & 0xff);
  60
  61        if (mbx_resp->resp_status)
  62                return mbx_resp->resp_status;
  63
  64        if (resp_data)
  65                memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
  66
  67        hclgevf_reset_mbx_resp_status(hdev);
  68
  69        if (!(r_code0 == code0 && r_code1 == code1 && !mbx_resp->resp_status)) {
  70                dev_err(&hdev->pdev->dev,
  71                        "VF could not match resp code(code0=%d,code1=%d), %d",
  72                        code0, code1, mbx_resp->resp_status);
  73                return -EIO;
  74        }
  75
  76        return 0;
  77}
  78
  79int hclgevf_send_mbx_msg(struct hclgevf_dev *hdev, u16 code, u16 subcode,
  80                         const u8 *msg_data, u8 msg_len, bool need_resp,
  81                         u8 *resp_data, u16 resp_len)
  82{
  83        struct hclge_mbx_vf_to_pf_cmd *req;
  84        struct hclgevf_desc desc;
  85        int status;
  86
  87        req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data;
  88
  89        /* first two bytes are reserved for code & subcode */
  90        if (msg_len > (HCLGE_MBX_MAX_MSG_SIZE - 2)) {
  91                dev_err(&hdev->pdev->dev,
  92                        "VF send mbx msg fail, msg len %d exceeds max len %d\n",
  93                        msg_len, HCLGE_MBX_MAX_MSG_SIZE);
  94                return -EINVAL;
  95        }
  96
  97        hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false);
  98        req->msg[0] = code;
  99        req->msg[1] = subcode;
 100        memcpy(&req->msg[2], msg_data, msg_len);
 101
 102        /* synchronous send */
 103        if (need_resp) {
 104                mutex_lock(&hdev->mbx_resp.mbx_mutex);
 105                hclgevf_reset_mbx_resp_status(hdev);
 106                status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
 107                if (status) {
 108                        dev_err(&hdev->pdev->dev,
 109                                "VF failed(=%d) to send mbx message to PF\n",
 110                                status);
 111                        mutex_unlock(&hdev->mbx_resp.mbx_mutex);
 112                        return status;
 113                }
 114
 115                status = hclgevf_get_mbx_resp(hdev, code, subcode, resp_data,
 116                                              resp_len);
 117                mutex_unlock(&hdev->mbx_resp.mbx_mutex);
 118        } else {
 119                /* asynchronous send */
 120                status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
 121                if (status) {
 122                        dev_err(&hdev->pdev->dev,
 123                                "VF failed(=%d) to send mbx message to PF\n",
 124                                status);
 125                        return status;
 126                }
 127        }
 128
 129        return status;
 130}
 131
 132static bool hclgevf_cmd_crq_empty(struct hclgevf_hw *hw)
 133{
 134        u32 tail = hclgevf_read_dev(hw, HCLGEVF_NIC_CRQ_TAIL_REG);
 135
 136        return tail == hw->cmq.crq.next_to_use;
 137}
 138
 139void hclgevf_mbx_handler(struct hclgevf_dev *hdev)
 140{
 141        struct hclgevf_mbx_resp_status *resp;
 142        struct hclge_mbx_pf_to_vf_cmd *req;
 143        struct hclgevf_cmq_ring *crq;
 144        struct hclgevf_desc *desc;
 145        u16 *msg_q;
 146        u16 flag;
 147        u8 *temp;
 148        int i;
 149
 150        resp = &hdev->mbx_resp;
 151        crq = &hdev->hw.cmq.crq;
 152
 153        while (!hclgevf_cmd_crq_empty(&hdev->hw)) {
 154                if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
 155                        dev_info(&hdev->pdev->dev, "vf crq need init\n");
 156                        return;
 157                }
 158
 159                desc = &crq->desc[crq->next_to_use];
 160                req = (struct hclge_mbx_pf_to_vf_cmd *)desc->data;
 161
 162                flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
 163                if (unlikely(!hnae3_get_bit(flag, HCLGEVF_CMDQ_RX_OUTVLD_B))) {
 164                        dev_warn(&hdev->pdev->dev,
 165                                 "dropped invalid mailbox message, code = %d\n",
 166                                 req->msg[0]);
 167
 168                        /* dropping/not processing this invalid message */
 169                        crq->desc[crq->next_to_use].flag = 0;
 170                        hclge_mbx_ring_ptr_move_crq(crq);
 171                        continue;
 172                }
 173
 174                /* synchronous messages are time critical and need preferential
 175                 * treatment. Therefore, we need to acknowledge all the sync
 176                 * responses as quickly as possible so that waiting tasks do not
 177                 * timeout and simultaneously queue the async messages for later
 178                 * prcessing in context of mailbox task i.e. the slow path.
 179                 */
 180                switch (req->msg[0]) {
 181                case HCLGE_MBX_PF_VF_RESP:
 182                        if (resp->received_resp)
 183                                dev_warn(&hdev->pdev->dev,
 184                                         "VF mbx resp flag not clear(%d)\n",
 185                                         req->msg[1]);
 186                        resp->received_resp = true;
 187
 188                        resp->origin_mbx_msg = (req->msg[1] << 16);
 189                        resp->origin_mbx_msg |= req->msg[2];
 190                        resp->resp_status = req->msg[3];
 191
 192                        temp = (u8 *)&req->msg[4];
 193                        for (i = 0; i < HCLGE_MBX_MAX_RESP_DATA_SIZE; i++) {
 194                                resp->additional_info[i] = *temp;
 195                                temp++;
 196                        }
 197                        break;
 198                case HCLGE_MBX_LINK_STAT_CHANGE:
 199                case HCLGE_MBX_ASSERTING_RESET:
 200                        /* set this mbx event as pending. This is required as we
 201                         * might loose interrupt event when mbx task is busy
 202                         * handling. This shall be cleared when mbx task just
 203                         * enters handling state.
 204                         */
 205                        hdev->mbx_event_pending = true;
 206
 207                        /* we will drop the async msg if we find ARQ as full
 208                         * and continue with next message
 209                         */
 210                        if (hdev->arq.count >= HCLGE_MBX_MAX_ARQ_MSG_NUM) {
 211                                dev_warn(&hdev->pdev->dev,
 212                                         "Async Q full, dropping msg(%d)\n",
 213                                         req->msg[1]);
 214                                break;
 215                        }
 216
 217                        /* tail the async message in arq */
 218                        msg_q = hdev->arq.msg_q[hdev->arq.tail];
 219                        memcpy(&msg_q[0], req->msg,
 220                               HCLGE_MBX_MAX_ARQ_MSG_SIZE * sizeof(u16));
 221                        hclge_mbx_tail_ptr_move_arq(hdev->arq);
 222                        hdev->arq.count++;
 223
 224                        hclgevf_mbx_task_schedule(hdev);
 225
 226                        break;
 227                default:
 228                        dev_err(&hdev->pdev->dev,
 229                                "VF received unsupported(%d) mbx msg from PF\n",
 230                                req->msg[0]);
 231                        break;
 232                }
 233                crq->desc[crq->next_to_use].flag = 0;
 234                hclge_mbx_ring_ptr_move_crq(crq);
 235        }
 236
 237        /* Write back CMDQ_RQ header pointer, M7 need this pointer */
 238        hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CRQ_HEAD_REG,
 239                          crq->next_to_use);
 240}
 241
 242void hclgevf_mbx_async_handler(struct hclgevf_dev *hdev)
 243{
 244        enum hnae3_reset_type reset_type;
 245        u16 link_status;
 246        u16 *msg_q;
 247        u8 duplex;
 248        u32 speed;
 249        u32 tail;
 250
 251        /* we can safely clear it now as we are at start of the async message
 252         * processing
 253         */
 254        hdev->mbx_event_pending = false;
 255
 256        tail = hdev->arq.tail;
 257
 258        /* process all the async queue messages */
 259        while (tail != hdev->arq.head) {
 260                if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
 261                        dev_info(&hdev->pdev->dev,
 262                                 "vf crq need init in async\n");
 263                        return;
 264                }
 265
 266                msg_q = hdev->arq.msg_q[hdev->arq.head];
 267
 268                switch (msg_q[0]) {
 269                case HCLGE_MBX_LINK_STAT_CHANGE:
 270                        link_status = le16_to_cpu(msg_q[1]);
 271                        memcpy(&speed, &msg_q[2], sizeof(speed));
 272                        duplex = (u8)le16_to_cpu(msg_q[4]);
 273
 274                        /* update upper layer with new link link status */
 275                        hclgevf_update_link_status(hdev, link_status);
 276                        hclgevf_update_speed_duplex(hdev, speed, duplex);
 277
 278                        break;
 279                case HCLGE_MBX_ASSERTING_RESET:
 280                        /* PF has asserted reset hence VF should go in pending
 281                         * state and poll for the hardware reset status till it
 282                         * has been completely reset. After this stack should
 283                         * eventually be re-initialized.
 284                         */
 285                        reset_type = le16_to_cpu(msg_q[1]);
 286                        set_bit(reset_type, &hdev->reset_pending);
 287                        set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
 288                        hclgevf_reset_task_schedule(hdev);
 289
 290                        break;
 291                default:
 292                        dev_err(&hdev->pdev->dev,
 293                                "fetched unsupported(%d) message from arq\n",
 294                                msg_q[0]);
 295                        break;
 296                }
 297
 298                hclge_mbx_head_ptr_move_arq(hdev->arq);
 299                hdev->arq.count--;
 300                msg_q = hdev->arq.msg_q[hdev->arq.head];
 301        }
 302}
 303