Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / hisilicon / hns3 / hns3pf / hclge_mbx.c
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 #define CREATE_TRACE_POINTS
9 #include "hclge_trace.h"
10
11 static u16 hclge_errno_to_resp(int errno)
12 {
13         int resp = abs(errno);
14
15         /* The status for pf to vf msg cmd is u16, constrainted by HW.
16          * We need to keep the same type with it.
17          * The intput errno is the stander error code, it's safely to
18          * use a u16 to store the abs(errno).
19          */
20         return (u16)resp;
21 }
22
23 /* hclge_gen_resp_to_vf: used to generate a synchronous response to VF when PF
24  * receives a mailbox message from VF.
25  * @vport: pointer to struct hclge_vport
26  * @vf_to_pf_req: pointer to hclge_mbx_vf_to_pf_cmd of the original mailbox
27  *                message
28  * @resp_status: indicate to VF whether its request success(0) or failed.
29  */
30 static int hclge_gen_resp_to_vf(struct hclge_vport *vport,
31                                 struct hclge_mbx_vf_to_pf_cmd *vf_to_pf_req,
32                                 struct hclge_respond_to_vf_msg *resp_msg)
33 {
34         struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf;
35         struct hclge_dev *hdev = vport->back;
36         enum hclge_cmd_status status;
37         struct hclge_desc desc;
38         u16 resp;
39
40         resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data;
41
42         if (resp_msg->len > HCLGE_MBX_MAX_RESP_DATA_SIZE) {
43                 dev_err(&hdev->pdev->dev,
44                         "PF fail to gen resp to VF len %u exceeds max len %u\n",
45                         resp_msg->len,
46                         HCLGE_MBX_MAX_RESP_DATA_SIZE);
47                 /* If resp_msg->len is too long, set the value to max length
48                  * and return the msg to VF
49                  */
50                 resp_msg->len = HCLGE_MBX_MAX_RESP_DATA_SIZE;
51         }
52
53         hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false);
54
55         resp_pf_to_vf->dest_vfid = vf_to_pf_req->mbx_src_vfid;
56         resp_pf_to_vf->msg_len = vf_to_pf_req->msg_len;
57         resp_pf_to_vf->match_id = vf_to_pf_req->match_id;
58
59         resp_pf_to_vf->msg.code = HCLGE_MBX_PF_VF_RESP;
60         resp_pf_to_vf->msg.vf_mbx_msg_code = vf_to_pf_req->msg.code;
61         resp_pf_to_vf->msg.vf_mbx_msg_subcode = vf_to_pf_req->msg.subcode;
62         resp = hclge_errno_to_resp(resp_msg->status);
63         if (resp < SHRT_MAX) {
64                 resp_pf_to_vf->msg.resp_status = resp;
65         } else {
66                 dev_warn(&hdev->pdev->dev,
67                          "failed to send response to VF, response status %u is out-of-bound\n",
68                          resp);
69                 resp_pf_to_vf->msg.resp_status = EIO;
70         }
71
72         if (resp_msg->len > 0)
73                 memcpy(resp_pf_to_vf->msg.resp_data, resp_msg->data,
74                        resp_msg->len);
75
76         trace_hclge_pf_mbx_send(hdev, resp_pf_to_vf);
77
78         status = hclge_cmd_send(&hdev->hw, &desc, 1);
79         if (status)
80                 dev_err(&hdev->pdev->dev,
81                         "failed to send response to VF, status: %d, vfid: %u, code: %u, subcode: %u.\n",
82                         status, vf_to_pf_req->mbx_src_vfid,
83                         vf_to_pf_req->msg.code, vf_to_pf_req->msg.subcode);
84
85         return status;
86 }
87
88 static int hclge_send_mbx_msg(struct hclge_vport *vport, u8 *msg, u16 msg_len,
89                               u16 mbx_opcode, u8 dest_vfid)
90 {
91         struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf;
92         struct hclge_dev *hdev = vport->back;
93         enum hclge_cmd_status status;
94         struct hclge_desc desc;
95
96         resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data;
97
98         hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false);
99
100         resp_pf_to_vf->dest_vfid = dest_vfid;
101         resp_pf_to_vf->msg_len = msg_len;
102         resp_pf_to_vf->msg.code = mbx_opcode;
103
104         memcpy(&resp_pf_to_vf->msg.vf_mbx_msg_code, msg, msg_len);
105
106         trace_hclge_pf_mbx_send(hdev, resp_pf_to_vf);
107
108         status = hclge_cmd_send(&hdev->hw, &desc, 1);
109         if (status)
110                 dev_err(&hdev->pdev->dev,
111                         "failed to send mailbox to VF, status: %d, vfid: %u, opcode: %u\n",
112                         status, dest_vfid, mbx_opcode);
113
114         return status;
115 }
116
117 int hclge_inform_reset_assert_to_vf(struct hclge_vport *vport)
118 {
119         struct hclge_dev *hdev = vport->back;
120         u16 reset_type;
121         u8 msg_data[2];
122         u8 dest_vfid;
123
124         BUILD_BUG_ON(HNAE3_MAX_RESET > U16_MAX);
125
126         dest_vfid = (u8)vport->vport_id;
127
128         if (hdev->reset_type == HNAE3_FUNC_RESET)
129                 reset_type = HNAE3_VF_PF_FUNC_RESET;
130         else if (hdev->reset_type == HNAE3_FLR_RESET)
131                 reset_type = HNAE3_VF_FULL_RESET;
132         else
133                 reset_type = HNAE3_VF_FUNC_RESET;
134
135         memcpy(&msg_data[0], &reset_type, sizeof(u16));
136
137         /* send this requested info to VF */
138         return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
139                                   HCLGE_MBX_ASSERTING_RESET, dest_vfid);
140 }
141
142 static void hclge_free_vector_ring_chain(struct hnae3_ring_chain_node *head)
143 {
144         struct hnae3_ring_chain_node *chain_tmp, *chain;
145
146         chain = head->next;
147
148         while (chain) {
149                 chain_tmp = chain->next;
150                 kfree_sensitive(chain);
151                 chain = chain_tmp;
152         }
153 }
154
155 /* hclge_get_ring_chain_from_mbx: get ring type & tqp id & int_gl idx
156  * from mailbox message
157  * msg[0]: opcode
158  * msg[1]: <not relevant to this function>
159  * msg[2]: ring_num
160  * msg[3]: first ring type (TX|RX)
161  * msg[4]: first tqp id
162  * msg[5]: first int_gl idx
163  * msg[6] ~ msg[14]: other ring type, tqp id and int_gl idx
164  */
165 static int hclge_get_ring_chain_from_mbx(
166                         struct hclge_mbx_vf_to_pf_cmd *req,
167                         struct hnae3_ring_chain_node *ring_chain,
168                         struct hclge_vport *vport)
169 {
170         struct hnae3_ring_chain_node *cur_chain, *new_chain;
171         struct hclge_dev *hdev = vport->back;
172         int ring_num;
173         int i;
174
175         ring_num = req->msg.ring_num;
176
177         if (ring_num > HCLGE_MBX_MAX_RING_CHAIN_PARAM_NUM)
178                 return -ENOMEM;
179
180         for (i = 0; i < ring_num; i++) {
181                 if (req->msg.param[i].tqp_index >= vport->nic.kinfo.rss_size) {
182                         dev_err(&hdev->pdev->dev, "tqp index(%u) is out of range(0-%u)\n",
183                                 req->msg.param[i].tqp_index,
184                                 vport->nic.kinfo.rss_size - 1);
185                         return -EINVAL;
186                 }
187         }
188
189         hnae3_set_bit(ring_chain->flag, HNAE3_RING_TYPE_B,
190                       req->msg.param[0].ring_type);
191         ring_chain->tqp_index =
192                 hclge_get_queue_id(vport->nic.kinfo.tqp
193                                    [req->msg.param[0].tqp_index]);
194         hnae3_set_field(ring_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
195                         HNAE3_RING_GL_IDX_S, req->msg.param[0].int_gl_index);
196
197         cur_chain = ring_chain;
198
199         for (i = 1; i < ring_num; i++) {
200                 new_chain = kzalloc(sizeof(*new_chain), GFP_KERNEL);
201                 if (!new_chain)
202                         goto err;
203
204                 hnae3_set_bit(new_chain->flag, HNAE3_RING_TYPE_B,
205                               req->msg.param[i].ring_type);
206
207                 new_chain->tqp_index =
208                 hclge_get_queue_id(vport->nic.kinfo.tqp
209                         [req->msg.param[i].tqp_index]);
210
211                 hnae3_set_field(new_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
212                                 HNAE3_RING_GL_IDX_S,
213                                 req->msg.param[i].int_gl_index);
214
215                 cur_chain->next = new_chain;
216                 cur_chain = new_chain;
217         }
218
219         return 0;
220 err:
221         hclge_free_vector_ring_chain(ring_chain);
222         return -ENOMEM;
223 }
224
225 static int hclge_map_unmap_ring_to_vf_vector(struct hclge_vport *vport, bool en,
226                                              struct hclge_mbx_vf_to_pf_cmd *req)
227 {
228         struct hnae3_ring_chain_node ring_chain;
229         int vector_id = req->msg.vector_id;
230         int ret;
231
232         memset(&ring_chain, 0, sizeof(ring_chain));
233         ret = hclge_get_ring_chain_from_mbx(req, &ring_chain, vport);
234         if (ret)
235                 return ret;
236
237         ret = hclge_bind_ring_with_vector(vport, vector_id, en, &ring_chain);
238
239         hclge_free_vector_ring_chain(&ring_chain);
240
241         return ret;
242 }
243
244 static void hclge_set_vf_promisc_mode(struct hclge_vport *vport,
245                                       struct hclge_mbx_vf_to_pf_cmd *req)
246 {
247         struct hnae3_handle *handle = &vport->nic;
248         struct hclge_dev *hdev = vport->back;
249
250         vport->vf_info.request_uc_en = req->msg.en_uc;
251         vport->vf_info.request_mc_en = req->msg.en_mc;
252         vport->vf_info.request_bc_en = req->msg.en_bc;
253
254         if (req->msg.en_limit_promisc)
255                 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
256         else
257                 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC,
258                           &handle->priv_flags);
259
260         set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
261         hclge_task_schedule(hdev, 0);
262 }
263
264 static int hclge_set_vf_uc_mac_addr(struct hclge_vport *vport,
265                                     struct hclge_mbx_vf_to_pf_cmd *mbx_req)
266 {
267 #define HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET        6
268
269         const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
270         struct hclge_dev *hdev = vport->back;
271         int status;
272
273         if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_MODIFY) {
274                 const u8 *old_addr = (const u8 *)
275                 (&mbx_req->msg.data[HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET]);
276
277                 /* If VF MAC has been configured by the host then it
278                  * cannot be overridden by the MAC specified by the VM.
279                  */
280                 if (!is_zero_ether_addr(vport->vf_info.mac) &&
281                     !ether_addr_equal(mac_addr, vport->vf_info.mac))
282                         return -EPERM;
283
284                 if (!is_valid_ether_addr(mac_addr))
285                         return -EINVAL;
286
287                 spin_lock_bh(&vport->mac_list_lock);
288                 status = hclge_update_mac_node_for_dev_addr(vport, old_addr,
289                                                             mac_addr);
290                 spin_unlock_bh(&vport->mac_list_lock);
291                 hclge_task_schedule(hdev, 0);
292         } else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_ADD) {
293                 status = hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
294                                                HCLGE_MAC_ADDR_UC, mac_addr);
295         } else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_REMOVE) {
296                 status = hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
297                                                HCLGE_MAC_ADDR_UC, mac_addr);
298         } else {
299                 dev_err(&hdev->pdev->dev,
300                         "failed to set unicast mac addr, unknown subcode %u\n",
301                         mbx_req->msg.subcode);
302                 return -EIO;
303         }
304
305         return status;
306 }
307
308 static int hclge_set_vf_mc_mac_addr(struct hclge_vport *vport,
309                                     struct hclge_mbx_vf_to_pf_cmd *mbx_req)
310 {
311         const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
312         struct hclge_dev *hdev = vport->back;
313
314         if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_ADD) {
315                 hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
316                                       HCLGE_MAC_ADDR_MC, mac_addr);
317         } else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_REMOVE) {
318                 hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
319                                       HCLGE_MAC_ADDR_MC, mac_addr);
320         } else {
321                 dev_err(&hdev->pdev->dev,
322                         "failed to set mcast mac addr, unknown subcode %u\n",
323                         mbx_req->msg.subcode);
324                 return -EIO;
325         }
326
327         return 0;
328 }
329
330 int hclge_push_vf_port_base_vlan_info(struct hclge_vport *vport, u8 vfid,
331                                       u16 state,
332                                       struct hclge_vlan_info *vlan_info)
333 {
334 #define MSG_DATA_SIZE   8
335
336         u8 msg_data[MSG_DATA_SIZE];
337
338         memcpy(&msg_data[0], &state, sizeof(u16));
339         memcpy(&msg_data[2], &vlan_info->vlan_proto, sizeof(u16));
340         memcpy(&msg_data[4], &vlan_info->qos, sizeof(u16));
341         memcpy(&msg_data[6], &vlan_info->vlan_tag, sizeof(u16));
342
343         return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
344                                   HCLGE_MBX_PUSH_VLAN_INFO, vfid);
345 }
346
347 static int hclge_set_vf_vlan_cfg(struct hclge_vport *vport,
348                                  struct hclge_mbx_vf_to_pf_cmd *mbx_req,
349                                  struct hclge_respond_to_vf_msg *resp_msg)
350 {
351 #define HCLGE_MBX_VLAN_STATE_OFFSET     0
352 #define HCLGE_MBX_VLAN_INFO_OFFSET      2
353
354         struct hnae3_handle *handle = &vport->nic;
355         struct hclge_dev *hdev = vport->back;
356         struct hclge_vf_vlan_cfg *msg_cmd;
357
358         msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
359         switch (msg_cmd->subcode) {
360         case HCLGE_MBX_VLAN_FILTER:
361                 return hclge_set_vlan_filter(handle,
362                                              cpu_to_be16(msg_cmd->proto),
363                                              msg_cmd->vlan, msg_cmd->is_kill);
364         case HCLGE_MBX_VLAN_RX_OFF_CFG:
365                 return hclge_en_hw_strip_rxvtag(handle, msg_cmd->enable);
366         case HCLGE_MBX_GET_PORT_BASE_VLAN_STATE:
367                 /* vf does not need to know about the port based VLAN state
368                  * on device HNAE3_DEVICE_VERSION_V3. So always return disable
369                  * on device HNAE3_DEVICE_VERSION_V3 if vf queries the port
370                  * based VLAN state.
371                  */
372                 resp_msg->data[0] =
373                         hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3 ?
374                         HNAE3_PORT_BASE_VLAN_DISABLE :
375                         vport->port_base_vlan_cfg.state;
376                 resp_msg->len = sizeof(u8);
377                 return 0;
378         case HCLGE_MBX_ENABLE_VLAN_FILTER:
379                 return hclge_enable_vport_vlan_filter(vport, msg_cmd->enable);
380         default:
381                 return 0;
382         }
383 }
384
385 static int hclge_set_vf_alive(struct hclge_vport *vport,
386                               struct hclge_mbx_vf_to_pf_cmd *mbx_req)
387 {
388         bool alive = !!mbx_req->msg.data[0];
389         int ret = 0;
390
391         if (alive)
392                 ret = hclge_vport_start(vport);
393         else
394                 hclge_vport_stop(vport);
395
396         return ret;
397 }
398
399 static void hclge_get_basic_info(struct hclge_vport *vport,
400                                  struct hclge_respond_to_vf_msg *resp_msg)
401 {
402         struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
403         struct hnae3_ae_dev *ae_dev = vport->back->ae_dev;
404         struct hclge_basic_info *basic_info;
405         unsigned int i;
406
407         basic_info = (struct hclge_basic_info *)resp_msg->data;
408         for (i = 0; i < kinfo->tc_info.num_tc; i++)
409                 basic_info->hw_tc_map |= BIT(i);
410
411         if (test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps))
412                 hnae3_set_bit(basic_info->pf_caps,
413                               HNAE3_PF_SUPPORT_VLAN_FLTR_MDF_B, 1);
414
415         resp_msg->len = HCLGE_MBX_MAX_RESP_DATA_SIZE;
416 }
417
418 static void hclge_get_vf_queue_info(struct hclge_vport *vport,
419                                     struct hclge_respond_to_vf_msg *resp_msg)
420 {
421 #define HCLGE_TQPS_RSS_INFO_LEN         6
422 #define HCLGE_TQPS_ALLOC_OFFSET         0
423 #define HCLGE_TQPS_RSS_SIZE_OFFSET      2
424 #define HCLGE_TQPS_RX_BUFFER_LEN_OFFSET 4
425
426         struct hclge_dev *hdev = vport->back;
427
428         /* get the queue related info */
429         memcpy(&resp_msg->data[HCLGE_TQPS_ALLOC_OFFSET],
430                &vport->alloc_tqps, sizeof(u16));
431         memcpy(&resp_msg->data[HCLGE_TQPS_RSS_SIZE_OFFSET],
432                &vport->nic.kinfo.rss_size, sizeof(u16));
433         memcpy(&resp_msg->data[HCLGE_TQPS_RX_BUFFER_LEN_OFFSET],
434                &hdev->rx_buf_len, sizeof(u16));
435         resp_msg->len = HCLGE_TQPS_RSS_INFO_LEN;
436 }
437
438 static void hclge_get_vf_mac_addr(struct hclge_vport *vport,
439                                   struct hclge_respond_to_vf_msg *resp_msg)
440 {
441         ether_addr_copy(resp_msg->data, vport->vf_info.mac);
442         resp_msg->len = ETH_ALEN;
443 }
444
445 static void hclge_get_vf_queue_depth(struct hclge_vport *vport,
446                                      struct hclge_respond_to_vf_msg *resp_msg)
447 {
448 #define HCLGE_TQPS_DEPTH_INFO_LEN       4
449 #define HCLGE_TQPS_NUM_TX_DESC_OFFSET   0
450 #define HCLGE_TQPS_NUM_RX_DESC_OFFSET   2
451
452         struct hclge_dev *hdev = vport->back;
453
454         /* get the queue depth info */
455         memcpy(&resp_msg->data[HCLGE_TQPS_NUM_TX_DESC_OFFSET],
456                &hdev->num_tx_desc, sizeof(u16));
457         memcpy(&resp_msg->data[HCLGE_TQPS_NUM_RX_DESC_OFFSET],
458                &hdev->num_rx_desc, sizeof(u16));
459         resp_msg->len = HCLGE_TQPS_DEPTH_INFO_LEN;
460 }
461
462 static void hclge_get_vf_media_type(struct hclge_vport *vport,
463                                     struct hclge_respond_to_vf_msg *resp_msg)
464 {
465 #define HCLGE_VF_MEDIA_TYPE_OFFSET      0
466 #define HCLGE_VF_MODULE_TYPE_OFFSET     1
467 #define HCLGE_VF_MEDIA_TYPE_LENGTH      2
468
469         struct hclge_dev *hdev = vport->back;
470
471         resp_msg->data[HCLGE_VF_MEDIA_TYPE_OFFSET] =
472                 hdev->hw.mac.media_type;
473         resp_msg->data[HCLGE_VF_MODULE_TYPE_OFFSET] =
474                 hdev->hw.mac.module_type;
475         resp_msg->len = HCLGE_VF_MEDIA_TYPE_LENGTH;
476 }
477
478 int hclge_push_vf_link_status(struct hclge_vport *vport)
479 {
480 #define HCLGE_VF_LINK_STATE_UP          1U
481 #define HCLGE_VF_LINK_STATE_DOWN        0U
482
483         struct hclge_dev *hdev = vport->back;
484         u16 link_status;
485         u8 msg_data[9];
486         u16 duplex;
487
488         /* mac.link can only be 0 or 1 */
489         switch (vport->vf_info.link_state) {
490         case IFLA_VF_LINK_STATE_ENABLE:
491                 link_status = HCLGE_VF_LINK_STATE_UP;
492                 break;
493         case IFLA_VF_LINK_STATE_DISABLE:
494                 link_status = HCLGE_VF_LINK_STATE_DOWN;
495                 break;
496         case IFLA_VF_LINK_STATE_AUTO:
497         default:
498                 link_status = (u16)hdev->hw.mac.link;
499                 break;
500         }
501
502         duplex = hdev->hw.mac.duplex;
503         memcpy(&msg_data[0], &link_status, sizeof(u16));
504         memcpy(&msg_data[2], &hdev->hw.mac.speed, sizeof(u32));
505         memcpy(&msg_data[6], &duplex, sizeof(u16));
506         msg_data[8] = HCLGE_MBX_PUSH_LINK_STATUS_EN;
507
508         /* send this requested info to VF */
509         return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
510                                   HCLGE_MBX_LINK_STAT_CHANGE, vport->vport_id);
511 }
512
513 static void hclge_get_link_mode(struct hclge_vport *vport,
514                                 struct hclge_mbx_vf_to_pf_cmd *mbx_req)
515 {
516 #define HCLGE_SUPPORTED   1
517         struct hclge_dev *hdev = vport->back;
518         unsigned long advertising;
519         unsigned long supported;
520         unsigned long send_data;
521         u8 msg_data[10] = {};
522         u8 dest_vfid;
523
524         advertising = hdev->hw.mac.advertising[0];
525         supported = hdev->hw.mac.supported[0];
526         dest_vfid = mbx_req->mbx_src_vfid;
527         msg_data[0] = mbx_req->msg.data[0];
528
529         send_data = msg_data[0] == HCLGE_SUPPORTED ? supported : advertising;
530
531         memcpy(&msg_data[2], &send_data, sizeof(unsigned long));
532         hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
533                            HCLGE_MBX_LINK_STAT_MODE, dest_vfid);
534 }
535
536 static int hclge_mbx_reset_vf_queue(struct hclge_vport *vport,
537                                     struct hclge_mbx_vf_to_pf_cmd *mbx_req,
538                                     struct hclge_respond_to_vf_msg *resp_msg)
539 {
540 #define HCLGE_RESET_ALL_QUEUE_DONE      1U
541         struct hnae3_handle *handle = &vport->nic;
542         struct hclge_dev *hdev = vport->back;
543         u16 queue_id;
544         int ret;
545
546         memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
547         resp_msg->data[0] = HCLGE_RESET_ALL_QUEUE_DONE;
548         resp_msg->len = sizeof(u8);
549
550         /* pf will reset vf's all queues at a time. So it is unnecessary
551          * to reset queues if queue_id > 0, just return success.
552          */
553         if (queue_id > 0)
554                 return 0;
555
556         ret = hclge_reset_tqp(handle);
557         if (ret)
558                 dev_err(&hdev->pdev->dev, "failed to reset vf %u queue, ret = %d\n",
559                         vport->vport_id - HCLGE_VF_VPORT_START_NUM, ret);
560
561         return ret;
562 }
563
564 static int hclge_reset_vf(struct hclge_vport *vport)
565 {
566         struct hclge_dev *hdev = vport->back;
567
568         dev_warn(&hdev->pdev->dev, "PF received VF reset request from VF %u!",
569                  vport->vport_id - HCLGE_VF_VPORT_START_NUM);
570
571         return hclge_func_reset_cmd(hdev, vport->vport_id);
572 }
573
574 static void hclge_vf_keep_alive(struct hclge_vport *vport)
575 {
576         vport->last_active_jiffies = jiffies;
577 }
578
579 static int hclge_set_vf_mtu(struct hclge_vport *vport,
580                             struct hclge_mbx_vf_to_pf_cmd *mbx_req)
581 {
582         u32 mtu;
583
584         memcpy(&mtu, mbx_req->msg.data, sizeof(mtu));
585
586         return hclge_set_vport_mtu(vport, mtu);
587 }
588
589 static void hclge_get_queue_id_in_pf(struct hclge_vport *vport,
590                                      struct hclge_mbx_vf_to_pf_cmd *mbx_req,
591                                      struct hclge_respond_to_vf_msg *resp_msg)
592 {
593         struct hnae3_handle *handle = &vport->nic;
594         struct hclge_dev *hdev = vport->back;
595         u16 queue_id, qid_in_pf;
596
597         memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
598         if (queue_id >= handle->kinfo.num_tqps) {
599                 dev_err(&hdev->pdev->dev, "Invalid queue id(%u) from VF %u\n",
600                         queue_id, mbx_req->mbx_src_vfid);
601                 return;
602         }
603
604         qid_in_pf = hclge_covert_handle_qid_global(&vport->nic, queue_id);
605         memcpy(resp_msg->data, &qid_in_pf, sizeof(qid_in_pf));
606         resp_msg->len = sizeof(qid_in_pf);
607 }
608
609 static void hclge_get_rss_key(struct hclge_vport *vport,
610                               struct hclge_mbx_vf_to_pf_cmd *mbx_req,
611                               struct hclge_respond_to_vf_msg *resp_msg)
612 {
613 #define HCLGE_RSS_MBX_RESP_LEN  8
614         struct hclge_dev *hdev = vport->back;
615         u8 index;
616
617         index = mbx_req->msg.data[0];
618
619         /* Check the query index of rss_hash_key from VF, make sure no
620          * more than the size of rss_hash_key.
621          */
622         if (((index + 1) * HCLGE_RSS_MBX_RESP_LEN) >
623               sizeof(vport[0].rss_hash_key)) {
624                 dev_warn(&hdev->pdev->dev,
625                          "failed to get the rss hash key, the index(%u) invalid !\n",
626                          index);
627                 return;
628         }
629
630         memcpy(resp_msg->data,
631                &hdev->vport[0].rss_hash_key[index * HCLGE_RSS_MBX_RESP_LEN],
632                HCLGE_RSS_MBX_RESP_LEN);
633         resp_msg->len = HCLGE_RSS_MBX_RESP_LEN;
634 }
635
636 static void hclge_link_fail_parse(struct hclge_dev *hdev, u8 link_fail_code)
637 {
638         switch (link_fail_code) {
639         case HCLGE_LF_REF_CLOCK_LOST:
640                 dev_warn(&hdev->pdev->dev, "Reference clock lost!\n");
641                 break;
642         case HCLGE_LF_XSFP_TX_DISABLE:
643                 dev_warn(&hdev->pdev->dev, "SFP tx is disabled!\n");
644                 break;
645         case HCLGE_LF_XSFP_ABSENT:
646                 dev_warn(&hdev->pdev->dev, "SFP is absent!\n");
647                 break;
648         default:
649                 break;
650         }
651 }
652
653 static void hclge_handle_link_change_event(struct hclge_dev *hdev,
654                                            struct hclge_mbx_vf_to_pf_cmd *req)
655 {
656         hclge_task_schedule(hdev, 0);
657
658         if (!req->msg.subcode)
659                 hclge_link_fail_parse(hdev, req->msg.data[0]);
660 }
661
662 static bool hclge_cmd_crq_empty(struct hclge_hw *hw)
663 {
664         u32 tail = hclge_read_dev(hw, HCLGE_NIC_CRQ_TAIL_REG);
665
666         return tail == hw->cmq.crq.next_to_use;
667 }
668
669 static void hclge_handle_ncsi_error(struct hclge_dev *hdev)
670 {
671         struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
672
673         ae_dev->ops->set_default_reset_request(ae_dev, HNAE3_GLOBAL_RESET);
674         dev_warn(&hdev->pdev->dev, "requesting reset due to NCSI error\n");
675         ae_dev->ops->reset_event(hdev->pdev, NULL);
676 }
677
678 static void hclge_handle_vf_tbl(struct hclge_vport *vport,
679                                 struct hclge_mbx_vf_to_pf_cmd *mbx_req)
680 {
681         struct hclge_dev *hdev = vport->back;
682         struct hclge_vf_vlan_cfg *msg_cmd;
683
684         msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
685         if (msg_cmd->subcode == HCLGE_MBX_VPORT_LIST_CLEAR) {
686                 hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_UC);
687                 hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_MC);
688                 hclge_rm_vport_all_vlan_table(vport, true);
689         } else {
690                 dev_warn(&hdev->pdev->dev, "Invalid cmd(%u)\n",
691                          msg_cmd->subcode);
692         }
693 }
694
695 void hclge_mbx_handler(struct hclge_dev *hdev)
696 {
697         struct hclge_cmq_ring *crq = &hdev->hw.cmq.crq;
698         struct hclge_respond_to_vf_msg resp_msg;
699         struct hclge_mbx_vf_to_pf_cmd *req;
700         struct hclge_vport *vport;
701         struct hclge_desc *desc;
702         bool is_del = false;
703         unsigned int flag;
704         int ret = 0;
705
706         /* handle all the mailbox requests in the queue */
707         while (!hclge_cmd_crq_empty(&hdev->hw)) {
708                 if (test_bit(HCLGE_STATE_CMD_DISABLE, &hdev->state)) {
709                         dev_warn(&hdev->pdev->dev,
710                                  "command queue needs re-initializing\n");
711                         return;
712                 }
713
714                 desc = &crq->desc[crq->next_to_use];
715                 req = (struct hclge_mbx_vf_to_pf_cmd *)desc->data;
716
717                 flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
718                 if (unlikely(!hnae3_get_bit(flag, HCLGE_CMDQ_RX_OUTVLD_B))) {
719                         dev_warn(&hdev->pdev->dev,
720                                  "dropped invalid mailbox message, code = %u\n",
721                                  req->msg.code);
722
723                         /* dropping/not processing this invalid message */
724                         crq->desc[crq->next_to_use].flag = 0;
725                         hclge_mbx_ring_ptr_move_crq(crq);
726                         continue;
727                 }
728
729                 vport = &hdev->vport[req->mbx_src_vfid];
730
731                 trace_hclge_pf_mbx_get(hdev, req);
732
733                 /* clear the resp_msg before processing every mailbox message */
734                 memset(&resp_msg, 0, sizeof(resp_msg));
735
736                 switch (req->msg.code) {
737                 case HCLGE_MBX_MAP_RING_TO_VECTOR:
738                         ret = hclge_map_unmap_ring_to_vf_vector(vport, true,
739                                                                 req);
740                         break;
741                 case HCLGE_MBX_UNMAP_RING_TO_VECTOR:
742                         ret = hclge_map_unmap_ring_to_vf_vector(vport, false,
743                                                                 req);
744                         break;
745                 case HCLGE_MBX_SET_PROMISC_MODE:
746                         hclge_set_vf_promisc_mode(vport, req);
747                         break;
748                 case HCLGE_MBX_SET_UNICAST:
749                         ret = hclge_set_vf_uc_mac_addr(vport, req);
750                         if (ret)
751                                 dev_err(&hdev->pdev->dev,
752                                         "PF fail(%d) to set VF UC MAC Addr\n",
753                                         ret);
754                         break;
755                 case HCLGE_MBX_SET_MULTICAST:
756                         ret = hclge_set_vf_mc_mac_addr(vport, req);
757                         if (ret)
758                                 dev_err(&hdev->pdev->dev,
759                                         "PF fail(%d) to set VF MC MAC Addr\n",
760                                         ret);
761                         break;
762                 case HCLGE_MBX_SET_VLAN:
763                         ret = hclge_set_vf_vlan_cfg(vport, req, &resp_msg);
764                         if (ret)
765                                 dev_err(&hdev->pdev->dev,
766                                         "PF failed(%d) to config VF's VLAN\n",
767                                         ret);
768                         break;
769                 case HCLGE_MBX_SET_ALIVE:
770                         ret = hclge_set_vf_alive(vport, req);
771                         if (ret)
772                                 dev_err(&hdev->pdev->dev,
773                                         "PF failed(%d) to set VF's ALIVE\n",
774                                         ret);
775                         break;
776                 case HCLGE_MBX_GET_QINFO:
777                         hclge_get_vf_queue_info(vport, &resp_msg);
778                         break;
779                 case HCLGE_MBX_GET_QDEPTH:
780                         hclge_get_vf_queue_depth(vport, &resp_msg);
781                         break;
782                 case HCLGE_MBX_GET_BASIC_INFO:
783                         hclge_get_basic_info(vport, &resp_msg);
784                         break;
785                 case HCLGE_MBX_GET_LINK_STATUS:
786                         ret = hclge_push_vf_link_status(vport);
787                         if (ret)
788                                 dev_err(&hdev->pdev->dev,
789                                         "failed to inform link stat to VF, ret = %d\n",
790                                         ret);
791                         break;
792                 case HCLGE_MBX_QUEUE_RESET:
793                         ret = hclge_mbx_reset_vf_queue(vport, req, &resp_msg);
794                         break;
795                 case HCLGE_MBX_RESET:
796                         ret = hclge_reset_vf(vport);
797                         break;
798                 case HCLGE_MBX_KEEP_ALIVE:
799                         hclge_vf_keep_alive(vport);
800                         break;
801                 case HCLGE_MBX_SET_MTU:
802                         ret = hclge_set_vf_mtu(vport, req);
803                         if (ret)
804                                 dev_err(&hdev->pdev->dev,
805                                         "VF fail(%d) to set mtu\n", ret);
806                         break;
807                 case HCLGE_MBX_GET_QID_IN_PF:
808                         hclge_get_queue_id_in_pf(vport, req, &resp_msg);
809                         break;
810                 case HCLGE_MBX_GET_RSS_KEY:
811                         hclge_get_rss_key(vport, req, &resp_msg);
812                         break;
813                 case HCLGE_MBX_GET_LINK_MODE:
814                         hclge_get_link_mode(vport, req);
815                         break;
816                 case HCLGE_MBX_GET_VF_FLR_STATUS:
817                 case HCLGE_MBX_VF_UNINIT:
818                         is_del = req->msg.code == HCLGE_MBX_VF_UNINIT;
819                         hclge_rm_vport_all_mac_table(vport, is_del,
820                                                      HCLGE_MAC_ADDR_UC);
821                         hclge_rm_vport_all_mac_table(vport, is_del,
822                                                      HCLGE_MAC_ADDR_MC);
823                         hclge_rm_vport_all_vlan_table(vport, is_del);
824                         break;
825                 case HCLGE_MBX_GET_MEDIA_TYPE:
826                         hclge_get_vf_media_type(vport, &resp_msg);
827                         break;
828                 case HCLGE_MBX_PUSH_LINK_STATUS:
829                         hclge_handle_link_change_event(hdev, req);
830                         break;
831                 case HCLGE_MBX_GET_MAC_ADDR:
832                         hclge_get_vf_mac_addr(vport, &resp_msg);
833                         break;
834                 case HCLGE_MBX_NCSI_ERROR:
835                         hclge_handle_ncsi_error(hdev);
836                         break;
837                 case HCLGE_MBX_HANDLE_VF_TBL:
838                         hclge_handle_vf_tbl(vport, req);
839                         break;
840                 default:
841                         dev_err(&hdev->pdev->dev,
842                                 "un-supported mailbox message, code = %u\n",
843                                 req->msg.code);
844                         break;
845                 }
846
847                 /* PF driver should not reply IMP */
848                 if (hnae3_get_bit(req->mbx_need_resp, HCLGE_MBX_NEED_RESP_B) &&
849                     req->msg.code < HCLGE_MBX_GET_VF_FLR_STATUS) {
850                         resp_msg.status = ret;
851                         hclge_gen_resp_to_vf(vport, req, &resp_msg);
852                 }
853
854                 crq->desc[crq->next_to_use].flag = 0;
855                 hclge_mbx_ring_ptr_move_crq(crq);
856
857                 /* reinitialize ret after complete the mbx message processing */
858                 ret = 0;
859         }
860
861         /* Write back CMDQ_RQ header pointer, M7 need this pointer */
862         hclge_write_dev(&hdev->hw, HCLGE_NIC_CRQ_HEAD_REG, crq->next_to_use);
863 }