unsolicited_frame_control.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. /**
  59. * This method will program the unsolicited frames (UFs) into the UF address
  60. * table and construct the UF frame structure being modeled in the core. It
  61. * will handle the case where some of the UFs are not being used and thus
  62. * should have entries programmed to zero in the address table.
  63. * @uf_control: This parameter specifies the unsolicted frame control object
  64. * for which to construct the unsolicited frames objects.
  65. * @uf_buffer_phys_address: This parameter specifies the physical address for
  66. * the first unsolicited frame buffer.
  67. * @uf_buffer_virt_address: This parameter specifies the virtual address for
  68. * the first unsolicited frame buffer.
  69. * @unused_uf_header_entries: This parameter specifies the number of unused UF
  70. * headers. This value can be non-zero when there are a non-power of 2
  71. * number of unsolicited frames being supported.
  72. * @used_uf_header_entries: This parameter specifies the number of actually
  73. * utilized UF headers.
  74. *
  75. */
  76. static void scic_sds_unsolicited_frame_control_construct_frames(
  77. struct scic_sds_unsolicited_frame_control *uf_control,
  78. dma_addr_t uf_buffer_phys_address,
  79. void *uf_buffer_virt_address,
  80. u32 unused_uf_header_entries,
  81. u32 used_uf_header_entries)
  82. {
  83. u32 index;
  84. struct scic_sds_unsolicited_frame *uf;
  85. /*
  86. * Program the unused buffers into the UF address table and the
  87. * controller's array of UFs.
  88. */
  89. for (index = 0; index < unused_uf_header_entries; index++) {
  90. uf = &uf_control->buffers.array[index];
  91. uf->buffer = NULL;
  92. uf_control->address_table.array[index] = 0;
  93. uf->header = &uf_control->headers.array[index];
  94. uf->state = UNSOLICITED_FRAME_EMPTY;
  95. }
  96. /*
  97. * Program the actual used UF buffers into the UF address table and
  98. * the controller's array of UFs.
  99. */
  100. for (index = unused_uf_header_entries;
  101. index < unused_uf_header_entries + used_uf_header_entries;
  102. index++) {
  103. uf = &uf_control->buffers.array[index];
  104. uf_control->address_table.array[index] = uf_buffer_phys_address;
  105. uf->buffer = uf_buffer_virt_address;
  106. uf->header = &uf_control->headers.array[index];
  107. uf->state = UNSOLICITED_FRAME_EMPTY;
  108. /*
  109. * Increment the address of the physical and virtual memory
  110. * pointers. Everything is aligned on 1k boundary with an
  111. * increment of 1k.
  112. */
  113. uf_buffer_virt_address += SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
  114. uf_buffer_phys_address += SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
  115. }
  116. }
  117. int scic_sds_unsolicited_frame_control_construct(struct scic_sds_controller *scic)
  118. {
  119. struct scic_sds_unsolicited_frame_control *uf_control = &scic->uf_control;
  120. u32 unused_uf_header_entries;
  121. u32 used_uf_header_entries;
  122. u32 used_uf_buffer_bytes;
  123. u32 unused_uf_header_bytes;
  124. u32 used_uf_header_bytes;
  125. dma_addr_t uf_buffer_phys_address;
  126. void *uf_buffer_virt_address;
  127. size_t size;
  128. /*
  129. * The UF buffer address table size must be programmed to a power
  130. * of 2. Find the first power of 2 that is equal to or greater then
  131. * the number of unsolicited frame buffers to be utilized.
  132. */
  133. uf_control->address_table.count = SCU_MIN_UF_TABLE_ENTRIES;
  134. while (uf_control->address_table.count < uf_control->buffers.count &&
  135. uf_control->address_table.count < SCU_ABSOLUTE_MAX_UNSOLICITED_FRAMES)
  136. uf_control->address_table.count <<= 1;
  137. /*
  138. * Prepare all of the memory sizes for the UF headers, UF address
  139. * table, and UF buffers themselves.
  140. */
  141. used_uf_buffer_bytes = uf_control->buffers.count
  142. * SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
  143. unused_uf_header_entries = uf_control->address_table.count
  144. - uf_control->buffers.count;
  145. used_uf_header_entries = uf_control->buffers.count;
  146. unused_uf_header_bytes = unused_uf_header_entries
  147. * sizeof(struct scu_unsolicited_frame_header);
  148. used_uf_header_bytes = used_uf_header_entries
  149. * sizeof(struct scu_unsolicited_frame_header);
  150. size = used_uf_buffer_bytes + used_uf_header_bytes +
  151. uf_control->address_table.count * sizeof(dma_addr_t);
  152. /*
  153. * The Unsolicited Frame buffers are set at the start of the UF
  154. * memory descriptor entry. The headers and address table will be
  155. * placed after the buffers.
  156. */
  157. uf_buffer_virt_address = dmam_alloc_coherent(scic_to_dev(scic), size,
  158. &uf_buffer_phys_address, GFP_KERNEL);
  159. if (!uf_buffer_virt_address)
  160. return -ENOMEM;
  161. /*
  162. * Program the location of the UF header table into the SCU.
  163. * Notes:
  164. * - The address must align on a 64-byte boundary. Guaranteed to be
  165. * on 64-byte boundary already 1KB boundary for unsolicited frames.
  166. * - Program unused header entries to overlap with the last
  167. * unsolicited frame. The silicon will never DMA to these unused
  168. * headers, since we program the UF address table pointers to
  169. * NULL.
  170. */
  171. uf_control->headers.physical_address =
  172. uf_buffer_phys_address +
  173. used_uf_buffer_bytes -
  174. unused_uf_header_bytes;
  175. uf_control->headers.array =
  176. uf_buffer_virt_address +
  177. used_uf_buffer_bytes -
  178. unused_uf_header_bytes;
  179. /*
  180. * Program the location of the UF address table into the SCU.
  181. * Notes:
  182. * - The address must align on a 64-bit boundary. Guaranteed to be on 64
  183. * byte boundary already due to above programming headers being on a
  184. * 64-bit boundary and headers are on a 64-bytes in size.
  185. */
  186. uf_control->address_table.physical_address =
  187. uf_buffer_phys_address +
  188. used_uf_buffer_bytes +
  189. used_uf_header_bytes;
  190. uf_control->address_table.array =
  191. uf_buffer_virt_address +
  192. used_uf_buffer_bytes +
  193. used_uf_header_bytes;
  194. uf_control->get = 0;
  195. /*
  196. * UF buffer requirements are:
  197. * - The last entry in the UF queue is not NULL.
  198. * - There is a power of 2 number of entries (NULL or not-NULL)
  199. * programmed into the queue.
  200. * - Aligned on a 1KB boundary. */
  201. /*
  202. * If the user provided less then the maximum amount of memory,
  203. * then be sure that we programm the first entries in the UF
  204. * address table to NULL. */
  205. scic_sds_unsolicited_frame_control_construct_frames(
  206. uf_control,
  207. uf_buffer_phys_address,
  208. uf_buffer_virt_address,
  209. unused_uf_header_entries,
  210. used_uf_header_entries
  211. );
  212. return 0;
  213. }
  214. /**
  215. * This method returns the frame header for the specified frame index.
  216. * @uf_control:
  217. * @frame_index:
  218. * @frame_header:
  219. *
  220. * enum sci_status
  221. */
  222. enum sci_status scic_sds_unsolicited_frame_control_get_header(
  223. struct scic_sds_unsolicited_frame_control *uf_control,
  224. u32 frame_index,
  225. void **frame_header)
  226. {
  227. if (frame_index < uf_control->address_table.count) {
  228. /*
  229. * Skip the first word in the frame since this is a controll word used
  230. * by the hardware. */
  231. *frame_header = &uf_control->buffers.array[frame_index].header->data;
  232. return SCI_SUCCESS;
  233. }
  234. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  235. }
  236. /**
  237. * This method returns the frame buffer for the specified frame index.
  238. * @uf_control:
  239. * @frame_index:
  240. * @frame_buffer:
  241. *
  242. * enum sci_status
  243. */
  244. enum sci_status scic_sds_unsolicited_frame_control_get_buffer(
  245. struct scic_sds_unsolicited_frame_control *uf_control,
  246. u32 frame_index,
  247. void **frame_buffer)
  248. {
  249. if (frame_index < uf_control->address_table.count) {
  250. *frame_buffer = uf_control->buffers.array[frame_index].buffer;
  251. return SCI_SUCCESS;
  252. }
  253. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  254. }
  255. /**
  256. * This method releases the frame once this is done the frame is available for
  257. * re-use by the hardware. The data contained in the frame header and frame
  258. * buffer is no longer valid.
  259. * @uf_control: This parameter specifies the UF control object
  260. * @frame_index: This parameter specifies the frame index to attempt to release.
  261. *
  262. * This method returns an indication to the caller as to whether the
  263. * unsolicited frame get pointer should be updated.
  264. */
  265. bool scic_sds_unsolicited_frame_control_release_frame(
  266. struct scic_sds_unsolicited_frame_control *uf_control,
  267. u32 frame_index)
  268. {
  269. u32 frame_get;
  270. u32 frame_cycle;
  271. frame_get = uf_control->get & (uf_control->address_table.count - 1);
  272. frame_cycle = uf_control->get & uf_control->address_table.count;
  273. /*
  274. * In the event there are NULL entries in the UF table, we need to
  275. * advance the get pointer in order to find out if this frame should
  276. * be released (i.e. update the get pointer). */
  277. while (((lower_32_bits(uf_control->address_table.array[frame_get])
  278. == 0) &&
  279. (upper_32_bits(uf_control->address_table.array[frame_get])
  280. == 0)) &&
  281. (frame_get < uf_control->address_table.count))
  282. frame_get++;
  283. /*
  284. * The table has a NULL entry as it's last element. This is
  285. * illegal. */
  286. BUG_ON(frame_get >= uf_control->address_table.count);
  287. if (frame_index < uf_control->address_table.count) {
  288. uf_control->buffers.array[frame_index].state = UNSOLICITED_FRAME_RELEASED;
  289. /*
  290. * The frame index is equal to the current get pointer so we
  291. * can now free up all of the frame entries that */
  292. if (frame_get == frame_index) {
  293. while (
  294. uf_control->buffers.array[frame_get].state
  295. == UNSOLICITED_FRAME_RELEASED
  296. ) {
  297. uf_control->buffers.array[frame_get].state = UNSOLICITED_FRAME_EMPTY;
  298. INCREMENT_QUEUE_GET(
  299. frame_get,
  300. frame_cycle,
  301. uf_control->address_table.count - 1,
  302. uf_control->address_table.count
  303. );
  304. }
  305. uf_control->get =
  306. (SCU_UFQGP_GEN_BIT(ENABLE_BIT) | frame_cycle | frame_get);
  307. return true;
  308. } else {
  309. /*
  310. * Frames remain in use until we advance the get pointer
  311. * so there is nothing we can do here */
  312. }
  313. }
  314. return false;
  315. }