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