1 // SPDX-License-Identifier: GPL-2.0-only
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/pci.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
14 #include <linux/spinlock.h>
15 #include <linux/sizes.h>
16 #include <linux/atomic.h>
17 #include <linux/log2.h>
19 #include <linux/completion.h>
20 #include <linux/err.h>
21 #include <asm/byteorder.h>
22 #include <asm/barrier.h>
24 #include "hinic_common.h"
25 #include "hinic_hw_if.h"
26 #include "hinic_hw_eqs.h"
27 #include "hinic_hw_mgmt.h"
28 #include "hinic_hw_wqe.h"
29 #include "hinic_hw_wq.h"
30 #include "hinic_hw_cmdq.h"
31 #include "hinic_hw_io.h"
32 #include "hinic_hw_dev.h"
34 #define CMDQ_CEQE_TYPE_SHIFT 0
36 #define CMDQ_CEQE_TYPE_MASK 0x7
38 #define CMDQ_CEQE_GET(val, member) \
39 (((val) >> CMDQ_CEQE_##member##_SHIFT) \
40 & CMDQ_CEQE_##member##_MASK)
42 #define CMDQ_WQE_ERRCODE_VAL_SHIFT 20
44 #define CMDQ_WQE_ERRCODE_VAL_MASK 0xF
46 #define CMDQ_WQE_ERRCODE_GET(val, member) \
47 (((val) >> CMDQ_WQE_ERRCODE_##member##_SHIFT) \
48 & CMDQ_WQE_ERRCODE_##member##_MASK)
50 #define CMDQ_DB_PI_OFF(pi) (((u16)LOWER_8_BITS(pi)) << 3)
52 #define CMDQ_DB_ADDR(db_base, pi) ((db_base) + CMDQ_DB_PI_OFF(pi))
54 #define CMDQ_WQE_HEADER(wqe) ((struct hinic_cmdq_header *)(wqe))
56 #define CMDQ_WQE_COMPLETED(ctrl_info) \
57 HINIC_CMDQ_CTRL_GET(ctrl_info, HW_BUSY_BIT)
59 #define FIRST_DATA_TO_WRITE_LAST sizeof(u64)
61 #define CMDQ_DB_OFF SZ_2K
63 #define CMDQ_WQEBB_SIZE 64
64 #define CMDQ_WQE_SIZE 64
65 #define CMDQ_DEPTH SZ_4K
67 #define CMDQ_WQ_PAGE_SIZE SZ_256K
69 #define WQE_LCMD_SIZE 64
70 #define WQE_SCMD_SIZE 64
72 #define COMPLETE_LEN 3
74 #define CMDQ_TIMEOUT 1000
76 #define CMDQ_PFN(addr, page_size) ((addr) >> (ilog2(page_size)))
78 #define cmdq_to_cmdqs(cmdq) container_of((cmdq) - (cmdq)->cmdq_type, \
79 struct hinic_cmdqs, cmdq[0])
81 #define cmdqs_to_func_to_io(cmdqs) container_of(cmdqs, \
82 struct hinic_func_to_io, \
85 enum completion_format {
96 BUFDESC_LCMD_LEN = 2, /* 16 bytes - 2(8 byte unit) */
97 BUFDESC_SCMD_LEN = 3, /* 24 bytes - 3(8 byte unit) */
101 CTRL_SECT_LEN = 1, /* 4 bytes (ctrl) - 1(8 byte unit) */
102 CTRL_DIRECT_SECT_LEN = 2, /* 12 bytes (ctrl + rsvd) - 2(8 byte unit) */
105 enum cmdq_scmd_type {
106 CMDQ_SET_ARM_CMD = 2,
110 CMDQ_CMD_SYNC_DIRECT_RESP = 0,
111 CMDQ_CMD_SYNC_SGE_RESP = 1,
114 enum completion_request {
120 * hinic_alloc_cmdq_buf - alloc buffer for sending command
122 * @cmdq_buf: the buffer returned in this struct
124 * Return 0 - Success, negative - Failure
126 int hinic_alloc_cmdq_buf(struct hinic_cmdqs *cmdqs,
127 struct hinic_cmdq_buf *cmdq_buf)
129 struct hinic_hwif *hwif = cmdqs->hwif;
130 struct pci_dev *pdev = hwif->pdev;
132 cmdq_buf->buf = dma_pool_alloc(cmdqs->cmdq_buf_pool, GFP_KERNEL,
133 &cmdq_buf->dma_addr);
134 if (!cmdq_buf->buf) {
135 dev_err(&pdev->dev, "Failed to allocate cmd from the pool\n");
143 * hinic_free_cmdq_buf - free buffer
145 * @cmdq_buf: the buffer to free that is in this struct
147 void hinic_free_cmdq_buf(struct hinic_cmdqs *cmdqs,
148 struct hinic_cmdq_buf *cmdq_buf)
150 dma_pool_free(cmdqs->cmdq_buf_pool, cmdq_buf->buf, cmdq_buf->dma_addr);
153 static unsigned int cmdq_wqe_size_from_bdlen(enum bufdesc_len len)
155 unsigned int wqe_size = 0;
158 case BUFDESC_LCMD_LEN:
159 wqe_size = WQE_LCMD_SIZE;
161 case BUFDESC_SCMD_LEN:
162 wqe_size = WQE_SCMD_SIZE;
169 static void cmdq_set_sge_completion(struct hinic_cmdq_completion *completion,
170 struct hinic_cmdq_buf *buf_out)
172 struct hinic_sge_resp *sge_resp = &completion->sge_resp;
174 hinic_set_sge(&sge_resp->sge, buf_out->dma_addr, buf_out->size);
177 static void cmdq_prepare_wqe_ctrl(struct hinic_cmdq_wqe *wqe, int wrapped,
178 enum hinic_cmd_ack_type ack_type,
179 enum hinic_mod_type mod, u8 cmd, u16 prod_idx,
180 enum completion_format complete_format,
181 enum data_format data_format,
182 enum bufdesc_len buf_len)
184 struct hinic_cmdq_wqe_lcmd *wqe_lcmd;
185 struct hinic_cmdq_wqe_scmd *wqe_scmd;
186 enum ctrl_sect_len ctrl_len;
187 struct hinic_ctrl *ctrl;
190 if (data_format == DATA_SGE) {
191 wqe_lcmd = &wqe->wqe_lcmd;
193 wqe_lcmd->status.status_info = 0;
194 ctrl = &wqe_lcmd->ctrl;
195 ctrl_len = CTRL_SECT_LEN;
197 wqe_scmd = &wqe->direct_wqe.wqe_scmd;
199 wqe_scmd->status.status_info = 0;
200 ctrl = &wqe_scmd->ctrl;
201 ctrl_len = CTRL_DIRECT_SECT_LEN;
204 ctrl->ctrl_info = HINIC_CMDQ_CTRL_SET(prod_idx, PI) |
205 HINIC_CMDQ_CTRL_SET(cmd, CMD) |
206 HINIC_CMDQ_CTRL_SET(mod, MOD) |
207 HINIC_CMDQ_CTRL_SET(ack_type, ACK_TYPE);
209 CMDQ_WQE_HEADER(wqe)->header_info =
210 HINIC_CMDQ_WQE_HEADER_SET(buf_len, BUFDESC_LEN) |
211 HINIC_CMDQ_WQE_HEADER_SET(complete_format, COMPLETE_FMT) |
212 HINIC_CMDQ_WQE_HEADER_SET(data_format, DATA_FMT) |
213 HINIC_CMDQ_WQE_HEADER_SET(CEQ_SET, COMPLETE_REQ) |
214 HINIC_CMDQ_WQE_HEADER_SET(COMPLETE_LEN, COMPLETE_SECT_LEN) |
215 HINIC_CMDQ_WQE_HEADER_SET(ctrl_len, CTRL_LEN) |
216 HINIC_CMDQ_WQE_HEADER_SET(wrapped, TOGGLED_WRAPPED);
218 saved_data = CMDQ_WQE_HEADER(wqe)->saved_data;
219 saved_data = HINIC_SAVED_DATA_CLEAR(saved_data, ARM);
221 if (cmd == CMDQ_SET_ARM_CMD && mod == HINIC_MOD_COMM)
222 CMDQ_WQE_HEADER(wqe)->saved_data |=
223 HINIC_SAVED_DATA_SET(1, ARM);
225 CMDQ_WQE_HEADER(wqe)->saved_data = saved_data;
228 static void cmdq_set_lcmd_bufdesc(struct hinic_cmdq_wqe_lcmd *wqe_lcmd,
229 struct hinic_cmdq_buf *buf_in)
231 hinic_set_sge(&wqe_lcmd->buf_desc.sge, buf_in->dma_addr, buf_in->size);
234 static void cmdq_set_direct_wqe_data(struct hinic_cmdq_direct_wqe *wqe,
235 void *buf_in, u32 in_size)
237 struct hinic_cmdq_wqe_scmd *wqe_scmd = &wqe->wqe_scmd;
239 wqe_scmd->buf_desc.buf_len = in_size;
240 memcpy(wqe_scmd->buf_desc.data, buf_in, in_size);
243 static void cmdq_set_lcmd_wqe(struct hinic_cmdq_wqe *wqe,
244 enum cmdq_cmd_type cmd_type,
245 struct hinic_cmdq_buf *buf_in,
246 struct hinic_cmdq_buf *buf_out, int wrapped,
247 enum hinic_cmd_ack_type ack_type,
248 enum hinic_mod_type mod, u8 cmd, u16 prod_idx)
250 struct hinic_cmdq_wqe_lcmd *wqe_lcmd = &wqe->wqe_lcmd;
251 enum completion_format complete_format;
254 case CMDQ_CMD_SYNC_SGE_RESP:
255 complete_format = COMPLETE_SGE;
256 cmdq_set_sge_completion(&wqe_lcmd->completion, buf_out);
258 case CMDQ_CMD_SYNC_DIRECT_RESP:
259 complete_format = COMPLETE_DIRECT;
260 wqe_lcmd->completion.direct_resp = 0;
264 cmdq_prepare_wqe_ctrl(wqe, wrapped, ack_type, mod, cmd,
265 prod_idx, complete_format, DATA_SGE,
268 cmdq_set_lcmd_bufdesc(wqe_lcmd, buf_in);
271 static void cmdq_set_direct_wqe(struct hinic_cmdq_wqe *wqe,
272 enum cmdq_cmd_type cmd_type,
273 void *buf_in, u16 in_size,
274 struct hinic_cmdq_buf *buf_out, int wrapped,
275 enum hinic_cmd_ack_type ack_type,
276 enum hinic_mod_type mod, u8 cmd, u16 prod_idx)
278 struct hinic_cmdq_direct_wqe *direct_wqe = &wqe->direct_wqe;
279 enum completion_format complete_format;
280 struct hinic_cmdq_wqe_scmd *wqe_scmd;
282 wqe_scmd = &direct_wqe->wqe_scmd;
285 case CMDQ_CMD_SYNC_SGE_RESP:
286 complete_format = COMPLETE_SGE;
287 cmdq_set_sge_completion(&wqe_scmd->completion, buf_out);
289 case CMDQ_CMD_SYNC_DIRECT_RESP:
290 complete_format = COMPLETE_DIRECT;
291 wqe_scmd->completion.direct_resp = 0;
295 cmdq_prepare_wqe_ctrl(wqe, wrapped, ack_type, mod, cmd, prod_idx,
296 complete_format, DATA_DIRECT, BUFDESC_SCMD_LEN);
298 cmdq_set_direct_wqe_data(direct_wqe, buf_in, in_size);
301 static void cmdq_wqe_fill(void *dst, void *src)
303 memcpy(dst + FIRST_DATA_TO_WRITE_LAST, src + FIRST_DATA_TO_WRITE_LAST,
304 CMDQ_WQE_SIZE - FIRST_DATA_TO_WRITE_LAST);
306 wmb(); /* The first 8 bytes should be written last */
308 *(u64 *)dst = *(u64 *)src;
311 static void cmdq_fill_db(u32 *db_info,
312 enum hinic_cmdq_type cmdq_type, u16 prod_idx)
314 *db_info = HINIC_CMDQ_DB_INFO_SET(UPPER_8_BITS(prod_idx), HI_PROD_IDX) |
315 HINIC_CMDQ_DB_INFO_SET(HINIC_CTRL_PATH, PATH) |
316 HINIC_CMDQ_DB_INFO_SET(cmdq_type, CMDQ_TYPE) |
317 HINIC_CMDQ_DB_INFO_SET(HINIC_DB_CMDQ_TYPE, DB_TYPE);
320 static void cmdq_set_db(struct hinic_cmdq *cmdq,
321 enum hinic_cmdq_type cmdq_type, u16 prod_idx)
325 cmdq_fill_db(&db_info, cmdq_type, prod_idx);
327 /* The data that is written to HW should be in Big Endian Format */
328 db_info = cpu_to_be32(db_info);
330 wmb(); /* write all before the doorbell */
332 writel(db_info, CMDQ_DB_ADDR(cmdq->db_base, prod_idx));
335 static int cmdq_sync_cmd_direct_resp(struct hinic_cmdq *cmdq,
336 enum hinic_mod_type mod, u8 cmd,
337 struct hinic_cmdq_buf *buf_in,
340 struct hinic_cmdq_wqe *curr_cmdq_wqe, cmdq_wqe;
341 u16 curr_prod_idx, next_prod_idx;
342 int errcode, wrapped, num_wqebbs;
343 struct hinic_wq *wq = cmdq->wq;
344 struct hinic_hw_wqe *hw_wqe;
345 struct completion done;
347 /* Keep doorbell index correct. bh - for tasklet(ceq). */
348 spin_lock_bh(&cmdq->cmdq_lock);
350 /* WQE_SIZE = WQEBB_SIZE, we will get the wq element and not shadow*/
351 hw_wqe = hinic_get_wqe(wq, WQE_LCMD_SIZE, &curr_prod_idx);
352 if (IS_ERR(hw_wqe)) {
353 spin_unlock_bh(&cmdq->cmdq_lock);
357 curr_cmdq_wqe = &hw_wqe->cmdq_wqe;
359 wrapped = cmdq->wrapped;
361 num_wqebbs = ALIGN(WQE_LCMD_SIZE, wq->wqebb_size) / wq->wqebb_size;
362 next_prod_idx = curr_prod_idx + num_wqebbs;
363 if (next_prod_idx >= wq->q_depth) {
364 cmdq->wrapped = !cmdq->wrapped;
365 next_prod_idx -= wq->q_depth;
368 cmdq->errcode[curr_prod_idx] = &errcode;
370 init_completion(&done);
371 cmdq->done[curr_prod_idx] = &done;
373 cmdq_set_lcmd_wqe(&cmdq_wqe, CMDQ_CMD_SYNC_DIRECT_RESP, buf_in, NULL,
374 wrapped, HINIC_CMD_ACK_TYPE_CMDQ, mod, cmd,
377 /* The data that is written to HW should be in Big Endian Format */
378 hinic_cpu_to_be32(&cmdq_wqe, WQE_LCMD_SIZE);
380 /* CMDQ WQE is not shadow, therefore wqe will be written to wq */
381 cmdq_wqe_fill(curr_cmdq_wqe, &cmdq_wqe);
383 cmdq_set_db(cmdq, HINIC_CMDQ_SYNC, next_prod_idx);
385 spin_unlock_bh(&cmdq->cmdq_lock);
387 if (!wait_for_completion_timeout(&done,
388 msecs_to_jiffies(CMDQ_TIMEOUT))) {
389 spin_lock_bh(&cmdq->cmdq_lock);
391 if (cmdq->errcode[curr_prod_idx] == &errcode)
392 cmdq->errcode[curr_prod_idx] = NULL;
394 if (cmdq->done[curr_prod_idx] == &done)
395 cmdq->done[curr_prod_idx] = NULL;
397 spin_unlock_bh(&cmdq->cmdq_lock);
399 hinic_dump_ceq_info(cmdq->hwdev);
403 smp_rmb(); /* read error code after completion */
406 struct hinic_cmdq_wqe_lcmd *wqe_lcmd = &curr_cmdq_wqe->wqe_lcmd;
408 *resp = cpu_to_be64(wqe_lcmd->completion.direct_resp);
417 static int cmdq_set_arm_bit(struct hinic_cmdq *cmdq, void *buf_in,
420 struct hinic_cmdq_wqe *curr_cmdq_wqe, cmdq_wqe;
421 u16 curr_prod_idx, next_prod_idx;
422 struct hinic_wq *wq = cmdq->wq;
423 struct hinic_hw_wqe *hw_wqe;
424 int wrapped, num_wqebbs;
426 /* Keep doorbell index correct */
427 spin_lock(&cmdq->cmdq_lock);
429 /* WQE_SIZE = WQEBB_SIZE, we will get the wq element and not shadow*/
430 hw_wqe = hinic_get_wqe(wq, WQE_SCMD_SIZE, &curr_prod_idx);
431 if (IS_ERR(hw_wqe)) {
432 spin_unlock(&cmdq->cmdq_lock);
436 curr_cmdq_wqe = &hw_wqe->cmdq_wqe;
438 wrapped = cmdq->wrapped;
440 num_wqebbs = ALIGN(WQE_SCMD_SIZE, wq->wqebb_size) / wq->wqebb_size;
441 next_prod_idx = curr_prod_idx + num_wqebbs;
442 if (next_prod_idx >= wq->q_depth) {
443 cmdq->wrapped = !cmdq->wrapped;
444 next_prod_idx -= wq->q_depth;
447 cmdq_set_direct_wqe(&cmdq_wqe, CMDQ_CMD_SYNC_DIRECT_RESP, buf_in,
448 in_size, NULL, wrapped, HINIC_CMD_ACK_TYPE_CMDQ,
449 HINIC_MOD_COMM, CMDQ_SET_ARM_CMD, curr_prod_idx);
451 /* The data that is written to HW should be in Big Endian Format */
452 hinic_cpu_to_be32(&cmdq_wqe, WQE_SCMD_SIZE);
454 /* cmdq wqe is not shadow, therefore wqe will be written to wq */
455 cmdq_wqe_fill(curr_cmdq_wqe, &cmdq_wqe);
457 cmdq_set_db(cmdq, HINIC_CMDQ_SYNC, next_prod_idx);
459 spin_unlock(&cmdq->cmdq_lock);
463 static int cmdq_params_valid(struct hinic_cmdq_buf *buf_in)
465 if (buf_in->size > HINIC_CMDQ_MAX_DATA_SIZE)
472 * hinic_cmdq_direct_resp - send command with direct data as resp
474 * @mod: module on the card that will handle the command
476 * @buf_in: the buffer for the command
477 * @resp: the response to return
479 * Return 0 - Success, negative - Failure
481 int hinic_cmdq_direct_resp(struct hinic_cmdqs *cmdqs,
482 enum hinic_mod_type mod, u8 cmd,
483 struct hinic_cmdq_buf *buf_in, u64 *resp)
485 struct hinic_hwif *hwif = cmdqs->hwif;
486 struct pci_dev *pdev = hwif->pdev;
489 err = cmdq_params_valid(buf_in);
491 dev_err(&pdev->dev, "Invalid CMDQ parameters\n");
495 return cmdq_sync_cmd_direct_resp(&cmdqs->cmdq[HINIC_CMDQ_SYNC],
496 mod, cmd, buf_in, resp);
500 * hinic_set_arm_bit - set arm bit for enable interrupt again
502 * @q_type: type of queue to set the arm bit for
503 * @q_id: the queue number
505 * Return 0 - Success, negative - Failure
507 static int hinic_set_arm_bit(struct hinic_cmdqs *cmdqs,
508 enum hinic_set_arm_qtype q_type, u32 q_id)
510 struct hinic_cmdq *cmdq = &cmdqs->cmdq[HINIC_CMDQ_SYNC];
511 struct hinic_hwif *hwif = cmdqs->hwif;
512 struct pci_dev *pdev = hwif->pdev;
513 struct hinic_cmdq_arm_bit arm_bit;
516 arm_bit.q_type = q_type;
519 err = cmdq_set_arm_bit(cmdq, &arm_bit, sizeof(arm_bit));
521 dev_err(&pdev->dev, "Failed to set arm for qid %d\n", q_id);
528 static void clear_wqe_complete_bit(struct hinic_cmdq *cmdq,
529 struct hinic_cmdq_wqe *wqe)
531 u32 header_info = be32_to_cpu(CMDQ_WQE_HEADER(wqe)->header_info);
532 unsigned int bufdesc_len, wqe_size;
533 struct hinic_ctrl *ctrl;
535 bufdesc_len = HINIC_CMDQ_WQE_HEADER_GET(header_info, BUFDESC_LEN);
536 wqe_size = cmdq_wqe_size_from_bdlen(bufdesc_len);
537 if (wqe_size == WQE_LCMD_SIZE) {
538 struct hinic_cmdq_wqe_lcmd *wqe_lcmd = &wqe->wqe_lcmd;
540 ctrl = &wqe_lcmd->ctrl;
542 struct hinic_cmdq_direct_wqe *direct_wqe = &wqe->direct_wqe;
543 struct hinic_cmdq_wqe_scmd *wqe_scmd;
545 wqe_scmd = &direct_wqe->wqe_scmd;
546 ctrl = &wqe_scmd->ctrl;
549 /* clear HW busy bit */
552 wmb(); /* verify wqe is clear */
556 * cmdq_arm_ceq_handler - cmdq completion event handler for arm command
557 * @cmdq: the cmdq of the arm command
558 * @wqe: the wqe of the arm command
560 * Return 0 - Success, negative - Failure
562 static int cmdq_arm_ceq_handler(struct hinic_cmdq *cmdq,
563 struct hinic_cmdq_wqe *wqe)
565 struct hinic_cmdq_direct_wqe *direct_wqe = &wqe->direct_wqe;
566 struct hinic_cmdq_wqe_scmd *wqe_scmd;
567 struct hinic_ctrl *ctrl;
570 wqe_scmd = &direct_wqe->wqe_scmd;
571 ctrl = &wqe_scmd->ctrl;
572 ctrl_info = be32_to_cpu(ctrl->ctrl_info);
574 /* HW should toggle the HW BUSY BIT */
575 if (!CMDQ_WQE_COMPLETED(ctrl_info))
578 clear_wqe_complete_bit(cmdq, wqe);
580 hinic_put_wqe(cmdq->wq, WQE_SCMD_SIZE);
584 static void cmdq_update_errcode(struct hinic_cmdq *cmdq, u16 prod_idx,
587 if (cmdq->errcode[prod_idx])
588 *cmdq->errcode[prod_idx] = errcode;
592 * cmdq_sync_cmd_handler - cmdq completion event handler for sync command
593 * @cmdq: the cmdq of the command
594 * @cons_idx: the consumer index to update the error code for
595 * @errcode: the error code
597 static void cmdq_sync_cmd_handler(struct hinic_cmdq *cmdq, u16 cons_idx,
600 u16 prod_idx = cons_idx;
602 spin_lock(&cmdq->cmdq_lock);
603 cmdq_update_errcode(cmdq, prod_idx, errcode);
605 wmb(); /* write all before update for the command request */
607 if (cmdq->done[prod_idx])
608 complete(cmdq->done[prod_idx]);
609 spin_unlock(&cmdq->cmdq_lock);
612 static int cmdq_cmd_ceq_handler(struct hinic_cmdq *cmdq, u16 ci,
613 struct hinic_cmdq_wqe *cmdq_wqe)
615 struct hinic_cmdq_wqe_lcmd *wqe_lcmd = &cmdq_wqe->wqe_lcmd;
616 struct hinic_status *status = &wqe_lcmd->status;
617 struct hinic_ctrl *ctrl = &wqe_lcmd->ctrl;
620 if (!CMDQ_WQE_COMPLETED(be32_to_cpu(ctrl->ctrl_info)))
625 errcode = CMDQ_WQE_ERRCODE_GET(be32_to_cpu(status->status_info), VAL);
627 cmdq_sync_cmd_handler(cmdq, ci, errcode);
629 clear_wqe_complete_bit(cmdq, cmdq_wqe);
630 hinic_put_wqe(cmdq->wq, WQE_LCMD_SIZE);
635 * cmdq_ceq_handler - cmdq completion event handler
636 * @handle: private data for the handler(cmdqs)
637 * @ceqe_data: ceq element data
639 static void cmdq_ceq_handler(void *handle, u32 ceqe_data)
641 enum hinic_cmdq_type cmdq_type = CMDQ_CEQE_GET(ceqe_data, TYPE);
642 struct hinic_cmdqs *cmdqs = (struct hinic_cmdqs *)handle;
643 struct hinic_cmdq *cmdq = &cmdqs->cmdq[cmdq_type];
644 struct hinic_cmdq_header *header;
645 struct hinic_hw_wqe *hw_wqe;
646 int err, set_arm = 0;
650 /* Read the smallest wqe size for getting wqe size */
651 while ((hw_wqe = hinic_read_wqe(cmdq->wq, WQE_SCMD_SIZE, &ci))) {
655 header = CMDQ_WQE_HEADER(&hw_wqe->cmdq_wqe);
656 saved_data = be32_to_cpu(header->saved_data);
658 if (HINIC_SAVED_DATA_GET(saved_data, ARM)) {
659 /* arm_bit was set until here */
662 if (cmdq_arm_ceq_handler(cmdq, &hw_wqe->cmdq_wqe))
667 hw_wqe = hinic_read_wqe(cmdq->wq, WQE_LCMD_SIZE, &ci);
671 if (cmdq_cmd_ceq_handler(cmdq, ci, &hw_wqe->cmdq_wqe))
677 struct hinic_hwif *hwif = cmdqs->hwif;
678 struct pci_dev *pdev = hwif->pdev;
680 err = hinic_set_arm_bit(cmdqs, HINIC_SET_ARM_CMDQ, cmdq_type);
682 dev_err(&pdev->dev, "Failed to set arm for CMDQ\n");
687 * cmdq_init_queue_ctxt - init the queue ctxt of a cmdq
688 * @cmdq_ctxt: cmdq ctxt to initialize
690 * @cmdq_pages: the memory of the queue
692 static void cmdq_init_queue_ctxt(struct hinic_cmdq_ctxt *cmdq_ctxt,
693 struct hinic_cmdq *cmdq,
694 struct hinic_cmdq_pages *cmdq_pages)
696 struct hinic_cmdq_ctxt_info *ctxt_info = &cmdq_ctxt->ctxt_info;
697 u64 wq_first_page_paddr, cmdq_first_block_paddr, pfn;
698 struct hinic_cmdqs *cmdqs = cmdq_to_cmdqs(cmdq);
699 struct hinic_wq *wq = cmdq->wq;
701 /* The data in the HW is in Big Endian Format */
702 wq_first_page_paddr = be64_to_cpu(*wq->block_vaddr);
704 pfn = CMDQ_PFN(wq_first_page_paddr, SZ_4K);
706 ctxt_info->curr_wqe_page_pfn =
707 HINIC_CMDQ_CTXT_PAGE_INFO_SET(pfn, CURR_WQE_PAGE_PFN) |
708 HINIC_CMDQ_CTXT_PAGE_INFO_SET(HINIC_CEQ_ID_CMDQ, EQ_ID) |
709 HINIC_CMDQ_CTXT_PAGE_INFO_SET(1, CEQ_ARM) |
710 HINIC_CMDQ_CTXT_PAGE_INFO_SET(1, CEQ_EN) |
711 HINIC_CMDQ_CTXT_PAGE_INFO_SET(cmdq->wrapped, WRAPPED);
713 if (wq->num_q_pages != 1) {
714 /* block PFN - Read Modify Write */
715 cmdq_first_block_paddr = cmdq_pages->page_paddr;
717 pfn = CMDQ_PFN(cmdq_first_block_paddr, wq->wq_page_size);
720 ctxt_info->wq_block_pfn =
721 HINIC_CMDQ_CTXT_BLOCK_INFO_SET(pfn, WQ_BLOCK_PFN) |
722 HINIC_CMDQ_CTXT_BLOCK_INFO_SET(atomic_read(&wq->cons_idx), CI);
724 cmdq_ctxt->func_idx = HINIC_HWIF_FUNC_IDX(cmdqs->hwif);
725 cmdq_ctxt->ppf_idx = HINIC_HWIF_PPF_IDX(cmdqs->hwif);
726 cmdq_ctxt->cmdq_type = cmdq->cmdq_type;
730 * init_cmdq - initialize cmdq
732 * @wq: the wq attaced to the cmdq
733 * @q_type: the cmdq type of the cmdq
734 * @db_area: doorbell area for the cmdq
736 * Return 0 - Success, negative - Failure
738 static int init_cmdq(struct hinic_cmdq *cmdq, struct hinic_wq *wq,
739 enum hinic_cmdq_type q_type, void __iomem *db_area)
744 cmdq->cmdq_type = q_type;
747 spin_lock_init(&cmdq->cmdq_lock);
749 cmdq->done = vzalloc(array_size(sizeof(*cmdq->done), wq->q_depth));
753 cmdq->errcode = vzalloc(array_size(sizeof(*cmdq->errcode),
755 if (!cmdq->errcode) {
760 cmdq->db_base = db_area + CMDQ_DB_OFF;
769 * free_cmdq - Free cmdq
770 * @cmdq: the cmdq to free
772 static void free_cmdq(struct hinic_cmdq *cmdq)
774 vfree(cmdq->errcode);
779 * init_cmdqs_ctxt - write the cmdq ctxt to HW after init all cmdq
780 * @hwdev: the NIC HW device
781 * @cmdqs: cmdqs to write the ctxts for
782 * @db_area: db_area for all the cmdqs
784 * Return 0 - Success, negative - Failure
786 static int init_cmdqs_ctxt(struct hinic_hwdev *hwdev,
787 struct hinic_cmdqs *cmdqs, void __iomem **db_area)
789 struct hinic_hwif *hwif = hwdev->hwif;
790 enum hinic_cmdq_type type, cmdq_type;
791 struct hinic_cmdq_ctxt *cmdq_ctxts;
792 struct pci_dev *pdev = hwif->pdev;
793 struct hinic_pfhwdev *pfhwdev;
796 cmdq_ctxts = devm_kcalloc(&pdev->dev, HINIC_MAX_CMDQ_TYPES,
797 sizeof(*cmdq_ctxts), GFP_KERNEL);
801 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
803 cmdq_type = HINIC_CMDQ_SYNC;
804 for (; cmdq_type < HINIC_MAX_CMDQ_TYPES; cmdq_type++) {
805 cmdqs->cmdq[cmdq_type].hwdev = hwdev;
806 err = init_cmdq(&cmdqs->cmdq[cmdq_type],
807 &cmdqs->saved_wqs[cmdq_type], cmdq_type,
810 dev_err(&pdev->dev, "Failed to initialize cmdq\n");
814 cmdq_init_queue_ctxt(&cmdq_ctxts[cmdq_type],
815 &cmdqs->cmdq[cmdq_type],
819 /* Write the CMDQ ctxts */
820 cmdq_type = HINIC_CMDQ_SYNC;
821 for (; cmdq_type < HINIC_MAX_CMDQ_TYPES; cmdq_type++) {
822 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
823 HINIC_COMM_CMD_CMDQ_CTXT_SET,
824 &cmdq_ctxts[cmdq_type],
825 sizeof(cmdq_ctxts[cmdq_type]),
826 NULL, NULL, HINIC_MGMT_MSG_SYNC);
828 dev_err(&pdev->dev, "Failed to set CMDQ CTXT type = %d\n",
830 goto err_write_cmdq_ctxt;
834 devm_kfree(&pdev->dev, cmdq_ctxts);
838 cmdq_type = HINIC_MAX_CMDQ_TYPES;
841 for (type = HINIC_CMDQ_SYNC; type < cmdq_type; type++)
842 free_cmdq(&cmdqs->cmdq[type]);
844 devm_kfree(&pdev->dev, cmdq_ctxts);
848 static int hinic_set_cmdq_depth(struct hinic_hwdev *hwdev, u16 cmdq_depth)
850 struct hinic_cmd_hw_ioctxt hw_ioctxt = { 0 };
851 struct hinic_pfhwdev *pfhwdev;
853 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
855 hw_ioctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwdev->hwif);
856 hw_ioctxt.ppf_idx = HINIC_HWIF_PPF_IDX(hwdev->hwif);
858 hw_ioctxt.set_cmdq_depth = HW_IOCTXT_SET_CMDQ_DEPTH_ENABLE;
859 hw_ioctxt.cmdq_depth = (u8)ilog2(cmdq_depth);
861 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
862 HINIC_COMM_CMD_HWCTXT_SET,
863 &hw_ioctxt, sizeof(hw_ioctxt), NULL,
864 NULL, HINIC_MGMT_MSG_SYNC);
868 * hinic_init_cmdqs - init all cmdqs
869 * @cmdqs: cmdqs to init
870 * @hwif: HW interface for accessing cmdqs
871 * @db_area: doorbell areas for all the cmdqs
873 * Return 0 - Success, negative - Failure
875 int hinic_init_cmdqs(struct hinic_cmdqs *cmdqs, struct hinic_hwif *hwif,
876 void __iomem **db_area)
878 struct hinic_func_to_io *func_to_io = cmdqs_to_func_to_io(cmdqs);
879 struct pci_dev *pdev = hwif->pdev;
880 struct hinic_hwdev *hwdev;
885 cmdqs->cmdq_buf_pool = dma_pool_create("hinic_cmdq", &pdev->dev,
887 HINIC_CMDQ_BUF_SIZE, 0);
888 if (!cmdqs->cmdq_buf_pool)
891 cmdqs->saved_wqs = devm_kcalloc(&pdev->dev, HINIC_MAX_CMDQ_TYPES,
892 sizeof(*cmdqs->saved_wqs), GFP_KERNEL);
893 if (!cmdqs->saved_wqs) {
898 max_wqe_size = WQE_LCMD_SIZE;
899 err = hinic_wqs_cmdq_alloc(&cmdqs->cmdq_pages, cmdqs->saved_wqs, hwif,
900 HINIC_MAX_CMDQ_TYPES, CMDQ_WQEBB_SIZE,
901 CMDQ_WQ_PAGE_SIZE, CMDQ_DEPTH, max_wqe_size);
903 dev_err(&pdev->dev, "Failed to allocate CMDQ wqs\n");
907 hwdev = container_of(func_to_io, struct hinic_hwdev, func_to_io);
908 err = init_cmdqs_ctxt(hwdev, cmdqs, db_area);
910 dev_err(&pdev->dev, "Failed to write cmdq ctxt\n");
914 hinic_ceq_register_cb(&func_to_io->ceqs, HINIC_CEQ_CMDQ, cmdqs,
917 err = hinic_set_cmdq_depth(hwdev, CMDQ_DEPTH);
919 dev_err(&hwif->pdev->dev, "Failed to set cmdq depth\n");
920 goto err_set_cmdq_depth;
926 hinic_ceq_unregister_cb(&func_to_io->ceqs, HINIC_CEQ_CMDQ);
927 free_cmdq(&cmdqs->cmdq[HINIC_CMDQ_SYNC]);
929 hinic_wqs_cmdq_free(&cmdqs->cmdq_pages, cmdqs->saved_wqs,
930 HINIC_MAX_CMDQ_TYPES);
933 devm_kfree(&pdev->dev, cmdqs->saved_wqs);
936 dma_pool_destroy(cmdqs->cmdq_buf_pool);
941 * hinic_free_cmdqs - free all cmdqs
942 * @cmdqs: cmdqs to free
944 void hinic_free_cmdqs(struct hinic_cmdqs *cmdqs)
946 struct hinic_func_to_io *func_to_io = cmdqs_to_func_to_io(cmdqs);
947 struct hinic_hwif *hwif = cmdqs->hwif;
948 struct pci_dev *pdev = hwif->pdev;
949 enum hinic_cmdq_type cmdq_type;
951 hinic_ceq_unregister_cb(&func_to_io->ceqs, HINIC_CEQ_CMDQ);
953 cmdq_type = HINIC_CMDQ_SYNC;
954 for (; cmdq_type < HINIC_MAX_CMDQ_TYPES; cmdq_type++)
955 free_cmdq(&cmdqs->cmdq[cmdq_type]);
957 hinic_wqs_cmdq_free(&cmdqs->cmdq_pages, cmdqs->saved_wqs,
958 HINIC_MAX_CMDQ_TYPES);
960 devm_kfree(&pdev->dev, cmdqs->saved_wqs);
962 dma_pool_destroy(cmdqs->cmdq_buf_pool);