|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
| 2 | +// Copyright (c) 2016-2017 Hisilicon Limited. |
| 3 | + |
| 4 | +#include "hclge_main.h" |
| 5 | +#include "hclge_mbx.h" |
| 6 | +#include "hnae3.h" |
| 7 | + |
| 8 | +/* hclge_gen_resp_to_vf: used to generate a synchronous response to VF when PF |
| 9 | + * receives a mailbox message from VF. |
| 10 | + * @vport: pointer to struct hclge_vport |
| 11 | + * @vf_to_pf_req: pointer to hclge_mbx_vf_to_pf_cmd of the original mailbox |
| 12 | + * message |
| 13 | + * @resp_status: indicate to VF whether its request success(0) or failed. |
| 14 | + */ |
| 15 | +static int hclge_gen_resp_to_vf(struct hclge_vport *vport, |
| 16 | + struct hclge_mbx_vf_to_pf_cmd *vf_to_pf_req, |
| 17 | + int resp_status, |
| 18 | + u8 *resp_data, u16 resp_data_len) |
| 19 | +{ |
| 20 | + struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf; |
| 21 | + struct hclge_dev *hdev = vport->back; |
| 22 | + enum hclge_cmd_status status; |
| 23 | + struct hclge_desc desc; |
| 24 | + |
| 25 | + resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data; |
| 26 | + |
| 27 | + if (resp_data_len > HCLGE_MBX_MAX_RESP_DATA_SIZE) { |
| 28 | + dev_err(&hdev->pdev->dev, |
| 29 | + "PF fail to gen resp to VF len %d exceeds max len %d\n", |
| 30 | + resp_data_len, |
| 31 | + HCLGE_MBX_MAX_RESP_DATA_SIZE); |
| 32 | + } |
| 33 | + |
| 34 | + hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false); |
| 35 | + |
| 36 | + resp_pf_to_vf->dest_vfid = vf_to_pf_req->mbx_src_vfid; |
| 37 | + resp_pf_to_vf->msg_len = vf_to_pf_req->msg_len; |
| 38 | + |
| 39 | + resp_pf_to_vf->msg[0] = HCLGE_MBX_PF_VF_RESP; |
| 40 | + resp_pf_to_vf->msg[1] = vf_to_pf_req->msg[0]; |
| 41 | + resp_pf_to_vf->msg[2] = vf_to_pf_req->msg[1]; |
| 42 | + resp_pf_to_vf->msg[3] = (resp_status == 0) ? 0 : 1; |
| 43 | + |
| 44 | + if (resp_data && resp_data_len > 0) |
| 45 | + memcpy(&resp_pf_to_vf->msg[4], resp_data, resp_data_len); |
| 46 | + |
| 47 | + status = hclge_cmd_send(&hdev->hw, &desc, 1); |
| 48 | + if (status) |
| 49 | + dev_err(&hdev->pdev->dev, |
| 50 | + "PF failed(=%d) to send response to VF\n", status); |
| 51 | + |
| 52 | + return status; |
| 53 | +} |
| 54 | + |
| 55 | +static int hclge_send_mbx_msg(struct hclge_vport *vport, u8 *msg, u16 msg_len, |
| 56 | + u16 mbx_opcode, u8 dest_vfid) |
| 57 | +{ |
| 58 | + struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf; |
| 59 | + struct hclge_dev *hdev = vport->back; |
| 60 | + enum hclge_cmd_status status; |
| 61 | + struct hclge_desc desc; |
| 62 | + |
| 63 | + resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data; |
| 64 | + |
| 65 | + hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false); |
| 66 | + |
| 67 | + resp_pf_to_vf->dest_vfid = dest_vfid; |
| 68 | + resp_pf_to_vf->msg_len = msg_len; |
| 69 | + resp_pf_to_vf->msg[0] = mbx_opcode; |
| 70 | + |
| 71 | + memcpy(&resp_pf_to_vf->msg[1], msg, msg_len); |
| 72 | + |
| 73 | + status = hclge_cmd_send(&hdev->hw, &desc, 1); |
| 74 | + if (status) |
| 75 | + dev_err(&hdev->pdev->dev, |
| 76 | + "PF failed(=%d) to send mailbox message to VF\n", |
| 77 | + status); |
| 78 | + |
| 79 | + return status; |
| 80 | +} |
| 81 | + |
| 82 | +static int hclge_set_vf_promisc_mode(struct hclge_vport *vport, |
| 83 | + struct hclge_mbx_vf_to_pf_cmd *req) |
| 84 | +{ |
| 85 | + bool en = req->msg[1] ? true : false; |
| 86 | + struct hclge_promisc_param param; |
| 87 | + |
| 88 | + /* always enable broadcast promisc bit */ |
| 89 | + hclge_promisc_param_init(¶m, en, en, true, vport->vport_id); |
| 90 | + return hclge_cmd_set_promisc_mode(vport->back, ¶m); |
| 91 | +} |
| 92 | + |
| 93 | +static int hclge_set_vf_uc_mac_addr(struct hclge_vport *vport, |
| 94 | + struct hclge_mbx_vf_to_pf_cmd *mbx_req, |
| 95 | + bool gen_resp) |
| 96 | +{ |
| 97 | + const u8 *mac_addr = (const u8 *)(&mbx_req->msg[2]); |
| 98 | + struct hclge_dev *hdev = vport->back; |
| 99 | + int status; |
| 100 | + |
| 101 | + if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_MODIFY) { |
| 102 | + const u8 *old_addr = (const u8 *)(&mbx_req->msg[8]); |
| 103 | + |
| 104 | + hclge_rm_uc_addr_common(vport, old_addr); |
| 105 | + status = hclge_add_uc_addr_common(vport, mac_addr); |
| 106 | + } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_ADD) { |
| 107 | + status = hclge_add_uc_addr_common(vport, mac_addr); |
| 108 | + } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_REMOVE) { |
| 109 | + status = hclge_rm_uc_addr_common(vport, mac_addr); |
| 110 | + } else { |
| 111 | + dev_err(&hdev->pdev->dev, |
| 112 | + "failed to set unicast mac addr, unknown subcode %d\n", |
| 113 | + mbx_req->msg[1]); |
| 114 | + return -EIO; |
| 115 | + } |
| 116 | + |
| 117 | + if (gen_resp) |
| 118 | + hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +static int hclge_set_vf_mc_mac_addr(struct hclge_vport *vport, |
| 124 | + struct hclge_mbx_vf_to_pf_cmd *mbx_req, |
| 125 | + bool gen_resp) |
| 126 | +{ |
| 127 | + const u8 *mac_addr = (const u8 *)(&mbx_req->msg[2]); |
| 128 | + struct hclge_dev *hdev = vport->back; |
| 129 | + int status; |
| 130 | + |
| 131 | + if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_ADD) { |
| 132 | + status = hclge_add_mc_addr_common(vport, mac_addr); |
| 133 | + } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_REMOVE) { |
| 134 | + status = hclge_rm_mc_addr_common(vport, mac_addr); |
| 135 | + } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_FUNC_MTA_ENABLE) { |
| 136 | + u8 func_id = vport->vport_id; |
| 137 | + bool enable = mbx_req->msg[2]; |
| 138 | + |
| 139 | + status = hclge_cfg_func_mta_filter(hdev, func_id, enable); |
| 140 | + } else { |
| 141 | + dev_err(&hdev->pdev->dev, |
| 142 | + "failed to set mcast mac addr, unknown subcode %d\n", |
| 143 | + mbx_req->msg[1]); |
| 144 | + return -EIO; |
| 145 | + } |
| 146 | + |
| 147 | + if (gen_resp) |
| 148 | + hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); |
| 149 | + |
| 150 | + return 0; |
| 151 | +} |
| 152 | + |
| 153 | +static int hclge_set_vf_vlan_cfg(struct hclge_vport *vport, |
| 154 | + struct hclge_mbx_vf_to_pf_cmd *mbx_req, |
| 155 | + bool gen_resp) |
| 156 | +{ |
| 157 | + struct hclge_dev *hdev = vport->back; |
| 158 | + int status = 0; |
| 159 | + |
| 160 | + if (mbx_req->msg[1] == HCLGE_MBX_VLAN_FILTER) { |
| 161 | + u16 vlan, proto; |
| 162 | + bool is_kill; |
| 163 | + |
| 164 | + is_kill = !!mbx_req->msg[2]; |
| 165 | + memcpy(&vlan, &mbx_req->msg[3], sizeof(vlan)); |
| 166 | + memcpy(&proto, &mbx_req->msg[5], sizeof(proto)); |
| 167 | + status = hclge_set_vf_vlan_common(hdev, vport->vport_id, |
| 168 | + is_kill, vlan, 0, |
| 169 | + cpu_to_be16(proto)); |
| 170 | + } |
| 171 | + |
| 172 | + if (gen_resp) |
| 173 | + status = hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); |
| 174 | + |
| 175 | + return status; |
| 176 | +} |
| 177 | + |
| 178 | +static int hclge_get_vf_tcinfo(struct hclge_vport *vport, |
| 179 | + struct hclge_mbx_vf_to_pf_cmd *mbx_req, |
| 180 | + bool gen_resp) |
| 181 | +{ |
| 182 | + struct hclge_dev *hdev = vport->back; |
| 183 | + int ret; |
| 184 | + |
| 185 | + ret = hclge_gen_resp_to_vf(vport, mbx_req, 0, &hdev->hw_tc_map, |
| 186 | + sizeof(u8)); |
| 187 | + |
| 188 | + return ret; |
| 189 | +} |
| 190 | + |
| 191 | +static int hclge_get_vf_queue_info(struct hclge_vport *vport, |
| 192 | + struct hclge_mbx_vf_to_pf_cmd *mbx_req, |
| 193 | + bool gen_resp) |
| 194 | +{ |
| 195 | +#define HCLGE_TQPS_RSS_INFO_LEN 8 |
| 196 | + u8 resp_data[HCLGE_TQPS_RSS_INFO_LEN]; |
| 197 | + struct hclge_dev *hdev = vport->back; |
| 198 | + |
| 199 | + /* get the queue related info */ |
| 200 | + memcpy(&resp_data[0], &vport->alloc_tqps, sizeof(u16)); |
| 201 | + memcpy(&resp_data[2], &hdev->rss_size_max, sizeof(u16)); |
| 202 | + memcpy(&resp_data[4], &hdev->num_desc, sizeof(u16)); |
| 203 | + memcpy(&resp_data[6], &hdev->rx_buf_len, sizeof(u16)); |
| 204 | + |
| 205 | + return hclge_gen_resp_to_vf(vport, mbx_req, 0, resp_data, |
| 206 | + HCLGE_TQPS_RSS_INFO_LEN); |
| 207 | +} |
| 208 | + |
| 209 | +static int hclge_get_link_info(struct hclge_vport *vport, |
| 210 | + struct hclge_mbx_vf_to_pf_cmd *mbx_req) |
| 211 | +{ |
| 212 | + struct hclge_dev *hdev = vport->back; |
| 213 | + u16 link_status; |
| 214 | + u8 msg_data[2]; |
| 215 | + u8 dest_vfid; |
| 216 | + |
| 217 | + /* mac.link can only be 0 or 1 */ |
| 218 | + link_status = (u16)hdev->hw.mac.link; |
| 219 | + memcpy(&msg_data[0], &link_status, sizeof(u16)); |
| 220 | + dest_vfid = mbx_req->mbx_src_vfid; |
| 221 | + |
| 222 | + /* send this requested info to VF */ |
| 223 | + return hclge_send_mbx_msg(vport, msg_data, sizeof(u8), |
| 224 | + HCLGE_MBX_LINK_STAT_CHANGE, dest_vfid); |
| 225 | +} |
| 226 | + |
| 227 | +void hclge_mbx_handler(struct hclge_dev *hdev) |
| 228 | +{ |
| 229 | + struct hclge_cmq_ring *crq = &hdev->hw.cmq.crq; |
| 230 | + struct hclge_mbx_vf_to_pf_cmd *req; |
| 231 | + struct hclge_vport *vport; |
| 232 | + struct hclge_desc *desc; |
| 233 | + int ret; |
| 234 | + |
| 235 | + /* handle all the mailbox requests in the queue */ |
| 236 | + while (hnae_get_bit(crq->desc[crq->next_to_use].flag, |
| 237 | + HCLGE_CMDQ_RX_OUTVLD_B)) { |
| 238 | + desc = &crq->desc[crq->next_to_use]; |
| 239 | + req = (struct hclge_mbx_vf_to_pf_cmd *)desc->data; |
| 240 | + |
| 241 | + vport = &hdev->vport[req->mbx_src_vfid]; |
| 242 | + |
| 243 | + switch (req->msg[0]) { |
| 244 | + case HCLGE_MBX_SET_PROMISC_MODE: |
| 245 | + ret = hclge_set_vf_promisc_mode(vport, req); |
| 246 | + if (ret) |
| 247 | + dev_err(&hdev->pdev->dev, |
| 248 | + "PF fail(%d) to set VF promisc mode\n", |
| 249 | + ret); |
| 250 | + break; |
| 251 | + case HCLGE_MBX_SET_UNICAST: |
| 252 | + ret = hclge_set_vf_uc_mac_addr(vport, req, false); |
| 253 | + if (ret) |
| 254 | + dev_err(&hdev->pdev->dev, |
| 255 | + "PF fail(%d) to set VF UC MAC Addr\n", |
| 256 | + ret); |
| 257 | + break; |
| 258 | + case HCLGE_MBX_SET_MULTICAST: |
| 259 | + ret = hclge_set_vf_mc_mac_addr(vport, req, false); |
| 260 | + if (ret) |
| 261 | + dev_err(&hdev->pdev->dev, |
| 262 | + "PF fail(%d) to set VF MC MAC Addr\n", |
| 263 | + ret); |
| 264 | + break; |
| 265 | + case HCLGE_MBX_SET_VLAN: |
| 266 | + ret = hclge_set_vf_vlan_cfg(vport, req, false); |
| 267 | + if (ret) |
| 268 | + dev_err(&hdev->pdev->dev, |
| 269 | + "PF failed(%d) to config VF's VLAN\n", |
| 270 | + ret); |
| 271 | + break; |
| 272 | + case HCLGE_MBX_GET_QINFO: |
| 273 | + ret = hclge_get_vf_queue_info(vport, req, true); |
| 274 | + if (ret) |
| 275 | + dev_err(&hdev->pdev->dev, |
| 276 | + "PF failed(%d) to get Q info for VF\n", |
| 277 | + ret); |
| 278 | + break; |
| 279 | + case HCLGE_MBX_GET_TCINFO: |
| 280 | + ret = hclge_get_vf_tcinfo(vport, req, true); |
| 281 | + if (ret) |
| 282 | + dev_err(&hdev->pdev->dev, |
| 283 | + "PF failed(%d) to get TC info for VF\n", |
| 284 | + ret); |
| 285 | + break; |
| 286 | + case HCLGE_MBX_GET_LINK_STATUS: |
| 287 | + ret = hclge_get_link_info(vport, req); |
| 288 | + if (ret) |
| 289 | + dev_err(&hdev->pdev->dev, |
| 290 | + "PF fail(%d) to get link stat for VF\n", |
| 291 | + ret); |
| 292 | + break; |
| 293 | + default: |
| 294 | + dev_err(&hdev->pdev->dev, |
| 295 | + "un-supported mailbox message, code = %d\n", |
| 296 | + req->msg[0]); |
| 297 | + break; |
| 298 | + } |
| 299 | + hclge_mbx_ring_ptr_move_crq(crq); |
| 300 | + } |
| 301 | + |
| 302 | + /* Write back CMDQ_RQ header pointer, M7 need this pointer */ |
| 303 | + hclge_write_dev(&hdev->hw, HCLGE_NIC_CRQ_HEAD_REG, crq->next_to_use); |
| 304 | +} |
0 commit comments