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