unsolicited_frame_control.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include "host.h"
  56. #include "unsolicited_frame_control.h"
  57. #include "registers.h"
  58. int scic_sds_unsolicited_frame_control_construct(struct scic_sds_controller *scic)
  59. {
  60. struct scic_sds_unsolicited_frame_control *uf_control = &scic->uf_control;
  61. struct scic_sds_unsolicited_frame *uf;
  62. u32 buf_len, header_len, i;
  63. dma_addr_t dma;
  64. size_t size;
  65. void *virt;
  66. /*
  67. * Prepare all of the memory sizes for the UF headers, UF address
  68. * table, and UF buffers themselves.
  69. */
  70. buf_len = SCU_MAX_UNSOLICITED_FRAMES * SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
  71. header_len = SCU_MAX_UNSOLICITED_FRAMES * sizeof(struct scu_unsolicited_frame_header);
  72. size = buf_len + header_len + SCU_MAX_UNSOLICITED_FRAMES * sizeof(dma_addr_t);
  73. /*
  74. * The Unsolicited Frame buffers are set at the start of the UF
  75. * memory descriptor entry. The headers and address table will be
  76. * placed after the buffers.
  77. */
  78. virt = dmam_alloc_coherent(scic_to_dev(scic), size, &dma, GFP_KERNEL);
  79. if (!virt)
  80. return -ENOMEM;
  81. /*
  82. * Program the location of the UF header table into the SCU.
  83. * Notes:
  84. * - The address must align on a 64-byte boundary. Guaranteed to be
  85. * on 64-byte boundary already 1KB boundary for unsolicited frames.
  86. * - Program unused header entries to overlap with the last
  87. * unsolicited frame. The silicon will never DMA to these unused
  88. * headers, since we program the UF address table pointers to
  89. * NULL.
  90. */
  91. uf_control->headers.physical_address = dma + buf_len;
  92. uf_control->headers.array = virt + buf_len;
  93. /*
  94. * Program the location of the UF address table into the SCU.
  95. * Notes:
  96. * - The address must align on a 64-bit boundary. Guaranteed to be on 64
  97. * byte boundary already due to above programming headers being on a
  98. * 64-bit boundary and headers are on a 64-bytes in size.
  99. */
  100. uf_control->address_table.physical_address = dma + buf_len + header_len;
  101. uf_control->address_table.array = virt + buf_len + header_len;
  102. uf_control->get = 0;
  103. /*
  104. * UF buffer requirements are:
  105. * - The last entry in the UF queue is not NULL.
  106. * - There is a power of 2 number of entries (NULL or not-NULL)
  107. * programmed into the queue.
  108. * - Aligned on a 1KB boundary. */
  109. /*
  110. * Program the actual used UF buffers into the UF address table and
  111. * the controller's array of UFs.
  112. */
  113. for (i = 0; i < SCU_MAX_UNSOLICITED_FRAMES; i++) {
  114. uf = &uf_control->buffers.array[i];
  115. uf_control->address_table.array[i] = dma;
  116. uf->buffer = virt;
  117. uf->header = &uf_control->headers.array[i];
  118. uf->state = UNSOLICITED_FRAME_EMPTY;
  119. /*
  120. * Increment the address of the physical and virtual memory
  121. * pointers. Everything is aligned on 1k boundary with an
  122. * increment of 1k.
  123. */
  124. virt += SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
  125. dma += SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
  126. }
  127. return 0;
  128. }
  129. /**
  130. * This method returns the frame header for the specified frame index.
  131. * @uf_control:
  132. * @frame_index:
  133. * @frame_header:
  134. *
  135. * enum sci_status
  136. */
  137. enum sci_status scic_sds_unsolicited_frame_control_get_header(
  138. struct scic_sds_unsolicited_frame_control *uf_control,
  139. u32 frame_index,
  140. void **frame_header)
  141. {
  142. if (frame_index < SCU_MAX_UNSOLICITED_FRAMES) {
  143. /*
  144. * Skip the first word in the frame since this is a controll word used
  145. * by the hardware. */
  146. *frame_header = &uf_control->buffers.array[frame_index].header->data;
  147. return SCI_SUCCESS;
  148. }
  149. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  150. }
  151. /**
  152. * This method returns the frame buffer for the specified frame index.
  153. * @uf_control:
  154. * @frame_index:
  155. * @frame_buffer:
  156. *
  157. * enum sci_status
  158. */
  159. enum sci_status scic_sds_unsolicited_frame_control_get_buffer(
  160. struct scic_sds_unsolicited_frame_control *uf_control,
  161. u32 frame_index,
  162. void **frame_buffer)
  163. {
  164. if (frame_index < SCU_MAX_UNSOLICITED_FRAMES) {
  165. *frame_buffer = uf_control->buffers.array[frame_index].buffer;
  166. return SCI_SUCCESS;
  167. }
  168. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  169. }
  170. /**
  171. * This method releases the frame once this is done the frame is available for
  172. * re-use by the hardware. The data contained in the frame header and frame
  173. * buffer is no longer valid.
  174. * @uf_control: This parameter specifies the UF control object
  175. * @frame_index: This parameter specifies the frame index to attempt to release.
  176. *
  177. * This method returns an indication to the caller as to whether the
  178. * unsolicited frame get pointer should be updated.
  179. */
  180. bool scic_sds_unsolicited_frame_control_release_frame(
  181. struct scic_sds_unsolicited_frame_control *uf_control,
  182. u32 frame_index)
  183. {
  184. u32 frame_get;
  185. u32 frame_cycle;
  186. frame_get = uf_control->get & (SCU_MAX_UNSOLICITED_FRAMES - 1);
  187. frame_cycle = uf_control->get & SCU_MAX_UNSOLICITED_FRAMES;
  188. /*
  189. * In the event there are NULL entries in the UF table, we need to
  190. * advance the get pointer in order to find out if this frame should
  191. * be released (i.e. update the get pointer)
  192. */
  193. while (lower_32_bits(uf_control->address_table.array[frame_get]) == 0 &&
  194. upper_32_bits(uf_control->address_table.array[frame_get]) == 0 &&
  195. frame_get < SCU_MAX_UNSOLICITED_FRAMES)
  196. frame_get++;
  197. /*
  198. * The table has a NULL entry as it's last element. This is
  199. * illegal.
  200. */
  201. BUG_ON(frame_get >= SCU_MAX_UNSOLICITED_FRAMES);
  202. if (frame_index >= SCU_MAX_UNSOLICITED_FRAMES)
  203. return false;
  204. uf_control->buffers.array[frame_index].state = UNSOLICITED_FRAME_RELEASED;
  205. if (frame_get != frame_index) {
  206. /*
  207. * Frames remain in use until we advance the get pointer
  208. * so there is nothing we can do here
  209. */
  210. return false;
  211. }
  212. /*
  213. * The frame index is equal to the current get pointer so we
  214. * can now free up all of the frame entries that
  215. */
  216. while (uf_control->buffers.array[frame_get].state == UNSOLICITED_FRAME_RELEASED) {
  217. uf_control->buffers.array[frame_get].state = UNSOLICITED_FRAME_EMPTY;
  218. if (frame_get+1 == SCU_MAX_UNSOLICITED_FRAMES-1) {
  219. frame_cycle ^= SCU_MAX_UNSOLICITED_FRAMES;
  220. frame_get = 0;
  221. } else
  222. frame_get++;
  223. }
  224. uf_control->get = SCU_UFQGP_GEN_BIT(ENABLE_BIT) | frame_cycle | frame_get;
  225. return true;
  226. }