1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Huawei HiNIC PCI Express Linux driver
3 * Copyright(c) 2017 Huawei Technologies Co., Ltd
6 #include <linux/delay.h>
7 #include <linux/types.h>
8 #include <linux/completion.h>
9 #include <linux/semaphore.h>
10 #include <linux/spinlock.h>
11 #include <linux/workqueue.h>
13 #include "hinic_hw_if.h"
14 #include "hinic_hw_mgmt.h"
15 #include "hinic_hw_csr.h"
16 #include "hinic_hw_dev.h"
17 #include "hinic_hw_mbox.h"
19 #define HINIC_MBOX_INT_DST_FUNC_SHIFT 0
20 #define HINIC_MBOX_INT_DST_AEQN_SHIFT 10
21 #define HINIC_MBOX_INT_SRC_RESP_AEQN_SHIFT 12
22 #define HINIC_MBOX_INT_STAT_DMA_SHIFT 14
23 /* The size of data to be sended (unit of 4 bytes) */
24 #define HINIC_MBOX_INT_TX_SIZE_SHIFT 20
25 /* SO_RO(strong order, relax order) */
26 #define HINIC_MBOX_INT_STAT_DMA_SO_RO_SHIFT 25
27 #define HINIC_MBOX_INT_WB_EN_SHIFT 28
29 #define HINIC_MBOX_INT_DST_FUNC_MASK 0x3FF
30 #define HINIC_MBOX_INT_DST_AEQN_MASK 0x3
31 #define HINIC_MBOX_INT_SRC_RESP_AEQN_MASK 0x3
32 #define HINIC_MBOX_INT_STAT_DMA_MASK 0x3F
33 #define HINIC_MBOX_INT_TX_SIZE_MASK 0x1F
34 #define HINIC_MBOX_INT_STAT_DMA_SO_RO_MASK 0x3
35 #define HINIC_MBOX_INT_WB_EN_MASK 0x1
37 #define HINIC_MBOX_INT_SET(val, field) \
38 (((val) & HINIC_MBOX_INT_##field##_MASK) << \
39 HINIC_MBOX_INT_##field##_SHIFT)
41 enum hinic_mbox_tx_status {
45 #define HINIC_MBOX_CTRL_TRIGGER_AEQE_SHIFT 0
47 /* specifies the issue request for the message data.
48 * 0 - Tx request is done;
49 * 1 - Tx request is in process.
51 #define HINIC_MBOX_CTRL_TX_STATUS_SHIFT 1
53 #define HINIC_MBOX_CTRL_TRIGGER_AEQE_MASK 0x1
54 #define HINIC_MBOX_CTRL_TX_STATUS_MASK 0x1
56 #define HINIC_MBOX_CTRL_SET(val, field) \
57 (((val) & HINIC_MBOX_CTRL_##field##_MASK) << \
58 HINIC_MBOX_CTRL_##field##_SHIFT)
60 #define HINIC_MBOX_HEADER_MSG_LEN_SHIFT 0
61 #define HINIC_MBOX_HEADER_MODULE_SHIFT 11
62 #define HINIC_MBOX_HEADER_SEG_LEN_SHIFT 16
63 #define HINIC_MBOX_HEADER_NO_ACK_SHIFT 22
64 #define HINIC_MBOX_HEADER_SEQID_SHIFT 24
65 #define HINIC_MBOX_HEADER_LAST_SHIFT 30
67 /* specifies the mailbox message direction
71 #define HINIC_MBOX_HEADER_DIRECTION_SHIFT 31
72 #define HINIC_MBOX_HEADER_CMD_SHIFT 32
73 #define HINIC_MBOX_HEADER_MSG_ID_SHIFT 40
74 #define HINIC_MBOX_HEADER_STATUS_SHIFT 48
75 #define HINIC_MBOX_HEADER_SRC_GLB_FUNC_IDX_SHIFT 54
77 #define HINIC_MBOX_HEADER_MSG_LEN_MASK 0x7FF
78 #define HINIC_MBOX_HEADER_MODULE_MASK 0x1F
79 #define HINIC_MBOX_HEADER_SEG_LEN_MASK 0x3F
80 #define HINIC_MBOX_HEADER_NO_ACK_MASK 0x1
81 #define HINIC_MBOX_HEADER_SEQID_MASK 0x3F
82 #define HINIC_MBOX_HEADER_LAST_MASK 0x1
83 #define HINIC_MBOX_HEADER_DIRECTION_MASK 0x1
84 #define HINIC_MBOX_HEADER_CMD_MASK 0xFF
85 #define HINIC_MBOX_HEADER_MSG_ID_MASK 0xFF
86 #define HINIC_MBOX_HEADER_STATUS_MASK 0x3F
87 #define HINIC_MBOX_HEADER_SRC_GLB_FUNC_IDX_MASK 0x3FF
89 #define HINIC_MBOX_HEADER_GET(val, field) \
90 (((val) >> HINIC_MBOX_HEADER_##field##_SHIFT) & \
91 HINIC_MBOX_HEADER_##field##_MASK)
92 #define HINIC_MBOX_HEADER_SET(val, field) \
93 ((u64)((val) & HINIC_MBOX_HEADER_##field##_MASK) << \
94 HINIC_MBOX_HEADER_##field##_SHIFT)
96 #define MBOX_SEGLEN_MASK \
97 HINIC_MBOX_HEADER_SET(HINIC_MBOX_HEADER_SEG_LEN_MASK, SEG_LEN)
99 #define HINIC_MBOX_SEG_LEN 48
100 #define HINIC_MBOX_COMP_TIME 8000U
101 #define MBOX_MSG_POLLING_TIMEOUT 8000
103 #define HINIC_MBOX_DATA_SIZE 2040
105 #define MBOX_MAX_BUF_SZ 2048UL
106 #define MBOX_HEADER_SZ 8
108 #define MBOX_INFO_SZ 4
110 /* MBOX size is 64B, 8B for mbox_header, 4B reserved */
111 #define MBOX_SEG_LEN 48
112 #define MBOX_SEG_LEN_ALIGN 4
113 #define MBOX_WB_STATUS_LEN 16UL
115 /* mbox write back status is 16B, only first 4B is used */
116 #define MBOX_WB_STATUS_ERRCODE_MASK 0xFFFF
117 #define MBOX_WB_STATUS_MASK 0xFF
118 #define MBOX_WB_ERROR_CODE_MASK 0xFF00
119 #define MBOX_WB_STATUS_FINISHED_SUCCESS 0xFF
120 #define MBOX_WB_STATUS_NOT_FINISHED 0x00
122 #define MBOX_STATUS_FINISHED(wb) \
123 (((wb) & MBOX_WB_STATUS_MASK) != MBOX_WB_STATUS_NOT_FINISHED)
124 #define MBOX_STATUS_SUCCESS(wb) \
125 (((wb) & MBOX_WB_STATUS_MASK) == MBOX_WB_STATUS_FINISHED_SUCCESS)
126 #define MBOX_STATUS_ERRCODE(wb) \
127 ((wb) & MBOX_WB_ERROR_CODE_MASK)
129 #define SEQ_ID_START_VAL 0
130 #define SEQ_ID_MAX_VAL 42
132 #define NO_DMA_ATTRIBUTE_VAL 0
134 #define HINIC_MBOX_RSP_AEQN 2
135 #define HINIC_MBOX_RECV_AEQN 0
137 #define MBOX_MSG_NO_DATA_LEN 1
139 #define MBOX_BODY_FROM_HDR(header) ((u8 *)(header) + MBOX_HEADER_SZ)
140 #define MBOX_AREA(hwif) \
141 ((hwif)->cfg_regs_bar + HINIC_FUNC_CSR_MAILBOX_DATA_OFF)
143 #define IS_PF_OR_PPF_SRC(src_func_idx) ((src_func_idx) < HINIC_MAX_PF_FUNCS)
145 #define MBOX_MSG_ID_MASK 0xFF
146 #define MBOX_MSG_ID(func_to_func) ((func_to_func)->send_msg_id)
147 #define MBOX_MSG_ID_INC(func_to_func_mbox) (MBOX_MSG_ID(func_to_func_mbox) = \
148 (MBOX_MSG_ID(func_to_func_mbox) + 1) & MBOX_MSG_ID_MASK)
150 #define FUNC_ID_OFF_SET_8B 8
152 /* max message counter wait to process for one function */
153 #define HINIC_MAX_MSG_CNT_TO_PROCESS 10
155 #define HINIC_QUEUE_MIN_DEPTH 6
156 #define HINIC_QUEUE_MAX_DEPTH 12
157 #define HINIC_MAX_RX_BUFFER_SIZE 15
159 enum hinic_hwif_direction_type {
160 HINIC_HWIF_DIRECT_SEND = 0,
161 HINIC_HWIF_RESPONSE = 1,
173 enum mbox_ordering_type {
177 enum mbox_write_back_type {
181 enum mbox_aeq_trig_type {
186 static bool check_func_id(struct hinic_hwdev *hwdev, u16 src_func_idx,
187 const void *buf_in, u16 in_size, u16 offset)
191 if (in_size < offset + sizeof(func_idx)) {
192 dev_warn(&hwdev->hwif->pdev->dev,
193 "Receive mailbox msg len: %d less than %d Bytes is invalid\n",
198 func_idx = *((u16 *)((u8 *)buf_in + offset));
200 if (src_func_idx != func_idx) {
201 dev_warn(&hwdev->hwif->pdev->dev,
202 "Receive mailbox function id: 0x%x not equal to msg function id: 0x%x\n",
203 src_func_idx, func_idx);
210 bool hinic_mbox_check_func_id_8B(struct hinic_hwdev *hwdev, u16 func_idx,
211 void *buf_in, u16 in_size)
213 return check_func_id(hwdev, func_idx, buf_in, in_size,
218 * hinic_register_pf_mbox_cb - register mbox callback for pf
219 * @hwdev: the pointer to hw device
220 * @mod: specific mod that the callback will handle
221 * @callback: callback function
222 * Return: 0 - success, negative - failure
224 int hinic_register_pf_mbox_cb(struct hinic_hwdev *hwdev,
225 enum hinic_mod_type mod,
226 hinic_pf_mbox_cb callback)
228 struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
230 if (mod >= HINIC_MOD_MAX)
233 func_to_func->pf_mbox_cb[mod] = callback;
235 set_bit(HINIC_PF_MBOX_CB_REG, &func_to_func->pf_mbox_cb_state[mod]);
241 * hinic_register_vf_mbox_cb - register mbox callback for vf
242 * @hwdev: the pointer to hw device
243 * @mod: specific mod that the callback will handle
244 * @callback: callback function
245 * Return: 0 - success, negative - failure
247 int hinic_register_vf_mbox_cb(struct hinic_hwdev *hwdev,
248 enum hinic_mod_type mod,
249 hinic_vf_mbox_cb callback)
251 struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
253 if (mod >= HINIC_MOD_MAX)
256 func_to_func->vf_mbox_cb[mod] = callback;
258 set_bit(HINIC_VF_MBOX_CB_REG, &func_to_func->vf_mbox_cb_state[mod]);
264 * hinic_unregister_pf_mbox_cb - unregister the mbox callback for pf
265 * @hwdev: the pointer to hw device
266 * @mod: specific mod that the callback will handle
268 void hinic_unregister_pf_mbox_cb(struct hinic_hwdev *hwdev,
269 enum hinic_mod_type mod)
271 struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
273 clear_bit(HINIC_PF_MBOX_CB_REG, &func_to_func->pf_mbox_cb_state[mod]);
275 while (test_bit(HINIC_PF_MBOX_CB_RUNNING,
276 &func_to_func->pf_mbox_cb_state[mod]))
277 usleep_range(900, 1000);
279 func_to_func->pf_mbox_cb[mod] = NULL;
283 * hinic_unregister_vf_mbox_cb - unregister the mbox callback for vf
284 * @hwdev: the pointer to hw device
285 * @mod: specific mod that the callback will handle
287 void hinic_unregister_vf_mbox_cb(struct hinic_hwdev *hwdev,
288 enum hinic_mod_type mod)
290 struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
292 clear_bit(HINIC_VF_MBOX_CB_REG, &func_to_func->vf_mbox_cb_state[mod]);
294 while (test_bit(HINIC_VF_MBOX_CB_RUNNING,
295 &func_to_func->vf_mbox_cb_state[mod]))
296 usleep_range(900, 1000);
298 func_to_func->vf_mbox_cb[mod] = NULL;
301 static int recv_vf_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
302 struct hinic_recv_mbox *recv_mbox,
303 void *buf_out, u16 *out_size)
308 if (recv_mbox->mod >= HINIC_MOD_MAX) {
309 dev_err(&func_to_func->hwif->pdev->dev, "Receive illegal mbox message, mod = %d\n",
314 set_bit(HINIC_VF_MBOX_CB_RUNNING,
315 &func_to_func->vf_mbox_cb_state[recv_mbox->mod]);
317 cb = func_to_func->vf_mbox_cb[recv_mbox->mod];
318 if (cb && test_bit(HINIC_VF_MBOX_CB_REG,
319 &func_to_func->vf_mbox_cb_state[recv_mbox->mod])) {
320 cb(func_to_func->hwdev, recv_mbox->cmd, recv_mbox->mbox,
321 recv_mbox->mbox_len, buf_out, out_size);
323 dev_err(&func_to_func->hwif->pdev->dev, "VF mbox cb is not registered\n");
327 clear_bit(HINIC_VF_MBOX_CB_RUNNING,
328 &func_to_func->vf_mbox_cb_state[recv_mbox->mod]);
334 recv_pf_from_vf_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
335 struct hinic_recv_mbox *recv_mbox,
336 u16 src_func_idx, void *buf_out,
343 if (recv_mbox->mod >= HINIC_MOD_MAX) {
344 dev_err(&func_to_func->hwif->pdev->dev, "Receive illegal mbox message, mod = %d\n",
349 set_bit(HINIC_PF_MBOX_CB_RUNNING,
350 &func_to_func->pf_mbox_cb_state[recv_mbox->mod]);
352 cb = func_to_func->pf_mbox_cb[recv_mbox->mod];
353 if (cb && test_bit(HINIC_PF_MBOX_CB_REG,
354 &func_to_func->pf_mbox_cb_state[recv_mbox->mod])) {
355 vf_id = src_func_idx -
356 hinic_glb_pf_vf_offset(func_to_func->hwif);
357 ret = cb(func_to_func->hwdev, vf_id, recv_mbox->cmd,
358 recv_mbox->mbox, recv_mbox->mbox_len,
361 dev_err(&func_to_func->hwif->pdev->dev, "PF mbox mod(0x%x) cb is not registered\n",
366 clear_bit(HINIC_PF_MBOX_CB_RUNNING,
367 &func_to_func->pf_mbox_cb_state[recv_mbox->mod]);
372 static bool check_mbox_seq_id_and_seg_len(struct hinic_recv_mbox *recv_mbox,
373 u8 seq_id, u8 seg_len)
375 if (seq_id > SEQ_ID_MAX_VAL || seg_len > MBOX_SEG_LEN)
379 recv_mbox->seq_id = seq_id;
381 if (seq_id != recv_mbox->seq_id + 1)
384 recv_mbox->seq_id = seq_id;
390 static void resp_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
391 struct hinic_recv_mbox *recv_mbox)
393 spin_lock(&func_to_func->mbox_lock);
394 if (recv_mbox->msg_info.msg_id == func_to_func->send_msg_id &&
395 func_to_func->event_flag == EVENT_START)
396 complete(&recv_mbox->recv_done);
398 dev_err(&func_to_func->hwif->pdev->dev,
399 "Mbox response timeout, current send msg id(0x%x), recv msg id(0x%x), status(0x%x)\n",
400 func_to_func->send_msg_id, recv_mbox->msg_info.msg_id,
401 recv_mbox->msg_info.status);
402 spin_unlock(&func_to_func->mbox_lock);
405 static void recv_func_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
406 struct hinic_recv_mbox *recv_mbox,
409 static void recv_func_mbox_work_handler(struct work_struct *work)
411 struct hinic_mbox_work *mbox_work =
412 container_of(work, struct hinic_mbox_work, work);
413 struct hinic_recv_mbox *recv_mbox;
415 recv_func_mbox_handler(mbox_work->func_to_func, mbox_work->recv_mbox,
416 mbox_work->src_func_idx);
419 &mbox_work->func_to_func->mbox_send[mbox_work->src_func_idx];
421 atomic_dec(&recv_mbox->msg_cnt);
426 static void recv_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
427 void *header, struct hinic_recv_mbox *recv_mbox)
429 void *mbox_body = MBOX_BODY_FROM_HDR(header);
430 struct hinic_recv_mbox *rcv_mbox_temp = NULL;
431 u64 mbox_header = *((u64 *)header);
432 struct hinic_mbox_work *mbox_work;
437 seq_id = HINIC_MBOX_HEADER_GET(mbox_header, SEQID);
438 seg_len = HINIC_MBOX_HEADER_GET(mbox_header, SEG_LEN);
439 src_func_idx = HINIC_MBOX_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
441 if (!check_mbox_seq_id_and_seg_len(recv_mbox, seq_id, seg_len)) {
442 dev_err(&func_to_func->hwif->pdev->dev,
443 "Mailbox sequence and segment check fail, src func id: 0x%x, front id: 0x%x, current id: 0x%x, seg len: 0x%x\n",
444 src_func_idx, recv_mbox->seq_id, seq_id, seg_len);
445 recv_mbox->seq_id = SEQ_ID_MAX_VAL;
449 pos = seq_id * MBOX_SEG_LEN;
450 memcpy((u8 *)recv_mbox->mbox + pos, mbox_body,
451 HINIC_MBOX_HEADER_GET(mbox_header, SEG_LEN));
453 if (!HINIC_MBOX_HEADER_GET(mbox_header, LAST))
456 recv_mbox->cmd = HINIC_MBOX_HEADER_GET(mbox_header, CMD);
457 recv_mbox->mod = HINIC_MBOX_HEADER_GET(mbox_header, MODULE);
458 recv_mbox->mbox_len = HINIC_MBOX_HEADER_GET(mbox_header, MSG_LEN);
459 recv_mbox->ack_type = HINIC_MBOX_HEADER_GET(mbox_header, NO_ACK);
460 recv_mbox->msg_info.msg_id = HINIC_MBOX_HEADER_GET(mbox_header, MSG_ID);
461 recv_mbox->msg_info.status = HINIC_MBOX_HEADER_GET(mbox_header, STATUS);
462 recv_mbox->seq_id = SEQ_ID_MAX_VAL;
464 if (HINIC_MBOX_HEADER_GET(mbox_header, DIRECTION) ==
465 HINIC_HWIF_RESPONSE) {
466 resp_mbox_handler(func_to_func, recv_mbox);
470 if (atomic_read(&recv_mbox->msg_cnt) > HINIC_MAX_MSG_CNT_TO_PROCESS) {
471 dev_warn(&func_to_func->hwif->pdev->dev,
472 "This function(%u) have %d message wait to process,can't add to work queue\n",
473 src_func_idx, atomic_read(&recv_mbox->msg_cnt));
477 rcv_mbox_temp = kmemdup(recv_mbox, sizeof(*rcv_mbox_temp), GFP_KERNEL);
481 rcv_mbox_temp->mbox = kmemdup(recv_mbox->mbox, MBOX_MAX_BUF_SZ,
483 if (!rcv_mbox_temp->mbox)
484 goto err_alloc_rcv_mbox_msg;
486 rcv_mbox_temp->buf_out = kzalloc(MBOX_MAX_BUF_SZ, GFP_KERNEL);
487 if (!rcv_mbox_temp->buf_out)
488 goto err_alloc_rcv_mbox_buf;
490 mbox_work = kzalloc(sizeof(*mbox_work), GFP_KERNEL);
492 goto err_alloc_mbox_work;
494 mbox_work->func_to_func = func_to_func;
495 mbox_work->recv_mbox = rcv_mbox_temp;
496 mbox_work->src_func_idx = src_func_idx;
498 atomic_inc(&recv_mbox->msg_cnt);
499 INIT_WORK(&mbox_work->work, recv_func_mbox_work_handler);
500 queue_work(func_to_func->workq, &mbox_work->work);
505 kfree(rcv_mbox_temp->buf_out);
507 err_alloc_rcv_mbox_buf:
508 kfree(rcv_mbox_temp->mbox);
510 err_alloc_rcv_mbox_msg:
511 kfree(rcv_mbox_temp);
514 static int set_vf_mbox_random_id(struct hinic_hwdev *hwdev, u16 func_id)
516 struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
517 struct hinic_set_random_id rand_info = {0};
518 u16 out_size = sizeof(rand_info);
519 struct hinic_pfhwdev *pfhwdev;
522 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
524 rand_info.version = HINIC_CMD_VER_FUNC_ID;
525 rand_info.func_idx = func_id;
526 rand_info.vf_in_pf = func_id - hinic_glb_pf_vf_offset(hwdev->hwif);
527 rand_info.random_id = get_random_u32();
529 func_to_func->vf_mbx_rand_id[func_id] = rand_info.random_id;
531 ret = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
532 HINIC_MGMT_CMD_SET_VF_RANDOM_ID,
533 &rand_info, sizeof(rand_info),
534 &rand_info, &out_size, HINIC_MGMT_MSG_SYNC);
535 if ((rand_info.status != HINIC_MGMT_CMD_UNSUPPORTED &&
536 rand_info.status) || !out_size || ret) {
537 dev_err(&hwdev->hwif->pdev->dev, "Set VF random id failed, err: %d, status: 0x%x, out size: 0x%x\n",
538 ret, rand_info.status, out_size);
542 if (rand_info.status == HINIC_MGMT_CMD_UNSUPPORTED)
543 return rand_info.status;
545 func_to_func->vf_mbx_old_rand_id[func_id] =
546 func_to_func->vf_mbx_rand_id[func_id];
551 static void update_random_id_work_handler(struct work_struct *work)
553 struct hinic_mbox_work *mbox_work =
554 container_of(work, struct hinic_mbox_work, work);
555 struct hinic_mbox_func_to_func *func_to_func;
556 u16 src = mbox_work->src_func_idx;
558 func_to_func = mbox_work->func_to_func;
560 if (set_vf_mbox_random_id(func_to_func->hwdev, src))
561 dev_warn(&func_to_func->hwdev->hwif->pdev->dev, "Update VF id: 0x%x random id failed\n",
562 mbox_work->src_func_idx);
567 static bool check_vf_mbox_random_id(struct hinic_mbox_func_to_func *func_to_func,
570 struct hinic_hwdev *hwdev = func_to_func->hwdev;
571 struct hinic_mbox_work *mbox_work = NULL;
572 u64 mbox_header = *((u64 *)header);
577 src = HINIC_MBOX_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
579 if (IS_PF_OR_PPF_SRC(src) || !func_to_func->support_vf_random)
582 if (!HINIC_IS_PPF(hwdev->hwif)) {
583 offset = hinic_glb_pf_vf_offset(hwdev->hwif);
584 vf_in_pf = src - offset;
586 if (vf_in_pf < 1 || vf_in_pf > hwdev->nic_cap.max_vf) {
587 dev_warn(&hwdev->hwif->pdev->dev,
588 "Receive vf id(0x%x) is invalid, vf id should be from 0x%x to 0x%x\n",
590 hwdev->nic_cap.max_vf + offset);
595 random_id = be32_to_cpu(*(u32 *)(header + MBOX_SEG_LEN +
598 if (random_id == func_to_func->vf_mbx_rand_id[src] ||
599 random_id == func_to_func->vf_mbx_old_rand_id[src])
602 dev_warn(&hwdev->hwif->pdev->dev,
603 "The mailbox random id(0x%x) of func_id(0x%x) doesn't match with pf reservation(0x%x)\n",
604 random_id, src, func_to_func->vf_mbx_rand_id[src]);
606 mbox_work = kzalloc(sizeof(*mbox_work), GFP_KERNEL);
610 mbox_work->func_to_func = func_to_func;
611 mbox_work->src_func_idx = src;
613 INIT_WORK(&mbox_work->work, update_random_id_work_handler);
614 queue_work(func_to_func->workq, &mbox_work->work);
619 static void hinic_mbox_func_aeqe_handler(void *handle, void *header, u8 size)
621 struct hinic_mbox_func_to_func *func_to_func;
622 u64 mbox_header = *((u64 *)header);
623 struct hinic_recv_mbox *recv_mbox;
626 func_to_func = ((struct hinic_hwdev *)handle)->func_to_func;
628 dir = HINIC_MBOX_HEADER_GET(mbox_header, DIRECTION);
629 src = HINIC_MBOX_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
631 if (src >= HINIC_MAX_FUNCTIONS) {
632 dev_err(&func_to_func->hwif->pdev->dev,
633 "Mailbox source function id:%u is invalid\n", (u32)src);
637 if (!check_vf_mbox_random_id(func_to_func, header))
640 recv_mbox = (dir == HINIC_HWIF_DIRECT_SEND) ?
641 &func_to_func->mbox_send[src] :
642 &func_to_func->mbox_resp[src];
644 recv_mbox_handler(func_to_func, (u64 *)header, recv_mbox);
647 static void hinic_mbox_self_aeqe_handler(void *handle, void *header, u8 size)
649 struct hinic_mbox_func_to_func *func_to_func;
650 struct hinic_send_mbox *send_mbox;
652 func_to_func = ((struct hinic_hwdev *)handle)->func_to_func;
653 send_mbox = &func_to_func->send_mbox;
655 complete(&send_mbox->send_done);
658 static void clear_mbox_status(struct hinic_send_mbox *mbox)
660 *mbox->wb_status = 0;
662 /* clear mailbox write back status */
666 static void mbox_copy_header(struct hinic_hwdev *hwdev,
667 struct hinic_send_mbox *mbox, u64 *header)
669 u32 i, idx_max = MBOX_HEADER_SZ / sizeof(u32);
670 u32 *data = (u32 *)header;
672 for (i = 0; i < idx_max; i++)
673 __raw_writel(*(data + i), mbox->data + i * sizeof(u32));
676 static void mbox_copy_send_data(struct hinic_hwdev *hwdev,
677 struct hinic_send_mbox *mbox, void *seg,
680 u8 mbox_max_buf[MBOX_SEG_LEN] = {0};
681 u32 data_len, chk_sz = sizeof(u32);
685 /* The mbox message should be aligned in 4 bytes. */
686 if (seg_len % chk_sz) {
687 memcpy(mbox_max_buf, seg, seg_len);
688 data = (u32 *)mbox_max_buf;
692 idx_max = ALIGN(data_len, chk_sz) / chk_sz;
694 for (i = 0; i < idx_max; i++)
695 __raw_writel(*(data + i),
696 mbox->data + MBOX_HEADER_SZ + i * sizeof(u32));
699 static void write_mbox_msg_attr(struct hinic_mbox_func_to_func *func_to_func,
700 u16 dst_func, u16 dst_aeqn, u16 seg_len,
703 u16 rsp_aeq = (dst_aeqn == 0) ? 0 : HINIC_MBOX_RSP_AEQN;
704 u32 mbox_int, mbox_ctrl;
706 mbox_int = HINIC_MBOX_INT_SET(dst_func, DST_FUNC) |
707 HINIC_MBOX_INT_SET(dst_aeqn, DST_AEQN) |
708 HINIC_MBOX_INT_SET(rsp_aeq, SRC_RESP_AEQN) |
709 HINIC_MBOX_INT_SET(NO_DMA_ATTRIBUTE_VAL, STAT_DMA) |
710 HINIC_MBOX_INT_SET(ALIGN(MBOX_SEG_LEN + MBOX_HEADER_SZ +
711 MBOX_INFO_SZ, MBOX_SEG_LEN_ALIGN) >> 2,
713 HINIC_MBOX_INT_SET(STRONG_ORDER, STAT_DMA_SO_RO) |
714 HINIC_MBOX_INT_SET(WRITE_BACK, WB_EN);
716 hinic_hwif_write_reg(func_to_func->hwif,
717 HINIC_FUNC_CSR_MAILBOX_INT_OFFSET_OFF, mbox_int);
719 wmb(); /* writing the mbox int attributes */
720 mbox_ctrl = HINIC_MBOX_CTRL_SET(TX_NOT_DONE, TX_STATUS);
723 mbox_ctrl |= HINIC_MBOX_CTRL_SET(NOT_TRIGGER, TRIGGER_AEQE);
725 mbox_ctrl |= HINIC_MBOX_CTRL_SET(TRIGGER, TRIGGER_AEQE);
727 hinic_hwif_write_reg(func_to_func->hwif,
728 HINIC_FUNC_CSR_MAILBOX_CONTROL_OFF, mbox_ctrl);
731 static void dump_mox_reg(struct hinic_hwdev *hwdev)
735 val = hinic_hwif_read_reg(hwdev->hwif,
736 HINIC_FUNC_CSR_MAILBOX_CONTROL_OFF);
737 dev_err(&hwdev->hwif->pdev->dev, "Mailbox control reg: 0x%x\n", val);
739 val = hinic_hwif_read_reg(hwdev->hwif,
740 HINIC_FUNC_CSR_MAILBOX_INT_OFFSET_OFF);
741 dev_err(&hwdev->hwif->pdev->dev, "Mailbox interrupt offset: 0x%x\n",
745 static u16 get_mbox_status(struct hinic_send_mbox *mbox)
747 /* write back is 16B, but only use first 4B */
748 u64 wb_val = be64_to_cpu(*mbox->wb_status);
750 rmb(); /* verify reading before check */
752 return (u16)(wb_val & MBOX_WB_STATUS_ERRCODE_MASK);
756 wait_for_mbox_seg_completion(struct hinic_mbox_func_to_func *func_to_func,
757 int poll, u16 *wb_status)
759 struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
760 struct hinic_hwdev *hwdev = func_to_func->hwdev;
761 struct completion *done = &send_mbox->send_done;
766 while (cnt < MBOX_MSG_POLLING_TIMEOUT) {
767 *wb_status = get_mbox_status(send_mbox);
768 if (MBOX_STATUS_FINISHED(*wb_status))
771 usleep_range(900, 1000);
775 if (cnt == MBOX_MSG_POLLING_TIMEOUT) {
776 dev_err(&hwdev->hwif->pdev->dev, "Send mailbox segment timeout, wb status: 0x%x\n",
782 jif = msecs_to_jiffies(HINIC_MBOX_COMP_TIME);
783 if (!wait_for_completion_timeout(done, jif)) {
784 dev_err(&hwdev->hwif->pdev->dev, "Send mailbox segment timeout\n");
786 hinic_dump_aeq_info(hwdev);
790 *wb_status = get_mbox_status(send_mbox);
796 static int send_mbox_seg(struct hinic_mbox_func_to_func *func_to_func,
797 u64 header, u16 dst_func, void *seg, u16 seg_len,
798 int poll, void *msg_info)
800 struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
801 u16 seq_dir = HINIC_MBOX_HEADER_GET(header, DIRECTION);
802 struct hinic_hwdev *hwdev = func_to_func->hwdev;
803 struct completion *done = &send_mbox->send_done;
804 u8 num_aeqs = hwdev->hwif->attr.num_aeqs;
805 u16 dst_aeqn, wb_status = 0, errcode;
808 dst_aeqn = (seq_dir == HINIC_HWIF_DIRECT_SEND) ?
809 HINIC_MBOX_RECV_AEQN : HINIC_MBOX_RSP_AEQN;
814 init_completion(done);
816 clear_mbox_status(send_mbox);
818 mbox_copy_header(hwdev, send_mbox, &header);
820 mbox_copy_send_data(hwdev, send_mbox, seg, seg_len);
822 write_mbox_msg_attr(func_to_func, dst_func, dst_aeqn, seg_len, poll);
824 wmb(); /* writing the mbox msg attributes */
826 if (wait_for_mbox_seg_completion(func_to_func, poll, &wb_status))
829 if (!MBOX_STATUS_SUCCESS(wb_status)) {
830 dev_err(&hwdev->hwif->pdev->dev, "Send mailbox segment to function %d error, wb status: 0x%x\n",
831 dst_func, wb_status);
832 errcode = MBOX_STATUS_ERRCODE(wb_status);
833 return errcode ? errcode : -EFAULT;
839 static int send_mbox_to_func(struct hinic_mbox_func_to_func *func_to_func,
840 enum hinic_mod_type mod, u16 cmd, void *msg,
841 u16 msg_len, u16 dst_func,
842 enum hinic_hwif_direction_type direction,
843 enum hinic_mbox_ack_type ack_type,
844 struct mbox_msg_info *msg_info)
846 struct hinic_hwdev *hwdev = func_to_func->hwdev;
847 u16 seg_len = MBOX_SEG_LEN;
848 u8 *msg_seg = (u8 *)msg;
854 down(&func_to_func->msg_send_sem);
856 header = HINIC_MBOX_HEADER_SET(msg_len, MSG_LEN) |
857 HINIC_MBOX_HEADER_SET(mod, MODULE) |
858 HINIC_MBOX_HEADER_SET(seg_len, SEG_LEN) |
859 HINIC_MBOX_HEADER_SET(ack_type, NO_ACK) |
860 HINIC_MBOX_HEADER_SET(SEQ_ID_START_VAL, SEQID) |
861 HINIC_MBOX_HEADER_SET(NOT_LAST_SEG, LAST) |
862 HINIC_MBOX_HEADER_SET(direction, DIRECTION) |
863 HINIC_MBOX_HEADER_SET(cmd, CMD) |
864 /* The vf's offset to it's associated pf */
865 HINIC_MBOX_HEADER_SET(msg_info->msg_id, MSG_ID) |
866 HINIC_MBOX_HEADER_SET(msg_info->status, STATUS) |
867 HINIC_MBOX_HEADER_SET(hinic_global_func_id_hw(hwdev->hwif),
870 while (!(HINIC_MBOX_HEADER_GET(header, LAST))) {
871 if (left <= HINIC_MBOX_SEG_LEN) {
872 header &= ~MBOX_SEGLEN_MASK;
873 header |= HINIC_MBOX_HEADER_SET(left, SEG_LEN);
874 header |= HINIC_MBOX_HEADER_SET(LAST_SEG, LAST);
879 err = send_mbox_seg(func_to_func, header, dst_func, msg_seg,
880 seg_len, MBOX_SEND_MSG_INT, msg_info);
882 dev_err(&hwdev->hwif->pdev->dev, "Failed to send mbox seg, seq_id=0x%llx\n",
883 HINIC_MBOX_HEADER_GET(header, SEQID));
884 goto err_send_mbox_seg;
887 left -= HINIC_MBOX_SEG_LEN;
888 msg_seg += HINIC_MBOX_SEG_LEN;
891 header &= ~(HINIC_MBOX_HEADER_SET(HINIC_MBOX_HEADER_SEQID_MASK,
893 header |= HINIC_MBOX_HEADER_SET(seq_id, SEQID);
897 up(&func_to_func->msg_send_sem);
903 response_for_recv_func_mbox(struct hinic_mbox_func_to_func *func_to_func,
904 struct hinic_recv_mbox *recv_mbox, int err,
905 u16 out_size, u16 src_func_idx)
907 struct mbox_msg_info msg_info = {0};
909 if (recv_mbox->ack_type == MBOX_ACK) {
910 msg_info.msg_id = recv_mbox->msg_info.msg_id;
911 if (err == HINIC_MBOX_PF_BUSY_ACTIVE_FW)
912 msg_info.status = HINIC_MBOX_PF_BUSY_ACTIVE_FW;
913 else if (err == HINIC_MBOX_VF_CMD_ERROR)
914 msg_info.status = HINIC_MBOX_VF_CMD_ERROR;
916 msg_info.status = HINIC_MBOX_PF_SEND_ERR;
918 /* if no data needs to response, set out_size to 1 */
919 if (!out_size || err)
920 out_size = MBOX_MSG_NO_DATA_LEN;
922 send_mbox_to_func(func_to_func, recv_mbox->mod, recv_mbox->cmd,
923 recv_mbox->buf_out, out_size, src_func_idx,
924 HINIC_HWIF_RESPONSE, MBOX_ACK,
929 static void recv_func_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
930 struct hinic_recv_mbox *recv_mbox,
933 void *buf_out = recv_mbox->buf_out;
934 u16 out_size = MBOX_MAX_BUF_SZ;
937 if (HINIC_IS_VF(func_to_func->hwif)) {
938 err = recv_vf_mbox_handler(func_to_func, recv_mbox, buf_out,
941 if (IS_PF_OR_PPF_SRC(src_func_idx))
942 dev_warn(&func_to_func->hwif->pdev->dev,
943 "Unsupported pf2pf mbox msg\n");
945 err = recv_pf_from_vf_mbox_handler(func_to_func,
951 response_for_recv_func_mbox(func_to_func, recv_mbox, err, out_size,
953 kfree(recv_mbox->buf_out);
954 kfree(recv_mbox->mbox);
958 static void set_mbox_to_func_event(struct hinic_mbox_func_to_func *func_to_func,
959 enum mbox_event_state event_flag)
961 spin_lock(&func_to_func->mbox_lock);
962 func_to_func->event_flag = event_flag;
963 spin_unlock(&func_to_func->mbox_lock);
966 static int mbox_resp_info_handler(struct hinic_mbox_func_to_func *func_to_func,
967 struct hinic_recv_mbox *mbox_for_resp,
968 enum hinic_mod_type mod, u16 cmd,
969 void *buf_out, u16 *out_size)
973 if (mbox_for_resp->msg_info.status) {
974 err = mbox_for_resp->msg_info.status;
975 if (err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
976 dev_err(&func_to_func->hwif->pdev->dev, "Mbox response error(0x%x)\n",
977 mbox_for_resp->msg_info.status);
981 if (buf_out && out_size) {
982 if (*out_size < mbox_for_resp->mbox_len) {
983 dev_err(&func_to_func->hwif->pdev->dev,
984 "Invalid response mbox message length: %d for mod %d cmd %d, should less than: %d\n",
985 mbox_for_resp->mbox_len, mod, cmd, *out_size);
989 if (mbox_for_resp->mbox_len)
990 memcpy(buf_out, mbox_for_resp->mbox,
991 mbox_for_resp->mbox_len);
993 *out_size = mbox_for_resp->mbox_len;
999 int hinic_mbox_to_func(struct hinic_mbox_func_to_func *func_to_func,
1000 enum hinic_mod_type mod, u16 cmd, u16 dst_func,
1001 void *buf_in, u16 in_size, void *buf_out,
1002 u16 *out_size, u32 timeout)
1004 struct hinic_recv_mbox *mbox_for_resp;
1005 struct mbox_msg_info msg_info = {0};
1006 unsigned long timeo;
1009 mbox_for_resp = &func_to_func->mbox_resp[dst_func];
1011 down(&func_to_func->mbox_send_sem);
1013 init_completion(&mbox_for_resp->recv_done);
1015 msg_info.msg_id = MBOX_MSG_ID_INC(func_to_func);
1017 set_mbox_to_func_event(func_to_func, EVENT_START);
1019 err = send_mbox_to_func(func_to_func, mod, cmd, buf_in, in_size,
1020 dst_func, HINIC_HWIF_DIRECT_SEND, MBOX_ACK,
1023 dev_err(&func_to_func->hwif->pdev->dev, "Send mailbox failed, msg_id: %d\n",
1025 set_mbox_to_func_event(func_to_func, EVENT_FAIL);
1029 timeo = msecs_to_jiffies(timeout ? timeout : HINIC_MBOX_COMP_TIME);
1030 if (!wait_for_completion_timeout(&mbox_for_resp->recv_done, timeo)) {
1031 set_mbox_to_func_event(func_to_func, EVENT_TIMEOUT);
1032 dev_err(&func_to_func->hwif->pdev->dev,
1033 "Send mbox msg timeout, msg_id: %d\n", msg_info.msg_id);
1034 hinic_dump_aeq_info(func_to_func->hwdev);
1039 set_mbox_to_func_event(func_to_func, EVENT_END);
1041 err = mbox_resp_info_handler(func_to_func, mbox_for_resp, mod, cmd,
1045 up(&func_to_func->mbox_send_sem);
1050 static int mbox_func_params_valid(struct hinic_mbox_func_to_func *func_to_func,
1051 void *buf_in, u16 in_size)
1053 if (in_size > HINIC_MBOX_DATA_SIZE) {
1054 dev_err(&func_to_func->hwif->pdev->dev,
1055 "Mbox msg len(%d) exceed limit(%d)\n",
1056 in_size, HINIC_MBOX_DATA_SIZE);
1063 int hinic_mbox_to_pf(struct hinic_hwdev *hwdev,
1064 enum hinic_mod_type mod, u8 cmd, void *buf_in,
1065 u16 in_size, void *buf_out, u16 *out_size, u32 timeout)
1067 struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
1068 int err = mbox_func_params_valid(func_to_func, buf_in, in_size);
1073 if (!HINIC_IS_VF(hwdev->hwif)) {
1074 dev_err(&hwdev->hwif->pdev->dev, "Params error, func_type: %d\n",
1075 HINIC_FUNC_TYPE(hwdev->hwif));
1079 return hinic_mbox_to_func(func_to_func, mod, cmd,
1080 hinic_pf_id_of_vf_hw(hwdev->hwif), buf_in,
1081 in_size, buf_out, out_size, timeout);
1084 int hinic_mbox_to_vf(struct hinic_hwdev *hwdev,
1085 enum hinic_mod_type mod, u16 vf_id, u8 cmd, void *buf_in,
1086 u16 in_size, void *buf_out, u16 *out_size, u32 timeout)
1088 struct hinic_mbox_func_to_func *func_to_func;
1095 func_to_func = hwdev->func_to_func;
1096 err = mbox_func_params_valid(func_to_func, buf_in, in_size);
1100 if (HINIC_IS_VF(hwdev->hwif)) {
1101 dev_err(&hwdev->hwif->pdev->dev, "Params error, func_type: %d\n",
1102 HINIC_FUNC_TYPE(hwdev->hwif));
1107 dev_err(&hwdev->hwif->pdev->dev,
1108 "VF id(%d) error!\n", vf_id);
1112 /* vf_offset_to_pf + vf_id is the vf's global function id of vf in
1115 dst_func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
1117 return hinic_mbox_to_func(func_to_func, mod, cmd, dst_func_idx, buf_in,
1118 in_size, buf_out, out_size, timeout);
1121 static int init_mbox_info(struct hinic_recv_mbox *mbox_info)
1125 mbox_info->seq_id = SEQ_ID_MAX_VAL;
1127 mbox_info->mbox = kzalloc(MBOX_MAX_BUF_SZ, GFP_KERNEL);
1128 if (!mbox_info->mbox)
1131 mbox_info->buf_out = kzalloc(MBOX_MAX_BUF_SZ, GFP_KERNEL);
1132 if (!mbox_info->buf_out) {
1134 goto err_alloc_buf_out;
1137 atomic_set(&mbox_info->msg_cnt, 0);
1142 kfree(mbox_info->mbox);
1147 static void clean_mbox_info(struct hinic_recv_mbox *mbox_info)
1149 kfree(mbox_info->buf_out);
1150 kfree(mbox_info->mbox);
1153 static int alloc_mbox_info(struct hinic_hwdev *hwdev,
1154 struct hinic_recv_mbox *mbox_info)
1159 for (func_idx = 0; func_idx < HINIC_MAX_FUNCTIONS; func_idx++) {
1160 err = init_mbox_info(&mbox_info[func_idx]);
1162 dev_err(&hwdev->hwif->pdev->dev, "Failed to init function %d mbox info\n",
1164 goto err_init_mbox_info;
1171 for (i = 0; i < func_idx; i++)
1172 clean_mbox_info(&mbox_info[i]);
1177 static void free_mbox_info(struct hinic_recv_mbox *mbox_info)
1181 for (func_idx = 0; func_idx < HINIC_MAX_FUNCTIONS; func_idx++)
1182 clean_mbox_info(&mbox_info[func_idx]);
1185 static void prepare_send_mbox(struct hinic_mbox_func_to_func *func_to_func)
1187 struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
1189 send_mbox->data = MBOX_AREA(func_to_func->hwif);
1192 static int alloc_mbox_wb_status(struct hinic_mbox_func_to_func *func_to_func)
1194 struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
1195 struct hinic_hwdev *hwdev = func_to_func->hwdev;
1198 send_mbox->wb_vaddr = dma_alloc_coherent(&hwdev->hwif->pdev->dev,
1200 &send_mbox->wb_paddr,
1202 if (!send_mbox->wb_vaddr)
1205 send_mbox->wb_status = send_mbox->wb_vaddr;
1207 addr_h = upper_32_bits(send_mbox->wb_paddr);
1208 addr_l = lower_32_bits(send_mbox->wb_paddr);
1210 hinic_hwif_write_reg(hwdev->hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_H_OFF,
1212 hinic_hwif_write_reg(hwdev->hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_L_OFF,
1218 static void free_mbox_wb_status(struct hinic_mbox_func_to_func *func_to_func)
1220 struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
1221 struct hinic_hwdev *hwdev = func_to_func->hwdev;
1223 hinic_hwif_write_reg(hwdev->hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_H_OFF,
1225 hinic_hwif_write_reg(hwdev->hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_L_OFF,
1228 dma_free_coherent(&hwdev->hwif->pdev->dev, MBOX_WB_STATUS_LEN,
1229 send_mbox->wb_vaddr,
1230 send_mbox->wb_paddr);
1233 bool hinic_mbox_check_cmd_valid(struct hinic_hwdev *hwdev,
1234 struct vf_cmd_check_handle *cmd_handle,
1235 u16 vf_id, u8 cmd, void *buf_in,
1236 u16 in_size, u8 size)
1238 u16 src_idx = vf_id + hinic_glb_pf_vf_offset(hwdev->hwif);
1241 for (i = 0; i < size; i++) {
1242 if (cmd == cmd_handle[i].cmd) {
1243 if (cmd_handle[i].check_cmd)
1244 return cmd_handle[i].check_cmd(hwdev, src_idx,
1251 dev_err(&hwdev->hwif->pdev->dev,
1252 "PF Receive VF(%d) unsupported cmd(0x%x)\n",
1253 vf_id + hinic_glb_pf_vf_offset(hwdev->hwif), cmd);
1258 static bool hinic_cmdq_check_vf_ctxt(struct hinic_hwdev *hwdev,
1259 struct hinic_cmdq_ctxt *cmdq_ctxt)
1261 struct hinic_cmdq_ctxt_info *ctxt_info = &cmdq_ctxt->ctxt_info;
1262 u64 curr_pg_pfn, wq_block_pfn;
1264 if (cmdq_ctxt->ppf_idx != HINIC_HWIF_PPF_IDX(hwdev->hwif) ||
1265 cmdq_ctxt->cmdq_type > HINIC_MAX_CMDQ_TYPES)
1268 curr_pg_pfn = HINIC_CMDQ_CTXT_PAGE_INFO_GET
1269 (ctxt_info->curr_wqe_page_pfn, CURR_WQE_PAGE_PFN);
1270 wq_block_pfn = HINIC_CMDQ_CTXT_BLOCK_INFO_GET
1271 (ctxt_info->wq_block_pfn, WQ_BLOCK_PFN);
1272 /* VF must use 0-level CLA */
1273 if (curr_pg_pfn != wq_block_pfn)
1279 static bool check_cmdq_ctxt(struct hinic_hwdev *hwdev, u16 func_idx,
1280 void *buf_in, u16 in_size)
1282 if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size))
1285 return hinic_cmdq_check_vf_ctxt(hwdev, buf_in);
1288 #define HW_CTX_QPS_VALID(hw_ctxt) \
1289 ((hw_ctxt)->rq_depth >= HINIC_QUEUE_MIN_DEPTH && \
1290 (hw_ctxt)->rq_depth <= HINIC_QUEUE_MAX_DEPTH && \
1291 (hw_ctxt)->sq_depth >= HINIC_QUEUE_MIN_DEPTH && \
1292 (hw_ctxt)->sq_depth <= HINIC_QUEUE_MAX_DEPTH && \
1293 (hw_ctxt)->rx_buf_sz_idx <= HINIC_MAX_RX_BUFFER_SIZE)
1295 static bool hw_ctxt_qps_param_valid(struct hinic_cmd_hw_ioctxt *hw_ctxt)
1297 if (HW_CTX_QPS_VALID(hw_ctxt))
1300 if (!hw_ctxt->rq_depth && !hw_ctxt->sq_depth &&
1301 !hw_ctxt->rx_buf_sz_idx)
1307 static bool check_hwctxt(struct hinic_hwdev *hwdev, u16 func_idx,
1308 void *buf_in, u16 in_size)
1310 struct hinic_cmd_hw_ioctxt *hw_ctxt = buf_in;
1312 if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size))
1315 if (hw_ctxt->ppf_idx != HINIC_HWIF_PPF_IDX(hwdev->hwif))
1318 if (hw_ctxt->set_cmdq_depth) {
1319 if (hw_ctxt->cmdq_depth >= HINIC_QUEUE_MIN_DEPTH &&
1320 hw_ctxt->cmdq_depth <= HINIC_QUEUE_MAX_DEPTH)
1326 return hw_ctxt_qps_param_valid(hw_ctxt);
1329 static bool check_set_wq_page_size(struct hinic_hwdev *hwdev, u16 func_idx,
1330 void *buf_in, u16 in_size)
1332 struct hinic_wq_page_size *page_size_info = buf_in;
1334 if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size))
1337 if (page_size_info->ppf_idx != HINIC_HWIF_PPF_IDX(hwdev->hwif))
1340 if (((1U << page_size_info->page_size) * SZ_4K) !=
1341 HINIC_DEFAULT_WQ_PAGE_SIZE)
1347 static struct vf_cmd_check_handle hw_cmd_support_vf[] = {
1348 {HINIC_COMM_CMD_START_FLR, hinic_mbox_check_func_id_8B},
1349 {HINIC_COMM_CMD_DMA_ATTR_SET, hinic_mbox_check_func_id_8B},
1350 {HINIC_COMM_CMD_CMDQ_CTXT_SET, check_cmdq_ctxt},
1351 {HINIC_COMM_CMD_CMDQ_CTXT_GET, check_cmdq_ctxt},
1352 {HINIC_COMM_CMD_HWCTXT_SET, check_hwctxt},
1353 {HINIC_COMM_CMD_HWCTXT_GET, check_hwctxt},
1354 {HINIC_COMM_CMD_SQ_HI_CI_SET, hinic_mbox_check_func_id_8B},
1355 {HINIC_COMM_CMD_RES_STATE_SET, hinic_mbox_check_func_id_8B},
1356 {HINIC_COMM_CMD_IO_RES_CLEAR, hinic_mbox_check_func_id_8B},
1357 {HINIC_COMM_CMD_CEQ_CTRL_REG_WR_BY_UP, hinic_mbox_check_func_id_8B},
1358 {HINIC_COMM_CMD_MSI_CTRL_REG_WR_BY_UP, hinic_mbox_check_func_id_8B},
1359 {HINIC_COMM_CMD_MSI_CTRL_REG_RD_BY_UP, hinic_mbox_check_func_id_8B},
1360 {HINIC_COMM_CMD_L2NIC_RESET, hinic_mbox_check_func_id_8B},
1361 {HINIC_COMM_CMD_PAGESIZE_SET, check_set_wq_page_size},
1364 static int comm_pf_mbox_handler(void *handle, u16 vf_id, u8 cmd, void *buf_in,
1365 u16 in_size, void *buf_out, u16 *out_size)
1367 u8 size = ARRAY_SIZE(hw_cmd_support_vf);
1368 struct hinic_hwdev *hwdev = handle;
1369 struct hinic_pfhwdev *pfhwdev;
1372 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
1374 if (!hinic_mbox_check_cmd_valid(handle, hw_cmd_support_vf, vf_id, cmd,
1375 buf_in, in_size, size)) {
1376 dev_err(&hwdev->hwif->pdev->dev,
1377 "PF Receive VF: %d common cmd: 0x%x or mbox len: 0x%x is invalid\n",
1378 vf_id + hinic_glb_pf_vf_offset(hwdev->hwif), cmd,
1380 return HINIC_MBOX_VF_CMD_ERROR;
1383 if (cmd == HINIC_COMM_CMD_START_FLR) {
1386 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
1387 cmd, buf_in, in_size, buf_out, out_size,
1388 HINIC_MGMT_MSG_SYNC);
1389 if (err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
1390 dev_err(&hwdev->hwif->pdev->dev,
1391 "PF mbox common callback handler err: %d\n",
1398 int hinic_func_to_func_init(struct hinic_hwdev *hwdev)
1400 struct hinic_mbox_func_to_func *func_to_func;
1401 struct hinic_pfhwdev *pfhwdev;
1404 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
1405 func_to_func = kzalloc(sizeof(*func_to_func), GFP_KERNEL);
1409 hwdev->func_to_func = func_to_func;
1410 func_to_func->hwdev = hwdev;
1411 func_to_func->hwif = hwdev->hwif;
1412 sema_init(&func_to_func->mbox_send_sem, 1);
1413 sema_init(&func_to_func->msg_send_sem, 1);
1414 spin_lock_init(&func_to_func->mbox_lock);
1415 func_to_func->workq = create_singlethread_workqueue(HINIC_MBOX_WQ_NAME);
1416 if (!func_to_func->workq) {
1417 dev_err(&hwdev->hwif->pdev->dev, "Failed to initialize MBOX workqueue\n");
1419 goto err_create_mbox_workq;
1422 err = alloc_mbox_info(hwdev, func_to_func->mbox_send);
1424 dev_err(&hwdev->hwif->pdev->dev, "Failed to alloc mem for mbox_active\n");
1425 goto err_alloc_mbox_for_send;
1428 err = alloc_mbox_info(hwdev, func_to_func->mbox_resp);
1430 dev_err(&hwdev->hwif->pdev->dev, "Failed to alloc mem for mbox_passive\n");
1431 goto err_alloc_mbox_for_resp;
1434 err = alloc_mbox_wb_status(func_to_func);
1436 dev_err(&hwdev->hwif->pdev->dev, "Failed to alloc mbox write back status\n");
1437 goto err_alloc_wb_status;
1440 prepare_send_mbox(func_to_func);
1442 hinic_aeq_register_hw_cb(&hwdev->aeqs, HINIC_MBX_FROM_FUNC,
1443 &pfhwdev->hwdev, hinic_mbox_func_aeqe_handler);
1444 hinic_aeq_register_hw_cb(&hwdev->aeqs, HINIC_MBX_SEND_RSLT,
1445 &pfhwdev->hwdev, hinic_mbox_self_aeqe_handler);
1447 if (!HINIC_IS_VF(hwdev->hwif))
1448 hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_COMM,
1449 comm_pf_mbox_handler);
1453 err_alloc_wb_status:
1454 free_mbox_info(func_to_func->mbox_resp);
1456 err_alloc_mbox_for_resp:
1457 free_mbox_info(func_to_func->mbox_send);
1459 err_alloc_mbox_for_send:
1460 destroy_workqueue(func_to_func->workq);
1462 err_create_mbox_workq:
1463 kfree(func_to_func);
1468 void hinic_func_to_func_free(struct hinic_hwdev *hwdev)
1470 struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
1472 hinic_aeq_unregister_hw_cb(&hwdev->aeqs, HINIC_MBX_FROM_FUNC);
1473 hinic_aeq_unregister_hw_cb(&hwdev->aeqs, HINIC_MBX_SEND_RSLT);
1475 hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_COMM);
1476 /* destroy workqueue before free related mbox resources in case of
1477 * illegal resource access
1479 destroy_workqueue(func_to_func->workq);
1481 free_mbox_wb_status(func_to_func);
1482 free_mbox_info(func_to_func->mbox_resp);
1483 free_mbox_info(func_to_func->mbox_send);
1485 kfree(func_to_func);
1488 int hinic_vf_mbox_random_id_init(struct hinic_hwdev *hwdev)
1494 if (HINIC_IS_VF(hwdev->hwif))
1497 vf_offset = hinic_glb_pf_vf_offset(hwdev->hwif);
1499 for (vf_in_pf = 1; vf_in_pf <= hwdev->nic_cap.max_vf; vf_in_pf++) {
1500 err = set_vf_mbox_random_id(hwdev, vf_offset + vf_in_pf);
1505 if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
1506 hwdev->func_to_func->support_vf_random = false;
1508 dev_warn(&hwdev->hwif->pdev->dev, "Mgmt is unsupported to set VF%d random id\n",
1511 hwdev->func_to_func->support_vf_random = true;