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/atomic.h>
8 #include <linux/mutex.h>
9 #include <linux/workqueue.h>
10 
11 #include "ionic_if.h"
12 #include "ionic_regs.h"
13 
14 #define IONIC_MAX_TX_DESC		8192
15 #define IONIC_MAX_RX_DESC		16384
16 #define IONIC_MIN_TXRX_DESC		64
17 #define IONIC_DEF_TXRX_DESC		4096
18 #define IONIC_RX_FILL_THRESHOLD		16
19 #define IONIC_RX_FILL_DIV		8
20 #define IONIC_LIFS_MAX			1024
21 #define IONIC_WATCHDOG_SECS		5
22 #define IONIC_ITR_COAL_USEC_DEFAULT	64
23 
24 #define IONIC_DEV_CMD_REG_VERSION	1
25 #define IONIC_DEV_INFO_REG_COUNT	32
26 #define IONIC_DEV_CMD_REG_COUNT		32
27 
28 struct ionic_dev_bar {
29 	void __iomem *vaddr;
30 	phys_addr_t bus_addr;
31 	unsigned long len;
32 	int res_index;
33 };
34 
35 #ifndef __CHECKER__
36 /* Registers */
37 static_assert(sizeof(struct ionic_intr) == 32);
38 
39 static_assert(sizeof(struct ionic_doorbell) == 8);
40 static_assert(sizeof(struct ionic_intr_status) == 8);
41 static_assert(sizeof(union ionic_dev_regs) == 4096);
42 static_assert(sizeof(union ionic_dev_info_regs) == 2048);
43 static_assert(sizeof(union ionic_dev_cmd_regs) == 2048);
44 static_assert(sizeof(struct ionic_lif_stats) == 1024);
45 
46 static_assert(sizeof(struct ionic_admin_cmd) == 64);
47 static_assert(sizeof(struct ionic_admin_comp) == 16);
48 static_assert(sizeof(struct ionic_nop_cmd) == 64);
49 static_assert(sizeof(struct ionic_nop_comp) == 16);
50 
51 /* Device commands */
52 static_assert(sizeof(struct ionic_dev_identify_cmd) == 64);
53 static_assert(sizeof(struct ionic_dev_identify_comp) == 16);
54 static_assert(sizeof(struct ionic_dev_init_cmd) == 64);
55 static_assert(sizeof(struct ionic_dev_init_comp) == 16);
56 static_assert(sizeof(struct ionic_dev_reset_cmd) == 64);
57 static_assert(sizeof(struct ionic_dev_reset_comp) == 16);
58 static_assert(sizeof(struct ionic_dev_getattr_cmd) == 64);
59 static_assert(sizeof(struct ionic_dev_getattr_comp) == 16);
60 static_assert(sizeof(struct ionic_dev_setattr_cmd) == 64);
61 static_assert(sizeof(struct ionic_dev_setattr_comp) == 16);
62 static_assert(sizeof(struct ionic_lif_setphc_cmd) == 64);
63 
64 /* Port commands */
65 static_assert(sizeof(struct ionic_port_identify_cmd) == 64);
66 static_assert(sizeof(struct ionic_port_identify_comp) == 16);
67 static_assert(sizeof(struct ionic_port_init_cmd) == 64);
68 static_assert(sizeof(struct ionic_port_init_comp) == 16);
69 static_assert(sizeof(struct ionic_port_reset_cmd) == 64);
70 static_assert(sizeof(struct ionic_port_reset_comp) == 16);
71 static_assert(sizeof(struct ionic_port_getattr_cmd) == 64);
72 static_assert(sizeof(struct ionic_port_getattr_comp) == 16);
73 static_assert(sizeof(struct ionic_port_setattr_cmd) == 64);
74 static_assert(sizeof(struct ionic_port_setattr_comp) == 16);
75 
76 /* LIF commands */
77 static_assert(sizeof(struct ionic_lif_init_cmd) == 64);
78 static_assert(sizeof(struct ionic_lif_init_comp) == 16);
79 static_assert(sizeof(struct ionic_lif_reset_cmd) == 64);
80 static_assert(sizeof(ionic_lif_reset_comp) == 16);
81 static_assert(sizeof(struct ionic_lif_getattr_cmd) == 64);
82 static_assert(sizeof(struct ionic_lif_getattr_comp) == 16);
83 static_assert(sizeof(struct ionic_lif_setattr_cmd) == 64);
84 static_assert(sizeof(struct ionic_lif_setattr_comp) == 16);
85 
86 static_assert(sizeof(struct ionic_q_init_cmd) == 64);
87 static_assert(sizeof(struct ionic_q_init_comp) == 16);
88 static_assert(sizeof(struct ionic_q_control_cmd) == 64);
89 static_assert(sizeof(ionic_q_control_comp) == 16);
90 static_assert(sizeof(struct ionic_q_identify_cmd) == 64);
91 static_assert(sizeof(struct ionic_q_identify_comp) == 16);
92 
93 static_assert(sizeof(struct ionic_rx_mode_set_cmd) == 64);
94 static_assert(sizeof(ionic_rx_mode_set_comp) == 16);
95 static_assert(sizeof(struct ionic_rx_filter_add_cmd) == 64);
96 static_assert(sizeof(struct ionic_rx_filter_add_comp) == 16);
97 static_assert(sizeof(struct ionic_rx_filter_del_cmd) == 64);
98 static_assert(sizeof(ionic_rx_filter_del_comp) == 16);
99 
100 /* RDMA commands */
101 static_assert(sizeof(struct ionic_rdma_reset_cmd) == 64);
102 static_assert(sizeof(struct ionic_rdma_queue_cmd) == 64);
103 
104 /* Events */
105 static_assert(sizeof(struct ionic_notifyq_cmd) == 4);
106 static_assert(sizeof(union ionic_notifyq_comp) == 64);
107 static_assert(sizeof(struct ionic_notifyq_event) == 64);
108 static_assert(sizeof(struct ionic_link_change_event) == 64);
109 static_assert(sizeof(struct ionic_reset_event) == 64);
110 static_assert(sizeof(struct ionic_heartbeat_event) == 64);
111 static_assert(sizeof(struct ionic_log_event) == 64);
112 
113 /* I/O */
114 static_assert(sizeof(struct ionic_txq_desc) == 16);
115 static_assert(sizeof(struct ionic_txq_sg_desc) == 128);
116 static_assert(sizeof(struct ionic_txq_comp) == 16);
117 
118 static_assert(sizeof(struct ionic_rxq_desc) == 16);
119 static_assert(sizeof(struct ionic_rxq_sg_desc) == 128);
120 static_assert(sizeof(struct ionic_rxq_comp) == 16);
121 
122 /* SR/IOV */
123 static_assert(sizeof(struct ionic_vf_setattr_cmd) == 64);
124 static_assert(sizeof(struct ionic_vf_setattr_comp) == 16);
125 static_assert(sizeof(struct ionic_vf_getattr_cmd) == 64);
126 static_assert(sizeof(struct ionic_vf_getattr_comp) == 16);
127 #endif /* __CHECKER__ */
128 
129 struct ionic_devinfo {
130 	u8 asic_type;
131 	u8 asic_rev;
132 	char fw_version[IONIC_DEVINFO_FWVERS_BUFLEN + 1];
133 	char serial_num[IONIC_DEVINFO_SERIAL_BUFLEN + 1];
134 };
135 
136 struct ionic_dev {
137 	union ionic_dev_info_regs __iomem *dev_info_regs;
138 	union ionic_dev_cmd_regs __iomem *dev_cmd_regs;
139 	struct ionic_hwstamp_regs __iomem *hwstamp_regs;
140 
141 	atomic_long_t last_check_time;
142 	unsigned long last_hb_time;
143 	u32 last_fw_hb;
144 	bool fw_hb_ready;
145 	bool fw_status_ready;
146 	u8 fw_generation;
147 
148 	u64 __iomem *db_pages;
149 	dma_addr_t phy_db_pages;
150 
151 	struct ionic_intr __iomem *intr_ctrl;
152 	u64 __iomem *intr_status;
153 
154 	u32 port_info_sz;
155 	struct ionic_port_info *port_info;
156 	dma_addr_t port_info_pa;
157 
158 	struct ionic_devinfo dev_info;
159 };
160 
161 struct ionic_cq_info {
162 	union {
163 		void *cq_desc;
164 		struct ionic_admin_comp *admincq;
165 		struct ionic_notifyq_event *notifyq;
166 	};
167 };
168 
169 struct ionic_queue;
170 struct ionic_qcq;
171 struct ionic_desc_info;
172 
173 typedef void (*ionic_desc_cb)(struct ionic_queue *q,
174 			      struct ionic_desc_info *desc_info,
175 			      struct ionic_cq_info *cq_info, void *cb_arg);
176 
177 #define IONIC_PAGE_SIZE				PAGE_SIZE
178 #define IONIC_PAGE_SPLIT_SZ			(PAGE_SIZE / 2)
179 #define IONIC_PAGE_GFP_MASK			(GFP_ATOMIC | __GFP_NOWARN |\
180 						 __GFP_COMP | __GFP_MEMALLOC)
181 
182 struct ionic_buf_info {
183 	struct page *page;
184 	dma_addr_t dma_addr;
185 	u32 page_offset;
186 	u32 len;
187 };
188 
189 #define IONIC_MAX_FRAGS			(1 + IONIC_TX_MAX_SG_ELEMS_V1)
190 
191 struct ionic_desc_info {
192 	union {
193 		void *desc;
194 		struct ionic_txq_desc *txq_desc;
195 		struct ionic_rxq_desc *rxq_desc;
196 		struct ionic_admin_cmd *adminq_desc;
197 	};
198 	union {
199 		void *sg_desc;
200 		struct ionic_txq_sg_desc *txq_sg_desc;
201 		struct ionic_rxq_sg_desc *rxq_sgl_desc;
202 	};
203 	unsigned int bytes;
204 	unsigned int nbufs;
205 	struct ionic_buf_info bufs[IONIC_MAX_FRAGS];
206 	ionic_desc_cb cb;
207 	void *cb_arg;
208 };
209 
210 #define IONIC_QUEUE_NAME_MAX_SZ		32
211 
212 struct ionic_queue {
213 	struct device *dev;
214 	struct ionic_lif *lif;
215 	struct ionic_desc_info *info;
216 	u64 dbval;
217 	u16 head_idx;
218 	u16 tail_idx;
219 	unsigned int index;
220 	unsigned int num_descs;
221 	unsigned int max_sg_elems;
222 	u64 features;
223 	u64 dbell_count;
224 	u64 stop;
225 	u64 wake;
226 	u64 drop;
227 	struct ionic_dev *idev;
228 	unsigned int type;
229 	unsigned int hw_index;
230 	unsigned int hw_type;
231 	union {
232 		void *base;
233 		struct ionic_txq_desc *txq;
234 		struct ionic_rxq_desc *rxq;
235 		struct ionic_admin_cmd *adminq;
236 	};
237 	union {
238 		void *sg_base;
239 		struct ionic_txq_sg_desc *txq_sgl;
240 		struct ionic_rxq_sg_desc *rxq_sgl;
241 	};
242 	dma_addr_t base_pa;
243 	dma_addr_t sg_base_pa;
244 	unsigned int desc_size;
245 	unsigned int sg_desc_size;
246 	unsigned int pid;
247 	char name[IONIC_QUEUE_NAME_MAX_SZ];
248 } ____cacheline_aligned_in_smp;
249 
250 #define IONIC_INTR_INDEX_NOT_ASSIGNED	-1
251 #define IONIC_INTR_NAME_MAX_SZ		32
252 
253 struct ionic_intr_info {
254 	char name[IONIC_INTR_NAME_MAX_SZ];
255 	unsigned int index;
256 	unsigned int vector;
257 	u64 rearm_count;
258 	unsigned int cpu;
259 	cpumask_t affinity_mask;
260 	u32 dim_coal_hw;
261 };
262 
263 struct ionic_cq {
264 	struct ionic_lif *lif;
265 	struct ionic_cq_info *info;
266 	struct ionic_queue *bound_q;
267 	struct ionic_intr_info *bound_intr;
268 	u16 tail_idx;
269 	bool done_color;
270 	unsigned int num_descs;
271 	unsigned int desc_size;
272 	u64 compl_count;
273 	void *base;
274 	dma_addr_t base_pa;
275 } ____cacheline_aligned_in_smp;
276 
277 struct ionic;
278 
279 static inline void ionic_intr_init(struct ionic_dev *idev,
280 				   struct ionic_intr_info *intr,
281 				   unsigned long index)
282 {
283 	ionic_intr_clean(idev->intr_ctrl, index);
284 	intr->index = index;
285 }
286 
287 static inline unsigned int ionic_q_space_avail(struct ionic_queue *q)
288 {
289 	unsigned int avail = q->tail_idx;
290 
291 	if (q->head_idx >= avail)
292 		avail += q->num_descs - q->head_idx - 1;
293 	else
294 		avail -= q->head_idx + 1;
295 
296 	return avail;
297 }
298 
299 static inline bool ionic_q_has_space(struct ionic_queue *q, unsigned int want)
300 {
301 	return ionic_q_space_avail(q) >= want;
302 }
303 
304 void ionic_init_devinfo(struct ionic *ionic);
305 int ionic_dev_setup(struct ionic *ionic);
306 
307 void ionic_dev_cmd_go(struct ionic_dev *idev, union ionic_dev_cmd *cmd);
308 u8 ionic_dev_cmd_status(struct ionic_dev *idev);
309 bool ionic_dev_cmd_done(struct ionic_dev *idev);
310 void ionic_dev_cmd_comp(struct ionic_dev *idev, union ionic_dev_cmd_comp *comp);
311 
312 void ionic_dev_cmd_identify(struct ionic_dev *idev, u8 ver);
313 void ionic_dev_cmd_init(struct ionic_dev *idev);
314 void ionic_dev_cmd_reset(struct ionic_dev *idev);
315 
316 void ionic_dev_cmd_port_identify(struct ionic_dev *idev);
317 void ionic_dev_cmd_port_init(struct ionic_dev *idev);
318 void ionic_dev_cmd_port_reset(struct ionic_dev *idev);
319 void ionic_dev_cmd_port_state(struct ionic_dev *idev, u8 state);
320 void ionic_dev_cmd_port_speed(struct ionic_dev *idev, u32 speed);
321 void ionic_dev_cmd_port_autoneg(struct ionic_dev *idev, u8 an_enable);
322 void ionic_dev_cmd_port_fec(struct ionic_dev *idev, u8 fec_type);
323 void ionic_dev_cmd_port_pause(struct ionic_dev *idev, u8 pause_type);
324 
325 int ionic_set_vf_config(struct ionic *ionic, int vf, u8 attr, u8 *data);
326 void ionic_dev_cmd_queue_identify(struct ionic_dev *idev,
327 				  u16 lif_type, u8 qtype, u8 qver);
328 void ionic_dev_cmd_lif_identify(struct ionic_dev *idev, u8 type, u8 ver);
329 void ionic_dev_cmd_lif_init(struct ionic_dev *idev, u16 lif_index,
330 			    dma_addr_t addr);
331 void ionic_dev_cmd_lif_reset(struct ionic_dev *idev, u16 lif_index);
332 void ionic_dev_cmd_adminq_init(struct ionic_dev *idev, struct ionic_qcq *qcq,
333 			       u16 lif_index, u16 intr_index);
334 
335 int ionic_db_page_num(struct ionic_lif *lif, int pid);
336 
337 int ionic_cq_init(struct ionic_lif *lif, struct ionic_cq *cq,
338 		  struct ionic_intr_info *intr,
339 		  unsigned int num_descs, size_t desc_size);
340 void ionic_cq_map(struct ionic_cq *cq, void *base, dma_addr_t base_pa);
341 void ionic_cq_bind(struct ionic_cq *cq, struct ionic_queue *q);
342 typedef bool (*ionic_cq_cb)(struct ionic_cq *cq, struct ionic_cq_info *cq_info);
343 typedef void (*ionic_cq_done_cb)(void *done_arg);
344 unsigned int ionic_cq_service(struct ionic_cq *cq, unsigned int work_to_do,
345 			      ionic_cq_cb cb, ionic_cq_done_cb done_cb,
346 			      void *done_arg);
347 
348 int ionic_q_init(struct ionic_lif *lif, struct ionic_dev *idev,
349 		 struct ionic_queue *q, unsigned int index, const char *name,
350 		 unsigned int num_descs, size_t desc_size,
351 		 size_t sg_desc_size, unsigned int pid);
352 void ionic_q_map(struct ionic_queue *q, void *base, dma_addr_t base_pa);
353 void ionic_q_sg_map(struct ionic_queue *q, void *base, dma_addr_t base_pa);
354 void ionic_q_post(struct ionic_queue *q, bool ring_doorbell, ionic_desc_cb cb,
355 		  void *cb_arg);
356 void ionic_q_rewind(struct ionic_queue *q, struct ionic_desc_info *start);
357 void ionic_q_service(struct ionic_queue *q, struct ionic_cq_info *cq_info,
358 		     unsigned int stop_index);
359 int ionic_heartbeat_check(struct ionic *ionic);
360 
361 #endif /* _IONIC_DEV_H_ */
362