1 #ifndef ECORE_ERASE
2 #ifdef __LINUX
3 
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <asm/byteorder.h>
9 #include <linux/version.h>
10 #include <linux/module.h>
11 #include <linux/crc32.h>
12 #include <linux/etherdevice.h>
13 
14 #define ECORE_ALIGN(x, a) ALIGN(x, a)
15 #endif
16 
17 /* Always define ECORE_OOO for VBD */
18 #define ECORE_OOO
19 
20 #include "bcmtype.h"
21 #include "utils.h"
22 #include "lm5710.h"
23 #include "ecore_sp_verbs.h"
24 #include "command.h"
25 #include "debug.h"
26 #include "ecore_common.h"
27 
28 /************************ Debug print macros **********************************/
29 #if !defined(UEFI) && defined(DBG)
30 #define ECORE_MSG(pdev, m, ...) \
31 	DbgMessage(pdev, WARNi, m, ##__VA_ARGS__)
32 #else
33 #define ECORE_MSG
34 #endif
35 
36 /************************ Error prints ****************************************/
37 #if !defined(UEFI) && defined(DBG)
38 #define ECORE_ERR(str, ...) DbgMessage(pdev, FATAL, str, ##__VA_ARGS__)
39 #else
40 #define ECORE_ERR
41 #endif
42 
43 
44 /***********************  ECORE WRAPPER MACROS ********************************/
45 
46 #define ECORE_RET_PENDING(pending_bit, pending) \
47 	(ECORE_TEST_BIT(pending_bit, pending) ? ECORE_PENDING : ECORE_SUCCESS)
48 
49 #define ECORE_ZALLOC(_size, _flags, _pdev) mm_rt_zalloc_mem(_pdev, _size)
50 #define ECORE_CALLOC(_len, _size, _flags, _pdev) mm_rt_zalloc_mem(_pdev, _len * _size)
51 #define ECORE_FREE(_pdev, _buf, _size) mm_rt_free_mem(_pdev, _buf, _size, 0)
52 
53 /*
54  *  Ecore implementation of set/get flag
55  *  (differs from VBD set_flags, get_flags)
56  */
57 #define ECORE_SET_FLAG(value, mask, flag) \
58 	do {\
59 		(value) &= ~(mask);\
60 		(value) |= ((flag) << (mask##_SHIFT));\
61 	} while (0)
62 
63 #define ECORE_GET_FLAG(value, mask) \
64 	(((value) &= (mask)) >> (mask##_SHIFT))
65 
66 #define ecore_sp_post(_pdev, _cmd , _cid, _data, _con_type) \
67 	lm_sq_post(_pdev, _cid, (u8)(_cmd), CMD_PRIORITY_NORMAL, _con_type, \
68 	_data)
69 
70 #define ECORE_SET_CTX_VALIDATION(_pdev, _cxt, _cid) \
71 	lm_set_cdu_validation_data(_pdev, _cid, FALSE) /* context? type? */
72 /************************ TODO for LM people!!! *******************************/
73 #define ECORE_TODO_UPDATE_COALESCE_SB_INDEX(a1, a2, a3, a4, a5)
74 #define ECORE_TODO_LINK_REPORT(pdev)
75 #define ECORE_TODO_FW_COMMAND(_pdev, _drv_msg_code, _val) (-1)
76 
77 /************************ Lists ***********************************************/
78 #define ECORE_LIST_FOR_EACH_ENTRY(pos, _head, _link, cast) \
79 	for (pos = (cast *)d_list_peek_head(_head); \
80 	     pos; \
81 	     pos = (cast *)d_list_next_entry(&pos->_link))
82 
83 /**
84  * ECORE_LIST_FOR_EACH_ENTRY_SAFE - iterate over list of given type
85  * @pos:        the type * to use as a loop cursor.
86  * @n:          another type * to use as temporary storage
87  * @head:       the head for your list.
88  * @member:     the name of the list_struct within the struct.
89  *
90  * iterate over list of given type safe against removal of list entry
91  */
92 #define ECORE_LIST_FOR_EACH_ENTRY_SAFE(pos, n, head, member, cast)   \
93 	 for (pos = (cast *)d_list_peek_head(head), \
94 	      n = (pos) ? (cast *)d_list_next_entry(&pos->member) : NULL; \
95 	      pos != NULL;  \
96 	      pos = (cast *)n, \
97 	      n = (pos) ? (cast *)d_list_next_entry(&pos->member) : NULL)
98 
99 #define ECORE_LIST_IS_LAST(_link, _list)                (_link == (_list)->tail)
100 
101 #define ECORE_LIST_IS_EMPTY(head)                       \
102 	d_list_is_empty(head)
103 
104 #define ECORE_LIST_FIRST_ENTRY(head, cast, link)	\
105 	(cast *)d_list_peek_head(head)
106 
107 #define ECORE_LIST_NEXT(pos, link, cast)	\
108 	(cast *)d_list_next_entry(&((pos)->link))
109 
110 #define ECORE_LIST_INIT(head)				\
111 do { \
112 	d_list_clear(head); \
113 } while (0)
114 
115 #define ECORE_LIST_PUSH_TAIL(link, head)		\
116 do { \
117 	d_list_push_tail(head, link); \
118 } while (0)
119 
120 #define ECORE_LIST_PUSH_HEAD(link, head)		\
121 do { \
122 	d_list_push_head(head, link); \
123 } while (0)
124 
125 #define ECORE_LIST_REMOVE_ENTRY(link, head)		\
126 do { \
127 	d_list_remove_entry(head, link); \
128 } while (0)
129 
130 #define ECORE_LIST_SPLICE_INIT(new_head, head) \
131 do { \
132 	d_list_add_head(head, new_head); \
133 	d_list_clear(new_head); \
134 } while (0)
135 
ecore_crc32_le(u32_t seed,u8_t * mac,u32_t len)136 static __inline u32_t ecore_crc32_le(u32_t seed, u8_t *mac, u32_t len)
137 {
138 	u32_t packet_buf[2] = {0};
139 
140 	memcpy(((u8_t *)(&packet_buf[0]))+2, &mac[0], 2);
141 	memcpy(&packet_buf[1], &mac[2], 4);
142 	return SWAP_BYTES32(calc_crc32((u8_t *)packet_buf, 8, seed, 0));
143 }
144 
145 /************************ Per compilation target ******************************/
146 #ifdef __LINUX
147 
148 #define ECORE_UNLIKELY	unlikely
149 #define ECORE_LIKELY	likely
150 
151 #define ecore_atomic_read		mm_atomic_read
152 #define ecore_atomic_cmpxchg		mm_atomic_cmpxchg
153 #define ecore_atomic_set(a, v)		mm_atomic_set((u32_t *)(a), v)
154 #define smp_mb__before_atomic() mm_barrier()
155 #define smp_mb__after_atomic()  mm_barrier()
156 
157 /* Other */
158 #define ECORE_IS_VALID_ETHER_ADDR(_mac)               is_valid_ether_addr(_mac)
159 #define ECORE_SET_WAIT_COUNT(_cnt)
160 #define ECORE_SET_WAIT_DELAY_US(_cnt, _delay_us)
161 
162 /* Mutex related */
163 #define ECORE_MUTEX_INIT(_mutex)	mutex_init(_mutex)
164 #define ECORE_MUTEX_LOCK(_mutex)	mutex_lock(_mutex)
165 #define ECORE_MUTEX_UNLOCK(_mutex)	mutex_unlock(_mutex)
166 
167 #define ECORE_MIGHT_SLEEP() ediag_might_sleep()
168 #define ECORE_TEST_BIT(bit, var)  test_bit(bit, var)
169 #define ECORE_TEST_AND_CLEAR_BIT(bit, var) test_and_clear_bit(bit, var)
170 
171 #else /* ! LINUX */
172 
173 typedef u16 __le16;
174 
175 #define ecore_atomic_read		mm_atomic_read
176 #define ecore_atomic_cmpxchg		mm_atomic_cmpxchg
177 #define ecore_atomic_set(a, val)	mm_atomic_set((u32_t *)(a), val)
178 
179 #define ECORE_UNLIKELY(x)	(x)
180 #define ECORE_LIKELY(x)		(x)
181 #define BUG() DbgBreakMsg("Bug")
182 #define smp_mb()                   mm_barrier()
183 #define smp_mb__before_atomic() mm_barrier()
184 #define smp_mb__after_atomic()  mm_barrier()
185 #define mb()                       mm_barrier()
186 #define wmb()                      mm_barrier()
187 #define mmiowb()		   mm_barrier()
188 
189 #define ECORE_MIGHT_SLEEP() /* IRQL_PASSIVE_CODE() */
190 
191 /* Mutex related */
192 #define ECORE_MUTEX_INIT(_mutex)
193 #define ECORE_MUTEX_LOCK(_mutex)
194 #define ECORE_MUTEX_UNLOCK(_mutex)
195 
196 /* Atomic Bit Manipulation */
197 #define ECORE_TEST_BIT(_bit, _var) \
198 	(mm_atomic_long_read(_var) & (1 << (_bit)))
199 
200 /* Other */
201 #define ECORE_IS_VALID_ETHER_ADDR(_mac)         TRUE
202 #define ECORE_SET_WAIT_DELAY_US(_cnt, _delay_us) \
203 do { \
204 	_delay_us = (_cnt >= 2360) ? 100 : 25000; \
205 } while (0)
206 
207 /*
208  * In VBD We'll wait 10,000 times 100us (1 second) +
209  * 2360 times 25000us (59sec) = total 60 sec
210  * (Winodws only note) the 25000 wait will cause
211  * wait to be without CPU stall (look in win_util.c)
212  */
213 #define ECORE_SET_WAIT_COUNT(_cnt) \
214 do { \
215 	_cnt = 10000 + 2360; \
216 } while (0)
217 
ECORE_TEST_AND_CLEAR_BIT(int bit,unsigned long * vec)218 static __inline BOOL ECORE_TEST_AND_CLEAR_BIT(int bit, unsigned long *vec)
219 {
220 	BOOL set = ECORE_TEST_BIT(bit, vec);
221 	ECORE_CLEAR_BIT(bit, vec);
222 
223 	return set;
224 }
225 
226 #endif /* END if "per LM target type" */
227 
228 /* Spin lock related */
229 #define ECORE_SPIN_LOCK_INIT(_spin, _pdev)	mm_init_lock(_pdev, _spin)
230 #define ECORE_SPIN_LOCK_BH(_spin)		mm_acquire_lock(_spin)
231 #define ECORE_SPIN_UNLOCK_BH(_spin)		mm_release_lock(_spin)
232 
233 #endif /* not ECORE_ERASE */
234 #if defined(__FreeBSD__) && !defined(NOT_LINUX)
235 #include "bxe.h"
236 #include "ecore_init.h"
237 #elif !defined(EDIAG)
238 #ifdef ECORE_ERASE
239 #include <linux/version.h>
240 #include <linux/module.h>
241 #include <linux/crc32.h>
242 #include <linux/netdevice.h>
243 #include <linux/etherdevice.h>
244 #if (LINUX_VERSION_CODE >= 0x02061b) && !defined(BNX2X_DRIVER_DISK) && !defined(__VMKLNX__) /* BNX2X_UPSTREAM */
245 #include <linux/crc32c.h>
246 #endif
247 #include "bnx2x.h"
248 #include "bnx2x_cmn.h"
249 #include "bnx2x_sp.h"
250 
251 #define ECORE_MAX_EMUL_MULTI		16
252 #endif
253 #endif
254 
255 /**** Exe Queue interfaces ****/
256 
257 /**
258  * ecore_exe_queue_init - init the Exe Queue object
259  *
260  * @o:		pointer to the object
261  * @exe_len:	length
262  * @owner:	pointer to the owner
263  * @validate:	validate function pointer
264  * @optimize:	optimize function pointer
265  * @exec:	execute function pointer
266  * @get:	get function pointer
267  */
ecore_exe_queue_init(struct _lm_device_t * pdev,struct ecore_exe_queue_obj * o,int exe_len,union ecore_qable_obj * owner,exe_q_validate validate,exe_q_remove remove,exe_q_optimize optimize,exe_q_execute exec,exe_q_get get)268 static INLINE void ecore_exe_queue_init(struct _lm_device_t *pdev,
269 					struct ecore_exe_queue_obj *o,
270 					int exe_len,
271 					union ecore_qable_obj *owner,
272 					exe_q_validate validate,
273 					exe_q_remove remove,
274 					exe_q_optimize optimize,
275 					exe_q_execute exec,
276 					exe_q_get get)
277 {
278 	mm_memset(o, 0, sizeof(*o));
279 
280 	ECORE_LIST_INIT(&o->exe_queue);
281 	ECORE_LIST_INIT(&o->pending_comp);
282 
283 	ECORE_SPIN_LOCK_INIT(&o->lock, pdev);
284 
285 	o->exe_chunk_len = exe_len;
286 	o->owner         = owner;
287 
288 	/* Owner specific callbacks */
289 	o->validate      = validate;
290 	o->remove        = remove;
291 	o->optimize      = optimize;
292 	o->execute       = exec;
293 	o->get           = get;
294 
295 	ECORE_MSG(pdev, "Setup the execution queue with the chunk length of %d\n",
296 		  exe_len);
297 }
298 
ecore_exe_queue_free_elem(struct _lm_device_t * pdev,struct ecore_exeq_elem * elem)299 static INLINE void ecore_exe_queue_free_elem(struct _lm_device_t *pdev,
300 					     struct ecore_exeq_elem *elem)
301 {
302 	ECORE_MSG(pdev, "Deleting an exe_queue element\n");
303 	ECORE_FREE(pdev, elem, sizeof(*elem));
304 }
305 
ecore_exe_queue_length(struct ecore_exe_queue_obj * o)306 static INLINE int ecore_exe_queue_length(struct ecore_exe_queue_obj *o)
307 {
308 	struct ecore_exeq_elem *elem;
309 	int cnt = 0;
310 
311 #ifdef ECORE_ERASE
312 	spin_lock_bh(&o->lock);
313 #endif
314 
315 	ECORE_LIST_FOR_EACH_ENTRY(elem, &o->exe_queue, link,
316 				  struct ecore_exeq_elem)
317 		cnt++;
318 
319 #ifdef ECORE_ERASE
320 	spin_unlock_bh(&o->lock);
321 #endif
322 
323 	return cnt;
324 }
325 
326 /**
327  * ecore_exe_queue_add - add a new element to the execution queue
328  *
329  * @pdev:	driver handle
330  * @o:		queue
331  * @cmd:	new command to add
332  * @restore:	true - do not optimize the command
333  *
334  * If the element is optimized or is illegal, frees it.
335  */
ecore_exe_queue_add(struct _lm_device_t * pdev,struct ecore_exe_queue_obj * o,struct ecore_exeq_elem * elem,BOOL restore)336 static INLINE int ecore_exe_queue_add(struct _lm_device_t *pdev,
337 				      struct ecore_exe_queue_obj *o,
338 				      struct ecore_exeq_elem *elem,
339 				      BOOL restore)
340 {
341 	int rc;
342 
343 	ECORE_SPIN_LOCK_BH(&o->lock);
344 
345 	if (!restore) {
346 		/* Try to cancel this element queue */
347 		rc = o->optimize(pdev, o->owner, elem);
348 		if (rc)
349 			goto free_and_exit;
350 
351 		/* Check if this request is ok */
352 		rc = o->validate(pdev, o->owner, elem);
353 		if (rc) {
354 			ECORE_MSG(pdev, "Preamble failed: %d\n", rc);
355 			goto free_and_exit;
356 		}
357 	}
358 
359 	/* If so, add it to the execution queue */
360 	ECORE_LIST_PUSH_TAIL(&elem->link, &o->exe_queue);
361 
362 	ECORE_SPIN_UNLOCK_BH(&o->lock);
363 
364 	return ECORE_SUCCESS;
365 
366 free_and_exit:
367 	ecore_exe_queue_free_elem(pdev, elem);
368 
369 	ECORE_SPIN_UNLOCK_BH(&o->lock);
370 
371 	return rc;
372 }
373 
__ecore_exe_queue_reset_pending(struct _lm_device_t * pdev,struct ecore_exe_queue_obj * o)374 static INLINE void __ecore_exe_queue_reset_pending(
375 	struct _lm_device_t *pdev,
376 	struct ecore_exe_queue_obj *o)
377 {
378 	struct ecore_exeq_elem *elem;
379 
380 	while (!ECORE_LIST_IS_EMPTY(&o->pending_comp)) {
381 		elem = ECORE_LIST_FIRST_ENTRY(&o->pending_comp,
382 					      struct ecore_exeq_elem,
383 					      link);
384 
385 		ECORE_LIST_REMOVE_ENTRY(&elem->link, &o->pending_comp);
386 		ecore_exe_queue_free_elem(pdev, elem);
387 	}
388 }
389 
390 /**
391  * ecore_exe_queue_step - execute one execution chunk atomically
392  *
393  * @pdev:		driver handle
394  * @o:			queue
395  * @ramrod_flags:	flags
396  *
397  * (Should be called while holding the exe_queue->lock).
398  */
ecore_exe_queue_step(struct _lm_device_t * pdev,struct ecore_exe_queue_obj * o,unsigned long * ramrod_flags)399 static INLINE int ecore_exe_queue_step(struct _lm_device_t *pdev,
400 				       struct ecore_exe_queue_obj *o,
401 				       unsigned long *ramrod_flags)
402 {
403 	struct ecore_exeq_elem *elem, spacer;
404 	int cur_len = 0, rc;
405 
406 	mm_memset(&spacer, 0, sizeof(spacer));
407 
408 	/* Next step should not be performed until the current is finished,
409 	 * unless a DRV_CLEAR_ONLY bit is set. In this case we just want to
410 	 * properly clear object internals without sending any command to the FW
411 	 * which also implies there won't be any completion to clear the
412 	 * 'pending' list.
413 	 */
414 	if (!ECORE_LIST_IS_EMPTY(&o->pending_comp)) {
415 		if (ECORE_TEST_BIT(RAMROD_DRV_CLR_ONLY, ramrod_flags)) {
416 			ECORE_MSG(pdev, "RAMROD_DRV_CLR_ONLY requested: resetting a pending_comp list\n");
417 			__ecore_exe_queue_reset_pending(pdev, o);
418 		} else {
419 			return ECORE_PENDING;
420 		}
421 	}
422 
423 	/* Run through the pending commands list and create a next
424 	 * execution chunk.
425 	 */
426 	while (!ECORE_LIST_IS_EMPTY(&o->exe_queue)) {
427 		elem = ECORE_LIST_FIRST_ENTRY(&o->exe_queue,
428 					      struct ecore_exeq_elem,
429 					      link);
430 		DbgBreakIf(!elem->cmd_len);
431 
432 		if (cur_len + elem->cmd_len <= o->exe_chunk_len) {
433 			cur_len += elem->cmd_len;
434 			/* Prevent from both lists being empty when moving an
435 			 * element. This will allow the call of
436 			 * ecore_exe_queue_empty() without locking.
437 			 */
438 			ECORE_LIST_PUSH_TAIL(&spacer.link, &o->pending_comp);
439 			mb();
440 			ECORE_LIST_REMOVE_ENTRY(&elem->link, &o->exe_queue);
441 			ECORE_LIST_PUSH_TAIL(&elem->link, &o->pending_comp);
442 			ECORE_LIST_REMOVE_ENTRY(&spacer.link, &o->pending_comp);
443 		} else
444 			break;
445 	}
446 
447 	/* Sanity check */
448 	if (!cur_len)
449 		return ECORE_SUCCESS;
450 
451 	rc = o->execute(pdev, o->owner, &o->pending_comp, ramrod_flags);
452 	if (rc < 0)
453 		/* In case of an error return the commands back to the queue
454 		 *  and reset the pending_comp.
455 		 */
456 		ECORE_LIST_SPLICE_INIT(&o->pending_comp, &o->exe_queue);
457 	else if (!rc)
458 		/* If zero is returned, means there are no outstanding pending
459 		 * completions and we may dismiss the pending list.
460 		 */
461 		__ecore_exe_queue_reset_pending(pdev, o);
462 
463 	return rc;
464 }
465 
ecore_exe_queue_empty(struct ecore_exe_queue_obj * o)466 static INLINE BOOL ecore_exe_queue_empty(struct ecore_exe_queue_obj *o)
467 {
468 	BOOL empty = ECORE_LIST_IS_EMPTY(&o->exe_queue);
469 
470 	/* Don't reorder!!! */
471 	mb();
472 
473 	return empty && ECORE_LIST_IS_EMPTY(&o->pending_comp);
474 }
475 
ecore_exe_queue_alloc_elem(struct _lm_device_t * pdev)476 static INLINE struct ecore_exeq_elem *ecore_exe_queue_alloc_elem(
477 	struct _lm_device_t *pdev)
478 {
479 	ECORE_MSG(pdev, "Allocating a new exe_queue element\n");
480 	return ECORE_ZALLOC(sizeof(struct ecore_exeq_elem), GFP_ATOMIC,
481 			    pdev);
482 }
483 
484 /************************ raw_obj functions ***********************************/
ecore_raw_check_pending(struct ecore_raw_obj * o)485 static BOOL ecore_raw_check_pending(struct ecore_raw_obj *o)
486 {
487 	/*
488      * !! converts the value returned by ECORE_TEST_BIT such that it
489      * is guaranteed not to be truncated regardless of BOOL definition.
490 	 *
491 	 * Note we cannot simply define the function's return value type
492      * to match the type returned by ECORE_TEST_BIT, as it varies by
493      * platform/implementation.
494 	 */
495 
496 	return !!ECORE_TEST_BIT(o->state, o->pstate);
497 }
498 
ecore_raw_clear_pending(struct ecore_raw_obj * o)499 static void ecore_raw_clear_pending(struct ecore_raw_obj *o)
500 {
501 	smp_mb__before_atomic();
502 	ECORE_CLEAR_BIT(o->state, o->pstate);
503 	smp_mb__after_atomic();
504 }
505 
ecore_raw_set_pending(struct ecore_raw_obj * o)506 static void ecore_raw_set_pending(struct ecore_raw_obj *o)
507 {
508 	smp_mb__before_atomic();
509 	ECORE_SET_BIT(o->state, o->pstate);
510 	smp_mb__after_atomic();
511 }
512 
513 /**
514  * ecore_state_wait - wait until the given bit(state) is cleared
515  *
516  * @pdev:	device handle
517  * @state:	state which is to be cleared
518  * @state_p:	state buffer
519  *
520  */
ecore_state_wait(struct _lm_device_t * pdev,int state,unsigned long * pstate)521 static INLINE int ecore_state_wait(struct _lm_device_t *pdev, int state,
522 				   unsigned long *pstate)
523 {
524 	/* can take a while if any port is running */
525 	int cnt = 5000;
526 
527 #ifndef ECORE_ERASE
528 	int delay_us = 1000;
529 
530 	/* In VBD We'll wait 10,000 times 100us (1 second) +
531 	* 2360 times 25000us (59sec) = total 60 sec
532 	* (Winodws only note) the 25000 wait will cause wait
533 	* to be without CPU stall (look in win_util.c)
534 	*/
535 	cnt = 10000 + 2360;
536 #endif
537 
538 	if (CHIP_REV_IS_EMUL(pdev))
539 		cnt *= 20;
540 
541 	ECORE_MSG(pdev, "waiting for state to become %d\n", state);
542 
543 	ECORE_MIGHT_SLEEP();
544 	while (cnt--) {
545 		if (!ECORE_TEST_BIT(state, pstate)) {
546 #ifdef ECORE_STOP_ON_ERROR
547 			ECORE_MSG(pdev, "exit  (cnt %d)\n", 5000 - cnt);
548 #endif
549 			return ECORE_SUCCESS;
550 		}
551 
552 #ifndef ECORE_ERASE
553 		/* in case reset is in progress we won't get completion */
554 		if (lm_reset_is_inprogress(pdev))
555 			return 0;
556 
557 		delay_us = (cnt >= 2360) ? 100 : 25000;
558 #endif
559 		mm_wait(pdev, delay_us);
560 
561 		if (pdev->panic)
562 			return ECORE_IO;
563 	}
564 
565 	/* timeout! */
566 	ECORE_ERR("timeout waiting for state %d\n", state);
567 #ifdef ECORE_STOP_ON_ERROR
568 	ecore_panic();
569 #endif
570 
571 	return ECORE_TIMEOUT;
572 }
573 
ecore_raw_wait(struct _lm_device_t * pdev,struct ecore_raw_obj * raw)574 static int ecore_raw_wait(struct _lm_device_t *pdev, struct ecore_raw_obj *raw)
575 {
576 	return ecore_state_wait(pdev, raw->state, raw->pstate);
577 }
578 
579 /***************** Classification verbs: Set/Del MAC/VLAN/VLAN-MAC ************/
580 /* credit handling callbacks */
ecore_get_cam_offset_mac(struct ecore_vlan_mac_obj * o,int * offset)581 static BOOL ecore_get_cam_offset_mac(struct ecore_vlan_mac_obj *o, int *offset)
582 {
583 	struct ecore_credit_pool_obj *mp = o->macs_pool;
584 
585 	DbgBreakIf(!mp);
586 
587 	return mp->get_entry(mp, offset);
588 }
589 
ecore_get_credit_mac(struct ecore_vlan_mac_obj * o)590 static BOOL ecore_get_credit_mac(struct ecore_vlan_mac_obj *o)
591 {
592 	struct ecore_credit_pool_obj *mp = o->macs_pool;
593 
594 	DbgBreakIf(!mp);
595 
596 	return mp->get(mp, 1);
597 }
598 
ecore_get_cam_offset_vlan(struct ecore_vlan_mac_obj * o,int * offset)599 static BOOL ecore_get_cam_offset_vlan(struct ecore_vlan_mac_obj *o, int *offset)
600 {
601 	struct ecore_credit_pool_obj *vp = o->vlans_pool;
602 
603 	DbgBreakIf(!vp);
604 
605 	return vp->get_entry(vp, offset);
606 }
607 
ecore_get_credit_vlan(struct ecore_vlan_mac_obj * o)608 static BOOL ecore_get_credit_vlan(struct ecore_vlan_mac_obj *o)
609 {
610 	struct ecore_credit_pool_obj *vp = o->vlans_pool;
611 
612 	DbgBreakIf(!vp);
613 
614 	return vp->get(vp, 1);
615 }
616 
ecore_get_credit_vlan_mac(struct ecore_vlan_mac_obj * o)617 static BOOL ecore_get_credit_vlan_mac(struct ecore_vlan_mac_obj *o)
618 {
619 	struct ecore_credit_pool_obj *mp = o->macs_pool;
620 	struct ecore_credit_pool_obj *vp = o->vlans_pool;
621 
622 	if (!mp->get(mp, 1))
623 		return FALSE;
624 
625 	if (!vp->get(vp, 1)) {
626 		mp->put(mp, 1);
627 		return FALSE;
628 	}
629 
630 	return TRUE;
631 }
632 
ecore_put_cam_offset_mac(struct ecore_vlan_mac_obj * o,int offset)633 static BOOL ecore_put_cam_offset_mac(struct ecore_vlan_mac_obj *o, int offset)
634 {
635 	struct ecore_credit_pool_obj *mp = o->macs_pool;
636 
637 	return mp->put_entry(mp, offset);
638 }
639 
ecore_put_credit_mac(struct ecore_vlan_mac_obj * o)640 static BOOL ecore_put_credit_mac(struct ecore_vlan_mac_obj *o)
641 {
642 	struct ecore_credit_pool_obj *mp = o->macs_pool;
643 
644 	return mp->put(mp, 1);
645 }
646 
ecore_put_cam_offset_vlan(struct ecore_vlan_mac_obj * o,int offset)647 static BOOL ecore_put_cam_offset_vlan(struct ecore_vlan_mac_obj *o, int offset)
648 {
649 	struct ecore_credit_pool_obj *vp = o->vlans_pool;
650 
651 	return vp->put_entry(vp, offset);
652 }
653 
ecore_put_credit_vlan(struct ecore_vlan_mac_obj * o)654 static BOOL ecore_put_credit_vlan(struct ecore_vlan_mac_obj *o)
655 {
656 	struct ecore_credit_pool_obj *vp = o->vlans_pool;
657 
658 	return vp->put(vp, 1);
659 }
660 
ecore_put_credit_vlan_mac(struct ecore_vlan_mac_obj * o)661 static BOOL ecore_put_credit_vlan_mac(struct ecore_vlan_mac_obj *o)
662 {
663 	struct ecore_credit_pool_obj *mp = o->macs_pool;
664 	struct ecore_credit_pool_obj *vp = o->vlans_pool;
665 
666 	if (!mp->put(mp, 1))
667 		return FALSE;
668 
669 	if (!vp->put(vp, 1)) {
670 		mp->get(mp, 1);
671 		return FALSE;
672 	}
673 
674 	return TRUE;
675 }
676 
677 /**
678  * __ecore_vlan_mac_h_write_trylock - try getting the writer lock on vlan mac
679  * head list.
680  *
681  * @pdev:	device handle
682  * @o:		vlan_mac object
683  *
684  * @details: Non-blocking implementation; should be called under execution
685  *           queue lock.
686  */
__ecore_vlan_mac_h_write_trylock(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)687 static int __ecore_vlan_mac_h_write_trylock(struct _lm_device_t *pdev,
688 					    struct ecore_vlan_mac_obj *o)
689 {
690 	if (o->head_reader) {
691 		ECORE_MSG(pdev, "vlan_mac_lock writer - There are readers; Busy\n");
692 		return ECORE_BUSY;
693 	}
694 
695 	ECORE_MSG(pdev, "vlan_mac_lock writer - Taken\n");
696 	return ECORE_SUCCESS;
697 }
698 
699 /**
700  * __ecore_vlan_mac_h_exec_pending - execute step instead of a previous step
701  * which wasn't able to run due to a taken lock on vlan mac head list.
702  *
703  * @pdev:	device handle
704  * @o:		vlan_mac object
705  *
706  * @details Should be called under execution queue lock; notice it might release
707  *          and reclaim it during its run.
708  */
__ecore_vlan_mac_h_exec_pending(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)709 static void __ecore_vlan_mac_h_exec_pending(struct _lm_device_t *pdev,
710 					    struct ecore_vlan_mac_obj *o)
711 {
712 	int rc;
713 	unsigned long ramrod_flags = o->saved_ramrod_flags;
714 
715 	ECORE_MSG(pdev, "vlan_mac_lock execute pending command with ramrod flags %lu\n",
716 		  ramrod_flags);
717 	o->head_exe_request = FALSE;
718 	o->saved_ramrod_flags = 0;
719 	rc = ecore_exe_queue_step(pdev, &o->exe_queue, &ramrod_flags);
720 	if (rc != ECORE_SUCCESS) {
721 		ECORE_ERR("execution of pending commands failed with rc %d\n",
722 			  rc);
723 #ifdef ECORE_STOP_ON_ERROR
724 		ecore_panic();
725 #endif
726 	}
727 }
728 
729 /**
730  * __ecore_vlan_mac_h_pend - Pend an execution step which couldn't have been
731  * called due to vlan mac head list lock being taken.
732  *
733  * @pdev:		device handle
734  * @o:			vlan_mac object
735  * @ramrod_flags:	ramrod flags of missed execution
736  *
737  * @details Should be called under execution queue lock.
738  */
__ecore_vlan_mac_h_pend(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,unsigned long ramrod_flags)739 static void __ecore_vlan_mac_h_pend(struct _lm_device_t *pdev,
740 				    struct ecore_vlan_mac_obj *o,
741 				    unsigned long ramrod_flags)
742 {
743 	o->head_exe_request = TRUE;
744 	o->saved_ramrod_flags = ramrod_flags;
745 	ECORE_MSG(pdev, "Placing pending execution with ramrod flags %lu\n",
746 		  ramrod_flags);
747 }
748 
749 /**
750  * __ecore_vlan_mac_h_write_unlock - unlock the vlan mac head list writer lock
751  *
752  * @pdev:		device handle
753  * @o:			vlan_mac object
754  *
755  * @details Should be called under execution queue lock. Notice if a pending
756  *          execution exists, it would perform it - possibly releasing and
757  *          reclaiming the execution queue lock.
758  */
__ecore_vlan_mac_h_write_unlock(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)759 static void __ecore_vlan_mac_h_write_unlock(struct _lm_device_t *pdev,
760 					    struct ecore_vlan_mac_obj *o)
761 {
762 	/* It's possible a new pending execution was added since this writer
763 	 * executed. If so, execute again. [Ad infinitum]
764 	 */
765 	while(o->head_exe_request) {
766 		ECORE_MSG(pdev, "vlan_mac_lock - writer release encountered a pending request\n");
767 		__ecore_vlan_mac_h_exec_pending(pdev, o);
768 	}
769 }
770 
771 /**
772  * ecore_vlan_mac_h_write_unlock - unlock the vlan mac head list writer lock
773  *
774  * @pdev:		device handle
775  * @o:			vlan_mac object
776  *
777  * @details Notice if a pending execution exists, it would perform it -
778  *          possibly releasing and reclaiming the execution queue lock.
779  */
ecore_vlan_mac_h_write_unlock(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)780 void ecore_vlan_mac_h_write_unlock(struct _lm_device_t *pdev,
781 				   struct ecore_vlan_mac_obj *o)
782 {
783 	ECORE_SPIN_LOCK_BH(&o->exe_queue.lock);
784 	__ecore_vlan_mac_h_write_unlock(pdev, o);
785 	ECORE_SPIN_UNLOCK_BH(&o->exe_queue.lock);
786 }
787 
788 /**
789  * __ecore_vlan_mac_h_read_lock - lock the vlan mac head list reader lock
790  *
791  * @pdev:		device handle
792  * @o:			vlan_mac object
793  *
794  * @details Should be called under the execution queue lock. May sleep. May
795  *          release and reclaim execution queue lock during its run.
796  */
__ecore_vlan_mac_h_read_lock(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)797 static int __ecore_vlan_mac_h_read_lock(struct _lm_device_t *pdev,
798 					struct ecore_vlan_mac_obj *o)
799 {
800 	/* If we got here, we're holding lock --> no WRITER exists */
801 	o->head_reader++;
802 	ECORE_MSG(pdev, "vlan_mac_lock - locked reader - number %d\n",
803 		  o->head_reader);
804 
805 	return ECORE_SUCCESS;
806 }
807 
808 /**
809  * ecore_vlan_mac_h_read_lock - lock the vlan mac head list reader lock
810  *
811  * @pdev:		device handle
812  * @o:			vlan_mac object
813  *
814  * @details May sleep. Claims and releases execution queue lock during its run.
815  */
ecore_vlan_mac_h_read_lock(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)816 int ecore_vlan_mac_h_read_lock(struct _lm_device_t *pdev,
817 			       struct ecore_vlan_mac_obj *o)
818 {
819 	int rc;
820 
821 	ECORE_SPIN_LOCK_BH(&o->exe_queue.lock);
822 	rc = __ecore_vlan_mac_h_read_lock(pdev, o);
823 	ECORE_SPIN_UNLOCK_BH(&o->exe_queue.lock);
824 
825 	return rc;
826 }
827 
828 /**
829  * __ecore_vlan_mac_h_read_unlock - unlock the vlan mac head list reader lock
830  *
831  * @pdev:		device handle
832  * @o:			vlan_mac object
833  *
834  * @details Should be called under execution queue lock. Notice if a pending
835  *          execution exists, it would be performed if this was the last
836  *          reader. possibly releasing and reclaiming the execution queue lock.
837  */
__ecore_vlan_mac_h_read_unlock(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)838 static void __ecore_vlan_mac_h_read_unlock(struct _lm_device_t *pdev,
839 					  struct ecore_vlan_mac_obj *o)
840 {
841 	if (!o->head_reader) {
842 		ECORE_ERR("Need to release vlan mac reader lock, but lock isn't taken\n");
843 #ifdef ECORE_STOP_ON_ERROR
844 		ecore_panic();
845 #endif
846 	} else {
847 		o->head_reader--;
848 		ECORE_MSG(pdev, "vlan_mac_lock - decreased readers to %d\n",
849 			  o->head_reader);
850 	}
851 
852 	/* It's possible a new pending execution was added, and that this reader
853 	 * was last - if so we need to execute the command.
854 	 */
855 	if (!o->head_reader && o->head_exe_request) {
856 		ECORE_MSG(pdev, "vlan_mac_lock - reader release encountered a pending request\n");
857 
858 		/* Writer release will do the trick */
859 		__ecore_vlan_mac_h_write_unlock(pdev, o);
860 	}
861 }
862 
863 /**
864  * ecore_vlan_mac_h_read_unlock - unlock the vlan mac head list reader lock
865  *
866  * @pdev:		device handle
867  * @o:			vlan_mac object
868  *
869  * @details Notice if a pending execution exists, it would be performed if this
870  *          was the last reader. Claims and releases the execution queue lock
871  *          during its run.
872  */
ecore_vlan_mac_h_read_unlock(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)873 void ecore_vlan_mac_h_read_unlock(struct _lm_device_t *pdev,
874 				  struct ecore_vlan_mac_obj *o)
875 {
876 	ECORE_SPIN_LOCK_BH(&o->exe_queue.lock);
877 	__ecore_vlan_mac_h_read_unlock(pdev, o);
878 	ECORE_SPIN_UNLOCK_BH(&o->exe_queue.lock);
879 }
880 
881 /**
882  * ecore_vlan_mac_h_read_unlock - unlock the vlan mac head list reader lock
883  *
884  * @pdev:		device handle
885  * @o:			vlan_mac object
886  * @n:			number of elements to get
887  * @base:		base address for element placement
888  * @stride:		stride between elements (in bytes)
889  */
ecore_get_n_elements(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,int n,u8 * base,u8 stride,u8 size)890 static int ecore_get_n_elements(struct _lm_device_t *pdev, struct ecore_vlan_mac_obj *o,
891 				 int n, u8 *base, u8 stride, u8 size)
892 {
893 	struct ecore_vlan_mac_registry_elem *pos;
894 	u8 *next = base;
895 	int counter = 0;
896 	int read_lock;
897 
898 	ECORE_MSG(pdev, "get_n_elements - taking vlan_mac_lock (reader)\n");
899 	read_lock = ecore_vlan_mac_h_read_lock(pdev, o);
900 	if (read_lock != ECORE_SUCCESS)
901 		ECORE_ERR("get_n_elements failed to get vlan mac reader lock; Access without lock\n");
902 
903 	/* traverse list */
904 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
905 				  struct ecore_vlan_mac_registry_elem) {
906 		if (counter < n) {
907 			mm_memcpy(next, &pos->u, size);
908 			counter++;
909 			ECORE_MSG(pdev, "copied element number %d to address %p element was:\n",
910 				  counter, next);
911 			next += stride + size;
912 		}
913 	}
914 
915 	if (read_lock == ECORE_SUCCESS) {
916 		ECORE_MSG(pdev, "get_n_elements - releasing vlan_mac_lock (reader)\n");
917 		ecore_vlan_mac_h_read_unlock(pdev, o);
918 	}
919 
920 	return counter * ETH_ALEN;
921 }
922 
923 /* check_add() callbacks */
ecore_check_mac_add(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,union ecore_classification_ramrod_data * data)924 static int ecore_check_mac_add(struct _lm_device_t *pdev,
925 			       struct ecore_vlan_mac_obj *o,
926 			       union ecore_classification_ramrod_data *data)
927 {
928 	struct ecore_vlan_mac_registry_elem *pos;
929 
930 	ECORE_MSG(pdev, "Checking MAC %02x:%02x:%02x:%02x:%02x:%02x for ADD command\n", data->mac.mac[0], data->mac.mac[1], data->mac.mac[2], data->mac.mac[3], data->mac.mac[4], data->mac.mac[5]);
931 
932 	if (!ECORE_IS_VALID_ETHER_ADDR(data->mac.mac))
933 		return ECORE_INVAL;
934 
935 	/* Check if a requested MAC already exists */
936 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
937 				  struct ecore_vlan_mac_registry_elem)
938 		if (mm_memcmp(data->mac.mac, pos->u.mac.mac, ETH_ALEN) &&
939 		    (data->mac.is_inner_mac == pos->u.mac.is_inner_mac))
940 			return ECORE_EXISTS;
941 
942 	return ECORE_SUCCESS;
943 }
944 
ecore_check_vlan_add(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,union ecore_classification_ramrod_data * data)945 static int ecore_check_vlan_add(struct _lm_device_t *pdev,
946 				struct ecore_vlan_mac_obj *o,
947 				union ecore_classification_ramrod_data *data)
948 {
949 	struct ecore_vlan_mac_registry_elem *pos;
950 
951 	ECORE_MSG(pdev, "Checking VLAN %d for ADD command\n", data->vlan.vlan);
952 
953 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
954 				  struct ecore_vlan_mac_registry_elem)
955 		if (data->vlan.vlan == pos->u.vlan.vlan)
956 			return ECORE_EXISTS;
957 
958 	return ECORE_SUCCESS;
959 }
960 
ecore_check_vlan_mac_add(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,union ecore_classification_ramrod_data * data)961 static int ecore_check_vlan_mac_add(struct _lm_device_t *pdev,
962 				    struct ecore_vlan_mac_obj *o,
963 				   union ecore_classification_ramrod_data *data)
964 {
965 	struct ecore_vlan_mac_registry_elem *pos;
966 
967 	ECORE_MSG(pdev, "Checking VLAN_MAC (%02x:%02x:%02x:%02x:%02x:%02x, %d) for ADD command\n",
968 		  data->vlan_mac.mac[0], data->vlan_mac.mac[1], data->vlan_mac.mac[2], data->vlan_mac.mac[3], data->vlan_mac.mac[4], data->vlan_mac.mac[5], data->vlan_mac.vlan);
969 
970 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
971 				  struct ecore_vlan_mac_registry_elem)
972 		if ((data->vlan_mac.vlan == pos->u.vlan_mac.vlan) &&
973 		    (mm_memcmp(data->vlan_mac.mac, pos->u.vlan_mac.mac,
974 				  ETH_ALEN)) &&
975 		    (data->vlan_mac.is_inner_mac ==
976 		     pos->u.vlan_mac.is_inner_mac))
977 			return ECORE_EXISTS;
978 
979 	return ECORE_SUCCESS;
980 }
981 
982 /* check_del() callbacks */
983 static struct ecore_vlan_mac_registry_elem *
ecore_check_mac_del(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,union ecore_classification_ramrod_data * data)984 	ecore_check_mac_del(struct _lm_device_t *pdev,
985 			    struct ecore_vlan_mac_obj *o,
986 			    union ecore_classification_ramrod_data *data)
987 {
988 	struct ecore_vlan_mac_registry_elem *pos;
989 
990 	ECORE_MSG(pdev, "Checking MAC %02x:%02x:%02x:%02x:%02x:%02x for DEL command\n", data->mac.mac[0], data->mac.mac[1], data->mac.mac[2], data->mac.mac[3], data->mac.mac[4], data->mac.mac[5]);
991 
992 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
993 				  struct ecore_vlan_mac_registry_elem)
994 		if ((mm_memcmp(data->mac.mac, pos->u.mac.mac, ETH_ALEN)) &&
995 		    (data->mac.is_inner_mac == pos->u.mac.is_inner_mac))
996 			return pos;
997 
998 	return NULL;
999 }
1000 
1001 static struct ecore_vlan_mac_registry_elem *
ecore_check_vlan_del(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,union ecore_classification_ramrod_data * data)1002 	ecore_check_vlan_del(struct _lm_device_t *pdev,
1003 			     struct ecore_vlan_mac_obj *o,
1004 			     union ecore_classification_ramrod_data *data)
1005 {
1006 	struct ecore_vlan_mac_registry_elem *pos;
1007 
1008 	ECORE_MSG(pdev, "Checking VLAN %d for DEL command\n", data->vlan.vlan);
1009 
1010 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
1011 				  struct ecore_vlan_mac_registry_elem)
1012 		if (data->vlan.vlan == pos->u.vlan.vlan)
1013 			return pos;
1014 
1015 	return NULL;
1016 }
1017 
1018 static struct ecore_vlan_mac_registry_elem *
ecore_check_vlan_mac_del(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,union ecore_classification_ramrod_data * data)1019 	ecore_check_vlan_mac_del(struct _lm_device_t *pdev,
1020 				 struct ecore_vlan_mac_obj *o,
1021 				 union ecore_classification_ramrod_data *data)
1022 {
1023 	struct ecore_vlan_mac_registry_elem *pos;
1024 
1025 	ECORE_MSG(pdev, "Checking VLAN_MAC (%02x:%02x:%02x:%02x:%02x:%02x, %d) for DEL command\n",
1026 		  data->vlan_mac.mac[0], data->vlan_mac.mac[1], data->vlan_mac.mac[2], data->vlan_mac.mac[3], data->vlan_mac.mac[4], data->vlan_mac.mac[5], data->vlan_mac.vlan);
1027 
1028 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
1029 				  struct ecore_vlan_mac_registry_elem)
1030 		if ((data->vlan_mac.vlan == pos->u.vlan_mac.vlan) &&
1031 		    (mm_memcmp(data->vlan_mac.mac, pos->u.vlan_mac.mac,
1032 			     ETH_ALEN)) &&
1033 		    (data->vlan_mac.is_inner_mac ==
1034 		     pos->u.vlan_mac.is_inner_mac))
1035 			return pos;
1036 
1037 	return NULL;
1038 }
1039 
1040 /* check_move() callback */
ecore_check_move(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * src_o,struct ecore_vlan_mac_obj * dst_o,union ecore_classification_ramrod_data * data)1041 static BOOL ecore_check_move(struct _lm_device_t *pdev,
1042 			     struct ecore_vlan_mac_obj *src_o,
1043 			     struct ecore_vlan_mac_obj *dst_o,
1044 			     union ecore_classification_ramrod_data *data)
1045 {
1046 	struct ecore_vlan_mac_registry_elem *pos;
1047 	int rc;
1048 
1049 	/* Check if we can delete the requested configuration from the first
1050 	 * object.
1051 	 */
1052 	pos = src_o->check_del(pdev, src_o, data);
1053 
1054 	/*  check if configuration can be added */
1055 	rc = dst_o->check_add(pdev, dst_o, data);
1056 
1057 	/* If this classification can not be added (is already set)
1058 	 * or can't be deleted - return an error.
1059 	 */
1060 	if (rc || !pos)
1061 		return FALSE;
1062 
1063 	return TRUE;
1064 }
1065 
ecore_check_move_always_err(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * src_o,struct ecore_vlan_mac_obj * dst_o,union ecore_classification_ramrod_data * data)1066 static BOOL ecore_check_move_always_err(
1067 	struct _lm_device_t *pdev,
1068 	struct ecore_vlan_mac_obj *src_o,
1069 	struct ecore_vlan_mac_obj *dst_o,
1070 	union ecore_classification_ramrod_data *data)
1071 {
1072 	return FALSE;
1073 }
1074 
ecore_vlan_mac_get_rx_tx_flag(struct ecore_vlan_mac_obj * o)1075 static INLINE u8 ecore_vlan_mac_get_rx_tx_flag(struct ecore_vlan_mac_obj *o)
1076 {
1077 	struct ecore_raw_obj *raw = &o->raw;
1078 	u8 rx_tx_flag = 0;
1079 
1080 	if ((raw->obj_type == ECORE_OBJ_TYPE_TX) ||
1081 	    (raw->obj_type == ECORE_OBJ_TYPE_RX_TX))
1082 		rx_tx_flag |= ETH_CLASSIFY_CMD_HEADER_TX_CMD;
1083 
1084 	if ((raw->obj_type == ECORE_OBJ_TYPE_RX) ||
1085 	    (raw->obj_type == ECORE_OBJ_TYPE_RX_TX))
1086 		rx_tx_flag |= ETH_CLASSIFY_CMD_HEADER_RX_CMD;
1087 
1088 	return rx_tx_flag;
1089 }
1090 
ecore_set_mac_in_nig(struct _lm_device_t * pdev,BOOL add,unsigned char * dev_addr,int index)1091 void ecore_set_mac_in_nig(struct _lm_device_t *pdev,
1092 			  BOOL add, unsigned char *dev_addr, int index)
1093 {
1094 	u32 wb_data[2];
1095 	u32 reg_offset = PORT_ID(pdev) ? NIG_REG_LLH1_FUNC_MEM :
1096 			 NIG_REG_LLH0_FUNC_MEM;
1097 
1098 	if (!IS_MF_SI_MODE(pdev) && !IS_MF_AFEX(pdev))
1099 		return;
1100 
1101 	if (index > ECORE_LLH_CAM_MAX_PF_LINE)
1102 		return;
1103 
1104 	ECORE_MSG(pdev, "Going to %s LLH configuration at entry %d\n",
1105 		  (add ? "ADD" : "DELETE"), index);
1106 
1107 	if (add) {
1108 		/* LLH_FUNC_MEM is a u64 WB register */
1109 		reg_offset += 8*index;
1110 
1111 		wb_data[0] = ((dev_addr[2] << 24) | (dev_addr[3] << 16) |
1112 			      (dev_addr[4] <<  8) |  dev_addr[5]);
1113 		wb_data[1] = ((dev_addr[0] <<  8) |  dev_addr[1]);
1114 
1115 		REG_WR_DMAE_LEN(pdev, reg_offset, wb_data, 2);
1116 	}
1117 
1118 	REG_WR(pdev, (PORT_ID(pdev) ? NIG_REG_LLH1_FUNC_MEM_ENABLE :
1119 				  NIG_REG_LLH0_FUNC_MEM_ENABLE) + 4*index, add);
1120 }
1121 
1122 /**
1123  * ecore_vlan_mac_set_cmd_hdr_e2 - set a header in a single classify ramrod
1124  *
1125  * @pdev:	device handle
1126  * @o:		queue for which we want to configure this rule
1127  * @add:	if TRUE the command is an ADD command, DEL otherwise
1128  * @opcode:	CLASSIFY_RULE_OPCODE_XXX
1129  * @hdr:	pointer to a header to setup
1130  *
1131  */
ecore_vlan_mac_set_cmd_hdr_e2(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,BOOL add,int opcode,struct eth_classify_cmd_header * hdr)1132 static INLINE void ecore_vlan_mac_set_cmd_hdr_e2(struct _lm_device_t *pdev,
1133 	struct ecore_vlan_mac_obj *o, BOOL add, int opcode,
1134 	struct eth_classify_cmd_header *hdr)
1135 {
1136 	struct ecore_raw_obj *raw = &o->raw;
1137 
1138 	hdr->client_id = raw->cl_id;
1139 	hdr->func_id = raw->func_id;
1140 
1141 	/* Rx or/and Tx (internal switching) configuration ? */
1142 	hdr->cmd_general_data |=
1143 		ecore_vlan_mac_get_rx_tx_flag(o);
1144 
1145 	if (add)
1146 		hdr->cmd_general_data |= ETH_CLASSIFY_CMD_HEADER_IS_ADD;
1147 
1148 	hdr->cmd_general_data |=
1149 		(opcode << ETH_CLASSIFY_CMD_HEADER_OPCODE_SHIFT);
1150 }
1151 
1152 /**
1153  * ecore_vlan_mac_set_rdata_hdr_e2 - set the classify ramrod data header
1154  *
1155  * @cid:	connection id
1156  * @type:	ECORE_FILTER_XXX_PENDING
1157  * @hdr:	pointer to header to setup
1158  * @rule_cnt:
1159  *
1160  * currently we always configure one rule and echo field to contain a CID and an
1161  * opcode type.
1162  */
ecore_vlan_mac_set_rdata_hdr_e2(u32 cid,int type,struct eth_classify_header * hdr,int rule_cnt)1163 static INLINE void ecore_vlan_mac_set_rdata_hdr_e2(u32 cid, int type,
1164 				struct eth_classify_header *hdr, int rule_cnt)
1165 {
1166 	hdr->echo = mm_cpu_to_le32((cid & ECORE_SWCID_MASK) |
1167 				(type << ECORE_SWCID_SHIFT));
1168 	hdr->rule_cnt = (u8)rule_cnt;
1169 }
1170 
1171 /* hw_config() callbacks */
ecore_set_one_mac_e2(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,struct ecore_exeq_elem * elem,int rule_idx,int cam_offset)1172 static void ecore_set_one_mac_e2(struct _lm_device_t *pdev,
1173 				 struct ecore_vlan_mac_obj *o,
1174 				 struct ecore_exeq_elem *elem, int rule_idx,
1175 				 int cam_offset)
1176 {
1177 	struct ecore_raw_obj *raw = &o->raw;
1178 	struct eth_classify_rules_ramrod_data *data =
1179 		(struct eth_classify_rules_ramrod_data *)(raw->rdata);
1180 	int rule_cnt = rule_idx + 1, cmd = elem->cmd_data.vlan_mac.cmd;
1181 	union eth_classify_rule_cmd *rule_entry = &data->rules[rule_idx];
1182 	BOOL add = (cmd == ECORE_VLAN_MAC_ADD) ? TRUE : FALSE;
1183 	unsigned long *vlan_mac_flags = &elem->cmd_data.vlan_mac.vlan_mac_flags;
1184 	u8 *mac = elem->cmd_data.vlan_mac.u.mac.mac;
1185 
1186 	/* Set LLH CAM entry: currently only iSCSI and ETH macs are
1187 	 * relevant. In addition, current implementation is tuned for a
1188 	 * single ETH MAC.
1189 	 *
1190 	 * When multiple unicast ETH MACs PF configuration in switch
1191 	 * independent mode is required (NetQ, multiple netdev MACs,
1192 	 * etc.), consider better utilisation of 8 per function MAC
1193 	 * entries in the LLH register. There is also
1194 	 * NIG_REG_P[01]_LLH_FUNC_MEM2 registers that complete the
1195 	 * total number of CAM entries to 16.
1196 	 *
1197 	 * Currently we won't configure NIG for MACs other than a primary ETH
1198 	 * MAC and iSCSI L2 MAC.
1199 	 *
1200 	 * If this MAC is moving from one Queue to another, no need to change
1201 	 * NIG configuration.
1202 	 */
1203 	if (cmd != ECORE_VLAN_MAC_MOVE) {
1204 		if (ECORE_TEST_BIT(ECORE_ISCSI_ETH_MAC, vlan_mac_flags))
1205 			ecore_set_mac_in_nig(pdev, add, mac,
1206 					     ECORE_LLH_CAM_ISCSI_ETH_LINE);
1207 		else if (ECORE_TEST_BIT(ECORE_ETH_MAC, vlan_mac_flags))
1208 			ecore_set_mac_in_nig(pdev, add, mac,
1209 					     ECORE_LLH_CAM_ETH_LINE);
1210 	}
1211 
1212 	/* Reset the ramrod data buffer for the first rule */
1213 	if (rule_idx == 0)
1214 		mm_memset(data, 0, sizeof(*data));
1215 
1216 	/* Setup a command header */
1217 	ecore_vlan_mac_set_cmd_hdr_e2(pdev, o, add, CLASSIFY_RULE_OPCODE_MAC,
1218 				      &rule_entry->mac.header);
1219 
1220 	ECORE_MSG(pdev, "About to %s MAC %02x:%02x:%02x:%02x:%02x:%02x for Queue %d\n",
1221 		  (add ? "add" : "delete"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], raw->cl_id);
1222 
1223 	/* Set a MAC itself */
1224 	ecore_set_fw_mac_addr(&rule_entry->mac.mac_msb,
1225 			      &rule_entry->mac.mac_mid,
1226 			      &rule_entry->mac.mac_lsb, mac);
1227 	rule_entry->mac.inner_mac =
1228 		mm_cpu_to_le16(elem->cmd_data.vlan_mac.u.mac.is_inner_mac);
1229 
1230 	/* MOVE: Add a rule that will add this MAC to the target Queue */
1231 	if (cmd == ECORE_VLAN_MAC_MOVE) {
1232 		rule_entry++;
1233 		rule_cnt++;
1234 
1235 		/* Setup ramrod data */
1236 		ecore_vlan_mac_set_cmd_hdr_e2(pdev,
1237 					elem->cmd_data.vlan_mac.target_obj,
1238 					      TRUE, CLASSIFY_RULE_OPCODE_MAC,
1239 					      &rule_entry->mac.header);
1240 
1241 		/* Set a MAC itself */
1242 		ecore_set_fw_mac_addr(&rule_entry->mac.mac_msb,
1243 				      &rule_entry->mac.mac_mid,
1244 				      &rule_entry->mac.mac_lsb, mac);
1245 		rule_entry->mac.inner_mac =
1246 			mm_cpu_to_le16(elem->cmd_data.vlan_mac.
1247 				       u.mac.is_inner_mac);
1248 	}
1249 
1250 	/* Set the ramrod data header */
1251 	/* TODO: take this to the higher level in order to prevent multiple
1252 		 writing */
1253 	ecore_vlan_mac_set_rdata_hdr_e2(raw->cid, raw->state, &data->header,
1254 					rule_cnt);
1255 }
1256 
1257 /**
1258  * ecore_vlan_mac_set_rdata_hdr_e1x - set a header in a single classify ramrod
1259  *
1260  * @pdev:	device handle
1261  * @o:		queue
1262  * @type:
1263  * @cam_offset:	offset in cam memory
1264  * @hdr:	pointer to a header to setup
1265  *
1266  * E1/E1H
1267  */
ecore_vlan_mac_set_rdata_hdr_e1x(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,int type,int cam_offset,struct mac_configuration_hdr * hdr)1268 static INLINE void ecore_vlan_mac_set_rdata_hdr_e1x(struct _lm_device_t *pdev,
1269 	struct ecore_vlan_mac_obj *o, int type, int cam_offset,
1270 	struct mac_configuration_hdr *hdr)
1271 {
1272 	struct ecore_raw_obj *r = &o->raw;
1273 
1274 	hdr->length = 1;
1275 	hdr->offset = (u8)cam_offset;
1276 	hdr->client_id = mm_cpu_to_le16(0xff);
1277 	hdr->echo = mm_cpu_to_le32((r->cid & ECORE_SWCID_MASK) |
1278 				(type << ECORE_SWCID_SHIFT));
1279 }
1280 
ecore_vlan_mac_set_cfg_entry_e1x(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,BOOL add,int opcode,u8 * mac,u16 vlan_id,struct mac_configuration_entry * cfg_entry)1281 static INLINE void ecore_vlan_mac_set_cfg_entry_e1x(struct _lm_device_t *pdev,
1282 	struct ecore_vlan_mac_obj *o, BOOL add, int opcode, u8 *mac,
1283 	u16 vlan_id, struct mac_configuration_entry *cfg_entry)
1284 {
1285 	struct ecore_raw_obj *r = &o->raw;
1286 	u32 cl_bit_vec = (1 << r->cl_id);
1287 
1288 	cfg_entry->clients_bit_vector = mm_cpu_to_le32(cl_bit_vec);
1289 	cfg_entry->pf_id = r->func_id;
1290 	cfg_entry->vlan_id = mm_cpu_to_le16(vlan_id);
1291 
1292 	if (add) {
1293 		ECORE_SET_FLAG(cfg_entry->flags,
1294 			       MAC_CONFIGURATION_ENTRY_ACTION_TYPE,
1295 			       T_ETH_MAC_COMMAND_SET);
1296 		ECORE_SET_FLAG(cfg_entry->flags,
1297 			       MAC_CONFIGURATION_ENTRY_VLAN_FILTERING_MODE,
1298 			       opcode);
1299 
1300 		/* Set a MAC in a ramrod data */
1301 		ecore_set_fw_mac_addr(&cfg_entry->msb_mac_addr,
1302 				      &cfg_entry->middle_mac_addr,
1303 				      &cfg_entry->lsb_mac_addr, mac);
1304 	} else
1305 		ECORE_SET_FLAG(cfg_entry->flags,
1306 			       MAC_CONFIGURATION_ENTRY_ACTION_TYPE,
1307 			       T_ETH_MAC_COMMAND_INVALIDATE);
1308 }
1309 
ecore_vlan_mac_set_rdata_e1x(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,int type,int cam_offset,BOOL add,u8 * mac,u16 vlan_id,int opcode,struct mac_configuration_cmd * config)1310 static INLINE void ecore_vlan_mac_set_rdata_e1x(struct _lm_device_t *pdev,
1311 	struct ecore_vlan_mac_obj *o, int type, int cam_offset, BOOL add,
1312 	u8 *mac, u16 vlan_id, int opcode, struct mac_configuration_cmd *config)
1313 {
1314 	struct mac_configuration_entry *cfg_entry = &config->config_table[0];
1315 	struct ecore_raw_obj *raw = &o->raw;
1316 
1317 	ecore_vlan_mac_set_rdata_hdr_e1x(pdev, o, type, cam_offset,
1318 					 &config->hdr);
1319 	ecore_vlan_mac_set_cfg_entry_e1x(pdev, o, add, opcode, mac, vlan_id,
1320 					 cfg_entry);
1321 
1322 	ECORE_MSG(pdev, "%s MAC %02x:%02x:%02x:%02x:%02x:%02x CLID %d CAM offset %d\n",
1323 		  (add ? "setting" : "clearing"),
1324 		  mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], raw->cl_id, cam_offset);
1325 }
1326 
1327 /**
1328  * ecore_set_one_mac_e1x - fill a single MAC rule ramrod data
1329  *
1330  * @pdev:	device handle
1331  * @o:		ecore_vlan_mac_obj
1332  * @elem:	ecore_exeq_elem
1333  * @rule_idx:	rule_idx
1334  * @cam_offset: cam_offset
1335  */
ecore_set_one_mac_e1x(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,struct ecore_exeq_elem * elem,int rule_idx,int cam_offset)1336 static void ecore_set_one_mac_e1x(struct _lm_device_t *pdev,
1337 				  struct ecore_vlan_mac_obj *o,
1338 				  struct ecore_exeq_elem *elem, int rule_idx,
1339 				  int cam_offset)
1340 {
1341 	struct ecore_raw_obj *raw = &o->raw;
1342 	struct mac_configuration_cmd *config =
1343 		(struct mac_configuration_cmd *)(raw->rdata);
1344 	/* 57710 and 57711 do not support MOVE command,
1345 	 * so it's either ADD or DEL
1346 	 */
1347 	BOOL add = (elem->cmd_data.vlan_mac.cmd == ECORE_VLAN_MAC_ADD) ?
1348 		TRUE : FALSE;
1349 
1350 	/* Reset the ramrod data buffer */
1351 	mm_memset(config, 0, sizeof(*config));
1352 
1353 	ecore_vlan_mac_set_rdata_e1x(pdev, o, raw->state,
1354 				     cam_offset, add,
1355 				     elem->cmd_data.vlan_mac.u.mac.mac, 0,
1356 				     ETH_VLAN_FILTER_ANY_VLAN, config);
1357 }
1358 
ecore_set_one_vlan_e2(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,struct ecore_exeq_elem * elem,int rule_idx,int cam_offset)1359 static void ecore_set_one_vlan_e2(struct _lm_device_t *pdev,
1360 				  struct ecore_vlan_mac_obj *o,
1361 				  struct ecore_exeq_elem *elem, int rule_idx,
1362 				  int cam_offset)
1363 {
1364 	struct ecore_raw_obj *raw = &o->raw;
1365 	struct eth_classify_rules_ramrod_data *data =
1366 		(struct eth_classify_rules_ramrod_data *)(raw->rdata);
1367 	int rule_cnt = rule_idx + 1;
1368 	union eth_classify_rule_cmd *rule_entry = &data->rules[rule_idx];
1369 	enum ecore_vlan_mac_cmd cmd = elem->cmd_data.vlan_mac.cmd;
1370 	BOOL add = (cmd == ECORE_VLAN_MAC_ADD) ? TRUE : FALSE;
1371 	u16 vlan = elem->cmd_data.vlan_mac.u.vlan.vlan;
1372 
1373 	/* Reset the ramrod data buffer for the first rule */
1374 	if (rule_idx == 0)
1375 		mm_memset(data, 0, sizeof(*data));
1376 
1377 	/* Set a rule header */
1378 	ecore_vlan_mac_set_cmd_hdr_e2(pdev, o, add, CLASSIFY_RULE_OPCODE_VLAN,
1379 				      &rule_entry->vlan.header);
1380 
1381 	ECORE_MSG(pdev, "About to %s VLAN %d\n", (add ? "add" : "delete"),
1382 		  vlan);
1383 
1384 	/* Set a VLAN itself */
1385 	rule_entry->vlan.vlan = mm_cpu_to_le16(vlan);
1386 
1387 	/* MOVE: Add a rule that will add this MAC to the target Queue */
1388 	if (cmd == ECORE_VLAN_MAC_MOVE) {
1389 		rule_entry++;
1390 		rule_cnt++;
1391 
1392 		/* Setup ramrod data */
1393 		ecore_vlan_mac_set_cmd_hdr_e2(pdev,
1394 					elem->cmd_data.vlan_mac.target_obj,
1395 					      TRUE, CLASSIFY_RULE_OPCODE_VLAN,
1396 					      &rule_entry->vlan.header);
1397 
1398 		/* Set a VLAN itself */
1399 		rule_entry->vlan.vlan = mm_cpu_to_le16(vlan);
1400 	}
1401 
1402 	/* Set the ramrod data header */
1403 	/* TODO: take this to the higher level in order to prevent multiple
1404 		 writing */
1405 	ecore_vlan_mac_set_rdata_hdr_e2(raw->cid, raw->state, &data->header,
1406 					rule_cnt);
1407 }
1408 
ecore_set_one_vlan_mac_e2(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,struct ecore_exeq_elem * elem,int rule_idx,int cam_offset)1409 static void ecore_set_one_vlan_mac_e2(struct _lm_device_t *pdev,
1410 				      struct ecore_vlan_mac_obj *o,
1411 				      struct ecore_exeq_elem *elem,
1412 				      int rule_idx, int cam_offset)
1413 {
1414 	struct ecore_raw_obj *raw = &o->raw;
1415 	struct eth_classify_rules_ramrod_data *data =
1416 		(struct eth_classify_rules_ramrod_data *)(raw->rdata);
1417 	int rule_cnt = rule_idx + 1;
1418 	union eth_classify_rule_cmd *rule_entry = &data->rules[rule_idx];
1419 	enum ecore_vlan_mac_cmd cmd = elem->cmd_data.vlan_mac.cmd;
1420 	BOOL add = (cmd == ECORE_VLAN_MAC_ADD) ? TRUE : FALSE;
1421 	u16 vlan = elem->cmd_data.vlan_mac.u.vlan_mac.vlan;
1422 	u8 *mac = elem->cmd_data.vlan_mac.u.vlan_mac.mac;
1423 
1424 	/* Reset the ramrod data buffer for the first rule */
1425 	if (rule_idx == 0)
1426 		mm_memset(data, 0, sizeof(*data));
1427 
1428 	/* Set a rule header */
1429 	ecore_vlan_mac_set_cmd_hdr_e2(pdev, o, add, CLASSIFY_RULE_OPCODE_PAIR,
1430 				      &rule_entry->pair.header);
1431 
1432 	/* Set VLAN and MAC themselves */
1433 	rule_entry->pair.vlan = mm_cpu_to_le16(vlan);
1434 	ecore_set_fw_mac_addr(&rule_entry->pair.mac_msb,
1435 			      &rule_entry->pair.mac_mid,
1436 			      &rule_entry->pair.mac_lsb, mac);
1437 	rule_entry->pair.inner_mac =
1438 			elem->cmd_data.vlan_mac.u.vlan_mac.is_inner_mac;
1439 	/* MOVE: Add a rule that will add this MAC to the target Queue */
1440 	if (cmd == ECORE_VLAN_MAC_MOVE) {
1441 		rule_entry++;
1442 		rule_cnt++;
1443 
1444 		/* Setup ramrod data */
1445 		ecore_vlan_mac_set_cmd_hdr_e2(pdev,
1446 					elem->cmd_data.vlan_mac.target_obj,
1447 					      TRUE, CLASSIFY_RULE_OPCODE_PAIR,
1448 					      &rule_entry->pair.header);
1449 
1450 		/* Set a VLAN itself */
1451 		rule_entry->pair.vlan = mm_cpu_to_le16(vlan);
1452 		ecore_set_fw_mac_addr(&rule_entry->pair.mac_msb,
1453 				      &rule_entry->pair.mac_mid,
1454 				      &rule_entry->pair.mac_lsb, mac);
1455 		rule_entry->pair.inner_mac =
1456 			elem->cmd_data.vlan_mac.u.vlan_mac.is_inner_mac;
1457 	}
1458 
1459 	/* Set the ramrod data header */
1460 	/* TODO: take this to the higher level in order to prevent multiple
1461 		 writing */
1462 	ecore_vlan_mac_set_rdata_hdr_e2(raw->cid, raw->state, &data->header,
1463 					rule_cnt);
1464 }
1465 
1466 /**
1467  * ecore_set_one_vlan_mac_e1h -
1468  *
1469  * @pdev:	device handle
1470  * @o:		ecore_vlan_mac_obj
1471  * @elem:	ecore_exeq_elem
1472  * @rule_idx:	rule_idx
1473  * @cam_offset:	cam_offset
1474  */
ecore_set_one_vlan_mac_e1h(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,struct ecore_exeq_elem * elem,int rule_idx,int cam_offset)1475 static void ecore_set_one_vlan_mac_e1h(struct _lm_device_t *pdev,
1476 				       struct ecore_vlan_mac_obj *o,
1477 				       struct ecore_exeq_elem *elem,
1478 				       int rule_idx, int cam_offset)
1479 {
1480 	struct ecore_raw_obj *raw = &o->raw;
1481 	struct mac_configuration_cmd *config =
1482 		(struct mac_configuration_cmd *)(raw->rdata);
1483 	/* 57710 and 57711 do not support MOVE command,
1484 	 * so it's either ADD or DEL
1485 	 */
1486 	BOOL add = (elem->cmd_data.vlan_mac.cmd == ECORE_VLAN_MAC_ADD) ?
1487 		TRUE : FALSE;
1488 
1489 	/* Reset the ramrod data buffer */
1490 	mm_memset(config, 0, sizeof(*config));
1491 
1492 	ecore_vlan_mac_set_rdata_e1x(pdev, o, ECORE_FILTER_VLAN_MAC_PENDING,
1493 				     cam_offset, add,
1494 				     elem->cmd_data.vlan_mac.u.vlan_mac.mac,
1495 				     elem->cmd_data.vlan_mac.u.vlan_mac.vlan,
1496 				     ETH_VLAN_FILTER_CLASSIFY, config);
1497 }
1498 
1499 #define list_next_entry(pos, member) \
1500 	list_entry((pos)->member.next, typeof(*(pos)), member)
1501 
1502 /**
1503  * ecore_vlan_mac_restore - reconfigure next MAC/VLAN/VLAN-MAC element
1504  *
1505  * @pdev:	device handle
1506  * @p:		command parameters
1507  * @ppos:	pointer to the cookie
1508  *
1509  * reconfigure next MAC/VLAN/VLAN-MAC element from the
1510  * previously configured elements list.
1511  *
1512  * from command parameters only RAMROD_COMP_WAIT bit in ramrod_flags is	taken
1513  * into an account
1514  *
1515  * pointer to the cookie  - that should be given back in the next call to make
1516  * function handle the next element. If *ppos is set to NULL it will restart the
1517  * iterator. If returned *ppos == NULL this means that the last element has been
1518  * handled.
1519  *
1520  */
ecore_vlan_mac_restore(struct _lm_device_t * pdev,struct ecore_vlan_mac_ramrod_params * p,struct ecore_vlan_mac_registry_elem ** ppos)1521 static int ecore_vlan_mac_restore(struct _lm_device_t *pdev,
1522 			   struct ecore_vlan_mac_ramrod_params *p,
1523 			   struct ecore_vlan_mac_registry_elem **ppos)
1524 {
1525 	struct ecore_vlan_mac_registry_elem *pos;
1526 	struct ecore_vlan_mac_obj *o = p->vlan_mac_obj;
1527 
1528 	/* If list is empty - there is nothing to do here */
1529 	if (ECORE_LIST_IS_EMPTY(&o->head)) {
1530 		*ppos = NULL;
1531 		return 0;
1532 	}
1533 
1534 	/* make a step... */
1535 	if (*ppos == NULL)
1536 		*ppos = ECORE_LIST_FIRST_ENTRY(&o->head,
1537 					    struct ecore_vlan_mac_registry_elem,
1538 					       link);
1539 	else
1540 		*ppos = ECORE_LIST_NEXT(*ppos, link,
1541 					struct ecore_vlan_mac_registry_elem);
1542 
1543 	pos = *ppos;
1544 
1545 	/* If it's the last step - return NULL */
1546 	if (ECORE_LIST_IS_LAST(&pos->link, &o->head))
1547 		*ppos = NULL;
1548 
1549 	/* Prepare a 'user_req' */
1550 	mm_memcpy(&p->user_req.u, &pos->u, sizeof(pos->u));
1551 
1552 	/* Set the command */
1553 	p->user_req.cmd = ECORE_VLAN_MAC_ADD;
1554 
1555 	/* Set vlan_mac_flags */
1556 	p->user_req.vlan_mac_flags = pos->vlan_mac_flags;
1557 
1558 	/* Set a restore bit */
1559 	ECORE_SET_BIT_NA(RAMROD_RESTORE, &p->ramrod_flags);
1560 
1561 	return ecore_config_vlan_mac(pdev, p);
1562 }
1563 
1564 /* ecore_exeq_get_mac/ecore_exeq_get_vlan/ecore_exeq_get_vlan_mac return a
1565  * pointer to an element with a specific criteria and NULL if such an element
1566  * hasn't been found.
1567  */
ecore_exeq_get_mac(struct ecore_exe_queue_obj * o,struct ecore_exeq_elem * elem)1568 static struct ecore_exeq_elem *ecore_exeq_get_mac(
1569 	struct ecore_exe_queue_obj *o,
1570 	struct ecore_exeq_elem *elem)
1571 {
1572 	struct ecore_exeq_elem *pos;
1573 	struct ecore_mac_ramrod_data *data = &elem->cmd_data.vlan_mac.u.mac;
1574 
1575 	/* Check pending for execution commands */
1576 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->exe_queue, link,
1577 				  struct ecore_exeq_elem)
1578 		if (mm_memcmp(&pos->cmd_data.vlan_mac.u.mac, data,
1579 			      sizeof(*data)) &&
1580 		    (pos->cmd_data.vlan_mac.cmd == elem->cmd_data.vlan_mac.cmd))
1581 			return pos;
1582 
1583 	return NULL;
1584 }
1585 
ecore_exeq_get_vlan(struct ecore_exe_queue_obj * o,struct ecore_exeq_elem * elem)1586 static struct ecore_exeq_elem *ecore_exeq_get_vlan(
1587 	struct ecore_exe_queue_obj *o,
1588 	struct ecore_exeq_elem *elem)
1589 {
1590 	struct ecore_exeq_elem *pos;
1591 	struct ecore_vlan_ramrod_data *data = &elem->cmd_data.vlan_mac.u.vlan;
1592 
1593 	/* Check pending for execution commands */
1594 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->exe_queue, link,
1595 				  struct ecore_exeq_elem)
1596 		if (mm_memcmp(&pos->cmd_data.vlan_mac.u.vlan, data,
1597 			      sizeof(*data)) &&
1598 		    (pos->cmd_data.vlan_mac.cmd == elem->cmd_data.vlan_mac.cmd))
1599 			return pos;
1600 
1601 	return NULL;
1602 }
1603 
ecore_exeq_get_vlan_mac(struct ecore_exe_queue_obj * o,struct ecore_exeq_elem * elem)1604 static struct ecore_exeq_elem *ecore_exeq_get_vlan_mac(
1605 	struct ecore_exe_queue_obj *o,
1606 	struct ecore_exeq_elem *elem)
1607 {
1608 	struct ecore_exeq_elem *pos;
1609 	struct ecore_vlan_mac_ramrod_data *data =
1610 		&elem->cmd_data.vlan_mac.u.vlan_mac;
1611 
1612 	/* Check pending for execution commands */
1613 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->exe_queue, link,
1614 				  struct ecore_exeq_elem)
1615 		if (mm_memcmp(&pos->cmd_data.vlan_mac.u.vlan_mac, data,
1616 			      sizeof(*data)) &&
1617 		    (pos->cmd_data.vlan_mac.cmd == elem->cmd_data.vlan_mac.cmd))
1618 			return pos;
1619 
1620 	return NULL;
1621 }
1622 
1623 /**
1624  * ecore_validate_vlan_mac_add - check if an ADD command can be executed
1625  *
1626  * @pdev:	device handle
1627  * @qo:		ecore_qable_obj
1628  * @elem:	ecore_exeq_elem
1629  *
1630  * Checks that the requested configuration can be added. If yes and if
1631  * requested, consume CAM credit.
1632  *
1633  * The 'validate' is run after the 'optimize'.
1634  *
1635  */
ecore_validate_vlan_mac_add(struct _lm_device_t * pdev,union ecore_qable_obj * qo,struct ecore_exeq_elem * elem)1636 static INLINE int ecore_validate_vlan_mac_add(struct _lm_device_t *pdev,
1637 					      union ecore_qable_obj *qo,
1638 					      struct ecore_exeq_elem *elem)
1639 {
1640 	struct ecore_vlan_mac_obj *o = &qo->vlan_mac;
1641 	struct ecore_exe_queue_obj *exeq = &o->exe_queue;
1642 	int rc;
1643 
1644 	/* Check the registry */
1645 	rc = o->check_add(pdev, o, &elem->cmd_data.vlan_mac.u);
1646 	if (rc) {
1647 		ECORE_MSG(pdev, "ADD command is not allowed considering current registry state.\n");
1648 		return rc;
1649 	}
1650 
1651 	/* Check if there is a pending ADD command for this
1652 	 * MAC/VLAN/VLAN-MAC. Return an error if there is.
1653 	 */
1654 	if (exeq->get(exeq, elem)) {
1655 		ECORE_MSG(pdev, "There is a pending ADD command already\n");
1656 		return ECORE_EXISTS;
1657 	}
1658 
1659 	/* TODO: Check the pending MOVE from other objects where this
1660 	 * object is a destination object.
1661 	 */
1662 
1663 	/* Consume the credit if not requested not to */
1664 	if (!(ECORE_TEST_BIT(ECORE_DONT_CONSUME_CAM_CREDIT,
1665 			     &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
1666 	    o->get_credit(o)))
1667 		return ECORE_INVAL;
1668 
1669 	return ECORE_SUCCESS;
1670 }
1671 
1672 /**
1673  * ecore_validate_vlan_mac_del - check if the DEL command can be executed
1674  *
1675  * @pdev:	device handle
1676  * @qo:		quable object to check
1677  * @elem:	element that needs to be deleted
1678  *
1679  * Checks that the requested configuration can be deleted. If yes and if
1680  * requested, returns a CAM credit.
1681  *
1682  * The 'validate' is run after the 'optimize'.
1683  */
ecore_validate_vlan_mac_del(struct _lm_device_t * pdev,union ecore_qable_obj * qo,struct ecore_exeq_elem * elem)1684 static INLINE int ecore_validate_vlan_mac_del(struct _lm_device_t *pdev,
1685 					      union ecore_qable_obj *qo,
1686 					      struct ecore_exeq_elem *elem)
1687 {
1688 	struct ecore_vlan_mac_obj *o = &qo->vlan_mac;
1689 	struct ecore_vlan_mac_registry_elem *pos;
1690 	struct ecore_exe_queue_obj *exeq = &o->exe_queue;
1691 	struct ecore_exeq_elem query_elem;
1692 
1693 	/* If this classification can not be deleted (doesn't exist)
1694 	 * - return a ECORE_EXIST.
1695 	 */
1696 	pos = o->check_del(pdev, o, &elem->cmd_data.vlan_mac.u);
1697 	if (!pos) {
1698 		ECORE_MSG(pdev, "DEL command is not allowed considering current registry state\n");
1699 		return ECORE_EXISTS;
1700 	}
1701 
1702 	/* Check if there are pending DEL or MOVE commands for this
1703 	 * MAC/VLAN/VLAN-MAC. Return an error if so.
1704 	 */
1705 	mm_memcpy(&query_elem, elem, sizeof(query_elem));
1706 
1707 	/* Check for MOVE commands */
1708 	query_elem.cmd_data.vlan_mac.cmd = ECORE_VLAN_MAC_MOVE;
1709 	if (exeq->get(exeq, &query_elem)) {
1710 		ECORE_ERR("There is a pending MOVE command already\n");
1711 		return ECORE_INVAL;
1712 	}
1713 
1714 	/* Check for DEL commands */
1715 	if (exeq->get(exeq, elem)) {
1716 		ECORE_MSG(pdev, "There is a pending DEL command already\n");
1717 		return ECORE_EXISTS;
1718 	}
1719 
1720 	/* Return the credit to the credit pool if not requested not to */
1721 	if (!(ECORE_TEST_BIT(ECORE_DONT_CONSUME_CAM_CREDIT,
1722 			     &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
1723 	    o->put_credit(o))) {
1724 		ECORE_ERR("Failed to return a credit\n");
1725 		return ECORE_INVAL;
1726 	}
1727 
1728 	return ECORE_SUCCESS;
1729 }
1730 
1731 /**
1732  * ecore_validate_vlan_mac_move - check if the MOVE command can be executed
1733  *
1734  * @pdev:	device handle
1735  * @qo:		quable object to check (source)
1736  * @elem:	element that needs to be moved
1737  *
1738  * Checks that the requested configuration can be moved. If yes and if
1739  * requested, returns a CAM credit.
1740  *
1741  * The 'validate' is run after the 'optimize'.
1742  */
ecore_validate_vlan_mac_move(struct _lm_device_t * pdev,union ecore_qable_obj * qo,struct ecore_exeq_elem * elem)1743 static INLINE int ecore_validate_vlan_mac_move(struct _lm_device_t *pdev,
1744 					       union ecore_qable_obj *qo,
1745 					       struct ecore_exeq_elem *elem)
1746 {
1747 	struct ecore_vlan_mac_obj *src_o = &qo->vlan_mac;
1748 	struct ecore_vlan_mac_obj *dest_o = elem->cmd_data.vlan_mac.target_obj;
1749 	struct ecore_exeq_elem query_elem;
1750 	struct ecore_exe_queue_obj *src_exeq = &src_o->exe_queue;
1751 	struct ecore_exe_queue_obj *dest_exeq = &dest_o->exe_queue;
1752 
1753 	/* Check if we can perform this operation based on the current registry
1754 	 * state.
1755 	 */
1756 	if (!src_o->check_move(pdev, src_o, dest_o,
1757 			       &elem->cmd_data.vlan_mac.u)) {
1758 		ECORE_MSG(pdev, "MOVE command is not allowed considering current registry state\n");
1759 		return ECORE_INVAL;
1760 	}
1761 
1762 	/* Check if there is an already pending DEL or MOVE command for the
1763 	 * source object or ADD command for a destination object. Return an
1764 	 * error if so.
1765 	 */
1766 	mm_memcpy(&query_elem, elem, sizeof(query_elem));
1767 
1768 	/* Check DEL on source */
1769 	query_elem.cmd_data.vlan_mac.cmd = ECORE_VLAN_MAC_DEL;
1770 	if (src_exeq->get(src_exeq, &query_elem)) {
1771 		ECORE_ERR("There is a pending DEL command on the source queue already\n");
1772 		return ECORE_INVAL;
1773 	}
1774 
1775 	/* Check MOVE on source */
1776 	if (src_exeq->get(src_exeq, elem)) {
1777 		ECORE_MSG(pdev, "There is a pending MOVE command already\n");
1778 		return ECORE_EXISTS;
1779 	}
1780 
1781 	/* Check ADD on destination */
1782 	query_elem.cmd_data.vlan_mac.cmd = ECORE_VLAN_MAC_ADD;
1783 	if (dest_exeq->get(dest_exeq, &query_elem)) {
1784 		ECORE_ERR("There is a pending ADD command on the destination queue already\n");
1785 		return ECORE_INVAL;
1786 	}
1787 
1788 	/* Consume the credit if not requested not to */
1789 	if (!(ECORE_TEST_BIT(ECORE_DONT_CONSUME_CAM_CREDIT_DEST,
1790 			     &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
1791 	    dest_o->get_credit(dest_o)))
1792 		return ECORE_INVAL;
1793 
1794 	if (!(ECORE_TEST_BIT(ECORE_DONT_CONSUME_CAM_CREDIT,
1795 			     &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
1796 	    src_o->put_credit(src_o))) {
1797 		/* return the credit taken from dest... */
1798 		dest_o->put_credit(dest_o);
1799 		return ECORE_INVAL;
1800 	}
1801 
1802 	return ECORE_SUCCESS;
1803 }
1804 
ecore_validate_vlan_mac(struct _lm_device_t * pdev,union ecore_qable_obj * qo,struct ecore_exeq_elem * elem)1805 static int ecore_validate_vlan_mac(struct _lm_device_t *pdev,
1806 				   union ecore_qable_obj *qo,
1807 				   struct ecore_exeq_elem *elem)
1808 {
1809 	switch (elem->cmd_data.vlan_mac.cmd) {
1810 	case ECORE_VLAN_MAC_ADD:
1811 		return ecore_validate_vlan_mac_add(pdev, qo, elem);
1812 	case ECORE_VLAN_MAC_DEL:
1813 		return ecore_validate_vlan_mac_del(pdev, qo, elem);
1814 	case ECORE_VLAN_MAC_MOVE:
1815 		return ecore_validate_vlan_mac_move(pdev, qo, elem);
1816 	default:
1817 		return ECORE_INVAL;
1818 	}
1819 }
1820 
ecore_remove_vlan_mac(struct _lm_device_t * pdev,union ecore_qable_obj * qo,struct ecore_exeq_elem * elem)1821 static int ecore_remove_vlan_mac(struct _lm_device_t *pdev,
1822 				  union ecore_qable_obj *qo,
1823 				  struct ecore_exeq_elem *elem)
1824 {
1825 	int rc = 0;
1826 
1827 	/* If consumption wasn't required, nothing to do */
1828 	if (ECORE_TEST_BIT(ECORE_DONT_CONSUME_CAM_CREDIT,
1829 			   &elem->cmd_data.vlan_mac.vlan_mac_flags))
1830 		return ECORE_SUCCESS;
1831 
1832 	switch (elem->cmd_data.vlan_mac.cmd) {
1833 	case ECORE_VLAN_MAC_ADD:
1834 	case ECORE_VLAN_MAC_MOVE:
1835 		rc = qo->vlan_mac.put_credit(&qo->vlan_mac);
1836 		break;
1837 	case ECORE_VLAN_MAC_DEL:
1838 		rc = qo->vlan_mac.get_credit(&qo->vlan_mac);
1839 		break;
1840 	default:
1841 		return ECORE_INVAL;
1842 	}
1843 
1844 	if (rc != TRUE)
1845 		return ECORE_INVAL;
1846 
1847 	return ECORE_SUCCESS;
1848 }
1849 
1850 /**
1851  * ecore_wait_vlan_mac - passively wait for 5 seconds until all work completes.
1852  *
1853  * @pdev:	device handle
1854  * @o:		ecore_vlan_mac_obj
1855  *
1856  */
ecore_wait_vlan_mac(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o)1857 static int ecore_wait_vlan_mac(struct _lm_device_t *pdev,
1858 			       struct ecore_vlan_mac_obj *o)
1859 {
1860 	int cnt = 5000, rc;
1861 	struct ecore_exe_queue_obj *exeq = &o->exe_queue;
1862 	struct ecore_raw_obj *raw = &o->raw;
1863 
1864 	while (cnt--) {
1865 		/* Wait for the current command to complete */
1866 		rc = raw->wait_comp(pdev, raw);
1867 		if (rc)
1868 			return rc;
1869 
1870 		/* Wait until there are no pending commands */
1871 		if (!ecore_exe_queue_empty(exeq))
1872 			mm_wait(pdev, 1000);
1873 		else
1874 			return ECORE_SUCCESS;
1875 	}
1876 
1877 	return ECORE_TIMEOUT;
1878 }
1879 
__ecore_vlan_mac_execute_step(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,unsigned long * ramrod_flags)1880 static int __ecore_vlan_mac_execute_step(struct _lm_device_t *pdev,
1881 					 struct ecore_vlan_mac_obj *o,
1882 					 unsigned long *ramrod_flags)
1883 {
1884 	int rc = ECORE_SUCCESS;
1885 
1886 	ECORE_SPIN_LOCK_BH(&o->exe_queue.lock);
1887 
1888 	ECORE_MSG(pdev, "vlan_mac_execute_step - trying to take writer lock\n");
1889 	rc = __ecore_vlan_mac_h_write_trylock(pdev, o);
1890 
1891 	if (rc != ECORE_SUCCESS) {
1892 		__ecore_vlan_mac_h_pend(pdev, o, *ramrod_flags);
1893 
1894 		/** Calling function should not diffrentiate between this case
1895 		 *  and the case in which there is already a pending ramrod
1896 		 */
1897 		rc = ECORE_PENDING;
1898 	} else {
1899 		rc = ecore_exe_queue_step(pdev, &o->exe_queue, ramrod_flags);
1900 	}
1901 	ECORE_SPIN_UNLOCK_BH(&o->exe_queue.lock);
1902 
1903 	return rc;
1904 }
1905 
1906 /**
1907  * ecore_complete_vlan_mac - complete one VLAN-MAC ramrod
1908  *
1909  * @pdev:	device handle
1910  * @o:		ecore_vlan_mac_obj
1911  * @cqe:
1912  * @cont:	if TRUE schedule next execution chunk
1913  *
1914  */
ecore_complete_vlan_mac(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,union event_ring_elem * cqe,unsigned long * ramrod_flags)1915 static int ecore_complete_vlan_mac(struct _lm_device_t *pdev,
1916 				   struct ecore_vlan_mac_obj *o,
1917 				   union event_ring_elem *cqe,
1918 				   unsigned long *ramrod_flags)
1919 {
1920 	struct ecore_raw_obj *r = &o->raw;
1921 	int rc;
1922 
1923 	/* Clearing the pending list & raw state should be made
1924 	 * atomically (as execution flow assumes they represent the same)
1925 	 */
1926 	ECORE_SPIN_LOCK_BH(&o->exe_queue.lock);
1927 
1928 	/* Reset pending list */
1929 	__ecore_exe_queue_reset_pending(pdev, &o->exe_queue);
1930 
1931 	/* Clear pending */
1932 	r->clear_pending(r);
1933 
1934 	ECORE_SPIN_UNLOCK_BH(&o->exe_queue.lock);
1935 
1936 	/* If ramrod failed this is most likely a SW bug */
1937 	if (cqe->message.error)
1938 		return ECORE_INVAL;
1939 
1940 	/* Run the next bulk of pending commands if requested */
1941 	if (ECORE_TEST_BIT(RAMROD_CONT, ramrod_flags)) {
1942 		rc = __ecore_vlan_mac_execute_step(pdev, o, ramrod_flags);
1943 		if (rc < 0)
1944 			return rc;
1945 	}
1946 
1947 	/* If there is more work to do return PENDING */
1948 	if (!ecore_exe_queue_empty(&o->exe_queue))
1949 		return ECORE_PENDING;
1950 
1951 	return ECORE_SUCCESS;
1952 }
1953 
1954 /**
1955  * ecore_optimize_vlan_mac - optimize ADD and DEL commands.
1956  *
1957  * @pdev:	device handle
1958  * @o:		ecore_qable_obj
1959  * @elem:	ecore_exeq_elem
1960  */
ecore_optimize_vlan_mac(struct _lm_device_t * pdev,union ecore_qable_obj * qo,struct ecore_exeq_elem * elem)1961 static int ecore_optimize_vlan_mac(struct _lm_device_t *pdev,
1962 				   union ecore_qable_obj *qo,
1963 				   struct ecore_exeq_elem *elem)
1964 {
1965 	struct ecore_exeq_elem query, *pos;
1966 	struct ecore_vlan_mac_obj *o = &qo->vlan_mac;
1967 	struct ecore_exe_queue_obj *exeq = &o->exe_queue;
1968 
1969 	mm_memcpy(&query, elem, sizeof(query));
1970 
1971 	switch (elem->cmd_data.vlan_mac.cmd) {
1972 	case ECORE_VLAN_MAC_ADD:
1973 		query.cmd_data.vlan_mac.cmd = ECORE_VLAN_MAC_DEL;
1974 		break;
1975 	case ECORE_VLAN_MAC_DEL:
1976 		query.cmd_data.vlan_mac.cmd = ECORE_VLAN_MAC_ADD;
1977 		break;
1978 	default:
1979 		/* Don't handle anything other than ADD or DEL */
1980 		return 0;
1981 	}
1982 
1983 	/* If we found the appropriate element - delete it */
1984 	pos = exeq->get(exeq, &query);
1985 	if (pos) {
1986 
1987 		/* Return the credit of the optimized command */
1988 		if (!ECORE_TEST_BIT(ECORE_DONT_CONSUME_CAM_CREDIT,
1989 				     &pos->cmd_data.vlan_mac.vlan_mac_flags)) {
1990 			if ((query.cmd_data.vlan_mac.cmd ==
1991 			     ECORE_VLAN_MAC_ADD) && !o->put_credit(o)) {
1992 				ECORE_ERR("Failed to return the credit for the optimized ADD command\n");
1993 				return ECORE_INVAL;
1994 			} else if (!o->get_credit(o)) { /* VLAN_MAC_DEL */
1995 				ECORE_ERR("Failed to recover the credit from the optimized DEL command\n");
1996 				return ECORE_INVAL;
1997 			}
1998 		}
1999 
2000 		ECORE_MSG(pdev, "Optimizing %s command\n",
2001 			  (elem->cmd_data.vlan_mac.cmd == ECORE_VLAN_MAC_ADD) ?
2002 			  "ADD" : "DEL");
2003 
2004 		ECORE_LIST_REMOVE_ENTRY(&pos->link, &exeq->exe_queue);
2005 		ecore_exe_queue_free_elem(pdev, pos);
2006 		return 1;
2007 	}
2008 
2009 	return 0;
2010 }
2011 
2012 /**
2013  * ecore_vlan_mac_get_registry_elem - prepare a registry element
2014  *
2015  * @pdev:  device handle
2016  * @o:
2017  * @elem:
2018  * @restore:
2019  * @re:
2020  *
2021  * prepare a registry element according to the current command request.
2022  */
ecore_vlan_mac_get_registry_elem(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,struct ecore_exeq_elem * elem,BOOL restore,struct ecore_vlan_mac_registry_elem ** re)2023 static INLINE int ecore_vlan_mac_get_registry_elem(
2024 	struct _lm_device_t *pdev,
2025 	struct ecore_vlan_mac_obj *o,
2026 	struct ecore_exeq_elem *elem,
2027 	BOOL restore,
2028 	struct ecore_vlan_mac_registry_elem **re)
2029 {
2030 	enum ecore_vlan_mac_cmd cmd = elem->cmd_data.vlan_mac.cmd;
2031 	struct ecore_vlan_mac_registry_elem *reg_elem;
2032 
2033 	/* Allocate a new registry element if needed. */
2034 	if (!restore &&
2035 	    ((cmd == ECORE_VLAN_MAC_ADD) || (cmd == ECORE_VLAN_MAC_MOVE))) {
2036 		reg_elem = ECORE_ZALLOC(sizeof(*reg_elem), GFP_ATOMIC, pdev);
2037 		if (!reg_elem)
2038 			return ECORE_NOMEM;
2039 
2040 		/* Get a new CAM offset */
2041 		if (!o->get_cam_offset(o, &reg_elem->cam_offset)) {
2042 			/* This shall never happen, because we have checked the
2043 			 * CAM availability in the 'validate'.
2044 			 */
2045 			DbgBreakIf(1);
2046 			ECORE_FREE(pdev, reg_elem, sizeof(*reg_elem));
2047 			return ECORE_INVAL;
2048 		}
2049 
2050 		ECORE_MSG(pdev, "Got cam offset %d\n", reg_elem->cam_offset);
2051 
2052 		/* Set a VLAN-MAC data */
2053 		mm_memcpy(&reg_elem->u, &elem->cmd_data.vlan_mac.u,
2054 			  sizeof(reg_elem->u));
2055 
2056 		/* Copy the flags (needed for DEL and RESTORE flows) */
2057 		reg_elem->vlan_mac_flags =
2058 			elem->cmd_data.vlan_mac.vlan_mac_flags;
2059 	} else /* DEL, RESTORE */
2060 		reg_elem = o->check_del(pdev, o, &elem->cmd_data.vlan_mac.u);
2061 
2062 	*re = reg_elem;
2063 	return ECORE_SUCCESS;
2064 }
2065 
2066 /**
2067  * ecore_execute_vlan_mac - execute vlan mac command
2068  *
2069  * @pdev:		device handle
2070  * @qo:
2071  * @exe_chunk:
2072  * @ramrod_flags:
2073  *
2074  * go and send a ramrod!
2075  */
ecore_execute_vlan_mac(struct _lm_device_t * pdev,union ecore_qable_obj * qo,d_list_t * exe_chunk,unsigned long * ramrod_flags)2076 static int ecore_execute_vlan_mac(struct _lm_device_t *pdev,
2077 				  union ecore_qable_obj *qo,
2078 				  d_list_t *exe_chunk,
2079 				  unsigned long *ramrod_flags)
2080 {
2081 	struct ecore_exeq_elem *elem;
2082 	struct ecore_vlan_mac_obj *o = &qo->vlan_mac, *cam_obj;
2083 	struct ecore_raw_obj *r = &o->raw;
2084 	int rc, idx = 0;
2085 	BOOL restore = ECORE_TEST_BIT(RAMROD_RESTORE, ramrod_flags);
2086 	BOOL drv_only = ECORE_TEST_BIT(RAMROD_DRV_CLR_ONLY, ramrod_flags);
2087 	struct ecore_vlan_mac_registry_elem *reg_elem;
2088 	enum ecore_vlan_mac_cmd cmd;
2089 
2090 	/* If DRIVER_ONLY execution is requested, cleanup a registry
2091 	 * and exit. Otherwise send a ramrod to FW.
2092 	 */
2093 	if (!drv_only) {
2094 		DbgBreakIf(r->check_pending(r));
2095 
2096 		/* Set pending */
2097 		r->set_pending(r);
2098 
2099 		/* Fill the ramrod data */
2100 		ECORE_LIST_FOR_EACH_ENTRY(elem, exe_chunk, link,
2101 					  struct ecore_exeq_elem) {
2102 			cmd = elem->cmd_data.vlan_mac.cmd;
2103 			/* We will add to the target object in MOVE command, so
2104 			 * change the object for a CAM search.
2105 			 */
2106 			if (cmd == ECORE_VLAN_MAC_MOVE)
2107 				cam_obj = elem->cmd_data.vlan_mac.target_obj;
2108 			else
2109 				cam_obj = o;
2110 
2111 			rc = ecore_vlan_mac_get_registry_elem(pdev, cam_obj,
2112 							      elem, restore,
2113 							      &reg_elem);
2114 			if (rc)
2115 				goto error_exit;
2116 
2117 			DbgBreakIf(!reg_elem);
2118 
2119 			/* Push a new entry into the registry */
2120 			if (!restore &&
2121 			    ((cmd == ECORE_VLAN_MAC_ADD) ||
2122 			    (cmd == ECORE_VLAN_MAC_MOVE)))
2123 				ECORE_LIST_PUSH_HEAD(&reg_elem->link,
2124 						     &cam_obj->head);
2125 
2126 			/* Configure a single command in a ramrod data buffer */
2127 			o->set_one_rule(pdev, o, elem, idx,
2128 					reg_elem->cam_offset);
2129 
2130 			/* MOVE command consumes 2 entries in the ramrod data */
2131 			if (cmd == ECORE_VLAN_MAC_MOVE)
2132 				idx += 2;
2133 			else
2134 				idx++;
2135 		}
2136 
2137 		/* No need for an explicit memory barrier here as long as we
2138 		 * ensure the ordering of writing to the SPQ element
2139 		 * and updating of the SPQ producer which involves a memory
2140 		 * read. If the memory read is removed we will have to put a
2141 		 * full memory barrier there (inside ecore_sp_post()).
2142 		 */
2143 		rc = ecore_sp_post(pdev, o->ramrod_cmd, r->cid,
2144 				   r->rdata_mapping.as_u64,
2145 				   ETH_CONNECTION_TYPE);
2146 		if (rc)
2147 			goto error_exit;
2148 	}
2149 
2150 	/* Now, when we are done with the ramrod - clean up the registry */
2151 	ECORE_LIST_FOR_EACH_ENTRY(elem, exe_chunk, link,
2152 				  struct ecore_exeq_elem) {
2153 		cmd = elem->cmd_data.vlan_mac.cmd;
2154 		if ((cmd == ECORE_VLAN_MAC_DEL) ||
2155 		    (cmd == ECORE_VLAN_MAC_MOVE)) {
2156 			reg_elem = o->check_del(pdev, o,
2157 						&elem->cmd_data.vlan_mac.u);
2158 
2159 			DbgBreakIf(!reg_elem);
2160 
2161 			o->put_cam_offset(o, reg_elem->cam_offset);
2162 			ECORE_LIST_REMOVE_ENTRY(&reg_elem->link, &o->head);
2163 			ECORE_FREE(pdev, reg_elem, sizeof(*reg_elem));
2164 		}
2165 	}
2166 
2167 	if (!drv_only)
2168 		return ECORE_PENDING;
2169 	else
2170 		return ECORE_SUCCESS;
2171 
2172 error_exit:
2173 	r->clear_pending(r);
2174 
2175 	/* Cleanup a registry in case of a failure */
2176 	ECORE_LIST_FOR_EACH_ENTRY(elem, exe_chunk, link,
2177 				  struct ecore_exeq_elem) {
2178 		cmd = elem->cmd_data.vlan_mac.cmd;
2179 
2180 		if (cmd == ECORE_VLAN_MAC_MOVE)
2181 			cam_obj = elem->cmd_data.vlan_mac.target_obj;
2182 		else
2183 			cam_obj = o;
2184 
2185 		/* Delete all newly added above entries */
2186 		if (!restore &&
2187 		    ((cmd == ECORE_VLAN_MAC_ADD) ||
2188 		    (cmd == ECORE_VLAN_MAC_MOVE))) {
2189 			reg_elem = o->check_del(pdev, cam_obj,
2190 						&elem->cmd_data.vlan_mac.u);
2191 			if (reg_elem) {
2192 				ECORE_LIST_REMOVE_ENTRY(&reg_elem->link,
2193 							&cam_obj->head);
2194 				ECORE_FREE(pdev, reg_elem, sizeof(*reg_elem));
2195 			}
2196 		}
2197 	}
2198 
2199 	return rc;
2200 }
2201 
ecore_vlan_mac_push_new_cmd(struct _lm_device_t * pdev,struct ecore_vlan_mac_ramrod_params * p)2202 static INLINE int ecore_vlan_mac_push_new_cmd(
2203 	struct _lm_device_t *pdev,
2204 	struct ecore_vlan_mac_ramrod_params *p)
2205 {
2206 	struct ecore_exeq_elem *elem;
2207 	struct ecore_vlan_mac_obj *o = p->vlan_mac_obj;
2208 	BOOL restore = ECORE_TEST_BIT(RAMROD_RESTORE, &p->ramrod_flags);
2209 
2210 	/* Allocate the execution queue element */
2211 	elem = ecore_exe_queue_alloc_elem(pdev);
2212 	if (!elem)
2213 		return ECORE_NOMEM;
2214 
2215 	/* Set the command 'length' */
2216 	switch (p->user_req.cmd) {
2217 	case ECORE_VLAN_MAC_MOVE:
2218 		elem->cmd_len = 2;
2219 		break;
2220 	default:
2221 		elem->cmd_len = 1;
2222 	}
2223 
2224 	/* Fill the object specific info */
2225 	mm_memcpy(&elem->cmd_data.vlan_mac, &p->user_req, sizeof(p->user_req));
2226 
2227 	/* Try to add a new command to the pending list */
2228 	return ecore_exe_queue_add(pdev, &o->exe_queue, elem, restore);
2229 }
2230 
2231 /**
2232  * ecore_config_vlan_mac - configure VLAN/MAC/VLAN_MAC filtering rules.
2233  *
2234  * @pdev:  device handle
2235  * @p:
2236  *
2237  */
ecore_config_vlan_mac(struct _lm_device_t * pdev,struct ecore_vlan_mac_ramrod_params * p)2238 int ecore_config_vlan_mac(struct _lm_device_t *pdev,
2239 			   struct ecore_vlan_mac_ramrod_params *p)
2240 {
2241 	int rc = ECORE_SUCCESS;
2242 	struct ecore_vlan_mac_obj *o = p->vlan_mac_obj;
2243 	unsigned long *ramrod_flags = &p->ramrod_flags;
2244 	BOOL cont = ECORE_TEST_BIT(RAMROD_CONT, ramrod_flags);
2245 	struct ecore_raw_obj *raw = &o->raw;
2246 
2247 	/*
2248 	 * Add new elements to the execution list for commands that require it.
2249 	 */
2250 	if (!cont) {
2251 		rc = ecore_vlan_mac_push_new_cmd(pdev, p);
2252 		if (rc)
2253 			return rc;
2254 	}
2255 
2256 	/* If nothing will be executed further in this iteration we want to
2257 	 * return PENDING if there are pending commands
2258 	 */
2259 	if (!ecore_exe_queue_empty(&o->exe_queue))
2260 		rc = ECORE_PENDING;
2261 
2262 	if (ECORE_TEST_BIT(RAMROD_DRV_CLR_ONLY, ramrod_flags))  {
2263 		ECORE_MSG(pdev, "RAMROD_DRV_CLR_ONLY requested: clearing a pending bit.\n");
2264 		raw->clear_pending(raw);
2265 	}
2266 
2267 	/* Execute commands if required */
2268 	if (cont || ECORE_TEST_BIT(RAMROD_EXEC, ramrod_flags) ||
2269 	    ECORE_TEST_BIT(RAMROD_COMP_WAIT, ramrod_flags)) {
2270 		rc = __ecore_vlan_mac_execute_step(pdev, p->vlan_mac_obj,
2271 						   &p->ramrod_flags);
2272 		if (rc < 0)
2273 			return rc;
2274 	}
2275 
2276 	/* RAMROD_COMP_WAIT is a superset of RAMROD_EXEC. If it was set
2277 	 * then user want to wait until the last command is done.
2278 	 */
2279 	if (ECORE_TEST_BIT(RAMROD_COMP_WAIT, &p->ramrod_flags)) {
2280 		/* Wait maximum for the current exe_queue length iterations plus
2281 		 * one (for the current pending command).
2282 		 */
2283 		int max_iterations = ecore_exe_queue_length(&o->exe_queue) + 1;
2284 
2285 		while (!ecore_exe_queue_empty(&o->exe_queue) &&
2286 		       max_iterations--) {
2287 
2288 			/* Wait for the current command to complete */
2289 			rc = raw->wait_comp(pdev, raw);
2290 			if (rc)
2291 				return rc;
2292 
2293 			/* Make a next step */
2294 			rc = __ecore_vlan_mac_execute_step(pdev,
2295 							   p->vlan_mac_obj,
2296 							   &p->ramrod_flags);
2297 			if (rc < 0)
2298 				return rc;
2299 		}
2300 
2301 		return ECORE_SUCCESS;
2302 	}
2303 
2304 	return rc;
2305 }
2306 
2307 /**
2308  * ecore_vlan_mac_del_all - delete elements with given vlan_mac_flags spec
2309  *
2310  * @pdev:		device handle
2311  * @o:
2312  * @vlan_mac_flags:
2313  * @ramrod_flags:	execution flags to be used for this deletion
2314  *
2315  * if the last operation has completed successfully and there are no
2316  * more elements left, positive value if the last operation has completed
2317  * successfully and there are more previously configured elements, negative
2318  * value is current operation has failed.
2319  */
ecore_vlan_mac_del_all(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * o,unsigned long * vlan_mac_flags,unsigned long * ramrod_flags)2320 static int ecore_vlan_mac_del_all(struct _lm_device_t *pdev,
2321 				  struct ecore_vlan_mac_obj *o,
2322 				  unsigned long *vlan_mac_flags,
2323 				  unsigned long *ramrod_flags)
2324 {
2325 	struct ecore_vlan_mac_registry_elem *pos = NULL;
2326 	struct ecore_vlan_mac_ramrod_params p;
2327 	struct ecore_exe_queue_obj *exeq = &o->exe_queue;
2328 	struct ecore_exeq_elem *exeq_pos, *exeq_pos_n;
2329 	unsigned long flags;
2330 	int read_lock;
2331 	int rc = 0;
2332 
2333 	/* Clear pending commands first */
2334 
2335 	ECORE_SPIN_LOCK_BH(&exeq->lock);
2336 
2337 	ECORE_LIST_FOR_EACH_ENTRY_SAFE(exeq_pos, exeq_pos_n,
2338 				       &exeq->exe_queue, link,
2339 				       struct ecore_exeq_elem) {
2340 		flags = exeq_pos->cmd_data.vlan_mac.vlan_mac_flags;
2341 		if (ECORE_VLAN_MAC_CMP_FLAGS(flags) ==
2342 		    ECORE_VLAN_MAC_CMP_FLAGS(*vlan_mac_flags)) {
2343 			rc = exeq->remove(pdev, exeq->owner, exeq_pos);
2344 			if (rc) {
2345 				ECORE_ERR("Failed to remove command\n");
2346 				ECORE_SPIN_UNLOCK_BH(&exeq->lock);
2347 				return rc;
2348 			}
2349 			ECORE_LIST_REMOVE_ENTRY(&exeq_pos->link,
2350 						&exeq->exe_queue);
2351 			ecore_exe_queue_free_elem(pdev, exeq_pos);
2352 		}
2353 	}
2354 
2355 	ECORE_SPIN_UNLOCK_BH(&exeq->lock);
2356 
2357 	/* Prepare a command request */
2358 	mm_memset(&p, 0, sizeof(p));
2359 	p.vlan_mac_obj = o;
2360 	p.ramrod_flags = *ramrod_flags;
2361 	p.user_req.cmd = ECORE_VLAN_MAC_DEL;
2362 
2363 	/* Add all but the last VLAN-MAC to the execution queue without actually
2364 	 * execution anything.
2365 	 */
2366 	ECORE_CLEAR_BIT_NA(RAMROD_COMP_WAIT, &p.ramrod_flags);
2367 	ECORE_CLEAR_BIT_NA(RAMROD_EXEC, &p.ramrod_flags);
2368 	ECORE_CLEAR_BIT_NA(RAMROD_CONT, &p.ramrod_flags);
2369 
2370 	ECORE_MSG(pdev, "vlan_mac_del_all -- taking vlan_mac_lock (reader)\n");
2371 	read_lock = ecore_vlan_mac_h_read_lock(pdev, o);
2372 	if (read_lock != ECORE_SUCCESS)
2373 		return read_lock;
2374 
2375 	ECORE_LIST_FOR_EACH_ENTRY(pos, &o->head, link,
2376 				  struct ecore_vlan_mac_registry_elem) {
2377 		flags = pos->vlan_mac_flags;
2378 		if (ECORE_VLAN_MAC_CMP_FLAGS(flags) ==
2379 		    ECORE_VLAN_MAC_CMP_FLAGS(*vlan_mac_flags)) {
2380 			p.user_req.vlan_mac_flags = pos->vlan_mac_flags;
2381 			mm_memcpy(&p.user_req.u, &pos->u, sizeof(pos->u));
2382 			rc = ecore_config_vlan_mac(pdev, &p);
2383 			if (rc < 0) {
2384 				ECORE_ERR("Failed to add a new DEL command\n");
2385 				ecore_vlan_mac_h_read_unlock(pdev, o);
2386 				return rc;
2387 			}
2388 		}
2389 	}
2390 
2391 	ECORE_MSG(pdev, "vlan_mac_del_all -- releasing vlan_mac_lock (reader)\n");
2392 	ecore_vlan_mac_h_read_unlock(pdev, o);
2393 
2394 	p.ramrod_flags = *ramrod_flags;
2395 	ECORE_SET_BIT_NA(RAMROD_CONT, &p.ramrod_flags);
2396 
2397 	return ecore_config_vlan_mac(pdev, &p);
2398 }
2399 
ecore_init_raw_obj(struct ecore_raw_obj * raw,u8 cl_id,u32 cid,u8 func_id,void * rdata,lm_address_t rdata_mapping,int state,unsigned long * pstate,ecore_obj_type type)2400 static INLINE void ecore_init_raw_obj(struct ecore_raw_obj *raw, u8 cl_id,
2401 	u32 cid, u8 func_id, void *rdata, lm_address_t rdata_mapping, int state,
2402 	unsigned long *pstate, ecore_obj_type type)
2403 {
2404 	raw->func_id = func_id;
2405 	raw->cid = cid;
2406 	raw->cl_id = cl_id;
2407 	raw->rdata = rdata;
2408 	raw->rdata_mapping = rdata_mapping;
2409 	raw->state = state;
2410 	raw->pstate = pstate;
2411 	raw->obj_type = type;
2412 	raw->check_pending = ecore_raw_check_pending;
2413 	raw->clear_pending = ecore_raw_clear_pending;
2414 	raw->set_pending = ecore_raw_set_pending;
2415 	raw->wait_comp = ecore_raw_wait;
2416 }
2417 
ecore_init_vlan_mac_common(struct ecore_vlan_mac_obj * o,u8 cl_id,u32 cid,u8 func_id,void * rdata,lm_address_t rdata_mapping,int state,unsigned long * pstate,ecore_obj_type type,struct ecore_credit_pool_obj * macs_pool,struct ecore_credit_pool_obj * vlans_pool)2418 static INLINE void ecore_init_vlan_mac_common(struct ecore_vlan_mac_obj *o,
2419 	u8 cl_id, u32 cid, u8 func_id, void *rdata, lm_address_t rdata_mapping,
2420 	int state, unsigned long *pstate, ecore_obj_type type,
2421 	struct ecore_credit_pool_obj *macs_pool,
2422 	struct ecore_credit_pool_obj *vlans_pool)
2423 {
2424 	ECORE_LIST_INIT(&o->head);
2425 	o->head_reader = 0;
2426 	o->head_exe_request = FALSE;
2427 	o->saved_ramrod_flags = 0;
2428 
2429 	o->macs_pool = macs_pool;
2430 	o->vlans_pool = vlans_pool;
2431 
2432 	o->delete_all = ecore_vlan_mac_del_all;
2433 	o->restore = ecore_vlan_mac_restore;
2434 	o->complete = ecore_complete_vlan_mac;
2435 	o->wait = ecore_wait_vlan_mac;
2436 
2437 	ecore_init_raw_obj(&o->raw, cl_id, cid, func_id, rdata, rdata_mapping,
2438 			   state, pstate, type);
2439 }
2440 
ecore_init_mac_obj(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * mac_obj,u8 cl_id,u32 cid,u8 func_id,void * rdata,lm_address_t rdata_mapping,int state,unsigned long * pstate,ecore_obj_type type,struct ecore_credit_pool_obj * macs_pool)2441 void ecore_init_mac_obj(struct _lm_device_t *pdev,
2442 			struct ecore_vlan_mac_obj *mac_obj,
2443 			u8 cl_id, u32 cid, u8 func_id, void *rdata,
2444 			lm_address_t rdata_mapping, int state,
2445 			unsigned long *pstate, ecore_obj_type type,
2446 			struct ecore_credit_pool_obj *macs_pool)
2447 {
2448 	union ecore_qable_obj *qable_obj = (union ecore_qable_obj *)mac_obj;
2449 
2450 	ecore_init_vlan_mac_common(mac_obj, cl_id, cid, func_id, rdata,
2451 				   rdata_mapping, state, pstate, type,
2452 				   macs_pool, NULL);
2453 
2454 	/* CAM credit pool handling */
2455 	mac_obj->get_credit = ecore_get_credit_mac;
2456 	mac_obj->put_credit = ecore_put_credit_mac;
2457 	mac_obj->get_cam_offset = ecore_get_cam_offset_mac;
2458 	mac_obj->put_cam_offset = ecore_put_cam_offset_mac;
2459 
2460 	if (CHIP_IS_E1x(pdev)) {
2461 		mac_obj->set_one_rule      = ecore_set_one_mac_e1x;
2462 		mac_obj->check_del         = ecore_check_mac_del;
2463 		mac_obj->check_add         = ecore_check_mac_add;
2464 		mac_obj->check_move        = ecore_check_move_always_err;
2465 		mac_obj->ramrod_cmd        = RAMROD_CMD_ID_ETH_SET_MAC;
2466 
2467 		/* Exe Queue */
2468 		ecore_exe_queue_init(pdev,
2469 				     &mac_obj->exe_queue, 1, qable_obj,
2470 				     ecore_validate_vlan_mac,
2471 				     ecore_remove_vlan_mac,
2472 				     ecore_optimize_vlan_mac,
2473 				     ecore_execute_vlan_mac,
2474 				     ecore_exeq_get_mac);
2475 	} else {
2476 		mac_obj->set_one_rule      = ecore_set_one_mac_e2;
2477 		mac_obj->check_del         = ecore_check_mac_del;
2478 		mac_obj->check_add         = ecore_check_mac_add;
2479 		mac_obj->check_move        = ecore_check_move;
2480 		mac_obj->ramrod_cmd        =
2481 			RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES;
2482 		mac_obj->get_n_elements    = ecore_get_n_elements;
2483 
2484 		/* Exe Queue */
2485 		ecore_exe_queue_init(pdev,
2486 				     &mac_obj->exe_queue, CLASSIFY_RULES_COUNT,
2487 				     qable_obj, ecore_validate_vlan_mac,
2488 				     ecore_remove_vlan_mac,
2489 				     ecore_optimize_vlan_mac,
2490 				     ecore_execute_vlan_mac,
2491 				     ecore_exeq_get_mac);
2492 	}
2493 }
2494 
ecore_init_vlan_obj(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * vlan_obj,u8 cl_id,u32 cid,u8 func_id,void * rdata,lm_address_t rdata_mapping,int state,unsigned long * pstate,ecore_obj_type type,struct ecore_credit_pool_obj * vlans_pool)2495 void ecore_init_vlan_obj(struct _lm_device_t *pdev,
2496 			 struct ecore_vlan_mac_obj *vlan_obj,
2497 			 u8 cl_id, u32 cid, u8 func_id, void *rdata,
2498 			 lm_address_t rdata_mapping, int state,
2499 			 unsigned long *pstate, ecore_obj_type type,
2500 			 struct ecore_credit_pool_obj *vlans_pool)
2501 {
2502 	union ecore_qable_obj *qable_obj = (union ecore_qable_obj *)vlan_obj;
2503 
2504 	ecore_init_vlan_mac_common(vlan_obj, cl_id, cid, func_id, rdata,
2505 				   rdata_mapping, state, pstate, type, NULL,
2506 				   vlans_pool);
2507 
2508 	vlan_obj->get_credit = ecore_get_credit_vlan;
2509 	vlan_obj->put_credit = ecore_put_credit_vlan;
2510 	vlan_obj->get_cam_offset = ecore_get_cam_offset_vlan;
2511 	vlan_obj->put_cam_offset = ecore_put_cam_offset_vlan;
2512 
2513 	if (CHIP_IS_E1x(pdev)) {
2514 		ECORE_ERR("Do not support chips others than E2 and newer\n");
2515 		BUG();
2516 	} else {
2517 		vlan_obj->set_one_rule      = ecore_set_one_vlan_e2;
2518 		vlan_obj->check_del         = ecore_check_vlan_del;
2519 		vlan_obj->check_add         = ecore_check_vlan_add;
2520 		vlan_obj->check_move        = ecore_check_move;
2521 		vlan_obj->ramrod_cmd        =
2522 			RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES;
2523 		vlan_obj->get_n_elements    = ecore_get_n_elements;
2524 
2525 		/* Exe Queue */
2526 		ecore_exe_queue_init(pdev,
2527 				     &vlan_obj->exe_queue, CLASSIFY_RULES_COUNT,
2528 				     qable_obj, ecore_validate_vlan_mac,
2529 				     ecore_remove_vlan_mac,
2530 				     ecore_optimize_vlan_mac,
2531 				     ecore_execute_vlan_mac,
2532 				     ecore_exeq_get_vlan);
2533 	}
2534 }
2535 
ecore_init_vlan_mac_obj(struct _lm_device_t * pdev,struct ecore_vlan_mac_obj * vlan_mac_obj,u8 cl_id,u32 cid,u8 func_id,void * rdata,lm_address_t rdata_mapping,int state,unsigned long * pstate,ecore_obj_type type,struct ecore_credit_pool_obj * macs_pool,struct ecore_credit_pool_obj * vlans_pool)2536 void ecore_init_vlan_mac_obj(struct _lm_device_t *pdev,
2537 			     struct ecore_vlan_mac_obj *vlan_mac_obj,
2538 			     u8 cl_id, u32 cid, u8 func_id, void *rdata,
2539 			     lm_address_t rdata_mapping, int state,
2540 			     unsigned long *pstate, ecore_obj_type type,
2541 			     struct ecore_credit_pool_obj *macs_pool,
2542 			     struct ecore_credit_pool_obj *vlans_pool)
2543 {
2544 	union ecore_qable_obj *qable_obj =
2545 		(union ecore_qable_obj *)vlan_mac_obj;
2546 
2547 	ecore_init_vlan_mac_common(vlan_mac_obj, cl_id, cid, func_id, rdata,
2548 				   rdata_mapping, state, pstate, type,
2549 				   macs_pool, vlans_pool);
2550 
2551 	/* CAM pool handling */
2552 	vlan_mac_obj->get_credit = ecore_get_credit_vlan_mac;
2553 	vlan_mac_obj->put_credit = ecore_put_credit_vlan_mac;
2554 	/* CAM offset is relevant for 57710 and 57711 chips only which have a
2555 	 * single CAM for both MACs and VLAN-MAC pairs. So the offset
2556 	 * will be taken from MACs' pool object only.
2557 	 */
2558 	vlan_mac_obj->get_cam_offset = ecore_get_cam_offset_mac;
2559 	vlan_mac_obj->put_cam_offset = ecore_put_cam_offset_mac;
2560 
2561 	if (CHIP_IS_E1(pdev)) {
2562 		ECORE_ERR("Do not support chips others than E2\n");
2563 		BUG();
2564 	} else if (CHIP_IS_E1H(pdev)) {
2565 		vlan_mac_obj->set_one_rule      = ecore_set_one_vlan_mac_e1h;
2566 		vlan_mac_obj->check_del         = ecore_check_vlan_mac_del;
2567 		vlan_mac_obj->check_add         = ecore_check_vlan_mac_add;
2568 		vlan_mac_obj->check_move        = ecore_check_move_always_err;
2569 		vlan_mac_obj->ramrod_cmd        = RAMROD_CMD_ID_ETH_SET_MAC;
2570 
2571 		/* Exe Queue */
2572 		ecore_exe_queue_init(pdev,
2573 				     &vlan_mac_obj->exe_queue, 1, qable_obj,
2574 				     ecore_validate_vlan_mac,
2575 				     ecore_remove_vlan_mac,
2576 				     ecore_optimize_vlan_mac,
2577 				     ecore_execute_vlan_mac,
2578 				     ecore_exeq_get_vlan_mac);
2579 	} else {
2580 		vlan_mac_obj->set_one_rule      = ecore_set_one_vlan_mac_e2;
2581 		vlan_mac_obj->check_del         = ecore_check_vlan_mac_del;
2582 		vlan_mac_obj->check_add         = ecore_check_vlan_mac_add;
2583 		vlan_mac_obj->check_move        = ecore_check_move;
2584 		vlan_mac_obj->ramrod_cmd        =
2585 			RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES;
2586 
2587 		/* Exe Queue */
2588 		ecore_exe_queue_init(pdev,
2589 				     &vlan_mac_obj->exe_queue,
2590 				     CLASSIFY_RULES_COUNT,
2591 				     qable_obj, ecore_validate_vlan_mac,
2592 				     ecore_remove_vlan_mac,
2593 				     ecore_optimize_vlan_mac,
2594 				     ecore_execute_vlan_mac,
2595 				     ecore_exeq_get_vlan_mac);
2596 	}
2597 }
2598 
2599 /* RX_MODE verbs: DROP_ALL/ACCEPT_ALL/ACCEPT_ALL_MULTI/ACCEPT_ALL_VLAN/NORMAL */
__storm_memset_mac_filters(struct _lm_device_t * pdev,struct tstorm_eth_mac_filter_config * mac_filters,u16 pf_id)2600 static INLINE void __storm_memset_mac_filters(struct _lm_device_t *pdev,
2601 			struct tstorm_eth_mac_filter_config *mac_filters,
2602 			u16 pf_id)
2603 {
2604 	size_t size = sizeof(struct tstorm_eth_mac_filter_config);
2605 
2606 	u32 addr = BAR_TSTRORM_INTMEM +
2607 			TSTORM_MAC_FILTER_CONFIG_OFFSET(pf_id);
2608 
2609 	__storm_memset_struct(pdev, addr, size, (u32 *)mac_filters);
2610 }
2611 
ecore_set_rx_mode_e1x(struct _lm_device_t * pdev,struct ecore_rx_mode_ramrod_params * p)2612 static int ecore_set_rx_mode_e1x(struct _lm_device_t *pdev,
2613 				 struct ecore_rx_mode_ramrod_params *p)
2614 {
2615 	/* update the pdev MAC filter structure */
2616 	u32 mask = (1 << p->cl_id);
2617 
2618 	struct tstorm_eth_mac_filter_config *mac_filters =
2619 		(struct tstorm_eth_mac_filter_config *)p->rdata;
2620 
2621 	/* initial setting is drop-all */
2622 	u8 drop_all_ucast = 1, drop_all_mcast = 1;
2623 	u8 accp_all_ucast = 0, accp_all_bcast = 0, accp_all_mcast = 0;
2624 	u8 unmatched_unicast = 0;
2625 
2626     /* In e1x there we only take into account rx accept flag since tx switching
2627      * isn't enabled. */
2628 	if (ECORE_TEST_BIT(ECORE_ACCEPT_UNICAST, &p->rx_accept_flags))
2629 		/* accept matched ucast */
2630 		drop_all_ucast = 0;
2631 
2632 	if (ECORE_TEST_BIT(ECORE_ACCEPT_MULTICAST, &p->rx_accept_flags))
2633 		/* accept matched mcast */
2634 		drop_all_mcast = 0;
2635 
2636 	if (ECORE_TEST_BIT(ECORE_ACCEPT_ALL_UNICAST, &p->rx_accept_flags)) {
2637 		/* accept all mcast */
2638 		drop_all_ucast = 0;
2639 		accp_all_ucast = 1;
2640 	}
2641 	if (ECORE_TEST_BIT(ECORE_ACCEPT_ALL_MULTICAST, &p->rx_accept_flags)) {
2642 		/* accept all mcast */
2643 		drop_all_mcast = 0;
2644 		accp_all_mcast = 1;
2645 	}
2646 	if (ECORE_TEST_BIT(ECORE_ACCEPT_BROADCAST, &p->rx_accept_flags))
2647 		/* accept (all) bcast */
2648 		accp_all_bcast = 1;
2649 	if (ECORE_TEST_BIT(ECORE_ACCEPT_UNMATCHED, &p->rx_accept_flags))
2650 		/* accept unmatched unicasts */
2651 		unmatched_unicast = 1;
2652 
2653 	mac_filters->ucast_drop_all = drop_all_ucast ?
2654 		mac_filters->ucast_drop_all | mask :
2655 		mac_filters->ucast_drop_all & ~mask;
2656 
2657 	mac_filters->mcast_drop_all = drop_all_mcast ?
2658 		mac_filters->mcast_drop_all | mask :
2659 		mac_filters->mcast_drop_all & ~mask;
2660 
2661 	mac_filters->ucast_accept_all = accp_all_ucast ?
2662 		mac_filters->ucast_accept_all | mask :
2663 		mac_filters->ucast_accept_all & ~mask;
2664 
2665 	mac_filters->mcast_accept_all = accp_all_mcast ?
2666 		mac_filters->mcast_accept_all | mask :
2667 		mac_filters->mcast_accept_all & ~mask;
2668 
2669 	mac_filters->bcast_accept_all = accp_all_bcast ?
2670 		mac_filters->bcast_accept_all | mask :
2671 		mac_filters->bcast_accept_all & ~mask;
2672 
2673 	mac_filters->unmatched_unicast = unmatched_unicast ?
2674 		mac_filters->unmatched_unicast | mask :
2675 		mac_filters->unmatched_unicast & ~mask;
2676 
2677 	ECORE_MSG(pdev, "drop_ucast 0x%x\ndrop_mcast 0x%x\n accp_ucast 0x%x\n"
2678 			 "accp_mcast 0x%x\naccp_bcast 0x%x\n",
2679 	   mac_filters->ucast_drop_all, mac_filters->mcast_drop_all,
2680 	   mac_filters->ucast_accept_all, mac_filters->mcast_accept_all,
2681 	   mac_filters->bcast_accept_all);
2682 
2683 	/* write the MAC filter structure*/
2684 	__storm_memset_mac_filters(pdev, mac_filters, p->func_id);
2685 
2686 	/* The operation is completed */
2687 	ECORE_CLEAR_BIT(p->state, p->pstate);
2688 	smp_mb__after_atomic();
2689 
2690 	return ECORE_SUCCESS;
2691 }
2692 
2693 /* Setup ramrod data */
ecore_rx_mode_set_rdata_hdr_e2(u32 cid,struct eth_classify_header * hdr,u8 rule_cnt)2694 static INLINE void ecore_rx_mode_set_rdata_hdr_e2(u32 cid,
2695 				struct eth_classify_header *hdr,
2696 				u8 rule_cnt)
2697 {
2698 	hdr->echo = mm_cpu_to_le32(cid);
2699 	hdr->rule_cnt = rule_cnt;
2700 }
2701 
ecore_rx_mode_set_cmd_state_e2(struct _lm_device_t * pdev,unsigned long * accept_flags,struct eth_filter_rules_cmd * cmd,BOOL clear_accept_all)2702 static INLINE void ecore_rx_mode_set_cmd_state_e2(struct _lm_device_t *pdev,
2703 				unsigned long *accept_flags,
2704 				struct eth_filter_rules_cmd *cmd,
2705 				BOOL clear_accept_all)
2706 {
2707 	u16 state;
2708 
2709 	/* start with 'drop-all' */
2710 	state = ETH_FILTER_RULES_CMD_UCAST_DROP_ALL |
2711 		ETH_FILTER_RULES_CMD_MCAST_DROP_ALL;
2712 
2713 	if (ECORE_TEST_BIT(ECORE_ACCEPT_UNICAST, accept_flags))
2714 		state &= ~ETH_FILTER_RULES_CMD_UCAST_DROP_ALL;
2715 
2716 	if (ECORE_TEST_BIT(ECORE_ACCEPT_MULTICAST, accept_flags))
2717 		state &= ~ETH_FILTER_RULES_CMD_MCAST_DROP_ALL;
2718 
2719 	if (ECORE_TEST_BIT(ECORE_ACCEPT_ALL_UNICAST, accept_flags)) {
2720 		state &= ~ETH_FILTER_RULES_CMD_UCAST_DROP_ALL;
2721 		state |= ETH_FILTER_RULES_CMD_UCAST_ACCEPT_ALL;
2722 	}
2723 
2724 	if (ECORE_TEST_BIT(ECORE_ACCEPT_ALL_MULTICAST, accept_flags)) {
2725 		state |= ETH_FILTER_RULES_CMD_MCAST_ACCEPT_ALL;
2726 		state &= ~ETH_FILTER_RULES_CMD_MCAST_DROP_ALL;
2727 	}
2728 	if (ECORE_TEST_BIT(ECORE_ACCEPT_BROADCAST, accept_flags))
2729 		state |= ETH_FILTER_RULES_CMD_BCAST_ACCEPT_ALL;
2730 
2731 	if (ECORE_TEST_BIT(ECORE_ACCEPT_UNMATCHED, accept_flags)) {
2732 		state &= ~ETH_FILTER_RULES_CMD_UCAST_DROP_ALL;
2733 		state |= ETH_FILTER_RULES_CMD_UCAST_ACCEPT_UNMATCHED;
2734 	}
2735 	if (ECORE_TEST_BIT(ECORE_ACCEPT_ANY_VLAN, accept_flags))
2736 		state |= ETH_FILTER_RULES_CMD_ACCEPT_ANY_VLAN;
2737 
2738 	/* Clear ACCEPT_ALL_XXX flags for FCoE L2 Queue */
2739 	if (clear_accept_all) {
2740 		state &= ~ETH_FILTER_RULES_CMD_MCAST_ACCEPT_ALL;
2741 		state &= ~ETH_FILTER_RULES_CMD_BCAST_ACCEPT_ALL;
2742 		state &= ~ETH_FILTER_RULES_CMD_UCAST_ACCEPT_ALL;
2743 		state &= ~ETH_FILTER_RULES_CMD_UCAST_ACCEPT_UNMATCHED;
2744 	}
2745 
2746 	cmd->state = mm_cpu_to_le16(state);
2747 }
2748 
ecore_set_rx_mode_e2(struct _lm_device_t * pdev,struct ecore_rx_mode_ramrod_params * p)2749 static int ecore_set_rx_mode_e2(struct _lm_device_t *pdev,
2750 				struct ecore_rx_mode_ramrod_params *p)
2751 {
2752 	struct eth_filter_rules_ramrod_data *data = p->rdata;
2753 	int rc;
2754 	u8 rule_idx = 0;
2755 
2756 	/* Reset the ramrod data buffer */
2757 	mm_memset(data, 0, sizeof(*data));
2758 
2759 	/* Setup ramrod data */
2760 
2761 	/* Tx (internal switching) */
2762 	if (ECORE_TEST_BIT(RAMROD_TX, &p->ramrod_flags)) {
2763 		data->rules[rule_idx].client_id = p->cl_id;
2764 		data->rules[rule_idx].func_id = p->func_id;
2765 
2766 		data->rules[rule_idx].cmd_general_data =
2767 			ETH_FILTER_RULES_CMD_TX_CMD;
2768 
2769 		ecore_rx_mode_set_cmd_state_e2(pdev, &p->tx_accept_flags,
2770 					       &(data->rules[rule_idx++]),
2771 					       FALSE);
2772 	}
2773 
2774 	/* Rx */
2775 	if (ECORE_TEST_BIT(RAMROD_RX, &p->ramrod_flags)) {
2776 		data->rules[rule_idx].client_id = p->cl_id;
2777 		data->rules[rule_idx].func_id = p->func_id;
2778 
2779 		data->rules[rule_idx].cmd_general_data =
2780 			ETH_FILTER_RULES_CMD_RX_CMD;
2781 
2782 		ecore_rx_mode_set_cmd_state_e2(pdev, &p->rx_accept_flags,
2783 					       &(data->rules[rule_idx++]),
2784 					       FALSE);
2785 	}
2786 
2787 	/* If FCoE Queue configuration has been requested configure the Rx and
2788 	 * internal switching modes for this queue in separate rules.
2789 	 *
2790 	 * FCoE queue shell never be set to ACCEPT_ALL packets of any sort:
2791 	 * MCAST_ALL, UCAST_ALL, BCAST_ALL and UNMATCHED.
2792 	 */
2793 	if (ECORE_TEST_BIT(ECORE_RX_MODE_FCOE_ETH, &p->rx_mode_flags)) {
2794 		/*  Tx (internal switching) */
2795 		if (ECORE_TEST_BIT(RAMROD_TX, &p->ramrod_flags)) {
2796 			data->rules[rule_idx].client_id = FCOE_CID(pdev);
2797 			data->rules[rule_idx].func_id = p->func_id;
2798 
2799 			data->rules[rule_idx].cmd_general_data =
2800 						ETH_FILTER_RULES_CMD_TX_CMD;
2801 
2802 			ecore_rx_mode_set_cmd_state_e2(pdev, &p->tx_accept_flags,
2803 						       &(data->rules[rule_idx]),
2804 						       TRUE);
2805 			rule_idx++;
2806 		}
2807 
2808 		/* Rx */
2809 		if (ECORE_TEST_BIT(RAMROD_RX, &p->ramrod_flags)) {
2810 			data->rules[rule_idx].client_id = FCOE_CID(pdev);
2811 			data->rules[rule_idx].func_id = p->func_id;
2812 
2813 			data->rules[rule_idx].cmd_general_data =
2814 						ETH_FILTER_RULES_CMD_RX_CMD;
2815 
2816 			ecore_rx_mode_set_cmd_state_e2(pdev, &p->rx_accept_flags,
2817 						       &(data->rules[rule_idx]),
2818 						       TRUE);
2819 			rule_idx++;
2820 		}
2821 	}
2822 
2823 	/* Set the ramrod header (most importantly - number of rules to
2824 	 * configure).
2825 	 */
2826 	ecore_rx_mode_set_rdata_hdr_e2(p->cid, &data->header, rule_idx);
2827 
2828 	ECORE_MSG(pdev, "About to configure %d rules, rx_accept_flags 0x%lx, tx_accept_flags 0x%lx\n",
2829 		  data->header.rule_cnt, p->rx_accept_flags,
2830 		  p->tx_accept_flags);
2831 
2832 	/* No need for an explicit memory barrier here as long as we
2833 	 * ensure the ordering of writing to the SPQ element
2834 	 * and updating of the SPQ producer which involves a memory
2835 	 * read. If the memory read is removed we will have to put a
2836 	 * full memory barrier there (inside ecore_sp_post()).
2837 	 */
2838 
2839 	/* Send a ramrod */
2840 	rc = ecore_sp_post(pdev,
2841 			   RAMROD_CMD_ID_ETH_FILTER_RULES,
2842 			   p->cid,
2843 			   p->rdata_mapping.as_u64,
2844 			   ETH_CONNECTION_TYPE);
2845 	if (rc)
2846 		return rc;
2847 
2848 	/* Ramrod completion is pending */
2849 	return ECORE_PENDING;
2850 }
2851 
ecore_wait_rx_mode_comp_e2(struct _lm_device_t * pdev,struct ecore_rx_mode_ramrod_params * p)2852 static int ecore_wait_rx_mode_comp_e2(struct _lm_device_t *pdev,
2853 				      struct ecore_rx_mode_ramrod_params *p)
2854 {
2855 	return ecore_state_wait(pdev, p->state, p->pstate);
2856 }
2857 
ecore_empty_rx_mode_wait(struct _lm_device_t * pdev,struct ecore_rx_mode_ramrod_params * p)2858 static int ecore_empty_rx_mode_wait(struct _lm_device_t *pdev,
2859 				    struct ecore_rx_mode_ramrod_params *p)
2860 {
2861 	/* Do nothing */
2862 	return ECORE_SUCCESS;
2863 }
2864 
ecore_config_rx_mode(struct _lm_device_t * pdev,struct ecore_rx_mode_ramrod_params * p)2865 int ecore_config_rx_mode(struct _lm_device_t *pdev,
2866 			 struct ecore_rx_mode_ramrod_params *p)
2867 {
2868 	int rc;
2869 
2870 	/* Configure the new classification in the chip */
2871 	rc = p->rx_mode_obj->config_rx_mode(pdev, p);
2872 	if (rc < 0)
2873 		return rc;
2874 
2875 	/* Wait for a ramrod completion if was requested */
2876 	if (ECORE_TEST_BIT(RAMROD_COMP_WAIT, &p->ramrod_flags)) {
2877 		rc = p->rx_mode_obj->wait_comp(pdev, p);
2878 		if (rc)
2879 			return rc;
2880 	}
2881 
2882 	return rc;
2883 }
2884 
ecore_init_rx_mode_obj(struct _lm_device_t * pdev,struct ecore_rx_mode_obj * o)2885 void ecore_init_rx_mode_obj(struct _lm_device_t *pdev,
2886 			    struct ecore_rx_mode_obj *o)
2887 {
2888 	if (CHIP_IS_E1x(pdev)) {
2889 		o->wait_comp      = ecore_empty_rx_mode_wait;
2890 		o->config_rx_mode = ecore_set_rx_mode_e1x;
2891 	} else {
2892 		o->wait_comp      = ecore_wait_rx_mode_comp_e2;
2893 		o->config_rx_mode = ecore_set_rx_mode_e2;
2894 	}
2895 }
2896 
2897 /********************* Multicast verbs: SET, CLEAR ****************************/
ecore_mcast_bin_from_mac(u8 * mac)2898 static INLINE u8 ecore_mcast_bin_from_mac(u8 *mac)
2899 {
2900 	return (ecore_crc32_le(0, mac, ETH_ALEN) >> 24) & 0xff;
2901 }
2902 
2903 struct ecore_mcast_mac_elem {
2904 	d_list_entry_t link;
2905 	u8 mac[ETH_ALEN];
2906 	u8 pad[2]; /* For a natural alignment of the following buffer */
2907 };
2908 
2909 struct ecore_pending_mcast_cmd {
2910 	d_list_entry_t link;
2911 	int type; /* ECORE_MCAST_CMD_X */
2912 	union {
2913 		d_list_t macs_head;
2914 		u32 macs_num; /* Needed for DEL command */
2915 		int next_bin; /* Needed for RESTORE flow with aprox match */
2916 	} data;
2917 
2918 	BOOL done; /* set to TRUE, when the command has been handled,
2919 		    * practically used in 57712 handling only, where one pending
2920 		    * command may be handled in a few operations. As long as for
2921 		    * other chips every operation handling is completed in a
2922 		    * single ramrod, there is no need to utilize this field.
2923 		    */
2924 #ifndef ECORE_ERASE
2925 	u32 alloc_len; /* passed to ECORE_FREE */
2926 #endif
2927 };
2928 
ecore_mcast_wait(struct _lm_device_t * pdev,struct ecore_mcast_obj * o)2929 static int ecore_mcast_wait(struct _lm_device_t *pdev,
2930 			    struct ecore_mcast_obj *o)
2931 {
2932 	if (ecore_state_wait(pdev, o->sched_state, o->raw.pstate) ||
2933 			o->raw.wait_comp(pdev, &o->raw))
2934 		return ECORE_TIMEOUT;
2935 
2936 	return ECORE_SUCCESS;
2937 }
2938 
ecore_mcast_enqueue_cmd(struct _lm_device_t * pdev,struct ecore_mcast_obj * o,struct ecore_mcast_ramrod_params * p,enum ecore_mcast_cmd cmd)2939 static int ecore_mcast_enqueue_cmd(struct _lm_device_t *pdev,
2940 				   struct ecore_mcast_obj *o,
2941 				   struct ecore_mcast_ramrod_params *p,
2942 				   enum ecore_mcast_cmd cmd)
2943 {
2944 	int total_sz;
2945 	struct ecore_pending_mcast_cmd *new_cmd;
2946 	struct ecore_mcast_mac_elem *cur_mac = NULL;
2947 	struct ecore_mcast_list_elem *pos;
2948 	int macs_list_len = ((cmd == ECORE_MCAST_CMD_ADD) ?
2949 			     p->mcast_list_len : 0);
2950 
2951 	/* If the command is empty ("handle pending commands only"), break */
2952 	if (!p->mcast_list_len)
2953 		return ECORE_SUCCESS;
2954 
2955 	total_sz = sizeof(*new_cmd) +
2956 		macs_list_len * sizeof(struct ecore_mcast_mac_elem);
2957 
2958 	/* Add mcast is called under spin_lock, thus calling with GFP_ATOMIC */
2959 	new_cmd = ECORE_ZALLOC(total_sz, GFP_ATOMIC, pdev);
2960 
2961 	if (!new_cmd)
2962 		return ECORE_NOMEM;
2963 
2964 	ECORE_MSG(pdev, "About to enqueue a new %d command. macs_list_len=%d\n",
2965 		  cmd, macs_list_len);
2966 
2967 	ECORE_LIST_INIT(&new_cmd->data.macs_head);
2968 
2969 	new_cmd->type = cmd;
2970 	new_cmd->done = FALSE;
2971 #ifndef ECORE_ERASE
2972 	new_cmd->alloc_len = total_sz;
2973 #endif
2974 
2975 	switch (cmd) {
2976 	case ECORE_MCAST_CMD_ADD:
2977 		cur_mac = (struct ecore_mcast_mac_elem *)
2978 			  ((u8 *)new_cmd + sizeof(*new_cmd));
2979 
2980 		/* Push the MACs of the current command into the pending command
2981 		 * MACs list: FIFO
2982 		 */
2983 		ECORE_LIST_FOR_EACH_ENTRY(pos, &p->mcast_list, link,
2984 					  struct ecore_mcast_list_elem) {
2985 			mm_memcpy(cur_mac->mac, pos->mac, ETH_ALEN);
2986 			ECORE_LIST_PUSH_TAIL(&cur_mac->link,
2987 					     &new_cmd->data.macs_head);
2988 			cur_mac++;
2989 		}
2990 
2991 		break;
2992 
2993 	case ECORE_MCAST_CMD_DEL:
2994 		new_cmd->data.macs_num = p->mcast_list_len;
2995 		break;
2996 
2997 	case ECORE_MCAST_CMD_RESTORE:
2998 		new_cmd->data.next_bin = 0;
2999 		break;
3000 
3001 	default:
3002 		ECORE_FREE(pdev, new_cmd, total_sz);
3003 		ECORE_ERR("Unknown command: %d\n", cmd);
3004 		return ECORE_INVAL;
3005 	}
3006 
3007 	/* Push the new pending command to the tail of the pending list: FIFO */
3008 	ECORE_LIST_PUSH_TAIL(&new_cmd->link, &o->pending_cmds_head);
3009 
3010 	o->set_sched(o);
3011 
3012 	return ECORE_PENDING;
3013 }
3014 
3015 /**
3016  * ecore_mcast_get_next_bin - get the next set bin (index)
3017  *
3018  * @o:
3019  * @last:	index to start looking from (including)
3020  *
3021  * returns the next found (set) bin or a negative value if none is found.
3022  */
ecore_mcast_get_next_bin(struct ecore_mcast_obj * o,int last)3023 static INLINE int ecore_mcast_get_next_bin(struct ecore_mcast_obj *o, int last)
3024 {
3025 	int i, j, inner_start = last % BIT_VEC64_ELEM_SZ;
3026 
3027 	for (i = last / BIT_VEC64_ELEM_SZ; i < ECORE_MCAST_VEC_SZ; i++) {
3028 		if (o->registry.aprox_match.vec[i])
3029 			for (j = inner_start; j < BIT_VEC64_ELEM_SZ; j++) {
3030 				int cur_bit = j + BIT_VEC64_ELEM_SZ * i;
3031 				if (BIT_VEC64_TEST_BIT(o->registry.aprox_match.
3032 						       vec, cur_bit)) {
3033 					return cur_bit;
3034 				}
3035 			}
3036 		inner_start = 0;
3037 	}
3038 
3039 	/* None found */
3040 	return -1;
3041 }
3042 
3043 /**
3044  * ecore_mcast_clear_first_bin - find the first set bin and clear it
3045  *
3046  * @o:
3047  *
3048  * returns the index of the found bin or -1 if none is found
3049  */
ecore_mcast_clear_first_bin(struct ecore_mcast_obj * o)3050 static INLINE int ecore_mcast_clear_first_bin(struct ecore_mcast_obj *o)
3051 {
3052 	int cur_bit = ecore_mcast_get_next_bin(o, 0);
3053 
3054 	if (cur_bit >= 0)
3055 		BIT_VEC64_CLEAR_BIT(o->registry.aprox_match.vec, cur_bit);
3056 
3057 	return cur_bit;
3058 }
3059 
ecore_mcast_get_rx_tx_flag(struct ecore_mcast_obj * o)3060 static INLINE u8 ecore_mcast_get_rx_tx_flag(struct ecore_mcast_obj *o)
3061 {
3062 	struct ecore_raw_obj *raw = &o->raw;
3063 	u8 rx_tx_flag = 0;
3064 
3065 	if ((raw->obj_type == ECORE_OBJ_TYPE_TX) ||
3066 	    (raw->obj_type == ECORE_OBJ_TYPE_RX_TX))
3067 		rx_tx_flag |= ETH_MULTICAST_RULES_CMD_TX_CMD;
3068 
3069 	if ((raw->obj_type == ECORE_OBJ_TYPE_RX) ||
3070 	    (raw->obj_type == ECORE_OBJ_TYPE_RX_TX))
3071 		rx_tx_flag |= ETH_MULTICAST_RULES_CMD_RX_CMD;
3072 
3073 	return rx_tx_flag;
3074 }
3075 
ecore_mcast_set_one_rule_e2(struct _lm_device_t * pdev,struct ecore_mcast_obj * o,int idx,union ecore_mcast_config_data * cfg_data,enum ecore_mcast_cmd cmd)3076 static void ecore_mcast_set_one_rule_e2(struct _lm_device_t *pdev,
3077 					struct ecore_mcast_obj *o, int idx,
3078 					union ecore_mcast_config_data *cfg_data,
3079 					enum ecore_mcast_cmd cmd)
3080 {
3081 	struct ecore_raw_obj *r = &o->raw;
3082 	struct eth_multicast_rules_ramrod_data *data =
3083 		(struct eth_multicast_rules_ramrod_data *)(r->rdata);
3084 	u8 func_id = r->func_id;
3085 	u8 rx_tx_add_flag = ecore_mcast_get_rx_tx_flag(o);
3086 	int bin;
3087 
3088 	if ((cmd == ECORE_MCAST_CMD_ADD) || (cmd == ECORE_MCAST_CMD_RESTORE))
3089 		rx_tx_add_flag |= ETH_MULTICAST_RULES_CMD_IS_ADD;
3090 
3091 	data->rules[idx].cmd_general_data |= rx_tx_add_flag;
3092 
3093 	/* Get a bin and update a bins' vector */
3094 	switch (cmd) {
3095 	case ECORE_MCAST_CMD_ADD:
3096 		bin = ecore_mcast_bin_from_mac(cfg_data->mac);
3097 		BIT_VEC64_SET_BIT(o->registry.aprox_match.vec, bin);
3098 		break;
3099 
3100 	case ECORE_MCAST_CMD_DEL:
3101 		/* If there were no more bins to clear
3102 		 * (ecore_mcast_clear_first_bin() returns -1) then we would
3103 		 * clear any (0xff) bin.
3104 		 * See ecore_mcast_validate_e2() for explanation when it may
3105 		 * happen.
3106 		 */
3107 		bin = ecore_mcast_clear_first_bin(o);
3108 		break;
3109 
3110 	case ECORE_MCAST_CMD_RESTORE:
3111 		bin = cfg_data->bin;
3112 		break;
3113 
3114 	default:
3115 		ECORE_ERR("Unknown command: %d\n", cmd);
3116 		return;
3117 	}
3118 
3119 	ECORE_MSG(pdev, "%s bin %d\n",
3120 		  ((rx_tx_add_flag & ETH_MULTICAST_RULES_CMD_IS_ADD) ?
3121 		   "Setting"  : "Clearing"), bin);
3122 
3123 	data->rules[idx].bin_id    = (u8)bin;
3124 	data->rules[idx].