1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
3 
4 #ifndef _IONIC_DEV_H_
5 #define _IONIC_DEV_H_
6 
7 #include <linux/mutex.h>
8 #include <linux/workqueue.h>
9 
10 #include "ionic_if.h"
11 #include "ionic_regs.h"
12 
13 #define IONIC_MIN_MTU			ETH_MIN_MTU
14 #define IONIC_MAX_MTU			9194
15 #define IONIC_MAX_TXRX_DESC		16384
16 #define IONIC_MIN_TXRX_DESC		16
17 #define IONIC_DEF_TXRX_DESC		4096
18 #define IONIC_LIFS_MAX			1024
19 #define IONIC_ITR_COAL_USEC_DEFAULT	64
20 
21 #define IONIC_DEV_CMD_REG_VERSION	1
22 #define IONIC_DEV_INFO_REG_COUNT	32
23 #define IONIC_DEV_CMD_REG_COUNT		32
24 
25 struct ionic_dev_bar {
26 	void __iomem *vaddr;
27 	phys_addr_t bus_addr;
28 	unsigned long len;
29 	int res_index;
30 };
31 
32 /* Registers */
33 static_assert(sizeof(struct ionic_intr) == 32);
34 
35 static_assert(sizeof(struct ionic_doorbell) == 8);
36 static_assert(sizeof(struct ionic_intr_status) == 8);
37 static_assert(sizeof(union ionic_dev_regs) == 4096);
38 static_assert(sizeof(union ionic_dev_info_regs) == 2048);
39 static_assert(sizeof(union ionic_dev_cmd_regs) == 2048);
40 static_assert(sizeof(struct ionic_lif_stats) == 1024);
41 
42 static_assert(sizeof(struct ionic_admin_cmd) == 64);
43 static_assert(sizeof(struct ionic_admin_comp) == 16);
44 static_assert(sizeof(struct ionic_nop_cmd) == 64);
45 static_assert(sizeof(struct ionic_nop_comp) == 16);
46 
47 /* Device commands */
48 static_assert(sizeof(struct ionic_dev_identify_cmd) == 64);
49 static_assert(sizeof(struct ionic_dev_identify_comp) == 16);
50 static_assert(sizeof(struct ionic_dev_init_cmd) == 64);
51 static_assert(sizeof(struct ionic_dev_init_comp) == 16);
52 static_assert(sizeof(struct ionic_dev_reset_cmd) == 64);
53 static_assert(sizeof(struct ionic_dev_reset_comp) == 16);
54 static_assert(sizeof(struct ionic_dev_getattr_cmd) == 64);
55 static_assert(sizeof(struct ionic_dev_getattr_comp) == 16);
56 static_assert(sizeof(struct ionic_dev_setattr_cmd) == 64);
57 static_assert(sizeof(struct ionic_dev_setattr_comp) == 16);
58 
59 /* Port commands */
60 static_assert(sizeof(struct ionic_port_identify_cmd) == 64);
61 static_assert(sizeof(struct ionic_port_identify_comp) == 16);
62 static_assert(sizeof(struct ionic_port_init_cmd) == 64);
63 static_assert(sizeof(struct ionic_port_init_comp) == 16);
64 static_assert(sizeof(struct ionic_port_reset_cmd) == 64);
65 static_assert(sizeof(struct ionic_port_reset_comp) == 16);
66 static_assert(sizeof(struct ionic_port_getattr_cmd) == 64);
67 static_assert(sizeof(struct ionic_port_getattr_comp) == 16);
68 static_assert(sizeof(struct ionic_port_setattr_cmd) == 64);
69 static_assert(sizeof(struct ionic_port_setattr_comp) == 16);
70 
71 /* LIF commands */
72 static_assert(sizeof(struct ionic_lif_init_cmd) == 64);
73 static_assert(sizeof(struct ionic_lif_init_comp) == 16);
74 static_assert(sizeof(struct ionic_lif_reset_cmd) == 64);
75 static_assert(sizeof(ionic_lif_reset_comp) == 16);
76 static_assert(sizeof(struct ionic_lif_getattr_cmd) == 64);
77 static_assert(sizeof(struct ionic_lif_getattr_comp) == 16);
78 static_assert(sizeof(struct ionic_lif_setattr_cmd) == 64);
79 static_assert(sizeof(struct ionic_lif_setattr_comp) == 16);
80 
81 static_assert(sizeof(struct ionic_q_init_cmd) == 64);
82 static_assert(sizeof(struct ionic_q_init_comp) == 16);
83 static_assert(sizeof(struct ionic_q_control_cmd) == 64);
84 static_assert(sizeof(ionic_q_control_comp) == 16);
85 
86 static_assert(sizeof(struct ionic_rx_mode_set_cmd) == 64);
87 static_assert(sizeof(ionic_rx_mode_set_comp) == 16);
88 static_assert(sizeof(struct ionic_rx_filter_add_cmd) == 64);
89 static_assert(sizeof(struct ionic_rx_filter_add_comp) == 16);
90 static_assert(sizeof(struct ionic_rx_filter_del_cmd) == 64);
91 static_assert(sizeof(ionic_rx_filter_del_comp) == 16);
92 
93 /* RDMA commands */
94 static_assert(sizeof(struct ionic_rdma_reset_cmd) == 64);
95 static_assert(sizeof(struct ionic_rdma_queue_cmd) == 64);
96 
97 /* Events */
98 static_assert(sizeof(struct ionic_notifyq_cmd) == 4);
99 static_assert(sizeof(union ionic_notifyq_comp) == 64);
100 static_assert(sizeof(struct ionic_notifyq_event) == 64);
101 static_assert(sizeof(struct ionic_link_change_event) == 64);
102 static_assert(sizeof(struct ionic_reset_event) == 64);
103 static_assert(sizeof(struct ionic_heartbeat_event) == 64);
104 static_assert(sizeof(struct ionic_log_event) == 64);
105 
106 /* I/O */
107 static_assert(sizeof(struct ionic_txq_desc) == 16);
108 static_assert(sizeof(struct ionic_txq_sg_desc) == 128);
109 static_assert(sizeof(struct ionic_txq_comp) == 16);
110 
111 static_assert(sizeof(struct ionic_rxq_desc) == 16);
112 static_assert(sizeof(struct ionic_rxq_sg_desc) == 128);
113 static_assert(sizeof(struct ionic_rxq_comp) == 16);
114 
115 struct ionic_devinfo {
116 	u8 asic_type;
117 	u8 asic_rev;
118 	char fw_version[IONIC_DEVINFO_FWVERS_BUFLEN + 1];
119 	char serial_num[IONIC_DEVINFO_SERIAL_BUFLEN + 1];
120 };
121 
122 struct ionic_dev {
123 	union ionic_dev_info_regs __iomem *dev_info_regs;
124 	union ionic_dev_cmd_regs __iomem *dev_cmd_regs;
125 
126 	u64 __iomem *db_pages;
127 	dma_addr_t phy_db_pages;
128 
129 	struct ionic_intr __iomem *intr_ctrl;
130 	u64 __iomem *intr_status;
131 
132 	u32 port_info_sz;
133 	struct ionic_port_info *port_info;
134 	dma_addr_t port_info_pa;
135 
136 	struct ionic_devinfo dev_info;
137 };
138 
139 struct ionic_cq_info {
140 	void *cq_desc;
141 	struct ionic_cq_info *next;
142 	unsigned int index;
143 	bool last;
144 };
145 
146 struct ionic_queue;
147 struct ionic_qcq;
148 struct ionic_desc_info;
149 
150 typedef void (*ionic_desc_cb)(struct ionic_queue *q,
151 			      struct ionic_desc_info *desc_info,
152 			      struct ionic_cq_info *cq_info, void *cb_arg);
153 
154 struct ionic_desc_info {
155 	void *desc;
156 	void *sg_desc;
157 	struct ionic_desc_info *next;
158 	unsigned int index;
159 	unsigned int left;
160 	ionic_desc_cb cb;
161 	void *cb_arg;
162 };
163 
164 #define QUEUE_NAME_MAX_SZ		32
165 
166 struct ionic_queue {
167 	u64 dbell_count;
168 	u64 drop;
169 	u64 stop;
170 	u64 wake;
171 	struct ionic_lif *lif;
172 	struct ionic_desc_info *info;
173 	struct ionic_desc_info *tail;
174 	struct ionic_desc_info *head;
175 	struct ionic_dev *idev;
176 	unsigned int index;
177 	unsigned int type;
178 	unsigned int hw_index;
179 	unsigned int hw_type;
180 	u64 dbval;
181 	void *base;
182 	void *sg_base;
183 	dma_addr_t base_pa;
184 	dma_addr_t sg_base_pa;
185 	unsigned int num_descs;
186 	unsigned int desc_size;
187 	unsigned int sg_desc_size;
188 	unsigned int pid;
189 	char name[QUEUE_NAME_MAX_SZ];
190 };
191 
192 #define INTR_INDEX_NOT_ASSIGNED		-1
193 #define INTR_NAME_MAX_SZ		32
194 
195 struct ionic_intr_info {
196 	char name[INTR_NAME_MAX_SZ];
197 	unsigned int index;
198 	unsigned int vector;
199 	u64 rearm_count;
200 	unsigned int cpu;
201 	cpumask_t affinity_mask;
202 };
203 
204 struct ionic_cq {
205 	void *base;
206 	dma_addr_t base_pa;
207 	struct ionic_lif *lif;
208 	struct ionic_cq_info *info;
209 	struct ionic_cq_info *tail;
210 	struct ionic_queue *bound_q;
211 	struct ionic_intr_info *bound_intr;
212 	bool done_color;
213 	unsigned int num_descs;
214 	u64 compl_count;
215 	unsigned int desc_size;
216 };
217 
218 struct ionic;
219 
220 static inline void ionic_intr_init(struct ionic_dev *idev,
221 				   struct ionic_intr_info *intr,
222 				   unsigned long index)
223 {
224 	ionic_intr_clean(idev->intr_ctrl, index);
225 	intr->index = index;
226 }
227 
228 static inline unsigned int ionic_q_space_avail(struct ionic_queue *q)
229 {
230 	unsigned int avail = q->tail->index;
231 
232 	if (q->head->index >= avail)
233 		avail += q->head->left - 1;
234 	else
235 		avail -= q->head->index + 1;
236 
237 	return avail;
238 }
239 
240 static inline bool ionic_q_has_space(struct ionic_queue *q, unsigned int want)
241 {
242 	return ionic_q_space_avail(q) >= want;
243 }
244 
245 void ionic_init_devinfo(struct ionic *ionic);
246 int ionic_dev_setup(struct ionic *ionic);
247 void ionic_dev_teardown(struct ionic *ionic);
248 
249 void ionic_dev_cmd_go(struct ionic_dev *idev, union ionic_dev_cmd *cmd);
250 u8 ionic_dev_cmd_status(struct ionic_dev *idev);
251 bool ionic_dev_cmd_done(struct ionic_dev *idev);
252 void ionic_dev_cmd_comp(struct ionic_dev *idev, union ionic_dev_cmd_comp *comp);
253 
254 void ionic_dev_cmd_identify(struct ionic_dev *idev, u8 ver);
255 void ionic_dev_cmd_init(struct ionic_dev *idev);
256 void ionic_dev_cmd_reset(struct ionic_dev *idev);
257 
258 void ionic_dev_cmd_port_identify(struct ionic_dev *idev);
259 void ionic_dev_cmd_port_init(struct ionic_dev *idev);
260 void ionic_dev_cmd_port_reset(struct ionic_dev *idev);
261 void ionic_dev_cmd_port_state(struct ionic_dev *idev, u8 state);
262 void ionic_dev_cmd_port_speed(struct ionic_dev *idev, u32 speed);
263 void ionic_dev_cmd_port_autoneg(struct ionic_dev *idev, u8 an_enable);
264 void ionic_dev_cmd_port_fec(struct ionic_dev *idev, u8 fec_type);
265 void ionic_dev_cmd_port_pause(struct ionic_dev *idev, u8 pause_type);
266 
267 void ionic_dev_cmd_lif_identify(struct ionic_dev *idev, u8 type, u8 ver);
268 void ionic_dev_cmd_lif_init(struct ionic_dev *idev, u16 lif_index,
269 			    dma_addr_t addr);
270 void ionic_dev_cmd_lif_reset(struct ionic_dev *idev, u16 lif_index);
271 void ionic_dev_cmd_adminq_init(struct ionic_dev *idev, struct ionic_qcq *qcq,
272 			       u16 lif_index, u16 intr_index);
273 
274 int ionic_db_page_num(struct ionic_lif *lif, int pid);
275 
276 int ionic_cq_init(struct ionic_lif *lif, struct ionic_cq *cq,
277 		  struct ionic_intr_info *intr,
278 		  unsigned int num_descs, size_t desc_size);
279 void ionic_cq_map(struct ionic_cq *cq, void *base, dma_addr_t base_pa);
280 void ionic_cq_bind(struct ionic_cq *cq, struct ionic_queue *q);
281 typedef bool (*ionic_cq_cb)(struct ionic_cq *cq, struct ionic_cq_info *cq_info);
282 typedef void (*ionic_cq_done_cb)(void *done_arg);
283 unsigned int ionic_cq_service(struct ionic_cq *cq, unsigned int work_to_do,
284 			      ionic_cq_cb cb, ionic_cq_done_cb done_cb,
285 			      void *done_arg);
286 
287 int ionic_q_init(struct ionic_lif *lif, struct ionic_dev *idev,
288 		 struct ionic_queue *q, unsigned int index, const char *name,
289 		 unsigned int num_descs, size_t desc_size,
290 		 size_t sg_desc_size, unsigned int pid);
291 void ionic_q_map(struct ionic_queue *q, void *base, dma_addr_t base_pa);
292 void ionic_q_sg_map(struct ionic_queue *q, void *base, dma_addr_t base_pa);
293 void ionic_q_post(struct ionic_queue *q, bool ring_doorbell, ionic_desc_cb cb,
294 		  void *cb_arg);
295 void ionic_q_rewind(struct ionic_queue *q, struct ionic_desc_info *start);
296 void ionic_q_service(struct ionic_queue *q, struct ionic_cq_info *cq_info,
297 		     unsigned int stop_index);
298 
299 #endif /* _IONIC_DEV_H_ */
300