xref: /linux/drivers/scsi/isci/unsolicited_frame_control.c (revision e5451c8f8330e03ad3cfa16048b4daf961af434f)
163a3a15fSDan Williams /*
263a3a15fSDan Williams  * This file is provided under a dual BSD/GPLv2 license.  When using or
363a3a15fSDan Williams  * redistributing this file, you may do so under either license.
463a3a15fSDan Williams  *
563a3a15fSDan Williams  * GPL LICENSE SUMMARY
663a3a15fSDan Williams  *
763a3a15fSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
863a3a15fSDan Williams  *
963a3a15fSDan Williams  * This program is free software; you can redistribute it and/or modify
1063a3a15fSDan Williams  * it under the terms of version 2 of the GNU General Public License as
1163a3a15fSDan Williams  * published by the Free Software Foundation.
1263a3a15fSDan Williams  *
1363a3a15fSDan Williams  * This program is distributed in the hope that it will be useful, but
1463a3a15fSDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
1563a3a15fSDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1663a3a15fSDan Williams  * General Public License for more details.
1763a3a15fSDan Williams  *
1863a3a15fSDan Williams  * You should have received a copy of the GNU General Public License
1963a3a15fSDan Williams  * along with this program; if not, write to the Free Software
2063a3a15fSDan Williams  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
2163a3a15fSDan Williams  * The full GNU General Public License is included in this distribution
2263a3a15fSDan Williams  * in the file called LICENSE.GPL.
2363a3a15fSDan Williams  *
2463a3a15fSDan Williams  * BSD LICENSE
2563a3a15fSDan Williams  *
2663a3a15fSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
2763a3a15fSDan Williams  * All rights reserved.
2863a3a15fSDan Williams  *
2963a3a15fSDan Williams  * Redistribution and use in source and binary forms, with or without
3063a3a15fSDan Williams  * modification, are permitted provided that the following conditions
3163a3a15fSDan Williams  * are met:
3263a3a15fSDan Williams  *
3363a3a15fSDan Williams  *   * Redistributions of source code must retain the above copyright
3463a3a15fSDan Williams  *     notice, this list of conditions and the following disclaimer.
3563a3a15fSDan Williams  *   * Redistributions in binary form must reproduce the above copyright
3663a3a15fSDan Williams  *     notice, this list of conditions and the following disclaimer in
3763a3a15fSDan Williams  *     the documentation and/or other materials provided with the
3863a3a15fSDan Williams  *     distribution.
3963a3a15fSDan Williams  *   * Neither the name of Intel Corporation nor the names of its
4063a3a15fSDan Williams  *     contributors may be used to endorse or promote products derived
4163a3a15fSDan Williams  *     from this software without specific prior written permission.
4263a3a15fSDan Williams  *
4363a3a15fSDan Williams  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4463a3a15fSDan Williams  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4563a3a15fSDan Williams  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4663a3a15fSDan Williams  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4763a3a15fSDan Williams  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4863a3a15fSDan Williams  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4963a3a15fSDan Williams  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5063a3a15fSDan Williams  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5163a3a15fSDan Williams  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5263a3a15fSDan Williams  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5363a3a15fSDan Williams  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5463a3a15fSDan Williams  */
5563a3a15fSDan Williams 
5663a3a15fSDan Williams #include "host.h"
5763a3a15fSDan Williams #include "unsolicited_frame_control.h"
5863a3a15fSDan Williams #include "registers.h"
5963a3a15fSDan Williams 
sci_unsolicited_frame_control_construct(struct isci_host * ihost)60*abec912dSDan Williams void sci_unsolicited_frame_control_construct(struct isci_host *ihost)
6163a3a15fSDan Williams {
6289a7301fSDan Williams 	struct sci_unsolicited_frame_control *uf_control = &ihost->uf_control;
6389a7301fSDan Williams 	struct sci_unsolicited_frame *uf;
64*abec912dSDan Williams 	dma_addr_t dma = ihost->ufi_dma;
65*abec912dSDan Williams 	void *virt = ihost->ufi_buf;
66*abec912dSDan Williams 	int i;
6763a3a15fSDan Williams 
6863a3a15fSDan Williams 	/*
6963a3a15fSDan Williams 	 * The Unsolicited Frame buffers are set at the start of the UF
7063a3a15fSDan Williams 	 * memory descriptor entry. The headers and address table will be
7163a3a15fSDan Williams 	 * placed after the buffers.
7263a3a15fSDan Williams 	 */
7363a3a15fSDan Williams 
7463a3a15fSDan Williams 	/*
7563a3a15fSDan Williams 	 * Program the location of the UF header table into the SCU.
7663a3a15fSDan Williams 	 * Notes:
7763a3a15fSDan Williams 	 * - The address must align on a 64-byte boundary. Guaranteed to be
7863a3a15fSDan Williams 	 *   on 64-byte boundary already 1KB boundary for unsolicited frames.
7963a3a15fSDan Williams 	 * - Program unused header entries to overlap with the last
8063a3a15fSDan Williams 	 *   unsolicited frame.  The silicon will never DMA to these unused
8163a3a15fSDan Williams 	 *   headers, since we program the UF address table pointers to
8263a3a15fSDan Williams 	 *   NULL.
8363a3a15fSDan Williams 	 */
84*abec912dSDan Williams 	uf_control->headers.physical_address = dma + SCI_UFI_BUF_SIZE;
85*abec912dSDan Williams 	uf_control->headers.array = virt + SCI_UFI_BUF_SIZE;
8663a3a15fSDan Williams 
8763a3a15fSDan Williams 	/*
8863a3a15fSDan Williams 	 * Program the location of the UF address table into the SCU.
8963a3a15fSDan Williams 	 * Notes:
9063a3a15fSDan Williams 	 * - The address must align on a 64-bit boundary. Guaranteed to be on 64
9163a3a15fSDan Williams 	 *   byte boundary already due to above programming headers being on a
9263a3a15fSDan Williams 	 *   64-bit boundary and headers are on a 64-bytes in size.
9363a3a15fSDan Williams 	 */
94*abec912dSDan Williams 	uf_control->address_table.physical_address = dma + SCI_UFI_BUF_SIZE + SCI_UFI_HDR_SIZE;
95*abec912dSDan Williams 	uf_control->address_table.array = virt + SCI_UFI_BUF_SIZE + SCI_UFI_HDR_SIZE;
9663a3a15fSDan Williams 	uf_control->get = 0;
9763a3a15fSDan Williams 
9863a3a15fSDan Williams 	/*
9963a3a15fSDan Williams 	 * UF buffer requirements are:
10063a3a15fSDan Williams 	 * - The last entry in the UF queue is not NULL.
10163a3a15fSDan Williams 	 * - There is a power of 2 number of entries (NULL or not-NULL)
10263a3a15fSDan Williams 	 *   programmed into the queue.
10363a3a15fSDan Williams 	 * - Aligned on a 1KB boundary. */
10463a3a15fSDan Williams 
10563a3a15fSDan Williams 	/*
1067c78da31SDan Williams 	 * Program the actual used UF buffers into the UF address table and
1077c78da31SDan Williams 	 * the controller's array of UFs.
1087c78da31SDan Williams 	 */
1097c78da31SDan Williams 	for (i = 0; i < SCU_MAX_UNSOLICITED_FRAMES; i++) {
1107c78da31SDan Williams 		uf = &uf_control->buffers.array[i];
1117c78da31SDan Williams 
1127c78da31SDan Williams 		uf_control->address_table.array[i] = dma;
1137c78da31SDan Williams 
1147c78da31SDan Williams 		uf->buffer = virt;
1157c78da31SDan Williams 		uf->header = &uf_control->headers.array[i];
1167c78da31SDan Williams 		uf->state  = UNSOLICITED_FRAME_EMPTY;
1177c78da31SDan Williams 
1187c78da31SDan Williams 		/*
1197c78da31SDan Williams 		 * Increment the address of the physical and virtual memory
1207c78da31SDan Williams 		 * pointers. Everything is aligned on 1k boundary with an
1217c78da31SDan Williams 		 * increment of 1k.
1227c78da31SDan Williams 		 */
1237c78da31SDan Williams 		virt += SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
1247c78da31SDan Williams 		dma += SCU_UNSOLICITED_FRAME_BUFFER_SIZE;
1257c78da31SDan Williams 	}
12663a3a15fSDan Williams }
12763a3a15fSDan Williams 
sci_unsolicited_frame_control_get_header(struct sci_unsolicited_frame_control * uf_control,u32 frame_index,void ** frame_header)12889a7301fSDan Williams enum sci_status sci_unsolicited_frame_control_get_header(struct sci_unsolicited_frame_control *uf_control,
12963a3a15fSDan Williams 							 u32 frame_index,
13063a3a15fSDan Williams 							 void **frame_header)
13163a3a15fSDan Williams {
1327c78da31SDan Williams 	if (frame_index < SCU_MAX_UNSOLICITED_FRAMES) {
13389a7301fSDan Williams 		/* Skip the first word in the frame since this is a controll word used
13489a7301fSDan Williams 		 * by the hardware.
13589a7301fSDan Williams 		 */
13663a3a15fSDan Williams 		*frame_header = &uf_control->buffers.array[frame_index].header->data;
13763a3a15fSDan Williams 
13863a3a15fSDan Williams 		return SCI_SUCCESS;
13963a3a15fSDan Williams 	}
14063a3a15fSDan Williams 
14163a3a15fSDan Williams 	return SCI_FAILURE_INVALID_PARAMETER_VALUE;
14263a3a15fSDan Williams }
14363a3a15fSDan Williams 
sci_unsolicited_frame_control_get_buffer(struct sci_unsolicited_frame_control * uf_control,u32 frame_index,void ** frame_buffer)14489a7301fSDan Williams enum sci_status sci_unsolicited_frame_control_get_buffer(struct sci_unsolicited_frame_control *uf_control,
14563a3a15fSDan Williams 							 u32 frame_index,
14663a3a15fSDan Williams 							 void **frame_buffer)
14763a3a15fSDan Williams {
1487c78da31SDan Williams 	if (frame_index < SCU_MAX_UNSOLICITED_FRAMES) {
14963a3a15fSDan Williams 		*frame_buffer = uf_control->buffers.array[frame_index].buffer;
15063a3a15fSDan Williams 
15163a3a15fSDan Williams 		return SCI_SUCCESS;
15263a3a15fSDan Williams 	}
15363a3a15fSDan Williams 
15463a3a15fSDan Williams 	return SCI_FAILURE_INVALID_PARAMETER_VALUE;
15563a3a15fSDan Williams }
15663a3a15fSDan Williams 
sci_unsolicited_frame_control_release_frame(struct sci_unsolicited_frame_control * uf_control,u32 frame_index)15789a7301fSDan Williams bool sci_unsolicited_frame_control_release_frame(struct sci_unsolicited_frame_control *uf_control,
15863a3a15fSDan Williams 						 u32 frame_index)
15963a3a15fSDan Williams {
16063a3a15fSDan Williams 	u32 frame_get;
16163a3a15fSDan Williams 	u32 frame_cycle;
16263a3a15fSDan Williams 
1637c78da31SDan Williams 	frame_get   = uf_control->get & (SCU_MAX_UNSOLICITED_FRAMES - 1);
1647c78da31SDan Williams 	frame_cycle = uf_control->get & SCU_MAX_UNSOLICITED_FRAMES;
16563a3a15fSDan Williams 
16663a3a15fSDan Williams 	/*
16763a3a15fSDan Williams 	 * In the event there are NULL entries in the UF table, we need to
16863a3a15fSDan Williams 	 * advance the get pointer in order to find out if this frame should
169994a9303SDan Williams 	 * be released (i.e. update the get pointer)
170994a9303SDan Williams 	 */
1717c78da31SDan Williams 	while (lower_32_bits(uf_control->address_table.array[frame_get]) == 0 &&
1727c78da31SDan Williams 	       upper_32_bits(uf_control->address_table.array[frame_get]) == 0 &&
1737c78da31SDan Williams 	       frame_get < SCU_MAX_UNSOLICITED_FRAMES)
17463a3a15fSDan Williams 		frame_get++;
17563a3a15fSDan Williams 
17663a3a15fSDan Williams 	/*
17763a3a15fSDan Williams 	 * The table has a NULL entry as it's last element.  This is
178994a9303SDan Williams 	 * illegal.
179994a9303SDan Williams 	 */
1807c78da31SDan Williams 	BUG_ON(frame_get >= SCU_MAX_UNSOLICITED_FRAMES);
181994a9303SDan Williams 	if (frame_index >= SCU_MAX_UNSOLICITED_FRAMES)
182994a9303SDan Williams 		return false;
18363a3a15fSDan Williams 
18463a3a15fSDan Williams 	uf_control->buffers.array[frame_index].state = UNSOLICITED_FRAME_RELEASED;
18563a3a15fSDan Williams 
186994a9303SDan Williams 	if (frame_get != frame_index) {
18763a3a15fSDan Williams 		/*
18863a3a15fSDan Williams 		 * Frames remain in use until we advance the get pointer
189994a9303SDan Williams 		 * so there is nothing we can do here
190994a9303SDan Williams 		 */
19163a3a15fSDan Williams 		return false;
19263a3a15fSDan Williams 	}
19363a3a15fSDan Williams 
194994a9303SDan Williams 	/*
195994a9303SDan Williams 	 * The frame index is equal to the current get pointer so we
196994a9303SDan Williams 	 * can now free up all of the frame entries that
197994a9303SDan Williams 	 */
198994a9303SDan Williams 	while (uf_control->buffers.array[frame_get].state == UNSOLICITED_FRAME_RELEASED) {
199994a9303SDan Williams 		uf_control->buffers.array[frame_get].state = UNSOLICITED_FRAME_EMPTY;
200994a9303SDan Williams 
201994a9303SDan Williams 		if (frame_get+1 == SCU_MAX_UNSOLICITED_FRAMES-1) {
202994a9303SDan Williams 			frame_cycle ^= SCU_MAX_UNSOLICITED_FRAMES;
203994a9303SDan Williams 			frame_get = 0;
204994a9303SDan Williams 		} else
205994a9303SDan Williams 			frame_get++;
206994a9303SDan Williams 	}
207994a9303SDan Williams 
208994a9303SDan Williams 	uf_control->get = SCU_UFQGP_GEN_BIT(ENABLE_BIT) | frame_cycle | frame_get;
209994a9303SDan Williams 
210994a9303SDan Williams 	return true;
211994a9303SDan Williams }
212