1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #ifndef _QED_LL2_H
34 #define _QED_LL2_H
35 
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/list.h>
39 #include <linux/mutex.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42 #include <linux/qed/qed_chain.h>
43 #include <linux/qed/qed_ll2_if.h>
44 #include "qed.h"
45 #include "qed_hsi.h"
46 #include "qed_sp.h"
47 
48 #define QED_MAX_NUM_OF_LL2_CONNECTIONS                    (4)
49 /* LL2 queues handles will be split as follows:
50  * first will be legacy queues, and then the ctx based queues.
51  */
52 #define QED_MAX_NUM_OF_LL2_CONNS_PF            (4)
53 #define QED_MAX_NUM_OF_LEGACY_LL2_CONNS_PF   (3)
54 
55 #define QED_MAX_NUM_OF_CTX_LL2_CONNS_PF	\
56 	(QED_MAX_NUM_OF_LL2_CONNS_PF - QED_MAX_NUM_OF_LEGACY_LL2_CONNS_PF)
57 
58 #define QED_LL2_LEGACY_CONN_BASE_PF     0
59 #define QED_LL2_CTX_CONN_BASE_PF        QED_MAX_NUM_OF_LEGACY_LL2_CONNS_PF
60 
61 
62 struct qed_ll2_rx_packet {
63 	struct list_head list_entry;
64 	struct core_rx_bd_with_buff_len *rxq_bd;
65 	dma_addr_t rx_buf_addr;
66 	u16 buf_length;
67 	void *cookie;
68 	u8 placement_offset;
69 	u16 parse_flags;
70 	u16 packet_length;
71 	u16 vlan;
72 	u32 opaque_data[2];
73 };
74 
75 struct qed_ll2_tx_packet {
76 	struct list_head list_entry;
77 	u16 bd_used;
78 	bool notify_fw;
79 	void *cookie;
80 	/* Flexible Array of bds_set determined by max_bds_per_packet */
81 	struct {
82 		struct core_tx_bd *txq_bd;
83 		dma_addr_t tx_frag;
84 		u16 frag_len;
85 	} bds_set[1];
86 };
87 
88 struct qed_ll2_rx_queue {
89 	/* Lock protecting the Rx queue manipulation */
90 	spinlock_t lock;
91 	struct qed_chain rxq_chain;
92 	struct qed_chain rcq_chain;
93 	u8 rx_sb_index;
94 	u8 ctx_based;
95 	bool b_cb_registered;
96 	__le16 *p_fw_cons;
97 	struct list_head active_descq;
98 	struct list_head free_descq;
99 	struct list_head posting_descq;
100 	struct qed_ll2_rx_packet *descq_array;
101 	void __iomem *set_prod_addr;
102 	struct core_pwm_prod_update_data db_data;
103 };
104 
105 struct qed_ll2_tx_queue {
106 	/* Lock protecting the Tx queue manipulation */
107 	spinlock_t lock;
108 	struct qed_chain txq_chain;
109 	u8 tx_sb_index;
110 	bool b_cb_registered;
111 	__le16 *p_fw_cons;
112 	struct list_head active_descq;
113 	struct list_head free_descq;
114 	struct list_head sending_descq;
115 	void *descq_mem; /* memory for variable sized qed_ll2_tx_packet*/
116 	struct qed_ll2_tx_packet *cur_send_packet;
117 	struct qed_ll2_tx_packet cur_completing_packet;
118 	u16 cur_completing_bd_idx;
119 	void __iomem *doorbell_addr;
120 	struct core_db_data db_msg;
121 	u16 bds_idx;
122 	u16 cur_send_frag_num;
123 	u16 cur_completing_frag_num;
124 	bool b_completing_packet;
125 };
126 
127 struct qed_ll2_info {
128 	/* Lock protecting the state of LL2 */
129 	struct mutex mutex;
130 
131 	struct qed_ll2_acquire_data_inputs input;
132 	u32 cid;
133 	u8 my_id;
134 	u8 queue_id;
135 	u8 tx_stats_id;
136 	bool b_active;
137 	enum core_tx_dest tx_dest;
138 	u8 tx_stats_en;
139 	bool main_func_queue;
140 	struct qed_ll2_rx_queue rx_queue;
141 	struct qed_ll2_tx_queue tx_queue;
142 	struct qed_ll2_cbs cbs;
143 };
144 
145 /**
146  * @brief qed_ll2_acquire_connection - allocate resources,
147  *        starts rx & tx (if relevant) queues pair. Provides
148  *        connecion handler as output parameter.
149  *
150  *
151  * @param cxt - pointer to the hw-function [opaque to some]
152  * @param data - describes connection parameters
153  * @return int
154  */
155 int qed_ll2_acquire_connection(void *cxt, struct qed_ll2_acquire_data *data);
156 
157 /**
158  * @brief qed_ll2_establish_connection - start previously
159  *        allocated LL2 queues pair
160  *
161  * @param cxt - pointer to the hw-function [opaque to some]
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(void *cxt, u8 connection_handle);
169 
170 /**
171  * @brief qed_ll2_post_rx_buffers - submit buffers to LL2 Rx queue.
172  *
173  * @param cxt - pointer to the hw-function [opaque to some]
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(void *cxt,
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 cxt - pointer to the hw-function [opaque to some]
192  * @param connection_handle
193  * @param pkt - info regarding the tx packet
194  * @param notify_fw - issue doorbell to fw for this packet
195  *
196  * @return 0 on success, failure otherwise
197  */
198 int qed_ll2_prepare_tx_packet(void *cxt,
199 			      u8 connection_handle,
200 			      struct qed_ll2_tx_pkt_info *pkt,
201 			      bool notify_fw);
202 
203 /**
204  * @brief qed_ll2_release_connection -	releases resources
205  *					allocated for LL2 connection
206  *
207  * @param cxt - pointer to the hw-function [opaque to some]
208  * @param connection_handle		LL2 connection's handle obtained from
209  *					qed_ll2_require_connection
210  */
211 void qed_ll2_release_connection(void *cxt, u8 connection_handle);
212 
213 /**
214  * @brief qed_ll2_set_fragment_of_tx_packet -	provides fragments to fill
215  *						Tx BD of BDs requested by
216  *						qed_ll2_prepare_tx_packet
217  *
218  * @param cxt - pointer to the hw-function [opaque to some]
219  * @param connection_handle			LL2 connection's handle
220  *						obtained from
221  *						qed_ll2_require_connection
222  * @param addr
223  * @param nbytes
224  *
225  * @return 0 on success, failure otherwise
226  */
227 int qed_ll2_set_fragment_of_tx_packet(void *cxt,
228 				      u8 connection_handle,
229 				      dma_addr_t addr, u16 nbytes);
230 
231 /**
232  * @brief qed_ll2_terminate_connection -	stops Tx/Rx queues
233  *
234  *
235  * @param cxt - pointer to the hw-function [opaque to some]
236  * @param connection_handle			LL2 connection's handle
237  *						obtained from
238  *						qed_ll2_require_connection
239  *
240  * @return 0 on success, failure otherwise
241  */
242 int qed_ll2_terminate_connection(void *cxt, u8 connection_handle);
243 
244 /**
245  * @brief qed_ll2_get_stats -	get LL2 queue's statistics
246  *
247  *
248  * @param cxt - pointer to the hw-function [opaque to some]
249  * @param connection_handle	LL2 connection's handle obtained from
250  *				qed_ll2_require_connection
251  * @param p_stats
252  *
253  * @return 0 on success, failure otherwise
254  */
255 int qed_ll2_get_stats(void *cxt,
256 		      u8 connection_handle, struct qed_ll2_stats *p_stats);
257 
258 /**
259  * @brief qed_ll2_alloc - Allocates LL2 connections set
260  *
261  * @param p_hwfn
262  *
263  * @return int
264  */
265 int qed_ll2_alloc(struct qed_hwfn *p_hwfn);
266 
267 /**
268  * @brief qed_ll2_setup - Inits LL2 connections set
269  *
270  * @param p_hwfn
271  *
272  */
273 void qed_ll2_setup(struct qed_hwfn *p_hwfn);
274 
275 /**
276  * @brief qed_ll2_free - Releases LL2 connections set
277  *
278  * @param p_hwfn
279  *
280  */
281 void qed_ll2_free(struct qed_hwfn *p_hwfn);
282 
283 #endif
284