xref: /openbmc/linux/drivers/crypto/cavium/zip/common.h (revision 1aa33c7a)
1640035a2SMahipal Challa /***********************license start************************************
2640035a2SMahipal Challa  * Copyright (c) 2003-2017 Cavium, Inc.
3640035a2SMahipal Challa  * All rights reserved.
4640035a2SMahipal Challa  *
5640035a2SMahipal Challa  * License: one of 'Cavium License' or 'GNU General Public License Version 2'
6640035a2SMahipal Challa  *
7640035a2SMahipal Challa  * This file is provided under the terms of the Cavium License (see below)
8640035a2SMahipal Challa  * or under the terms of GNU General Public License, Version 2, as
9640035a2SMahipal Challa  * published by the Free Software Foundation. When using or redistributing
10640035a2SMahipal Challa  * this file, you may do so under either license.
11640035a2SMahipal Challa  *
12640035a2SMahipal Challa  * Cavium License:  Redistribution and use in source and binary forms, with
13640035a2SMahipal Challa  * or without modification, are permitted provided that the following
14640035a2SMahipal Challa  * conditions are met:
15640035a2SMahipal Challa  *
16640035a2SMahipal Challa  *  * Redistributions of source code must retain the above copyright
17640035a2SMahipal Challa  *    notice, this list of conditions and the following disclaimer.
18640035a2SMahipal Challa  *
19640035a2SMahipal Challa  *  * Redistributions in binary form must reproduce the above
20640035a2SMahipal Challa  *    copyright notice, this list of conditions and the following
21640035a2SMahipal Challa  *    disclaimer in the documentation and/or other materials provided
22640035a2SMahipal Challa  *    with the distribution.
23640035a2SMahipal Challa  *
24640035a2SMahipal Challa  *  * Neither the name of Cavium Inc. nor the names of its contributors may be
25640035a2SMahipal Challa  *    used to endorse or promote products derived from this software without
26640035a2SMahipal Challa  *    specific prior written permission.
27640035a2SMahipal Challa  *
28640035a2SMahipal Challa  * This Software, including technical data, may be subject to U.S. export
29640035a2SMahipal Challa  * control laws, including the U.S. Export Administration Act and its
30640035a2SMahipal Challa  * associated regulations, and may be subject to export or import
31640035a2SMahipal Challa  * regulations in other countries.
32640035a2SMahipal Challa  *
33640035a2SMahipal Challa  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34640035a2SMahipal Challa  * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35640035a2SMahipal Challa  * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36640035a2SMahipal Challa  * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37640035a2SMahipal Challa  * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38640035a2SMahipal Challa  * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39640035a2SMahipal Challa  * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40640035a2SMahipal Challa  * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41640035a2SMahipal Challa  * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42640035a2SMahipal Challa  * ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43640035a2SMahipal Challa  * WITH YOU.
44640035a2SMahipal Challa  ***********************license end**************************************/
45640035a2SMahipal Challa 
46640035a2SMahipal Challa #ifndef __COMMON_H__
47640035a2SMahipal Challa #define __COMMON_H__
48640035a2SMahipal Challa 
49c782a8c4SJan Glauber #include <linux/delay.h>
50640035a2SMahipal Challa #include <linux/init.h>
51640035a2SMahipal Challa #include <linux/interrupt.h>
52c782a8c4SJan Glauber #include <linux/io.h>
53640035a2SMahipal Challa #include <linux/kernel.h>
54640035a2SMahipal Challa #include <linux/module.h>
55640035a2SMahipal Challa #include <linux/pci.h>
56640035a2SMahipal Challa #include <linux/seq_file.h>
57640035a2SMahipal Challa #include <linux/string.h>
58640035a2SMahipal Challa #include <linux/types.h>
59640035a2SMahipal Challa 
60640035a2SMahipal Challa /* Device specific zlib function definitions */
61640035a2SMahipal Challa #include "zip_device.h"
62640035a2SMahipal Challa 
63640035a2SMahipal Challa /* ZIP device definitions */
64640035a2SMahipal Challa #include "zip_main.h"
65640035a2SMahipal Challa 
66640035a2SMahipal Challa /* ZIP memory allocation/deallocation related definitions */
67640035a2SMahipal Challa #include "zip_mem.h"
68640035a2SMahipal Challa 
69640035a2SMahipal Challa /* Device specific structure definitions */
70640035a2SMahipal Challa #include "zip_regs.h"
71640035a2SMahipal Challa 
72640035a2SMahipal Challa #define ZIP_ERROR    -1
73640035a2SMahipal Challa 
74640035a2SMahipal Challa #define ZIP_FLUSH_FINISH  4
75640035a2SMahipal Challa 
76640035a2SMahipal Challa #define RAW_FORMAT		0  /* for rawpipe */
77640035a2SMahipal Challa #define ZLIB_FORMAT		1  /* for zpipe */
78640035a2SMahipal Challa #define GZIP_FORMAT		2  /* for gzpipe */
79640035a2SMahipal Challa #define LZS_FORMAT		3  /* for lzspipe */
80640035a2SMahipal Challa 
81640035a2SMahipal Challa /* Max number of ZIP devices supported */
82640035a2SMahipal Challa #define MAX_ZIP_DEVICES		2
83640035a2SMahipal Challa 
84640035a2SMahipal Challa /* Configures the number of zip queues to be used */
85640035a2SMahipal Challa #define ZIP_NUM_QUEUES		2
86640035a2SMahipal Challa 
87640035a2SMahipal Challa #define DYNAMIC_STOP_EXCESS	1024
88640035a2SMahipal Challa 
89640035a2SMahipal Challa /* Maximum buffer sizes in direct mode */
90640035a2SMahipal Challa #define MAX_INPUT_BUFFER_SIZE   (64 * 1024)
91640035a2SMahipal Challa #define MAX_OUTPUT_BUFFER_SIZE  (64 * 1024)
92640035a2SMahipal Challa 
93640035a2SMahipal Challa /**
94640035a2SMahipal Challa  * struct zip_operation - common data structure for comp and decomp operations
95640035a2SMahipal Challa  * @input:               Next input byte is read from here
96640035a2SMahipal Challa  * @output:              Next output byte written here
97640035a2SMahipal Challa  * @ctx_addr:            Inflate context buffer address
98640035a2SMahipal Challa  * @history:             Pointer to the history buffer
99640035a2SMahipal Challa  * @input_len:           Number of bytes available at next_in
100640035a2SMahipal Challa  * @input_total_len:     Total number of input bytes read
101640035a2SMahipal Challa  * @output_len:          Remaining free space at next_out
102640035a2SMahipal Challa  * @output_total_len:    Total number of bytes output so far
103640035a2SMahipal Challa  * @csum:                Checksum value of the uncompressed data
104640035a2SMahipal Challa  * @flush:               Flush flag
105640035a2SMahipal Challa  * @format:              Format (depends on stream's wrap)
106640035a2SMahipal Challa  * @speed:               Speed depends on stream's level
107640035a2SMahipal Challa  * @ccode:               Compression code ( stream's strategy)
108640035a2SMahipal Challa  * @lzs_flag:            Flag for LZS support
109640035a2SMahipal Challa  * @begin_file:          Beginning of file indication for inflate
110640035a2SMahipal Challa  * @history_len:         Size of the history data
111640035a2SMahipal Challa  * @end_file:            Ending of the file indication for inflate
112640035a2SMahipal Challa  * @compcode:            Completion status of the ZIP invocation
113640035a2SMahipal Challa  * @bytes_read:          Input bytes read in current instruction
114640035a2SMahipal Challa  * @bits_processed:      Total bits processed for entire file
115640035a2SMahipal Challa  * @sizeofptr:           To distinguish between ILP32 and LP64
116640035a2SMahipal Challa  * @sizeofzops:          Optional just for padding
117640035a2SMahipal Challa  *
118640035a2SMahipal Challa  * This structure is used to maintain the required meta data for the
119640035a2SMahipal Challa  * comp and decomp operations.
120640035a2SMahipal Challa  */
121640035a2SMahipal Challa struct zip_operation {
122640035a2SMahipal Challa 	u8    *input;
123640035a2SMahipal Challa 	u8    *output;
124640035a2SMahipal Challa 	u64   ctx_addr;
125640035a2SMahipal Challa 	u64   history;
126640035a2SMahipal Challa 
127640035a2SMahipal Challa 	u32   input_len;
128640035a2SMahipal Challa 	u32   input_total_len;
129640035a2SMahipal Challa 
130640035a2SMahipal Challa 	u32   output_len;
131640035a2SMahipal Challa 	u32   output_total_len;
132640035a2SMahipal Challa 
133640035a2SMahipal Challa 	u32   csum;
134640035a2SMahipal Challa 	u32   flush;
135640035a2SMahipal Challa 
136640035a2SMahipal Challa 	u32   format;
137640035a2SMahipal Challa 	u32   speed;
138640035a2SMahipal Challa 	u32   ccode;
139640035a2SMahipal Challa 	u32   lzs_flag;
140640035a2SMahipal Challa 
141640035a2SMahipal Challa 	u32   begin_file;
142640035a2SMahipal Challa 	u32   history_len;
143640035a2SMahipal Challa 
144640035a2SMahipal Challa 	u32   end_file;
145640035a2SMahipal Challa 	u32   compcode;
146640035a2SMahipal Challa 	u32   bytes_read;
147640035a2SMahipal Challa 	u32   bits_processed;
148640035a2SMahipal Challa 
149640035a2SMahipal Challa 	u32   sizeofptr;
150640035a2SMahipal Challa 	u32   sizeofzops;
151640035a2SMahipal Challa };
152640035a2SMahipal Challa 
zip_poll_result(union zip_zres_s * result)153c782a8c4SJan Glauber static inline int zip_poll_result(union zip_zres_s *result)
154c782a8c4SJan Glauber {
155c782a8c4SJan Glauber 	int retries = 1000;
156c782a8c4SJan Glauber 
157c782a8c4SJan Glauber 	while (!result->s.compcode) {
158c782a8c4SJan Glauber 		if (!--retries) {
159c782a8c4SJan Glauber 			pr_err("ZIP ERR: request timed out");
160c782a8c4SJan Glauber 			return -ETIMEDOUT;
161c782a8c4SJan Glauber 		}
162c782a8c4SJan Glauber 		udelay(10);
163c782a8c4SJan Glauber 		/*
164c782a8c4SJan Glauber 		 * Force re-reading of compcode which is updated
165c782a8c4SJan Glauber 		 * by the ZIP coprocessor.
166c782a8c4SJan Glauber 		 */
167c782a8c4SJan Glauber 		rmb();
168c782a8c4SJan Glauber 	}
169c782a8c4SJan Glauber 	return 0;
170c782a8c4SJan Glauber }
171c782a8c4SJan Glauber 
172640035a2SMahipal Challa /* error messages */
173640035a2SMahipal Challa #define zip_err(fmt, args...) pr_err("ZIP ERR:%s():%d: " \
174640035a2SMahipal Challa 			      fmt "\n", __func__, __LINE__, ## args)
175640035a2SMahipal Challa 
176640035a2SMahipal Challa #ifdef MSG_ENABLE
177640035a2SMahipal Challa /* Enable all messages */
178640035a2SMahipal Challa #define zip_msg(fmt, args...) pr_info("ZIP_MSG:" fmt "\n", ## args)
179640035a2SMahipal Challa #else
180640035a2SMahipal Challa #define zip_msg(fmt, args...)
181640035a2SMahipal Challa #endif
182640035a2SMahipal Challa 
183640035a2SMahipal Challa #if defined(ZIP_DEBUG_ENABLE) && defined(MSG_ENABLE)
184640035a2SMahipal Challa 
185640035a2SMahipal Challa #ifdef DEBUG_LEVEL
186640035a2SMahipal Challa 
187640035a2SMahipal Challa #define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \
188640035a2SMahipal Challa 	strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
189640035a2SMahipal Challa 
190640035a2SMahipal Challa #if DEBUG_LEVEL >= 4
191640035a2SMahipal Challa 
192640035a2SMahipal Challa #define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \
193640035a2SMahipal Challa 			      fmt "\n", FILE_NAME, __func__, __LINE__, ## args)
194640035a2SMahipal Challa 
195640035a2SMahipal Challa #elif DEBUG_LEVEL >= 3
196640035a2SMahipal Challa 
197640035a2SMahipal Challa #define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \
198640035a2SMahipal Challa 			      fmt "\n", FILE_NAME, __func__, __LINE__, ## args)
199640035a2SMahipal Challa 
200640035a2SMahipal Challa #elif DEBUG_LEVEL >= 2
201640035a2SMahipal Challa 
202640035a2SMahipal Challa #define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s() : %d: " \
203640035a2SMahipal Challa 			      fmt "\n", __func__, __LINE__, ## args)
204640035a2SMahipal Challa 
205640035a2SMahipal Challa #else
206640035a2SMahipal Challa 
207640035a2SMahipal Challa #define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args)
208640035a2SMahipal Challa 
209640035a2SMahipal Challa #endif /* DEBUG LEVEL >=4 */
210640035a2SMahipal Challa 
211640035a2SMahipal Challa #else
212640035a2SMahipal Challa 
213640035a2SMahipal Challa #define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args)
214640035a2SMahipal Challa 
215640035a2SMahipal Challa #endif /* DEBUG_LEVEL */
216640035a2SMahipal Challa #else
217640035a2SMahipal Challa 
218640035a2SMahipal Challa #define zip_dbg(fmt, args...)
219640035a2SMahipal Challa 
220640035a2SMahipal Challa #endif /* ZIP_DEBUG_ENABLE && MSG_ENABLE*/
221640035a2SMahipal Challa 
222640035a2SMahipal Challa #endif
223