xref: /openbmc/linux/drivers/scsi/qedi/qedi.h (revision 22d55f02)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * QLogic iSCSI Offload Driver
4  * Copyright (c) 2016 Cavium Inc.
5  */
6 
7 #ifndef _QEDI_H_
8 #define _QEDI_H_
9 
10 #define __PREVENT_QED_HSI__
11 
12 #include <scsi/scsi_transport_iscsi.h>
13 #include <scsi/libiscsi.h>
14 #include <scsi/scsi_host.h>
15 #include <linux/uio_driver.h>
16 
17 #include "qedi_hsi.h"
18 #include <linux/qed/qed_if.h>
19 #include "qedi_dbg.h"
20 #include <linux/qed/qed_iscsi_if.h>
21 #include <linux/qed/qed_ll2_if.h>
22 #include "qedi_version.h"
23 #include "qedi_nvm_iscsi_cfg.h"
24 
25 #define QEDI_MODULE_NAME		"qedi"
26 
27 struct qedi_endpoint;
28 
29 #ifndef GET_FIELD2
30 #define GET_FIELD2(value, name) \
31 	(((value) & (name ## _MASK)) >> (name ## _OFFSET))
32 #endif
33 
34 /*
35  * PCI function probe defines
36  */
37 #define QEDI_MODE_NORMAL	0
38 #define QEDI_MODE_RECOVERY	1
39 
40 #define ISCSI_WQE_SET_PTU_INVALIDATE	1
41 #define QEDI_MAX_ISCSI_TASK		4096
42 #define QEDI_MAX_TASK_NUM		0x0FFF
43 #define QEDI_MAX_ISCSI_CONNS_PER_HBA	1024
44 #define QEDI_ISCSI_MAX_BDS_PER_CMD	255	/* Firmware max BDs is 255 */
45 #define MAX_OUTSTANDING_TASKS_PER_CON	1024
46 
47 #define QEDI_MAX_BD_LEN		0xffff
48 #define QEDI_BD_SPLIT_SZ	0x1000
49 #define QEDI_PAGE_SIZE		4096
50 #define QEDI_FAST_SGE_COUNT	4
51 /* MAX Length for cached SGL */
52 #define MAX_SGLEN_FOR_CACHESGL	((1U << 16) - 1)
53 
54 #define MIN_NUM_CPUS_MSIX(x)	min_t(u32, x->dev_info.num_cqs, \
55 					num_online_cpus())
56 
57 #define QEDI_LOCAL_PORT_MIN     60000
58 #define QEDI_LOCAL_PORT_MAX     61024
59 #define QEDI_LOCAL_PORT_RANGE   (QEDI_LOCAL_PORT_MAX - QEDI_LOCAL_PORT_MIN)
60 #define QEDI_LOCAL_PORT_INVALID	0xffff
61 #define TX_RX_RING		16
62 #define RX_RING			(TX_RX_RING - 1)
63 #define QEDI_PAGE_ALIGN(addr)	ALIGN(addr, QEDI_PAGE_SIZE)
64 #define QEDI_PAGE_MASK		(~((QEDI_PAGE_SIZE) - 1))
65 
66 #define QEDI_HW_DMA_BOUNDARY	0xfff
67 #define QEDI_PATH_HANDLE	0xFE0000000UL
68 
69 enum qedi_nvm_tgts {
70 	QEDI_NVM_TGT_PRI,
71 	QEDI_NVM_TGT_SEC,
72 };
73 
74 struct qedi_nvm_iscsi_image {
75 	struct nvm_iscsi_cfg iscsi_cfg;
76 	u32 crc;
77 };
78 
79 struct qedi_uio_ctrl {
80 	/* meta data */
81 	u32 uio_hsi_version;
82 
83 	/* user writes */
84 	u32 host_tx_prod;
85 	u32 host_rx_cons;
86 	u32 host_rx_bd_cons;
87 	u32 host_tx_pkt_len;
88 	u32 host_rx_cons_cnt;
89 
90 	/* driver writes */
91 	u32 hw_tx_cons;
92 	u32 hw_rx_prod;
93 	u32 hw_rx_bd_prod;
94 	u32 hw_rx_prod_cnt;
95 
96 	/* other */
97 	u8 mac_addr[6];
98 	u8 reserve[2];
99 };
100 
101 struct qedi_rx_bd {
102 	u32 rx_pkt_index;
103 	u32 rx_pkt_len;
104 	u16 vlan_id;
105 };
106 
107 #define QEDI_RX_DESC_CNT	(QEDI_PAGE_SIZE / sizeof(struct qedi_rx_bd))
108 #define QEDI_MAX_RX_DESC_CNT	(QEDI_RX_DESC_CNT - 1)
109 #define QEDI_NUM_RX_BD		(QEDI_RX_DESC_CNT * 1)
110 #define QEDI_MAX_RX_BD		(QEDI_NUM_RX_BD - 1)
111 
112 #define QEDI_NEXT_RX_IDX(x)	((((x) & (QEDI_MAX_RX_DESC_CNT)) ==	\
113 				  (QEDI_MAX_RX_DESC_CNT - 1)) ?		\
114 				 (x) + 2 : (x) + 1)
115 
116 struct qedi_uio_dev {
117 	struct uio_info		qedi_uinfo;
118 	u32			uio_dev;
119 	struct list_head	list;
120 
121 	u32			ll2_ring_size;
122 	void			*ll2_ring;
123 
124 	u32			ll2_buf_size;
125 	void			*ll2_buf;
126 
127 	void			*rx_pkt;
128 	void			*tx_pkt;
129 
130 	struct qedi_ctx		*qedi;
131 	struct pci_dev		*pdev;
132 	void			*uctrl;
133 };
134 
135 /* List to maintain the skb pointers */
136 struct skb_work_list {
137 	struct list_head list;
138 	struct sk_buff *skb;
139 	u16 vlan_id;
140 };
141 
142 /* Queue sizes in number of elements */
143 #define QEDI_SQ_SIZE		MAX_OUTSTANDING_TASKS_PER_CON
144 #define QEDI_CQ_SIZE		2048
145 #define QEDI_CMDQ_SIZE		QEDI_MAX_ISCSI_TASK
146 #define QEDI_PROTO_CQ_PROD_IDX	0
147 
148 struct qedi_glbl_q_params {
149 	u64 hw_p_cq;	/* Completion queue PBL */
150 	u64 hw_p_rq;	/* Request queue PBL */
151 	u64 hw_p_cmdq;	/* Command queue PBL */
152 };
153 
154 struct global_queue {
155 	union iscsi_cqe *cq;
156 	dma_addr_t cq_dma;
157 	u32 cq_mem_size;
158 	u32 cq_cons_idx; /* Completion queue consumer index */
159 
160 	void *cq_pbl;
161 	dma_addr_t cq_pbl_dma;
162 	u32 cq_pbl_size;
163 
164 };
165 
166 struct qedi_fastpath {
167 	struct qed_sb_info	*sb_info;
168 	u16			sb_id;
169 #define QEDI_NAME_SIZE		16
170 	char			name[QEDI_NAME_SIZE];
171 	struct qedi_ctx         *qedi;
172 };
173 
174 /* Used to pass fastpath information needed to process CQEs */
175 struct qedi_io_work {
176 	struct list_head list;
177 	struct iscsi_cqe_solicited cqe;
178 	u16	que_idx;
179 };
180 
181 /**
182  * struct iscsi_cid_queue - Per adapter iscsi cid queue
183  *
184  * @cid_que_base:           queue base memory
185  * @cid_que:                queue memory pointer
186  * @cid_q_prod_idx:         produce index
187  * @cid_q_cons_idx:         consumer index
188  * @cid_q_max_idx:          max index. used to detect wrap around condition
189  * @cid_free_cnt:           queue size
190  * @conn_cid_tbl:           iscsi cid to conn structure mapping table
191  *
192  * Per adapter iSCSI CID Queue
193  */
194 struct iscsi_cid_queue {
195 	void *cid_que_base;
196 	u32 *cid_que;
197 	u32 cid_q_prod_idx;
198 	u32 cid_q_cons_idx;
199 	u32 cid_q_max_idx;
200 	u32 cid_free_cnt;
201 	struct qedi_conn **conn_cid_tbl;
202 };
203 
204 struct qedi_portid_tbl {
205 	spinlock_t      lock;	/* Port id lock */
206 	u16             start;
207 	u16             max;
208 	u16             next;
209 	unsigned long   *table;
210 };
211 
212 struct qedi_itt_map {
213 	__le32	itt;
214 	struct qedi_cmd *p_cmd;
215 };
216 
217 /* I/O tracing entry */
218 #define QEDI_IO_TRACE_SIZE             2048
219 struct qedi_io_log {
220 #define QEDI_IO_TRACE_REQ              0
221 #define QEDI_IO_TRACE_RSP              1
222 	u8 direction;
223 	u16 task_id;
224 	u32 cid;
225 	u32 port_id;	/* Remote port fabric ID */
226 	int lun;
227 	u8 op;		/* SCSI CDB */
228 	u8 lba[4];
229 	unsigned int bufflen;	/* SCSI buffer length */
230 	unsigned int sg_count;	/* Number of SG elements */
231 	u8 fast_sgs;		/* number of fast sgls */
232 	u8 slow_sgs;		/* number of slow sgls */
233 	u8 cached_sgs;		/* number of cached sgls */
234 	int result;		/* Result passed back to mid-layer */
235 	unsigned long jiffies;	/* Time stamp when I/O logged */
236 	int refcount;		/* Reference count for task id */
237 	unsigned int blk_req_cpu; /* CPU that the task is queued on by
238 				   * blk layer
239 				   */
240 	unsigned int req_cpu;	/* CPU that the task is queued on */
241 	unsigned int intr_cpu;	/* Interrupt CPU that the task is received on */
242 	unsigned int blk_rsp_cpu;/* CPU that task is actually processed and
243 				  * returned to blk layer
244 				  */
245 	bool cached_sge;
246 	bool slow_sge;
247 	bool fast_sge;
248 };
249 
250 /* Number of entries in BDQ */
251 #define QEDI_BDQ_NUM		256
252 #define QEDI_BDQ_BUF_SIZE	256
253 
254 /* DMA coherent buffers for BDQ */
255 struct qedi_bdq_buf {
256 	void *buf_addr;
257 	dma_addr_t buf_dma;
258 };
259 
260 /* Main port level struct */
261 struct qedi_ctx {
262 	struct qedi_dbg_ctx dbg_ctx;
263 	struct Scsi_Host *shost;
264 	struct pci_dev *pdev;
265 	struct qed_dev *cdev;
266 	struct qed_dev_iscsi_info dev_info;
267 	struct qed_int_info int_info;
268 	struct qedi_glbl_q_params *p_cpuq;
269 	struct global_queue **global_queues;
270 	/* uio declaration */
271 	struct qedi_uio_dev *udev;
272 	struct list_head ll2_skb_list;
273 	spinlock_t ll2_lock;	/* Light L2 lock */
274 	spinlock_t hba_lock;	/* per port lock */
275 	struct task_struct *ll2_recv_thread;
276 	unsigned long flags;
277 #define UIO_DEV_OPENED		1
278 #define QEDI_IOTHREAD_WAKE	2
279 #define QEDI_IN_RECOVERY	5
280 #define QEDI_IN_OFFLINE		6
281 
282 	u8 mac[ETH_ALEN];
283 	u32 src_ip[4];
284 	u8 ip_type;
285 
286 	/* Physical address of above array */
287 	dma_addr_t hw_p_cpuq;
288 
289 	struct qedi_bdq_buf bdq[QEDI_BDQ_NUM];
290 	void *bdq_pbl;
291 	dma_addr_t bdq_pbl_dma;
292 	size_t bdq_pbl_mem_size;
293 	void *bdq_pbl_list;
294 	dma_addr_t bdq_pbl_list_dma;
295 	u8 bdq_pbl_list_num_entries;
296 	struct qedi_nvm_iscsi_image *iscsi_image;
297 	dma_addr_t nvm_buf_dma;
298 	void __iomem *bdq_primary_prod;
299 	void __iomem *bdq_secondary_prod;
300 	u16 bdq_prod_idx;
301 	u16 rq_num_entries;
302 
303 	u32 max_sqes;
304 	u8 num_queues;
305 	u32 max_active_conns;
306 
307 	struct iscsi_cid_queue cid_que;
308 	struct qedi_endpoint **ep_tbl;
309 	struct qedi_portid_tbl lcl_port_tbl;
310 
311 	/* Rx fast path intr context */
312 	struct qed_sb_info	*sb_array;
313 	struct qedi_fastpath	*fp_array;
314 	struct qed_iscsi_tid	tasks;
315 
316 #define QEDI_LINK_DOWN		0
317 #define QEDI_LINK_UP		1
318 	atomic_t link_state;
319 
320 #define QEDI_RESERVE_TASK_ID	0
321 #define MAX_ISCSI_TASK_ENTRIES	4096
322 #define QEDI_INVALID_TASK_ID	(MAX_ISCSI_TASK_ENTRIES + 1)
323 	unsigned long task_idx_map[MAX_ISCSI_TASK_ENTRIES / BITS_PER_LONG];
324 	struct qedi_itt_map *itt_map;
325 	u16 tid_reuse_count[QEDI_MAX_ISCSI_TASK];
326 	struct qed_pf_params pf_params;
327 
328 	struct workqueue_struct *tmf_thread;
329 	struct workqueue_struct *offload_thread;
330 
331 	u16 ll2_mtu;
332 
333 	struct workqueue_struct *dpc_wq;
334 
335 	spinlock_t task_idx_lock;	/* To protect gbl context */
336 	s32 last_tidx_alloc;
337 	s32 last_tidx_clear;
338 
339 	struct qedi_io_log io_trace_buf[QEDI_IO_TRACE_SIZE];
340 	spinlock_t io_trace_lock;	/* prtect trace Log buf */
341 	u16 io_trace_idx;
342 	unsigned int intr_cpu;
343 	u32 cached_sgls;
344 	bool use_cached_sge;
345 	u32 slow_sgls;
346 	bool use_slow_sge;
347 	u32 fast_sgls;
348 	bool use_fast_sge;
349 
350 	atomic_t num_offloads;
351 #define SYSFS_FLAG_FW_SEL_BOOT 2
352 #define IPV6_LEN	41
353 #define IPV4_LEN	17
354 	struct iscsi_boot_kset *boot_kset;
355 
356 	/* Used for iscsi statistics */
357 	struct mutex stats_lock;
358 };
359 
360 struct qedi_work {
361 	struct list_head list;
362 	struct qedi_ctx *qedi;
363 	union iscsi_cqe cqe;
364 	u16     que_idx;
365 	bool is_solicited;
366 };
367 
368 struct qedi_percpu_s {
369 	struct task_struct *iothread;
370 	struct list_head work_list;
371 	spinlock_t p_work_lock;		/* Per cpu worker lock */
372 };
373 
374 static inline void *qedi_get_task_mem(struct qed_iscsi_tid *info, u32 tid)
375 {
376 	return (info->blocks[tid / info->num_tids_per_block] +
377 		(tid % info->num_tids_per_block) * info->size);
378 }
379 
380 #define QEDI_U64_HI(val) ((u32)(((u64)(val)) >> 32))
381 #define QEDI_U64_LO(val) ((u32)(((u64)(val)) & 0xffffffff))
382 
383 #endif /* _QEDI_H_ */
384