isci: Intel(R) C600 Series Chipset Storage Control Unit Driver
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / isci / core / scic_sds_unsolicited_frame_control.c
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
56 /**
57  * This file contains the implementation of the
58  *    struct scic_sds_unsolicited_frame_control object and it's public, protected, and
59  *    private methods.
60  *
61  *
62  */
63
64 #include "scic_sds_unsolicited_frame_control.h"
65 #include "scu_registers.h"
66 #include "scic_sds_controller.h"
67 #include "scic_user_callback.h"
68 #include "sci_util.h"
69 #include "sci_environment.h"
70
71 /**
72  * The UF buffer address table size must be programmed to a power of 2.  Find
73  *    the first power of 2 that is equal to or greater then the number of
74  *    unsolicited frame buffers to be utilized.
75  * @uf_control: This parameter specifies the UF control object for which to
76  *    update the address table count.
77  *
78  */
79 void scic_sds_unsolicited_frame_control_set_address_table_count(
80         struct scic_sds_unsolicited_frame_control *uf_control)
81 {
82         uf_control->address_table.count = SCU_MIN_UF_TABLE_ENTRIES;
83         while (
84                 (uf_control->address_table.count < uf_control->buffers.count)
85                 && (uf_control->address_table.count < SCU_ABSOLUTE_MAX_UNSOLICITED_FRAMES)
86                 ) {
87                 uf_control->address_table.count <<= 1;
88         }
89 }
90
91 /**
92  * This method will program the unsolicited frames (UFs) into the UF address
93  *    table and construct the UF frame structure being modeled in the core.  It
94  *    will handle the case where some of the UFs are not being used and thus
95  *    should have entries programmed to zero in the address table.
96  * @uf_control: This parameter specifies the unsolicted frame control object
97  *    for which to construct the unsolicited frames objects.
98  * @uf_buffer_phys_address: This parameter specifies the physical address for
99  *    the first unsolicited frame buffer.
100  * @uf_buffer_virt_address: This parameter specifies the virtual address for
101  *    the first unsolicited frame buffer.
102  * @unused_uf_header_entries: This parameter specifies the number of unused UF
103  *    headers.  This value can be non-zero when there are a non-power of 2
104  *    number of unsolicited frames being supported.
105  * @used_uf_header_entries: This parameter specifies the number of actually
106  *    utilized UF headers.
107  *
108  */
109 static void scic_sds_unsolicited_frame_control_construct_frames(
110         struct scic_sds_unsolicited_frame_control *uf_control,
111         dma_addr_t uf_buffer_phys_address,
112         unsigned long uf_buffer_virt_address,
113         u32 unused_uf_header_entries,
114         u32 used_uf_header_entries)
115 {
116         u32 index;
117         struct scic_sds_unsolicited_frame *uf;
118
119         /*
120          * Program the unused buffers into the UF address table and the
121          * controller's array of UFs. */
122         for (index = 0; index < unused_uf_header_entries; index++) {
123                 uf = &uf_control->buffers.array[index];
124
125                 sci_cb_make_physical_address(
126                         uf_control->address_table.array[index], 0, 0
127                         );
128                 uf->buffer = NULL;
129                 uf->header = &uf_control->headers.array[index];
130                 uf->state  = UNSOLICITED_FRAME_EMPTY;
131         }
132
133         /*
134          * Program the actual used UF buffers into the UF address table and
135          * the controller's array of UFs. */
136         for (index = unused_uf_header_entries;
137              index < unused_uf_header_entries + used_uf_header_entries;
138              index++) {
139                 uf = &uf_control->buffers.array[index];
140
141                 uf_control->address_table.array[index] = uf_buffer_phys_address;
142
143                 uf->buffer = (void *)uf_buffer_virt_address;
144                 uf->header = &uf_control->headers.array[index];
145                 uf->state  = UNSOLICITED_FRAME_EMPTY;
146
147                 /*
148                  * Increment the address of the physical and virtual memory pointers
149                  * Everything is aligned on 1k boundary with an increment of 1k */
150                 uf_buffer_virt_address += SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
151                 sci_physical_address_add(
152                         uf_buffer_phys_address, SCU_UNSOLICITED_FRAME_BUFFER_SIZE
153                         );
154         }
155 }
156
157 /**
158  * This method constructs the various members of the unsolicted frame control
159  *    object (buffers, headers, address, table, etc).
160  * @uf_control: This parameter specifies the unsolicited frame control object
161  *    to construct.
162  * @mde: This parameter specifies the memory descriptor from which to derive
163  *    all of the address information needed to get the unsolicited frame
164  *    functionality working.
165  * @controller: This parameter specifies the controller object associated with
166  *    the uf_control being constructed.
167  *
168  */
169 void scic_sds_unsolicited_frame_control_construct(
170         struct scic_sds_unsolicited_frame_control *uf_control,
171         struct sci_physical_memory_descriptor *mde,
172         struct scic_sds_controller *controller)
173 {
174         u32 unused_uf_header_entries;
175         u32 used_uf_header_entries;
176         u32 used_uf_buffer_bytes;
177         u32 unused_uf_header_bytes;
178         u32 used_uf_header_bytes;
179         dma_addr_t uf_buffer_phys_address;
180
181         /*
182          * Prepare all of the memory sizes for the UF headers, UF address
183          * table, and UF buffers themselves. */
184         used_uf_buffer_bytes     = uf_control->buffers.count
185                                    * SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
186         unused_uf_header_entries = uf_control->address_table.count
187                                    - uf_control->buffers.count;
188         used_uf_header_entries   = uf_control->buffers.count;
189         unused_uf_header_bytes   = unused_uf_header_entries
190                                    * sizeof(struct scu_unsolicited_frame_header);
191         used_uf_header_bytes     = used_uf_header_entries
192                                    * sizeof(struct scu_unsolicited_frame_header);
193
194         /*
195          * The Unsolicited Frame buffers are set at the start of the UF
196          * memory descriptor entry.  The headers and address table will be
197          * placed after the buffers. */
198         uf_buffer_phys_address = mde->physical_address;
199
200         /*
201          * Program the location of the UF header table into the SCU.
202          * Notes:
203          * - The address must align on a 64-byte boundary. Guaranteed to be
204          *   on 64-byte boundary already 1KB boundary for unsolicited frames.
205          * - Program unused header entries to overlap with the last
206          *   unsolicited frame.  The silicon will never DMA to these unused
207          *   headers, since we program the UF address table pointers to
208          *   NULL. */
209         uf_control->headers.physical_address = uf_buffer_phys_address;
210         sci_physical_address_add(
211                 uf_control->headers.physical_address, used_uf_buffer_bytes);
212         sci_physical_address_subtract(
213                 uf_control->headers.physical_address, unused_uf_header_bytes);
214         uf_control->headers.array
215                 = (struct scu_unsolicited_frame_header *)
216                   scic_cb_get_virtual_address(
217                 controller, uf_control->headers.physical_address
218                 );
219
220         /*
221          * Program the location of the UF address table into the SCU.
222          * Notes:
223          * - The address must align on a 64-bit boundary. Guaranteed to be on 64
224          *   byte boundary already due to above programming headers being on a
225          *   64-bit boundary and headers are on a 64-bytes in size. */
226         uf_control->address_table.physical_address = uf_buffer_phys_address;
227         sci_physical_address_add(
228                 uf_control->address_table.physical_address, used_uf_buffer_bytes);
229         sci_physical_address_add(
230                 uf_control->address_table.physical_address, used_uf_header_bytes);
231         uf_control->address_table.array
232                 = (dma_addr_t *)
233                   scic_cb_get_virtual_address(
234                 controller, uf_control->address_table.physical_address
235                 );
236
237         uf_control->get = 0;
238
239         /*
240          * UF buffer requirements are:
241          * - The last entry in the UF queue is not NULL.
242          * - There is a power of 2 number of entries (NULL or not-NULL)
243          *   programmed into the queue.
244          * - Aligned on a 1KB boundary. */
245
246         /*
247          * If the user provided less then the maximum amount of memory,
248          * then be sure that we programm the first entries in the UF
249          * address table to NULL. */
250         scic_sds_unsolicited_frame_control_construct_frames(
251                 uf_control,
252                 uf_buffer_phys_address,
253                 (unsigned long)mde->virtual_address,
254                 unused_uf_header_entries,
255                 used_uf_header_entries
256                 );
257 }
258
259 /**
260  * This method returns the frame header for the specified frame index.
261  * @uf_control:
262  * @frame_index:
263  * @frame_header:
264  *
265  * enum sci_status
266  */
267 enum sci_status scic_sds_unsolicited_frame_control_get_header(
268         struct scic_sds_unsolicited_frame_control *uf_control,
269         u32 frame_index,
270         void **frame_header)
271 {
272         if (frame_index < uf_control->address_table.count) {
273                 /*
274                  * Skip the first word in the frame since this is a controll word used
275                  * by the hardware. */
276                 *frame_header = &uf_control->buffers.array[frame_index].header->data;
277
278                 return SCI_SUCCESS;
279         }
280
281         return SCI_FAILURE_INVALID_PARAMETER_VALUE;
282 }
283
284 /**
285  * This method returns the frame buffer for the specified frame index.
286  * @uf_control:
287  * @frame_index:
288  * @frame_buffer:
289  *
290  * enum sci_status
291  */
292 enum sci_status scic_sds_unsolicited_frame_control_get_buffer(
293         struct scic_sds_unsolicited_frame_control *uf_control,
294         u32 frame_index,
295         void **frame_buffer)
296 {
297         if (frame_index < uf_control->address_table.count) {
298                 *frame_buffer = uf_control->buffers.array[frame_index].buffer;
299
300                 return SCI_SUCCESS;
301         }
302
303         return SCI_FAILURE_INVALID_PARAMETER_VALUE;
304 }
305
306 /**
307  * This method releases the frame once this is done the frame is available for
308  *    re-use by the hardware.  The data contained in the frame header and frame
309  *    buffer is no longer valid.
310  * @uf_control: This parameter specifies the UF control object
311  * @frame_index: This parameter specifies the frame index to attempt to release.
312  *
313  * This method returns an indication to the caller as to whether the
314  * unsolicited frame get pointer should be updated. true This value indicates
315  * the unsolicited frame get pointer should be updated (i.e. write
316  * SCU_UFQGP_WRITE). false This value indicates the get pointer should not be
317  * updated.
318  */
319 bool scic_sds_unsolicited_frame_control_release_frame(
320         struct scic_sds_unsolicited_frame_control *uf_control,
321         u32 frame_index)
322 {
323         u32 frame_get;
324         u32 frame_cycle;
325
326         frame_get   = uf_control->get & (uf_control->address_table.count - 1);
327         frame_cycle = uf_control->get & uf_control->address_table.count;
328
329         /*
330          * In the event there are NULL entries in the UF table, we need to
331          * advance the get pointer in order to find out if this frame should
332          * be released (i.e. update the get pointer). */
333         while (((lower_32_bits(uf_control->address_table.array[frame_get])
334                                         == 0) &&
335                 (upper_32_bits(uf_control->address_table.array[frame_get])
336                                         == 0)) &&
337                (frame_get < uf_control->address_table.count))
338                 frame_get++;
339
340         /*
341          * The table has a NULL entry as it's last element.  This is
342          * illegal. */
343         BUG_ON(frame_get >= uf_control->address_table.count);
344
345         if (frame_index < uf_control->address_table.count) {
346                 uf_control->buffers.array[frame_index].state = UNSOLICITED_FRAME_RELEASED;
347
348                 /*
349                  * The frame index is equal to the current get pointer so we
350                  * can now free up all of the frame entries that */
351                 if (frame_get == frame_index) {
352                         while (
353                                 uf_control->buffers.array[frame_get].state
354                                 == UNSOLICITED_FRAME_RELEASED
355                                 ) {
356                                 uf_control->buffers.array[frame_get].state = UNSOLICITED_FRAME_EMPTY;
357
358                                 INCREMENT_QUEUE_GET(
359                                         frame_get,
360                                         frame_cycle,
361                                         uf_control->address_table.count - 1,
362                                         uf_control->address_table.count
363                                         );
364                         }
365
366                         uf_control->get =
367                                 (SCU_UFQGP_GEN_BIT(ENABLE_BIT) | frame_cycle | frame_get);
368
369                         return true;
370                 } else {
371                         /*
372                          * Frames remain in use until we advance the get pointer
373                          * so there is nothing we can do here */
374                 }
375         }
376
377         return false;
378 }
379