zip_inflate.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /***********************license start************************************
  2. * Copyright (c) 2003-2017 Cavium, Inc.
  3. * All rights reserved.
  4. *
  5. * License: one of 'Cavium License' or 'GNU General Public License Version 2'
  6. *
  7. * This file is provided under the terms of the Cavium License (see below)
  8. * or under the terms of GNU General Public License, Version 2, as
  9. * published by the Free Software Foundation. When using or redistributing
  10. * this file, you may do so under either license.
  11. *
  12. * Cavium License: Redistribution and use in source and binary forms, with
  13. * or without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * * Neither the name of Cavium Inc. nor the names of its contributors may be
  25. * used to endorse or promote products derived from this software without
  26. * specific prior written permission.
  27. *
  28. * This Software, including technical data, may be subject to U.S. export
  29. * control laws, including the U.S. Export Administration Act and its
  30. * associated regulations, and may be subject to export or import
  31. * regulations in other countries.
  32. *
  33. * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
  34. * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
  35. * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
  36. * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
  37. * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
  38. * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
  39. * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
  40. * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
  41. * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
  42. * ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
  43. * WITH YOU.
  44. ***********************license end**************************************/
  45. #include <linux/delay.h>
  46. #include <linux/sched.h>
  47. #include "common.h"
  48. #include "zip_inflate.h"
  49. static int prepare_inflate_zcmd(struct zip_operation *zip_ops,
  50. struct zip_state *s, union zip_inst_s *zip_cmd)
  51. {
  52. union zip_zres_s *result_ptr = &s->result;
  53. memset(zip_cmd, 0, sizeof(s->zip_cmd));
  54. memset(result_ptr, 0, sizeof(s->result));
  55. /* IWORD#0 */
  56. /* Decompression History Gather list - no gather list */
  57. zip_cmd->s.hg = 0;
  58. /* For decompression, CE must be 0x0. */
  59. zip_cmd->s.ce = 0;
  60. /* For decompression, SS must be 0x0. */
  61. zip_cmd->s.ss = 0;
  62. /* For decompression, SF should always be set. */
  63. zip_cmd->s.sf = 1;
  64. /* Begin File */
  65. if (zip_ops->begin_file == 0)
  66. zip_cmd->s.bf = 0;
  67. else
  68. zip_cmd->s.bf = 1;
  69. zip_cmd->s.ef = 1;
  70. /* 0: for Deflate decompression, 3: for LZS decompression */
  71. zip_cmd->s.cc = zip_ops->ccode;
  72. /* IWORD #1*/
  73. /* adler checksum */
  74. zip_cmd->s.adlercrc32 = zip_ops->csum;
  75. /*
  76. * HISTORYLENGTH must be 0x0 for any ZIP decompress operation.
  77. * History data is added to a decompression operation via IWORD3.
  78. */
  79. zip_cmd->s.historylength = 0;
  80. zip_cmd->s.ds = 0;
  81. /* IWORD # 8 and 9 - Output pointer */
  82. zip_cmd->s.out_ptr_addr.s.addr = __pa(zip_ops->output);
  83. zip_cmd->s.out_ptr_ctl.s.length = zip_ops->output_len;
  84. /* Maximum number of output-stream bytes that can be written */
  85. zip_cmd->s.totaloutputlength = zip_ops->output_len;
  86. zip_dbg("Data Direct Input case ");
  87. /* IWORD # 6 and 7 - input pointer */
  88. zip_cmd->s.dg = 0;
  89. zip_cmd->s.inp_ptr_addr.s.addr = __pa((u8 *)zip_ops->input);
  90. zip_cmd->s.inp_ptr_ctl.s.length = zip_ops->input_len;
  91. /* IWORD # 10 and 11 - Result pointer */
  92. zip_cmd->s.res_ptr_addr.s.addr = __pa(result_ptr);
  93. /* Clearing completion code */
  94. result_ptr->s.compcode = 0;
  95. /* Returning 0 for time being.*/
  96. return 0;
  97. }
  98. /**
  99. * zip_inflate - API to offload inflate operation to hardware
  100. * @zip_ops: Pointer to zip operation structure
  101. * @s: Pointer to the structure representing zip state
  102. * @zip_dev: Pointer to zip device structure
  103. *
  104. * This function prepares the zip inflate command and submits it to the zip
  105. * engine for processing.
  106. *
  107. * Return: 0 if successful or error code
  108. */
  109. int zip_inflate(struct zip_operation *zip_ops, struct zip_state *s,
  110. struct zip_device *zip_dev)
  111. {
  112. union zip_inst_s *zip_cmd = &s->zip_cmd;
  113. union zip_zres_s *result_ptr = &s->result;
  114. u32 queue;
  115. /* Prepare inflate zip command */
  116. prepare_inflate_zcmd(zip_ops, s, zip_cmd);
  117. atomic64_add(zip_ops->input_len, &zip_dev->stats.decomp_in_bytes);
  118. /* Load inflate command to zip queue and ring the doorbell */
  119. queue = zip_load_instr(zip_cmd, zip_dev);
  120. /* Decompression requests submitted stats update */
  121. atomic64_inc(&zip_dev->stats.decomp_req_submit);
  122. /* Wait for completion or error */
  123. zip_poll_result(result_ptr);
  124. /* Decompression requests completed stats update */
  125. atomic64_inc(&zip_dev->stats.decomp_req_complete);
  126. zip_ops->compcode = result_ptr->s.compcode;
  127. switch (zip_ops->compcode) {
  128. case ZIP_CMD_NOTDONE:
  129. zip_dbg("Zip Instruction not yet completed\n");
  130. return ZIP_ERROR;
  131. case ZIP_CMD_SUCCESS:
  132. zip_dbg("Zip Instruction completed successfully\n");
  133. break;
  134. case ZIP_CMD_DYNAMIC_STOP:
  135. zip_dbg(" Dynamic stop Initiated\n");
  136. break;
  137. default:
  138. zip_dbg("Instruction failed. Code = %d\n", zip_ops->compcode);
  139. atomic64_inc(&zip_dev->stats.decomp_bad_reqs);
  140. zip_update_cmd_bufs(zip_dev, queue);
  141. return ZIP_ERROR;
  142. }
  143. zip_update_cmd_bufs(zip_dev, queue);
  144. if ((zip_ops->ccode == 3) && (zip_ops->flush == 4) &&
  145. (zip_ops->compcode != ZIP_CMD_DYNAMIC_STOP))
  146. result_ptr->s.ef = 1;
  147. zip_ops->csum = result_ptr->s.adler32;
  148. atomic64_add(result_ptr->s.totalbyteswritten,
  149. &zip_dev->stats.decomp_out_bytes);
  150. if (zip_ops->output_len < result_ptr->s.totalbyteswritten) {
  151. zip_err("output_len (%d) < total bytes written (%d)\n",
  152. zip_ops->output_len, result_ptr->s.totalbyteswritten);
  153. zip_ops->output_len = 0;
  154. } else {
  155. zip_ops->output_len = result_ptr->s.totalbyteswritten;
  156. }
  157. zip_ops->bytes_read = result_ptr->s.totalbytesread;
  158. zip_ops->bits_processed = result_ptr->s.totalbitsprocessed;
  159. zip_ops->end_file = result_ptr->s.ef;
  160. if (zip_ops->end_file) {
  161. switch (zip_ops->format) {
  162. case RAW_FORMAT:
  163. zip_dbg("RAW Format: %d ", zip_ops->format);
  164. /* Get checksum from engine */
  165. zip_ops->csum = result_ptr->s.adler32;
  166. break;
  167. case ZLIB_FORMAT:
  168. zip_dbg("ZLIB Format: %d ", zip_ops->format);
  169. zip_ops->csum = result_ptr->s.adler32;
  170. break;
  171. case GZIP_FORMAT:
  172. zip_dbg("GZIP Format: %d ", zip_ops->format);
  173. zip_ops->csum = result_ptr->s.crc32;
  174. break;
  175. case LZS_FORMAT:
  176. zip_dbg("LZS Format: %d ", zip_ops->format);
  177. break;
  178. default:
  179. zip_err("Format error:%d\n", zip_ops->format);
  180. }
  181. }
  182. return 0;
  183. }