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_IO_STATUS_GET    = 0x3,
64 
65 	HINIC_COMM_CMD_CMDQ_CTXT_SET    = 0x10,
66 	HINIC_COMM_CMD_CMDQ_CTXT_GET    = 0x11,
67 
68 	HINIC_COMM_CMD_HWCTXT_SET       = 0x12,
69 	HINIC_COMM_CMD_HWCTXT_GET       = 0x13,
70 
71 	HINIC_COMM_CMD_SQ_HI_CI_SET     = 0x14,
72 
73 	HINIC_COMM_CMD_RES_STATE_SET    = 0x24,
74 
75 	HINIC_COMM_CMD_IO_RES_CLEAR     = 0x29,
76 
77 	HINIC_COMM_CMD_MAX              = 0x32,
78 };
79 
80 enum hinic_mgmt_cb_state {
81 	HINIC_MGMT_CB_ENABLED = BIT(0),
82 	HINIC_MGMT_CB_RUNNING = BIT(1),
83 };
84 
85 struct hinic_recv_msg {
86 	u8                      *msg;
87 	u8                      *buf_out;
88 
89 	struct completion       recv_done;
90 
91 	u16                     cmd;
92 	enum hinic_mod_type     mod;
93 	int                     async_mgmt_to_pf;
94 
95 	u16                     msg_len;
96 	u16                     msg_id;
97 };
98 
99 struct hinic_mgmt_cb {
100 	void    (*cb)(void *handle, u8 cmd,
101 		      void *buf_in, u16 in_size,
102 		      void *buf_out, u16 *out_size);
103 
104 	void            *handle;
105 	unsigned long   state;
106 };
107 
108 struct hinic_pf_to_mgmt {
109 	struct hinic_hwif               *hwif;
110 
111 	struct semaphore                sync_msg_lock;
112 	u16                             sync_msg_id;
113 	u8                              *sync_msg_buf;
114 
115 	struct hinic_recv_msg           recv_resp_msg_from_mgmt;
116 	struct hinic_recv_msg           recv_msg_from_mgmt;
117 
118 	struct hinic_api_cmd_chain      *cmd_chain[HINIC_API_CMD_MAX];
119 
120 	struct hinic_mgmt_cb            mgmt_cb[HINIC_MOD_MAX];
121 };
122 
123 void hinic_register_mgmt_msg_cb(struct hinic_pf_to_mgmt *pf_to_mgmt,
124 				enum hinic_mod_type mod,
125 				void *handle,
126 				void (*callback)(void *handle,
127 						 u8 cmd, void *buf_in,
128 						 u16 in_size, void *buf_out,
129 						 u16 *out_size));
130 
131 void hinic_unregister_mgmt_msg_cb(struct hinic_pf_to_mgmt *pf_to_mgmt,
132 				  enum hinic_mod_type mod);
133 
134 int hinic_msg_to_mgmt(struct hinic_pf_to_mgmt *pf_to_mgmt,
135 		      enum hinic_mod_type mod, u8 cmd,
136 		      void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
137 		      enum hinic_mgmt_msg_type sync);
138 
139 int hinic_pf_to_mgmt_init(struct hinic_pf_to_mgmt *pf_to_mgmt,
140 			  struct hinic_hwif *hwif);
141 
142 void hinic_pf_to_mgmt_free(struct hinic_pf_to_mgmt *pf_to_mgmt);
143 
144 #endif
145