1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Huawei HiNIC PCI Express Linux driver
4  * Copyright(c) 2017 Huawei Technologies Co., Ltd
5  */
6 
7 #ifndef HINIC_HW_MGMT_H
8 #define HINIC_HW_MGMT_H
9 
10 #include <linux/types.h>
11 #include <linux/semaphore.h>
12 #include <linux/completion.h>
13 #include <linux/bitops.h>
14 
15 #include "hinic_hw_if.h"
16 #include "hinic_hw_api_cmd.h"
17 
18 #define HINIC_MSG_HEADER_MSG_LEN_SHIFT                          0
19 #define HINIC_MSG_HEADER_MODULE_SHIFT                           11
20 #define HINIC_MSG_HEADER_SEG_LEN_SHIFT                          16
21 #define HINIC_MSG_HEADER_NO_ACK_SHIFT                           22
22 #define HINIC_MSG_HEADER_ASYNC_MGMT_TO_PF_SHIFT                 23
23 #define HINIC_MSG_HEADER_SEQID_SHIFT                            24
24 #define HINIC_MSG_HEADER_LAST_SHIFT                             30
25 #define HINIC_MSG_HEADER_DIRECTION_SHIFT                        31
26 #define HINIC_MSG_HEADER_CMD_SHIFT                              32
27 #define HINIC_MSG_HEADER_ZEROS_SHIFT                            40
28 #define HINIC_MSG_HEADER_PCI_INTF_SHIFT                         48
29 #define HINIC_MSG_HEADER_PF_IDX_SHIFT                           50
30 #define HINIC_MSG_HEADER_MSG_ID_SHIFT                           54
31 
32 #define HINIC_MSG_HEADER_MSG_LEN_MASK                           0x7FF
33 #define HINIC_MSG_HEADER_MODULE_MASK                            0x1F
34 #define HINIC_MSG_HEADER_SEG_LEN_MASK                           0x3F
35 #define HINIC_MSG_HEADER_NO_ACK_MASK                            0x1
36 #define HINIC_MSG_HEADER_ASYNC_MGMT_TO_PF_MASK                  0x1
37 #define HINIC_MSG_HEADER_SEQID_MASK                             0x3F
38 #define HINIC_MSG_HEADER_LAST_MASK                              0x1
39 #define HINIC_MSG_HEADER_DIRECTION_MASK                         0x1
40 #define HINIC_MSG_HEADER_CMD_MASK                               0xFF
41 #define HINIC_MSG_HEADER_ZEROS_MASK                             0xFF
42 #define HINIC_MSG_HEADER_PCI_INTF_MASK                          0x3
43 #define HINIC_MSG_HEADER_PF_IDX_MASK                            0xF
44 #define HINIC_MSG_HEADER_MSG_ID_MASK                            0x3FF
45 
46 #define HINIC_MSG_HEADER_SET(val, member)                       \
47 		((u64)((val) & HINIC_MSG_HEADER_##member##_MASK) << \
48 		 HINIC_MSG_HEADER_##member##_SHIFT)
49 
50 #define HINIC_MSG_HEADER_GET(val, member)                       \
51 		(((val) >> HINIC_MSG_HEADER_##member##_SHIFT) & \
52 		 HINIC_MSG_HEADER_##member##_MASK)
53 
54 enum hinic_mgmt_msg_type {
55 	HINIC_MGMT_MSG_SYNC = 1,
56 };
57 
58 enum hinic_cfg_cmd {
59 	HINIC_CFG_NIC_CAP = 0,
60 };
61 
62 enum hinic_comm_cmd {
63 	HINIC_COMM_CMD_START_FLR          = 0x1,
64 	HINIC_COMM_CMD_IO_STATUS_GET    = 0x3,
65 	HINIC_COMM_CMD_DMA_ATTR_SET	    = 0x4,
66 
67 	HINIC_COMM_CMD_CMDQ_CTXT_SET    = 0x10,
68 	HINIC_COMM_CMD_CMDQ_CTXT_GET    = 0x11,
69 
70 	HINIC_COMM_CMD_HWCTXT_SET       = 0x12,
71 	HINIC_COMM_CMD_HWCTXT_GET       = 0x13,
72 
73 	HINIC_COMM_CMD_SQ_HI_CI_SET     = 0x14,
74 
75 	HINIC_COMM_CMD_RES_STATE_SET    = 0x24,
76 
77 	HINIC_COMM_CMD_IO_RES_CLEAR     = 0x29,
78 
79 	HINIC_COMM_CMD_CEQ_CTRL_REG_WR_BY_UP = 0x33,
80 
81 	HINIC_COMM_CMD_L2NIC_RESET		= 0x4b,
82 
83 	HINIC_COMM_CMD_PAGESIZE_SET	= 0x50,
84 
85 	HINIC_COMM_CMD_MAX              = 0x51,
86 };
87 
88 enum hinic_mgmt_cb_state {
89 	HINIC_MGMT_CB_ENABLED = BIT(0),
90 	HINIC_MGMT_CB_RUNNING = BIT(1),
91 };
92 
93 struct hinic_recv_msg {
94 	u8                      *msg;
95 	u8                      *buf_out;
96 
97 	struct completion       recv_done;
98 
99 	u16                     cmd;
100 	enum hinic_mod_type     mod;
101 	int                     async_mgmt_to_pf;
102 
103 	u16                     msg_len;
104 	u16                     msg_id;
105 };
106 
107 struct hinic_mgmt_cb {
108 	void    (*cb)(void *handle, u8 cmd,
109 		      void *buf_in, u16 in_size,
110 		      void *buf_out, u16 *out_size);
111 
112 	void            *handle;
113 	unsigned long   state;
114 };
115 
116 struct hinic_pf_to_mgmt {
117 	struct hinic_hwif               *hwif;
118 	struct hinic_hwdev		*hwdev;
119 	struct semaphore                sync_msg_lock;
120 	u16                             sync_msg_id;
121 	u8                              *sync_msg_buf;
122 
123 	struct hinic_recv_msg           recv_resp_msg_from_mgmt;
124 	struct hinic_recv_msg           recv_msg_from_mgmt;
125 
126 	struct hinic_api_cmd_chain      *cmd_chain[HINIC_API_CMD_MAX];
127 
128 	struct hinic_mgmt_cb            mgmt_cb[HINIC_MOD_MAX];
129 };
130 
131 void hinic_register_mgmt_msg_cb(struct hinic_pf_to_mgmt *pf_to_mgmt,
132 				enum hinic_mod_type mod,
133 				void *handle,
134 				void (*callback)(void *handle,
135 						 u8 cmd, void *buf_in,
136 						 u16 in_size, void *buf_out,
137 						 u16 *out_size));
138 
139 void hinic_unregister_mgmt_msg_cb(struct hinic_pf_to_mgmt *pf_to_mgmt,
140 				  enum hinic_mod_type mod);
141 
142 int hinic_msg_to_mgmt(struct hinic_pf_to_mgmt *pf_to_mgmt,
143 		      enum hinic_mod_type mod, u8 cmd,
144 		      void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
145 		      enum hinic_mgmt_msg_type sync);
146 
147 int hinic_pf_to_mgmt_init(struct hinic_pf_to_mgmt *pf_to_mgmt,
148 			  struct hinic_hwif *hwif);
149 
150 void hinic_pf_to_mgmt_free(struct hinic_pf_to_mgmt *pf_to_mgmt);
151 
152 #endif
153