xref: /f-stack/dpdk/drivers/net/dpaa2/mc/dpkg.c (revision 031be553)
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2017 NXP
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * * Neither the name of the above-listed copyright holders nor the
17  * names of any contributors may be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  *   GPL LICENSE SUMMARY
21  *
22  * ALTERNATIVELY, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") as published by the Free Software
24  * Foundation, either version 2 of that License or (at your option) any
25  * later version.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 #include <fsl_mc_sys.h>
40 #include <fsl_mc_cmd.h>
41 #include <fsl_dpkg.h>
42 
43 /**
44  * dpkg_prepare_key_cfg() - function prepare extract parameters
45  * @cfg: defining a full Key Generation profile (rule)
46  * @key_cfg_buf: Zeroed 256 bytes of memory before mapping it to DMA
47  *
48  * This function has to be called before the following functions:
49  *	- dpni_set_rx_tc_dist()
50  *	- dpni_set_qos_table()
51  *	- dpkg_prepare_key_cfg()
52  */
53 int
54 dpkg_prepare_key_cfg(const struct dpkg_profile_cfg *cfg, uint8_t *key_cfg_buf)
55 {
56 	int i, j;
57 	struct dpni_ext_set_rx_tc_dist *dpni_ext;
58 	struct dpni_dist_extract *extr;
59 
60 	if (cfg->num_extracts > DPKG_MAX_NUM_OF_EXTRACTS)
61 		return -EINVAL;
62 
63 	dpni_ext = (struct dpni_ext_set_rx_tc_dist *)key_cfg_buf;
64 	dpni_ext->num_extracts = cfg->num_extracts;
65 
66 	for (i = 0; i < cfg->num_extracts; i++) {
67 		extr = &dpni_ext->extracts[i];
68 
69 		switch (cfg->extracts[i].type) {
70 		case DPKG_EXTRACT_FROM_HDR:
71 			extr->prot = cfg->extracts[i].extract.from_hdr.prot;
72 			dpkg_set_field(extr->efh_type, EFH_TYPE,
73 				       cfg->extracts[i].extract.from_hdr.type);
74 			extr->size = cfg->extracts[i].extract.from_hdr.size;
75 			extr->offset = cfg->extracts[i].extract.from_hdr.offset;
76 			extr->field = cpu_to_le32(
77 				cfg->extracts[i].extract.from_hdr.field);
78 			extr->hdr_index =
79 				cfg->extracts[i].extract.from_hdr.hdr_index;
80 			break;
81 		case DPKG_EXTRACT_FROM_DATA:
82 			extr->size = cfg->extracts[i].extract.from_data.size;
83 			extr->offset =
84 				cfg->extracts[i].extract.from_data.offset;
85 			break;
86 		case DPKG_EXTRACT_FROM_PARSE:
87 			extr->size = cfg->extracts[i].extract.from_parse.size;
88 			extr->offset =
89 				cfg->extracts[i].extract.from_parse.offset;
90 			break;
91 		default:
92 			return -EINVAL;
93 		}
94 
95 		extr->num_of_byte_masks = cfg->extracts[i].num_of_byte_masks;
96 		dpkg_set_field(extr->extract_type, EXTRACT_TYPE,
97 			       cfg->extracts[i].type);
98 
99 		for (j = 0; j < DPKG_NUM_OF_MASKS; j++) {
100 			extr->masks[j].mask = cfg->extracts[i].masks[j].mask;
101 			extr->masks[j].offset =
102 				cfg->extracts[i].masks[j].offset;
103 		}
104 	}
105 
106 	return 0;
107 }
108