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
60static struct ecore_ll2_info *
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
92static struct ecore_ll2_info *
93ecore_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
100static struct ecore_ll2_info *
101ecore_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
108static struct ecore_ll2_info *
109ecore_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? */
118enum _ecore_status_t
119ecore_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
150static 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
208static enum _ecore_status_t
209ecore_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;
283out:
284	OSAL_SPIN_UNLOCK_IRQSAVE(&p_tx->lock, flags);
285	return rc;
286}
287
288static 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
308static 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
330static enum _ecore_status_t
331ecore_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
365static enum _ecore_status_t
366ecore_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
419static 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
463static 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
515static bool
516ecore_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
536static enum _ecore_status_t
537ecore_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 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				ecore_ooo_put_ready_buffer(p_hwfn->p_ooo_info,
671							   p_buffer, true);
672				break;
673			}
674		} else {
675			DP_NOTICE(p_hwfn, true,
676				  "Unexpected event (%d) TX OOO completion\n",
677				  iscsi_ooo->ooo_opcode);
678		}
679	}
680
681	return ECORE_SUCCESS;
682}
683
684static void
685ecore_ooo_submit_tx_buffers(struct ecore_hwfn *p_hwfn,
686			    struct ecore_ll2_info *p_ll2_conn)
687{
688	struct ecore_ll2_tx_pkt_info tx_pkt;
689	struct ecore_ooo_buffer *p_buffer;
690	dma_addr_t first_frag;
691	u16 l4_hdr_offset_w;
692	u8 bd_flags;
693	enum _ecore_status_t rc;
694
695	/* Submit Tx buffers here */
696	while ((p_buffer = ecore_ooo_get_ready_buffer(p_hwfn->p_ooo_info))) {
697		l4_hdr_offset_w = 0;
698		bd_flags = 0;
699
700		first_frag = p_buffer->rx_buffer_phys_addr +
701			     p_buffer->placement_offset;
702		SET_FIELD(bd_flags, CORE_TX_BD_DATA_FORCE_VLAN_MODE, 1);
703		SET_FIELD(bd_flags, CORE_TX_BD_DATA_L4_PROTOCOL, 1);
704
705		OSAL_MEM_ZERO(&tx_pkt, sizeof(tx_pkt));
706		tx_pkt.num_of_bds = 1;
707		tx_pkt.vlan = p_buffer->vlan;
708		tx_pkt.bd_flags = bd_flags;
709		tx_pkt.l4_hdr_offset_w = l4_hdr_offset_w;
710		tx_pkt.tx_dest = (enum ecore_ll2_tx_dest)p_ll2_conn->tx_dest;
711		tx_pkt.first_frag = first_frag;
712		tx_pkt.first_frag_len = p_buffer->packet_length;
713		tx_pkt.cookie = p_buffer;
714
715		rc = ecore_ll2_prepare_tx_packet(p_hwfn, p_ll2_conn->my_id,
716						 &tx_pkt, true);
717		if (rc != ECORE_SUCCESS) {
718			ecore_ooo_put_ready_buffer(p_hwfn->p_ooo_info,
719						   p_buffer, false);
720			break;
721		}
722	}
723}
724
725static void
726ecore_ooo_submit_rx_buffers(struct ecore_hwfn *p_hwfn,
727			    struct ecore_ll2_info *p_ll2_conn)
728{
729	struct ecore_ooo_buffer *p_buffer;
730	enum _ecore_status_t rc;
731
732	while ((p_buffer = ecore_ooo_get_free_buffer(p_hwfn->p_ooo_info))) {
733		rc = ecore_ll2_post_rx_buffer(p_hwfn,
734					    p_ll2_conn->my_id,
735					    p_buffer->rx_buffer_phys_addr,
736					    0, p_buffer, true);
737		if (rc != ECORE_SUCCESS) {
738			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
739			break;
740		}
741	}
742}
743
744static enum _ecore_status_t
745ecore_ll2_lb_rxq_completion(struct ecore_hwfn *p_hwfn,
746			    void *p_cookie)
747{
748	struct ecore_ll2_info *p_ll2_conn = (struct ecore_ll2_info *)p_cookie;
749	enum _ecore_status_t rc;
750
751	rc = ecore_ll2_lb_rxq_handler(p_hwfn, p_ll2_conn);
752	if (rc != ECORE_SUCCESS)
753		return rc;
754
755	ecore_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
756	ecore_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
757
758	return 0;
759}
760
761static enum _ecore_status_t
762ecore_ll2_lb_txq_completion(struct ecore_hwfn *p_hwfn,
763			    void *p_cookie)
764{
765	struct ecore_ll2_info *p_ll2_conn = (struct ecore_ll2_info *)p_cookie;
766	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
767	struct ecore_ll2_tx_packet *p_pkt = OSAL_NULL;
768	struct ecore_ooo_buffer	*p_buffer;
769	bool b_dont_submit_rx = false;
770	u16 new_idx = 0, num_bds = 0;
771	enum _ecore_status_t rc;
772
773	new_idx = OSAL_LE16_TO_CPU(*p_tx->p_fw_cons);
774	num_bds = ((s16)new_idx - (s16)p_tx->bds_idx);
775
776	if (!num_bds)
777		return ECORE_SUCCESS;
778
779	while (num_bds) {
780		if (OSAL_LIST_IS_EMPTY(&p_tx->active_descq))
781			return ECORE_INVAL;
782
783		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_tx->active_descq,
784					      struct ecore_ll2_tx_packet,
785					      list_entry);
786		if (!p_pkt)
787			return ECORE_INVAL;
788
789		if (p_pkt->bd_used != 1) {
790			DP_NOTICE(p_hwfn, true,
791				  "Unexpectedly many BDs(%d) in TX OOO completion\n",
792				  p_pkt->bd_used);
793			return ECORE_INVAL;
794		}
795
796		OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
797				       &p_tx->active_descq);
798
799		num_bds--;
800		p_tx->bds_idx++;
801		ecore_chain_consume(&p_tx->txq_chain);
802
803		p_buffer = (struct ecore_ooo_buffer *)p_pkt->cookie;
804		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
805				    &p_tx->free_descq);
806
807		if (b_dont_submit_rx) {
808			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
809			continue;
810		}
811
812		rc = ecore_ll2_post_rx_buffer(p_hwfn, p_ll2_conn->my_id,
813					      p_buffer->rx_buffer_phys_addr, 0,
814					      p_buffer, true);
815		if (rc != ECORE_SUCCESS) {
816			ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buffer);
817			b_dont_submit_rx = true;
818		}
819	}
820
821	ecore_ooo_submit_tx_buffers(p_hwfn, p_ll2_conn);
822
823	return ECORE_SUCCESS;
824}
825
826static enum _ecore_status_t ecore_sp_ll2_rx_queue_start(struct ecore_hwfn *p_hwfn,
827							struct ecore_ll2_info *p_ll2_conn,
828							u8 action_on_error)
829{
830	enum ecore_ll2_conn_type conn_type = p_ll2_conn->input.conn_type;
831	struct ecore_ll2_rx_queue *p_rx = &p_ll2_conn->rx_queue;
832	struct core_rx_start_ramrod_data *p_ramrod = OSAL_NULL;
833	struct ecore_spq_entry *p_ent = OSAL_NULL;
834	struct ecore_sp_init_data init_data;
835	u16 cqe_pbl_size;
836	enum _ecore_status_t rc	= ECORE_SUCCESS;
837
838	/* Get SPQ entry */
839	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
840	init_data.cid = p_ll2_conn->cid;
841	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
842	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
843
844	rc = ecore_sp_init_request(p_hwfn, &p_ent,
845				   CORE_RAMROD_RX_QUEUE_START,
846				   PROTOCOLID_CORE, &init_data);
847	if (rc != ECORE_SUCCESS)
848		return rc;
849
850	p_ramrod = &p_ent->ramrod.core_rx_queue_start;
851
852	p_ramrod->sb_id = OSAL_CPU_TO_LE16(ecore_int_get_sp_sb_id(p_hwfn));
853	p_ramrod->sb_index = p_rx->rx_sb_index;
854	p_ramrod->complete_event_flg = 1;
855
856	p_ramrod->mtu = OSAL_CPU_TO_LE16(p_ll2_conn->input.mtu);
857	DMA_REGPAIR_LE(p_ramrod->bd_base,
858		       p_rx->rxq_chain.p_phys_addr);
859	cqe_pbl_size = (u16)ecore_chain_get_page_cnt(&p_rx->rcq_chain);
860	p_ramrod->num_of_pbl_pages = OSAL_CPU_TO_LE16(cqe_pbl_size);
861	DMA_REGPAIR_LE(p_ramrod->cqe_pbl_addr,
862		       ecore_chain_get_pbl_phys(&p_rx->rcq_chain));
863
864	p_ramrod->drop_ttl0_flg = p_ll2_conn->input.rx_drop_ttl0_flg;
865	p_ramrod->inner_vlan_stripping_en =
866		p_ll2_conn->input.rx_vlan_removal_en;
867
868	if (OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC, &p_hwfn->p_dev->mf_bits) &&
869	    (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_FCOE))
870		p_ramrod->report_outer_vlan = 1;
871	p_ramrod->queue_id = p_ll2_conn->queue_id;
872	p_ramrod->main_func_queue = p_ll2_conn->main_func_queue;
873
874	if (OSAL_TEST_BIT(ECORE_MF_LL2_NON_UNICAST,
875			  &p_hwfn->p_dev->mf_bits) &&
876	    p_ramrod->main_func_queue &&
877	    ((conn_type != ECORE_LL2_TYPE_ROCE) &&
878	     (conn_type != ECORE_LL2_TYPE_IWARP))) {
879		p_ramrod->mf_si_bcast_accept_all = 1;
880		p_ramrod->mf_si_mcast_accept_all = 1;
881	} else {
882		p_ramrod->mf_si_bcast_accept_all = 0;
883		p_ramrod->mf_si_mcast_accept_all = 0;
884	}
885
886	p_ramrod->action_on_error.error_type = action_on_error;
887	p_ramrod->gsi_offload_flag = p_ll2_conn->input.gsi_enable;
888	return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
889}
890
891static enum _ecore_status_t ecore_sp_ll2_tx_queue_start(struct ecore_hwfn *p_hwfn,
892							struct ecore_ll2_info *p_ll2_conn)
893{
894	enum ecore_ll2_conn_type conn_type = p_ll2_conn->input.conn_type;
895	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
896	struct core_tx_start_ramrod_data *p_ramrod = OSAL_NULL;
897	struct ecore_spq_entry *p_ent = OSAL_NULL;
898	struct ecore_sp_init_data init_data;
899	u16 pq_id = 0, pbl_size;
900	enum _ecore_status_t rc = ECORE_NOTIMPL;
901
902	if (!ECORE_LL2_TX_REGISTERED(p_ll2_conn))
903		return ECORE_SUCCESS;
904
905	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_OOO)
906		p_ll2_conn->tx_stats_en = 0;
907	else
908		p_ll2_conn->tx_stats_en = 1;
909
910	/* Get SPQ entry */
911	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
912	init_data.cid = p_ll2_conn->cid;
913	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
914	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
915
916	rc = ecore_sp_init_request(p_hwfn, &p_ent,
917				   CORE_RAMROD_TX_QUEUE_START,
918				   PROTOCOLID_CORE, &init_data);
919	if (rc != ECORE_SUCCESS)
920		return rc;
921
922	p_ramrod = &p_ent->ramrod.core_tx_queue_start;
923
924	p_ramrod->sb_id = OSAL_CPU_TO_LE16(ecore_int_get_sp_sb_id(p_hwfn));
925	p_ramrod->sb_index = p_tx->tx_sb_index;
926	p_ramrod->mtu = OSAL_CPU_TO_LE16(p_ll2_conn->input.mtu);
927	p_ramrod->stats_en = p_ll2_conn->tx_stats_en;
928	p_ramrod->stats_id = p_ll2_conn->tx_stats_id;
929
930	DMA_REGPAIR_LE(p_ramrod->pbl_base_addr,
931		       ecore_chain_get_pbl_phys(&p_tx->txq_chain));
932	pbl_size = (u16)ecore_chain_get_page_cnt(&p_tx->txq_chain);
933	p_ramrod->pbl_size = OSAL_CPU_TO_LE16(pbl_size);
934
935	/* TODO RESC_ALLOC pq for ll2 */
936	switch (p_ll2_conn->input.tx_tc) {
937	case PURE_LB_TC:
938		pq_id = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
939		break;
940	case PKT_LB_TC:
941		pq_id = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OOO);
942		break;
943	default:
944		pq_id = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
945	}
946
947	p_ramrod->qm_pq_id = OSAL_CPU_TO_LE16(pq_id);
948
949	switch (conn_type) {
950	case ECORE_LL2_TYPE_FCOE:
951		p_ramrod->conn_type = PROTOCOLID_FCOE;
952		break;
953	case ECORE_LL2_TYPE_ISCSI:
954		p_ramrod->conn_type = PROTOCOLID_ISCSI;
955		break;
956	case ECORE_LL2_TYPE_ROCE:
957		p_ramrod->conn_type = PROTOCOLID_ROCE;
958		break;
959	case ECORE_LL2_TYPE_IWARP:
960		p_ramrod->conn_type = PROTOCOLID_IWARP;
961		break;
962	case ECORE_LL2_TYPE_OOO:
963		if (p_hwfn->hw_info.personality == ECORE_PCI_ISCSI) {
964			p_ramrod->conn_type = PROTOCOLID_ISCSI;
965		} else {
966			p_ramrod->conn_type = PROTOCOLID_IWARP;
967		}
968		break;
969	default:
970		p_ramrod->conn_type = PROTOCOLID_ETH;
971		DP_NOTICE(p_hwfn, false, "Unknown connection type: %d\n",
972			  conn_type);
973	}
974
975	p_ramrod->gsi_offload_flag = p_ll2_conn->input.gsi_enable;
976
977	rc = ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
978	if (rc != ECORE_SUCCESS)
979		return rc;
980
981	rc = ecore_db_recovery_add(p_hwfn->p_dev, p_tx->doorbell_addr,
982				   &p_tx->db_msg, DB_REC_WIDTH_32B,
983				   DB_REC_KERNEL);
984	return rc;
985}
986
987static enum _ecore_status_t ecore_sp_ll2_rx_queue_stop(struct ecore_hwfn *p_hwfn,
988						       struct ecore_ll2_info *p_ll2_conn)
989{
990	struct core_rx_stop_ramrod_data	*p_ramrod = OSAL_NULL;
991	struct ecore_spq_entry *p_ent = OSAL_NULL;
992	struct ecore_sp_init_data init_data;
993	enum _ecore_status_t rc = ECORE_NOTIMPL;
994
995	/* Get SPQ entry */
996	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
997	init_data.cid = p_ll2_conn->cid;
998	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
999	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
1000
1001	rc = ecore_sp_init_request(p_hwfn, &p_ent,
1002				   CORE_RAMROD_RX_QUEUE_STOP,
1003				   PROTOCOLID_CORE, &init_data);
1004	if (rc != ECORE_SUCCESS)
1005		return rc;
1006
1007	p_ramrod = &p_ent->ramrod.core_rx_queue_stop;
1008
1009	p_ramrod->complete_event_flg = 1;
1010	p_ramrod->queue_id = p_ll2_conn->queue_id;
1011
1012	return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
1013}
1014
1015static enum _ecore_status_t ecore_sp_ll2_tx_queue_stop(struct ecore_hwfn *p_hwfn,
1016						       struct ecore_ll2_info *p_ll2_conn)
1017{
1018	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
1019	struct ecore_spq_entry *p_ent = OSAL_NULL;
1020	struct ecore_sp_init_data init_data;
1021	enum _ecore_status_t rc = ECORE_NOTIMPL;
1022
1023	ecore_db_recovery_del(p_hwfn->p_dev, p_tx->doorbell_addr,
1024			      &p_tx->db_msg);
1025
1026	/* Get SPQ entry */
1027	OSAL_MEMSET(&init_data, 0, sizeof(init_data));
1028	init_data.cid = p_ll2_conn->cid;
1029	init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
1030	init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
1031
1032	rc = ecore_sp_init_request(p_hwfn, &p_ent,
1033				   CORE_RAMROD_TX_QUEUE_STOP,
1034				   PROTOCOLID_CORE, &init_data);
1035	if (rc != ECORE_SUCCESS)
1036		return rc;
1037
1038	return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
1039}
1040
1041static enum _ecore_status_t
1042ecore_ll2_acquire_connection_rx(struct ecore_hwfn *p_hwfn,
1043				struct ecore_ll2_info *p_ll2_info)
1044{
1045	struct ecore_ll2_rx_packet *p_descq;
1046	u32 capacity;
1047	enum _ecore_status_t rc = ECORE_SUCCESS;
1048
1049	if (!p_ll2_info->input.rx_num_desc)
1050		goto out;
1051
1052	rc = ecore_chain_alloc(p_hwfn->p_dev,
1053			       ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
1054			       ECORE_CHAIN_MODE_NEXT_PTR,
1055			       ECORE_CHAIN_CNT_TYPE_U16,
1056			       p_ll2_info->input.rx_num_desc,
1057			       sizeof(struct core_rx_bd),
1058			       &p_ll2_info->rx_queue.rxq_chain, OSAL_NULL);
1059	if (rc) {
1060		DP_NOTICE(p_hwfn, false,
1061			  "Failed to allocate ll2 rxq chain\n");
1062		goto out;
1063	}
1064
1065	capacity = ecore_chain_get_capacity(&p_ll2_info->rx_queue.rxq_chain);
1066	p_descq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1067			      capacity * sizeof(struct ecore_ll2_rx_packet));
1068	if (!p_descq) {
1069		rc = ECORE_NOMEM;
1070		DP_NOTICE(p_hwfn, false,
1071			  "Failed to allocate ll2 Rx desc\n");
1072		goto out;
1073	}
1074	p_ll2_info->rx_queue.descq_array = p_descq;
1075
1076	rc = ecore_chain_alloc(p_hwfn->p_dev,
1077			       ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
1078			       ECORE_CHAIN_MODE_PBL,
1079			       ECORE_CHAIN_CNT_TYPE_U16,
1080			       p_ll2_info->input.rx_num_desc,
1081			       sizeof(struct core_rx_fast_path_cqe),
1082			       &p_ll2_info->rx_queue.rcq_chain, OSAL_NULL);
1083	if (rc != ECORE_SUCCESS) {
1084		DP_NOTICE(p_hwfn, false,
1085			  "Failed to allocate ll2 rcq chain\n");
1086		goto out;
1087	}
1088
1089	DP_VERBOSE(p_hwfn, ECORE_MSG_LL2,
1090		   "Allocated LL2 Rxq [Type %08x] with 0x%08x buffers\n",
1091		   p_ll2_info->input.conn_type,
1092		   p_ll2_info->input.rx_num_desc);
1093
1094out:
1095	return rc;
1096}
1097
1098static enum _ecore_status_t
1099ecore_ll2_acquire_connection_tx(struct ecore_hwfn *p_hwfn,
1100				struct ecore_ll2_info *p_ll2_info)
1101{
1102	struct ecore_ll2_tx_packet *p_descq;
1103	u32 capacity;
1104	enum _ecore_status_t rc = ECORE_SUCCESS;
1105	u32 desc_size;
1106
1107	if (!p_ll2_info->input.tx_num_desc)
1108		goto out;
1109
1110	rc = ecore_chain_alloc(p_hwfn->p_dev,
1111			       ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
1112			       ECORE_CHAIN_MODE_PBL,
1113			       ECORE_CHAIN_CNT_TYPE_U16,
1114			       p_ll2_info->input.tx_num_desc,
1115			       sizeof(struct core_tx_bd),
1116			       &p_ll2_info->tx_queue.txq_chain, OSAL_NULL);
1117	if (rc != ECORE_SUCCESS)
1118		goto out;
1119
1120	capacity = ecore_chain_get_capacity(&p_ll2_info->tx_queue.txq_chain);
1121	desc_size = (sizeof(*p_descq) +
1122		     (p_ll2_info->input.tx_max_bds_per_packet - 1) *
1123		     sizeof(p_descq->bds_set));
1124
1125	p_descq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1126			      capacity * desc_size);
1127	if (!p_descq) {
1128		rc = ECORE_NOMEM;
1129		goto out;
1130	}
1131	p_ll2_info->tx_queue.descq_array = p_descq;
1132
1133	DP_VERBOSE(p_hwfn, ECORE_MSG_LL2,
1134		   "Allocated LL2 Txq [Type %08x] with 0x%08x buffers\n",
1135		   p_ll2_info->input.conn_type,
1136		   p_ll2_info->input.tx_num_desc);
1137
1138out:
1139	if (rc != ECORE_SUCCESS)
1140		DP_NOTICE(p_hwfn, false,
1141			  "Can't allocate memory for Tx LL2 with 0x%08x buffers\n",
1142			  p_ll2_info->input.tx_num_desc);
1143	return rc;
1144}
1145
1146static enum _ecore_status_t
1147ecore_ll2_acquire_connection_ooo(struct ecore_hwfn *p_hwfn,
1148				 struct ecore_ll2_info *p_ll2_info, u16 mtu)
1149{
1150	struct ecore_ooo_buffer *p_buf = OSAL_NULL;
1151	u32 rx_buffer_size = 0;
1152	void *p_virt;
1153	u16 buf_idx;
1154	enum _ecore_status_t rc = ECORE_SUCCESS;
1155
1156	if (p_ll2_info->input.conn_type != ECORE_LL2_TYPE_OOO)
1157		return rc;
1158
1159	/* Correct number of requested OOO buffers if needed */
1160	if (!p_ll2_info->input.rx_num_ooo_buffers) {
1161		u16 num_desc = p_ll2_info->input.rx_num_desc;
1162
1163		if (!num_desc)
1164			return ECORE_INVAL;
1165		p_ll2_info->input.rx_num_ooo_buffers = num_desc * 2;
1166	}
1167
1168	/* TODO - use some defines for buffer size */
1169	rx_buffer_size = mtu + 14 + 4 + 8 + ETH_CACHE_LINE_SIZE;
1170	rx_buffer_size = (rx_buffer_size + ETH_CACHE_LINE_SIZE - 1) &
1171			 ~(ETH_CACHE_LINE_SIZE - 1);
1172
1173	for (buf_idx = 0; buf_idx < p_ll2_info->input.rx_num_ooo_buffers;
1174	     buf_idx++) {
1175		p_buf = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_buf));
1176		if (!p_buf) {
1177			DP_NOTICE(p_hwfn, false,
1178				  "Failed to allocate ooo descriptor\n");
1179			rc = ECORE_NOMEM;
1180			goto out;
1181		}
1182
1183		p_buf->rx_buffer_size = rx_buffer_size;
1184		p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev,
1185						 &p_buf->rx_buffer_phys_addr,
1186						 p_buf->rx_buffer_size);
1187		if (!p_virt) {
1188			DP_NOTICE(p_hwfn, false,
1189				  "Failed to allocate ooo buffer\n");
1190			OSAL_FREE(p_hwfn->p_dev, p_buf);
1191			rc = ECORE_NOMEM;
1192			goto out;
1193		}
1194		p_buf->rx_buffer_virt_addr = p_virt;
1195		ecore_ooo_put_free_buffer(p_hwfn->p_ooo_info, p_buf);
1196	}
1197
1198	DP_VERBOSE(p_hwfn, ECORE_MSG_LL2,
1199		   "Allocated [%04x] LL2 OOO buffers [each of size 0x%08x]\n",
1200		   p_ll2_info->input.rx_num_ooo_buffers, rx_buffer_size);
1201
1202out:
1203	return rc;
1204}
1205
1206static enum _ecore_status_t
1207ecore_ll2_set_cbs(struct ecore_ll2_info *p_ll2_info,
1208		    const struct ecore_ll2_cbs *cbs)
1209{
1210	if (!cbs || (!cbs->rx_comp_cb ||
1211		     !cbs->rx_release_cb ||
1212		     !cbs->tx_comp_cb ||
1213		     !cbs->tx_release_cb ||
1214		     !cbs->cookie))
1215		return ECORE_INVAL;
1216
1217	p_ll2_info->cbs.rx_comp_cb = cbs->rx_comp_cb;
1218	p_ll2_info->cbs.rx_release_cb = cbs->rx_release_cb;
1219	p_ll2_info->cbs.tx_comp_cb = cbs->tx_comp_cb;
1220	p_ll2_info->cbs.tx_release_cb = cbs->tx_release_cb;
1221	p_ll2_info->cbs.slowpath_cb = cbs->slowpath_cb;
1222	p_ll2_info->cbs.cookie = cbs->cookie;
1223
1224	return ECORE_SUCCESS;
1225}
1226
1227static enum core_error_handle
1228ecore_ll2_get_error_choice(enum ecore_ll2_error_handle err)
1229{
1230	switch (err) {
1231	case ECORE_LL2_DROP_PACKET:
1232		return LL2_DROP_PACKET;
1233	case ECORE_LL2_DO_NOTHING:
1234		return LL2_DO_NOTHING;
1235	case ECORE_LL2_ASSERT:
1236		return LL2_ASSERT;
1237	default:
1238		return LL2_DO_NOTHING;
1239	}
1240}
1241
1242enum _ecore_status_t
1243ecore_ll2_acquire_connection(void *cxt,
1244			     struct ecore_ll2_acquire_data *data)
1245{
1246	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1247	ecore_int_comp_cb_t comp_rx_cb, comp_tx_cb;
1248	struct ecore_ll2_info *p_ll2_info = OSAL_NULL;
1249	enum _ecore_status_t rc;
1250	u8 i, *p_tx_max;
1251
1252	if (!data->p_connection_handle || !p_hwfn->p_ll2_info) {
1253		DP_NOTICE(p_hwfn, false, "Invalid connection handle, ll2_info not allocated\n");
1254		return ECORE_INVAL;
1255	}
1256
1257	/* Find a free connection to be used */
1258	for (i = 0; (i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS); i++) {
1259		OSAL_MUTEX_ACQUIRE(&p_hwfn->p_ll2_info[i].mutex);
1260		if (p_hwfn->p_ll2_info[i].b_active) {
1261			OSAL_MUTEX_RELEASE(&p_hwfn->p_ll2_info[i].mutex);
1262			continue;
1263		}
1264
1265		p_hwfn->p_ll2_info[i].b_active = true;
1266		p_ll2_info = &p_hwfn->p_ll2_info[i];
1267		OSAL_MUTEX_RELEASE(&p_hwfn->p_ll2_info[i].mutex);
1268		break;
1269	}
1270	if (p_ll2_info == OSAL_NULL) {
1271		DP_NOTICE(p_hwfn, false, "No available ll2 connection\n");
1272		return ECORE_BUSY;
1273	}
1274
1275	OSAL_MEMCPY(&p_ll2_info->input, &data->input,
1276		    sizeof(p_ll2_info->input));
1277
1278	switch (data->input.tx_dest) {
1279	case ECORE_LL2_TX_DEST_NW:
1280		p_ll2_info->tx_dest = CORE_TX_DEST_NW;
1281		break;
1282	case ECORE_LL2_TX_DEST_LB:
1283		p_ll2_info->tx_dest = CORE_TX_DEST_LB;
1284		break;
1285	case ECORE_LL2_TX_DEST_DROP:
1286		p_ll2_info->tx_dest = CORE_TX_DEST_DROP;
1287		break;
1288	default:
1289		return ECORE_INVAL;
1290	}
1291
1292	if ((data->input.conn_type == ECORE_LL2_TYPE_OOO) ||
1293	    data->input.secondary_queue)
1294		p_ll2_info->main_func_queue = false;
1295	else
1296		p_ll2_info->main_func_queue = true;
1297
1298	/* Correct maximum number of Tx BDs */
1299	p_tx_max = &p_ll2_info->input.tx_max_bds_per_packet;
1300	if (*p_tx_max == 0)
1301		*p_tx_max = CORE_LL2_TX_MAX_BDS_PER_PACKET;
1302	else
1303		*p_tx_max = OSAL_MIN_T(u8, *p_tx_max,
1304				       CORE_LL2_TX_MAX_BDS_PER_PACKET);
1305
1306	rc = ecore_ll2_set_cbs(p_ll2_info, data->cbs);
1307	if (rc) {
1308		DP_NOTICE(p_hwfn, false, "Invalid callback functions\n");
1309		goto q_allocate_fail;
1310	}
1311
1312	rc = ecore_ll2_acquire_connection_rx(p_hwfn, p_ll2_info);
1313	if (rc != ECORE_SUCCESS) {
1314		DP_NOTICE(p_hwfn, false, "ll2 acquire rx connection failed\n");
1315		goto q_allocate_fail;
1316	}
1317
1318	rc = ecore_ll2_acquire_connection_tx(p_hwfn, p_ll2_info);
1319	if (rc != ECORE_SUCCESS) {
1320		DP_NOTICE(p_hwfn, false, "ll2 acquire tx connection failed\n");
1321		goto q_allocate_fail;
1322	}
1323
1324	rc = ecore_ll2_acquire_connection_ooo(p_hwfn, p_ll2_info,
1325					      data->input.mtu);
1326	if (rc != ECORE_SUCCESS) {
1327		DP_NOTICE(p_hwfn, false, "ll2 acquire ooo connection failed\n");
1328		goto q_allocate_fail;
1329	}
1330
1331	/* Register callbacks for the Rx/Tx queues */
1332	if (data->input.conn_type == ECORE_LL2_TYPE_OOO) {
1333		comp_rx_cb = ecore_ll2_lb_rxq_completion;
1334		comp_tx_cb = ecore_ll2_lb_txq_completion;
1335
1336	} else {
1337		comp_rx_cb = ecore_ll2_rxq_completion;
1338		comp_tx_cb = ecore_ll2_txq_completion;
1339	}
1340
1341	if (data->input.rx_num_desc) {
1342		ecore_int_register_cb(p_hwfn, comp_rx_cb,
1343				      &p_hwfn->p_ll2_info[i],
1344				      &p_ll2_info->rx_queue.rx_sb_index,
1345				      &p_ll2_info->rx_queue.p_fw_cons);
1346		p_ll2_info->rx_queue.b_cb_registred   = true;
1347	}
1348
1349	if (data->input.tx_num_desc) {
1350		ecore_int_register_cb(p_hwfn,
1351				      comp_tx_cb,
1352				      &p_hwfn->p_ll2_info[i],
1353				      &p_ll2_info->tx_queue.tx_sb_index,
1354				      &p_ll2_info->tx_queue.p_fw_cons);
1355		p_ll2_info->tx_queue.b_cb_registred   = true;
1356	}
1357
1358	*(data->p_connection_handle) = i;
1359	return rc;
1360
1361q_allocate_fail:
1362	ecore_ll2_release_connection(p_hwfn, i);
1363	return ECORE_NOMEM;
1364}
1365
1366static enum _ecore_status_t ecore_ll2_establish_connection_rx(struct ecore_hwfn *p_hwfn,
1367							      struct ecore_ll2_info *p_ll2_conn)
1368{
1369	enum ecore_ll2_error_handle error_input;
1370	enum core_error_handle error_mode;
1371	u8 action_on_error = 0;
1372
1373	if (!ECORE_LL2_RX_REGISTERED(p_ll2_conn))
1374		return ECORE_SUCCESS;
1375
1376	DIRECT_REG_WR(p_hwfn, p_ll2_conn->rx_queue.set_prod_addr, 0x0);
1377	error_input = p_ll2_conn->input.ai_err_packet_too_big;
1378	error_mode = ecore_ll2_get_error_choice(error_input);
1379	SET_FIELD(action_on_error,
1380		  CORE_RX_ACTION_ON_ERROR_PACKET_TOO_BIG, error_mode);
1381	error_input = p_ll2_conn->input.ai_err_no_buf;
1382	error_mode = ecore_ll2_get_error_choice(error_input);
1383	SET_FIELD(action_on_error,
1384		  CORE_RX_ACTION_ON_ERROR_NO_BUFF, error_mode);
1385
1386	return ecore_sp_ll2_rx_queue_start(p_hwfn, p_ll2_conn, action_on_error);
1387}
1388
1389static void
1390ecore_ll2_establish_connection_ooo(struct ecore_hwfn *p_hwfn,
1391				   struct ecore_ll2_info *p_ll2_conn)
1392{
1393	if (p_ll2_conn->input.conn_type != ECORE_LL2_TYPE_OOO)
1394		return;
1395
1396	ecore_ooo_release_all_isles(p_hwfn->p_ooo_info);
1397	ecore_ooo_submit_rx_buffers(p_hwfn, p_ll2_conn);
1398}
1399
1400enum _ecore_status_t ecore_ll2_establish_connection(void *cxt,
1401						    u8 connection_handle)
1402{
1403	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1404	struct e4_core_conn_context *p_cxt;
1405	struct ecore_ll2_info *p_ll2_conn;
1406	struct ecore_cxt_info cxt_info;
1407	struct ecore_ll2_rx_queue *p_rx;
1408	struct ecore_ll2_tx_queue *p_tx;
1409	struct ecore_ll2_tx_packet *p_pkt;
1410	struct ecore_ptt *p_ptt;
1411	enum _ecore_status_t rc = ECORE_NOTIMPL;
1412	u32 i, capacity;
1413	u32 desc_size;
1414	u8 qid;
1415
1416	p_ptt = ecore_ptt_acquire(p_hwfn);
1417	if (!p_ptt)
1418		return ECORE_AGAIN;
1419
1420	p_ll2_conn = ecore_ll2_handle_sanity_lock(p_hwfn, connection_handle);
1421	if (p_ll2_conn == OSAL_NULL) {
1422		rc = ECORE_INVAL;
1423		goto out;
1424	}
1425
1426	p_rx = &p_ll2_conn->rx_queue;
1427	p_tx = &p_ll2_conn->tx_queue;
1428
1429	ecore_chain_reset(&p_rx->rxq_chain);
1430	ecore_chain_reset(&p_rx->rcq_chain);
1431	OSAL_LIST_INIT(&p_rx->active_descq);
1432	OSAL_LIST_INIT(&p_rx->free_descq);
1433	OSAL_LIST_INIT(&p_rx->posting_descq);
1434	OSAL_SPIN_LOCK_INIT(&p_rx->lock);
1435	capacity = ecore_chain_get_capacity(&p_rx->rxq_chain);
1436	for (i = 0; i < capacity; i++)
1437		OSAL_LIST_PUSH_TAIL(&p_rx->descq_array[i].list_entry,
1438				    &p_rx->free_descq);
1439	*p_rx->p_fw_cons = 0;
1440
1441	ecore_chain_reset(&p_tx->txq_chain);
1442	OSAL_LIST_INIT(&p_tx->active_descq);
1443	OSAL_LIST_INIT(&p_tx->free_descq);
1444	OSAL_LIST_INIT(&p_tx->sending_descq);
1445	OSAL_SPIN_LOCK_INIT(&p_tx->lock);
1446	capacity = ecore_chain_get_capacity(&p_tx->txq_chain);
1447	/* The size of the element in descq_array is flexible */
1448	desc_size = (sizeof(*p_pkt) +
1449		     (p_ll2_conn->input.tx_max_bds_per_packet - 1) *
1450		     sizeof(p_pkt->bds_set));
1451
1452	for (i = 0; i < capacity; i++) {
1453		p_pkt = (struct ecore_ll2_tx_packet *)((u8 *)p_tx->descq_array +
1454						       desc_size*i);
1455		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry,
1456				    &p_tx->free_descq);
1457	}
1458	p_tx->cur_completing_bd_idx = 0;
1459	p_tx->bds_idx = 0;
1460	p_tx->b_completing_packet = false;
1461	p_tx->cur_send_packet = OSAL_NULL;
1462	p_tx->cur_send_frag_num = 0;
1463	p_tx->cur_completing_frag_num = 0;
1464	*p_tx->p_fw_cons = 0;
1465
1466	rc = ecore_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_ll2_conn->cid);
1467	if (rc)
1468		goto out;
1469	cxt_info.iid = p_ll2_conn->cid;
1470	rc = ecore_cxt_get_cid_info(p_hwfn, &cxt_info);
1471	if (rc) {
1472		DP_NOTICE(p_hwfn, true, "Cannot find context info for cid=%d\n",
1473			  p_ll2_conn->cid);
1474		goto out;
1475	}
1476
1477	p_cxt = cxt_info.p_cxt;
1478
1479	/* @@@TBD we zero the context until we have ilt_reset implemented. */
1480	OSAL_MEM_ZERO(p_cxt, sizeof(*p_cxt));
1481
1482	qid = ecore_ll2_handle_to_queue_id(p_hwfn, connection_handle);
1483	p_ll2_conn->queue_id = qid;
1484	p_ll2_conn->tx_stats_id = qid;
1485	p_rx->set_prod_addr = (u8 OSAL_IOMEM*)p_hwfn->regview +
1486					      GTT_BAR0_MAP_REG_TSDM_RAM +
1487					      TSTORM_LL2_RX_PRODS_OFFSET(qid);
1488	p_tx->doorbell_addr = (u8 OSAL_IOMEM*)p_hwfn->doorbells +
1489					      DB_ADDR(p_ll2_conn->cid,
1490						      DQ_DEMS_LEGACY);
1491
1492	/* prepare db data */
1493	SET_FIELD(p_tx->db_msg.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
1494	SET_FIELD(p_tx->db_msg.params, CORE_DB_DATA_AGG_CMD,
1495		  DB_AGG_CMD_SET);
1496	SET_FIELD(p_tx->db_msg.params, CORE_DB_DATA_AGG_VAL_SEL,
1497		  DQ_XCM_CORE_TX_BD_PROD_CMD);
1498	p_tx->db_msg.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
1499
1500	rc = ecore_ll2_establish_connection_rx(p_hwfn, p_ll2_conn);
1501	if (rc)
1502		goto out;
1503
1504	rc = ecore_sp_ll2_tx_queue_start(p_hwfn, p_ll2_conn);
1505	if (rc)
1506		goto out;
1507
1508	if (!ECORE_IS_RDMA_PERSONALITY(p_hwfn))
1509		ecore_wr(p_hwfn, p_ptt, PRS_REG_USE_LIGHT_L2, 1);
1510
1511	ecore_ll2_establish_connection_ooo(p_hwfn, p_ll2_conn);
1512
1513	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_FCOE) {
1514		if (!OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC,
1515				   &p_hwfn->p_dev->mf_bits))
1516			ecore_llh_add_protocol_filter(p_hwfn->p_dev, 0,
1517						      ECORE_LLH_FILTER_ETHERTYPE,
1518						      0x8906, 0);
1519		ecore_llh_add_protocol_filter(p_hwfn->p_dev, 0,
1520					      ECORE_LLH_FILTER_ETHERTYPE,
1521					      0x8914, 0);
1522	}
1523
1524out:
1525	ecore_ptt_release(p_hwfn, p_ptt);
1526
1527	return rc;
1528}
1529
1530static void ecore_ll2_post_rx_buffer_notify_fw(struct ecore_hwfn *p_hwfn,
1531					       struct ecore_ll2_rx_queue *p_rx,
1532					       struct ecore_ll2_rx_packet *p_curp)
1533{
1534	struct ecore_ll2_rx_packet *p_posting_packet = OSAL_NULL;
1535	struct core_ll2_rx_prod rx_prod = {0, 0, 0};
1536	bool b_notify_fw = false;
1537	u16 bd_prod, cq_prod;
1538
1539	/* This handles the flushing of already posted buffers */
1540	while (!OSAL_LIST_IS_EMPTY(&p_rx->posting_descq)) {
1541		p_posting_packet = OSAL_LIST_FIRST_ENTRY(&p_rx->posting_descq,
1542							 struct ecore_ll2_rx_packet,
1543							 list_entry);
1544#if defined(_NTDDK_)
1545#pragma warning(suppress : 6011 28182)
1546#endif
1547		OSAL_LIST_REMOVE_ENTRY(&p_posting_packet->list_entry, &p_rx->posting_descq);
1548		OSAL_LIST_PUSH_TAIL(&p_posting_packet->list_entry, &p_rx->active_descq);
1549		b_notify_fw = true;
1550	}
1551
1552	/* This handles the supplied packet [if there is one] */
1553	if (p_curp) {
1554		OSAL_LIST_PUSH_TAIL(&p_curp->list_entry,
1555				    &p_rx->active_descq);
1556		b_notify_fw = true;
1557	}
1558
1559	if (!b_notify_fw)
1560		return;
1561
1562	bd_prod = ecore_chain_get_prod_idx(&p_rx->rxq_chain);
1563	cq_prod = ecore_chain_get_prod_idx(&p_rx->rcq_chain);
1564	rx_prod.bd_prod = OSAL_CPU_TO_LE16(bd_prod);
1565	rx_prod.cqe_prod = OSAL_CPU_TO_LE16(cq_prod);
1566	DIRECT_REG_WR(p_hwfn, p_rx->set_prod_addr, *((u32 *)&rx_prod));
1567}
1568
1569enum _ecore_status_t ecore_ll2_post_rx_buffer(void *cxt,
1570					      u8 connection_handle,
1571					      dma_addr_t addr,
1572					      u16 buf_len,
1573					      void *cookie,
1574					      u8 notify_fw)
1575{
1576	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1577	struct core_rx_bd_with_buff_len *p_curb = OSAL_NULL;
1578	struct ecore_ll2_rx_packet *p_curp = OSAL_NULL;
1579	struct ecore_ll2_info *p_ll2_conn;
1580	struct ecore_ll2_rx_queue *p_rx;
1581	unsigned long flags;
1582	void *p_data;
1583	enum _ecore_status_t rc = ECORE_SUCCESS;
1584
1585	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1586	if (p_ll2_conn == OSAL_NULL)
1587		return ECORE_INVAL;
1588	p_rx = &p_ll2_conn->rx_queue;
1589	if (p_rx->set_prod_addr == OSAL_NULL)
1590		return ECORE_IO;
1591
1592	OSAL_SPIN_LOCK_IRQSAVE(&p_rx->lock, flags);
1593	if (!OSAL_LIST_IS_EMPTY(&p_rx->free_descq))
1594		p_curp = OSAL_LIST_FIRST_ENTRY(&p_rx->free_descq,
1595					       struct ecore_ll2_rx_packet,
1596					       list_entry);
1597	if (p_curp) {
1598		if (ecore_chain_get_elem_left(&p_rx->rxq_chain) &&
1599		    ecore_chain_get_elem_left(&p_rx->rcq_chain)) {
1600			p_data = ecore_chain_produce(&p_rx->rxq_chain);
1601			p_curb = (struct core_rx_bd_with_buff_len *)p_data;
1602			ecore_chain_produce(&p_rx->rcq_chain);
1603		}
1604	}
1605
1606	/* If we're lacking entires, let's try to flush buffers to FW */
1607	if (!p_curp || !p_curb) {
1608		rc =  ECORE_BUSY;
1609		p_curp = OSAL_NULL;
1610		goto out_notify;
1611	}
1612
1613	/* We have an Rx packet we can fill */
1614	DMA_REGPAIR_LE(p_curb->addr, addr);
1615	p_curb->buff_length = OSAL_CPU_TO_LE16(buf_len);
1616	p_curp->rx_buf_addr = addr;
1617	p_curp->cookie = cookie;
1618	p_curp->rxq_bd = p_curb;
1619	p_curp->buf_length = buf_len;
1620	OSAL_LIST_REMOVE_ENTRY(&p_curp->list_entry,
1621			       &p_rx->free_descq);
1622
1623	/* Check if we only want to enqueue this packet without informing FW */
1624	if (!notify_fw) {
1625		OSAL_LIST_PUSH_TAIL(&p_curp->list_entry,
1626				    &p_rx->posting_descq);
1627		goto out;
1628	}
1629
1630out_notify:
1631	ecore_ll2_post_rx_buffer_notify_fw(p_hwfn, p_rx, p_curp);
1632out:
1633	OSAL_SPIN_UNLOCK_IRQSAVE(&p_rx->lock, flags);
1634	return rc;
1635}
1636
1637static void ecore_ll2_prepare_tx_packet_set(struct ecore_ll2_tx_queue *p_tx,
1638					    struct ecore_ll2_tx_packet *p_curp,
1639					    struct ecore_ll2_tx_pkt_info *pkt,
1640					    u8 notify_fw)
1641{
1642	OSAL_LIST_REMOVE_ENTRY(&p_curp->list_entry,
1643			       &p_tx->free_descq);
1644	p_curp->cookie = pkt->cookie;
1645	p_curp->bd_used = pkt->num_of_bds;
1646	p_curp->notify_fw = notify_fw;
1647	p_tx->cur_send_packet = p_curp;
1648	p_tx->cur_send_frag_num = 0;
1649
1650	p_curp->bds_set[p_tx->cur_send_frag_num].tx_frag = pkt->first_frag;
1651	p_curp->bds_set[p_tx->cur_send_frag_num].frag_len = pkt->first_frag_len;
1652	p_tx->cur_send_frag_num++;
1653}
1654
1655static void ecore_ll2_prepare_tx_packet_set_bd(
1656		struct ecore_hwfn *p_hwfn,
1657		struct ecore_ll2_info *p_ll2,
1658		struct ecore_ll2_tx_packet *p_curp,
1659		struct ecore_ll2_tx_pkt_info *pkt)
1660{
1661	struct ecore_chain *p_tx_chain = &p_ll2->tx_queue.txq_chain;
1662	u16 prod_idx = ecore_chain_get_prod_idx(p_tx_chain);
1663	struct core_tx_bd *start_bd = OSAL_NULL;
1664	enum core_roce_flavor_type roce_flavor;
1665	enum core_tx_dest tx_dest;
1666	u16 bd_data = 0, frag_idx;
1667
1668	roce_flavor = (pkt->ecore_roce_flavor == ECORE_LL2_ROCE) ?
1669		CORE_ROCE : CORE_RROCE;
1670
1671	switch (pkt->tx_dest) {
1672	case ECORE_LL2_TX_DEST_NW:
1673		tx_dest = CORE_TX_DEST_NW;
1674		break;
1675	case ECORE_LL2_TX_DEST_LB:
1676		tx_dest = CORE_TX_DEST_LB;
1677		break;
1678	case ECORE_LL2_TX_DEST_DROP:
1679		tx_dest = CORE_TX_DEST_DROP;
1680		break;
1681	default:
1682		tx_dest = CORE_TX_DEST_LB;
1683		break;
1684	}
1685
1686	start_bd = (struct core_tx_bd*)ecore_chain_produce(p_tx_chain);
1687
1688	if (ECORE_IS_IWARP_PERSONALITY(p_hwfn) &&
1689	    (p_ll2->input.conn_type == ECORE_LL2_TYPE_OOO)) {
1690		start_bd->nw_vlan_or_lb_echo =
1691			OSAL_CPU_TO_LE16(IWARP_LL2_IN_ORDER_TX_QUEUE);
1692	} else {
1693		start_bd->nw_vlan_or_lb_echo = OSAL_CPU_TO_LE16(pkt->vlan);
1694		if (OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC, &p_hwfn->p_dev->mf_bits) &&
1695		    (p_ll2->input.conn_type == ECORE_LL2_TYPE_FCOE))
1696			pkt->remove_stag = true;
1697	}
1698
1699	SET_FIELD(start_bd->bitfield1, CORE_TX_BD_L4_HDR_OFFSET_W,
1700		  OSAL_CPU_TO_LE16(pkt->l4_hdr_offset_w));
1701	SET_FIELD(start_bd->bitfield1, CORE_TX_BD_TX_DST, tx_dest);
1702	bd_data |= pkt->bd_flags;
1703	SET_FIELD(bd_data, CORE_TX_BD_DATA_START_BD, 0x1);
1704	SET_FIELD(bd_data, CORE_TX_BD_DATA_NBDS, pkt->num_of_bds);
1705	SET_FIELD(bd_data, CORE_TX_BD_DATA_ROCE_FLAV, roce_flavor);
1706	SET_FIELD(bd_data, CORE_TX_BD_DATA_IP_CSUM, !!(pkt->enable_ip_cksum));
1707	SET_FIELD(bd_data, CORE_TX_BD_DATA_L4_CSUM, !!(pkt->enable_l4_cksum));
1708	SET_FIELD(bd_data, CORE_TX_BD_DATA_IP_LEN, !!(pkt->calc_ip_len));
1709	SET_FIELD(bd_data, CORE_TX_BD_DATA_DISABLE_STAG_INSERTION,
1710		  !!(pkt->remove_stag));
1711
1712	start_bd->bd_data.as_bitfield = OSAL_CPU_TO_LE16(bd_data);
1713	DMA_REGPAIR_LE(start_bd->addr, pkt->first_frag);
1714	start_bd->nbytes = OSAL_CPU_TO_LE16(pkt->first_frag_len);
1715
1716	DP_VERBOSE(p_hwfn, (ECORE_MSG_TX_QUEUED | ECORE_MSG_LL2),
1717		   "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",
1718		   p_ll2->queue_id, p_ll2->cid, p_ll2->input.conn_type,
1719		   prod_idx, pkt->first_frag_len, pkt->num_of_bds,
1720		   OSAL_LE32_TO_CPU(start_bd->addr.hi),
1721		   OSAL_LE32_TO_CPU(start_bd->addr.lo));
1722
1723	if (p_ll2->tx_queue.cur_send_frag_num == pkt->num_of_bds)
1724		return;
1725
1726	/* Need to provide the packet with additional BDs for frags */
1727	for (frag_idx = p_ll2->tx_queue.cur_send_frag_num;
1728	     frag_idx < pkt->num_of_bds; frag_idx++) {
1729		struct core_tx_bd **p_bd = &p_curp->bds_set[frag_idx].txq_bd;
1730
1731		*p_bd = (struct core_tx_bd *)ecore_chain_produce(p_tx_chain);
1732		(*p_bd)->bd_data.as_bitfield = 0;
1733		(*p_bd)->bitfield1 = 0;
1734		p_curp->bds_set[frag_idx].tx_frag = 0;
1735		p_curp->bds_set[frag_idx].frag_len = 0;
1736	}
1737}
1738
1739/* This should be called while the Txq spinlock is being held */
1740static void ecore_ll2_tx_packet_notify(struct ecore_hwfn *p_hwfn,
1741				       struct ecore_ll2_info *p_ll2_conn)
1742{
1743	bool b_notify = p_ll2_conn->tx_queue.cur_send_packet->notify_fw;
1744	struct ecore_ll2_tx_queue *p_tx = &p_ll2_conn->tx_queue;
1745	struct ecore_ll2_tx_packet *p_pkt = OSAL_NULL;
1746	u16 bd_prod;
1747
1748	/* If there are missing BDs, don't do anything now */
1749	if (p_ll2_conn->tx_queue.cur_send_frag_num !=
1750	    p_ll2_conn->tx_queue.cur_send_packet->bd_used)
1751		return;
1752
1753	/* Push the current packet to the list and clean after it */
1754	OSAL_LIST_PUSH_TAIL(&p_ll2_conn->tx_queue.cur_send_packet->list_entry,
1755			    &p_ll2_conn->tx_queue.sending_descq);
1756	p_ll2_conn->tx_queue.cur_send_packet = OSAL_NULL;
1757	p_ll2_conn->tx_queue.cur_send_frag_num = 0;
1758
1759	/* Notify FW of packet only if requested to */
1760	if (!b_notify)
1761		return;
1762
1763	bd_prod = ecore_chain_get_prod_idx(&p_ll2_conn->tx_queue.txq_chain);
1764
1765	while (!OSAL_LIST_IS_EMPTY(&p_tx->sending_descq)) {
1766		p_pkt = OSAL_LIST_FIRST_ENTRY(&p_tx->sending_descq,
1767					      struct ecore_ll2_tx_packet,
1768					      list_entry);
1769		if (p_pkt == OSAL_NULL)
1770			break;
1771#if defined(_NTDDK_)
1772#pragma warning(suppress : 6011 28182)
1773#endif
1774		OSAL_LIST_REMOVE_ENTRY(&p_pkt->list_entry,
1775				       &p_tx->sending_descq);
1776		OSAL_LIST_PUSH_TAIL(&p_pkt->list_entry, &p_tx->active_descq);
1777	}
1778
1779	p_tx->db_msg.spq_prod = OSAL_CPU_TO_LE16(bd_prod);
1780
1781	/* Make sure the BDs data is updated before ringing the doorbell */
1782	OSAL_WMB(p_hwfn->p_dev);
1783
1784	//DIRECT_REG_WR(p_hwfn, p_tx->doorbell_addr, *((u32 *)&p_tx->db_msg));
1785	DIRECT_REG_WR_DB(p_hwfn, p_tx->doorbell_addr, *((u32 *)&p_tx->db_msg));
1786
1787	DP_VERBOSE(p_hwfn, (ECORE_MSG_TX_QUEUED | ECORE_MSG_LL2),
1788		   "LL2 [q 0x%02x cid 0x%08x type 0x%08x] Doorbelled [producer 0x%04x]\n",
1789		   p_ll2_conn->queue_id, p_ll2_conn->cid,
1790		   p_ll2_conn->input.conn_type,
1791		   p_tx->db_msg.spq_prod);
1792}
1793
1794enum _ecore_status_t ecore_ll2_prepare_tx_packet(
1795		void *cxt,
1796		u8 connection_handle,
1797		struct ecore_ll2_tx_pkt_info *pkt,
1798		bool notify_fw)
1799{
1800	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1801	struct ecore_ll2_tx_packet *p_curp = OSAL_NULL;
1802	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1803	struct ecore_ll2_tx_queue *p_tx;
1804	struct ecore_chain *p_tx_chain;
1805	unsigned long flags;
1806	enum _ecore_status_t rc = ECORE_SUCCESS;
1807
1808	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1809	if (p_ll2_conn == OSAL_NULL)
1810		return ECORE_INVAL;
1811	p_tx = &p_ll2_conn->tx_queue;
1812	p_tx_chain = &p_tx->txq_chain;
1813
1814	if (pkt->num_of_bds > p_ll2_conn->input.tx_max_bds_per_packet)
1815		return ECORE_IO; /* coalescing is requireed */
1816
1817	OSAL_SPIN_LOCK_IRQSAVE(&p_tx->lock, flags);
1818	if (p_tx->cur_send_packet) {
1819		rc = ECORE_EXISTS;
1820		goto out;
1821	}
1822
1823	/* Get entry, but only if we have tx elements for it */
1824	if (!OSAL_LIST_IS_EMPTY(&p_tx->free_descq))
1825		p_curp = OSAL_LIST_FIRST_ENTRY(&p_tx->free_descq,
1826					       struct ecore_ll2_tx_packet,
1827					       list_entry);
1828	if (p_curp && ecore_chain_get_elem_left(p_tx_chain) < pkt->num_of_bds)
1829		p_curp = OSAL_NULL;
1830
1831	if (!p_curp) {
1832		rc = ECORE_BUSY;
1833		goto out;
1834	}
1835
1836	/* Prepare packet and BD, and perhaps send a doorbell to FW */
1837	ecore_ll2_prepare_tx_packet_set(p_tx, p_curp, pkt, notify_fw);
1838
1839	ecore_ll2_prepare_tx_packet_set_bd(p_hwfn, p_ll2_conn, p_curp,
1840					   pkt);
1841
1842	ecore_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
1843
1844out:
1845	OSAL_SPIN_UNLOCK_IRQSAVE(&p_tx->lock, flags);
1846	return rc;
1847}
1848
1849enum _ecore_status_t ecore_ll2_set_fragment_of_tx_packet(void *cxt,
1850							 u8 connection_handle,
1851							 dma_addr_t addr,
1852							 u16 nbytes)
1853{
1854	struct ecore_ll2_tx_packet *p_cur_send_packet = OSAL_NULL;
1855	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1856	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1857	u16 cur_send_frag_num = 0;
1858	struct core_tx_bd *p_bd;
1859	unsigned long flags;
1860
1861	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1862	if (p_ll2_conn == OSAL_NULL)
1863		return ECORE_INVAL;
1864
1865	if (!p_ll2_conn->tx_queue.cur_send_packet)
1866		return ECORE_INVAL;
1867
1868	p_cur_send_packet = p_ll2_conn->tx_queue.cur_send_packet;
1869	cur_send_frag_num = p_ll2_conn->tx_queue.cur_send_frag_num;
1870
1871	if (cur_send_frag_num >= p_cur_send_packet->bd_used)
1872		return ECORE_INVAL;
1873
1874	/* Fill the BD information, and possibly notify FW */
1875	p_bd = p_cur_send_packet->bds_set[cur_send_frag_num].txq_bd;
1876	DMA_REGPAIR_LE(p_bd->addr, addr);
1877	p_bd->nbytes = OSAL_CPU_TO_LE16(nbytes);
1878	p_cur_send_packet->bds_set[cur_send_frag_num].tx_frag = addr;
1879	p_cur_send_packet->bds_set[cur_send_frag_num].frag_len = nbytes;
1880
1881	p_ll2_conn->tx_queue.cur_send_frag_num++;
1882
1883	OSAL_SPIN_LOCK_IRQSAVE(&p_ll2_conn->tx_queue.lock, flags);
1884	ecore_ll2_tx_packet_notify(p_hwfn, p_ll2_conn);
1885	OSAL_SPIN_UNLOCK_IRQSAVE(&p_ll2_conn->tx_queue.lock, flags);
1886
1887	return ECORE_SUCCESS;
1888}
1889
1890enum _ecore_status_t ecore_ll2_terminate_connection(void *cxt,
1891						    u8 connection_handle)
1892{
1893	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1894	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1895	enum _ecore_status_t rc = ECORE_NOTIMPL;
1896	struct ecore_ptt *p_ptt;
1897
1898	p_ptt = ecore_ptt_acquire(p_hwfn);
1899	if (!p_ptt)
1900		return ECORE_AGAIN;
1901
1902	p_ll2_conn = ecore_ll2_handle_sanity_lock(p_hwfn, connection_handle);
1903	if (p_ll2_conn == OSAL_NULL) {
1904		rc = ECORE_INVAL;
1905		goto out;
1906	}
1907
1908	/* Stop Tx & Rx of connection, if needed */
1909	if (ECORE_LL2_TX_REGISTERED(p_ll2_conn)) {
1910		rc = ecore_sp_ll2_tx_queue_stop(p_hwfn, p_ll2_conn);
1911		if (rc != ECORE_SUCCESS)
1912			goto out;
1913		ecore_ll2_txq_flush(p_hwfn, connection_handle);
1914	}
1915
1916	if (ECORE_LL2_RX_REGISTERED(p_ll2_conn)) {
1917		rc = ecore_sp_ll2_rx_queue_stop(p_hwfn, p_ll2_conn);
1918		if (rc)
1919			goto out;
1920		ecore_ll2_rxq_flush(p_hwfn, connection_handle);
1921	}
1922
1923	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_OOO)
1924		ecore_ooo_release_all_isles(p_hwfn->p_ooo_info);
1925
1926	if (p_ll2_conn->input.conn_type == ECORE_LL2_TYPE_FCOE) {
1927		if (!OSAL_TEST_BIT(ECORE_MF_UFP_SPECIFIC,
1928				   &p_hwfn->p_dev->mf_bits))
1929			ecore_llh_remove_protocol_filter(p_hwfn->p_dev, 0,
1930							 ECORE_LLH_FILTER_ETHERTYPE,
1931							 0x8906, 0);
1932		ecore_llh_remove_protocol_filter(p_hwfn->p_dev, 0,
1933						 ECORE_LLH_FILTER_ETHERTYPE,
1934						 0x8914, 0);
1935	}
1936
1937out:
1938	ecore_ptt_release(p_hwfn, p_ptt);
1939
1940	return rc;
1941}
1942
1943static void ecore_ll2_release_connection_ooo(struct ecore_hwfn *p_hwfn,
1944					     struct ecore_ll2_info *p_ll2_conn)
1945{
1946	struct ecore_ooo_buffer *p_buffer;
1947
1948	if (p_ll2_conn->input.conn_type != ECORE_LL2_TYPE_OOO)
1949		return;
1950
1951	ecore_ooo_release_all_isles(p_hwfn->p_ooo_info);
1952	while ((p_buffer = ecore_ooo_get_free_buffer(p_hwfn->p_ooo_info))) {
1953		OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
1954				       p_buffer->rx_buffer_virt_addr,
1955				       p_buffer->rx_buffer_phys_addr,
1956				       p_buffer->rx_buffer_size);
1957		OSAL_FREE(p_hwfn->p_dev, p_buffer);
1958	}
1959}
1960
1961void ecore_ll2_release_connection(void *cxt,
1962				  u8 connection_handle)
1963{
1964	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
1965	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
1966
1967	p_ll2_conn = ecore_ll2_handle_sanity(p_hwfn, connection_handle);
1968	if (p_ll2_conn == OSAL_NULL)
1969		return;
1970
1971	if (ECORE_LL2_RX_REGISTERED(p_ll2_conn)) {
1972		p_ll2_conn->rx_queue.b_cb_registred = false;
1973		ecore_int_unregister_cb(p_hwfn,
1974					p_ll2_conn->rx_queue.rx_sb_index);
1975	}
1976
1977	if (ECORE_LL2_TX_REGISTERED(p_ll2_conn)) {
1978		p_ll2_conn->tx_queue.b_cb_registred = false;
1979		ecore_int_unregister_cb(p_hwfn,
1980					p_ll2_conn->tx_queue.tx_sb_index);
1981	}
1982
1983	OSAL_FREE(p_hwfn->p_dev, p_ll2_conn->tx_queue.descq_array);
1984	ecore_chain_free(p_hwfn->p_dev, &p_ll2_conn->tx_queue.txq_chain);
1985
1986	OSAL_FREE(p_hwfn->p_dev, p_ll2_conn->rx_queue.descq_array);
1987	ecore_chain_free(p_hwfn->p_dev, &p_ll2_conn->rx_queue.rxq_chain);
1988	ecore_chain_free(p_hwfn->p_dev, &p_ll2_conn->rx_queue.rcq_chain);
1989
1990	ecore_cxt_release_cid(p_hwfn, p_ll2_conn->cid);
1991
1992	ecore_ll2_release_connection_ooo(p_hwfn, p_ll2_conn);
1993
1994	OSAL_MUTEX_ACQUIRE(&p_ll2_conn->mutex);
1995	p_ll2_conn->b_active = false;
1996	OSAL_MUTEX_RELEASE(&p_ll2_conn->mutex);
1997}
1998
1999/* ECORE LL2: internal functions */
2000
2001enum _ecore_status_t ecore_ll2_alloc(struct ecore_hwfn *p_hwfn)
2002{
2003	struct ecore_ll2_info *p_ll2_info;
2004	u8 i;
2005
2006	/* Allocate LL2's set struct */
2007	p_ll2_info = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
2008				 sizeof(struct ecore_ll2_info) *
2009				 ECORE_MAX_NUM_OF_LL2_CONNECTIONS);
2010	if (!p_ll2_info) {
2011		DP_NOTICE(p_hwfn, false,
2012			  "Failed to allocate `struct ecore_ll2'\n");
2013		return ECORE_NOMEM;
2014	}
2015
2016	p_hwfn->p_ll2_info = p_ll2_info;
2017
2018	for (i = 0; i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS; i++) {
2019#ifdef CONFIG_ECORE_LOCK_ALLOC
2020		if (OSAL_MUTEX_ALLOC(p_hwfn, &p_ll2_info[i].mutex))
2021			goto handle_err;
2022		if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_ll2_info[i].rx_queue.lock))
2023			goto handle_err;
2024		if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_ll2_info[i].tx_queue.lock))
2025			goto handle_err;
2026#endif
2027		p_ll2_info[i].my_id = i;
2028	}
2029
2030	return ECORE_SUCCESS;
2031#ifdef CONFIG_ECORE_LOCK_ALLOC
2032handle_err:
2033	ecore_ll2_free(p_hwfn);
2034	return ECORE_NOMEM;
2035#endif
2036}
2037
2038void ecore_ll2_setup(struct ecore_hwfn *p_hwfn)
2039{
2040	int  i;
2041
2042	for (i = 0; i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS; i++)
2043		OSAL_MUTEX_INIT(&p_hwfn->p_ll2_info[i].mutex);
2044}
2045
2046void ecore_ll2_free(struct ecore_hwfn *p_hwfn)
2047{
2048#ifdef CONFIG_ECORE_LOCK_ALLOC
2049	int i;
2050#endif
2051	if (!p_hwfn->p_ll2_info)
2052		return;
2053
2054#ifdef CONFIG_ECORE_LOCK_ALLOC
2055	for (i = 0; i < ECORE_MAX_NUM_OF_LL2_CONNECTIONS; i++) {
2056		OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->p_ll2_info[i].rx_queue.lock);
2057		OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->p_ll2_info[i].tx_queue.lock);
2058		OSAL_MUTEX_DEALLOC(&p_hwfn->p_ll2_info[i].mutex);
2059	}
2060#endif
2061	OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_ll2_info);
2062	p_hwfn->p_ll2_info = OSAL_NULL;
2063}
2064
2065static void _ecore_ll2_get_port_stats(struct ecore_hwfn *p_hwfn,
2066				      struct ecore_ptt *p_ptt,
2067				      struct ecore_ll2_stats *p_stats)
2068{
2069	struct core_ll2_port_stats port_stats;
2070
2071	OSAL_MEMSET(&port_stats, 0, sizeof(port_stats));
2072	ecore_memcpy_from(p_hwfn, p_ptt, &port_stats,
2073			  BAR0_MAP_REG_TSDM_RAM +
2074			  TSTORM_LL2_PORT_STAT_OFFSET(MFW_PORT(p_hwfn)),
2075			  sizeof(port_stats));
2076
2077	p_stats->gsi_invalid_hdr +=
2078		HILO_64_REGPAIR(port_stats.gsi_invalid_hdr);
2079	p_stats->gsi_invalid_pkt_length +=
2080		HILO_64_REGPAIR(port_stats.gsi_invalid_pkt_length);
2081	p_stats->gsi_unsupported_pkt_typ +=
2082		HILO_64_REGPAIR(port_stats.gsi_unsupported_pkt_typ);
2083	p_stats->gsi_crcchksm_error +=
2084		HILO_64_REGPAIR(port_stats.gsi_crcchksm_error);
2085}
2086
2087static void _ecore_ll2_get_tstats(struct ecore_hwfn *p_hwfn,
2088				  struct ecore_ptt *p_ptt,
2089				  struct ecore_ll2_info *p_ll2_conn,
2090				  struct ecore_ll2_stats *p_stats)
2091{
2092	struct core_ll2_tstorm_per_queue_stat tstats;
2093	u8 qid = p_ll2_conn->queue_id;
2094	u32 tstats_addr;
2095
2096	OSAL_MEMSET(&tstats, 0, sizeof(tstats));
2097	tstats_addr = BAR0_MAP_REG_TSDM_RAM +
2098		      CORE_LL2_TSTORM_PER_QUEUE_STAT_OFFSET(qid);
2099	ecore_memcpy_from(p_hwfn, p_ptt, &tstats,
2100			  tstats_addr,
2101			  sizeof(tstats));
2102
2103	p_stats->packet_too_big_discard +=
2104		HILO_64_REGPAIR(tstats.packet_too_big_discard);
2105	p_stats->no_buff_discard +=
2106		HILO_64_REGPAIR(tstats.no_buff_discard);
2107}
2108
2109static void _ecore_ll2_get_ustats(struct ecore_hwfn *p_hwfn,
2110				  struct ecore_ptt *p_ptt,
2111				  struct ecore_ll2_info *p_ll2_conn,
2112				  struct ecore_ll2_stats *p_stats)
2113{
2114	struct core_ll2_ustorm_per_queue_stat ustats;
2115	u8 qid = p_ll2_conn->queue_id;
2116	u32 ustats_addr;
2117
2118	OSAL_MEMSET(&ustats, 0, sizeof(ustats));
2119	ustats_addr = BAR0_MAP_REG_USDM_RAM +
2120		      CORE_LL2_USTORM_PER_QUEUE_STAT_OFFSET(qid);
2121	ecore_memcpy_from(p_hwfn, p_ptt, &ustats,
2122			  ustats_addr,
2123			  sizeof(ustats));
2124
2125	p_stats->rcv_ucast_bytes += HILO_64_REGPAIR(ustats.rcv_ucast_bytes);
2126	p_stats->rcv_mcast_bytes += HILO_64_REGPAIR(ustats.rcv_mcast_bytes);
2127	p_stats->rcv_bcast_bytes += HILO_64_REGPAIR(ustats.rcv_bcast_bytes);
2128	p_stats->rcv_ucast_pkts += HILO_64_REGPAIR(ustats.rcv_ucast_pkts);
2129	p_stats->rcv_mcast_pkts += HILO_64_REGPAIR(ustats.rcv_mcast_pkts);
2130	p_stats->rcv_bcast_pkts += HILO_64_REGPAIR(ustats.rcv_bcast_pkts);
2131}
2132
2133static void _ecore_ll2_get_pstats(struct ecore_hwfn *p_hwfn,
2134				  struct ecore_ptt *p_ptt,
2135				  struct ecore_ll2_info *p_ll2_conn,
2136				  struct ecore_ll2_stats *p_stats)
2137{
2138	struct core_ll2_pstorm_per_queue_stat pstats;
2139	u8 stats_id = p_ll2_conn->tx_stats_id;
2140	u32 pstats_addr;
2141
2142	OSAL_MEMSET(&pstats, 0, sizeof(pstats));
2143	pstats_addr = BAR0_MAP_REG_PSDM_RAM +
2144		      CORE_LL2_PSTORM_PER_QUEUE_STAT_OFFSET(stats_id);
2145	ecore_memcpy_from(p_hwfn, p_ptt, &pstats,
2146			  pstats_addr,
2147			  sizeof(pstats));
2148
2149	p_stats->sent_ucast_bytes += HILO_64_REGPAIR(pstats.sent_ucast_bytes);
2150	p_stats->sent_mcast_bytes += HILO_64_REGPAIR(pstats.sent_mcast_bytes);
2151	p_stats->sent_bcast_bytes += HILO_64_REGPAIR(pstats.sent_bcast_bytes);
2152	p_stats->sent_ucast_pkts += HILO_64_REGPAIR(pstats.sent_ucast_pkts);
2153	p_stats->sent_mcast_pkts += HILO_64_REGPAIR(pstats.sent_mcast_pkts);
2154	p_stats->sent_bcast_pkts += HILO_64_REGPAIR(pstats.sent_bcast_pkts);
2155}
2156
2157enum _ecore_status_t __ecore_ll2_get_stats(void *cxt,
2158					   u8 connection_handle,
2159					   struct ecore_ll2_stats *p_stats)
2160{
2161	struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)cxt;
2162	struct ecore_ll2_info *p_ll2_conn = OSAL_NULL;
2163	struct ecore_ptt *p_ptt;
2164
2165	if ((connection_handle >= ECORE_MAX_NUM_OF_LL2_CONNECTIONS) ||
2166	    !p_hwfn->p_ll2_info) {
2167		return ECORE_INVAL;
2168	}
2169
2170	p_ll2_conn = &p_hwfn->p_ll2_info[connection_handle];
2171
2172	p_ptt = ecore_ptt_acquire(p_hwfn);
2173	if (!p_ptt) {
2174		DP_ERR(p_hwfn, "Failed to acquire ptt\n");
2175		return ECORE_INVAL;
2176	}
2177
2178	if (p_ll2_conn->input.gsi_enable)
2179		_ecore_ll2_get_port_stats(p_hwfn, p_ptt, p_stats);
2180
2181	_ecore_ll2_get_tstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2182
2183	_ecore_ll2_get_ustats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2184
2185	if (p_ll2_conn->tx_stats_en)
2186		_ecore_ll2_get_pstats(p_hwfn, p_ptt, p_ll2_conn, p_stats);
2187
2188	ecore_ptt_release(p_hwfn, p_ptt);
2189
2190	return ECORE_SUCCESS;
2191}
2192
2193enum _ecore_status_t ecore_ll2_get_stats(void *cxt,
2194					 u8 connection_handle,
2195					 struct ecore_ll2_stats	*p_stats)
2196{
2197	OSAL_MEMSET(p_stats, 0, sizeof(*p_stats));
2198
2199	return __ecore_ll2_get_stats(cxt, connection_handle, p_stats);
2200}
2201
2202/**/
2203
2204#ifdef _NTDDK_
2205#pragma warning(pop)
2206#endif
2207