unsolicited_frame_control.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 sci_unsolicited_frame_control_construct(struct isci_host *ihost)
  59. {
  60. struct sci_unsolicited_frame_control *uf_control = &ihost->uf_control;
  61. struct sci_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(uf_control->address_table.array[0]);
  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(&ihost->pdev->dev, 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. enum sci_status sci_unsolicited_frame_control_get_header(struct sci_unsolicited_frame_control *uf_control,
  130. u32 frame_index,
  131. void **frame_header)
  132. {
  133. if (frame_index < SCU_MAX_UNSOLICITED_FRAMES) {
  134. /* Skip the first word in the frame since this is a controll word used
  135. * by the hardware.
  136. */
  137. *frame_header = &uf_control->buffers.array[frame_index].header->data;
  138. return SCI_SUCCESS;
  139. }
  140. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  141. }
  142. enum sci_status sci_unsolicited_frame_control_get_buffer(struct sci_unsolicited_frame_control *uf_control,
  143. u32 frame_index,
  144. void **frame_buffer)
  145. {
  146. if (frame_index < SCU_MAX_UNSOLICITED_FRAMES) {
  147. *frame_buffer = uf_control->buffers.array[frame_index].buffer;
  148. return SCI_SUCCESS;
  149. }
  150. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  151. }
  152. bool sci_unsolicited_frame_control_release_frame(struct sci_unsolicited_frame_control *uf_control,
  153. u32 frame_index)
  154. {
  155. u32 frame_get;
  156. u32 frame_cycle;
  157. frame_get = uf_control->get & (SCU_MAX_UNSOLICITED_FRAMES - 1);
  158. frame_cycle = uf_control->get & SCU_MAX_UNSOLICITED_FRAMES;
  159. /*
  160. * In the event there are NULL entries in the UF table, we need to
  161. * advance the get pointer in order to find out if this frame should
  162. * be released (i.e. update the get pointer)
  163. */
  164. while (lower_32_bits(uf_control->address_table.array[frame_get]) == 0 &&
  165. upper_32_bits(uf_control->address_table.array[frame_get]) == 0 &&
  166. frame_get < SCU_MAX_UNSOLICITED_FRAMES)
  167. frame_get++;
  168. /*
  169. * The table has a NULL entry as it's last element. This is
  170. * illegal.
  171. */
  172. BUG_ON(frame_get >= SCU_MAX_UNSOLICITED_FRAMES);
  173. if (frame_index >= SCU_MAX_UNSOLICITED_FRAMES)
  174. return false;
  175. uf_control->buffers.array[frame_index].state = UNSOLICITED_FRAME_RELEASED;
  176. if (frame_get != frame_index) {
  177. /*
  178. * Frames remain in use until we advance the get pointer
  179. * so there is nothing we can do here
  180. */
  181. return false;
  182. }
  183. /*
  184. * The frame index is equal to the current get pointer so we
  185. * can now free up all of the frame entries that
  186. */
  187. while (uf_control->buffers.array[frame_get].state == UNSOLICITED_FRAME_RELEASED) {
  188. uf_control->buffers.array[frame_get].state = UNSOLICITED_FRAME_EMPTY;
  189. if (frame_get+1 == SCU_MAX_UNSOLICITED_FRAMES-1) {
  190. frame_cycle ^= SCU_MAX_UNSOLICITED_FRAMES;
  191. frame_get = 0;
  192. } else
  193. frame_get++;
  194. }
  195. uf_control->get = SCU_UFQGP_GEN_BIT(ENABLE_BIT) | frame_cycle | frame_get;
  196. return true;
  197. }