162306a36Sopenharmony_ci/***********************license start************************************ 262306a36Sopenharmony_ci * Copyright (c) 2003-2017 Cavium, Inc. 362306a36Sopenharmony_ci * All rights reserved. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * License: one of 'Cavium License' or 'GNU General Public License Version 2' 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * This file is provided under the terms of the Cavium License (see below) 862306a36Sopenharmony_ci * or under the terms of GNU General Public License, Version 2, as 962306a36Sopenharmony_ci * published by the Free Software Foundation. When using or redistributing 1062306a36Sopenharmony_ci * this file, you may do so under either license. 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * Cavium License: Redistribution and use in source and binary forms, with 1362306a36Sopenharmony_ci * or without modification, are permitted provided that the following 1462306a36Sopenharmony_ci * conditions are met: 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * * Redistributions of source code must retain the above copyright 1762306a36Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * * Redistributions in binary form must reproduce the above 2062306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 2162306a36Sopenharmony_ci * disclaimer in the documentation and/or other materials provided 2262306a36Sopenharmony_ci * with the distribution. 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * * Neither the name of Cavium Inc. nor the names of its contributors may be 2562306a36Sopenharmony_ci * used to endorse or promote products derived from this software without 2662306a36Sopenharmony_ci * specific prior written permission. 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * This Software, including technical data, may be subject to U.S. export 2962306a36Sopenharmony_ci * control laws, including the U.S. Export Administration Act and its 3062306a36Sopenharmony_ci * associated regulations, and may be subject to export or import 3162306a36Sopenharmony_ci * regulations in other countries. 3262306a36Sopenharmony_ci * 3362306a36Sopenharmony_ci * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 3462306a36Sopenharmony_ci * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS 3562306a36Sopenharmony_ci * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH 3662306a36Sopenharmony_ci * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY 3762306a36Sopenharmony_ci * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT 3862306a36Sopenharmony_ci * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) 3962306a36Sopenharmony_ci * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A 4062306a36Sopenharmony_ci * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET 4162306a36Sopenharmony_ci * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE 4262306a36Sopenharmony_ci * ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES 4362306a36Sopenharmony_ci * WITH YOU. 4462306a36Sopenharmony_ci ***********************license end**************************************/ 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci#ifndef __COMMON_H__ 4762306a36Sopenharmony_ci#define __COMMON_H__ 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci#include <linux/delay.h> 5062306a36Sopenharmony_ci#include <linux/init.h> 5162306a36Sopenharmony_ci#include <linux/interrupt.h> 5262306a36Sopenharmony_ci#include <linux/io.h> 5362306a36Sopenharmony_ci#include <linux/kernel.h> 5462306a36Sopenharmony_ci#include <linux/module.h> 5562306a36Sopenharmony_ci#include <linux/pci.h> 5662306a36Sopenharmony_ci#include <linux/seq_file.h> 5762306a36Sopenharmony_ci#include <linux/string.h> 5862306a36Sopenharmony_ci#include <linux/types.h> 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci/* Device specific zlib function definitions */ 6162306a36Sopenharmony_ci#include "zip_device.h" 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci/* ZIP device definitions */ 6462306a36Sopenharmony_ci#include "zip_main.h" 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci/* ZIP memory allocation/deallocation related definitions */ 6762306a36Sopenharmony_ci#include "zip_mem.h" 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci/* Device specific structure definitions */ 7062306a36Sopenharmony_ci#include "zip_regs.h" 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci#define ZIP_ERROR -1 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci#define ZIP_FLUSH_FINISH 4 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci#define RAW_FORMAT 0 /* for rawpipe */ 7762306a36Sopenharmony_ci#define ZLIB_FORMAT 1 /* for zpipe */ 7862306a36Sopenharmony_ci#define GZIP_FORMAT 2 /* for gzpipe */ 7962306a36Sopenharmony_ci#define LZS_FORMAT 3 /* for lzspipe */ 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci/* Max number of ZIP devices supported */ 8262306a36Sopenharmony_ci#define MAX_ZIP_DEVICES 2 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/* Configures the number of zip queues to be used */ 8562306a36Sopenharmony_ci#define ZIP_NUM_QUEUES 2 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci#define DYNAMIC_STOP_EXCESS 1024 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/* Maximum buffer sizes in direct mode */ 9062306a36Sopenharmony_ci#define MAX_INPUT_BUFFER_SIZE (64 * 1024) 9162306a36Sopenharmony_ci#define MAX_OUTPUT_BUFFER_SIZE (64 * 1024) 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci/** 9462306a36Sopenharmony_ci * struct zip_operation - common data structure for comp and decomp operations 9562306a36Sopenharmony_ci * @input: Next input byte is read from here 9662306a36Sopenharmony_ci * @output: Next output byte written here 9762306a36Sopenharmony_ci * @ctx_addr: Inflate context buffer address 9862306a36Sopenharmony_ci * @history: Pointer to the history buffer 9962306a36Sopenharmony_ci * @input_len: Number of bytes available at next_in 10062306a36Sopenharmony_ci * @input_total_len: Total number of input bytes read 10162306a36Sopenharmony_ci * @output_len: Remaining free space at next_out 10262306a36Sopenharmony_ci * @output_total_len: Total number of bytes output so far 10362306a36Sopenharmony_ci * @csum: Checksum value of the uncompressed data 10462306a36Sopenharmony_ci * @flush: Flush flag 10562306a36Sopenharmony_ci * @format: Format (depends on stream's wrap) 10662306a36Sopenharmony_ci * @speed: Speed depends on stream's level 10762306a36Sopenharmony_ci * @ccode: Compression code ( stream's strategy) 10862306a36Sopenharmony_ci * @lzs_flag: Flag for LZS support 10962306a36Sopenharmony_ci * @begin_file: Beginning of file indication for inflate 11062306a36Sopenharmony_ci * @history_len: Size of the history data 11162306a36Sopenharmony_ci * @end_file: Ending of the file indication for inflate 11262306a36Sopenharmony_ci * @compcode: Completion status of the ZIP invocation 11362306a36Sopenharmony_ci * @bytes_read: Input bytes read in current instruction 11462306a36Sopenharmony_ci * @bits_processed: Total bits processed for entire file 11562306a36Sopenharmony_ci * @sizeofptr: To distinguish between ILP32 and LP64 11662306a36Sopenharmony_ci * @sizeofzops: Optional just for padding 11762306a36Sopenharmony_ci * 11862306a36Sopenharmony_ci * This structure is used to maintain the required meta data for the 11962306a36Sopenharmony_ci * comp and decomp operations. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_cistruct zip_operation { 12262306a36Sopenharmony_ci u8 *input; 12362306a36Sopenharmony_ci u8 *output; 12462306a36Sopenharmony_ci u64 ctx_addr; 12562306a36Sopenharmony_ci u64 history; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci u32 input_len; 12862306a36Sopenharmony_ci u32 input_total_len; 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci u32 output_len; 13162306a36Sopenharmony_ci u32 output_total_len; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci u32 csum; 13462306a36Sopenharmony_ci u32 flush; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci u32 format; 13762306a36Sopenharmony_ci u32 speed; 13862306a36Sopenharmony_ci u32 ccode; 13962306a36Sopenharmony_ci u32 lzs_flag; 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci u32 begin_file; 14262306a36Sopenharmony_ci u32 history_len; 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci u32 end_file; 14562306a36Sopenharmony_ci u32 compcode; 14662306a36Sopenharmony_ci u32 bytes_read; 14762306a36Sopenharmony_ci u32 bits_processed; 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci u32 sizeofptr; 15062306a36Sopenharmony_ci u32 sizeofzops; 15162306a36Sopenharmony_ci}; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_cistatic inline int zip_poll_result(union zip_zres_s *result) 15462306a36Sopenharmony_ci{ 15562306a36Sopenharmony_ci int retries = 1000; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci while (!result->s.compcode) { 15862306a36Sopenharmony_ci if (!--retries) { 15962306a36Sopenharmony_ci pr_err("ZIP ERR: request timed out"); 16062306a36Sopenharmony_ci return -ETIMEDOUT; 16162306a36Sopenharmony_ci } 16262306a36Sopenharmony_ci udelay(10); 16362306a36Sopenharmony_ci /* 16462306a36Sopenharmony_ci * Force re-reading of compcode which is updated 16562306a36Sopenharmony_ci * by the ZIP coprocessor. 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ci rmb(); 16862306a36Sopenharmony_ci } 16962306a36Sopenharmony_ci return 0; 17062306a36Sopenharmony_ci} 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci/* error messages */ 17362306a36Sopenharmony_ci#define zip_err(fmt, args...) pr_err("ZIP ERR:%s():%d: " \ 17462306a36Sopenharmony_ci fmt "\n", __func__, __LINE__, ## args) 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci#ifdef MSG_ENABLE 17762306a36Sopenharmony_ci/* Enable all messages */ 17862306a36Sopenharmony_ci#define zip_msg(fmt, args...) pr_info("ZIP_MSG:" fmt "\n", ## args) 17962306a36Sopenharmony_ci#else 18062306a36Sopenharmony_ci#define zip_msg(fmt, args...) 18162306a36Sopenharmony_ci#endif 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci#if defined(ZIP_DEBUG_ENABLE) && defined(MSG_ENABLE) 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci#ifdef DEBUG_LEVEL 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci#define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \ 18862306a36Sopenharmony_ci strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci#if DEBUG_LEVEL >= 4 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci#define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \ 19362306a36Sopenharmony_ci fmt "\n", FILE_NAME, __func__, __LINE__, ## args) 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci#elif DEBUG_LEVEL >= 3 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci#define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \ 19862306a36Sopenharmony_ci fmt "\n", FILE_NAME, __func__, __LINE__, ## args) 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci#elif DEBUG_LEVEL >= 2 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci#define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s() : %d: " \ 20362306a36Sopenharmony_ci fmt "\n", __func__, __LINE__, ## args) 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci#else 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci#define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args) 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci#endif /* DEBUG LEVEL >=4 */ 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci#else 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci#define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args) 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci#endif /* DEBUG_LEVEL */ 21662306a36Sopenharmony_ci#else 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci#define zip_dbg(fmt, args...) 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci#endif /* ZIP_DEBUG_ENABLE && MSG_ENABLE*/ 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci#endif 223