zip_deflate.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_deflate.h"
  49. /* Prepares the deflate zip command */
  50. static int prepare_zip_command(struct zip_operation *zip_ops,
  51. struct zip_state *s, union zip_inst_s *zip_cmd)
  52. {
  53. union zip_zres_s *result_ptr = &s->result;
  54. memset(zip_cmd, 0, sizeof(s->zip_cmd));
  55. memset(result_ptr, 0, sizeof(s->result));
  56. /* IWORD #0 */
  57. /* History gather */
  58. zip_cmd->s.hg = 0;
  59. /* compression enable = 1 for deflate */
  60. zip_cmd->s.ce = 1;
  61. /* sf (sync flush) */
  62. zip_cmd->s.sf = 1;
  63. /* ef (end of file) */
  64. if (zip_ops->flush == ZIP_FLUSH_FINISH) {
  65. zip_cmd->s.ef = 1;
  66. zip_cmd->s.sf = 0;
  67. }
  68. zip_cmd->s.cc = zip_ops->ccode;
  69. /* ss (compression speed/storage) */
  70. zip_cmd->s.ss = zip_ops->speed;
  71. /* IWORD #1 */
  72. /* adler checksum */
  73. zip_cmd->s.adlercrc32 = zip_ops->csum;
  74. zip_cmd->s.historylength = zip_ops->history_len;
  75. zip_cmd->s.dg = 0;
  76. /* IWORD # 6 and 7 - compression input/history pointer */
  77. zip_cmd->s.inp_ptr_addr.s.addr = __pa(zip_ops->input);
  78. zip_cmd->s.inp_ptr_ctl.s.length = (zip_ops->input_len +
  79. zip_ops->history_len);
  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. /* IWORD # 10 and 11 - Result pointer */
  87. zip_cmd->s.res_ptr_addr.s.addr = __pa(result_ptr);
  88. /* Clearing completion code */
  89. result_ptr->s.compcode = 0;
  90. return 0;
  91. }
  92. /**
  93. * zip_deflate - API to offload deflate operation to hardware
  94. * @zip_ops: Pointer to zip operation structure
  95. * @s: Pointer to the structure representing zip state
  96. * @zip_dev: Pointer to zip device structure
  97. *
  98. * This function prepares the zip deflate command and submits it to the zip
  99. * engine for processing.
  100. *
  101. * Return: 0 if successful or error code
  102. */
  103. int zip_deflate(struct zip_operation *zip_ops, struct zip_state *s,
  104. struct zip_device *zip_dev)
  105. {
  106. union zip_inst_s *zip_cmd = &s->zip_cmd;
  107. union zip_zres_s *result_ptr = &s->result;
  108. u32 queue;
  109. /* Prepares zip command based on the input parameters */
  110. prepare_zip_command(zip_ops, s, zip_cmd);
  111. atomic64_add(zip_ops->input_len, &zip_dev->stats.comp_in_bytes);
  112. /* Loads zip command into command queues and rings door bell */
  113. queue = zip_load_instr(zip_cmd, zip_dev);
  114. /* Stats update for compression requests submitted */
  115. atomic64_inc(&zip_dev->stats.comp_req_submit);
  116. /* Wait for completion or error */
  117. zip_poll_result(result_ptr);
  118. /* Stats update for compression requests completed */
  119. atomic64_inc(&zip_dev->stats.comp_req_complete);
  120. zip_ops->compcode = result_ptr->s.compcode;
  121. switch (zip_ops->compcode) {
  122. case ZIP_CMD_NOTDONE:
  123. zip_dbg("Zip instruction not yet completed");
  124. return ZIP_ERROR;
  125. case ZIP_CMD_SUCCESS:
  126. zip_dbg("Zip instruction completed successfully");
  127. zip_update_cmd_bufs(zip_dev, queue);
  128. break;
  129. case ZIP_CMD_DTRUNC:
  130. zip_dbg("Output Truncate error");
  131. /* Returning ZIP_ERROR to avoid copy to user */
  132. return ZIP_ERROR;
  133. default:
  134. zip_err("Zip instruction failed. Code:%d", zip_ops->compcode);
  135. return ZIP_ERROR;
  136. }
  137. /* Update the CRC depending on the format */
  138. switch (zip_ops->format) {
  139. case RAW_FORMAT:
  140. zip_dbg("RAW Format: %d ", zip_ops->format);
  141. /* Get checksum from engine, need to feed it again */
  142. zip_ops->csum = result_ptr->s.adler32;
  143. break;
  144. case ZLIB_FORMAT:
  145. zip_dbg("ZLIB Format: %d ", zip_ops->format);
  146. zip_ops->csum = result_ptr->s.adler32;
  147. break;
  148. case GZIP_FORMAT:
  149. zip_dbg("GZIP Format: %d ", zip_ops->format);
  150. zip_ops->csum = result_ptr->s.crc32;
  151. break;
  152. case LZS_FORMAT:
  153. zip_dbg("LZS Format: %d ", zip_ops->format);
  154. break;
  155. default:
  156. zip_err("Unknown Format:%d\n", zip_ops->format);
  157. }
  158. atomic64_add(result_ptr->s.totalbyteswritten,
  159. &zip_dev->stats.comp_out_bytes);
  160. /* Update output_len */
  161. if (zip_ops->output_len < result_ptr->s.totalbyteswritten) {
  162. /* Dynamic stop && strm->output_len < zipconstants[onfsize] */
  163. zip_err("output_len (%d) < total bytes written(%d)\n",
  164. zip_ops->output_len, result_ptr->s.totalbyteswritten);
  165. zip_ops->output_len = 0;
  166. } else {
  167. zip_ops->output_len = result_ptr->s.totalbyteswritten;
  168. }
  169. return 0;
  170. }