xref: /OK3568_Linux_fs/kernel/drivers/crypto/cavium/zip/common.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /***********************license start************************************
2*4882a593Smuzhiyun  * Copyright (c) 2003-2017 Cavium, Inc.
3*4882a593Smuzhiyun  * All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * License: one of 'Cavium License' or 'GNU General Public License Version 2'
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This file is provided under the terms of the Cavium License (see below)
8*4882a593Smuzhiyun  * or under the terms of GNU General Public License, Version 2, as
9*4882a593Smuzhiyun  * published by the Free Software Foundation. When using or redistributing
10*4882a593Smuzhiyun  * this file, you may do so under either license.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Cavium License:  Redistribution and use in source and binary forms, with
13*4882a593Smuzhiyun  * or without modification, are permitted provided that the following
14*4882a593Smuzhiyun  * conditions are met:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  * Redistributions of source code must retain the above copyright
17*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *  * Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun  *    copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun  *    disclaimer in the documentation and/or other materials provided
22*4882a593Smuzhiyun  *    with the distribution.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  * Neither the name of Cavium Inc. nor the names of its contributors may be
25*4882a593Smuzhiyun  *    used to endorse or promote products derived from this software without
26*4882a593Smuzhiyun  *    specific prior written permission.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * This Software, including technical data, may be subject to U.S. export
29*4882a593Smuzhiyun  * control laws, including the U.S. Export Administration Act and its
30*4882a593Smuzhiyun  * associated regulations, and may be subject to export or import
31*4882a593Smuzhiyun  * regulations in other countries.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34*4882a593Smuzhiyun  * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35*4882a593Smuzhiyun  * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36*4882a593Smuzhiyun  * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37*4882a593Smuzhiyun  * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38*4882a593Smuzhiyun  * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39*4882a593Smuzhiyun  * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40*4882a593Smuzhiyun  * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41*4882a593Smuzhiyun  * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42*4882a593Smuzhiyun  * ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43*4882a593Smuzhiyun  * WITH YOU.
44*4882a593Smuzhiyun  ***********************license end**************************************/
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #ifndef __COMMON_H__
47*4882a593Smuzhiyun #define __COMMON_H__
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include <linux/delay.h>
50*4882a593Smuzhiyun #include <linux/init.h>
51*4882a593Smuzhiyun #include <linux/interrupt.h>
52*4882a593Smuzhiyun #include <linux/io.h>
53*4882a593Smuzhiyun #include <linux/kernel.h>
54*4882a593Smuzhiyun #include <linux/module.h>
55*4882a593Smuzhiyun #include <linux/pci.h>
56*4882a593Smuzhiyun #include <linux/seq_file.h>
57*4882a593Smuzhiyun #include <linux/string.h>
58*4882a593Smuzhiyun #include <linux/types.h>
59*4882a593Smuzhiyun #include <linux/version.h>
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* Device specific zlib function definitions */
62*4882a593Smuzhiyun #include "zip_device.h"
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /* ZIP device definitions */
65*4882a593Smuzhiyun #include "zip_main.h"
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* ZIP memory allocation/deallocation related definitions */
68*4882a593Smuzhiyun #include "zip_mem.h"
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* Device specific structure definitions */
71*4882a593Smuzhiyun #include "zip_regs.h"
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #define ZIP_ERROR    -1
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #define ZIP_FLUSH_FINISH  4
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #define RAW_FORMAT		0  /* for rawpipe */
78*4882a593Smuzhiyun #define ZLIB_FORMAT		1  /* for zpipe */
79*4882a593Smuzhiyun #define GZIP_FORMAT		2  /* for gzpipe */
80*4882a593Smuzhiyun #define LZS_FORMAT		3  /* for lzspipe */
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* Max number of ZIP devices supported */
83*4882a593Smuzhiyun #define MAX_ZIP_DEVICES		2
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* Configures the number of zip queues to be used */
86*4882a593Smuzhiyun #define ZIP_NUM_QUEUES		2
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun #define DYNAMIC_STOP_EXCESS	1024
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* Maximum buffer sizes in direct mode */
91*4882a593Smuzhiyun #define MAX_INPUT_BUFFER_SIZE   (64 * 1024)
92*4882a593Smuzhiyun #define MAX_OUTPUT_BUFFER_SIZE  (64 * 1024)
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun  * struct zip_operation - common data structure for comp and decomp operations
96*4882a593Smuzhiyun  * @input:               Next input byte is read from here
97*4882a593Smuzhiyun  * @output:              Next output byte written here
98*4882a593Smuzhiyun  * @ctx_addr:            Inflate context buffer address
99*4882a593Smuzhiyun  * @history:             Pointer to the history buffer
100*4882a593Smuzhiyun  * @input_len:           Number of bytes available at next_in
101*4882a593Smuzhiyun  * @input_total_len:     Total number of input bytes read
102*4882a593Smuzhiyun  * @output_len:          Remaining free space at next_out
103*4882a593Smuzhiyun  * @output_total_len:    Total number of bytes output so far
104*4882a593Smuzhiyun  * @csum:                Checksum value of the uncompressed data
105*4882a593Smuzhiyun  * @flush:               Flush flag
106*4882a593Smuzhiyun  * @format:              Format (depends on stream's wrap)
107*4882a593Smuzhiyun  * @speed:               Speed depends on stream's level
108*4882a593Smuzhiyun  * @ccode:               Compression code ( stream's strategy)
109*4882a593Smuzhiyun  * @lzs_flag:            Flag for LZS support
110*4882a593Smuzhiyun  * @begin_file:          Beginning of file indication for inflate
111*4882a593Smuzhiyun  * @history_len:         Size of the history data
112*4882a593Smuzhiyun  * @end_file:            Ending of the file indication for inflate
113*4882a593Smuzhiyun  * @compcode:            Completion status of the ZIP invocation
114*4882a593Smuzhiyun  * @bytes_read:          Input bytes read in current instruction
115*4882a593Smuzhiyun  * @bits_processed:      Total bits processed for entire file
116*4882a593Smuzhiyun  * @sizeofptr:           To distinguish between ILP32 and LP64
117*4882a593Smuzhiyun  * @sizeofzops:          Optional just for padding
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * This structure is used to maintain the required meta data for the
120*4882a593Smuzhiyun  * comp and decomp operations.
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun struct zip_operation {
123*4882a593Smuzhiyun 	u8    *input;
124*4882a593Smuzhiyun 	u8    *output;
125*4882a593Smuzhiyun 	u64   ctx_addr;
126*4882a593Smuzhiyun 	u64   history;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	u32   input_len;
129*4882a593Smuzhiyun 	u32   input_total_len;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	u32   output_len;
132*4882a593Smuzhiyun 	u32   output_total_len;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	u32   csum;
135*4882a593Smuzhiyun 	u32   flush;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	u32   format;
138*4882a593Smuzhiyun 	u32   speed;
139*4882a593Smuzhiyun 	u32   ccode;
140*4882a593Smuzhiyun 	u32   lzs_flag;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	u32   begin_file;
143*4882a593Smuzhiyun 	u32   history_len;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	u32   end_file;
146*4882a593Smuzhiyun 	u32   compcode;
147*4882a593Smuzhiyun 	u32   bytes_read;
148*4882a593Smuzhiyun 	u32   bits_processed;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	u32   sizeofptr;
151*4882a593Smuzhiyun 	u32   sizeofzops;
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun 
zip_poll_result(union zip_zres_s * result)154*4882a593Smuzhiyun static inline int zip_poll_result(union zip_zres_s *result)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	int retries = 1000;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	while (!result->s.compcode) {
159*4882a593Smuzhiyun 		if (!--retries) {
160*4882a593Smuzhiyun 			pr_err("ZIP ERR: request timed out");
161*4882a593Smuzhiyun 			return -ETIMEDOUT;
162*4882a593Smuzhiyun 		}
163*4882a593Smuzhiyun 		udelay(10);
164*4882a593Smuzhiyun 		/*
165*4882a593Smuzhiyun 		 * Force re-reading of compcode which is updated
166*4882a593Smuzhiyun 		 * by the ZIP coprocessor.
167*4882a593Smuzhiyun 		 */
168*4882a593Smuzhiyun 		rmb();
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun 	return 0;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /* error messages */
174*4882a593Smuzhiyun #define zip_err(fmt, args...) pr_err("ZIP ERR:%s():%d: " \
175*4882a593Smuzhiyun 			      fmt "\n", __func__, __LINE__, ## args)
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun #ifdef MSG_ENABLE
178*4882a593Smuzhiyun /* Enable all messages */
179*4882a593Smuzhiyun #define zip_msg(fmt, args...) pr_info("ZIP_MSG:" fmt "\n", ## args)
180*4882a593Smuzhiyun #else
181*4882a593Smuzhiyun #define zip_msg(fmt, args...)
182*4882a593Smuzhiyun #endif
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun #if defined(ZIP_DEBUG_ENABLE) && defined(MSG_ENABLE)
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun #ifdef DEBUG_LEVEL
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun #define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \
189*4882a593Smuzhiyun 	strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun #if DEBUG_LEVEL >= 4
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun #define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \
194*4882a593Smuzhiyun 			      fmt "\n", FILE_NAME, __func__, __LINE__, ## args)
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun #elif DEBUG_LEVEL >= 3
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun #define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \
199*4882a593Smuzhiyun 			      fmt "\n", FILE_NAME, __func__, __LINE__, ## args)
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun #elif DEBUG_LEVEL >= 2
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun #define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s() : %d: " \
204*4882a593Smuzhiyun 			      fmt "\n", __func__, __LINE__, ## args)
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun #else
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun #define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args)
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun #endif /* DEBUG LEVEL >=4 */
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun #else
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun #define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args)
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun #endif /* DEBUG_LEVEL */
217*4882a593Smuzhiyun #else
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun #define zip_dbg(fmt, args...)
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun #endif /* ZIP_DEBUG_ENABLE && MSG_ENABLE*/
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun #endif
224