xref: /freebsd-13-stable/sys/dev/qlnx/qlnxe/ecore_ll2.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*
2  * Copyright (c) 2018-2019 Cavium, Inc.
3  * All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  *  POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * File : ecore_ll2.c
30  */
31 #include <sys/cdefs.h>
32 #include "bcm_osal.h"
33 
34 #include "ecore.h"
35 #include "ecore_status.h"
36 #include "ecore_ll2.h"
37 #include "reg_addr.h"
38 #include "ecore_int.h"
39 #include "ecore_cxt.h"
40 #include "ecore_sp_commands.h"
41 #include "ecore_hw.h"
42 #include "reg_addr.h"
43 #include "ecore_dev_api.h"
44 #include "ecore_iro.h"
45 #include "ecore_gtt_reg_addr.h"
46 #include "ecore_ooo.h"
47 #include "ecore_hw.h"
48 #include "ecore_mcp.h"
49 
50 #define ECORE_LL2_RX_REGISTERED(ll2)	((ll2)->rx_queue.b_cb_registred)
51 #define ECORE_LL2_TX_REGISTERED(ll2)	((ll2)->tx_queue.b_cb_registred)
52 
53 #ifdef _NTDDK_
54 #pragma warning(push)
55 #pragma warning(disable : 28167)
56 #pragma warning(disable : 28123)
57 #pragma warning(disable : 28121)
58 #endif
59 
60 static struct ecore_ll2_info *
__ecore_ll2_handle_sanity(struct ecore_hwfn * p_hwfn,u8 connection_handle,bool b_lock,bool b_only_active)61 __ecore_ll2_handle_sanity(struct ecore_hwfn *p_hwfn,
62 			  u8 connection_handle,
63 			  bool b_lock, bool b_only_active)
64 {
65 	struct ecore_ll2_info *p_ll2_conn, *p_ret = OSAL_NULL;
66 
67 	if (connection_handle >= ECORE_MAX_NUM_OF_LL2_CONNECTIONS)
68 		return OSAL_NULL;
69 
70 	if (!p_hwfn->p_ll2_info)
71 		return OSAL_NULL;
72 
73 	/* TODO - is there really need for the locked vs. unlocked
74 	 * variant? I simply used what was already there.
75 	 */
76 	p_ll2_conn = &p_hwfn->p_ll2_info[connection_handle];
77 
78 	if (b_only_active) {
79 		if (b_lock)
80 			OSAL_MUTEX_ACQUIRE(&p_ll2_conn->mutex);
81 		if (p_ll2_conn->b_active)
82 			p_ret = p_ll2_conn;
83 		if (b_lock)
84 			OSAL_MUTEX_RELEASE(&p_ll2_conn->mutex);
85 	} else {
86 		p_ret = p_ll2_conn;
87 	}
88 
89 	return p_ret;
90 }
91 
92 static struct ecore_ll2_info *
ecore_ll2_handle_sanity(struct ecore_hwfn * p_hwfn,u8 connection_handle)93 ecore_ll2_handle_sanity(struct ecore_hwfn *p_hwfn,
94 			u8 connection_handle)
95 {
96 	return __ecore_ll2_handle_sanity(p_hwfn, connection_handle,
97 					 false, true);
98 }
99 
100 static struct ecore_ll2_info *
ecore_ll2_handle_sanity_lock(struct ecore_hwfn * p_hwfn,u8 connection_handle)101 ecore_ll2_handle_sanity_lock(struct ecore_hwfn *p_hwfn,
102 			     u8 connection_handle)
103 {
104 	return __ecore_ll2_handle_sanity(p_hwfn, connection_handle,
105 					 true, true);
106 }
107 
108 static struct ecore_ll2_info *
ecore_ll2_handle_sanity_inactive(struct ecore_hwfn * p_hwfn,u8 connection_handle)109 ecore_ll2_handle_sanity_inactive(struct ecore_hwfn *p_hwfn,
110 				 u8 connection_handle)
111 {
112 	return __ecore_ll2_handle_sanity(p_hwfn, connection_handle,
113 					 false, false);
114 }
115 
116 #ifndef LINUX_REMOVE
117 /* TODO - is this really been used by anyone? Is it a on future todo list? */
118 enum _ecore_status_t
ecore_ll2_get_fragment_of_tx_packet(struct ecore_hwfn * p_hwfn,u8 connection_handle,dma_addr_t * p_addr,bool * b_last_fragment)119 ecore_ll2_get_fragment_of_tx_packet(struct ecore_hwfn *p_hwfn,
120 				    u8 connection_handle,
121 				    dma_addr_t *p_addr,
122 				    bool *b_last_fragment)
123 {
124 	struct ecore_ll2_tx_packet *p_pkt;
125 	struct ecore_ll2_info *p_ll2_conn;
126 	u16 cur_frag_idx = 0;
127 
128 	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
129 	if (p_ll2_conn == OSAL_NULL)
130 		return ECORE_INVAL;
131 	p_pkt = &p_ll2_conn->tx_queue.cur_completing_packet;
132 
133 	if (!p_ll2_conn->tx_queue.b_completing_packet || !p_addr)
134 		return ECORE_INVAL;
135 
136 	if (p_ll2_conn->tx_queue.cur_completing_bd_idx == p_pkt->bd_used)
137 		return ECORE_INVAL;
138 
139 	/* Packet is available and has at least one more frag - provide it */
140 	cur_frag_idx = p_ll2_conn->tx_queue.cur_completing_bd_idx++;
141 	*p_addr = p_pkt->bds_set[cur_frag_idx].tx_frag;
142 	if (b_last_fragment)
143 		*b_last_fragment = p_pkt->bd_used ==
144 				   p_ll2_conn->tx_queue.cur_completing_bd_idx;
145 
146 	return ECORE_SUCCESS;
147 }
148 #endif
149 
ecore_ll2_txq_flush(struct ecore_hwfn * p_hwfn,u8 connection_handle)150 static void ecore_ll2_txq_flush(struct ecore_hwfn *p_hwfn,
151 				u8 connection_handle)
152 {
153 	bool b_last_packet = false, b_last_frag = false;
154 	struct ecore_ll2_tx_packet *p_pkt = OSAL_NULL;
155 	struct ecore_ll2_info *p_ll2_conn;
156 	struct ecore_ll2_tx_queue *p_tx;
157 	unsigned long flags = 0;
158 	dma_addr_t tx_frag;
159 
160 	p_ll2_conn = ecore_ll2_handle_sanity_inactive(p_hwfn,
161 						      connection_handle);
162 	if (p_ll2_conn == OSAL_NULL)
163 		return;
164 	p_tx = &p_ll2_conn->tx_queue;
165 
166 	OSAL_SPIN_LOCK_IRQSAVE(&p_tx->lock, flags);
167 	while (!OSAL_LIST_IS_EMPTY(&p_tx->active_descq)) {
168 		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_tx->active_descq,
169 					      struct ecore_ll2_tx_packet,
170 					      list_entry);
171 
172 		if (p_pkt == OSAL_NULL)
173 			break;
174 
175 #if defined(_NTDDK_)
176 #pragma warning(suppress : 6011 28182)
177 #endif
178 		OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
179 				       &p_tx->active_descq);
180 		b_last_packet = OSAL_LIST_IS_EMPTY(&p_tx->active_descq);
181 		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
182 				    &p_tx->free_descq);
183 		OSAL_SPIN_UNLOCK_IRQSAVE(&p_tx->lock, flags);
184 		if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_OOO) {
185 			struct ecore_ooo_buffer	*p_buffer;
186 
187 			p_buffer = (struct ecore_ooo_buffer *)p_pkt->cookie;
188 			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
189 		} else {
190 			p_tx->cur_completing_packet = *p_pkt;
191 			p_tx->cur_completing_bd_idx = 1;
192 			b_last_frag = p_tx->cur_completing_bd_idx ==
193 				      p_pkt->bd_used;
194 
195 			tx_frag = p_pkt->bds_set[0].tx_frag;
196 			p_ll2_conn->cbs.tx_release_cb(p_ll2_conn->cbs.cookie,
197 						      p_ll2_conn->my_id,
198 						      p_pkt->cookie,
199 						      tx_frag,
200 						      b_last_frag,
201 						      b_last_packet);
202 		}
203 		OSAL_SPIN_LOCK_IRQSAVE(&p_tx->lock, flags);
204 	}
205 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_tx->lock, flags);
206 }
207 
208 static enum _ecore_status_t
ecore_ll2_txq_completion(struct ecore_hwfn * p_hwfn,void * p_cookie)209 ecore_ll2_txq_completion(struct ecore_hwfn *p_hwfn,
210 			 void *p_cookie)
211 {
212 	struct ecore_ll2_info *p_ll2_conn = (struct ecore_ll2_info*)p_cookie;
213 	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
214 	u16 new_idx = 0, num_bds = 0, num_bds_in_packet = 0;
215 	struct ecore_ll2_tx_packet *p_pkt;
216 	bool b_last_frag = false;
217 	unsigned long flags;
218 	enum _ecore_status_t rc = ECORE_INVAL;
219 
220 	OSAL_SPIN_LOCK_IRQSAVE(&p_tx->lock, flags);
221 	if (p_tx->b_completing_packet) {
222 		/* TODO - this looks completely unnecessary to me - the only
223 		 * way we can re-enter is by the DPC calling us again, but this
224 		 * would only happen AFTER we return, and we unset this at end
225 		 * of the function.
226 		 */
227 		rc = ECORE_BUSY;
228 		goto out;
229 	}
230 
231 	new_idx = OSAL_LE16_TO_CPU(*p_tx->p_fw_cons);
232 	num_bds = ((s16)new_idx - (s16)p_tx->bds_idx);
233 	while (num_bds) {
234 		if (OSAL_LIST_IS_EMPTY(&p_tx->active_descq))
235 			goto out;
236 
237 		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_tx->active_descq,
238 					      struct ecore_ll2_tx_packet,
239 					      list_entry);
240 		if (!p_pkt)
241 			goto out;
242 
243 		p_tx->b_completing_packet = true;
244 		p_tx->cur_completing_packet = *p_pkt;
245 		num_bds_in_packet = p_pkt->bd_used;
246 #if defined(_NTDDK_)
247 #pragma warning(suppress : 6011 28182)
248 #endif
249 		OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
250 				       &p_tx->active_descq);
251 
252 		if (num_bds < num_bds_in_packet) {
253 			DP_NOTICE(p_hwfn, true,
254 				  "Rest of BDs does not cover whole packet\n");
255 			goto out;
256 		}
257 
258 		num_bds -= num_bds_in_packet;
259 		p_tx->bds_idx += num_bds_in_packet;
260 		while (num_bds_in_packet--)
261 			ecore_chain_consume(&p_tx->txq_chain);
262 
263 		p_tx->cur_completing_bd_idx = 1;
264 		b_last_frag = p_tx->cur_completing_bd_idx ==
265 			      p_pkt->bd_used;
266 		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
267 				    &p_tx->free_descq);
268 
269 		OSAL_SPIN_UNLOCK_IRQSAVE(&p_tx->lock, flags);
270 
271 		p_ll2_conn->cbs.tx_comp_cb(p_ll2_conn->cbs.cookie,
272 					   p_ll2_conn->my_id,
273 					   p_pkt->cookie,
274 					   p_pkt->bds_set[0].tx_frag,
275 					   b_last_frag,
276 					   !num_bds);
277 
278 		OSAL_SPIN_LOCK_IRQSAVE(&p_tx->lock, flags);
279 	}
280 
281 	p_tx->b_completing_packet = false;
282 	rc = ECORE_SUCCESS;
283 out:
284 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_tx->lock, flags);
285 	return rc;
286 }
287 
ecore_ll2_rxq_parse_gsi(union core_rx_cqe_union * p_cqe,struct ecore_ll2_comp_rx_data * data)288 static void ecore_ll2_rxq_parse_gsi(union core_rx_cqe_union *p_cqe,
289 				    struct ecore_ll2_comp_rx_data *data)
290 {
291 	data->parse_flags =
292 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_gsi.parse_flags.flags);
293 	data->length.data_length =
294 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_gsi.data_length);
295 	data->vlan =
296 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_gsi.vlan);
297 	data->opaque_data_0 =
298 		OSAL_LE32_TO_CPU(p_cqe->rx_cqe_gsi.src_mac_addrhi);
299 	data->opaque_data_1 =
300 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_gsi.src_mac_addrlo);
301 	data->u.data_length_error =
302 		p_cqe->rx_cqe_gsi.data_length_error;
303 	data->qp_id = OSAL_LE16_TO_CPU(p_cqe->rx_cqe_gsi.qp_id);
304 
305 	data->src_qp = OSAL_LE32_TO_CPU(p_cqe->rx_cqe_gsi.src_qp);
306 }
307 
ecore_ll2_rxq_parse_reg(union core_rx_cqe_union * p_cqe,struct ecore_ll2_comp_rx_data * data)308 static void ecore_ll2_rxq_parse_reg(union core_rx_cqe_union *p_cqe,
309 				    struct ecore_ll2_comp_rx_data *data)
310 {
311 	data->parse_flags =
312 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_fp.parse_flags.flags);
313 	data->err_flags =
314 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_fp.err_flags.flags);
315 	data->length.packet_length =
316 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_fp.packet_length);
317 	data->vlan =
318 		OSAL_LE16_TO_CPU(p_cqe->rx_cqe_fp.vlan);
319 	data->opaque_data_0 =
320 		OSAL_LE32_TO_CPU(p_cqe->rx_cqe_fp.opaque_data.data[0]);
321 	data->opaque_data_1 =
322 		OSAL_LE32_TO_CPU(p_cqe->rx_cqe_fp.opaque_data.data[1]);
323 	data->u.placement_offset =
324 		p_cqe->rx_cqe_fp.placement_offset;
325 }
326 
327 #if defined(_NTDDK_)
328 #pragma warning(suppress : 28167 26110)
329 #endif
330 static enum _ecore_status_t
ecore_ll2_handle_slowpath(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn,union core_rx_cqe_union * p_cqe,unsigned long * p_lock_flags)331 ecore_ll2_handle_slowpath(struct ecore_hwfn *p_hwfn,
332 			  struct ecore_ll2_info *p_ll2_conn,
333 			  union core_rx_cqe_union *p_cqe,
334 			  unsigned long *p_lock_flags)
335 {
336 	struct ecore_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
337 	struct core_rx_slow_path_cqe *sp_cqe;
338 
339 	sp_cqe = &p_cqe->rx_cqe_sp;
340 	if (sp_cqe->ramrod_cmd_id != CORE_RAMROD_RX_QUEUE_FLUSH) {
341 		DP_NOTICE(p_hwfn, true,
342 			  "LL2 - unexpected Rx CQE slowpath ramrod_cmd_id:%d\n",
343 			  sp_cqe->ramrod_cmd_id);
344 		return ECORE_INVAL;
345 	}
346 
347 	if (p_ll2_conn->cbs.slowpath_cb == OSAL_NULL) {
348 		DP_NOTICE(p_hwfn, true,
349 			  "LL2 - received RX_QUEUE_FLUSH but no callback was provided\n");
350 		return ECORE_INVAL;
351 	}
352 
353 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_rx->lock, *p_lock_flags);
354 
355 	p_ll2_conn->cbs.slowpath_cb(p_ll2_conn->cbs.cookie,
356 				    p_ll2_conn->my_id,
357 				    OSAL_LE32_TO_CPU(sp_cqe->opaque_data.data[0]),
358 				    OSAL_LE32_TO_CPU(sp_cqe->opaque_data.data[1]));
359 
360 	OSAL_SPIN_LOCK_IRQSAVE(&p_rx->lock, *p_lock_flags);
361 
362 	return ECORE_SUCCESS;
363 }
364 
365 static enum _ecore_status_t
ecore_ll2_rxq_handle_completion(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn,union core_rx_cqe_union * p_cqe,unsigned long * p_lock_flags,bool b_last_cqe)366 ecore_ll2_rxq_handle_completion(struct ecore_hwfn *p_hwfn,
367 				struct ecore_ll2_info *p_ll2_conn,
368 				union core_rx_cqe_union *p_cqe,
369 				unsigned long *p_lock_flags,
370 				bool b_last_cqe)
371 {
372 	struct ecore_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
373 	struct ecore_ll2_rx_packet *p_pkt = OSAL_NULL;
374 	struct ecore_ll2_comp_rx_data data;
375 
376 	if (!OSAL_LIST_IS_EMPTY(&p_rx->active_descq))
377 		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_rx->active_descq,
378 					      struct ecore_ll2_rx_packet,
379 					      list_entry);
380 	if (!p_pkt) {
381 		DP_NOTICE(p_hwfn, false,
382 			  "[%d] LL2 Rx completion but active_descq is empty\n",
383 			  p_ll2_conn->input.conn_type);
384 
385 		return ECORE_IO;
386 	}
387 
388 	OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry, &p_rx->active_descq);
389 
390 	if (p_cqe->rx_cqe_sp.type == CORE_RX_CQE_TYPE_REGULAR)
391 		ecore_ll2_rxq_parse_reg(p_cqe, &data);
392 	else
393 		ecore_ll2_rxq_parse_gsi(p_cqe, &data);
394 
395 	if (ecore_chain_consume(&p_rx->rxq_chain) != p_pkt->rxq_bd) {
396 		DP_NOTICE(p_hwfn, false,
397 			  "Mismatch between active_descq and the LL2 Rx chain\n");
398 		/* TODO - didn't return error value since this wasn't handled
399 		 * before, but this is obviously lacking.
400 		 */
401 	}
402 
403 	OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry, &p_rx->free_descq);
404 
405 	data.connection_handle = p_ll2_conn->my_id;
406 	data.cookie = p_pkt->cookie;
407 	data.rx_buf_addr = p_pkt->rx_buf_addr;
408 	data.b_last_packet = b_last_cqe;
409 
410 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_rx->lock, *p_lock_flags);
411 	p_ll2_conn->cbs.rx_comp_cb(p_ll2_conn->cbs.cookie,
412 				   &data);
413 
414 	OSAL_SPIN_LOCK_IRQSAVE(&p_rx->lock, *p_lock_flags);
415 
416 	return ECORE_SUCCESS;
417 }
418 
ecore_ll2_rxq_completion(struct ecore_hwfn * p_hwfn,void * cookie)419 static enum _ecore_status_t ecore_ll2_rxq_completion(struct ecore_hwfn *p_hwfn,
420 						     void *cookie)
421 {
422 	struct ecore_ll2_info *p_ll2_conn = (struct ecore_ll2_info*)cookie;
423 	struct ecore_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
424 	union core_rx_cqe_union *cqe = OSAL_NULL;
425 	u16 cq_new_idx = 0, cq_old_idx = 0;
426 	unsigned long flags = 0;
427 	enum _ecore_status_t rc = ECORE_SUCCESS;
428 
429 	OSAL_SPIN_LOCK_IRQSAVE(&p_rx->lock, flags);
430 	cq_new_idx = OSAL_LE16_TO_CPU(*p_rx->p_fw_cons);
431 	cq_old_idx = ecore_chain_get_cons_idx(&p_rx->rcq_chain);
432 
433 	while (cq_new_idx != cq_old_idx) {
434 		bool b_last_cqe = (cq_new_idx == cq_old_idx);
435 
436 		cqe = (union core_rx_cqe_union *)ecore_chain_consume(&p_rx->rcq_chain);
437 		cq_old_idx = ecore_chain_get_cons_idx(&p_rx->rcq_chain);
438 
439 		DP_VERBOSE(p_hwfn, ECORE_MSG_LL2,
440 			   "LL2 [sw. cons %04x, fw. at %04x] - Got Packet of type %02x\n",
441 			   cq_old_idx, cq_new_idx, cqe->rx_cqe_sp.type);
442 
443 		switch (cqe->rx_cqe_sp.type) {
444 		case CORE_RX_CQE_TYPE_SLOW_PATH:
445 			rc = ecore_ll2_handle_slowpath(p_hwfn, p_ll2_conn,
446 						       cqe, &flags);
447 			break;
448 		case CORE_RX_CQE_TYPE_GSI_OFFLOAD:
449 		case CORE_RX_CQE_TYPE_REGULAR:
450 			rc = ecore_ll2_rxq_handle_completion(p_hwfn, p_ll2_conn,
451 							     cqe, &flags,
452 							     b_last_cqe);
453 			break;
454 		default:
455 			rc = ECORE_IO;
456 		}
457 	}
458 
459 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_rx->lock, flags);
460 	return rc;
461 }
462 
ecore_ll2_rxq_flush(struct ecore_hwfn * p_hwfn,u8 connection_handle)463 static void ecore_ll2_rxq_flush(struct ecore_hwfn *p_hwfn,
464 			 u8 connection_handle)
465 {
466 	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
467 	struct ecore_ll2_rx_packet *p_pkt = OSAL_NULL;
468 	struct ecore_ll2_rx_queue *p_rx;
469 	unsigned long flags = 0;
470 
471 	p_ll2_conn = ecore_ll2_handle_sanity_inactive(p_hwfn,
472 						      connection_handle);
473 	if (p_ll2_conn == OSAL_NULL)
474 		return;
475 	p_rx = &p_ll2_conn->rx_queue;
476 
477 	OSAL_SPIN_LOCK_IRQSAVE(&p_rx->lock, flags);
478 	while (!OSAL_LIST_IS_EMPTY(&p_rx->active_descq)) {
479 		bool b_last;
480 		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_rx->active_descq,
481 					      struct ecore_ll2_rx_packet,
482 					      list_entry);
483 		if (p_pkt == OSAL_NULL)
484 			break;
485 #if defined(_NTDDK_)
486 #pragma warning(suppress : 6011 28182)
487 #endif
488 		OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
489 				       &p_rx->active_descq);
490 		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
491 				    &p_rx->free_descq);
492 		b_last = OSAL_LIST_IS_EMPTY(&p_rx->active_descq);
493 		OSAL_SPIN_UNLOCK_IRQSAVE(&p_rx->lock, flags);
494 
495 		if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_OOO) {
496 			struct ecore_ooo_buffer	*p_buffer;
497 
498 			p_buffer = (struct ecore_ooo_buffer *)p_pkt->cookie;
499 			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
500 		} else {
501 			dma_addr_t rx_buf_addr = p_pkt->rx_buf_addr;
502 			void *cookie  = p_pkt->cookie;
503 
504 			p_ll2_conn->cbs.rx_release_cb(p_ll2_conn->cbs.cookie,
505 						      p_ll2_conn->my_id,
506 						      cookie,
507 						      rx_buf_addr,
508 						      b_last);
509 		}
510 		OSAL_SPIN_LOCK_IRQSAVE(&p_rx->lock, flags);
511 	}
512 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_rx->lock, flags);
513 }
514 
515 static bool
ecore_ll2_lb_rxq_handler_slowpath(struct ecore_hwfn * p_hwfn,struct core_rx_slow_path_cqe * p_cqe)516 ecore_ll2_lb_rxq_handler_slowpath(struct ecore_hwfn *p_hwfn,
517 				  struct core_rx_slow_path_cqe *p_cqe)
518 {
519 	struct ooo_opaque *iscsi_ooo;
520 	u32 cid;
521 
522 	if (p_cqe->ramrod_cmd_id != CORE_RAMROD_RX_QUEUE_FLUSH)
523 		return false;
524 
525 	iscsi_ooo = (struct ooo_opaque *)&p_cqe->opaque_data;
526 	if (iscsi_ooo->ooo_opcode != TCP_EVENT_DELETE_ISLES)
527 		return false;
528 
529 	/* Need to make a flush */
530 	cid = OSAL_LE32_TO_CPU(iscsi_ooo->cid);
531 	ecore_ooo_release_connection_isles(p_hwfn->p_ooo_info, cid);
532 
533 	return true;
534 }
535 
536 static enum _ecore_status_t
ecore_ll2_lb_rxq_handler(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)537 ecore_ll2_lb_rxq_handler(struct ecore_hwfn *p_hwfn,
538 			 struct ecore_ll2_info *p_ll2_conn)
539 {
540 	struct ecore_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
541 	u16 packet_length = 0, parse_flags = 0, vlan = 0;
542 	struct ecore_ll2_rx_packet *p_pkt = OSAL_NULL;
543 	u32 num_ooo_add_to_peninsula = 0, cid;
544 	union core_rx_cqe_union *cqe = OSAL_NULL;
545 	u16 cq_new_idx = 0, cq_old_idx = 0;
546 	struct ecore_ooo_buffer	*p_buffer;
547 	struct ooo_opaque *iscsi_ooo;
548 	u8 placement_offset = 0;
549 	u8 cqe_type;
550 
551 	cq_new_idx = OSAL_LE16_TO_CPU(*p_rx->p_fw_cons);
552 	cq_old_idx = ecore_chain_get_cons_idx(&p_rx->rcq_chain);
553 	if (cq_new_idx == cq_old_idx)
554 		return ECORE_SUCCESS;
555 
556 	while (cq_new_idx != cq_old_idx) {
557 		struct core_rx_fast_path_cqe *p_cqe_fp;
558 
559 		cqe = (union core_rx_cqe_union *)ecore_chain_consume(&p_rx->rcq_chain);
560 		cq_old_idx = ecore_chain_get_cons_idx(&p_rx->rcq_chain);
561 		cqe_type = cqe->rx_cqe_sp.type;
562 
563 		if (cqe_type == CORE_RX_CQE_TYPE_SLOW_PATH)
564 			if (ecore_ll2_lb_rxq_handler_slowpath(p_hwfn,
565 							      &cqe->rx_cqe_sp))
566 				continue;
567 
568 		if (cqe_type != CORE_RX_CQE_TYPE_REGULAR) {
569 			DP_NOTICE(p_hwfn, true,
570 				  "Got a non-regular LB LL2 completion [type 0x%02x]\n",
571 				  cqe_type);
572 			return ECORE_INVAL;
573 		}
574 		p_cqe_fp = &cqe->rx_cqe_fp;
575 
576 		placement_offset = p_cqe_fp->placement_offset;
577 		parse_flags = OSAL_LE16_TO_CPU(p_cqe_fp->parse_flags.flags);
578 		packet_length = OSAL_LE16_TO_CPU(p_cqe_fp->packet_length);
579 		vlan = OSAL_LE16_TO_CPU(p_cqe_fp->vlan);
580 		iscsi_ooo = (struct ooo_opaque *)&p_cqe_fp->opaque_data;
581 		ecore_ooo_save_history_entry(p_hwfn->p_ooo_info, iscsi_ooo);
582 		cid = OSAL_LE32_TO_CPU(iscsi_ooo->cid);
583 
584 		/* Process delete isle first*/
585 		if (iscsi_ooo->drop_size)
586 			ecore_ooo_delete_isles(p_hwfn, p_hwfn->p_ooo_info, cid,
587 					       iscsi_ooo->drop_isle,
588 					       iscsi_ooo->drop_size);
589 
590 		if (iscsi_ooo->ooo_opcode == TCP_EVENT_NOP)
591 			continue;
592 
593 		/* Now process create/add/join isles */
594 		if (OSAL_LIST_IS_EMPTY(&p_rx->active_descq)) {
595 			DP_NOTICE(p_hwfn, true,
596 				  "LL2 OOO RX chain has no submitted buffers\n");
597 			return ECORE_IO;
598 		}
599 
600 		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_rx->active_descq,
601 					      struct ecore_ll2_rx_packet,
602 					      list_entry);
603 
604 		if ((iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_NEW_ISLE) ||
605 		    (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_ISLE_RIGHT) ||
606 		    (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_ISLE_LEFT) ||
607 		    (iscsi_ooo->ooo_opcode == TCP_EVENT_ADD_PEN) ||
608 		    (iscsi_ooo->ooo_opcode == TCP_EVENT_JOIN)) {
609 			if (!p_pkt) {
610 				DP_NOTICE(p_hwfn, true,
611 					  "LL2 OOO RX packet is not valid\n");
612 				return ECORE_IO;
613 			}
614 #if defined(_NTDDK_)
615 #pragma warning(suppress : 6011 28182)
616 #endif
617 			OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
618 					       &p_rx->active_descq);
619 			p_buffer = (struct ecore_ooo_buffer *)p_pkt->cookie;
620 			p_buffer->packet_length = packet_length;
621 			p_buffer->parse_flags = parse_flags;
622 			p_buffer->vlan = vlan;
623 			p_buffer->placement_offset = placement_offset;
624 			if (ecore_chain_consume(&p_rx->rxq_chain) !=
625 			    p_pkt->rxq_bd) {
626 				/**/
627 			}
628 			ecore_ooo_dump_rx_event(p_hwfn, iscsi_ooo, p_buffer);
629 			OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
630 					    &p_rx->free_descq);
631 
632 			switch (iscsi_ooo->ooo_opcode) {
633 			case TCP_EVENT_ADD_NEW_ISLE:
634 				ecore_ooo_add_new_isle(p_hwfn,
635 						       p_hwfn->p_ooo_info,
636 						       cid,
637 						       iscsi_ooo->ooo_isle,
638 						       p_buffer);
639 				break;
640 			case TCP_EVENT_ADD_ISLE_RIGHT:
641 				ecore_ooo_add_new_buffer(p_hwfn,
642 							 p_hwfn->p_ooo_info,
643 							 cid,
644 							 iscsi_ooo->ooo_isle,
645 							 p_buffer,
646 							 ECORE_OOO_RIGHT_BUF);
647 				break;
648 			case TCP_EVENT_ADD_ISLE_LEFT:
649 				ecore_ooo_add_new_buffer(p_hwfn,
650 							 p_hwfn->p_ooo_info,
651 							 cid,
652 							 iscsi_ooo->ooo_isle,
653 							 p_buffer,
654 							 ECORE_OOO_LEFT_BUF);
655 				break;
656 			case TCP_EVENT_JOIN:
657 				ecore_ooo_add_new_buffer(p_hwfn,
658 							 p_hwfn->p_ooo_info,
659 							 cid,
660 							 iscsi_ooo->ooo_isle +
661 							 1,
662 							 p_buffer,
663 							 ECORE_OOO_LEFT_BUF);
664 				ecore_ooo_join_isles(p_hwfn,
665 						     p_hwfn->p_ooo_info,
666 						     cid,
667 						     iscsi_ooo->ooo_isle);
668 				break;
669 			case TCP_EVENT_ADD_PEN:
670 				num_ooo_add_to_peninsula++;
671 				ecore_ooo_put_ready_buffer(p_hwfn->p_ooo_info,
672 							   p_buffer, true);
673 				break;
674 			}
675 		} else {
676 			DP_NOTICE(p_hwfn, true,
677 				  "Unexpected event (%d) TX OOO completion\n",
678 				  iscsi_ooo->ooo_opcode);
679 		}
680 	}
681 
682 	return ECORE_SUCCESS;
683 }
684 
685 static void
ecore_ooo_submit_tx_buffers(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)686 ecore_ooo_submit_tx_buffers(struct ecore_hwfn *p_hwfn,
687 			    struct ecore_ll2_info *p_ll2_conn)
688 {
689 	struct ecore_ll2_tx_pkt_info tx_pkt;
690 	struct ecore_ooo_buffer *p_buffer;
691 	dma_addr_t first_frag;
692 	u16 l4_hdr_offset_w;
693 	u8 bd_flags;
694 	enum _ecore_status_t rc;
695 
696 	/* Submit Tx buffers here */
697 	while ((p_buffer = ecore_ooo_get_ready_buffer(p_hwfn->p_ooo_info))) {
698 		l4_hdr_offset_w = 0;
699 		bd_flags = 0;
700 
701 		first_frag = p_buffer->rx_buffer_phys_addr +
702 			     p_buffer->placement_offset;
703 		SET_FIELD(bd_flags, CORE_TX_BD_DATA_FORCE_VLAN_MODE, 1);
704 		SET_FIELD(bd_flags, CORE_TX_BD_DATA_L4_PROTOCOL, 1);
705 
706 		OSAL_MEM_ZERO(&tx_pkt, sizeof(tx_pkt));
707 		tx_pkt.num_of_bds = 1;
708 		tx_pkt.vlan = p_buffer->vlan;
709 		tx_pkt.bd_flags = bd_flags;
710 		tx_pkt.l4_hdr_offset_w = l4_hdr_offset_w;
711 		tx_pkt.tx_dest = (enum ecore_ll2_tx_dest)p_ll2_conn->tx_dest;
712 		tx_pkt.first_frag = first_frag;
713 		tx_pkt.first_frag_len = p_buffer->packet_length;
714 		tx_pkt.cookie = p_buffer;
715 
716 		rc = ecore_ll2_prepare_tx_packet(p_hwfn, p_ll2_conn->my_id,
717 						 &tx_pkt, true);
718 		if (rc != ECORE_SUCCESS) {
719 			ecore_ooo_put_ready_buffer(p_hwfn->p_ooo_info,
720 						   p_buffer, false);
721 			break;
722 		}
723 	}
724 }
725 
726 static void
ecore_ooo_submit_rx_buffers(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)727 ecore_ooo_submit_rx_buffers(struct ecore_hwfn *p_hwfn,
728 			    struct ecore_ll2_info *p_ll2_conn)
729 {
730 	struct ecore_ooo_buffer *p_buffer;
731 	enum _ecore_status_t rc;
732 
733 	while ((p_buffer = ecore_ooo_get_free_buffer(p_hwfn->p_ooo_info))) {
734 		rc = ecore_ll2_post_rx_buffer(p_hwfn,
735 					    p_ll2_conn->my_id,
736 					    p_buffer->rx_buffer_phys_addr,
737 					    0, p_buffer, true);
738 		if (rc != ECORE_SUCCESS) {
739 			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
740 			break;
741 		}
742 	}
743 }
744 
745 static enum _ecore_status_t
ecore_ll2_lb_rxq_completion(struct ecore_hwfn * p_hwfn,void * p_cookie)746 ecore_ll2_lb_rxq_completion(struct ecore_hwfn *p_hwfn,
747 			    void *p_cookie)
748 {
749 	struct ecore_ll2_info *p_ll2_conn = (struct ecore_ll2_info *)p_cookie;
750 	enum _ecore_status_t rc;
751 
752 	rc = ecore_ll2_lb_rxq_handler(p_hwfn, p_ll2_conn);
753 	if (rc != ECORE_SUCCESS)
754 		return rc;
755 
756 	ecore_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
757 	ecore_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
758 
759 	return 0;
760 }
761 
762 static enum _ecore_status_t
ecore_ll2_lb_txq_completion(struct ecore_hwfn * p_hwfn,void * p_cookie)763 ecore_ll2_lb_txq_completion(struct ecore_hwfn *p_hwfn,
764 			    void *p_cookie)
765 {
766 	struct ecore_ll2_info *p_ll2_conn = (struct ecore_ll2_info *)p_cookie;
767 	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
768 	struct ecore_ll2_tx_packet *p_pkt = OSAL_NULL;
769 	struct ecore_ooo_buffer	*p_buffer;
770 	bool b_dont_submit_rx = false;
771 	u16 new_idx = 0, num_bds = 0;
772 	enum _ecore_status_t rc;
773 
774 	new_idx = OSAL_LE16_TO_CPU(*p_tx->p_fw_cons);
775 	num_bds = ((s16)new_idx - (s16)p_tx->bds_idx);
776 
777 	if (!num_bds)
778 		return ECORE_SUCCESS;
779 
780 	while (num_bds) {
781 		if (OSAL_LIST_IS_EMPTY(&p_tx->active_descq))
782 			return ECORE_INVAL;
783 
784 		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_tx->active_descq,
785 					      struct ecore_ll2_tx_packet,
786 					      list_entry);
787 		if (!p_pkt)
788 			return ECORE_INVAL;
789 
790 		if (p_pkt->bd_used != 1) {
791 			DP_NOTICE(p_hwfn, true,
792 				  "Unexpectedly many BDs(%d) in TX OOO completion\n",
793 				  p_pkt->bd_used);
794 			return ECORE_INVAL;
795 		}
796 
797 		OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
798 				       &p_tx->active_descq);
799 
800 		num_bds--;
801 		p_tx->bds_idx++;
802 		ecore_chain_consume(&p_tx->txq_chain);
803 
804 		p_buffer = (struct ecore_ooo_buffer *)p_pkt->cookie;
805 		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
806 				    &p_tx->free_descq);
807 
808 		if (b_dont_submit_rx) {
809 			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
810 			continue;
811 		}
812 
813 		rc = ecore_ll2_post_rx_buffer(p_hwfn, p_ll2_conn->my_id,
814 					      p_buffer->rx_buffer_phys_addr, 0,
815 					      p_buffer, true);
816 		if (rc != ECORE_SUCCESS) {
817 			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
818 			b_dont_submit_rx = true;
819 		}
820 	}
821 
822 	ecore_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
823 
824 	return ECORE_SUCCESS;
825 }
826 
ecore_sp_ll2_rx_queue_start(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn,u8 action_on_error)827 static enum _ecore_status_t ecore_sp_ll2_rx_queue_start(struct ecore_hwfn *p_hwfn,
828 							struct ecore_ll2_info *p_ll2_conn,
829 							u8 action_on_error)
830 {
831 	enum ecore_ll2_conn_type conn_type = p_ll2_conn->input.conn_type;
832 	struct ecore_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
833 	struct core_rx_start_ramrod_data *p_ramrod = OSAL_NULL;
834 	struct ecore_spq_entry *p_ent = OSAL_NULL;
835 	struct ecore_sp_init_data init_data;
836 	u16 cqe_pbl_size;
837 	enum _ecore_status_t rc	= ECORE_SUCCESS;
838 
839 	/* Get SPQ entry */
840 	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
841 	init_data.cid = p_ll2_conn->cid;
842 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
843 	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
844 
845 	rc = ecore_sp_init_request(p_hwfn, &p_ent,
846 				   CORE_RAMROD_RX_QUEUE_START,
847 				   PROTOCOLID_CORE, &init_data);
848 	if (rc != ECORE_SUCCESS)
849 		return rc;
850 
851 	p_ramrod = &p_ent->ramrod.core_rx_queue_start;
852 
853 	p_ramrod->sb_id = OSAL_CPU_TO_LE16(ecore_int_get_sp_sb_id(p_hwfn));
854 	p_ramrod->sb_index = p_rx->rx_sb_index;
855 	p_ramrod->complete_event_flg = 1;
856 
857 	p_ramrod->mtu = OSAL_CPU_TO_LE16(p_ll2_conn->input.mtu);
858 	DMA_REGPAIR_LE(p_ramrod->bd_base,
859 		       p_rx->rxq_chain.p_phys_addr);
860 	cqe_pbl_size = (u16)ecore_chain_get_page_cnt(&p_rx->rcq_chain);
861 	p_ramrod->num_of_pbl_pages = OSAL_CPU_TO_LE16(cqe_pbl_size);
862 	DMA_REGPAIR_LE(p_ramrod->cqe_pbl_addr,
863 		       ecore_chain_get_pbl_phys(&p_rx->rcq_chain));
864 
865 	p_ramrod->drop_ttl0_flg = p_ll2_conn->input.rx_drop_ttl0_flg;
866 	p_ramrod->inner_vlan_stripping_en =
867 		p_ll2_conn->input.rx_vlan_removal_en;
868 
869 	if (OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC, &p_hwfn->p_dev->mf_bits) &&
870 	    (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_FCOE))
871 		p_ramrod->report_outer_vlan = 1;
872 	p_ramrod->queue_id = p_ll2_conn->queue_id;
873 	p_ramrod->main_func_queue = p_ll2_conn->main_func_queue;
874 
875 	if (OSAL_TEST_BIT(ECORE_MF_LL2_NON_UNICAST,
876 			  &p_hwfn->p_dev->mf_bits) &&
877 	    p_ramrod->main_func_queue &&
878 	    ((conn_type != ECORE_LL2_TYPE_ROCE) &&
879 	     (conn_type != ECORE_LL2_TYPE_IWARP))) {
880 		p_ramrod->mf_si_bcast_accept_all = 1;
881 		p_ramrod->mf_si_mcast_accept_all = 1;
882 	} else {
883 		p_ramrod->mf_si_bcast_accept_all = 0;
884 		p_ramrod->mf_si_mcast_accept_all = 0;
885 	}
886 
887 	p_ramrod->action_on_error.error_type = action_on_error;
888 	p_ramrod->gsi_offload_flag = p_ll2_conn->input.gsi_enable;
889 	return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
890 }
891 
ecore_sp_ll2_tx_queue_start(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)892 static enum _ecore_status_t ecore_sp_ll2_tx_queue_start(struct ecore_hwfn *p_hwfn,
893 							struct ecore_ll2_info *p_ll2_conn)
894 {
895 	enum ecore_ll2_conn_type conn_type = p_ll2_conn->input.conn_type;
896 	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
897 	struct core_tx_start_ramrod_data *p_ramrod = OSAL_NULL;
898 	struct ecore_spq_entry *p_ent = OSAL_NULL;
899 	struct ecore_sp_init_data init_data;
900 	u16 pq_id = 0, pbl_size;
901 	enum _ecore_status_t rc = ECORE_NOTIMPL;
902 
903 	if (!ECORE_LL2_TX_REGISTERED(p_ll2_conn))
904 		return ECORE_SUCCESS;
905 
906 	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_OOO)
907 		p_ll2_conn->tx_stats_en = 0;
908 	else
909 		p_ll2_conn->tx_stats_en = 1;
910 
911 	/* Get SPQ entry */
912 	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
913 	init_data.cid = p_ll2_conn->cid;
914 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
915 	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
916 
917 	rc = ecore_sp_init_request(p_hwfn, &p_ent,
918 				   CORE_RAMROD_TX_QUEUE_START,
919 				   PROTOCOLID_CORE, &init_data);
920 	if (rc != ECORE_SUCCESS)
921 		return rc;
922 
923 	p_ramrod = &p_ent->ramrod.core_tx_queue_start;
924 
925 	p_ramrod->sb_id = OSAL_CPU_TO_LE16(ecore_int_get_sp_sb_id(p_hwfn));
926 	p_ramrod->sb_index = p_tx->tx_sb_index;
927 	p_ramrod->mtu = OSAL_CPU_TO_LE16(p_ll2_conn->input.mtu);
928 	p_ramrod->stats_en = p_ll2_conn->tx_stats_en;
929 	p_ramrod->stats_id = p_ll2_conn->tx_stats_id;
930 
931 	DMA_REGPAIR_LE(p_ramrod->pbl_base_addr,
932 		       ecore_chain_get_pbl_phys(&p_tx->txq_chain));
933 	pbl_size = (u16)ecore_chain_get_page_cnt(&p_tx->txq_chain);
934 	p_ramrod->pbl_size = OSAL_CPU_TO_LE16(pbl_size);
935 
936 	/* TODO RESC_ALLOC pq for ll2 */
937 	switch (p_ll2_conn->input.tx_tc) {
938 	case PURE_LB_TC:
939 		pq_id = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
940 		break;
941 	case PKT_LB_TC:
942 		pq_id = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OOO);
943 		break;
944 	default:
945 		pq_id = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
946 	}
947 
948 	p_ramrod->qm_pq_id = OSAL_CPU_TO_LE16(pq_id);
949 
950 	switch (conn_type) {
951 	case ECORE_LL2_TYPE_FCOE:
952 		p_ramrod->conn_type = PROTOCOLID_FCOE;
953 		break;
954 	case ECORE_LL2_TYPE_ISCSI:
955 		p_ramrod->conn_type = PROTOCOLID_ISCSI;
956 		break;
957 	case ECORE_LL2_TYPE_ROCE:
958 		p_ramrod->conn_type = PROTOCOLID_ROCE;
959 		break;
960 	case ECORE_LL2_TYPE_IWARP:
961 		p_ramrod->conn_type = PROTOCOLID_IWARP;
962 		break;
963 	case ECORE_LL2_TYPE_OOO:
964 		if (p_hwfn->hw_info.personality == ECORE_PCI_ISCSI) {
965 			p_ramrod->conn_type = PROTOCOLID_ISCSI;
966 		} else {
967 			p_ramrod->conn_type = PROTOCOLID_IWARP;
968 		}
969 		break;
970 	default:
971 		p_ramrod->conn_type = PROTOCOLID_ETH;
972 		DP_NOTICE(p_hwfn, false, "Unknown connection type: %d\n",
973 			  conn_type);
974 	}
975 
976 	p_ramrod->gsi_offload_flag = p_ll2_conn->input.gsi_enable;
977 
978 	rc = ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
979 	if (rc != ECORE_SUCCESS)
980 		return rc;
981 
982 	rc = ecore_db_recovery_add(p_hwfn->p_dev, p_tx->doorbell_addr,
983 				   &p_tx->db_msg, DB_REC_WIDTH_32B,
984 				   DB_REC_KERNEL);
985 	return rc;
986 }
987 
ecore_sp_ll2_rx_queue_stop(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)988 static enum _ecore_status_t ecore_sp_ll2_rx_queue_stop(struct ecore_hwfn *p_hwfn,
989 						       struct ecore_ll2_info *p_ll2_conn)
990 {
991 	struct core_rx_stop_ramrod_data	*p_ramrod = OSAL_NULL;
992 	struct ecore_spq_entry *p_ent = OSAL_NULL;
993 	struct ecore_sp_init_data init_data;
994 	enum _ecore_status_t rc = ECORE_NOTIMPL;
995 
996 	/* Get SPQ entry */
997 	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
998 	init_data.cid = p_ll2_conn->cid;
999 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
1000 	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
1001 
1002 	rc = ecore_sp_init_request(p_hwfn, &p_ent,
1003 				   CORE_RAMROD_RX_QUEUE_STOP,
1004 				   PROTOCOLID_CORE, &init_data);
1005 	if (rc != ECORE_SUCCESS)
1006 		return rc;
1007 
1008 	p_ramrod = &p_ent->ramrod.core_rx_queue_stop;
1009 
1010 	p_ramrod->complete_event_flg = 1;
1011 	p_ramrod->queue_id = p_ll2_conn->queue_id;
1012 
1013 	return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
1014 }
1015 
ecore_sp_ll2_tx_queue_stop(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)1016 static enum _ecore_status_t ecore_sp_ll2_tx_queue_stop(struct ecore_hwfn *p_hwfn,
1017 						       struct ecore_ll2_info *p_ll2_conn)
1018 {
1019 	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
1020 	struct ecore_spq_entry *p_ent = OSAL_NULL;
1021 	struct ecore_sp_init_data init_data;
1022 	enum _ecore_status_t rc = ECORE_NOTIMPL;
1023 
1024 	ecore_db_recovery_del(p_hwfn->p_dev, p_tx->doorbell_addr,
1025 			      &p_tx->db_msg);
1026 
1027 	/* Get SPQ entry */
1028 	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
1029 	init_data.cid = p_ll2_conn->cid;
1030 	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
1031 	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
1032 
1033 	rc = ecore_sp_init_request(p_hwfn, &p_ent,
1034 				   CORE_RAMROD_TX_QUEUE_STOP,
1035 				   PROTOCOLID_CORE, &init_data);
1036 	if (rc != ECORE_SUCCESS)
1037 		return rc;
1038 
1039 	return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
1040 }
1041 
1042 static enum _ecore_status_t
ecore_ll2_acquire_connection_rx(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_info)1043 ecore_ll2_acquire_connection_rx(struct ecore_hwfn *p_hwfn,
1044 				struct ecore_ll2_info *p_ll2_info)
1045 {
1046 	struct ecore_ll2_rx_packet *p_descq;
1047 	u32 capacity;
1048 	enum _ecore_status_t rc = ECORE_SUCCESS;
1049 
1050 	if (!p_ll2_info->input.rx_num_desc)
1051 		goto out;
1052 
1053 	rc = ecore_chain_alloc(p_hwfn->p_dev,
1054 			       ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
1055 			       ECORE_CHAIN_MODE_NEXT_PTR,
1056 			       ECORE_CHAIN_CNT_TYPE_U16,
1057 			       p_ll2_info->input.rx_num_desc,
1058 			       sizeof(struct core_rx_bd),
1059 			       &p_ll2_info->rx_queue.rxq_chain, OSAL_NULL);
1060 	if (rc) {
1061 		DP_NOTICE(p_hwfn, false,
1062 			  "Failed to allocate ll2 rxq chain\n");
1063 		goto out;
1064 	}
1065 
1066 	capacity = ecore_chain_get_capacity(&p_ll2_info->rx_queue.rxq_chain);
1067 	p_descq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1068 			      capacity * sizeof(struct ecore_ll2_rx_packet));
1069 	if (!p_descq) {
1070 		rc = ECORE_NOMEM;
1071 		DP_NOTICE(p_hwfn, false,
1072 			  "Failed to allocate ll2 Rx desc\n");
1073 		goto out;
1074 	}
1075 	p_ll2_info->rx_queue.descq_array = p_descq;
1076 
1077 	rc = ecore_chain_alloc(p_hwfn->p_dev,
1078 			       ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
1079 			       ECORE_CHAIN_MODE_PBL,
1080 			       ECORE_CHAIN_CNT_TYPE_U16,
1081 			       p_ll2_info->input.rx_num_desc,
1082 			       sizeof(struct core_rx_fast_path_cqe),
1083 			       &p_ll2_info->rx_queue.rcq_chain, OSAL_NULL);
1084 	if (rc != ECORE_SUCCESS) {
1085 		DP_NOTICE(p_hwfn, false,
1086 			  "Failed to allocate ll2 rcq chain\n");
1087 		goto out;
1088 	}
1089 
1090 	DP_VERBOSE(p_hwfn, ECORE_MSG_LL2,
1091 		   "Allocated LL2 Rxq [Type %08x] with 0x%08x buffers\n",
1092 		   p_ll2_info->input.conn_type,
1093 		   p_ll2_info->input.rx_num_desc);
1094 
1095 out:
1096 	return rc;
1097 }
1098 
1099 static enum _ecore_status_t
ecore_ll2_acquire_connection_tx(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_info)1100 ecore_ll2_acquire_connection_tx(struct ecore_hwfn *p_hwfn,
1101 				struct ecore_ll2_info *p_ll2_info)
1102 {
1103 	struct ecore_ll2_tx_packet *p_descq;
1104 	u32 capacity;
1105 	enum _ecore_status_t rc = ECORE_SUCCESS;
1106 	u32 desc_size;
1107 
1108 	if (!p_ll2_info->input.tx_num_desc)
1109 		goto out;
1110 
1111 	rc = ecore_chain_alloc(p_hwfn->p_dev,
1112 			       ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
1113 			       ECORE_CHAIN_MODE_PBL,
1114 			       ECORE_CHAIN_CNT_TYPE_U16,
1115 			       p_ll2_info->input.tx_num_desc,
1116 			       sizeof(struct core_tx_bd),
1117 			       &p_ll2_info->tx_queue.txq_chain, OSAL_NULL);
1118 	if (rc != ECORE_SUCCESS)
1119 		goto out;
1120 
1121 	capacity = ecore_chain_get_capacity(&p_ll2_info->tx_queue.txq_chain);
1122 	desc_size = (sizeof(*p_descq) +
1123 		     (p_ll2_info->input.tx_max_bds_per_packet - 1) *
1124 		     sizeof(p_descq->bds_set));
1125 
1126 	p_descq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1127 			      capacity * desc_size);
1128 	if (!p_descq) {
1129 		rc = ECORE_NOMEM;
1130 		goto out;
1131 	}
1132 	p_ll2_info->tx_queue.descq_array = p_descq;
1133 
1134 	DP_VERBOSE(p_hwfn, ECORE_MSG_LL2,
1135 		   "Allocated LL2 Txq [Type %08x] with 0x%08x buffers\n",
1136 		   p_ll2_info->input.conn_type,
1137 		   p_ll2_info->input.tx_num_desc);
1138 
1139 out:
1140 	if (rc != ECORE_SUCCESS)
1141 		DP_NOTICE(p_hwfn, false,
1142 			  "Can't allocate memory for Tx LL2 with 0x%08x buffers\n",
1143 			  p_ll2_info->input.tx_num_desc);
1144 	return rc;
1145 }
1146 
1147 static enum _ecore_status_t
ecore_ll2_acquire_connection_ooo(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_info,u16 mtu)1148 ecore_ll2_acquire_connection_ooo(struct ecore_hwfn *p_hwfn,
1149 				 struct ecore_ll2_info *p_ll2_info, u16 mtu)
1150 {
1151 	struct ecore_ooo_buffer *p_buf = OSAL_NULL;
1152 	u32 rx_buffer_size = 0;
1153 	void *p_virt;
1154 	u16 buf_idx;
1155 	enum _ecore_status_t rc = ECORE_SUCCESS;
1156 
1157 	if (p_ll2_info->input.conn_type != ECORE_LL2_TYPE_OOO)
1158 		return rc;
1159 
1160 	/* Correct number of requested OOO buffers if needed */
1161 	if (!p_ll2_info->input.rx_num_ooo_buffers) {
1162 		u16 num_desc = p_ll2_info->input.rx_num_desc;
1163 
1164 		if (!num_desc)
1165 			return ECORE_INVAL;
1166 		p_ll2_info->input.rx_num_ooo_buffers = num_desc * 2;
1167 	}
1168 
1169 	/* TODO - use some defines for buffer size */
1170 	rx_buffer_size = mtu + 14 + 4 + 8 + ETH_CACHE_LINE_SIZE;
1171 	rx_buffer_size = (rx_buffer_size + ETH_CACHE_LINE_SIZE - 1) &
1172 			 ~(ETH_CACHE_LINE_SIZE - 1);
1173 
1174 	for (buf_idx = 0; buf_idx < p_ll2_info->input.rx_num_ooo_buffers;
1175 	     buf_idx++) {
1176 		p_buf = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_buf));
1177 		if (!p_buf) {
1178 			DP_NOTICE(p_hwfn, false,
1179 				  "Failed to allocate ooo descriptor\n");
1180 			rc = ECORE_NOMEM;
1181 			goto out;
1182 		}
1183 
1184 		p_buf->rx_buffer_size = rx_buffer_size;
1185 		p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev,
1186 						 &p_buf->rx_buffer_phys_addr,
1187 						 p_buf->rx_buffer_size);
1188 		if (!p_virt) {
1189 			DP_NOTICE(p_hwfn, false,
1190 				  "Failed to allocate ooo buffer\n");
1191 			OSAL_FREE(p_hwfn->p_dev, p_buf);
1192 			rc = ECORE_NOMEM;
1193 			goto out;
1194 		}
1195 		p_buf->rx_buffer_virt_addr = p_virt;
1196 		ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buf);
1197 	}
1198 
1199 	DP_VERBOSE(p_hwfn, ECORE_MSG_LL2,
1200 		   "Allocated [%04x] LL2 OOO buffers [each of size 0x%08x]\n",
1201 		   p_ll2_info->input.rx_num_ooo_buffers, rx_buffer_size);
1202 
1203 out:
1204 	return rc;
1205 }
1206 
1207 static enum _ecore_status_t
ecore_ll2_set_cbs(struct ecore_ll2_info * p_ll2_info,const struct ecore_ll2_cbs * cbs)1208 ecore_ll2_set_cbs(struct ecore_ll2_info *p_ll2_info,
1209 		    const struct ecore_ll2_cbs *cbs)
1210 {
1211 	if (!cbs || (!cbs->rx_comp_cb ||
1212 		     !cbs->rx_release_cb ||
1213 		     !cbs->tx_comp_cb ||
1214 		     !cbs->tx_release_cb ||
1215 		     !cbs->cookie))
1216 		return ECORE_INVAL;
1217 
1218 	p_ll2_info->cbs.rx_comp_cb = cbs->rx_comp_cb;
1219 	p_ll2_info->cbs.rx_release_cb = cbs->rx_release_cb;
1220 	p_ll2_info->cbs.tx_comp_cb = cbs->tx_comp_cb;
1221 	p_ll2_info->cbs.tx_release_cb = cbs->tx_release_cb;
1222 	p_ll2_info->cbs.slowpath_cb = cbs->slowpath_cb;
1223 	p_ll2_info->cbs.cookie = cbs->cookie;
1224 
1225 	return ECORE_SUCCESS;
1226 }
1227 
1228 static enum core_error_handle
ecore_ll2_get_error_choice(enum ecore_ll2_error_handle err)1229 ecore_ll2_get_error_choice(enum ecore_ll2_error_handle err)
1230 {
1231 	switch (err) {
1232 	case ECORE_LL2_DROP_PACKET:
1233 		return LL2_DROP_PACKET;
1234 	case ECORE_LL2_DO_NOTHING:
1235 		return LL2_DO_NOTHING;
1236 	case ECORE_LL2_ASSERT:
1237 		return LL2_ASSERT;
1238 	default:
1239 		return LL2_DO_NOTHING;
1240 	}
1241 }
1242 
1243 enum _ecore_status_t
ecore_ll2_acquire_connection(void * cxt,struct ecore_ll2_acquire_data * data)1244 ecore_ll2_acquire_connection(void *cxt,
1245 			     struct ecore_ll2_acquire_data *data)
1246 {
1247 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1248 	ecore_int_comp_cb_t comp_rx_cb, comp_tx_cb;
1249 	struct ecore_ll2_info *p_ll2_info = OSAL_NULL;
1250 	enum _ecore_status_t rc;
1251 	u8 i, *p_tx_max;
1252 
1253 	if (!data->p_connection_handle || !p_hwfn->p_ll2_info) {
1254 		DP_NOTICE(p_hwfn, false, "Invalid connection handle, ll2_info not allocated\n");
1255 		return ECORE_INVAL;
1256 	}
1257 
1258 	/* Find a free connection to be used */
1259 	for (i = 0; (i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS); i++) {
1260 		OSAL_MUTEX_ACQUIRE(&p_hwfn->p_ll2_info[i].mutex);
1261 		if (p_hwfn->p_ll2_info[i].b_active) {
1262 			OSAL_MUTEX_RELEASE(&p_hwfn->p_ll2_info[i].mutex);
1263 			continue;
1264 		}
1265 
1266 		p_hwfn->p_ll2_info[i].b_active = true;
1267 		p_ll2_info = &p_hwfn->p_ll2_info[i];
1268 		OSAL_MUTEX_RELEASE(&p_hwfn->p_ll2_info[i].mutex);
1269 		break;
1270 	}
1271 	if (p_ll2_info == OSAL_NULL) {
1272 		DP_NOTICE(p_hwfn, false, "No available ll2 connection\n");
1273 		return ECORE_BUSY;
1274 	}
1275 
1276 	OSAL_MEMCPY(&p_ll2_info->input, &data->input,
1277 		    sizeof(p_ll2_info->input));
1278 
1279 	switch (data->input.tx_dest) {
1280 	case ECORE_LL2_TX_DEST_NW:
1281 		p_ll2_info->tx_dest = CORE_TX_DEST_NW;
1282 		break;
1283 	case ECORE_LL2_TX_DEST_LB:
1284 		p_ll2_info->tx_dest = CORE_TX_DEST_LB;
1285 		break;
1286 	case ECORE_LL2_TX_DEST_DROP:
1287 		p_ll2_info->tx_dest = CORE_TX_DEST_DROP;
1288 		break;
1289 	default:
1290 		return ECORE_INVAL;
1291 	}
1292 
1293 	if ((data->input.conn_type == ECORE_LL2_TYPE_OOO) ||
1294 	    data->input.secondary_queue)
1295 		p_ll2_info->main_func_queue = false;
1296 	else
1297 		p_ll2_info->main_func_queue = true;
1298 
1299 	/* Correct maximum number of Tx BDs */
1300 	p_tx_max = &p_ll2_info->input.tx_max_bds_per_packet;
1301 	if (*p_tx_max == 0)
1302 		*p_tx_max = CORE_LL2_TX_MAX_BDS_PER_PACKET;
1303 	else
1304 		*p_tx_max = OSAL_MIN_T(u8, *p_tx_max,
1305 				       CORE_LL2_TX_MAX_BDS_PER_PACKET);
1306 
1307 	rc = ecore_ll2_set_cbs(p_ll2_info, data->cbs);
1308 	if (rc) {
1309 		DP_NOTICE(p_hwfn, false, "Invalid callback functions\n");
1310 		goto q_allocate_fail;
1311 	}
1312 
1313 	rc = ecore_ll2_acquire_connection_rx(p_hwfn, p_ll2_info);
1314 	if (rc != ECORE_SUCCESS) {
1315 		DP_NOTICE(p_hwfn, false, "ll2 acquire rx connection failed\n");
1316 		goto q_allocate_fail;
1317 	}
1318 
1319 	rc = ecore_ll2_acquire_connection_tx(p_hwfn, p_ll2_info);
1320 	if (rc != ECORE_SUCCESS) {
1321 		DP_NOTICE(p_hwfn, false, "ll2 acquire tx connection failed\n");
1322 		goto q_allocate_fail;
1323 	}
1324 
1325 	rc = ecore_ll2_acquire_connection_ooo(p_hwfn, p_ll2_info,
1326 					      data->input.mtu);
1327 	if (rc != ECORE_SUCCESS) {
1328 		DP_NOTICE(p_hwfn, false, "ll2 acquire ooo connection failed\n");
1329 		goto q_allocate_fail;
1330 	}
1331 
1332 	/* Register callbacks for the Rx/Tx queues */
1333 	if (data->input.conn_type == ECORE_LL2_TYPE_OOO) {
1334 		comp_rx_cb = ecore_ll2_lb_rxq_completion;
1335 		comp_tx_cb = ecore_ll2_lb_txq_completion;
1336 
1337 	} else {
1338 		comp_rx_cb = ecore_ll2_rxq_completion;
1339 		comp_tx_cb = ecore_ll2_txq_completion;
1340 	}
1341 
1342 	if (data->input.rx_num_desc) {
1343 		ecore_int_register_cb(p_hwfn, comp_rx_cb,
1344 				      &p_hwfn->p_ll2_info[i],
1345 				      &p_ll2_info->rx_queue.rx_sb_index,
1346 				      &p_ll2_info->rx_queue.p_fw_cons);
1347 		p_ll2_info->rx_queue.b_cb_registred   = true;
1348 	}
1349 
1350 	if (data->input.tx_num_desc) {
1351 		ecore_int_register_cb(p_hwfn,
1352 				      comp_tx_cb,
1353 				      &p_hwfn->p_ll2_info[i],
1354 				      &p_ll2_info->tx_queue.tx_sb_index,
1355 				      &p_ll2_info->tx_queue.p_fw_cons);
1356 		p_ll2_info->tx_queue.b_cb_registred   = true;
1357 	}
1358 
1359 	*(data->p_connection_handle) = i;
1360 	return rc;
1361 
1362 q_allocate_fail:
1363 	ecore_ll2_release_connection(p_hwfn, i);
1364 	return ECORE_NOMEM;
1365 }
1366 
ecore_ll2_establish_connection_rx(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)1367 static enum _ecore_status_t ecore_ll2_establish_connection_rx(struct ecore_hwfn *p_hwfn,
1368 							      struct ecore_ll2_info *p_ll2_conn)
1369 {
1370 	enum ecore_ll2_error_handle error_input;
1371 	enum core_error_handle error_mode;
1372 	u8 action_on_error = 0;
1373 
1374 	if (!ECORE_LL2_RX_REGISTERED(p_ll2_conn))
1375 		return ECORE_SUCCESS;
1376 
1377 	DIRECT_REG_WR(p_hwfn, p_ll2_conn->rx_queue.set_prod_addr, 0x0);
1378 	error_input = p_ll2_conn->input.ai_err_packet_too_big;
1379 	error_mode = ecore_ll2_get_error_choice(error_input);
1380 	SET_FIELD(action_on_error,
1381 		  CORE_RX_ACTION_ON_ERROR_PACKET_TOO_BIG, error_mode);
1382 	error_input = p_ll2_conn->input.ai_err_no_buf;
1383 	error_mode = ecore_ll2_get_error_choice(error_input);
1384 	SET_FIELD(action_on_error,
1385 		  CORE_RX_ACTION_ON_ERROR_NO_BUFF, error_mode);
1386 
1387 	return ecore_sp_ll2_rx_queue_start(p_hwfn, p_ll2_conn, action_on_error);
1388 }
1389 
1390 static void
ecore_ll2_establish_connection_ooo(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)1391 ecore_ll2_establish_connection_ooo(struct ecore_hwfn *p_hwfn,
1392 				   struct ecore_ll2_info *p_ll2_conn)
1393 {
1394 	if (p_ll2_conn->input.conn_type != ECORE_LL2_TYPE_OOO)
1395 		return;
1396 
1397 	ecore_ooo_release_all_isles(p_hwfn->p_ooo_info);
1398 	ecore_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
1399 }
1400 
ecore_ll2_establish_connection(void * cxt,u8 connection_handle)1401 enum _ecore_status_t ecore_ll2_establish_connection(void *cxt,
1402 						    u8 connection_handle)
1403 {
1404 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1405 	struct e4_core_conn_context *p_cxt;
1406 	struct ecore_ll2_info *p_ll2_conn;
1407 	struct ecore_cxt_info cxt_info;
1408 	struct ecore_ll2_rx_queue *p_rx;
1409 	struct ecore_ll2_tx_queue *p_tx;
1410 	struct ecore_ll2_tx_packet *p_pkt;
1411 	struct ecore_ptt *p_ptt;
1412 	enum _ecore_status_t rc = ECORE_NOTIMPL;
1413 	u32 i, capacity;
1414 	u32 desc_size;
1415 	u8 qid;
1416 
1417 	p_ptt = ecore_ptt_acquire(p_hwfn);
1418 	if (!p_ptt)
1419 		return ECORE_AGAIN;
1420 
1421 	p_ll2_conn = ecore_ll2_handle_sanity_lock(p_hwfn, connection_handle);
1422 	if (p_ll2_conn == OSAL_NULL) {
1423 		rc = ECORE_INVAL;
1424 		goto out;
1425 	}
1426 
1427 	p_rx = &p_ll2_conn->rx_queue;
1428 	p_tx = &p_ll2_conn->tx_queue;
1429 
1430 	ecore_chain_reset(&p_rx->rxq_chain);
1431 	ecore_chain_reset(&p_rx->rcq_chain);
1432 	OSAL_LIST_INIT(&p_rx->active_descq);
1433 	OSAL_LIST_INIT(&p_rx->free_descq);
1434 	OSAL_LIST_INIT(&p_rx->posting_descq);
1435 	OSAL_SPIN_LOCK_INIT(&p_rx->lock);
1436 	capacity = ecore_chain_get_capacity(&p_rx->rxq_chain);
1437 	for (i = 0; i < capacity; i++)
1438 		OSAL_LIST_PUSH_TAIL(&p_rx->descq_array[i].list_entry,
1439 				    &p_rx->free_descq);
1440 	*p_rx->p_fw_cons = 0;
1441 
1442 	ecore_chain_reset(&p_tx->txq_chain);
1443 	OSAL_LIST_INIT(&p_tx->active_descq);
1444 	OSAL_LIST_INIT(&p_tx->free_descq);
1445 	OSAL_LIST_INIT(&p_tx->sending_descq);
1446 	OSAL_SPIN_LOCK_INIT(&p_tx->lock);
1447 	capacity = ecore_chain_get_capacity(&p_tx->txq_chain);
1448 	/* The size of the element in descq_array is flexible */
1449 	desc_size = (sizeof(*p_pkt) +
1450 		     (p_ll2_conn->input.tx_max_bds_per_packet - 1) *
1451 		     sizeof(p_pkt->bds_set));
1452 
1453 	for (i = 0; i < capacity; i++) {
1454 		p_pkt = (struct ecore_ll2_tx_packet *)((u8 *)p_tx->descq_array +
1455 						       desc_size*i);
1456 		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
1457 				    &p_tx->free_descq);
1458 	}
1459 	p_tx->cur_completing_bd_idx = 0;
1460 	p_tx->bds_idx = 0;
1461 	p_tx->b_completing_packet = false;
1462 	p_tx->cur_send_packet = OSAL_NULL;
1463 	p_tx->cur_send_frag_num = 0;
1464 	p_tx->cur_completing_frag_num = 0;
1465 	*p_tx->p_fw_cons = 0;
1466 
1467 	rc = ecore_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_ll2_conn->cid);
1468 	if (rc)
1469 		goto out;
1470 	cxt_info.iid = p_ll2_conn->cid;
1471 	rc = ecore_cxt_get_cid_info(p_hwfn, &cxt_info);
1472 	if (rc) {
1473 		DP_NOTICE(p_hwfn, true, "Cannot find context info for cid=%d\n",
1474 			  p_ll2_conn->cid);
1475 		goto out;
1476 	}
1477 
1478 	p_cxt = cxt_info.p_cxt;
1479 
1480 	/* @@@TBD we zero the context until we have ilt_reset implemented. */
1481 	OSAL_MEM_ZERO(p_cxt, sizeof(*p_cxt));
1482 
1483 	qid = ecore_ll2_handle_to_queue_id(p_hwfn, connection_handle);
1484 	p_ll2_conn->queue_id = qid;
1485 	p_ll2_conn->tx_stats_id = qid;
1486 	p_rx->set_prod_addr = (u8 OSAL_IOMEM*)p_hwfn->regview +
1487 					      GTT_BAR0_MAP_REG_TSDM_RAM +
1488 					      TSTORM_LL2_RX_PRODS_OFFSET(qid);
1489 	p_tx->doorbell_addr = (u8 OSAL_IOMEM*)p_hwfn->doorbells +
1490 					      DB_ADDR(p_ll2_conn->cid,
1491 						      DQ_DEMS_LEGACY);
1492 
1493 	/* prepare db data */
1494 	SET_FIELD(p_tx->db_msg.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
1495 	SET_FIELD(p_tx->db_msg.params, CORE_DB_DATA_AGG_CMD,
1496 		  DB_AGG_CMD_SET);
1497 	SET_FIELD(p_tx->db_msg.params, CORE_DB_DATA_AGG_VAL_SEL,
1498 		  DQ_XCM_CORE_TX_BD_PROD_CMD);
1499 	p_tx->db_msg.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
1500 
1501 	rc = ecore_ll2_establish_connection_rx(p_hwfn, p_ll2_conn);
1502 	if (rc)
1503 		goto out;
1504 
1505 	rc = ecore_sp_ll2_tx_queue_start(p_hwfn, p_ll2_conn);
1506 	if (rc)
1507 		goto out;
1508 
1509 	if (!ECORE_IS_RDMA_PERSONALITY(p_hwfn))
1510 		ecore_wr(p_hwfn, p_ptt, PRS_REG_USE_LIGHT_L2, 1);
1511 
1512 	ecore_ll2_establish_connection_ooo(p_hwfn, p_ll2_conn);
1513 
1514 	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_FCOE) {
1515 		if (!OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC,
1516 				   &p_hwfn->p_dev->mf_bits))
1517 			ecore_llh_add_protocol_filter(p_hwfn->p_dev, 0,
1518 						      ECORE_LLH_FILTER_ETHERTYPE,
1519 						      0x8906, 0);
1520 		ecore_llh_add_protocol_filter(p_hwfn->p_dev, 0,
1521 					      ECORE_LLH_FILTER_ETHERTYPE,
1522 					      0x8914, 0);
1523 	}
1524 
1525 out:
1526 	ecore_ptt_release(p_hwfn, p_ptt);
1527 
1528 	return rc;
1529 }
1530 
ecore_ll2_post_rx_buffer_notify_fw(struct ecore_hwfn * p_hwfn,struct ecore_ll2_rx_queue * p_rx,struct ecore_ll2_rx_packet * p_curp)1531 static void ecore_ll2_post_rx_buffer_notify_fw(struct ecore_hwfn *p_hwfn,
1532 					       struct ecore_ll2_rx_queue *p_rx,
1533 					       struct ecore_ll2_rx_packet *p_curp)
1534 {
1535 	struct ecore_ll2_rx_packet *p_posting_packet = OSAL_NULL;
1536 	struct core_ll2_rx_prod rx_prod = {0, 0, 0};
1537 	bool b_notify_fw = false;
1538 	u16 bd_prod, cq_prod;
1539 
1540 	/* This handles the flushing of already posted buffers */
1541 	while (!OSAL_LIST_IS_EMPTY(&p_rx->posting_descq)) {
1542 		p_posting_packet = OSAL_LIST_FIRST_ENTRY(&p_rx->posting_descq,
1543 							 struct ecore_ll2_rx_packet,
1544 							 list_entry);
1545 #if defined(_NTDDK_)
1546 #pragma warning(suppress : 6011 28182)
1547 #endif
1548 		OSAL_LIST_REMOVE_ENTRY(&p_posting_packet->list_entry, &p_rx->posting_descq);
1549 		OSAL_LIST_PUSH_TAIL(&p_posting_packet->list_entry, &p_rx->active_descq);
1550 		b_notify_fw = true;
1551 	}
1552 
1553 	/* This handles the supplied packet [if there is one] */
1554 	if (p_curp) {
1555 		OSAL_LIST_PUSH_TAIL(&p_curp->list_entry,
1556 				    &p_rx->active_descq);
1557 		b_notify_fw = true;
1558 	}
1559 
1560 	if (!b_notify_fw)
1561 		return;
1562 
1563 	bd_prod = ecore_chain_get_prod_idx(&p_rx->rxq_chain);
1564 	cq_prod = ecore_chain_get_prod_idx(&p_rx->rcq_chain);
1565 	rx_prod.bd_prod = OSAL_CPU_TO_LE16(bd_prod);
1566 	rx_prod.cqe_prod = OSAL_CPU_TO_LE16(cq_prod);
1567 	DIRECT_REG_WR(p_hwfn, p_rx->set_prod_addr, *((u32 *)&rx_prod));
1568 }
1569 
ecore_ll2_post_rx_buffer(void * cxt,u8 connection_handle,dma_addr_t addr,u16 buf_len,void * cookie,u8 notify_fw)1570 enum _ecore_status_t ecore_ll2_post_rx_buffer(void *cxt,
1571 					      u8 connection_handle,
1572 					      dma_addr_t addr,
1573 					      u16 buf_len,
1574 					      void *cookie,
1575 					      u8 notify_fw)
1576 {
1577 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1578 	struct core_rx_bd_with_buff_len *p_curb = OSAL_NULL;
1579 	struct ecore_ll2_rx_packet *p_curp = OSAL_NULL;
1580 	struct ecore_ll2_info *p_ll2_conn;
1581 	struct ecore_ll2_rx_queue *p_rx;
1582 	unsigned long flags;
1583 	void *p_data;
1584 	enum _ecore_status_t rc = ECORE_SUCCESS;
1585 
1586 	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1587 	if (p_ll2_conn == OSAL_NULL)
1588 		return ECORE_INVAL;
1589 	p_rx = &p_ll2_conn->rx_queue;
1590 	if (p_rx->set_prod_addr == OSAL_NULL)
1591 		return ECORE_IO;
1592 
1593 	OSAL_SPIN_LOCK_IRQSAVE(&p_rx->lock, flags);
1594 	if (!OSAL_LIST_IS_EMPTY(&p_rx->free_descq))
1595 		p_curp = OSAL_LIST_FIRST_ENTRY(&p_rx->free_descq,
1596 					       struct ecore_ll2_rx_packet,
1597 					       list_entry);
1598 	if (p_curp) {
1599 		if (ecore_chain_get_elem_left(&p_rx->rxq_chain) &&
1600 		    ecore_chain_get_elem_left(&p_rx->rcq_chain)) {
1601 			p_data = ecore_chain_produce(&p_rx->rxq_chain);
1602 			p_curb = (struct core_rx_bd_with_buff_len *)p_data;
1603 			ecore_chain_produce(&p_rx->rcq_chain);
1604 		}
1605 	}
1606 
1607 	/* If we're lacking entires, let's try to flush buffers to FW */
1608 	if (!p_curp || !p_curb) {
1609 		rc =  ECORE_BUSY;
1610 		p_curp = OSAL_NULL;
1611 		goto out_notify;
1612 	}
1613 
1614 	/* We have an Rx packet we can fill */
1615 	DMA_REGPAIR_LE(p_curb->addr, addr);
1616 	p_curb->buff_length = OSAL_CPU_TO_LE16(buf_len);
1617 	p_curp->rx_buf_addr = addr;
1618 	p_curp->cookie = cookie;
1619 	p_curp->rxq_bd = p_curb;
1620 	p_curp->buf_length = buf_len;
1621 	OSAL_LIST_REMOVE_ENTRY(&p_curp->list_entry,
1622 			       &p_rx->free_descq);
1623 
1624 	/* Check if we only want to enqueue this packet without informing FW */
1625 	if (!notify_fw) {
1626 		OSAL_LIST_PUSH_TAIL(&p_curp->list_entry,
1627 				    &p_rx->posting_descq);
1628 		goto out;
1629 	}
1630 
1631 out_notify:
1632 	ecore_ll2_post_rx_buffer_notify_fw(p_hwfn, p_rx, p_curp);
1633 out:
1634 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_rx->lock, flags);
1635 	return rc;
1636 }
1637 
ecore_ll2_prepare_tx_packet_set(struct ecore_ll2_tx_queue * p_tx,struct ecore_ll2_tx_packet * p_curp,struct ecore_ll2_tx_pkt_info * pkt,u8 notify_fw)1638 static void ecore_ll2_prepare_tx_packet_set(struct ecore_ll2_tx_queue *p_tx,
1639 					    struct ecore_ll2_tx_packet *p_curp,
1640 					    struct ecore_ll2_tx_pkt_info *pkt,
1641 					    u8 notify_fw)
1642 {
1643 	OSAL_LIST_REMOVE_ENTRY(&p_curp->list_entry,
1644 			       &p_tx->free_descq);
1645 	p_curp->cookie = pkt->cookie;
1646 	p_curp->bd_used = pkt->num_of_bds;
1647 	p_curp->notify_fw = notify_fw;
1648 	p_tx->cur_send_packet = p_curp;
1649 	p_tx->cur_send_frag_num = 0;
1650 
1651 	p_curp->bds_set[p_tx->cur_send_frag_num].tx_frag = pkt->first_frag;
1652 	p_curp->bds_set[p_tx->cur_send_frag_num].frag_len = pkt->first_frag_len;
1653 	p_tx->cur_send_frag_num++;
1654 }
1655 
ecore_ll2_prepare_tx_packet_set_bd(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2,struct ecore_ll2_tx_packet * p_curp,struct ecore_ll2_tx_pkt_info * pkt)1656 static void ecore_ll2_prepare_tx_packet_set_bd(
1657 		struct ecore_hwfn *p_hwfn,
1658 		struct ecore_ll2_info *p_ll2,
1659 		struct ecore_ll2_tx_packet *p_curp,
1660 		struct ecore_ll2_tx_pkt_info *pkt)
1661 {
1662 	struct ecore_chain *p_tx_chain = &p_ll2->tx_queue.txq_chain;
1663 	u16 prod_idx = ecore_chain_get_prod_idx(p_tx_chain);
1664 	struct core_tx_bd *start_bd = OSAL_NULL;
1665 	enum core_roce_flavor_type roce_flavor;
1666 	enum core_tx_dest tx_dest;
1667 	u16 bd_data = 0, frag_idx;
1668 
1669 	roce_flavor = (pkt->ecore_roce_flavor == ECORE_LL2_ROCE) ?
1670 		CORE_ROCE : CORE_RROCE;
1671 
1672 	switch (pkt->tx_dest) {
1673 	case ECORE_LL2_TX_DEST_NW:
1674 		tx_dest = CORE_TX_DEST_NW;
1675 		break;
1676 	case ECORE_LL2_TX_DEST_LB:
1677 		tx_dest = CORE_TX_DEST_LB;
1678 		break;
1679 	case ECORE_LL2_TX_DEST_DROP:
1680 		tx_dest = CORE_TX_DEST_DROP;
1681 		break;
1682 	default:
1683 		tx_dest = CORE_TX_DEST_LB;
1684 		break;
1685 	}
1686 
1687 	start_bd = (struct core_tx_bd*)ecore_chain_produce(p_tx_chain);
1688 
1689 	if (ECORE_IS_IWARP_PERSONALITY(p_hwfn) &&
1690 	    (p_ll2->input.conn_type == ECORE_LL2_TYPE_OOO)) {
1691 		start_bd->nw_vlan_or_lb_echo =
1692 			OSAL_CPU_TO_LE16(IWARP_LL2_IN_ORDER_TX_QUEUE);
1693 	} else {
1694 		start_bd->nw_vlan_or_lb_echo = OSAL_CPU_TO_LE16(pkt->vlan);
1695 		if (OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC, &p_hwfn->p_dev->mf_bits) &&
1696 		    (p_ll2->input.conn_type == ECORE_LL2_TYPE_FCOE))
1697 			pkt->remove_stag = true;
1698 	}
1699 
1700 	SET_FIELD(start_bd->bitfield1, CORE_TX_BD_L4_HDR_OFFSET_W,
1701 		  OSAL_CPU_TO_LE16(pkt->l4_hdr_offset_w));
1702 	SET_FIELD(start_bd->bitfield1, CORE_TX_BD_TX_DST, tx_dest);
1703 	bd_data |= pkt->bd_flags;
1704 	SET_FIELD(bd_data, CORE_TX_BD_DATA_START_BD, 0x1);
1705 	SET_FIELD(bd_data, CORE_TX_BD_DATA_NBDS, pkt->num_of_bds);
1706 	SET_FIELD(bd_data, CORE_TX_BD_DATA_ROCE_FLAV, roce_flavor);
1707 	SET_FIELD(bd_data, CORE_TX_BD_DATA_IP_CSUM, !!(pkt->enable_ip_cksum));
1708 	SET_FIELD(bd_data, CORE_TX_BD_DATA_L4_CSUM, !!(pkt->enable_l4_cksum));
1709 	SET_FIELD(bd_data, CORE_TX_BD_DATA_IP_LEN, !!(pkt->calc_ip_len));
1710 	SET_FIELD(bd_data, CORE_TX_BD_DATA_DISABLE_STAG_INSERTION,
1711 		  !!(pkt->remove_stag));
1712 
1713 	start_bd->bd_data.as_bitfield = OSAL_CPU_TO_LE16(bd_data);
1714 	DMA_REGPAIR_LE(start_bd->addr, pkt->first_frag);
1715 	start_bd->nbytes = OSAL_CPU_TO_LE16(pkt->first_frag_len);
1716 
1717 	DP_VERBOSE(p_hwfn, (ECORE_MSG_TX_QUEUED | ECORE_MSG_LL2),
1718 		   "LL2 [q 0x%02x cid 0x%08x type 0x%08x] Tx Producer at [0x%04x] - set with a %04x bytes %02x BDs buffer at %08x:%08x\n",
1719 		   p_ll2->queue_id, p_ll2->cid, p_ll2->input.conn_type,
1720 		   prod_idx, pkt->first_frag_len, pkt->num_of_bds,
1721 		   OSAL_LE32_TO_CPU(start_bd->addr.hi),
1722 		   OSAL_LE32_TO_CPU(start_bd->addr.lo));
1723 
1724 	if (p_ll2->tx_queue.cur_send_frag_num == pkt->num_of_bds)
1725 		return;
1726 
1727 	/* Need to provide the packet with additional BDs for frags */
1728 	for (frag_idx = p_ll2->tx_queue.cur_send_frag_num;
1729 	     frag_idx < pkt->num_of_bds; frag_idx++) {
1730 		struct core_tx_bd **p_bd = &p_curp->bds_set[frag_idx].txq_bd;
1731 
1732 		*p_bd = (struct core_tx_bd *)ecore_chain_produce(p_tx_chain);
1733 		(*p_bd)->bd_data.as_bitfield = 0;
1734 		(*p_bd)->bitfield1 = 0;
1735 		p_curp->bds_set[frag_idx].tx_frag = 0;
1736 		p_curp->bds_set[frag_idx].frag_len = 0;
1737 	}
1738 }
1739 
1740 /* This should be called while the Txq spinlock is being held */
ecore_ll2_tx_packet_notify(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)1741 static void ecore_ll2_tx_packet_notify(struct ecore_hwfn *p_hwfn,
1742 				       struct ecore_ll2_info *p_ll2_conn)
1743 {
1744 	bool b_notify = p_ll2_conn->tx_queue.cur_send_packet->notify_fw;
1745 	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
1746 	struct ecore_ll2_tx_packet *p_pkt = OSAL_NULL;
1747 	u16 bd_prod;
1748 
1749 	/* If there are missing BDs, don't do anything now */
1750 	if (p_ll2_conn->tx_queue.cur_send_frag_num !=
1751 	    p_ll2_conn->tx_queue.cur_send_packet->bd_used)
1752 		return;
1753 
1754 	/* Push the current packet to the list and clean after it */
1755 	OSAL_LIST_PUSH_TAIL(&p_ll2_conn->tx_queue.cur_send_packet->list_entry,
1756 			    &p_ll2_conn->tx_queue.sending_descq);
1757 	p_ll2_conn->tx_queue.cur_send_packet = OSAL_NULL;
1758 	p_ll2_conn->tx_queue.cur_send_frag_num = 0;
1759 
1760 	/* Notify FW of packet only if requested to */
1761 	if (!b_notify)
1762 		return;
1763 
1764 	bd_prod = ecore_chain_get_prod_idx(&p_ll2_conn->tx_queue.txq_chain);
1765 
1766 	while (!OSAL_LIST_IS_EMPTY(&p_tx->sending_descq)) {
1767 		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_tx->sending_descq,
1768 					      struct ecore_ll2_tx_packet,
1769 					      list_entry);
1770 		if (p_pkt == OSAL_NULL)
1771 			break;
1772 #if defined(_NTDDK_)
1773 #pragma warning(suppress : 6011 28182)
1774 #endif
1775 		OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
1776 				       &p_tx->sending_descq);
1777 		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry, &p_tx->active_descq);
1778 	}
1779 
1780 	p_tx->db_msg.spq_prod = OSAL_CPU_TO_LE16(bd_prod);
1781 
1782 	/* Make sure the BDs data is updated before ringing the doorbell */
1783 	OSAL_WMB(p_hwfn->p_dev);
1784 
1785 	//DIRECT_REG_WR(p_hwfn, p_tx->doorbell_addr, *((u32 *)&p_tx->db_msg));
1786 	DIRECT_REG_WR_DB(p_hwfn, p_tx->doorbell_addr, *((u32 *)&p_tx->db_msg));
1787 
1788 	DP_VERBOSE(p_hwfn, (ECORE_MSG_TX_QUEUED | ECORE_MSG_LL2),
1789 		   "LL2 [q 0x%02x cid 0x%08x type 0x%08x] Doorbelled [producer 0x%04x]\n",
1790 		   p_ll2_conn->queue_id, p_ll2_conn->cid,
1791 		   p_ll2_conn->input.conn_type,
1792 		   p_tx->db_msg.spq_prod);
1793 }
1794 
ecore_ll2_prepare_tx_packet(void * cxt,u8 connection_handle,struct ecore_ll2_tx_pkt_info * pkt,bool notify_fw)1795 enum _ecore_status_t ecore_ll2_prepare_tx_packet(
1796 		void *cxt,
1797 		u8 connection_handle,
1798 		struct ecore_ll2_tx_pkt_info *pkt,
1799 		bool notify_fw)
1800 {
1801 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1802 	struct ecore_ll2_tx_packet *p_curp = OSAL_NULL;
1803 	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1804 	struct ecore_ll2_tx_queue *p_tx;
1805 	struct ecore_chain *p_tx_chain;
1806 	unsigned long flags;
1807 	enum _ecore_status_t rc = ECORE_SUCCESS;
1808 
1809 	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1810 	if (p_ll2_conn == OSAL_NULL)
1811 		return ECORE_INVAL;
1812 	p_tx = &p_ll2_conn->tx_queue;
1813 	p_tx_chain = &p_tx->txq_chain;
1814 
1815 	if (pkt->num_of_bds > p_ll2_conn->input.tx_max_bds_per_packet)
1816 		return ECORE_IO; /* coalescing is requireed */
1817 
1818 	OSAL_SPIN_LOCK_IRQSAVE(&p_tx->lock, flags);
1819 	if (p_tx->cur_send_packet) {
1820 		rc = ECORE_EXISTS;
1821 		goto out;
1822 	}
1823 
1824 	/* Get entry, but only if we have tx elements for it */
1825 	if (!OSAL_LIST_IS_EMPTY(&p_tx->free_descq))
1826 		p_curp = OSAL_LIST_FIRST_ENTRY(&p_tx->free_descq,
1827 					       struct ecore_ll2_tx_packet,
1828 					       list_entry);
1829 	if (p_curp && ecore_chain_get_elem_left(p_tx_chain) < pkt->num_of_bds)
1830 		p_curp = OSAL_NULL;
1831 
1832 	if (!p_curp) {
1833 		rc = ECORE_BUSY;
1834 		goto out;
1835 	}
1836 
1837 	/* Prepare packet and BD, and perhaps send a doorbell to FW */
1838 	ecore_ll2_prepare_tx_packet_set(p_tx, p_curp, pkt, notify_fw);
1839 
1840 	ecore_ll2_prepare_tx_packet_set_bd(p_hwfn, p_ll2_conn, p_curp,
1841 					   pkt);
1842 
1843 	ecore_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
1844 
1845 out:
1846 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_tx->lock, flags);
1847 	return rc;
1848 }
1849 
ecore_ll2_set_fragment_of_tx_packet(void * cxt,u8 connection_handle,dma_addr_t addr,u16 nbytes)1850 enum _ecore_status_t ecore_ll2_set_fragment_of_tx_packet(void *cxt,
1851 							 u8 connection_handle,
1852 							 dma_addr_t addr,
1853 							 u16 nbytes)
1854 {
1855 	struct ecore_ll2_tx_packet *p_cur_send_packet = OSAL_NULL;
1856 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1857 	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1858 	u16 cur_send_frag_num = 0;
1859 	struct core_tx_bd *p_bd;
1860 	unsigned long flags;
1861 
1862 	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1863 	if (p_ll2_conn == OSAL_NULL)
1864 		return ECORE_INVAL;
1865 
1866 	if (!p_ll2_conn->tx_queue.cur_send_packet)
1867 		return ECORE_INVAL;
1868 
1869 	p_cur_send_packet = p_ll2_conn->tx_queue.cur_send_packet;
1870 	cur_send_frag_num = p_ll2_conn->tx_queue.cur_send_frag_num;
1871 
1872 	if (cur_send_frag_num >= p_cur_send_packet->bd_used)
1873 		return ECORE_INVAL;
1874 
1875 	/* Fill the BD information, and possibly notify FW */
1876 	p_bd = p_cur_send_packet->bds_set[cur_send_frag_num].txq_bd;
1877 	DMA_REGPAIR_LE(p_bd->addr, addr);
1878 	p_bd->nbytes = OSAL_CPU_TO_LE16(nbytes);
1879 	p_cur_send_packet->bds_set[cur_send_frag_num].tx_frag = addr;
1880 	p_cur_send_packet->bds_set[cur_send_frag_num].frag_len = nbytes;
1881 
1882 	p_ll2_conn->tx_queue.cur_send_frag_num++;
1883 
1884 	OSAL_SPIN_LOCK_IRQSAVE(&p_ll2_conn->tx_queue.lock, flags);
1885 	ecore_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
1886 	OSAL_SPIN_UNLOCK_IRQSAVE(&p_ll2_conn->tx_queue.lock, flags);
1887 
1888 	return ECORE_SUCCESS;
1889 }
1890 
ecore_ll2_terminate_connection(void * cxt,u8 connection_handle)1891 enum _ecore_status_t ecore_ll2_terminate_connection(void *cxt,
1892 						    u8 connection_handle)
1893 {
1894 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1895 	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1896 	enum _ecore_status_t rc = ECORE_NOTIMPL;
1897 	struct ecore_ptt *p_ptt;
1898 
1899 	p_ptt = ecore_ptt_acquire(p_hwfn);
1900 	if (!p_ptt)
1901 		return ECORE_AGAIN;
1902 
1903 	p_ll2_conn = ecore_ll2_handle_sanity_lock(p_hwfn, connection_handle);
1904 	if (p_ll2_conn == OSAL_NULL) {
1905 		rc = ECORE_INVAL;
1906 		goto out;
1907 	}
1908 
1909 	/* Stop Tx & Rx of connection, if needed */
1910 	if (ECORE_LL2_TX_REGISTERED(p_ll2_conn)) {
1911 		rc = ecore_sp_ll2_tx_queue_stop(p_hwfn, p_ll2_conn);
1912 		if (rc != ECORE_SUCCESS)
1913 			goto out;
1914 		ecore_ll2_txq_flush(p_hwfn, connection_handle);
1915 	}
1916 
1917 	if (ECORE_LL2_RX_REGISTERED(p_ll2_conn)) {
1918 		rc = ecore_sp_ll2_rx_queue_stop(p_hwfn, p_ll2_conn);
1919 		if (rc)
1920 			goto out;
1921 		ecore_ll2_rxq_flush(p_hwfn, connection_handle);
1922 	}
1923 
1924 	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_OOO)
1925 		ecore_ooo_release_all_isles(p_hwfn->p_ooo_info);
1926 
1927 	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_FCOE) {
1928 		if (!OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC,
1929 				   &p_hwfn->p_dev->mf_bits))
1930 			ecore_llh_remove_protocol_filter(p_hwfn->p_dev, 0,
1931 							 ECORE_LLH_FILTER_ETHERTYPE,
1932 							 0x8906, 0);
1933 		ecore_llh_remove_protocol_filter(p_hwfn->p_dev, 0,
1934 						 ECORE_LLH_FILTER_ETHERTYPE,
1935 						 0x8914, 0);
1936 	}
1937 
1938 out:
1939 	ecore_ptt_release(p_hwfn, p_ptt);
1940 
1941 	return rc;
1942 }
1943 
ecore_ll2_release_connection_ooo(struct ecore_hwfn * p_hwfn,struct ecore_ll2_info * p_ll2_conn)1944 static void ecore_ll2_release_connection_ooo(struct ecore_hwfn *p_hwfn,
1945 					     struct ecore_ll2_info *p_ll2_conn)
1946 {
1947 	struct ecore_ooo_buffer *p_buffer;
1948 
1949 	if (p_ll2_conn->input.conn_type != ECORE_LL2_TYPE_OOO)
1950 		return;
1951 
1952 	ecore_ooo_release_all_isles(p_hwfn->p_ooo_info);
1953 	while ((p_buffer = ecore_ooo_get_free_buffer(p_hwfn->p_ooo_info))) {
1954 		OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
1955 				       p_buffer->rx_buffer_virt_addr,
1956 				       p_buffer->rx_buffer_phys_addr,
1957 				       p_buffer->rx_buffer_size);
1958 		OSAL_FREE(p_hwfn->p_dev, p_buffer);
1959 	}
1960 }
1961 
ecore_ll2_release_connection(void * cxt,u8 connection_handle)1962 void ecore_ll2_release_connection(void *cxt,
1963 				  u8 connection_handle)
1964 {
1965 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1966 	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1967 
1968 	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1969 	if (p_ll2_conn == OSAL_NULL)
1970 		return;
1971 
1972 	if (ECORE_LL2_RX_REGISTERED(p_ll2_conn)) {
1973 		p_ll2_conn->rx_queue.b_cb_registred = false;
1974 		ecore_int_unregister_cb(p_hwfn,
1975 					p_ll2_conn->rx_queue.rx_sb_index);
1976 	}
1977 
1978 	if (ECORE_LL2_TX_REGISTERED(p_ll2_conn)) {
1979 		p_ll2_conn->tx_queue.b_cb_registred = false;
1980 		ecore_int_unregister_cb(p_hwfn,
1981 					p_ll2_conn->tx_queue.tx_sb_index);
1982 	}
1983 
1984 	OSAL_FREE(p_hwfn->p_dev, p_ll2_conn->tx_queue.descq_array);
1985 	ecore_chain_free(p_hwfn->p_dev, &p_ll2_conn->tx_queue.txq_chain);
1986 
1987 	OSAL_FREE(p_hwfn->p_dev, p_ll2_conn->rx_queue.descq_array);
1988 	ecore_chain_free(p_hwfn->p_dev, &p_ll2_conn->rx_queue.rxq_chain);
1989 	ecore_chain_free(p_hwfn->p_dev, &p_ll2_conn->rx_queue.rcq_chain);
1990 
1991 	ecore_cxt_release_cid(p_hwfn, p_ll2_conn->cid);
1992 
1993 	ecore_ll2_release_connection_ooo(p_hwfn, p_ll2_conn);
1994 
1995 	OSAL_MUTEX_ACQUIRE(&p_ll2_conn->mutex);
1996 	p_ll2_conn->b_active = false;
1997 	OSAL_MUTEX_RELEASE(&p_ll2_conn->mutex);
1998 }
1999 
2000 /* ECORE LL2: internal functions */
2001 
ecore_ll2_alloc(struct ecore_hwfn * p_hwfn)2002 enum _ecore_status_t ecore_ll2_alloc(struct ecore_hwfn *p_hwfn)
2003 {
2004 	struct ecore_ll2_info *p_ll2_info;
2005 	u8 i;
2006 
2007 	/* Allocate LL2's set struct */
2008 	p_ll2_info = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
2009 				 sizeof(struct ecore_ll2_info) *
2010 				 ECORE_MAX_NUM_OF_LL2_CONNECTIONS);
2011 	if (!p_ll2_info) {
2012 		DP_NOTICE(p_hwfn, false,
2013 			  "Failed to allocate `struct ecore_ll2'\n");
2014 		return ECORE_NOMEM;
2015 	}
2016 
2017 	p_hwfn->p_ll2_info = p_ll2_info;
2018 
2019 	for (i = 0; i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS; i++) {
2020 #ifdef CONFIG_ECORE_LOCK_ALLOC
2021 		if (OSAL_MUTEX_ALLOC(p_hwfn, &p_ll2_info[i].mutex))
2022 			goto handle_err;
2023 		if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_ll2_info[i].rx_queue.lock))
2024 			goto handle_err;
2025 		if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_ll2_info[i].tx_queue.lock))
2026 			goto handle_err;
2027 #endif
2028 		p_ll2_info[i].my_id = i;
2029 	}
2030 
2031 	return ECORE_SUCCESS;
2032 #ifdef CONFIG_ECORE_LOCK_ALLOC
2033 handle_err:
2034 	ecore_ll2_free(p_hwfn);
2035 	return ECORE_NOMEM;
2036 #endif
2037 }
2038 
ecore_ll2_setup(struct ecore_hwfn * p_hwfn)2039 void ecore_ll2_setup(struct ecore_hwfn *p_hwfn)
2040 {
2041 	int  i;
2042 
2043 	for (i = 0; i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS; i++)
2044 		OSAL_MUTEX_INIT(&p_hwfn->p_ll2_info[i].mutex);
2045 }
2046 
ecore_ll2_free(struct ecore_hwfn * p_hwfn)2047 void ecore_ll2_free(struct ecore_hwfn *p_hwfn)
2048 {
2049 #ifdef CONFIG_ECORE_LOCK_ALLOC
2050 	int i;
2051 #endif
2052 	if (!p_hwfn->p_ll2_info)
2053 		return;
2054 
2055 #ifdef CONFIG_ECORE_LOCK_ALLOC
2056 	for (i = 0; i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS; i++) {
2057 		OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->p_ll2_info[i].rx_queue.lock);
2058 		OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->p_ll2_info[i].tx_queue.lock);
2059 		OSAL_MUTEX_DEALLOC(&p_hwfn->p_ll2_info[i].mutex);
2060 	}
2061 #endif
2062 	OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_ll2_info);
2063 	p_hwfn->p_ll2_info = OSAL_NULL;
2064 }
2065 
_ecore_ll2_get_port_stats(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,struct ecore_ll2_stats * p_stats)2066 static void _ecore_ll2_get_port_stats(struct ecore_hwfn *p_hwfn,
2067 				      struct ecore_ptt *p_ptt,
2068 				      struct ecore_ll2_stats *p_stats)
2069 {
2070 	struct core_ll2_port_stats port_stats;
2071 
2072 	OSAL_MEMSET(&port_stats, 0, sizeof(port_stats));
2073 	ecore_memcpy_from(p_hwfn, p_ptt, &port_stats,
2074 			  BAR0_MAP_REG_TSDM_RAM +
2075 			  TSTORM_LL2_PORT_STAT_OFFSET(MFW_PORT(p_hwfn)),
2076 			  sizeof(port_stats));
2077 
2078 	p_stats->gsi_invalid_hdr +=
2079 		HILO_64_REGPAIR(port_stats.gsi_invalid_hdr);
2080 	p_stats->gsi_invalid_pkt_length +=
2081 		HILO_64_REGPAIR(port_stats.gsi_invalid_pkt_length);
2082 	p_stats->gsi_unsupported_pkt_typ +=
2083 		HILO_64_REGPAIR(port_stats.gsi_unsupported_pkt_typ);
2084 	p_stats->gsi_crcchksm_error +=
2085 		HILO_64_REGPAIR(port_stats.gsi_crcchksm_error);
2086 }
2087 
_ecore_ll2_get_tstats(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,struct ecore_ll2_info * p_ll2_conn,struct ecore_ll2_stats * p_stats)2088 static void _ecore_ll2_get_tstats(struct ecore_hwfn *p_hwfn,
2089 				  struct ecore_ptt *p_ptt,
2090 				  struct ecore_ll2_info *p_ll2_conn,
2091 				  struct ecore_ll2_stats *p_stats)
2092 {
2093 	struct core_ll2_tstorm_per_queue_stat tstats;
2094 	u8 qid = p_ll2_conn->queue_id;
2095 	u32 tstats_addr;
2096 
2097 	OSAL_MEMSET(&tstats, 0, sizeof(tstats));
2098 	tstats_addr = BAR0_MAP_REG_TSDM_RAM +
2099 		      CORE_LL2_TSTORM_PER_QUEUE_STAT_OFFSET(qid);
2100 	ecore_memcpy_from(p_hwfn, p_ptt, &tstats,
2101 			  tstats_addr,
2102 			  sizeof(tstats));
2103 
2104 	p_stats->packet_too_big_discard +=
2105 		HILO_64_REGPAIR(tstats.packet_too_big_discard);
2106 	p_stats->no_buff_discard +=
2107 		HILO_64_REGPAIR(tstats.no_buff_discard);
2108 }
2109 
_ecore_ll2_get_ustats(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,struct ecore_ll2_info * p_ll2_conn,struct ecore_ll2_stats * p_stats)2110 static void _ecore_ll2_get_ustats(struct ecore_hwfn *p_hwfn,
2111 				  struct ecore_ptt *p_ptt,
2112 				  struct ecore_ll2_info *p_ll2_conn,
2113 				  struct ecore_ll2_stats *p_stats)
2114 {
2115 	struct core_ll2_ustorm_per_queue_stat ustats;
2116 	u8 qid = p_ll2_conn->queue_id;
2117 	u32 ustats_addr;
2118 
2119 	OSAL_MEMSET(&ustats, 0, sizeof(ustats));
2120 	ustats_addr = BAR0_MAP_REG_USDM_RAM +
2121 		      CORE_LL2_USTORM_PER_QUEUE_STAT_OFFSET(qid);
2122 	ecore_memcpy_from(p_hwfn, p_ptt, &ustats,
2123 			  ustats_addr,
2124 			  sizeof(ustats));
2125 
2126 	p_stats->rcv_ucast_bytes += HILO_64_REGPAIR(ustats.rcv_ucast_bytes);
2127 	p_stats->rcv_mcast_bytes += HILO_64_REGPAIR(ustats.rcv_mcast_bytes);
2128 	p_stats->rcv_bcast_bytes += HILO_64_REGPAIR(ustats.rcv_bcast_bytes);
2129 	p_stats->rcv_ucast_pkts += HILO_64_REGPAIR(ustats.rcv_ucast_pkts);
2130 	p_stats->rcv_mcast_pkts += HILO_64_REGPAIR(ustats.rcv_mcast_pkts);
2131 	p_stats->rcv_bcast_pkts += HILO_64_REGPAIR(ustats.rcv_bcast_pkts);
2132 }
2133 
_ecore_ll2_get_pstats(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,struct ecore_ll2_info * p_ll2_conn,struct ecore_ll2_stats * p_stats)2134 static void _ecore_ll2_get_pstats(struct ecore_hwfn *p_hwfn,
2135 				  struct ecore_ptt *p_ptt,
2136 				  struct ecore_ll2_info *p_ll2_conn,
2137 				  struct ecore_ll2_stats *p_stats)
2138 {
2139 	struct core_ll2_pstorm_per_queue_stat pstats;
2140 	u8 stats_id = p_ll2_conn->tx_stats_id;
2141 	u32 pstats_addr;
2142 
2143 	OSAL_MEMSET(&pstats, 0, sizeof(pstats));
2144 	pstats_addr = BAR0_MAP_REG_PSDM_RAM +
2145 		      CORE_LL2_PSTORM_PER_QUEUE_STAT_OFFSET(stats_id);
2146 	ecore_memcpy_from(p_hwfn, p_ptt, &pstats,
2147 			  pstats_addr,
2148 			  sizeof(pstats));
2149 
2150 	p_stats->sent_ucast_bytes += HILO_64_REGPAIR(pstats.sent_ucast_bytes);
2151 	p_stats->sent_mcast_bytes += HILO_64_REGPAIR(pstats.sent_mcast_bytes);
2152 	p_stats->sent_bcast_bytes += HILO_64_REGPAIR(pstats.sent_bcast_bytes);
2153 	p_stats->sent_ucast_pkts += HILO_64_REGPAIR(pstats.sent_ucast_pkts);
2154 	p_stats->sent_mcast_pkts += HILO_64_REGPAIR(pstats.sent_mcast_pkts);
2155 	p_stats->sent_bcast_pkts += HILO_64_REGPAIR(pstats.sent_bcast_pkts);
2156 }
2157 
__ecore_ll2_get_stats(void * cxt,u8 connection_handle,struct ecore_ll2_stats * p_stats)2158 enum _ecore_status_t __ecore_ll2_get_stats(void *cxt,
2159 					   u8 connection_handle,
2160 					   struct ecore_ll2_stats *p_stats)
2161 {
2162 	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
2163 	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
2164 	struct ecore_ptt *p_ptt;
2165 
2166 	if ((connection_handle >= ECORE_MAX_NUM_OF_LL2_CONNECTIONS) ||
2167 	    !p_hwfn->p_ll2_info) {
2168 		return ECORE_INVAL;
2169 	}
2170 
2171 	p_ll2_conn = &p_hwfn->p_ll2_info[connection_handle];
2172 
2173 	p_ptt = ecore_ptt_acquire(p_hwfn);
2174 	if (!p_ptt) {
2175 		DP_ERR(p_hwfn, "Failed to acquire ptt\n");
2176 		return ECORE_INVAL;
2177 	}
2178 
2179 	if (p_ll2_conn->input.gsi_enable)
2180 		_ecore_ll2_get_port_stats(p_hwfn, p_ptt, p_stats);
2181 
2182 	_ecore_ll2_get_tstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2183 
2184 	_ecore_ll2_get_ustats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2185 
2186 	if (p_ll2_conn->tx_stats_en)
2187 		_ecore_ll2_get_pstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2188 
2189 	ecore_ptt_release(p_hwfn, p_ptt);
2190 
2191 	return ECORE_SUCCESS;
2192 }
2193 
ecore_ll2_get_stats(void * cxt,u8 connection_handle,struct ecore_ll2_stats * p_stats)2194 enum _ecore_status_t ecore_ll2_get_stats(void *cxt,
2195 					 u8 connection_handle,
2196 					 struct ecore_ll2_stats	*p_stats)
2197 {
2198 	OSAL_MEMSET(p_stats, 0, sizeof(*p_stats));
2199 
2200 	return __ecore_ll2_get_stats(cxt, connection_handle, p_stats);
2201 }
2202 
2203 /**/
2204 
2205 #ifdef _NTDDK_
2206 #pragma warning(pop)
2207 #endif
2208