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