1 /* QLogic qed NIC Driver
2  *
3  * Copyright (c) 2015 QLogic Corporation
4  *
5  * This software is available under the terms of the GNU General Public License
6  * (GPL) Version 2, available from the file COPYING in the main directory of
7  * this source tree.
8  */
9 
10 #ifndef _QED_LL2_H
11 #define _QED_LL2_H
12 
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/qed/qed_chain.h>
20 #include <linux/qed/qed_ll2_if.h>
21 #include "qed.h"
22 #include "qed_hsi.h"
23 #include "qed_sp.h"
24 
25 #define QED_MAX_NUM_OF_LL2_CONNECTIONS                    (4)
26 
27 enum qed_ll2_roce_flavor_type {
28 	QED_LL2_ROCE,
29 	QED_LL2_RROCE,
30 	MAX_QED_LL2_ROCE_FLAVOR_TYPE
31 };
32 
33 enum qed_ll2_conn_type {
34 	QED_LL2_TYPE_RESERVED,
35 	QED_LL2_TYPE_ISCSI,
36 	QED_LL2_TYPE_TEST,
37 	QED_LL2_TYPE_ISCSI_OOO,
38 	QED_LL2_TYPE_RESERVED2,
39 	QED_LL2_TYPE_ROCE,
40 	QED_LL2_TYPE_RESERVED3,
41 	MAX_QED_LL2_RX_CONN_TYPE
42 };
43 
44 enum qed_ll2_tx_dest {
45 	QED_LL2_TX_DEST_NW, /* Light L2 TX Destination to the Network */
46 	QED_LL2_TX_DEST_LB, /* Light L2 TX Destination to the Loopback */
47 	QED_LL2_TX_DEST_MAX
48 };
49 
50 struct qed_ll2_rx_packet {
51 	struct list_head list_entry;
52 	struct core_rx_bd_with_buff_len *rxq_bd;
53 	dma_addr_t rx_buf_addr;
54 	u16 buf_length;
55 	void *cookie;
56 	u8 placement_offset;
57 	u16 parse_flags;
58 	u16 packet_length;
59 	u16 vlan;
60 	u32 opaque_data[2];
61 };
62 
63 struct qed_ll2_tx_packet {
64 	struct list_head list_entry;
65 	u16 bd_used;
66 	u16 vlan;
67 	u16 l4_hdr_offset_w;
68 	u8 bd_flags;
69 	bool notify_fw;
70 	void *cookie;
71 
72 	struct {
73 		struct core_tx_bd *txq_bd;
74 		dma_addr_t tx_frag;
75 		u16 frag_len;
76 	} bds_set[ETH_TX_MAX_BDS_PER_NON_LSO_PACKET];
77 };
78 
79 struct qed_ll2_rx_queue {
80 	/* Lock protecting the Rx queue manipulation */
81 	spinlock_t lock;
82 	struct qed_chain rxq_chain;
83 	struct qed_chain rcq_chain;
84 	u8 rx_sb_index;
85 	bool b_cb_registred;
86 	__le16 *p_fw_cons;
87 	struct list_head active_descq;
88 	struct list_head free_descq;
89 	struct list_head posting_descq;
90 	struct qed_ll2_rx_packet *descq_array;
91 	void __iomem *set_prod_addr;
92 };
93 
94 struct qed_ll2_tx_queue {
95 	/* Lock protecting the Tx queue manipulation */
96 	spinlock_t lock;
97 	struct qed_chain txq_chain;
98 	u8 tx_sb_index;
99 	bool b_cb_registred;
100 	__le16 *p_fw_cons;
101 	struct list_head active_descq;
102 	struct list_head free_descq;
103 	struct list_head sending_descq;
104 	struct qed_ll2_tx_packet *descq_array;
105 	struct qed_ll2_tx_packet *cur_send_packet;
106 	struct qed_ll2_tx_packet cur_completing_packet;
107 	u16 cur_completing_bd_idx;
108 	void __iomem *doorbell_addr;
109 	u16 bds_idx;
110 	u16 cur_send_frag_num;
111 	u16 cur_completing_frag_num;
112 	bool b_completing_packet;
113 };
114 
115 struct qed_ll2_info {
116 	/* Lock protecting the state of LL2 */
117 	struct mutex mutex;
118 	enum qed_ll2_conn_type conn_type;
119 	u32 cid;
120 	u8 my_id;
121 	u8 queue_id;
122 	u8 tx_stats_id;
123 	bool b_active;
124 	u16 mtu;
125 	u8 rx_drop_ttl0_flg;
126 	u8 rx_vlan_removal_en;
127 	u8 tx_tc;
128 	enum core_tx_dest tx_dest;
129 	enum core_error_handle ai_err_packet_too_big;
130 	enum core_error_handle ai_err_no_buf;
131 	u8 tx_stats_en;
132 	struct qed_ll2_rx_queue rx_queue;
133 	struct qed_ll2_tx_queue tx_queue;
134 	u8 gsi_enable;
135 };
136 
137 /**
138  * @brief qed_ll2_acquire_connection - allocate resources,
139  *        starts rx & tx (if relevant) queues pair. Provides
140  *        connecion handler as output parameter.
141  *
142  * @param p_hwfn
143  * @param p_params		Contain various configuration properties
144  * @param rx_num_desc
145  * @param tx_num_desc
146  *
147  * @param p_connection_handle  Output container for LL2 connection's handle
148  *
149  * @return 0 on success, failure otherwise
150  */
151 int qed_ll2_acquire_connection(struct qed_hwfn *p_hwfn,
152 			       struct qed_ll2_info *p_params,
153 			       u16 rx_num_desc,
154 			       u16 tx_num_desc,
155 			       u8 *p_connection_handle);
156 
157 /**
158  * @brief qed_ll2_establish_connection - start previously
159  *        allocated LL2 queues pair
160  *
161  * @param p_hwfn
162  * @param p_ptt
163  * @param connection_handle	LL2 connection's handle obtained from
164  *                              qed_ll2_require_connection
165  *
166  * @return 0 on success, failure otherwise
167  */
168 int qed_ll2_establish_connection(struct qed_hwfn *p_hwfn, u8 connection_handle);
169 
170 /**
171  * @brief qed_ll2_post_rx_buffers - submit buffers to LL2 Rx queue.
172  *
173  * @param p_hwfn
174  * @param connection_handle	LL2 connection's handle obtained from
175  *				qed_ll2_require_connection
176  * @param addr			rx (physical address) buffers to submit
177  * @param cookie
178  * @param notify_fw		produce corresponding Rx BD immediately
179  *
180  * @return 0 on success, failure otherwise
181  */
182 int qed_ll2_post_rx_buffer(struct qed_hwfn *p_hwfn,
183 			   u8 connection_handle,
184 			   dma_addr_t addr,
185 			   u16 buf_len, void *cookie, u8 notify_fw);
186 
187 /**
188  * @brief qed_ll2_prepare_tx_packet - request for start Tx BD
189  *				      to prepare Tx packet submission to FW.
190  *
191  * @param p_hwfn
192  * @param connection_handle	LL2 connection's handle obtained from
193  *				qed_ll2_require_connection
194  * @param num_of_bds		a number of requested BD equals a number of
195  *				fragments in Tx packet
196  * @param vlan			VLAN to insert to packet (if insertion set)
197  * @param bd_flags
198  * @param l4_hdr_offset_w	L4 Header Offset from start of packet
199  *				(in words). This is needed if both l4_csum
200  *				and ipv6_ext are set
201  * @param e_tx_dest             indicates if the packet is to be transmitted via
202  *                              loopback or to the network
203  * @param first_frag
204  * @param first_frag_len
205  * @param cookie
206  *
207  * @param notify_fw
208  *
209  * @return 0 on success, failure otherwise
210  */
211 int qed_ll2_prepare_tx_packet(struct qed_hwfn *p_hwfn,
212 			      u8 connection_handle,
213 			      u8 num_of_bds,
214 			      u16 vlan,
215 			      u8 bd_flags,
216 			      u16 l4_hdr_offset_w,
217 			      enum qed_ll2_tx_dest e_tx_dest,
218 			      enum qed_ll2_roce_flavor_type qed_roce_flavor,
219 			      dma_addr_t first_frag,
220 			      u16 first_frag_len, void *cookie, u8 notify_fw);
221 
222 /**
223  * @brief qed_ll2_release_connection -	releases resources
224  *					allocated for LL2 connection
225  *
226  * @param p_hwfn
227  * @param connection_handle		LL2 connection's handle obtained from
228  *					qed_ll2_require_connection
229  */
230 void qed_ll2_release_connection(struct qed_hwfn *p_hwfn, u8 connection_handle);
231 
232 /**
233  * @brief qed_ll2_set_fragment_of_tx_packet -	provides fragments to fill
234  *						Tx BD of BDs requested by
235  *						qed_ll2_prepare_tx_packet
236  *
237  * @param p_hwfn
238  * @param connection_handle			LL2 connection's handle
239  *						obtained from
240  *						qed_ll2_require_connection
241  * @param addr
242  * @param nbytes
243  *
244  * @return 0 on success, failure otherwise
245  */
246 int qed_ll2_set_fragment_of_tx_packet(struct qed_hwfn *p_hwfn,
247 				      u8 connection_handle,
248 				      dma_addr_t addr, u16 nbytes);
249 
250 /**
251  * @brief qed_ll2_terminate_connection -	stops Tx/Rx queues
252  *
253  *
254  * @param p_hwfn
255  * @param connection_handle			LL2 connection's handle
256  *						obtained from
257  *						qed_ll2_require_connection
258  *
259  * @return 0 on success, failure otherwise
260  */
261 int qed_ll2_terminate_connection(struct qed_hwfn *p_hwfn, u8 connection_handle);
262 
263 /**
264  * @brief qed_ll2_get_stats -	get LL2 queue's statistics
265  *
266  *
267  * @param p_hwfn
268  * @param connection_handle	LL2 connection's handle obtained from
269  *				qed_ll2_require_connection
270  * @param p_stats
271  *
272  * @return 0 on success, failure otherwise
273  */
274 int qed_ll2_get_stats(struct qed_hwfn *p_hwfn,
275 		      u8 connection_handle, struct qed_ll2_stats *p_stats);
276 
277 /**
278  * @brief qed_ll2_alloc - Allocates LL2 connections set
279  *
280  * @param p_hwfn
281  *
282  * @return pointer to alocated qed_ll2_info or NULL
283  */
284 struct qed_ll2_info *qed_ll2_alloc(struct qed_hwfn *p_hwfn);
285 
286 /**
287  * @brief qed_ll2_setup - Inits LL2 connections set
288  *
289  * @param p_hwfn
290  * @param p_ll2_connections
291  *
292  */
293 void qed_ll2_setup(struct qed_hwfn *p_hwfn,
294 		   struct qed_ll2_info *p_ll2_connections);
295 
296 /**
297  * @brief qed_ll2_free - Releases LL2 connections set
298  *
299  * @param p_hwfn
300  * @param p_ll2_connections
301  *
302  */
303 void qed_ll2_free(struct qed_hwfn *p_hwfn,
304 		  struct qed_ll2_info *p_ll2_connections);
305 #endif
306