xref: /illumos-gate/usr/src/uts/common/io/nxge/nxge_fflp.c (revision e3d11eee)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <npi_fflp.h>
28 #include <npi_mac.h>
29 #include <nxge_defs.h>
30 #include <nxge_flow.h>
31 #include <nxge_fflp.h>
32 #include <nxge_impl.h>
33 #include <nxge_fflp_hash.h>
34 #include <nxge_common.h>
35 
36 
37 /*
38  * Function prototypes
39  */
40 static nxge_status_t nxge_fflp_vlan_tbl_clear_all(p_nxge_t);
41 static nxge_status_t nxge_fflp_tcam_invalidate_all(p_nxge_t);
42 static nxge_status_t nxge_fflp_tcam_init(p_nxge_t);
43 static nxge_status_t nxge_fflp_fcram_invalidate_all(p_nxge_t);
44 static nxge_status_t nxge_fflp_fcram_init(p_nxge_t);
45 static int nxge_flow_need_hash_lookup(p_nxge_t, flow_resource_t *);
46 static void nxge_fill_tcam_entry_tcp(p_nxge_t, flow_spec_t *, tcam_entry_t *);
47 static void nxge_fill_tcam_entry_udp(p_nxge_t, flow_spec_t *, tcam_entry_t *);
48 static void nxge_fill_tcam_entry_sctp(p_nxge_t, flow_spec_t *, tcam_entry_t *);
49 static void nxge_fill_tcam_entry_tcp_ipv6(p_nxge_t, flow_spec_t *,
50 	tcam_entry_t *);
51 static void nxge_fill_tcam_entry_udp_ipv6(p_nxge_t, flow_spec_t *,
52 	tcam_entry_t *);
53 static void nxge_fill_tcam_entry_sctp_ipv6(p_nxge_t, flow_spec_t *,
54 	tcam_entry_t *);
55 static uint8_t nxge_get_rdc_offset(p_nxge_t, uint8_t, uint64_t);
56 static uint8_t nxge_get_rdc_group(p_nxge_t, uint8_t, uint64_t);
57 static uint16_t nxge_tcam_get_index(p_nxge_t, uint16_t);
58 static uint32_t nxge_tcam_cls_to_flow(uint32_t);
59 static uint8_t nxge_iptun_pkt_type_to_pid(uint8_t);
60 static npi_status_t nxge_set_iptun_usr_cls_reg(p_nxge_t, uint64_t,
61 					iptun_cfg_t *);
62 static boolean_t nxge_is_iptun_cls_present(p_nxge_t, uint8_t, int *);
63 
64 /*
65  * functions used outside this file
66  */
67 nxge_status_t nxge_fflp_config_vlan_table(p_nxge_t, uint16_t);
68 nxge_status_t nxge_fflp_ip_class_config_all(p_nxge_t);
69 nxge_status_t nxge_add_flow(p_nxge_t, flow_resource_t *);
70 static nxge_status_t nxge_tcam_handle_ip_fragment(p_nxge_t);
71 nxge_status_t nxge_add_tcam_entry(p_nxge_t, flow_resource_t *);
72 nxge_status_t nxge_add_fcram_entry(p_nxge_t, flow_resource_t *);
73 nxge_status_t nxge_flow_get_hash(p_nxge_t, flow_resource_t *,
74 	uint32_t *, uint16_t *);
75 int nxge_get_valid_tcam_cnt(p_nxge_t);
76 void nxge_get_tcam_entry_all(p_nxge_t, rx_class_cfg_t *);
77 void nxge_get_tcam_entry(p_nxge_t, flow_resource_t *);
78 void nxge_del_tcam_entry(p_nxge_t, uint32_t);
79 void nxge_add_iptun_class(p_nxge_t, iptun_cfg_t *, uint8_t *);
80 void nxge_cfg_iptun_hash(p_nxge_t, iptun_cfg_t *, uint8_t);
81 void nxge_del_iptun_class(p_nxge_t, uint8_t);
82 void nxge_get_iptun_class(p_nxge_t, iptun_cfg_t *, uint8_t);
83 void nxge_set_ip_cls_sym(p_nxge_t, uint8_t, uint8_t);
84 void nxge_get_ip_cls_sym(p_nxge_t, uint8_t, uint8_t *);
85 
86 
87 nxge_status_t
88 nxge_tcam_dump_entry(p_nxge_t nxgep, uint32_t location)
89 {
90 	tcam_entry_t tcam_rdptr;
91 	uint64_t asc_ram = 0;
92 	npi_handle_t handle;
93 	npi_status_t status;
94 
95 	handle = nxgep->npi_reg_handle;
96 
97 	bzero((char *)&tcam_rdptr, sizeof (struct tcam_entry));
98 	status = npi_fflp_tcam_entry_read(handle, (tcam_location_t)location,
99 	    (struct tcam_entry *)&tcam_rdptr);
100 	if (status & NPI_FAILURE) {
101 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
102 		    " nxge_tcam_dump_entry:"
103 		    "  tcam read failed at location %d ", location));
104 		return (NXGE_ERROR);
105 	}
106 	status = npi_fflp_tcam_asc_ram_entry_read(handle,
107 	    (tcam_location_t)location, &asc_ram);
108 
109 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "location %x\n"
110 	    " key:  %llx %llx %llx %llx \n"
111 	    " mask: %llx %llx %llx %llx \n"
112 	    " ASC RAM %llx \n", location,
113 	    tcam_rdptr.key0, tcam_rdptr.key1,
114 	    tcam_rdptr.key2, tcam_rdptr.key3,
115 	    tcam_rdptr.mask0, tcam_rdptr.mask1,
116 	    tcam_rdptr.mask2, tcam_rdptr.mask3, asc_ram));
117 	return (NXGE_OK);
118 }
119 
120 void
121 nxge_get_tcam(p_nxge_t nxgep, p_mblk_t mp)
122 {
123 	uint32_t tcam_loc;
124 	int *lptr;
125 	int location;
126 
127 	uint32_t start_location = 0;
128 	uint32_t stop_location = nxgep->classifier.tcam_size;
129 	lptr = (int *)mp->b_rptr;
130 	location = *lptr;
131 
132 	if ((location >= nxgep->classifier.tcam_size) || (location < -1)) {
133 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
134 		    "nxge_tcam_dump: Invalid location %d \n", location));
135 		return;
136 	}
137 	if (location == -1) {
138 		start_location = 0;
139 		stop_location = nxgep->classifier.tcam_size;
140 	} else {
141 		start_location = location;
142 		stop_location = location + 1;
143 	}
144 	for (tcam_loc = start_location; tcam_loc < stop_location; tcam_loc++)
145 		(void) nxge_tcam_dump_entry(nxgep, tcam_loc);
146 }
147 
148 /*
149  * nxge_fflp_vlan_table_invalidate_all
150  * invalidates the vlan RDC table entries.
151  * INPUT
152  * nxge    soft state data structure
153  * Return
154  *      NXGE_OK
155  *      NXGE_ERROR
156  *
157  */
158 
159 static nxge_status_t
160 nxge_fflp_vlan_tbl_clear_all(p_nxge_t nxgep)
161 {
162 	vlan_id_t vlan_id;
163 	npi_handle_t handle;
164 	npi_status_t rs = NPI_SUCCESS;
165 	vlan_id_t start = 0, stop = NXGE_MAX_VLANS;
166 
167 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_vlan_tbl_clear_all "));
168 	handle = nxgep->npi_reg_handle;
169 	for (vlan_id = start; vlan_id < stop; vlan_id++) {
170 		rs = npi_fflp_cfg_vlan_table_clear(handle, vlan_id);
171 		if (rs != NPI_SUCCESS) {
172 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
173 			    "VLAN Table invalidate failed for vlan id %d ",
174 			    vlan_id));
175 			return (NXGE_ERROR | rs);
176 		}
177 	}
178 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_vlan_tbl_clear_all "));
179 	return (NXGE_OK);
180 }
181 
182 /*
183  * The following functions are used by other modules to init
184  * the fflp module.
185  * these functions are the basic API used to init
186  * the fflp modules (tcam, fcram etc ......)
187  *
188  * The TCAM search future would be disabled  by default.
189  */
190 
191 static nxge_status_t
192 nxge_fflp_tcam_init(p_nxge_t nxgep)
193 {
194 	uint8_t access_ratio;
195 	tcam_class_t class;
196 	npi_status_t rs = NPI_SUCCESS;
197 	npi_handle_t handle;
198 
199 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_tcam_init"));
200 	handle = nxgep->npi_reg_handle;
201 
202 	rs = npi_fflp_cfg_tcam_disable(handle);
203 	if (rs != NPI_SUCCESS) {
204 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "failed TCAM Disable\n"));
205 		return (NXGE_ERROR | rs);
206 	}
207 
208 	access_ratio = nxgep->param_arr[param_tcam_access_ratio].value;
209 	rs = npi_fflp_cfg_tcam_access(handle, access_ratio);
210 	if (rs != NPI_SUCCESS) {
211 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
212 		    "failed TCAM Access cfg\n"));
213 		return (NXGE_ERROR | rs);
214 	}
215 
216 	/* disable configurable classes */
217 	/* disable the configurable ethernet classes; */
218 	for (class = TCAM_CLASS_ETYPE_1;
219 	    class <= TCAM_CLASS_ETYPE_2; class++) {
220 		rs = npi_fflp_cfg_enet_usr_cls_disable(handle, class);
221 		if (rs != NPI_SUCCESS) {
222 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
223 			    "TCAM USR Ether Class config failed."));
224 			return (NXGE_ERROR | rs);
225 		}
226 	}
227 
228 	/* disable the configurable ip classes; */
229 	for (class = TCAM_CLASS_IP_USER_4;
230 	    class <= TCAM_CLASS_IP_USER_7; class++) {
231 		rs = npi_fflp_cfg_ip_usr_cls_disable(handle, class);
232 		if (rs != NPI_SUCCESS) {
233 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
234 			    "TCAM USR IP Class cnfg failed."));
235 			return (NXGE_ERROR | rs);
236 		}
237 	}
238 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_tcam_init"));
239 	return (NXGE_OK);
240 }
241 
242 /*
243  * nxge_fflp_tcam_invalidate_all
244  * invalidates all the tcam entries.
245  * INPUT
246  * nxge    soft state data structure
247  * Return
248  *      NXGE_OK
249  *      NXGE_ERROR
250  *
251  */
252 
253 
254 static nxge_status_t
255 nxge_fflp_tcam_invalidate_all(p_nxge_t nxgep)
256 {
257 	uint16_t location;
258 	npi_status_t rs = NPI_SUCCESS;
259 	npi_handle_t handle;
260 	uint16_t start = 0, stop = nxgep->classifier.tcam_size;
261 	p_nxge_hw_list_t hw_p;
262 
263 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
264 	    "==> nxge_fflp_tcam_invalidate_all"));
265 	handle = nxgep->npi_reg_handle;
266 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
267 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
268 		    " nxge_fflp_tcam_invalidate_all:"
269 		    " common hardware not set", nxgep->niu_type));
270 		return (NXGE_ERROR);
271 	}
272 	MUTEX_ENTER(&hw_p->nxge_tcam_lock);
273 	for (location = start; location < stop; location++) {
274 		rs = npi_fflp_tcam_entry_invalidate(handle, location);
275 		if (rs != NPI_SUCCESS) {
276 			MUTEX_EXIT(&hw_p->nxge_tcam_lock);
277 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
278 			    "TCAM invalidate failed at loc %d ", location));
279 			return (NXGE_ERROR | rs);
280 		}
281 	}
282 	MUTEX_EXIT(&hw_p->nxge_tcam_lock);
283 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
284 	    "<== nxge_fflp_tcam_invalidate_all"));
285 	return (NXGE_OK);
286 }
287 
288 /*
289  * nxge_fflp_fcram_entry_invalidate_all
290  * invalidates all the FCRAM entries.
291  * INPUT
292  * nxge    soft state data structure
293  * Return
294  *      NXGE_OK
295  *      NXGE_ERROR
296  *
297  */
298 
299 static nxge_status_t
300 nxge_fflp_fcram_invalidate_all(p_nxge_t nxgep)
301 {
302 	npi_handle_t handle;
303 	npi_status_t rs = NPI_SUCCESS;
304 	part_id_t pid = 0;
305 	uint8_t base_mask, base_reloc;
306 	fcram_entry_t fc;
307 	uint32_t location;
308 	uint32_t increment, last_location;
309 
310 	/*
311 	 * (1) configure and enable partition 0 with no relocation
312 	 * (2) Assume the FCRAM is used as IPv4 exact match entry cells
313 	 * (3) Invalidate these cells by clearing the valid bit in
314 	 * the subareas 0 and 4
315 	 * (4) disable the partition
316 	 *
317 	 */
318 
319 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_fcram_invalidate_all"));
320 
321 	base_mask = base_reloc = 0x0;
322 	handle = nxgep->npi_reg_handle;
323 	rs = npi_fflp_cfg_fcram_partition(handle, pid, base_mask, base_reloc);
324 
325 	if (rs != NPI_SUCCESS) {
326 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "failed partition cfg\n"));
327 		return (NXGE_ERROR | rs);
328 	}
329 	rs = npi_fflp_cfg_fcram_partition_disable(handle, pid);
330 
331 	if (rs != NPI_SUCCESS) {
332 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
333 		    "failed partition enable\n"));
334 		return (NXGE_ERROR | rs);
335 	}
336 	fc.dreg[0].value = 0;
337 	fc.hash_hdr_valid = 0;
338 	fc.hash_hdr_ext = 1;	/* specify as IPV4 exact match entry */
339 	increment = sizeof (hash_ipv4_t);
340 	last_location = FCRAM_SIZE * 0x40;
341 
342 	for (location = 0; location < last_location; location += increment) {
343 		rs = npi_fflp_fcram_subarea_write(handle, pid,
344 		    location, fc.value[0]);
345 		if (rs != NPI_SUCCESS) {
346 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
347 			    "failed write at location %x ", location));
348 			return (NXGE_ERROR | rs);
349 		}
350 	}
351 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_fcram_invalidate_all"));
352 	return (NXGE_OK);
353 }
354 
355 static nxge_status_t
356 nxge_fflp_fcram_init(p_nxge_t nxgep)
357 {
358 	fflp_fcram_output_drive_t strength;
359 	fflp_fcram_qs_t qs;
360 	npi_status_t rs = NPI_SUCCESS;
361 	uint8_t access_ratio;
362 	int partition;
363 	npi_handle_t handle;
364 	uint32_t min_time, max_time, sys_time;
365 
366 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_fcram_init"));
367 
368 	/*
369 	 * Recommended values are needed.
370 	 */
371 	min_time = FCRAM_REFRESH_DEFAULT_MIN_TIME;
372 	max_time = FCRAM_REFRESH_DEFAULT_MAX_TIME;
373 	sys_time = FCRAM_REFRESH_DEFAULT_SYS_TIME;
374 
375 	handle = nxgep->npi_reg_handle;
376 	strength = FCRAM_OUTDR_NORMAL;
377 	qs = FCRAM_QS_MODE_QS;
378 	rs = npi_fflp_cfg_fcram_reset(handle, strength, qs);
379 	if (rs != NPI_SUCCESS) {
380 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "failed FCRAM Reset. "));
381 		return (NXGE_ERROR | rs);
382 	}
383 
384 	access_ratio = nxgep->param_arr[param_fcram_access_ratio].value;
385 	rs = npi_fflp_cfg_fcram_access(handle, access_ratio);
386 	if (rs != NPI_SUCCESS) {
387 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "failed FCRAM Access ratio"
388 		    "configuration \n"));
389 		return (NXGE_ERROR | rs);
390 	}
391 	rs = npi_fflp_cfg_fcram_refresh_time(handle, min_time,
392 	    max_time, sys_time);
393 	if (rs != NPI_SUCCESS) {
394 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
395 		    "failed FCRAM refresh cfg"));
396 		return (NXGE_ERROR);
397 	}
398 
399 	/* disable all the partitions until explicitly enabled */
400 	for (partition = 0; partition < FFLP_FCRAM_MAX_PARTITION; partition++) {
401 		rs = npi_fflp_cfg_fcram_partition_disable(handle, partition);
402 		if (rs != NPI_SUCCESS) {
403 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
404 			    "failed FCRAM partition"
405 			    " enable for partition %d ", partition));
406 			return (NXGE_ERROR | rs);
407 		}
408 	}
409 
410 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_fcram_init"));
411 	return (NXGE_OK);
412 }
413 
414 nxge_status_t
415 nxge_logical_mac_assign_rdc_table(p_nxge_t nxgep, uint8_t alt_mac)
416 {
417 	npi_status_t rs = NPI_SUCCESS;
418 	hostinfo_t mac_rdc;
419 	npi_handle_t handle;
420 	p_nxge_class_pt_cfg_t p_class_cfgp;
421 
422 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
423 	if (p_class_cfgp->mac_host_info[alt_mac].flag == 0) {
424 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
425 		    " nxge_logical_mac_assign_rdc_table"
426 		    " unconfigured alt MAC addr %d ", alt_mac));
427 		return (NXGE_ERROR);
428 	}
429 	handle = nxgep->npi_reg_handle;
430 	mac_rdc.value = 0;
431 	mac_rdc.bits.w0.rdc_tbl_num =
432 	    p_class_cfgp->mac_host_info[alt_mac].rdctbl;
433 	mac_rdc.bits.w0.mac_pref = p_class_cfgp->mac_host_info[alt_mac].mpr_npr;
434 
435 	rs = npi_mac_hostinfo_entry(handle, OP_SET,
436 	    nxgep->function_num, alt_mac, &mac_rdc);
437 
438 	if (rs != NPI_SUCCESS) {
439 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
440 		    "failed Assign RDC table"));
441 		return (NXGE_ERROR | rs);
442 	}
443 	return (NXGE_OK);
444 }
445 
446 nxge_status_t
447 nxge_main_mac_assign_rdc_table(p_nxge_t nxgep)
448 {
449 	npi_status_t rs = NPI_SUCCESS;
450 	hostinfo_t mac_rdc;
451 	npi_handle_t handle;
452 	int i;
453 
454 	handle = nxgep->npi_reg_handle;
455 	mac_rdc.value = 0;
456 	mac_rdc.bits.w0.rdc_tbl_num = nxgep->class_config.mac_rdcgrp;
457 	mac_rdc.bits.w0.mac_pref = 1;
458 	switch (nxgep->function_num) {
459 	case 0:
460 	case 1:
461 		/*
462 		 * Tests indicate that it is OK not to re-initialize the
463 		 * hostinfo registers for the XMAC's alternate MAC
464 		 * addresses. But that is necessary for BMAC (case 2
465 		 * and case 3 below)
466 		 */
467 		rs = npi_mac_hostinfo_entry(handle, OP_SET,
468 		    nxgep->function_num, XMAC_UNIQUE_HOST_INFO_ENTRY, &mac_rdc);
469 		break;
470 	case 2:
471 	case 3:
472 		rs = npi_mac_hostinfo_entry(handle, OP_SET,
473 		    nxgep->function_num, BMAC_UNIQUE_HOST_INFO_ENTRY, &mac_rdc);
474 		for (i = 1; i <= BMAC_MAX_ALT_ADDR_ENTRY; i++)
475 			rs |= npi_mac_hostinfo_entry(handle, OP_SET,
476 			    nxgep->function_num, i, &mac_rdc);
477 		break;
478 	default:
479 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
480 		    "failed Assign RDC table (invalid function #)"));
481 		return (NXGE_ERROR);
482 	}
483 
484 	if (rs != NPI_SUCCESS) {
485 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
486 		    "failed Assign RDC table"));
487 		return (NXGE_ERROR | rs);
488 	}
489 	return (NXGE_OK);
490 }
491 
492 /*
493  * Initialize hostinfo registers for alternate MAC addresses and
494  * multicast MAC address.
495  */
496 nxge_status_t
497 nxge_alt_mcast_mac_assign_rdc_table(p_nxge_t nxgep)
498 {
499 	npi_status_t rs = NPI_SUCCESS;
500 	hostinfo_t mac_rdc;
501 	npi_handle_t handle;
502 
503 	handle = nxgep->npi_reg_handle;
504 	mac_rdc.value = 0;
505 	mac_rdc.bits.w0.rdc_tbl_num = nxgep->class_config.mcast_rdcgrp;
506 	mac_rdc.bits.w0.mac_pref = 1;
507 	switch (nxgep->function_num) {
508 	case 0:
509 	case 1:
510 		rs = npi_mac_hostinfo_entry(handle, OP_SET,
511 		    nxgep->function_num, XMAC_MULTI_HOST_INFO_ENTRY, &mac_rdc);
512 		break;
513 	case 2:
514 	case 3:
515 		rs = npi_mac_hostinfo_entry(handle, OP_SET,
516 		    nxgep->function_num, BMAC_MULTI_HOST_INFO_ENTRY, &mac_rdc);
517 		break;
518 	default:
519 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
520 		    "failed Assign RDC table (invalid function #)"));
521 		return (NXGE_ERROR);
522 	}
523 
524 	if (rs != NPI_SUCCESS) {
525 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
526 		    "failed Assign RDC table"));
527 		return (NXGE_ERROR | rs);
528 	}
529 	return (NXGE_OK);
530 }
531 
532 nxge_status_t
533 nxge_fflp_init_hostinfo(p_nxge_t nxgep)
534 {
535 	nxge_status_t status = NXGE_OK;
536 
537 	status = nxge_alt_mcast_mac_assign_rdc_table(nxgep);
538 	status |= nxge_main_mac_assign_rdc_table(nxgep);
539 	return (status);
540 }
541 
542 nxge_status_t
543 nxge_fflp_hw_reset(p_nxge_t nxgep)
544 {
545 	npi_handle_t handle;
546 	npi_status_t rs = NPI_SUCCESS;
547 	nxge_status_t status = NXGE_OK;
548 
549 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_fflp_hw_reset"));
550 
551 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
552 		status = nxge_fflp_fcram_init(nxgep);
553 		if (status != NXGE_OK) {
554 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
555 			    " failed FCRAM init. "));
556 			return (status);
557 		}
558 	}
559 
560 	status = nxge_fflp_tcam_init(nxgep);
561 	if (status != NXGE_OK) {
562 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
563 		    "failed TCAM init."));
564 		return (status);
565 	}
566 
567 	handle = nxgep->npi_reg_handle;
568 	rs = npi_fflp_cfg_llcsnap_enable(handle);
569 	if (rs != NPI_SUCCESS) {
570 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
571 		    "failed LLCSNAP enable. "));
572 		return (NXGE_ERROR | rs);
573 	}
574 
575 	rs = npi_fflp_cfg_cam_errorcheck_disable(handle);
576 	if (rs != NPI_SUCCESS) {
577 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
578 		    "failed CAM Error Check enable. "));
579 		return (NXGE_ERROR | rs);
580 	}
581 
582 	/* init the hash generators */
583 	rs = npi_fflp_cfg_hash_h1poly(handle, 0);
584 	if (rs != NPI_SUCCESS) {
585 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
586 		    "failed H1 Poly Init. "));
587 		return (NXGE_ERROR | rs);
588 	}
589 
590 	rs = npi_fflp_cfg_hash_h2poly(handle, 0);
591 	if (rs != NPI_SUCCESS) {
592 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
593 		    "failed H2 Poly Init. "));
594 		return (NXGE_ERROR | rs);
595 	}
596 
597 	/* invalidate TCAM entries */
598 	status = nxge_fflp_tcam_invalidate_all(nxgep);
599 	if (status != NXGE_OK) {
600 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
601 		    "failed TCAM Entry Invalidate. "));
602 		return (status);
603 	}
604 
605 	/* invalidate FCRAM entries */
606 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
607 		status = nxge_fflp_fcram_invalidate_all(nxgep);
608 		if (status != NXGE_OK) {
609 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
610 			    "failed FCRAM Entry Invalidate."));
611 			return (status);
612 		}
613 	}
614 
615 	/* invalidate VLAN RDC tables */
616 	status = nxge_fflp_vlan_tbl_clear_all(nxgep);
617 	if (status != NXGE_OK) {
618 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
619 		    "failed VLAN Table Invalidate. "));
620 		return (status);
621 	}
622 	nxgep->classifier.state |= NXGE_FFLP_HW_RESET;
623 
624 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_hw_reset"));
625 	return (NXGE_OK);
626 }
627 
628 nxge_status_t
629 nxge_cfg_ip_cls_flow_key(p_nxge_t nxgep, tcam_class_t l3_class,
630 	uint32_t class_config)
631 {
632 	flow_key_cfg_t fcfg;
633 	npi_handle_t handle;
634 	npi_status_t rs = NPI_SUCCESS;
635 
636 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_cfg_ip_cls_flow_key"));
637 	handle = nxgep->npi_reg_handle;
638 	bzero(&fcfg, sizeof (flow_key_cfg_t));
639 
640 	if (class_config & NXGE_CLASS_FLOW_USE_PROTO)
641 		fcfg.use_proto = 1;
642 	if (class_config & NXGE_CLASS_FLOW_USE_DST_PORT)
643 		fcfg.use_dport = 1;
644 	if (class_config & NXGE_CLASS_FLOW_USE_SRC_PORT)
645 		fcfg.use_sport = 1;
646 	if (class_config & NXGE_CLASS_FLOW_USE_IPDST)
647 		fcfg.use_daddr = 1;
648 	if (class_config & NXGE_CLASS_FLOW_USE_IPSRC)
649 		fcfg.use_saddr = 1;
650 	if (class_config & NXGE_CLASS_FLOW_USE_VLAN)
651 		fcfg.use_vlan = 1;
652 	if (class_config & NXGE_CLASS_FLOW_USE_L2DA)
653 		fcfg.use_l2da = 1;
654 	if (class_config & NXGE_CLASS_FLOW_USE_PORTNUM)
655 		fcfg.use_portnum = 1;
656 	fcfg.ip_opts_exist = 0;
657 
658 	rs = npi_fflp_cfg_ip_cls_flow_key(handle, l3_class, &fcfg);
659 	if (rs & NPI_FFLP_ERROR) {
660 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, " nxge_cfg_ip_cls_flow_key"
661 		    " opt %x for class %d failed ", class_config, l3_class));
662 		return (NXGE_ERROR | rs);
663 	}
664 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " <== nxge_cfg_ip_cls_flow_key"));
665 	return (NXGE_OK);
666 }
667 
668 nxge_status_t
669 nxge_cfg_ip_cls_flow_key_get(p_nxge_t nxgep, tcam_class_t l3_class,
670 	uint32_t *class_config)
671 {
672 	flow_key_cfg_t fcfg;
673 	npi_handle_t handle;
674 	npi_status_t rs = NPI_SUCCESS;
675 	uint32_t ccfg = 0;
676 
677 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_cfg_ip_cls_flow_key_get"));
678 	handle = nxgep->npi_reg_handle;
679 	bzero(&fcfg, sizeof (flow_key_cfg_t));
680 
681 	rs = npi_fflp_cfg_ip_cls_flow_key_get(handle, l3_class, &fcfg);
682 	if (rs & NPI_FFLP_ERROR) {
683 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, " nxge_cfg_ip_cls_flow_key"
684 		    " opt %x for class %d failed ", class_config, l3_class));
685 		return (NXGE_ERROR | rs);
686 	}
687 
688 	if (fcfg.use_proto)
689 		ccfg |= NXGE_CLASS_FLOW_USE_PROTO;
690 	if (fcfg.use_dport)
691 		ccfg |= NXGE_CLASS_FLOW_USE_DST_PORT;
692 	if (fcfg.use_sport)
693 		ccfg |= NXGE_CLASS_FLOW_USE_SRC_PORT;
694 	if (fcfg.use_daddr)
695 		ccfg |= NXGE_CLASS_FLOW_USE_IPDST;
696 	if (fcfg.use_saddr)
697 		ccfg |= NXGE_CLASS_FLOW_USE_IPSRC;
698 	if (fcfg.use_vlan)
699 		ccfg |= NXGE_CLASS_FLOW_USE_VLAN;
700 	if (fcfg.use_l2da)
701 		ccfg |= NXGE_CLASS_FLOW_USE_L2DA;
702 	if (fcfg.use_portnum)
703 		ccfg |= NXGE_CLASS_FLOW_USE_PORTNUM;
704 
705 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
706 	    " nxge_cfg_ip_cls_flow_key_get %x", ccfg));
707 	*class_config = ccfg;
708 
709 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
710 	    " <== nxge_cfg_ip_cls_flow_key_get"));
711 	return (NXGE_OK);
712 }
713 
714 static nxge_status_t
715 nxge_cfg_tcam_ip_class_get(p_nxge_t nxgep, tcam_class_t class,
716 	uint32_t *class_config)
717 {
718 	npi_status_t rs = NPI_SUCCESS;
719 	tcam_key_cfg_t cfg;
720 	npi_handle_t handle;
721 	uint32_t ccfg = 0;
722 
723 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_cfg_tcam_ip_class"));
724 
725 	bzero(&cfg, sizeof (tcam_key_cfg_t));
726 	handle = nxgep->npi_reg_handle;
727 
728 	rs = npi_fflp_cfg_ip_cls_tcam_key_get(handle, class, &cfg);
729 	if (rs & NPI_FFLP_ERROR) {
730 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, " nxge_cfg_tcam_ip_class"
731 		    " opt %x for class %d failed ", class_config, class));
732 		return (NXGE_ERROR | rs);
733 	}
734 	if (cfg.discard)
735 		ccfg |= NXGE_CLASS_DISCARD;
736 	if (cfg.lookup_enable)
737 		ccfg |= NXGE_CLASS_TCAM_LOOKUP;
738 	if (cfg.use_ip_daddr)
739 		ccfg |= NXGE_CLASS_TCAM_USE_SRC_ADDR;
740 	*class_config = ccfg;
741 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
742 	    " ==> nxge_cfg_tcam_ip_class %x", ccfg));
743 	return (NXGE_OK);
744 }
745 
746 static nxge_status_t
747 nxge_cfg_tcam_ip_class(p_nxge_t nxgep, tcam_class_t class,
748 	uint32_t class_config)
749 {
750 	npi_status_t rs = NPI_SUCCESS;
751 	tcam_key_cfg_t cfg;
752 	npi_handle_t handle;
753 	p_nxge_class_pt_cfg_t p_class_cfgp;
754 
755 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_cfg_tcam_ip_class"));
756 
757 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
758 	p_class_cfgp->class_cfg[class] = class_config;
759 
760 	bzero(&cfg, sizeof (tcam_key_cfg_t));
761 	handle = nxgep->npi_reg_handle;
762 	cfg.discard = 0;
763 	cfg.lookup_enable = 0;
764 	cfg.use_ip_daddr = 0;
765 	if (class_config & NXGE_CLASS_DISCARD)
766 		cfg.discard = 1;
767 	if (class_config & NXGE_CLASS_TCAM_LOOKUP)
768 		cfg.lookup_enable = 1;
769 	if (class_config & NXGE_CLASS_TCAM_USE_SRC_ADDR)
770 		cfg.use_ip_daddr = 1;
771 
772 	rs = npi_fflp_cfg_ip_cls_tcam_key(handle, class, &cfg);
773 	if (rs & NPI_FFLP_ERROR) {
774 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, " nxge_cfg_tcam_ip_class"
775 		    " opt %x for class %d failed ", class_config, class));
776 		return (NXGE_ERROR | rs);
777 	}
778 	return (NXGE_OK);
779 }
780 
781 nxge_status_t
782 nxge_fflp_set_hash1(p_nxge_t nxgep, uint32_t h1)
783 {
784 	npi_status_t rs = NPI_SUCCESS;
785 	npi_handle_t handle;
786 	p_nxge_class_pt_cfg_t p_class_cfgp;
787 
788 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_fflp_init_h1"));
789 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
790 	p_class_cfgp->init_h1 = h1;
791 	handle = nxgep->npi_reg_handle;
792 	rs = npi_fflp_cfg_hash_h1poly(handle, h1);
793 	if (rs & NPI_FFLP_ERROR) {
794 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
795 		    " nxge_fflp_init_h1 %x failed ", h1));
796 		return (NXGE_ERROR | rs);
797 	}
798 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " <== nxge_fflp_init_h1"));
799 	return (NXGE_OK);
800 }
801 
802 nxge_status_t
803 nxge_fflp_set_hash2(p_nxge_t nxgep, uint16_t h2)
804 {
805 	npi_status_t rs = NPI_SUCCESS;
806 	npi_handle_t handle;
807 	p_nxge_class_pt_cfg_t p_class_cfgp;
808 
809 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_fflp_init_h2"));
810 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
811 	p_class_cfgp->init_h2 = h2;
812 
813 	handle = nxgep->npi_reg_handle;
814 	rs = npi_fflp_cfg_hash_h2poly(handle, h2);
815 	if (rs & NPI_FFLP_ERROR) {
816 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
817 		    " nxge_fflp_init_h2 %x failed ", h2));
818 		return (NXGE_ERROR | rs);
819 	}
820 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " <== nxge_fflp_init_h2"));
821 	return (NXGE_OK);
822 }
823 
824 nxge_status_t
825 nxge_classify_init_sw(p_nxge_t nxgep)
826 {
827 	nxge_classify_t *classify_ptr;
828 
829 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_classify_init_sw"));
830 	classify_ptr = &nxgep->classifier;
831 
832 	if (classify_ptr->state & NXGE_FFLP_SW_INIT) {
833 		NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
834 		    "nxge_classify_init_sw already init"));
835 		return (NXGE_OK);
836 	}
837 
838 	classify_ptr->tcam_size = nxgep->nxge_hw_p->tcam_size / nxgep->nports;
839 	classify_ptr->tcam_entries = (tcam_flow_spec_t *)nxgep->nxge_hw_p->tcam;
840 	classify_ptr->tcam_top = nxgep->function_num;
841 
842 	/* Init defaults */
843 	/*
844 	 * add hacks required for HW shortcomings for example, code to handle
845 	 * fragmented packets
846 	 */
847 	nxge_init_h1_table();
848 	nxge_crc_ccitt_init();
849 	nxgep->classifier.tcam_location = nxgep->function_num;
850 	nxgep->classifier.fragment_bug = 1;
851 	classify_ptr->state |= NXGE_FFLP_SW_INIT;
852 
853 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_classify_init_sw"));
854 	return (NXGE_OK);
855 }
856 
857 nxge_status_t
858 nxge_classify_exit_sw(p_nxge_t nxgep)
859 {
860 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_classify_exit_sw"));
861 	nxgep->classifier.state = 0;
862 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_classify_exit_sw"));
863 	return (NXGE_OK);
864 }
865 
866 /*
867  * Figures out the RDC Group for the entry
868  *
869  * The current implementation is just a place holder and it
870  * returns 0.
871  * The real location determining algorithm would consider
872  * the partition etc ... before deciding w
873  *
874  */
875 
876 /* ARGSUSED */
877 static uint8_t
878 nxge_get_rdc_group(p_nxge_t nxgep, uint8_t class, uint64_t cookie)
879 {
880 	int use_port_rdc_grp = 0;
881 	uint8_t rdc_grp = 0;
882 	p_nxge_dma_pt_cfg_t p_dma_cfgp;
883 	p_nxge_hw_pt_cfg_t p_cfgp;
884 	p_nxge_rdc_grp_t rdc_grp_p;
885 
886 	p_dma_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
887 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
888 	rdc_grp_p = &p_dma_cfgp->rdc_grps[use_port_rdc_grp];
889 	rdc_grp = p_cfgp->def_mac_rxdma_grpid;
890 
891 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
892 	    "nxge_get_rdc_group: grp 0x%x real_grp %x grpp $%p\n",
893 	    cookie, rdc_grp, rdc_grp_p));
894 	return (rdc_grp);
895 }
896 
897 /* ARGSUSED */
898 static uint8_t
899 nxge_get_rdc_offset(p_nxge_t nxgep, uint8_t class, uint64_t cookie)
900 {
901 	return ((uint8_t)cookie);
902 }
903 
904 /* ARGSUSED */
905 static void
906 nxge_fill_tcam_entry_udp(p_nxge_t nxgep, flow_spec_t *flow_spec,
907 	tcam_entry_t *tcam_ptr)
908 {
909 #define	fspec_key (flow_spec->uh.udpip4spec)
910 #define	fspec_mask (flow_spec->um.udpip4spec)
911 
912 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_key, fspec_key.ip4dst);
913 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_mask, fspec_mask.ip4dst);
914 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_key, fspec_key.ip4src);
915 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_mask, fspec_mask.ip4src);
916 	TCAM_IP_PORTS(tcam_ptr->ip4_port_key,
917 	    fspec_key.pdst, fspec_key.psrc);
918 	TCAM_IP_PORTS(tcam_ptr->ip4_port_mask,
919 	    fspec_mask.pdst, fspec_mask.psrc);
920 	TCAM_IP_CLASS(tcam_ptr->ip4_class_key,
921 	    tcam_ptr->ip4_class_mask,
922 	    TCAM_CLASS_UDP_IPV4);
923 	TCAM_IP_PROTO(tcam_ptr->ip4_proto_key,
924 	    tcam_ptr->ip4_proto_mask,
925 	    IPPROTO_UDP);
926 	tcam_ptr->ip4_tos_key = fspec_key.tos;
927 	tcam_ptr->ip4_tos_mask = fspec_mask.tos;
928 #undef fspec_key
929 #undef fspec_mask
930 }
931 
932 static void
933 nxge_fill_tcam_entry_udp_ipv6(p_nxge_t nxgep, flow_spec_t *flow_spec,
934 	tcam_entry_t *tcam_ptr)
935 {
936 	p_nxge_class_pt_cfg_t p_class_cfgp;
937 #define	fspec_key (flow_spec->uh.udpip6spec)
938 #define	fspec_mask (flow_spec->um.udpip6spec)
939 
940 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
941 	if (p_class_cfgp->class_cfg[TCAM_CLASS_UDP_IPV6] &
942 	    NXGE_CLASS_TCAM_USE_SRC_ADDR) {
943 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6src);
944 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6src);
945 	} else {
946 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6dst);
947 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6dst);
948 	}
949 
950 	TCAM_IP_CLASS(tcam_ptr->ip6_class_key,
951 	    tcam_ptr->ip6_class_mask, TCAM_CLASS_UDP_IPV6);
952 	TCAM_IP_PROTO(tcam_ptr->ip6_nxt_hdr_key,
953 	    tcam_ptr->ip6_nxt_hdr_mask, IPPROTO_UDP);
954 	TCAM_IP_PORTS(tcam_ptr->ip6_port_key,
955 	    fspec_key.pdst, fspec_key.psrc);
956 	TCAM_IP_PORTS(tcam_ptr->ip6_port_mask,
957 	    fspec_mask.pdst, fspec_mask.psrc);
958 	tcam_ptr->ip6_tos_key = fspec_key.tos;
959 	tcam_ptr->ip6_tos_mask = fspec_mask.tos;
960 #undef fspec_key
961 #undef fspec_mask
962 }
963 
964 /* ARGSUSED */
965 static void
966 nxge_fill_tcam_entry_tcp(p_nxge_t nxgep, flow_spec_t *flow_spec,
967 	tcam_entry_t *tcam_ptr)
968 {
969 #define	fspec_key (flow_spec->uh.tcpip4spec)
970 #define	fspec_mask (flow_spec->um.tcpip4spec)
971 
972 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_key, fspec_key.ip4dst);
973 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_mask, fspec_mask.ip4dst);
974 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_key, fspec_key.ip4src);
975 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_mask, fspec_mask.ip4src);
976 	TCAM_IP_PORTS(tcam_ptr->ip4_port_key,
977 	    fspec_key.pdst, fspec_key.psrc);
978 	TCAM_IP_PORTS(tcam_ptr->ip4_port_mask,
979 	    fspec_mask.pdst, fspec_mask.psrc);
980 	TCAM_IP_CLASS(tcam_ptr->ip4_class_key,
981 	    tcam_ptr->ip4_class_mask, TCAM_CLASS_TCP_IPV4);
982 	TCAM_IP_PROTO(tcam_ptr->ip4_proto_key,
983 	    tcam_ptr->ip4_proto_mask, IPPROTO_TCP);
984 	tcam_ptr->ip4_tos_key = fspec_key.tos;
985 	tcam_ptr->ip4_tos_mask = fspec_mask.tos;
986 #undef fspec_key
987 #undef fspec_mask
988 }
989 
990 /* ARGSUSED */
991 static void
992 nxge_fill_tcam_entry_sctp(p_nxge_t nxgep, flow_spec_t *flow_spec,
993 	tcam_entry_t *tcam_ptr)
994 {
995 #define	fspec_key (flow_spec->uh.tcpip4spec)
996 #define	fspec_mask (flow_spec->um.tcpip4spec)
997 
998 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_key, fspec_key.ip4dst);
999 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_mask, fspec_mask.ip4dst);
1000 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_key, fspec_key.ip4src);
1001 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_mask, fspec_mask.ip4src);
1002 	TCAM_IP_CLASS(tcam_ptr->ip4_class_key,
1003 	    tcam_ptr->ip4_class_mask, TCAM_CLASS_SCTP_IPV4);
1004 	TCAM_IP_PROTO(tcam_ptr->ip4_proto_key,
1005 	    tcam_ptr->ip4_proto_mask, IPPROTO_SCTP);
1006 	TCAM_IP_PORTS(tcam_ptr->ip4_port_key,
1007 	    fspec_key.pdst, fspec_key.psrc);
1008 	TCAM_IP_PORTS(tcam_ptr->ip4_port_mask,
1009 	    fspec_mask.pdst, fspec_mask.psrc);
1010 	tcam_ptr->ip4_tos_key = fspec_key.tos;
1011 	tcam_ptr->ip4_tos_mask = fspec_mask.tos;
1012 #undef fspec_key
1013 #undef fspec_mask
1014 }
1015 
1016 static void
1017 nxge_fill_tcam_entry_tcp_ipv6(p_nxge_t nxgep, flow_spec_t *flow_spec,
1018 	tcam_entry_t *tcam_ptr)
1019 {
1020 	p_nxge_class_pt_cfg_t p_class_cfgp;
1021 #define	fspec_key (flow_spec->uh.tcpip6spec)
1022 #define	fspec_mask (flow_spec->um.tcpip6spec)
1023 
1024 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
1025 	if (p_class_cfgp->class_cfg[TCAM_CLASS_UDP_IPV6] &
1026 	    NXGE_CLASS_TCAM_USE_SRC_ADDR) {
1027 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6src);
1028 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6src);
1029 	} else {
1030 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6dst);
1031 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6dst);
1032 	}
1033 
1034 	TCAM_IP_CLASS(tcam_ptr->ip6_class_key,
1035 	    tcam_ptr->ip6_class_mask, TCAM_CLASS_TCP_IPV6);
1036 	TCAM_IP_PROTO(tcam_ptr->ip6_nxt_hdr_key,
1037 	    tcam_ptr->ip6_nxt_hdr_mask, IPPROTO_TCP);
1038 	TCAM_IP_PORTS(tcam_ptr->ip6_port_key,
1039 	    fspec_key.pdst, fspec_key.psrc);
1040 	TCAM_IP_PORTS(tcam_ptr->ip6_port_mask,
1041 	    fspec_mask.pdst, fspec_mask.psrc);
1042 	tcam_ptr->ip6_tos_key = fspec_key.tos;
1043 	tcam_ptr->ip6_tos_mask = fspec_mask.tos;
1044 #undef fspec_key
1045 #undef fspec_mask
1046 }
1047 
1048 static void
1049 nxge_fill_tcam_entry_sctp_ipv6(p_nxge_t nxgep, flow_spec_t *flow_spec,
1050 	tcam_entry_t *tcam_ptr)
1051 {
1052 	p_nxge_class_pt_cfg_t p_class_cfgp;
1053 #define	fspec_key (flow_spec->uh.tcpip6spec)
1054 #define	fspec_mask (flow_spec->um.tcpip6spec)
1055 
1056 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
1057 
1058 	if (p_class_cfgp->class_cfg[TCAM_CLASS_UDP_IPV6] &
1059 	    NXGE_CLASS_TCAM_USE_SRC_ADDR) {
1060 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6src);
1061 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6src);
1062 	} else {
1063 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6dst);
1064 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6dst);
1065 	}
1066 
1067 	TCAM_IP_CLASS(tcam_ptr->ip6_class_key,
1068 	    tcam_ptr->ip6_class_mask, TCAM_CLASS_SCTP_IPV6);
1069 	TCAM_IP_PROTO(tcam_ptr->ip6_nxt_hdr_key,
1070 	    tcam_ptr->ip6_nxt_hdr_mask, IPPROTO_SCTP);
1071 	TCAM_IP_PORTS(tcam_ptr->ip6_port_key,
1072 	    fspec_key.pdst, fspec_key.psrc);
1073 	TCAM_IP_PORTS(tcam_ptr->ip6_port_mask,
1074 	    fspec_mask.pdst, fspec_mask.psrc);
1075 	tcam_ptr->ip6_tos_key = fspec_key.tos;
1076 	tcam_ptr->ip6_tos_mask = fspec_mask.tos;
1077 #undef fspec_key
1078 #undef fspec_mask
1079 }
1080 
1081 /* ARGSUSED */
1082 static void
1083 nxge_fill_tcam_entry_ah_esp(p_nxge_t nxgep, flow_spec_t *flow_spec,
1084 	tcam_entry_t *tcam_ptr)
1085 {
1086 #define	fspec_key (flow_spec->uh.ahip4spec)
1087 #define	fspec_mask (flow_spec->um.ahip4spec)
1088 
1089 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_key, fspec_key.ip4dst);
1090 	TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_mask, fspec_mask.ip4dst);
1091 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_key, fspec_key.ip4src);
1092 	TCAM_IPV4_ADDR(tcam_ptr->ip4_src_mask, fspec_mask.ip4src);
1093 
1094 	tcam_ptr->ip4_port_key = fspec_key.spi;
1095 	tcam_ptr->ip4_port_mask = fspec_mask.spi;
1096 
1097 	TCAM_IP_CLASS(tcam_ptr->ip4_class_key,
1098 	    tcam_ptr->ip4_class_mask,
1099 	    TCAM_CLASS_AH_ESP_IPV4);
1100 
1101 	if (flow_spec->flow_type == FSPEC_AHIP4) {
1102 		TCAM_IP_PROTO(tcam_ptr->ip4_proto_key,
1103 		    tcam_ptr->ip4_proto_mask, IPPROTO_AH);
1104 	} else {
1105 		TCAM_IP_PROTO(tcam_ptr->ip4_proto_key,
1106 		    tcam_ptr->ip4_proto_mask, IPPROTO_ESP);
1107 	}
1108 	tcam_ptr->ip4_tos_key = fspec_key.tos;
1109 	tcam_ptr->ip4_tos_mask = fspec_mask.tos;
1110 #undef fspec_key
1111 #undef fspec_mask
1112 }
1113 
1114 static void
1115 nxge_fill_tcam_entry_ah_esp_ipv6(p_nxge_t nxgep, flow_spec_t *flow_spec,
1116 	tcam_entry_t *tcam_ptr)
1117 {
1118 	p_nxge_class_pt_cfg_t p_class_cfgp;
1119 #define	fspec_key (flow_spec->uh.ahip6spec)
1120 #define	fspec_mask (flow_spec->um.ahip6spec)
1121 
1122 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
1123 	if (p_class_cfgp->class_cfg[TCAM_CLASS_AH_ESP_IPV6] &
1124 	    NXGE_CLASS_TCAM_USE_SRC_ADDR) {
1125 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6src);
1126 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6src);
1127 	} else {
1128 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_key, fspec_key.ip6dst);
1129 		TCAM_IPV6_ADDR(tcam_ptr->ip6_ip_addr_mask, fspec_mask.ip6dst);
1130 	}
1131 
1132 	TCAM_IP_CLASS(tcam_ptr->ip6_class_key,
1133 	    tcam_ptr->ip6_class_mask, TCAM_CLASS_AH_ESP_IPV6);
1134 
1135 	if (flow_spec->flow_type == FSPEC_AHIP6) {
1136 		TCAM_IP_PROTO(tcam_ptr->ip6_nxt_hdr_key,
1137 		    tcam_ptr->ip6_nxt_hdr_mask, IPPROTO_AH);
1138 	} else {
1139 		TCAM_IP_PROTO(tcam_ptr->ip6_nxt_hdr_key,
1140 		    tcam_ptr->ip6_nxt_hdr_mask, IPPROTO_ESP);
1141 	}
1142 	tcam_ptr->ip6_port_key = fspec_key.spi;
1143 	tcam_ptr->ip6_port_mask = fspec_mask.spi;
1144 	tcam_ptr->ip6_tos_key = fspec_key.tos;
1145 	tcam_ptr->ip6_tos_mask = fspec_mask.tos;
1146 #undef fspec_key
1147 #undef fspec_mask
1148 }
1149 
1150 /* ARGSUSED */
1151 static void
1152 nxge_fill_tcam_entry_ip_usr(p_nxge_t nxgep, flow_spec_t *flow_spec,
1153 	tcam_entry_t *tcam_ptr, tcam_class_t class)
1154 {
1155 #define	fspec_key (flow_spec->uh.ip_usr_spec)
1156 #define	fspec_mask (flow_spec->um.ip_usr_spec)
1157 
1158 	if (fspec_key.ip_ver == FSPEC_IP4) {
1159 		TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_key, fspec_key.ip4dst);
1160 		TCAM_IPV4_ADDR(tcam_ptr->ip4_dest_mask, fspec_mask.ip4dst);
1161 		TCAM_IPV4_ADDR(tcam_ptr->ip4_src_key, fspec_key.ip4src);
1162 		TCAM_IPV4_ADDR(tcam_ptr->ip4_src_mask, fspec_mask.ip4src);
1163 
1164 		tcam_ptr->ip4_port_key = fspec_key.l4_4_bytes;
1165 		tcam_ptr->ip4_port_mask = fspec_mask.l4_4_bytes;
1166 
1167 		TCAM_IP_CLASS(tcam_ptr->ip4_class_key,
1168 		    tcam_ptr->ip4_class_mask, class);
1169 
1170 		tcam_ptr->ip4_proto_key = fspec_key.proto;
1171 		tcam_ptr->ip4_proto_mask = fspec_mask.proto;
1172 
1173 		tcam_ptr->ip4_tos_key = fspec_key.tos;
1174 		tcam_ptr->ip4_tos_mask = fspec_mask.tos;
1175 	}
1176 #undef fspec_key
1177 #undef fspec_mask
1178 }
1179 
1180 
1181 nxge_status_t
1182 nxge_flow_get_hash(p_nxge_t nxgep, flow_resource_t *flow_res,
1183 	uint32_t *H1, uint16_t *H2)
1184 {
1185 	flow_spec_t *flow_spec;
1186 	uint32_t class_cfg;
1187 	flow_template_t ft;
1188 	p_nxge_class_pt_cfg_t p_class_cfgp;
1189 
1190 	int ft_size = sizeof (flow_template_t);
1191 
1192 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_flow_get_hash"));
1193 
1194 	flow_spec = (flow_spec_t *)&flow_res->flow_spec;
1195 	bzero((char *)&ft, ft_size);
1196 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
1197 
1198 	switch (flow_spec->flow_type) {
1199 	case FSPEC_TCPIP4:
1200 		class_cfg = p_class_cfgp->class_cfg[TCAM_CLASS_TCP_IPV4];
1201 		if (class_cfg & NXGE_CLASS_FLOW_USE_PROTO)
1202 			ft.ip_proto = IPPROTO_TCP;
1203 		if (class_cfg & NXGE_CLASS_FLOW_USE_IPSRC)
1204 			ft.ip4_saddr = flow_res->flow_spec.uh.tcpip4spec.ip4src;
1205 		if (class_cfg & NXGE_CLASS_FLOW_USE_IPDST)
1206 			ft.ip4_daddr = flow_res->flow_spec.uh.tcpip4spec.ip4dst;
1207 		if (class_cfg & NXGE_CLASS_FLOW_USE_SRC_PORT)
1208 			ft.ip_src_port = flow_res->flow_spec.uh.tcpip4spec.psrc;
1209 		if (class_cfg & NXGE_CLASS_FLOW_USE_DST_PORT)
1210 			ft.ip_dst_port = flow_res->flow_spec.uh.tcpip4spec.pdst;
1211 		break;
1212 
1213 	case FSPEC_UDPIP4:
1214 		class_cfg = p_class_cfgp->class_cfg[TCAM_CLASS_UDP_IPV4];
1215 		if (class_cfg & NXGE_CLASS_FLOW_USE_PROTO)
1216 			ft.ip_proto = IPPROTO_UDP;
1217 		if (class_cfg & NXGE_CLASS_FLOW_USE_IPSRC)
1218 			ft.ip4_saddr = flow_res->flow_spec.uh.udpip4spec.ip4src;
1219 		if (class_cfg & NXGE_CLASS_FLOW_USE_IPDST)
1220 			ft.ip4_daddr = flow_res->flow_spec.uh.udpip4spec.ip4dst;
1221 		if (class_cfg & NXGE_CLASS_FLOW_USE_SRC_PORT)
1222 			ft.ip_src_port = flow_res->flow_spec.uh.udpip4spec.psrc;
1223 		if (class_cfg & NXGE_CLASS_FLOW_USE_DST_PORT)
1224 			ft.ip_dst_port = flow_res->flow_spec.uh.udpip4spec.pdst;
1225 		break;
1226 
1227 	default:
1228 		return (NXGE_ERROR);
1229 	}
1230 
1231 	*H1 = nxge_compute_h1(p_class_cfgp->init_h1,
1232 	    (uint32_t *)&ft, ft_size) & 0xfffff;
1233 	*H2 = nxge_compute_h2(p_class_cfgp->init_h2,
1234 	    (uint8_t *)&ft, ft_size);
1235 
1236 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_flow_get_hash"));
1237 	return (NXGE_OK);
1238 }
1239 
1240 nxge_status_t
1241 nxge_add_fcram_entry(p_nxge_t nxgep, flow_resource_t *flow_res)
1242 {
1243 	uint32_t H1;
1244 	uint16_t H2;
1245 	nxge_status_t status = NXGE_OK;
1246 
1247 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_add_fcram_entry"));
1248 	status = nxge_flow_get_hash(nxgep, flow_res, &H1, &H2);
1249 	if (status != NXGE_OK) {
1250 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1251 		    " nxge_add_fcram_entry failed "));
1252 		return (status);
1253 	}
1254 
1255 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_add_fcram_entry"));
1256 	return (NXGE_OK);
1257 }
1258 
1259 /*
1260  * Already decided this flow goes into the tcam
1261  */
1262 
1263 nxge_status_t
1264 nxge_add_tcam_entry(p_nxge_t nxgep, flow_resource_t *flow_res)
1265 {
1266 	npi_handle_t handle;
1267 	uint64_t channel_cookie;
1268 	uint64_t flow_cookie;
1269 	flow_spec_t *flow_spec;
1270 	npi_status_t rs = NPI_SUCCESS;
1271 	tcam_entry_t tcam_ptr;
1272 	tcam_location_t location;
1273 	uint8_t offset, rdc_grp;
1274 	p_nxge_hw_list_t hw_p;
1275 	uint64_t class;
1276 
1277 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_add_tcam_entry"));
1278 	handle = nxgep->npi_reg_handle;
1279 
1280 	bzero((void *)&tcam_ptr, sizeof (tcam_entry_t));
1281 	flow_spec = (flow_spec_t *)&flow_res->flow_spec;
1282 	flow_cookie = flow_res->flow_cookie;
1283 	channel_cookie = flow_res->channel_cookie;
1284 	location = (tcam_location_t)nxge_tcam_get_index(nxgep,
1285 	    (uint16_t)flow_res->location);
1286 
1287 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
1288 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1289 		    " nxge_add_tcam_entry: common hardware not set",
1290 		    nxgep->niu_type));
1291 		return (NXGE_ERROR);
1292 	}
1293 
1294 	class = TCAM_CLASS_INVALID;
1295 	if (flow_spec->flow_type == FSPEC_IP_USR) {
1296 		int i;
1297 		int add_usr_cls = 0;
1298 		int ipv6 = 0;
1299 		nxge_usr_l3_cls_t *l3_ucls_p;
1300 #define	uspec (flow_spec->uh.ip_usr_spec)
1301 #define	umask (flow_spec->um.ip_usr_spec)
1302 
1303 		MUTEX_ENTER(&hw_p->nxge_tcam_lock);
1304 
1305 		for (i = 0; i < NXGE_L3_PROG_CLS; i++) {
1306 			l3_ucls_p = &hw_p->tcam_l3_prog_cls[i];
1307 			if (l3_ucls_p->valid && l3_ucls_p->tcam_ref_cnt) {
1308 				if (uspec.proto == l3_ucls_p->pid) {
1309 					class = l3_ucls_p->cls;
1310 					l3_ucls_p->tcam_ref_cnt++;
1311 					add_usr_cls = 1;
1312 					break;
1313 				}
1314 			} else if (l3_ucls_p->valid == 0) {
1315 				/* Program new user IP class */
1316 				switch (i) {
1317 				case 0:
1318 					class = TCAM_CLASS_IP_USER_4;
1319 					break;
1320 				case 1:
1321 					class = TCAM_CLASS_IP_USER_5;
1322 					break;
1323 				case 2:
1324 					class = TCAM_CLASS_IP_USER_6;
1325 					break;
1326 				case 3:
1327 					class = TCAM_CLASS_IP_USER_7;
1328 					break;
1329 				default:
1330 					break;
1331 				}
1332 				if (uspec.ip_ver == FSPEC_IP6)
1333 					ipv6 = 1;
1334 				rs = npi_fflp_cfg_ip_usr_cls_set(handle,
1335 				    (tcam_class_t)class, uspec.tos,
1336 				    umask.tos, uspec.proto, ipv6);
1337 				if (rs != NPI_SUCCESS)
1338 					goto fail;
1339 
1340 				rs = npi_fflp_cfg_ip_usr_cls_enable(handle,
1341 				    (tcam_class_t)class);
1342 				if (rs != NPI_SUCCESS)
1343 					goto fail;
1344 
1345 				l3_ucls_p->cls = class;
1346 				l3_ucls_p->pid = uspec.proto;
1347 				l3_ucls_p->tcam_ref_cnt++;
1348 				l3_ucls_p->valid = 1;
1349 				add_usr_cls = 1;
1350 				break;
1351 			} else if (l3_ucls_p->tcam_ref_cnt == 0 &&
1352 			    uspec.proto == l3_ucls_p->pid) {
1353 				/*
1354 				 * The class has already been programmed,
1355 				 * probably for flow hash
1356 				 */
1357 				class = l3_ucls_p->cls;
1358 				if (uspec.ip_ver == FSPEC_IP6)
1359 					ipv6 = 1;
1360 				rs = npi_fflp_cfg_ip_usr_cls_set(handle,
1361 				    (tcam_class_t)class, uspec.tos,
1362 				    umask.tos, uspec.proto, ipv6);
1363 				if (rs != NPI_SUCCESS)
1364 					goto fail;
1365 
1366 				rs = npi_fflp_cfg_ip_usr_cls_enable(handle,
1367 				    (tcam_class_t)class);
1368 				if (rs != NPI_SUCCESS)
1369 					goto fail;
1370 
1371 				l3_ucls_p->pid = uspec.proto;
1372 				l3_ucls_p->tcam_ref_cnt++;
1373 				add_usr_cls = 1;
1374 				break;
1375 			}
1376 		}
1377 		if (!add_usr_cls) {
1378 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1379 			    "nxge_add_tcam_entry: Could not find/insert class"
1380 			    "for pid %d", uspec.proto));
1381 			goto fail;
1382 		}
1383 		MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1384 #undef uspec
1385 #undef umask
1386 	}
1387 
1388 	switch (flow_spec->flow_type) {
1389 	case FSPEC_TCPIP4:
1390 		nxge_fill_tcam_entry_tcp(nxgep, flow_spec, &tcam_ptr);
1391 		rdc_grp = nxge_get_rdc_group(nxgep, TCAM_CLASS_TCP_IPV4,
1392 		    flow_cookie);
1393 		offset = nxge_get_rdc_offset(nxgep, TCAM_CLASS_TCP_IPV4,
1394 		    channel_cookie);
1395 		break;
1396 
1397 	case FSPEC_UDPIP4:
1398 		nxge_fill_tcam_entry_udp(nxgep, flow_spec, &tcam_ptr);
1399 		rdc_grp = nxge_get_rdc_group(nxgep,
1400 		    TCAM_CLASS_UDP_IPV4,
1401 		    flow_cookie);
1402 		offset = nxge_get_rdc_offset(nxgep,
1403 		    TCAM_CLASS_UDP_IPV4,
1404 		    channel_cookie);
1405 		break;
1406 
1407 	case FSPEC_TCPIP6:
1408 		nxge_fill_tcam_entry_tcp_ipv6(nxgep,
1409 		    flow_spec, &tcam_ptr);
1410 		rdc_grp = nxge_get_rdc_group(nxgep, TCAM_CLASS_TCP_IPV6,
1411 		    flow_cookie);
1412 		offset = nxge_get_rdc_offset(nxgep, TCAM_CLASS_TCP_IPV6,
1413 		    channel_cookie);
1414 		break;
1415 
1416 	case FSPEC_UDPIP6:
1417 		nxge_fill_tcam_entry_udp_ipv6(nxgep,
1418 		    flow_spec, &tcam_ptr);
1419 		rdc_grp = nxge_get_rdc_group(nxgep,
1420 		    TCAM_CLASS_UDP_IPV6,
1421 		    flow_cookie);
1422 		offset = nxge_get_rdc_offset(nxgep,
1423 		    TCAM_CLASS_UDP_IPV6,
1424 		    channel_cookie);
1425 		break;
1426 
1427 	case FSPEC_SCTPIP4:
1428 		nxge_fill_tcam_entry_sctp(nxgep, flow_spec, &tcam_ptr);
1429 		rdc_grp = nxge_get_rdc_group(nxgep,
1430 		    TCAM_CLASS_SCTP_IPV4,
1431 		    flow_cookie);
1432 		offset = nxge_get_rdc_offset(nxgep,
1433 		    TCAM_CLASS_SCTP_IPV4,
1434 		    channel_cookie);
1435 		break;
1436 
1437 	case FSPEC_SCTPIP6:
1438 		nxge_fill_tcam_entry_sctp_ipv6(nxgep,
1439 		    flow_spec, &tcam_ptr);
1440 		rdc_grp = nxge_get_rdc_group(nxgep,
1441 		    TCAM_CLASS_SCTP_IPV6,
1442 		    flow_cookie);
1443 		offset = nxge_get_rdc_offset(nxgep,
1444 		    TCAM_CLASS_SCTP_IPV6,
1445 		    channel_cookie);
1446 		break;
1447 
1448 	case FSPEC_AHIP4:
1449 	case FSPEC_ESPIP4:
1450 		nxge_fill_tcam_entry_ah_esp(nxgep, flow_spec, &tcam_ptr);
1451 		rdc_grp = nxge_get_rdc_group(nxgep,
1452 		    TCAM_CLASS_AH_ESP_IPV4,
1453 		    flow_cookie);
1454 		offset = nxge_get_rdc_offset(nxgep,
1455 		    TCAM_CLASS_AH_ESP_IPV4,
1456 		    channel_cookie);
1457 		break;
1458 
1459 	case FSPEC_AHIP6:
1460 	case FSPEC_ESPIP6:
1461 		nxge_fill_tcam_entry_ah_esp_ipv6(nxgep,
1462 		    flow_spec, &tcam_ptr);
1463 		rdc_grp = nxge_get_rdc_group(nxgep,
1464 		    TCAM_CLASS_AH_ESP_IPV6,
1465 		    flow_cookie);
1466 		offset = nxge_get_rdc_offset(nxgep,
1467 		    TCAM_CLASS_AH_ESP_IPV6,
1468 		    channel_cookie);
1469 		break;
1470 
1471 	case FSPEC_IP_USR:
1472 		nxge_fill_tcam_entry_ip_usr(nxgep, flow_spec, &tcam_ptr,
1473 		    (tcam_class_t)class);
1474 		rdc_grp = nxge_get_rdc_group(nxgep,
1475 		    (tcam_class_t)class, flow_cookie);
1476 		offset = nxge_get_rdc_offset(nxgep,
1477 		    (tcam_class_t)class, channel_cookie);
1478 		break;
1479 	default:
1480 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1481 		    "nxge_add_tcam_entry: Unknown flow spec 0x%x",
1482 		    flow_spec->flow_type));
1483 		return (NXGE_ERROR);
1484 	}
1485 
1486 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1487 	    " nxge_add_tcam_entry write"
1488 	    " for location %d offset %d", location, offset));
1489 
1490 	MUTEX_ENTER(&hw_p->nxge_tcam_lock);
1491 	rs = npi_fflp_tcam_entry_write(handle, location, &tcam_ptr);
1492 
1493 	if (rs & NPI_FFLP_ERROR) {
1494 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1495 		    " nxge_add_tcam_entry write"
1496 		    " failed for location %d", location));
1497 		goto fail;
1498 	}
1499 
1500 	tcam_ptr.match_action.value = 0;
1501 	tcam_ptr.match_action.bits.ldw.rdctbl = rdc_grp;
1502 	tcam_ptr.match_action.bits.ldw.offset = offset;
1503 	tcam_ptr.match_action.bits.ldw.tres =
1504 	    TRES_TERM_OVRD_L2RDC;
1505 	if (channel_cookie == NXGE_PKT_DISCARD)
1506 		tcam_ptr.match_action.bits.ldw.disc = 1;
1507 	rs = npi_fflp_tcam_asc_ram_entry_write(handle,
1508 	    location, tcam_ptr.match_action.value);
1509 	if (rs & NPI_FFLP_ERROR) {
1510 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1511 		    " nxge_add_tcam_entry write"
1512 		    " failed for ASC RAM location %d", location));
1513 		goto fail;
1514 	}
1515 	bcopy((void *) &tcam_ptr,
1516 	    (void *) &nxgep->classifier.tcam_entries[location].tce,
1517 	    sizeof (tcam_entry_t));
1518 	nxgep->classifier.tcam_entry_cnt++;
1519 	nxgep->classifier.tcam_entries[location].valid = 1;
1520 
1521 	MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1522 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_add_tcam_entry"));
1523 	return (NXGE_OK);
1524 fail:
1525 	MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1526 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_add_tcam_entry FAILED"));
1527 	return (NXGE_ERROR);
1528 }
1529 
1530 static nxge_status_t
1531 nxge_tcam_handle_ip_fragment(p_nxge_t nxgep)
1532 {
1533 	tcam_entry_t tcam_ptr;
1534 	tcam_location_t location;
1535 	uint8_t class;
1536 	uint32_t class_config;
1537 	npi_handle_t handle;
1538 	npi_status_t rs = NPI_SUCCESS;
1539 	p_nxge_hw_list_t hw_p;
1540 	nxge_status_t status = NXGE_OK;
1541 
1542 	handle = nxgep->npi_reg_handle;
1543 	class = 0;
1544 	bzero((void *)&tcam_ptr, sizeof (tcam_entry_t));
1545 	tcam_ptr.ip4_noport_key = 1;
1546 	tcam_ptr.ip4_noport_mask = 1;
1547 	location = nxgep->function_num;
1548 	nxgep->classifier.fragment_bug_location = location;
1549 
1550 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
1551 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1552 		    " nxge_tcam_handle_ip_fragment: common hardware not set",
1553 		    nxgep->niu_type));
1554 		return (NXGE_ERROR);
1555 	}
1556 	MUTEX_ENTER(&hw_p->nxge_tcam_lock);
1557 	rs = npi_fflp_tcam_entry_write(handle,
1558 	    location, &tcam_ptr);
1559 
1560 	if (rs & NPI_FFLP_ERROR) {
1561 		MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1562 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1563 		    " nxge_tcam_handle_ip_fragment "
1564 		    " tcam_entry write"
1565 		    " failed for location %d", location));
1566 		return (NXGE_ERROR);
1567 	}
1568 	tcam_ptr.match_action.bits.ldw.rdctbl = nxgep->class_config.mac_rdcgrp;
1569 	tcam_ptr.match_action.bits.ldw.offset = 0;	/* use the default */
1570 	tcam_ptr.match_action.bits.ldw.tres =
1571 	    TRES_TERM_USE_OFFSET;
1572 	rs = npi_fflp_tcam_asc_ram_entry_write(handle,
1573 	    location, tcam_ptr.match_action.value);
1574 
1575 	if (rs & NPI_FFLP_ERROR) {
1576 		MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1577 		NXGE_DEBUG_MSG((nxgep,
1578 		    FFLP_CTL,
1579 		    " nxge_tcam_handle_ip_fragment "
1580 		    " tcam_entry write"
1581 		    " failed for ASC RAM location %d", location));
1582 		return (NXGE_ERROR);
1583 	}
1584 	bcopy((void *) &tcam_ptr,
1585 	    (void *) &nxgep->classifier.tcam_entries[location].tce,
1586 	    sizeof (tcam_entry_t));
1587 	nxgep->classifier.tcam_entry_cnt++;
1588 	nxgep->classifier.tcam_entries[location].valid = 1;
1589 	for (class = TCAM_CLASS_TCP_IPV4;
1590 	    class <= TCAM_CLASS_SCTP_IPV6; class++) {
1591 		class_config = nxgep->class_config.class_cfg[class];
1592 		class_config |= NXGE_CLASS_TCAM_LOOKUP;
1593 		status = nxge_fflp_ip_class_config(nxgep, class, class_config);
1594 
1595 		if (status & NPI_FFLP_ERROR) {
1596 			MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1597 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1598 			    "nxge_tcam_handle_ip_fragment "
1599 			    "nxge_fflp_ip_class_config failed "
1600 			    " class %d config %x ", class, class_config));
1601 			return (NXGE_ERROR);
1602 		}
1603 	}
1604 
1605 	rs = npi_fflp_cfg_tcam_enable(handle);
1606 	if (rs & NPI_FFLP_ERROR) {
1607 		MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1608 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1609 		    "nxge_tcam_handle_ip_fragment "
1610 		    " nxge_fflp_config_tcam_enable failed"));
1611 		return (NXGE_ERROR);
1612 	}
1613 	MUTEX_EXIT(&hw_p->nxge_tcam_lock);
1614 	return (NXGE_OK);
1615 }
1616 
1617 /* ARGSUSED */
1618 static int
1619 nxge_flow_need_hash_lookup(p_nxge_t nxgep, flow_resource_t *flow_res)
1620 {
1621 	return (0);
1622 }
1623 
1624 nxge_status_t
1625 nxge_add_flow(p_nxge_t nxgep, flow_resource_t *flow_res)
1626 {
1627 
1628 	int insert_hash = 0;
1629 	nxge_status_t status = NXGE_OK;
1630 
1631 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
1632 		/* determine whether to do TCAM or Hash flow */
1633 		insert_hash = nxge_flow_need_hash_lookup(nxgep, flow_res);
1634 	}
1635 	if (insert_hash) {
1636 		status = nxge_add_fcram_entry(nxgep, flow_res);
1637 	} else {
1638 		status = nxge_add_tcam_entry(nxgep, flow_res);
1639 	}
1640 	return (status);
1641 }
1642 
1643 void
1644 nxge_put_tcam(p_nxge_t nxgep, p_mblk_t mp)
1645 {
1646 	flow_resource_t *fs;
1647 
1648 	fs = (flow_resource_t *)mp->b_rptr;
1649 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1650 	    "nxge_put_tcam addr fs $%p  type %x offset %x",
1651 	    fs, fs->flow_spec.flow_type, fs->channel_cookie));
1652 	(void) nxge_add_tcam_entry(nxgep, fs);
1653 }
1654 
1655 nxge_status_t
1656 nxge_fflp_config_tcam_enable(p_nxge_t nxgep)
1657 {
1658 	npi_handle_t handle = nxgep->npi_reg_handle;
1659 	npi_status_t rs = NPI_SUCCESS;
1660 
1661 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_fflp_config_tcam_enable"));
1662 	rs = npi_fflp_cfg_tcam_enable(handle);
1663 	if (rs & NPI_FFLP_ERROR) {
1664 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1665 		    " nxge_fflp_config_tcam_enable failed"));
1666 		return (NXGE_ERROR | rs);
1667 	}
1668 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " <== nxge_fflp_config_tcam_enable"));
1669 	return (NXGE_OK);
1670 }
1671 
1672 nxge_status_t
1673 nxge_fflp_config_tcam_disable(p_nxge_t nxgep)
1674 {
1675 	npi_handle_t handle = nxgep->npi_reg_handle;
1676 	npi_status_t rs = NPI_SUCCESS;
1677 
1678 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1679 	    " ==> nxge_fflp_config_tcam_disable"));
1680 	rs = npi_fflp_cfg_tcam_disable(handle);
1681 	if (rs & NPI_FFLP_ERROR) {
1682 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1683 		    " nxge_fflp_config_tcam_disable failed"));
1684 		return (NXGE_ERROR | rs);
1685 	}
1686 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1687 	    " <== nxge_fflp_config_tcam_disable"));
1688 	return (NXGE_OK);
1689 }
1690 
1691 nxge_status_t
1692 nxge_fflp_config_hash_lookup_enable(p_nxge_t nxgep)
1693 {
1694 	npi_handle_t handle = nxgep->npi_reg_handle;
1695 	npi_status_t rs = NPI_SUCCESS;
1696 	p_nxge_dma_pt_cfg_t p_dma_cfgp;
1697 	p_nxge_hw_pt_cfg_t p_cfgp;
1698 	uint8_t partition;
1699 
1700 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1701 	    " ==> nxge_fflp_config_hash_lookup_enable"));
1702 	p_dma_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
1703 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
1704 
1705 	for (partition = 0; partition < NXGE_MAX_RDC_GROUPS; partition++) {
1706 		if (p_cfgp->grpids[partition]) {
1707 			rs = npi_fflp_cfg_fcram_partition_enable(
1708 			    handle, partition);
1709 			if (rs != NPI_SUCCESS) {
1710 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1711 				    " nxge_fflp_config_hash_lookup_enable"
1712 				    "failed FCRAM partition"
1713 				    " enable for partition %d ", partition));
1714 				return (NXGE_ERROR | rs);
1715 			}
1716 		}
1717 	}
1718 
1719 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1720 	    " <== nxge_fflp_config_hash_lookup_enable"));
1721 	return (NXGE_OK);
1722 }
1723 
1724 nxge_status_t
1725 nxge_fflp_config_hash_lookup_disable(p_nxge_t nxgep)
1726 {
1727 	npi_handle_t handle = nxgep->npi_reg_handle;
1728 	npi_status_t rs = NPI_SUCCESS;
1729 	p_nxge_dma_pt_cfg_t p_dma_cfgp;
1730 	p_nxge_hw_pt_cfg_t p_cfgp;
1731 	uint8_t partition;
1732 
1733 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1734 	    " ==> nxge_fflp_config_hash_lookup_disable"));
1735 	p_dma_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
1736 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
1737 
1738 	for (partition = 0; partition < NXGE_MAX_RDC_GROUPS; partition++) {
1739 		if (p_cfgp->grpids[partition]) {
1740 			rs = npi_fflp_cfg_fcram_partition_disable(handle,
1741 			    partition);
1742 			if (rs != NPI_SUCCESS) {
1743 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1744 				    " nxge_fflp_config_hash_lookup_disable"
1745 				    " failed FCRAM partition"
1746 				    " disable for partition %d ", partition));
1747 				return (NXGE_ERROR | rs);
1748 			}
1749 		}
1750 	}
1751 
1752 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1753 	    " <== nxge_fflp_config_hash_lookup_disable"));
1754 	return (NXGE_OK);
1755 }
1756 
1757 nxge_status_t
1758 nxge_fflp_config_llc_snap_enable(p_nxge_t nxgep)
1759 {
1760 	npi_handle_t handle = nxgep->npi_reg_handle;
1761 	npi_status_t rs = NPI_SUCCESS;
1762 
1763 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1764 	    " ==> nxge_fflp_config_llc_snap_enable"));
1765 	rs = npi_fflp_cfg_llcsnap_enable(handle);
1766 	if (rs & NPI_FFLP_ERROR) {
1767 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1768 		    " nxge_fflp_config_llc_snap_enable failed"));
1769 		return (NXGE_ERROR | rs);
1770 	}
1771 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1772 	    " <== nxge_fflp_config_llc_snap_enable"));
1773 	return (NXGE_OK);
1774 }
1775 
1776 nxge_status_t
1777 nxge_fflp_config_llc_snap_disable(p_nxge_t nxgep)
1778 {
1779 	npi_handle_t handle = nxgep->npi_reg_handle;
1780 	npi_status_t rs = NPI_SUCCESS;
1781 
1782 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1783 	    " ==> nxge_fflp_config_llc_snap_disable"));
1784 	rs = npi_fflp_cfg_llcsnap_disable(handle);
1785 	if (rs & NPI_FFLP_ERROR) {
1786 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1787 		    " nxge_fflp_config_llc_snap_disable failed"));
1788 		return (NXGE_ERROR | rs);
1789 	}
1790 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1791 	    " <== nxge_fflp_config_llc_snap_disable"));
1792 	return (NXGE_OK);
1793 }
1794 
1795 nxge_status_t
1796 nxge_fflp_ip_usr_class_config(p_nxge_t nxgep, tcam_class_t class,
1797 	uint32_t config)
1798 {
1799 	npi_status_t rs = NPI_SUCCESS;
1800 	npi_handle_t handle = nxgep->npi_reg_handle;
1801 	uint8_t tos, tos_mask, proto, ver = 0;
1802 	uint8_t class_enable = 0;
1803 
1804 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_ip_usr_class_config"));
1805 
1806 	tos = (config & NXGE_CLASS_CFG_IP_TOS_MASK) >>
1807 	    NXGE_CLASS_CFG_IP_TOS_SHIFT;
1808 	tos_mask = (config & NXGE_CLASS_CFG_IP_TOS_MASK_MASK) >>
1809 	    NXGE_CLASS_CFG_IP_TOS_MASK_SHIFT;
1810 	proto = (config & NXGE_CLASS_CFG_IP_PROTO_MASK) >>
1811 	    NXGE_CLASS_CFG_IP_PROTO_SHIFT;
1812 	if (config & NXGE_CLASS_CFG_IP_IPV6_MASK)
1813 		ver = 1;
1814 	if (config & NXGE_CLASS_CFG_IP_ENABLE_MASK)
1815 		class_enable = 1;
1816 	rs = npi_fflp_cfg_ip_usr_cls_set(handle, class, tos, tos_mask,
1817 	    proto, ver);
1818 	if (rs & NPI_FFLP_ERROR) {
1819 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1820 		    " nxge_fflp_ip_usr_class_config"
1821 		    " for class %d failed ", class));
1822 		return (NXGE_ERROR | rs);
1823 	}
1824 	if (class_enable)
1825 		rs = npi_fflp_cfg_ip_usr_cls_enable(handle, class);
1826 	else
1827 		rs = npi_fflp_cfg_ip_usr_cls_disable(handle, class);
1828 
1829 	if (rs & NPI_FFLP_ERROR) {
1830 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1831 		    " nxge_fflp_ip_usr_class_config"
1832 		    " TCAM enable/disable for class %d failed ", class));
1833 		return (NXGE_ERROR | rs);
1834 	}
1835 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_ip_usr_class_config"));
1836 	return (NXGE_OK);
1837 }
1838 
1839 nxge_status_t
1840 nxge_fflp_ip_class_config(p_nxge_t nxgep, tcam_class_t class, uint32_t config)
1841 {
1842 	uint32_t class_config;
1843 	nxge_status_t t_status = NXGE_OK;
1844 	nxge_status_t f_status = NXGE_OK;
1845 	p_nxge_class_pt_cfg_t p_class_cfgp;
1846 
1847 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_fflp_ip_class_config"));
1848 
1849 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
1850 	class_config = p_class_cfgp->class_cfg[class];
1851 
1852 	if (class_config != config) {
1853 		p_class_cfgp->class_cfg[class] = config;
1854 		class_config = config;
1855 	}
1856 
1857 	t_status = nxge_cfg_tcam_ip_class(nxgep, class, class_config);
1858 	f_status = nxge_cfg_ip_cls_flow_key(nxgep, class, class_config);
1859 
1860 	if (t_status & NPI_FFLP_ERROR) {
1861 		NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1862 		    " nxge_fflp_ip_class_config %x"
1863 		    " for class %d tcam failed", config, class));
1864 		return (t_status);
1865 	}
1866 	if (f_status & NPI_FFLP_ERROR) {
1867 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1868 		    " nxge_fflp_ip_class_config %x"
1869 		    " for class %d flow key failed", config, class));
1870 		return (f_status);
1871 	}
1872 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_ip_class_config"));
1873 	return (NXGE_OK);
1874 }
1875 
1876 nxge_status_t
1877 nxge_fflp_ip_class_config_get(p_nxge_t nxgep, tcam_class_t class,
1878 	uint32_t *config)
1879 {
1880 	uint32_t t_class_config, f_class_config;
1881 	int t_status = NXGE_OK;
1882 	int f_status = NXGE_OK;
1883 
1884 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, " ==> nxge_fflp_ip_class_config"));
1885 
1886 	t_class_config = f_class_config = 0;
1887 	t_status = nxge_cfg_tcam_ip_class_get(nxgep, class, &t_class_config);
1888 	f_status = nxge_cfg_ip_cls_flow_key_get(nxgep, class, &f_class_config);
1889 
1890 	if (t_status & NPI_FFLP_ERROR) {
1891 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1892 		    " nxge_fflp_ip_class_config_get  "
1893 		    " for class %d tcam failed", class));
1894 		return (t_status);
1895 	}
1896 
1897 	if (f_status & NPI_FFLP_ERROR) {
1898 		NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1899 		    " nxge_fflp_ip_class_config_get  "
1900 		    " for class %d flow key failed", class));
1901 		return (f_status);
1902 	}
1903 
1904 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
1905 	    " nxge_fflp_ip_class_config tcam %x flow %x",
1906 	    t_class_config, f_class_config));
1907 
1908 	*config = t_class_config | f_class_config;
1909 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_ip_class_config_get"));
1910 	return (NXGE_OK);
1911 }
1912 
1913 nxge_status_t
1914 nxge_fflp_ip_class_config_all(p_nxge_t nxgep)
1915 {
1916 	uint32_t class_config;
1917 	tcam_class_t class;
1918 
1919 #ifdef	NXGE_DEBUG
1920 	int status = NXGE_OK;
1921 #endif
1922 
1923 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_ip_class_config"));
1924 	for (class = TCAM_CLASS_TCP_IPV4;
1925 	    class <= TCAM_CLASS_SCTP_IPV6; class++) {
1926 		class_config = nxgep->class_config.class_cfg[class];
1927 #ifndef	NXGE_DEBUG
1928 		(void) nxge_fflp_ip_class_config(nxgep, class, class_config);
1929 #else
1930 		status = nxge_fflp_ip_class_config(nxgep, class, class_config);
1931 		if (status & NPI_FFLP_ERROR) {
1932 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1933 			    "nxge_fflp_ip_class_config failed "
1934 			    " class %d config %x ",
1935 			    class, class_config));
1936 		}
1937 #endif
1938 	}
1939 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_ip_class_config"));
1940 	return (NXGE_OK);
1941 }
1942 
1943 nxge_status_t
1944 nxge_fflp_config_vlan_table(p_nxge_t nxgep, uint16_t vlan_id)
1945 {
1946 	uint8_t port, rdc_grp;
1947 	npi_handle_t handle;
1948 	npi_status_t rs = NPI_SUCCESS;
1949 	uint8_t priority = 1;
1950 	p_nxge_mv_cfg_t vlan_table;
1951 	p_nxge_class_pt_cfg_t p_class_cfgp;
1952 	p_nxge_hw_list_t hw_p;
1953 
1954 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_config_vlan_table"));
1955 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
1956 	handle = nxgep->npi_reg_handle;
1957 	vlan_table = p_class_cfgp->vlan_tbl;
1958 	port = nxgep->function_num;
1959 
1960 	if (vlan_table[vlan_id].flag == 0) {
1961 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1962 		    " nxge_fflp_config_vlan_table"
1963 		    " vlan id is not configured %d", vlan_id));
1964 		return (NXGE_ERROR);
1965 	}
1966 
1967 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
1968 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1969 		    " nxge_fflp_config_vlan_table:"
1970 		    " common hardware not set", nxgep->niu_type));
1971 		return (NXGE_ERROR);
1972 	}
1973 	MUTEX_ENTER(&hw_p->nxge_vlan_lock);
1974 	rdc_grp = vlan_table[vlan_id].rdctbl;
1975 	rs = npi_fflp_cfg_enet_vlan_table_assoc(handle,
1976 	    port, vlan_id,
1977 	    rdc_grp, priority);
1978 
1979 	MUTEX_EXIT(&hw_p->nxge_vlan_lock);
1980 	if (rs & NPI_FFLP_ERROR) {
1981 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1982 		    "nxge_fflp_config_vlan_table failed "
1983 		    " Port %d vlan_id %d rdc_grp %d",
1984 		    port, vlan_id, rdc_grp));
1985 		return (NXGE_ERROR | rs);
1986 	}
1987 
1988 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_fflp_config_vlan_table"));
1989 	return (NXGE_OK);
1990 }
1991 
1992 nxge_status_t
1993 nxge_fflp_update_hw(p_nxge_t nxgep)
1994 {
1995 	nxge_status_t status = NXGE_OK;
1996 	p_nxge_param_t pa;
1997 	uint64_t cfgd_vlans;
1998 	uint64_t *val_ptr;
1999 	int i;
2000 	int num_macs;
2001 	uint8_t alt_mac;
2002 	nxge_param_map_t *p_map;
2003 	p_nxge_mv_cfg_t vlan_table;
2004 	p_nxge_class_pt_cfg_t p_class_cfgp;
2005 	p_nxge_dma_pt_cfg_t p_all_cfgp;
2006 	p_nxge_hw_pt_cfg_t p_cfgp;
2007 
2008 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_fflp_update_hw"));
2009 
2010 	p_class_cfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
2011 	p_all_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
2012 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_all_cfgp->hw_config;
2013 
2014 	status = nxge_fflp_set_hash1(nxgep, p_class_cfgp->init_h1);
2015 	if (status != NXGE_OK) {
2016 		NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
2017 		    "nxge_fflp_set_hash1 Failed"));
2018 		return (NXGE_ERROR);
2019 	}
2020 
2021 	status = nxge_fflp_set_hash2(nxgep, p_class_cfgp->init_h2);
2022 	if (status != NXGE_OK) {
2023 		NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
2024 		    "nxge_fflp_set_hash2 Failed"));
2025 		return (NXGE_ERROR);
2026 	}
2027 	vlan_table = p_class_cfgp->vlan_tbl;
2028 
2029 	/* configure vlan tables */
2030 	pa = (p_nxge_param_t)&nxgep->param_arr[param_vlan_2rdc_grp];
2031 #if defined(__i386)
2032 	val_ptr = (uint64_t *)(uint32_t)pa->value;
2033 #else
2034 	val_ptr = (uint64_t *)pa->value;
2035 #endif
2036 	cfgd_vlans = ((pa->type & NXGE_PARAM_ARRAY_CNT_MASK) >>
2037 	    NXGE_PARAM_ARRAY_CNT_SHIFT);
2038 
2039 	for (i = 0; i < cfgd_vlans; i++) {
2040 		p_map = (nxge_param_map_t *)&val_ptr[i];
2041 		if (vlan_table[p_map->param_id].flag) {
2042 			status = nxge_fflp_config_vlan_table(nxgep,
2043 			    p_map->param_id);
2044 			if (status != NXGE_OK) {
2045 				NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
2046 				    "nxge_fflp_config_vlan_table Failed"));
2047 				return (NXGE_ERROR);
2048 			}
2049 		}
2050 	}
2051 
2052 	/* config MAC addresses */
2053 	num_macs = p_cfgp->max_macs;
2054 	pa = (p_nxge_param_t)&nxgep->param_arr[param_mac_2rdc_grp];
2055 #if defined(__i386)
2056 	val_ptr = (uint64_t *)(uint32_t)pa->value;
2057 #else
2058 	val_ptr = (uint64_t *)pa->value;
2059 #endif
2060 
2061 	for (alt_mac = 0; alt_mac < num_macs; alt_mac++) {
2062 		if (p_class_cfgp->mac_host_info[alt_mac].flag) {
2063 			status = nxge_logical_mac_assign_rdc_table(nxgep,
2064 			    alt_mac);
2065 			if (status != NXGE_OK) {
2066 				NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
2067 				    "nxge_logical_mac_assign_rdc_table"
2068 				    " Failed"));
2069 				return (NXGE_ERROR);
2070 			}
2071 		}
2072 	}
2073 
2074 	/* Config Hash values */
2075 	/* config classes */
2076 	status = nxge_fflp_ip_class_config_all(nxgep);
2077 	if (status != NXGE_OK) {
2078 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2079 		    "nxge_fflp_ip_class_config_all Failed"));
2080 		return (NXGE_ERROR);
2081 	}
2082 	return (NXGE_OK);
2083 }
2084 
2085 nxge_status_t
2086 nxge_classify_init_hw(p_nxge_t nxgep)
2087 {
2088 	nxge_status_t status = NXGE_OK;
2089 
2090 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "==> nxge_classify_init_hw"));
2091 
2092 	if (nxgep->classifier.state & NXGE_FFLP_HW_INIT) {
2093 		NXGE_DEBUG_MSG((nxgep, FFLP_CTL,
2094 		    "nxge_classify_init_hw already init"));
2095 		return (NXGE_OK);
2096 	}
2097 
2098 	/* Now do a real configuration */
2099 	status = nxge_fflp_update_hw(nxgep);
2100 	if (status != NXGE_OK) {
2101 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2102 		    "nxge_fflp_update_hw failed"));
2103 		return (NXGE_ERROR);
2104 	}
2105 
2106 	/* Init RDC tables? ? who should do that? rxdma or fflp ? */
2107 	/* attach rdc table to the MAC port. */
2108 	status = nxge_main_mac_assign_rdc_table(nxgep);
2109 	if (status != NXGE_OK) {
2110 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2111 		    "nxge_main_mac_assign_rdc_table failed"));
2112 		return (NXGE_ERROR);
2113 	}
2114 
2115 	status = nxge_alt_mcast_mac_assign_rdc_table(nxgep);
2116 	if (status != NXGE_OK) {
2117 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2118 		    "nxge_multicast_mac_assign_rdc_table failed"));
2119 		return (NXGE_ERROR);
2120 	}
2121 
2122 	if (nxgep->classifier.fragment_bug == 1) {
2123 		status = nxge_tcam_handle_ip_fragment(nxgep);
2124 		if (status != NXGE_OK) {
2125 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2126 			    "nxge_tcam_handle_ip_fragment failed"));
2127 			return (NXGE_ERROR);
2128 		}
2129 	}
2130 
2131 	nxgep->classifier.state |= NXGE_FFLP_HW_INIT;
2132 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_classify_init_hw"));
2133 	return (NXGE_OK);
2134 }
2135 
2136 nxge_status_t
2137 nxge_fflp_handle_sys_errors(p_nxge_t nxgep)
2138 {
2139 	npi_handle_t handle;
2140 	p_nxge_fflp_stats_t statsp;
2141 	uint8_t portn, rdc_grp;
2142 	p_nxge_dma_pt_cfg_t p_dma_cfgp;
2143 	p_nxge_hw_pt_cfg_t p_cfgp;
2144 	vlan_par_err_t vlan_err;
2145 	tcam_err_t tcam_err;
2146 	hash_lookup_err_log1_t fcram1_err;
2147 	hash_lookup_err_log2_t fcram2_err;
2148 	hash_tbl_data_log_t fcram_err;
2149 
2150 	handle = nxgep->npi_handle;
2151 	statsp = (p_nxge_fflp_stats_t)&nxgep->statsp->fflp_stats;
2152 	portn = nxgep->mac.portnum;
2153 
2154 	/*
2155 	 * need to read the fflp error registers to figure out what the error
2156 	 * is
2157 	 */
2158 	npi_fflp_vlan_error_get(handle, &vlan_err);
2159 	npi_fflp_tcam_error_get(handle, &tcam_err);
2160 
2161 	if (vlan_err.bits.ldw.m_err || vlan_err.bits.ldw.err) {
2162 		NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2163 		    " vlan table parity error on port %d"
2164 		    " addr: 0x%x data: 0x%x",
2165 		    portn, vlan_err.bits.ldw.addr,
2166 		    vlan_err.bits.ldw.data));
2167 		statsp->vlan_parity_err++;
2168 
2169 		if (vlan_err.bits.ldw.m_err) {
2170 			NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2171 			    " vlan table multiple errors on port %d",
2172 			    portn));
2173 		}
2174 		statsp->errlog.vlan = (uint32_t)vlan_err.value;
2175 		NXGE_FM_REPORT_ERROR(nxgep, 0, 0,
2176 		    NXGE_FM_EREPORT_FFLP_VLAN_PAR_ERR);
2177 		npi_fflp_vlan_error_clear(handle);
2178 	}
2179 
2180 	if (tcam_err.bits.ldw.err) {
2181 		if (tcam_err.bits.ldw.p_ecc != 0) {
2182 			NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2183 			    " TCAM ECC error on port %d"
2184 			    " TCAM entry: 0x%x syndrome: 0x%x",
2185 			    portn, tcam_err.bits.ldw.addr,
2186 			    tcam_err.bits.ldw.syndrome));
2187 			statsp->tcam_ecc_err++;
2188 		} else {
2189 			NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2190 			    " TCAM Parity error on port %d"
2191 			    " addr: 0x%x parity value: 0x%x",
2192 			    portn, tcam_err.bits.ldw.addr,
2193 			    tcam_err.bits.ldw.syndrome));
2194 			statsp->tcam_parity_err++;
2195 		}
2196 
2197 		if (tcam_err.bits.ldw.mult) {
2198 			NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2199 			    " TCAM Multiple errors on port %d", portn));
2200 		} else {
2201 			NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2202 			    " TCAM PIO error on port %d", portn));
2203 		}
2204 
2205 		statsp->errlog.tcam = (uint32_t)tcam_err.value;
2206 		NXGE_FM_REPORT_ERROR(nxgep, 0, 0,
2207 		    NXGE_FM_EREPORT_FFLP_TCAM_ERR);
2208 		npi_fflp_tcam_error_clear(handle);
2209 	}
2210 
2211 	p_dma_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
2212 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
2213 
2214 	for (rdc_grp = 0; rdc_grp < NXGE_MAX_RDC_GROUPS; rdc_grp++) {
2215 		if (p_cfgp->grpids[rdc_grp]) {
2216 			npi_fflp_fcram_error_get(handle, &fcram_err, rdc_grp);
2217 			if (fcram_err.bits.ldw.pio_err) {
2218 				NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2219 				    " FCRAM PIO ECC error on port %d"
2220 				    " rdc group: %d Hash Table addr: 0x%x"
2221 				    " syndrome: 0x%x",
2222 				    portn, rdc_grp,
2223 				    fcram_err.bits.ldw.fcram_addr,
2224 				    fcram_err.bits.ldw.syndrome));
2225 				statsp->hash_pio_err[rdc_grp]++;
2226 				statsp->errlog.hash_pio[rdc_grp] =
2227 				    (uint32_t)fcram_err.value;
2228 				NXGE_FM_REPORT_ERROR(nxgep, 0, 0,
2229 				    NXGE_FM_EREPORT_FFLP_HASHT_DATA_ERR);
2230 				npi_fflp_fcram_error_clear(handle, rdc_grp);
2231 			}
2232 		}
2233 	}
2234 
2235 	npi_fflp_fcram_error_log1_get(handle, &fcram1_err);
2236 	if (fcram1_err.bits.ldw.ecc_err) {
2237 		char *multi_str = "";
2238 		char *multi_bit_str = "";
2239 
2240 		npi_fflp_fcram_error_log2_get(handle, &fcram2_err);
2241 		if (fcram1_err.bits.ldw.mult_lk) {
2242 			multi_str = "multiple";
2243 		}
2244 		if (fcram1_err.bits.ldw.mult_bit) {
2245 			multi_bit_str = "multiple bits";
2246 		}
2247 		statsp->hash_lookup_err++;
2248 		NXGE_ERROR_MSG((nxgep, FFLP_CTL,
2249 		    " FCRAM %s lookup %s ECC error on port %d"
2250 		    " H1: 0x%x Subarea: 0x%x Syndrome: 0x%x",
2251 		    multi_str, multi_bit_str, portn,
2252 		    fcram2_err.bits.ldw.h1,
2253 		    fcram2_err.bits.ldw.subarea,
2254 		    fcram2_err.bits.ldw.syndrome));
2255 		NXGE_FM_REPORT_ERROR(nxgep, 0, 0,
2256 		    NXGE_FM_EREPORT_FFLP_HASHT_LOOKUP_ERR);
2257 	}
2258 	statsp->errlog.hash_lookup1 = (uint32_t)fcram1_err.value;
2259 	statsp->errlog.hash_lookup2 = (uint32_t)fcram2_err.value;
2260 	return (NXGE_OK);
2261 }
2262 
2263 int
2264 nxge_get_valid_tcam_cnt(p_nxge_t nxgep) {
2265 	return ((nxgep->classifier.fragment_bug == 1) ?
2266 		nxgep->classifier.tcam_entry_cnt - 1 :
2267 		nxgep->classifier.tcam_entry_cnt);
2268 }
2269 
2270 int
2271 nxge_rxdma_channel_cnt(p_nxge_t nxgep)
2272 {
2273 	p_nxge_dma_pt_cfg_t p_dma_cfgp;
2274 	p_nxge_hw_pt_cfg_t p_cfgp;
2275 
2276 	p_dma_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
2277 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
2278 	return (p_cfgp->max_rdcs);
2279 }
2280 
2281 /* ARGSUSED */
2282 int
2283 nxge_rxclass_ioctl(p_nxge_t nxgep, queue_t *wq, mblk_t *mp)
2284 {
2285 	uint32_t cmd;
2286 	rx_class_cfg_t *cfg_info = (rx_class_cfg_t *)mp->b_rptr;
2287 
2288 	if (nxgep == NULL) {
2289 		return (-1);
2290 	}
2291 	cmd = cfg_info->cmd;
2292 	switch (cmd) {
2293 	default:
2294 		return (-1);
2295 
2296 	case NXGE_RX_CLASS_GCHAN:
2297 		cfg_info->data = nxge_rxdma_channel_cnt(nxgep);
2298 		break;
2299 	case NXGE_RX_CLASS_GRULE_CNT:
2300 		MUTEX_ENTER(&nxgep->nxge_hw_p->nxge_tcam_lock);
2301 		cfg_info->rule_cnt = nxge_get_valid_tcam_cnt(nxgep);
2302 		MUTEX_EXIT(&nxgep->nxge_hw_p->nxge_tcam_lock);
2303 		break;
2304 	case NXGE_RX_CLASS_GRULE:
2305 		nxge_get_tcam_entry(nxgep, &cfg_info->fs);
2306 		break;
2307 	case NXGE_RX_CLASS_GRULE_ALL:
2308 		nxge_get_tcam_entry_all(nxgep, cfg_info);
2309 		break;
2310 	case NXGE_RX_CLASS_RULE_DEL:
2311 		nxge_del_tcam_entry(nxgep, cfg_info->fs.location);
2312 		break;
2313 	case NXGE_RX_CLASS_RULE_INS:
2314 		(void) nxge_add_tcam_entry(nxgep, &cfg_info->fs);
2315 		break;
2316 	}
2317 	return (0);
2318 }
2319 /* ARGSUSED */
2320 int
2321 nxge_rxhash_ioctl(p_nxge_t nxgep, queue_t *wq, mblk_t *mp)
2322 {
2323 	uint32_t cmd;
2324 	cfg_cmd_t	*cfg_info = (cfg_cmd_t *)mp->b_rptr;
2325 
2326 	if (nxgep == NULL) {
2327 		return (-1);
2328 	}
2329 	cmd = cfg_info->cmd;
2330 
2331 	switch (cmd) {
2332 	default:
2333 		return (-1);
2334 	case NXGE_IPTUN_CFG_ADD_CLS:
2335 		nxge_add_iptun_class(nxgep, &cfg_info->iptun_cfg,
2336 		    &cfg_info->class_id);
2337 		break;
2338 	case NXGE_IPTUN_CFG_SET_HASH:
2339 		nxge_cfg_iptun_hash(nxgep, &cfg_info->iptun_cfg,
2340 		    cfg_info->class_id);
2341 		break;
2342 	case NXGE_IPTUN_CFG_DEL_CLS:
2343 		nxge_del_iptun_class(nxgep, cfg_info->class_id);
2344 		break;
2345 	case NXGE_IPTUN_CFG_GET_CLS:
2346 		nxge_get_iptun_class(nxgep, &cfg_info->iptun_cfg,
2347 		    cfg_info->class_id);
2348 		break;
2349 	case NXGE_CLS_CFG_SET_SYM:
2350 		nxge_set_ip_cls_sym(nxgep, cfg_info->class_id, cfg_info->sym);
2351 		break;
2352 	case NXGE_CLS_CFG_GET_SYM:
2353 		nxge_get_ip_cls_sym(nxgep, cfg_info->class_id, &cfg_info->sym);
2354 		break;
2355 	}
2356 	return (0);
2357 }
2358 
2359 void
2360 nxge_get_tcam_entry_all(p_nxge_t nxgep, rx_class_cfg_t *cfgp)
2361 {
2362 	nxge_classify_t *clasp = &nxgep->classifier;
2363 	uint16_t	n_entries;
2364 	int		i, j, k;
2365 	tcam_flow_spec_t	*tcam_entryp;
2366 
2367 	cfgp->data = clasp->tcam_size;
2368 	MUTEX_ENTER(&nxgep->nxge_hw_p->nxge_tcam_lock);
2369 	n_entries = cfgp->rule_cnt;
2370 
2371 	for (i = 0, j = 0; j < cfgp->data; j++) {
2372 		k = nxge_tcam_get_index(nxgep, j);
2373 		tcam_entryp = &clasp->tcam_entries[k];
2374 		if (tcam_entryp->valid != 1)
2375 			continue;
2376 		cfgp->rule_locs[i] = j;
2377 		i++;
2378 	};
2379 	MUTEX_EXIT(&nxgep->nxge_hw_p->nxge_tcam_lock);
2380 
2381 	if (n_entries != i) {
2382 		/* print warning, this should not happen */
2383 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_get_tcam_entry_all"
2384 		    "n_entries[%d] != i[%d]!!!", n_entries, i));
2385 	}
2386 }
2387 
2388 
2389 /* Entries for the ports are interleaved in the TCAM */
2390 static uint16_t
2391 nxge_tcam_get_index(p_nxge_t nxgep, uint16_t index)
2392 {
2393 	/* One entry reserved for IP fragment rule */
2394 	if (index >= (nxgep->classifier.tcam_size - 1))
2395 		index = 0;
2396 	if (nxgep->classifier.fragment_bug == 1)
2397 		index++;
2398 	return (nxgep->classifier.tcam_top + (index * nxgep->nports));
2399 }
2400 
2401 static uint32_t
2402 nxge_tcam_cls_to_flow(uint32_t class_code) {
2403 	switch (class_code) {
2404 	case TCAM_CLASS_TCP_IPV4:
2405 		return (FSPEC_TCPIP4);
2406 	case TCAM_CLASS_UDP_IPV4:
2407 		return (FSPEC_UDPIP4);
2408 	case TCAM_CLASS_AH_ESP_IPV4:
2409 		return (FSPEC_AHIP4);
2410 	case TCAM_CLASS_SCTP_IPV4:
2411 		return (FSPEC_SCTPIP4);
2412 	case  TCAM_CLASS_TCP_IPV6:
2413 		return (FSPEC_TCPIP6);
2414 	case TCAM_CLASS_UDP_IPV6:
2415 		return (FSPEC_UDPIP6);
2416 	case TCAM_CLASS_AH_ESP_IPV6:
2417 		return (FSPEC_AHIP6);
2418 	case TCAM_CLASS_SCTP_IPV6:
2419 		return (FSPEC_SCTPIP6);
2420 	case TCAM_CLASS_IP_USER_4:
2421 	case TCAM_CLASS_IP_USER_5:
2422 	case TCAM_CLASS_IP_USER_6:
2423 	case TCAM_CLASS_IP_USER_7:
2424 		return (FSPEC_IP_USR);
2425 	default:
2426 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL, "nxge_tcam_cls_to_flow"
2427 		    ": Unknown class code [0x%x]", class_code));
2428 		break;
2429 	}
2430 	return (0);
2431 }
2432 
2433 void
2434 nxge_get_tcam_entry(p_nxge_t nxgep, flow_resource_t *fs)
2435 {
2436 	uint16_t 	index;
2437 	tcam_flow_spec_t *tcam_ep;
2438 	tcam_entry_t	*tp;
2439 	flow_spec_t	*fspec;
2440 #define	fspec_key (fspec->uh.tcpip4spec)
2441 #define	fspec_mask (fspec->um.tcpip4spec)
2442 
2443 	index = nxge_tcam_get_index(nxgep, (uint16_t)fs->location);
2444 	tcam_ep = &nxgep->classifier.tcam_entries[index];
2445 	if (tcam_ep->valid != 1) {
2446 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_get_tcam_entry: :"
2447 		    "Entry [%d] invalid for index [%d]", fs->location, index));
2448 		return;
2449 	}
2450 
2451 	/* Fill the flow spec entry */
2452 	tp = &tcam_ep->tce;
2453 	fspec = &fs->flow_spec;
2454 	fspec->flow_type = nxge_tcam_cls_to_flow(tp->ip4_class_key);
2455 
2456 	/* TODO - look at proto field to differentiate between AH and ESP */
2457 	if (fspec->flow_type == FSPEC_AHIP4) {
2458 		if (tp->ip4_proto_key == IPPROTO_ESP)
2459 			fspec->flow_type = FSPEC_ESPIP4;
2460 	}
2461 
2462 	switch (tp->ip4_class_key) {
2463 	case TCAM_CLASS_TCP_IPV4:
2464 	case TCAM_CLASS_UDP_IPV4:
2465 	case TCAM_CLASS_AH_ESP_IPV4:
2466 	case TCAM_CLASS_SCTP_IPV4:
2467 		FSPEC_IPV4_ADDR(fspec_key.ip4dst, tp->ip4_dest_key);
2468 		FSPEC_IPV4_ADDR(fspec_mask.ip4dst, tp->ip4_dest_mask);
2469 		FSPEC_IPV4_ADDR(fspec_key.ip4src, tp->ip4_src_key);
2470 		FSPEC_IPV4_ADDR(fspec_mask.ip4src, tp->ip4_src_mask);
2471 		fspec_key.tos = tp->ip4_tos_key;
2472 		fspec_mask.tos = tp->ip4_tos_mask;
2473 		break;
2474 	default:
2475 		break;
2476 	}
2477 
2478 	switch (tp->ip4_class_key) {
2479 	case TCAM_CLASS_TCP_IPV4:
2480 	case TCAM_CLASS_UDP_IPV4:
2481 	case TCAM_CLASS_SCTP_IPV4:
2482 		FSPEC_IP_PORTS(fspec_key.pdst, fspec_key.psrc,
2483 		    tp->ip4_port_key);
2484 		FSPEC_IP_PORTS(fspec_mask.pdst, fspec_mask.psrc,
2485 		    tp->ip4_port_mask);
2486 		break;
2487 	case TCAM_CLASS_AH_ESP_IPV4:
2488 		fspec->uh.ahip4spec.spi = tp->ip4_port_key;
2489 		fspec->um.ahip4spec.spi = tp->ip4_port_mask;
2490 		break;
2491 	case TCAM_CLASS_IP_USER_4:
2492 	case TCAM_CLASS_IP_USER_5:
2493 	case TCAM_CLASS_IP_USER_6:
2494 	case TCAM_CLASS_IP_USER_7:
2495 		fspec->uh.ip_usr_spec.l4_4_bytes = tp->ip4_port_key;
2496 		fspec->um.ip_usr_spec.l4_4_bytes = tp->ip4_port_mask;
2497 		fspec->uh.ip_usr_spec.ip_ver = FSPEC_IP4;
2498 		fspec->uh.ip_usr_spec.proto = tp->ip4_proto_key;
2499 		fspec->um.ip_usr_spec.proto = tp->ip4_proto_mask;
2500 		break;
2501 	default:
2502 		break;
2503 	}
2504 
2505 	if (tp->match_action.bits.ldw.disc == 1) {
2506 		fs->channel_cookie = NXGE_PKT_DISCARD;
2507 	} else {
2508 		fs->channel_cookie = tp->match_action.bits.ldw.offset;
2509 	}
2510 #undef fspec_key
2511 #undef fspec_mask
2512 }
2513 
2514 void
2515 nxge_del_tcam_entry(p_nxge_t nxgep, uint32_t location)
2516 {
2517 	npi_status_t rs = NPI_SUCCESS;
2518 	uint16_t 	index;
2519 	tcam_flow_spec_t *tcam_ep;
2520 	tcam_entry_t	*tp;
2521 	tcam_class_t	class;
2522 
2523 	MUTEX_ENTER(&nxgep->nxge_hw_p->nxge_tcam_lock);
2524 	index = nxge_tcam_get_index(nxgep, (uint16_t)location);
2525 	tcam_ep = &nxgep->classifier.tcam_entries[index];
2526 	if (tcam_ep->valid != 1) {
2527 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_del_tcam_entry: :"
2528 		    "Entry [%d] invalid for index [%d]", location, index));
2529 		goto fail;
2530 	}
2531 
2532 	/* Fill the flow spec entry */
2533 	tp = &tcam_ep->tce;
2534 	class = tp->ip4_class_key;
2535 	if (class >= TCAM_CLASS_IP_USER_4 && class <= TCAM_CLASS_IP_USER_7) {
2536 		int i;
2537 		nxge_usr_l3_cls_t *l3_ucls_p;
2538 		p_nxge_hw_list_t hw_p = nxgep->nxge_hw_p;
2539 
2540 		for (i = 0; i < NXGE_L3_PROG_CLS; i++) {
2541 			l3_ucls_p = &hw_p->tcam_l3_prog_cls[i];
2542 			if (l3_ucls_p->valid) {
2543 				if (l3_ucls_p->cls == class &&
2544 				    l3_ucls_p->tcam_ref_cnt) {
2545 					l3_ucls_p->tcam_ref_cnt--;
2546 					if (l3_ucls_p->tcam_ref_cnt > 0)
2547 						continue;
2548 					/* disable class */
2549 					rs = npi_fflp_cfg_ip_usr_cls_disable(
2550 					    nxgep->npi_reg_handle,
2551 					    (tcam_class_t)class);
2552 					if (rs != NPI_SUCCESS)
2553 						goto fail;
2554 					l3_ucls_p->cls = 0;
2555 					l3_ucls_p->pid = 0;
2556 					l3_ucls_p->valid = 0;
2557 					break;
2558 				}
2559 			}
2560 		}
2561 		if (i == NXGE_L3_PROG_CLS) {
2562 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2563 			    "nxge_del_tcam_entry: Usr class "
2564 			    "0x%llx not found", (unsigned long long) class));
2565 			goto fail;
2566 		}
2567 	}
2568 
2569 	rs = npi_fflp_tcam_entry_invalidate(nxgep->npi_reg_handle, index);
2570 	if (rs != NPI_SUCCESS) {
2571 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2572 		    "nxge_del_tcam_entry: TCAM invalidate failed "
2573 		    "at loc %d ", location));
2574 		goto fail;
2575 	}
2576 
2577 	nxgep->classifier.tcam_entries[index].valid = 0;
2578 	nxgep->classifier.tcam_entry_cnt--;
2579 
2580 	MUTEX_EXIT(&nxgep->nxge_hw_p->nxge_tcam_lock);
2581 	NXGE_DEBUG_MSG((nxgep, FFLP_CTL, "<== nxge_del_tcam_entry"));
2582 	return;
2583 fail:
2584 	MUTEX_EXIT(&nxgep->nxge_hw_p->nxge_tcam_lock);
2585 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2586 	    "<== nxge_del_tcam_entry FAILED"));
2587 }
2588 
2589 static uint8_t
2590 nxge_iptun_pkt_type_to_pid(uint8_t pkt_type)
2591 {
2592 	uint8_t pid = 0;
2593 
2594 	switch (pkt_type) {
2595 	case IPTUN_PKT_IPV4:
2596 		pid = 4;
2597 		break;
2598 	case IPTUN_PKT_IPV6:
2599 		pid = 41;
2600 		break;
2601 	case IPTUN_PKT_GRE:
2602 		pid = 47;
2603 		break;
2604 	case IPTUN_PKT_GTP:
2605 		pid = 17;
2606 		break;
2607 	default:
2608 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
2609 		    "nxge_iptun_pkt_type_to_pid: Unknown pkt type 0x%x",
2610 		    pkt_type));
2611 		break;
2612 	}
2613 
2614 	return (pid);
2615 }
2616 
2617 static npi_status_t
2618 nxge_set_iptun_usr_cls_reg(p_nxge_t nxgep, uint64_t class,
2619 		iptun_cfg_t *iptunp)
2620 {
2621 	npi_handle_t handle = nxgep->npi_reg_handle;
2622 	npi_status_t rs = NPI_SUCCESS;
2623 
2624 	switch (iptunp->in_pkt_type) {
2625 	case IPTUN_PKT_IPV4:
2626 	case IPTUN_PKT_IPV6:
2627 		rs = npi_fflp_cfg_ip_usr_cls_set_iptun(handle,
2628 		    (tcam_class_t)class, 0, 0, 0, 0);
2629 		break;
2630 	case IPTUN_PKT_GRE:
2631 		rs = npi_fflp_cfg_ip_usr_cls_set_iptun(handle,
2632 		    (tcam_class_t)class, iptunp->l4b0_val,
2633 		    iptunp->l4b0_mask, 0, 0);
2634 		break;
2635 	case IPTUN_PKT_GTP:
2636 		rs = npi_fflp_cfg_ip_usr_cls_set_iptun(handle,
2637 		    (tcam_class_t)class, 0, 0, iptunp->l4b23_val,
2638 		    (iptunp->l4b23_sel & 0x01));
2639 		break;
2640 	default:
2641 		rs = NPI_FFLP_TCAM_CLASS_INVALID;
2642 		break;
2643 	}
2644 	return (rs);
2645 }
2646 
2647 void
2648 nxge_add_iptun_class(p_nxge_t nxgep, iptun_cfg_t *iptunp,
2649 		uint8_t *cls_idp)
2650 {
2651 	int i, add_cls;
2652 	uint8_t pid;
2653 	uint64_t class;
2654 	p_nxge_hw_list_t hw_p = nxgep->nxge_hw_p;
2655 	npi_handle_t handle = nxgep->npi_reg_handle;
2656 	npi_status_t rs = NPI_SUCCESS;
2657 
2658 	pid = nxge_iptun_pkt_type_to_pid(iptunp->in_pkt_type);
2659 	if (pid == 0)
2660 		return;
2661 
2662 	add_cls = 0;
2663 	MUTEX_ENTER(&hw_p->nxge_tcam_lock);
2664 
2665 	/* Get an user programmable class ID */
2666 	class = TCAM_CLASS_INVALID;
2667 	for (i = 0; i < NXGE_L3_PROG_CLS; i++) {
2668 		if (hw_p->tcam_l3_prog_cls[i].valid == 0) {
2669 			/* todo add new usr class reg */
2670 			switch (i) {
2671 			case 0:
2672 				class = TCAM_CLASS_IP_USER_4;
2673 				break;
2674 			case 1:
2675 				class = TCAM_CLASS_IP_USER_5;
2676 				break;
2677 			case 2:
2678 				class = TCAM_CLASS_IP_USER_6;
2679 				break;
2680 			case 3:
2681 				class = TCAM_CLASS_IP_USER_7;
2682 				break;
2683 			default:
2684 				break;
2685 			}
2686 			rs = npi_fflp_cfg_ip_usr_cls_set(handle,
2687 			    (tcam_class_t)class, 0, 0, pid, 0);
2688 			if (rs != NPI_SUCCESS)
2689 				goto fail;
2690 
2691 			rs = nxge_set_iptun_usr_cls_reg(nxgep, class, iptunp);
2692 
2693 			if (rs != NPI_SUCCESS)
2694 				goto fail;
2695 
2696 			rs = npi_fflp_cfg_ip_usr_cls_enable(handle,
2697 			    (tcam_class_t)class);
2698 			if (rs != NPI_SUCCESS)
2699 				goto fail;
2700 
2701 			hw_p->tcam_l3_prog_cls[i].cls = class;
2702 			hw_p->tcam_l3_prog_cls[i].pid = pid;
2703 			hw_p->tcam_l3_prog_cls[i].flow_pkt_type =
2704 			    iptunp->in_pkt_type;
2705 			hw_p->tcam_l3_prog_cls[i].valid = 1;
2706 			*cls_idp = (uint8_t)class;
2707 			add_cls = 1;
2708 			break;
2709 		} else if (hw_p->tcam_l3_prog_cls[i].pid == pid) {
2710 			if (hw_p->tcam_l3_prog_cls[i].flow_pkt_type == 0) {
2711 				/* there is no flow key */
2712 				/* todo program the existing usr class reg */
2713 
2714 				rs = nxge_set_iptun_usr_cls_reg(nxgep, class,
2715 				    iptunp);
2716 				if (rs != NPI_SUCCESS)
2717 					goto fail;
2718 
2719 				rs = npi_fflp_cfg_ip_usr_cls_enable(handle,
2720 				    (tcam_class_t)class);
2721 				if (rs != NPI_SUCCESS)
2722 					goto fail;
2723 
2724 				hw_p->tcam_l3_prog_cls[i].flow_pkt_type =
2725 				    iptunp->in_pkt_type;
2726 				*cls_idp = (uint8_t)class;
2727 				add_cls = 1;
2728 			} else {
2729 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2730 				    "nxge_add_iptun_class: L3 usr "
2731 				    "programmable class with pid %d "
2732 				    "already exists", pid));
2733 			}
2734 			break;
2735 		}
2736 	}
2737 	MUTEX_EXIT(&hw_p->nxge_tcam_lock);
2738 
2739 	if (add_cls != 1) {
2740 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2741 		    "nxge_add_iptun_class: Could not add IP tunneling class"));
2742 	}
2743 	return;
2744 fail:
2745 	MUTEX_EXIT(&hw_p->nxge_tcam_lock);
2746 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_add_iptun_class: FAILED"));
2747 }
2748 
2749 static boolean_t
2750 nxge_is_iptun_cls_present(p_nxge_t nxgep, uint8_t cls_id, int *idx)
2751 {
2752 	int i;
2753 	p_nxge_hw_list_t hw_p = nxgep->nxge_hw_p;
2754 
2755 	MUTEX_ENTER(&hw_p->nxge_tcam_lock);
2756 	for (i = 0; i < NXGE_L3_PROG_CLS; i++) {
2757 		if (hw_p->tcam_l3_prog_cls[i].valid &&
2758 		    hw_p->tcam_l3_prog_cls[i].flow_pkt_type != 0) {
2759 			if (hw_p->tcam_l3_prog_cls[i].cls == cls_id)
2760 				break;
2761 		}
2762 	}
2763 	MUTEX_EXIT(&hw_p->nxge_tcam_lock);
2764 
2765 	if (i == NXGE_L3_PROG_CLS) {
2766 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2767 		    "nxge_is_iptun_cls_present: Invalid class %d", cls_id));
2768 		return (B_FALSE);
2769 	} else {
2770 		*idx = i;
2771 		return (B_TRUE);
2772 	}
2773 }
2774 
2775 void
2776 nxge_cfg_iptun_hash(p_nxge_t nxgep, iptun_cfg_t *iptunp, uint8_t cls_id)
2777 {
2778 	int idx;
2779 	npi_handle_t handle = nxgep->npi_reg_handle;
2780 	flow_key_cfg_t cfg;
2781 
2782 	/* check to see that this is a valid class ID */
2783 	if (!nxge_is_iptun_cls_present(nxgep, cls_id, &idx)) {
2784 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2785 		    "nxge_cfg_iptun_hash: nxge_is_iptun_cls_present "
2786 		    "failed for cls_id %d", cls_id));
2787 		return;
2788 	}
2789 
2790 	bzero((void *)&cfg, sizeof (flow_key_cfg_t));
2791 
2792 	/*
2793 	 * This ensures that all 4 bytes of the XOR value are loaded to the
2794 	 * hash key.
2795 	 */
2796 	cfg.use_dport = cfg.use_sport = cfg.ip_opts_exist = 1;
2797 
2798 	cfg.l4_xor_sel = (iptunp->l4xor_sel & FL_KEY_USR_L4XOR_MSK);
2799 	cfg.use_l4_md = 1;
2800 
2801 	if (iptunp->hash_flags & HASH_L3PROTO)
2802 		cfg.use_proto = 1;
2803 	else if (iptunp->hash_flags & HASH_IPDA)
2804 		cfg.use_daddr = 1;
2805 	else if (iptunp->hash_flags & HASH_IPSA)
2806 		cfg.use_saddr = 1;
2807 	else if (iptunp->hash_flags & HASH_VLAN)
2808 		cfg.use_vlan = 1;
2809 	else if (iptunp->hash_flags & HASH_L2DA)
2810 		cfg.use_l2da = 1;
2811 	else if (iptunp->hash_flags & HASH_IFPORT)
2812 		cfg.use_portnum = 1;
2813 
2814 	(void) npi_fflp_cfg_ip_cls_flow_key_rfnl(handle, (tcam_class_t)cls_id,
2815 	    &cfg);
2816 }
2817 
2818 void
2819 nxge_del_iptun_class(p_nxge_t nxgep, uint8_t cls_id)
2820 {
2821 	int i;
2822 	npi_handle_t handle = nxgep->npi_reg_handle;
2823 	npi_status_t rs = NPI_SUCCESS;
2824 
2825 
2826 	/* check to see that this is a valid class ID */
2827 	if (!nxge_is_iptun_cls_present(nxgep, cls_id, &i)) {
2828 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2829 		    "nxge_del_iptun_class: Invalid class ID 0x%x", cls_id));
2830 		return;
2831 	}
2832 
2833 	MUTEX_ENTER(&nxgep->nxge_hw_p->nxge_tcam_lock);
2834 	rs = npi_fflp_cfg_ip_usr_cls_disable(handle, (tcam_class_t)cls_id);
2835 	if (rs != NPI_SUCCESS)
2836 		goto fail;
2837 	nxgep->nxge_hw_p->tcam_l3_prog_cls[i].flow_pkt_type = 0;
2838 	if (nxgep->nxge_hw_p->tcam_l3_prog_cls[i].tcam_ref_cnt == 0)
2839 		nxgep->nxge_hw_p->tcam_l3_prog_cls[i].valid = 0;
2840 
2841 	MUTEX_EXIT(&nxgep->nxge_hw_p->nxge_tcam_lock);
2842 	return;
2843 fail:
2844 	MUTEX_EXIT(&nxgep->nxge_hw_p->nxge_tcam_lock);
2845 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_del_iptun_class: FAILED"));
2846 }
2847 
2848 void
2849 nxge_get_iptun_class(p_nxge_t nxgep, iptun_cfg_t *iptunp, uint8_t cls_id)
2850 {
2851 	int i;
2852 	uint8_t pid;
2853 	npi_handle_t handle = nxgep->npi_reg_handle;
2854 	npi_status_t rs = NPI_SUCCESS;
2855 	flow_key_cfg_t cfg;
2856 	uint8_t l4b0_val;
2857 	uint8_t l4b0_mask;
2858 	uint8_t l4b23_sel;
2859 	uint16_t l4b23_val;
2860 
2861 	/* check to see that this is a valid class ID */
2862 	if (!nxge_is_iptun_cls_present(nxgep, cls_id, &i))
2863 		return;
2864 
2865 	bzero((void *)iptunp, sizeof (iptun_cfg_t));
2866 
2867 	pid = nxgep->nxge_hw_p->tcam_l3_prog_cls[i].pid;
2868 
2869 	rs = npi_fflp_cfg_ip_usr_cls_get_iptun(handle, (tcam_class_t)cls_id,
2870 	    &l4b0_val, &l4b0_mask, &l4b23_val, &l4b23_sel);
2871 	if (rs != NPI_SUCCESS)
2872 		goto fail;
2873 
2874 	iptunp->l4b0_val = l4b0_val;
2875 	iptunp->l4b0_mask = l4b0_mask;
2876 	iptunp->l4b23_val = l4b23_val;
2877 	iptunp->l4b23_sel = l4b23_sel;
2878 
2879 	if (rs != NPI_SUCCESS)
2880 		goto fail;
2881 
2882 	rs = npi_fflp_cfg_ip_cls_flow_key_get_rfnl(handle,
2883 	    (tcam_class_t)cls_id, &cfg);
2884 	if (rs != NPI_SUCCESS)
2885 		goto fail;
2886 
2887 	iptunp->l4xor_sel = cfg.l4_xor_sel;
2888 	if (cfg.use_proto)
2889 		iptunp->hash_flags |= HASH_L3PROTO;
2890 	else if (cfg.use_daddr)
2891 		iptunp->hash_flags |= HASH_IPDA;
2892 	else if (cfg.use_saddr)
2893 		iptunp->hash_flags |= HASH_IPSA;
2894 	else if (cfg.use_vlan)
2895 		iptunp->hash_flags |= HASH_VLAN;
2896 	else if (cfg.use_l2da)
2897 		iptunp->hash_flags |= HASH_L2DA;
2898 	else if (cfg.use_portnum)
2899 		iptunp->hash_flags |= HASH_IFPORT;
2900 
2901 	switch (pid) {
2902 	case 4:
2903 		iptunp->in_pkt_type = IPTUN_PKT_IPV4;
2904 		break;
2905 	case 41:
2906 		iptunp->in_pkt_type = IPTUN_PKT_IPV6;
2907 		break;
2908 	case 47:
2909 		iptunp->in_pkt_type = IPTUN_PKT_GRE;
2910 		break;
2911 	case 17:
2912 		iptunp->in_pkt_type = IPTUN_PKT_GTP;
2913 		break;
2914 	default:
2915 		iptunp->in_pkt_type = 0;
2916 		break;
2917 	}
2918 
2919 	return;
2920 fail:
2921 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_get_iptun_class: FAILED"));
2922 }
2923 
2924 void
2925 nxge_set_ip_cls_sym(p_nxge_t nxgep, uint8_t cls_id, uint8_t sym)
2926 {
2927 	npi_handle_t handle = nxgep->npi_reg_handle;
2928 	npi_status_t rs = NPI_SUCCESS;
2929 	boolean_t sym_en = (sym == 1) ? B_TRUE : B_FALSE;
2930 
2931 	rs = npi_fflp_cfg_sym_ip_cls_flow_key(handle, (tcam_class_t)cls_id,
2932 	    sym_en);
2933 	if (rs != NPI_SUCCESS)
2934 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
2935 		    "nxge_set_ip_cls_sym: FAILED"));
2936 }
2937 
2938 void
2939 nxge_get_ip_cls_sym(p_nxge_t nxgep, uint8_t cls_id, uint8_t *sym)
2940 {
2941 	npi_handle_t handle = nxgep->npi_reg_handle;
2942 	npi_status_t rs = NPI_SUCCESS;
2943 	flow_key_cfg_t cfg;
2944 
2945 	rs = npi_fflp_cfg_ip_cls_flow_key_get_rfnl(handle,
2946 	    (tcam_class_t)cls_id, &cfg);
2947 	if (rs != NPI_SUCCESS)
2948 		goto fail;
2949 
2950 	if (cfg.use_sym)
2951 		*sym = 1;
2952 	else
2953 		*sym = 0;
2954 	return;
2955 fail:
2956 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_get_ip_cls_sym: FAILED"));
2957 }
2958