xref: /OK3568_Linux_fs/kernel/drivers/scsi/isci/host.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
3*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*4882a593Smuzhiyun  * General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
19*4882a593Smuzhiyun  * along with this program; if not, write to the Free Software
20*4882a593Smuzhiyun  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21*4882a593Smuzhiyun  * The full GNU General Public License is included in this distribution
22*4882a593Smuzhiyun  * in the file called LICENSE.GPL.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * BSD LICENSE
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27*4882a593Smuzhiyun  * All rights reserved.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
30*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
31*4882a593Smuzhiyun  * are met:
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  *   * Redistributions of source code must retain the above copyright
34*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer.
35*4882a593Smuzhiyun  *   * Redistributions in binary form must reproduce the above copyright
36*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer in
37*4882a593Smuzhiyun  *     the documentation and/or other materials provided with the
38*4882a593Smuzhiyun  *     distribution.
39*4882a593Smuzhiyun  *   * Neither the name of Intel Corporation nor the names of its
40*4882a593Smuzhiyun  *     contributors may be used to endorse or promote products derived
41*4882a593Smuzhiyun  *     from this software without specific prior written permission.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun #include <linux/circ_buf.h>
56*4882a593Smuzhiyun #include <linux/device.h>
57*4882a593Smuzhiyun #include <scsi/sas.h>
58*4882a593Smuzhiyun #include "host.h"
59*4882a593Smuzhiyun #include "isci.h"
60*4882a593Smuzhiyun #include "port.h"
61*4882a593Smuzhiyun #include "probe_roms.h"
62*4882a593Smuzhiyun #include "remote_device.h"
63*4882a593Smuzhiyun #include "request.h"
64*4882a593Smuzhiyun #include "scu_completion_codes.h"
65*4882a593Smuzhiyun #include "scu_event_codes.h"
66*4882a593Smuzhiyun #include "registers.h"
67*4882a593Smuzhiyun #include "scu_remote_node_context.h"
68*4882a593Smuzhiyun #include "scu_task_context.h"
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define SCU_CONTEXT_RAM_INIT_STALL_TIME      200
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #define smu_max_ports(dcc_value) \
73*4882a593Smuzhiyun 	(\
74*4882a593Smuzhiyun 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
75*4882a593Smuzhiyun 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
76*4882a593Smuzhiyun 	)
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #define smu_max_task_contexts(dcc_value)	\
79*4882a593Smuzhiyun 	(\
80*4882a593Smuzhiyun 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
81*4882a593Smuzhiyun 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
82*4882a593Smuzhiyun 	)
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun #define smu_max_rncs(dcc_value) \
85*4882a593Smuzhiyun 	(\
86*4882a593Smuzhiyun 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
87*4882a593Smuzhiyun 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
88*4882a593Smuzhiyun 	)
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT      100
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * The number of milliseconds to wait while a given phy is consuming power
96*4882a593Smuzhiyun  * before allowing another set of phys to consume power. Ultimately, this will
97*4882a593Smuzhiyun  * be specified by OEM parameter.
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun  * NORMALIZE_PUT_POINTER() -
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * This macro will normalize the completion queue put pointer so its value can
105*4882a593Smuzhiyun  * be used as an array inde
106*4882a593Smuzhiyun  */
107*4882a593Smuzhiyun #define NORMALIZE_PUT_POINTER(x) \
108*4882a593Smuzhiyun 	((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /**
112*4882a593Smuzhiyun  * NORMALIZE_EVENT_POINTER() -
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * This macro will normalize the completion queue event entry so its value can
115*4882a593Smuzhiyun  * be used as an index.
116*4882a593Smuzhiyun  */
117*4882a593Smuzhiyun #define NORMALIZE_EVENT_POINTER(x) \
118*4882a593Smuzhiyun 	(\
119*4882a593Smuzhiyun 		((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
120*4882a593Smuzhiyun 		>> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT	\
121*4882a593Smuzhiyun 	)
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun  * NORMALIZE_GET_POINTER() -
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * This macro will normalize the completion queue get pointer so its value can
127*4882a593Smuzhiyun  * be used as an index into an array
128*4882a593Smuzhiyun  */
129*4882a593Smuzhiyun #define NORMALIZE_GET_POINTER(x) \
130*4882a593Smuzhiyun 	((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun  * NORMALIZE_GET_POINTER_CYCLE_BIT() -
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  * This macro will normalize the completion queue cycle pointer so it matches
136*4882a593Smuzhiyun  * the completion queue cycle bit
137*4882a593Smuzhiyun  */
138*4882a593Smuzhiyun #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
139*4882a593Smuzhiyun 	((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /**
142*4882a593Smuzhiyun  * COMPLETION_QUEUE_CYCLE_BIT() -
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * This macro will return the cycle bit of the completion queue entry
145*4882a593Smuzhiyun  */
146*4882a593Smuzhiyun #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /* Init the state machine and call the state entry function (if any) */
sci_init_sm(struct sci_base_state_machine * sm,const struct sci_base_state * state_table,u32 initial_state)149*4882a593Smuzhiyun void sci_init_sm(struct sci_base_state_machine *sm,
150*4882a593Smuzhiyun 		 const struct sci_base_state *state_table, u32 initial_state)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	sci_state_transition_t handler;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	sm->initial_state_id    = initial_state;
155*4882a593Smuzhiyun 	sm->previous_state_id   = initial_state;
156*4882a593Smuzhiyun 	sm->current_state_id    = initial_state;
157*4882a593Smuzhiyun 	sm->state_table         = state_table;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	handler = sm->state_table[initial_state].enter_state;
160*4882a593Smuzhiyun 	if (handler)
161*4882a593Smuzhiyun 		handler(sm);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /* Call the state exit fn, update the current state, call the state entry fn */
sci_change_state(struct sci_base_state_machine * sm,u32 next_state)165*4882a593Smuzhiyun void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	sci_state_transition_t handler;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	handler = sm->state_table[sm->current_state_id].exit_state;
170*4882a593Smuzhiyun 	if (handler)
171*4882a593Smuzhiyun 		handler(sm);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	sm->previous_state_id = sm->current_state_id;
174*4882a593Smuzhiyun 	sm->current_state_id = next_state;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	handler = sm->state_table[sm->current_state_id].enter_state;
177*4882a593Smuzhiyun 	if (handler)
178*4882a593Smuzhiyun 		handler(sm);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
sci_controller_completion_queue_has_entries(struct isci_host * ihost)181*4882a593Smuzhiyun static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	u32 get_value = ihost->completion_queue_get;
184*4882a593Smuzhiyun 	u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
187*4882a593Smuzhiyun 	    COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
188*4882a593Smuzhiyun 		return true;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	return false;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
sci_controller_isr(struct isci_host * ihost)193*4882a593Smuzhiyun static bool sci_controller_isr(struct isci_host *ihost)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	if (sci_controller_completion_queue_has_entries(ihost))
196*4882a593Smuzhiyun 		return true;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/* we have a spurious interrupt it could be that we have already
199*4882a593Smuzhiyun 	 * emptied the completion queue from a previous interrupt
200*4882a593Smuzhiyun 	 * FIXME: really!?
201*4882a593Smuzhiyun 	 */
202*4882a593Smuzhiyun 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/* There is a race in the hardware that could cause us not to be
205*4882a593Smuzhiyun 	 * notified of an interrupt completion if we do not take this
206*4882a593Smuzhiyun 	 * step.  We will mask then unmask the interrupts so if there is
207*4882a593Smuzhiyun 	 * another interrupt pending the clearing of the interrupt
208*4882a593Smuzhiyun 	 * source we get the next interrupt message.
209*4882a593Smuzhiyun 	 */
210*4882a593Smuzhiyun 	spin_lock(&ihost->scic_lock);
211*4882a593Smuzhiyun 	if (test_bit(IHOST_IRQ_ENABLED, &ihost->flags)) {
212*4882a593Smuzhiyun 		writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
213*4882a593Smuzhiyun 		writel(0, &ihost->smu_registers->interrupt_mask);
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 	spin_unlock(&ihost->scic_lock);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	return false;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
isci_msix_isr(int vec,void * data)220*4882a593Smuzhiyun irqreturn_t isci_msix_isr(int vec, void *data)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	struct isci_host *ihost = data;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	if (sci_controller_isr(ihost))
225*4882a593Smuzhiyun 		tasklet_schedule(&ihost->completion_tasklet);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	return IRQ_HANDLED;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
sci_controller_error_isr(struct isci_host * ihost)230*4882a593Smuzhiyun static bool sci_controller_error_isr(struct isci_host *ihost)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	u32 interrupt_status;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	interrupt_status =
235*4882a593Smuzhiyun 		readl(&ihost->smu_registers->interrupt_status);
236*4882a593Smuzhiyun 	interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	if (interrupt_status != 0) {
239*4882a593Smuzhiyun 		/*
240*4882a593Smuzhiyun 		 * There is an error interrupt pending so let it through and handle
241*4882a593Smuzhiyun 		 * in the callback */
242*4882a593Smuzhiyun 		return true;
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/*
246*4882a593Smuzhiyun 	 * There is a race in the hardware that could cause us not to be notified
247*4882a593Smuzhiyun 	 * of an interrupt completion if we do not take this step.  We will mask
248*4882a593Smuzhiyun 	 * then unmask the error interrupts so if there was another interrupt
249*4882a593Smuzhiyun 	 * pending we will be notified.
250*4882a593Smuzhiyun 	 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
251*4882a593Smuzhiyun 	writel(0xff, &ihost->smu_registers->interrupt_mask);
252*4882a593Smuzhiyun 	writel(0, &ihost->smu_registers->interrupt_mask);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	return false;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
sci_controller_task_completion(struct isci_host * ihost,u32 ent)257*4882a593Smuzhiyun static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	u32 index = SCU_GET_COMPLETION_INDEX(ent);
260*4882a593Smuzhiyun 	struct isci_request *ireq = ihost->reqs[index];
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Make sure that we really want to process this IO request */
263*4882a593Smuzhiyun 	if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
264*4882a593Smuzhiyun 	    ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
265*4882a593Smuzhiyun 	    ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
266*4882a593Smuzhiyun 		/* Yep this is a valid io request pass it along to the
267*4882a593Smuzhiyun 		 * io request handler
268*4882a593Smuzhiyun 		 */
269*4882a593Smuzhiyun 		sci_io_request_tc_completion(ireq, ent);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
sci_controller_sdma_completion(struct isci_host * ihost,u32 ent)272*4882a593Smuzhiyun static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	u32 index;
275*4882a593Smuzhiyun 	struct isci_request *ireq;
276*4882a593Smuzhiyun 	struct isci_remote_device *idev;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	index = SCU_GET_COMPLETION_INDEX(ent);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	switch (scu_get_command_request_type(ent)) {
281*4882a593Smuzhiyun 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
282*4882a593Smuzhiyun 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
283*4882a593Smuzhiyun 		ireq = ihost->reqs[index];
284*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
285*4882a593Smuzhiyun 			 __func__, ent, ireq);
286*4882a593Smuzhiyun 		/* @todo For a post TC operation we need to fail the IO
287*4882a593Smuzhiyun 		 * request
288*4882a593Smuzhiyun 		 */
289*4882a593Smuzhiyun 		break;
290*4882a593Smuzhiyun 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
291*4882a593Smuzhiyun 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
292*4882a593Smuzhiyun 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
293*4882a593Smuzhiyun 		idev = ihost->device_table[index];
294*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
295*4882a593Smuzhiyun 			 __func__, ent, idev);
296*4882a593Smuzhiyun 		/* @todo For a port RNC operation we need to fail the
297*4882a593Smuzhiyun 		 * device
298*4882a593Smuzhiyun 		 */
299*4882a593Smuzhiyun 		break;
300*4882a593Smuzhiyun 	default:
301*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
302*4882a593Smuzhiyun 			 __func__, ent);
303*4882a593Smuzhiyun 		break;
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun 
sci_controller_unsolicited_frame(struct isci_host * ihost,u32 ent)307*4882a593Smuzhiyun static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	u32 index;
310*4882a593Smuzhiyun 	u32 frame_index;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	struct scu_unsolicited_frame_header *frame_header;
313*4882a593Smuzhiyun 	struct isci_phy *iphy;
314*4882a593Smuzhiyun 	struct isci_remote_device *idev;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	enum sci_status result = SCI_FAILURE;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	frame_index = SCU_GET_FRAME_INDEX(ent);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	frame_header = ihost->uf_control.buffers.array[frame_index].header;
321*4882a593Smuzhiyun 	ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	if (SCU_GET_FRAME_ERROR(ent)) {
324*4882a593Smuzhiyun 		/*
325*4882a593Smuzhiyun 		 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
326*4882a593Smuzhiyun 		 * /       this cause a problem? We expect the phy initialization will
327*4882a593Smuzhiyun 		 * /       fail if there is an error in the frame. */
328*4882a593Smuzhiyun 		sci_controller_release_frame(ihost, frame_index);
329*4882a593Smuzhiyun 		return;
330*4882a593Smuzhiyun 	}
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	if (frame_header->is_address_frame) {
333*4882a593Smuzhiyun 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
334*4882a593Smuzhiyun 		iphy = &ihost->phys[index];
335*4882a593Smuzhiyun 		result = sci_phy_frame_handler(iphy, frame_index);
336*4882a593Smuzhiyun 	} else {
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 		index = SCU_GET_COMPLETION_INDEX(ent);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 		if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
341*4882a593Smuzhiyun 			/*
342*4882a593Smuzhiyun 			 * This is a signature fis or a frame from a direct attached SATA
343*4882a593Smuzhiyun 			 * device that has not yet been created.  In either case forwared
344*4882a593Smuzhiyun 			 * the frame to the PE and let it take care of the frame data. */
345*4882a593Smuzhiyun 			index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
346*4882a593Smuzhiyun 			iphy = &ihost->phys[index];
347*4882a593Smuzhiyun 			result = sci_phy_frame_handler(iphy, frame_index);
348*4882a593Smuzhiyun 		} else {
349*4882a593Smuzhiyun 			if (index < ihost->remote_node_entries)
350*4882a593Smuzhiyun 				idev = ihost->device_table[index];
351*4882a593Smuzhiyun 			else
352*4882a593Smuzhiyun 				idev = NULL;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 			if (idev != NULL)
355*4882a593Smuzhiyun 				result = sci_remote_device_frame_handler(idev, frame_index);
356*4882a593Smuzhiyun 			else
357*4882a593Smuzhiyun 				sci_controller_release_frame(ihost, frame_index);
358*4882a593Smuzhiyun 		}
359*4882a593Smuzhiyun 	}
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (result != SCI_SUCCESS) {
362*4882a593Smuzhiyun 		/*
363*4882a593Smuzhiyun 		 * / @todo Is there any reason to report some additional error message
364*4882a593Smuzhiyun 		 * /       when we get this failure notifiction? */
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
sci_controller_event_completion(struct isci_host * ihost,u32 ent)368*4882a593Smuzhiyun static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	struct isci_remote_device *idev;
371*4882a593Smuzhiyun 	struct isci_request *ireq;
372*4882a593Smuzhiyun 	struct isci_phy *iphy;
373*4882a593Smuzhiyun 	u32 index;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	index = SCU_GET_COMPLETION_INDEX(ent);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	switch (scu_get_event_type(ent)) {
378*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
379*4882a593Smuzhiyun 		/* / @todo The driver did something wrong and we need to fix the condtion. */
380*4882a593Smuzhiyun 		dev_err(&ihost->pdev->dev,
381*4882a593Smuzhiyun 			"%s: SCIC Controller 0x%p received SMU command error "
382*4882a593Smuzhiyun 			"0x%x\n",
383*4882a593Smuzhiyun 			__func__,
384*4882a593Smuzhiyun 			ihost,
385*4882a593Smuzhiyun 			ent);
386*4882a593Smuzhiyun 		break;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
389*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_SMU_ERROR:
390*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
391*4882a593Smuzhiyun 		/*
392*4882a593Smuzhiyun 		 * / @todo This is a hardware failure and its likely that we want to
393*4882a593Smuzhiyun 		 * /       reset the controller. */
394*4882a593Smuzhiyun 		dev_err(&ihost->pdev->dev,
395*4882a593Smuzhiyun 			"%s: SCIC Controller 0x%p received fatal controller "
396*4882a593Smuzhiyun 			"event  0x%x\n",
397*4882a593Smuzhiyun 			__func__,
398*4882a593Smuzhiyun 			ihost,
399*4882a593Smuzhiyun 			ent);
400*4882a593Smuzhiyun 		break;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_TRANSPORT_ERROR:
403*4882a593Smuzhiyun 		ireq = ihost->reqs[index];
404*4882a593Smuzhiyun 		sci_io_request_event_handler(ireq, ent);
405*4882a593Smuzhiyun 		break;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
408*4882a593Smuzhiyun 		switch (scu_get_event_specifier(ent)) {
409*4882a593Smuzhiyun 		case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
410*4882a593Smuzhiyun 		case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
411*4882a593Smuzhiyun 			ireq = ihost->reqs[index];
412*4882a593Smuzhiyun 			if (ireq != NULL)
413*4882a593Smuzhiyun 				sci_io_request_event_handler(ireq, ent);
414*4882a593Smuzhiyun 			else
415*4882a593Smuzhiyun 				dev_warn(&ihost->pdev->dev,
416*4882a593Smuzhiyun 					 "%s: SCIC Controller 0x%p received "
417*4882a593Smuzhiyun 					 "event 0x%x for io request object "
418*4882a593Smuzhiyun 					 "that doesnt exist.\n",
419*4882a593Smuzhiyun 					 __func__,
420*4882a593Smuzhiyun 					 ihost,
421*4882a593Smuzhiyun 					 ent);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 			break;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 		case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
426*4882a593Smuzhiyun 			idev = ihost->device_table[index];
427*4882a593Smuzhiyun 			if (idev != NULL)
428*4882a593Smuzhiyun 				sci_remote_device_event_handler(idev, ent);
429*4882a593Smuzhiyun 			else
430*4882a593Smuzhiyun 				dev_warn(&ihost->pdev->dev,
431*4882a593Smuzhiyun 					 "%s: SCIC Controller 0x%p received "
432*4882a593Smuzhiyun 					 "event 0x%x for remote device object "
433*4882a593Smuzhiyun 					 "that doesnt exist.\n",
434*4882a593Smuzhiyun 					 __func__,
435*4882a593Smuzhiyun 					 ihost,
436*4882a593Smuzhiyun 					 ent);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 			break;
439*4882a593Smuzhiyun 		}
440*4882a593Smuzhiyun 		break;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_BROADCAST_CHANGE:
443*4882a593Smuzhiyun 	/*
444*4882a593Smuzhiyun 	 * direct the broadcast change event to the phy first and then let
445*4882a593Smuzhiyun 	 * the phy redirect the broadcast change to the port object */
446*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_ERR_CNT_EVENT:
447*4882a593Smuzhiyun 	/*
448*4882a593Smuzhiyun 	 * direct error counter event to the phy object since that is where
449*4882a593Smuzhiyun 	 * we get the event notification.  This is a type 4 event. */
450*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_OSSP_EVENT:
451*4882a593Smuzhiyun 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
452*4882a593Smuzhiyun 		iphy = &ihost->phys[index];
453*4882a593Smuzhiyun 		sci_phy_event_handler(iphy, ent);
454*4882a593Smuzhiyun 		break;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
457*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
458*4882a593Smuzhiyun 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
459*4882a593Smuzhiyun 		if (index < ihost->remote_node_entries) {
460*4882a593Smuzhiyun 			idev = ihost->device_table[index];
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 			if (idev != NULL)
463*4882a593Smuzhiyun 				sci_remote_device_event_handler(idev, ent);
464*4882a593Smuzhiyun 		} else
465*4882a593Smuzhiyun 			dev_err(&ihost->pdev->dev,
466*4882a593Smuzhiyun 				"%s: SCIC Controller 0x%p received event 0x%x "
467*4882a593Smuzhiyun 				"for remote device object 0x%0x that doesnt "
468*4882a593Smuzhiyun 				"exist.\n",
469*4882a593Smuzhiyun 				__func__,
470*4882a593Smuzhiyun 				ihost,
471*4882a593Smuzhiyun 				ent,
472*4882a593Smuzhiyun 				index);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 		break;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	default:
477*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev,
478*4882a593Smuzhiyun 			 "%s: SCIC Controller received unknown event code %x\n",
479*4882a593Smuzhiyun 			 __func__,
480*4882a593Smuzhiyun 			 ent);
481*4882a593Smuzhiyun 		break;
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
sci_controller_process_completions(struct isci_host * ihost)485*4882a593Smuzhiyun static void sci_controller_process_completions(struct isci_host *ihost)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun 	u32 completion_count = 0;
488*4882a593Smuzhiyun 	u32 ent;
489*4882a593Smuzhiyun 	u32 get_index;
490*4882a593Smuzhiyun 	u32 get_cycle;
491*4882a593Smuzhiyun 	u32 event_get;
492*4882a593Smuzhiyun 	u32 event_cycle;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	dev_dbg(&ihost->pdev->dev,
495*4882a593Smuzhiyun 		"%s: completion queue beginning get:0x%08x\n",
496*4882a593Smuzhiyun 		__func__,
497*4882a593Smuzhiyun 		ihost->completion_queue_get);
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	/* Get the component parts of the completion queue */
500*4882a593Smuzhiyun 	get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
501*4882a593Smuzhiyun 	get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
504*4882a593Smuzhiyun 	event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	while (
507*4882a593Smuzhiyun 		NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
508*4882a593Smuzhiyun 		== COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
509*4882a593Smuzhiyun 		) {
510*4882a593Smuzhiyun 		completion_count++;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 		ent = ihost->completion_queue[get_index];
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 		/* increment the get pointer and check for rollover to toggle the cycle bit */
515*4882a593Smuzhiyun 		get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
516*4882a593Smuzhiyun 			     (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
517*4882a593Smuzhiyun 		get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 		dev_dbg(&ihost->pdev->dev,
520*4882a593Smuzhiyun 			"%s: completion queue entry:0x%08x\n",
521*4882a593Smuzhiyun 			__func__,
522*4882a593Smuzhiyun 			ent);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 		switch (SCU_GET_COMPLETION_TYPE(ent)) {
525*4882a593Smuzhiyun 		case SCU_COMPLETION_TYPE_TASK:
526*4882a593Smuzhiyun 			sci_controller_task_completion(ihost, ent);
527*4882a593Smuzhiyun 			break;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 		case SCU_COMPLETION_TYPE_SDMA:
530*4882a593Smuzhiyun 			sci_controller_sdma_completion(ihost, ent);
531*4882a593Smuzhiyun 			break;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 		case SCU_COMPLETION_TYPE_UFI:
534*4882a593Smuzhiyun 			sci_controller_unsolicited_frame(ihost, ent);
535*4882a593Smuzhiyun 			break;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 		case SCU_COMPLETION_TYPE_EVENT:
538*4882a593Smuzhiyun 			sci_controller_event_completion(ihost, ent);
539*4882a593Smuzhiyun 			break;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		case SCU_COMPLETION_TYPE_NOTIFY: {
542*4882a593Smuzhiyun 			event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
543*4882a593Smuzhiyun 				       (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
544*4882a593Smuzhiyun 			event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 			sci_controller_event_completion(ihost, ent);
547*4882a593Smuzhiyun 			break;
548*4882a593Smuzhiyun 		}
549*4882a593Smuzhiyun 		default:
550*4882a593Smuzhiyun 			dev_warn(&ihost->pdev->dev,
551*4882a593Smuzhiyun 				 "%s: SCIC Controller received unknown "
552*4882a593Smuzhiyun 				 "completion type %x\n",
553*4882a593Smuzhiyun 				 __func__,
554*4882a593Smuzhiyun 				 ent);
555*4882a593Smuzhiyun 			break;
556*4882a593Smuzhiyun 		}
557*4882a593Smuzhiyun 	}
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	/* Update the get register if we completed one or more entries */
560*4882a593Smuzhiyun 	if (completion_count > 0) {
561*4882a593Smuzhiyun 		ihost->completion_queue_get =
562*4882a593Smuzhiyun 			SMU_CQGR_GEN_BIT(ENABLE) |
563*4882a593Smuzhiyun 			SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
564*4882a593Smuzhiyun 			event_cycle |
565*4882a593Smuzhiyun 			SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
566*4882a593Smuzhiyun 			get_cycle |
567*4882a593Smuzhiyun 			SMU_CQGR_GEN_VAL(POINTER, get_index);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 		writel(ihost->completion_queue_get,
570*4882a593Smuzhiyun 		       &ihost->smu_registers->completion_queue_get);
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	dev_dbg(&ihost->pdev->dev,
575*4882a593Smuzhiyun 		"%s: completion queue ending get:0x%08x\n",
576*4882a593Smuzhiyun 		__func__,
577*4882a593Smuzhiyun 		ihost->completion_queue_get);
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun 
sci_controller_error_handler(struct isci_host * ihost)581*4882a593Smuzhiyun static void sci_controller_error_handler(struct isci_host *ihost)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	u32 interrupt_status;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	interrupt_status =
586*4882a593Smuzhiyun 		readl(&ihost->smu_registers->interrupt_status);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
589*4882a593Smuzhiyun 	    sci_controller_completion_queue_has_entries(ihost)) {
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 		sci_controller_process_completions(ihost);
592*4882a593Smuzhiyun 		writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
593*4882a593Smuzhiyun 	} else {
594*4882a593Smuzhiyun 		dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
595*4882a593Smuzhiyun 			interrupt_status);
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 		sci_change_state(&ihost->sm, SCIC_FAILED);
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		return;
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	/* If we dont process any completions I am not sure that we want to do this.
603*4882a593Smuzhiyun 	 * We are in the middle of a hardware fault and should probably be reset.
604*4882a593Smuzhiyun 	 */
605*4882a593Smuzhiyun 	writel(0, &ihost->smu_registers->interrupt_mask);
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun 
isci_intx_isr(int vec,void * data)608*4882a593Smuzhiyun irqreturn_t isci_intx_isr(int vec, void *data)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	irqreturn_t ret = IRQ_NONE;
611*4882a593Smuzhiyun 	struct isci_host *ihost = data;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	if (sci_controller_isr(ihost)) {
614*4882a593Smuzhiyun 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
615*4882a593Smuzhiyun 		tasklet_schedule(&ihost->completion_tasklet);
616*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
617*4882a593Smuzhiyun 	} else if (sci_controller_error_isr(ihost)) {
618*4882a593Smuzhiyun 		spin_lock(&ihost->scic_lock);
619*4882a593Smuzhiyun 		sci_controller_error_handler(ihost);
620*4882a593Smuzhiyun 		spin_unlock(&ihost->scic_lock);
621*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
622*4882a593Smuzhiyun 	}
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	return ret;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
isci_error_isr(int vec,void * data)627*4882a593Smuzhiyun irqreturn_t isci_error_isr(int vec, void *data)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	struct isci_host *ihost = data;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	if (sci_controller_error_isr(ihost))
632*4882a593Smuzhiyun 		sci_controller_error_handler(ihost);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	return IRQ_HANDLED;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun /**
638*4882a593Smuzhiyun  * isci_host_start_complete() - This function is called by the core library,
639*4882a593Smuzhiyun  *    through the ISCI Module, to indicate controller start status.
640*4882a593Smuzhiyun  * @isci_host: This parameter specifies the ISCI host object
641*4882a593Smuzhiyun  * @completion_status: This parameter specifies the completion status from the
642*4882a593Smuzhiyun  *    core library.
643*4882a593Smuzhiyun  *
644*4882a593Smuzhiyun  */
isci_host_start_complete(struct isci_host * ihost,enum sci_status completion_status)645*4882a593Smuzhiyun static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
646*4882a593Smuzhiyun {
647*4882a593Smuzhiyun 	if (completion_status != SCI_SUCCESS)
648*4882a593Smuzhiyun 		dev_info(&ihost->pdev->dev,
649*4882a593Smuzhiyun 			"controller start timed out, continuing...\n");
650*4882a593Smuzhiyun 	clear_bit(IHOST_START_PENDING, &ihost->flags);
651*4882a593Smuzhiyun 	wake_up(&ihost->eventq);
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun 
isci_host_scan_finished(struct Scsi_Host * shost,unsigned long time)654*4882a593Smuzhiyun int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
657*4882a593Smuzhiyun 	struct isci_host *ihost = ha->lldd_ha;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
660*4882a593Smuzhiyun 		return 0;
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	sas_drain_work(ha);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	return 1;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun /**
668*4882a593Smuzhiyun  * sci_controller_get_suggested_start_timeout() - This method returns the
669*4882a593Smuzhiyun  *    suggested sci_controller_start() timeout amount.  The user is free to
670*4882a593Smuzhiyun  *    use any timeout value, but this method provides the suggested minimum
671*4882a593Smuzhiyun  *    start timeout value.  The returned value is based upon empirical
672*4882a593Smuzhiyun  *    information determined as a result of interoperability testing.
673*4882a593Smuzhiyun  * @controller: the handle to the controller object for which to return the
674*4882a593Smuzhiyun  *    suggested start timeout.
675*4882a593Smuzhiyun  *
676*4882a593Smuzhiyun  * This method returns the number of milliseconds for the suggested start
677*4882a593Smuzhiyun  * operation timeout.
678*4882a593Smuzhiyun  */
sci_controller_get_suggested_start_timeout(struct isci_host * ihost)679*4882a593Smuzhiyun static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun 	/* Validate the user supplied parameters. */
682*4882a593Smuzhiyun 	if (!ihost)
683*4882a593Smuzhiyun 		return 0;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	/*
686*4882a593Smuzhiyun 	 * The suggested minimum timeout value for a controller start operation:
687*4882a593Smuzhiyun 	 *
688*4882a593Smuzhiyun 	 *     Signature FIS Timeout
689*4882a593Smuzhiyun 	 *   + Phy Start Timeout
690*4882a593Smuzhiyun 	 *   + Number of Phy Spin Up Intervals
691*4882a593Smuzhiyun 	 *   ---------------------------------
692*4882a593Smuzhiyun 	 *   Number of milliseconds for the controller start operation.
693*4882a593Smuzhiyun 	 *
694*4882a593Smuzhiyun 	 * NOTE: The number of phy spin up intervals will be equivalent
695*4882a593Smuzhiyun 	 *       to the number of phys divided by the number phys allowed
696*4882a593Smuzhiyun 	 *       per interval - 1 (once OEM parameters are supported).
697*4882a593Smuzhiyun 	 *       Currently we assume only 1 phy per interval. */
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
700*4882a593Smuzhiyun 		+ SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
701*4882a593Smuzhiyun 		+ ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun 
sci_controller_enable_interrupts(struct isci_host * ihost)704*4882a593Smuzhiyun static void sci_controller_enable_interrupts(struct isci_host *ihost)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun 	set_bit(IHOST_IRQ_ENABLED, &ihost->flags);
707*4882a593Smuzhiyun 	writel(0, &ihost->smu_registers->interrupt_mask);
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
sci_controller_disable_interrupts(struct isci_host * ihost)710*4882a593Smuzhiyun void sci_controller_disable_interrupts(struct isci_host *ihost)
711*4882a593Smuzhiyun {
712*4882a593Smuzhiyun 	clear_bit(IHOST_IRQ_ENABLED, &ihost->flags);
713*4882a593Smuzhiyun 	writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
714*4882a593Smuzhiyun 	readl(&ihost->smu_registers->interrupt_mask); /* flush */
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun 
sci_controller_enable_port_task_scheduler(struct isci_host * ihost)717*4882a593Smuzhiyun static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	u32 port_task_scheduler_value;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	port_task_scheduler_value =
722*4882a593Smuzhiyun 		readl(&ihost->scu_registers->peg0.ptsg.control);
723*4882a593Smuzhiyun 	port_task_scheduler_value |=
724*4882a593Smuzhiyun 		(SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
725*4882a593Smuzhiyun 		 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
726*4882a593Smuzhiyun 	writel(port_task_scheduler_value,
727*4882a593Smuzhiyun 	       &ihost->scu_registers->peg0.ptsg.control);
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
sci_controller_assign_task_entries(struct isci_host * ihost)730*4882a593Smuzhiyun static void sci_controller_assign_task_entries(struct isci_host *ihost)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	u32 task_assignment;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	/*
735*4882a593Smuzhiyun 	 * Assign all the TCs to function 0
736*4882a593Smuzhiyun 	 * TODO: Do we actually need to read this register to write it back?
737*4882a593Smuzhiyun 	 */
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	task_assignment =
740*4882a593Smuzhiyun 		readl(&ihost->smu_registers->task_context_assignment[0]);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
743*4882a593Smuzhiyun 		(SMU_TCA_GEN_VAL(ENDING,  ihost->task_context_entries - 1)) |
744*4882a593Smuzhiyun 		(SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	writel(task_assignment,
747*4882a593Smuzhiyun 		&ihost->smu_registers->task_context_assignment[0]);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun 
sci_controller_initialize_completion_queue(struct isci_host * ihost)751*4882a593Smuzhiyun static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	u32 index;
754*4882a593Smuzhiyun 	u32 completion_queue_control_value;
755*4882a593Smuzhiyun 	u32 completion_queue_get_value;
756*4882a593Smuzhiyun 	u32 completion_queue_put_value;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	ihost->completion_queue_get = 0;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	completion_queue_control_value =
761*4882a593Smuzhiyun 		(SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
762*4882a593Smuzhiyun 		 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	writel(completion_queue_control_value,
765*4882a593Smuzhiyun 	       &ihost->smu_registers->completion_queue_control);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	/* Set the completion queue get pointer and enable the queue */
769*4882a593Smuzhiyun 	completion_queue_get_value = (
770*4882a593Smuzhiyun 		(SMU_CQGR_GEN_VAL(POINTER, 0))
771*4882a593Smuzhiyun 		| (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
772*4882a593Smuzhiyun 		| (SMU_CQGR_GEN_BIT(ENABLE))
773*4882a593Smuzhiyun 		| (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
774*4882a593Smuzhiyun 		);
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	writel(completion_queue_get_value,
777*4882a593Smuzhiyun 	       &ihost->smu_registers->completion_queue_get);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	/* Set the completion queue put pointer */
780*4882a593Smuzhiyun 	completion_queue_put_value = (
781*4882a593Smuzhiyun 		(SMU_CQPR_GEN_VAL(POINTER, 0))
782*4882a593Smuzhiyun 		| (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
783*4882a593Smuzhiyun 		);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	writel(completion_queue_put_value,
786*4882a593Smuzhiyun 	       &ihost->smu_registers->completion_queue_put);
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	/* Initialize the cycle bit of the completion queue entries */
789*4882a593Smuzhiyun 	for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
790*4882a593Smuzhiyun 		/*
791*4882a593Smuzhiyun 		 * If get.cycle_bit != completion_queue.cycle_bit
792*4882a593Smuzhiyun 		 * its not a valid completion queue entry
793*4882a593Smuzhiyun 		 * so at system start all entries are invalid */
794*4882a593Smuzhiyun 		ihost->completion_queue[index] = 0x80000000;
795*4882a593Smuzhiyun 	}
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
sci_controller_initialize_unsolicited_frame_queue(struct isci_host * ihost)798*4882a593Smuzhiyun static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	u32 frame_queue_control_value;
801*4882a593Smuzhiyun 	u32 frame_queue_get_value;
802*4882a593Smuzhiyun 	u32 frame_queue_put_value;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	/* Write the queue size */
805*4882a593Smuzhiyun 	frame_queue_control_value =
806*4882a593Smuzhiyun 		SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	writel(frame_queue_control_value,
809*4882a593Smuzhiyun 	       &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	/* Setup the get pointer for the unsolicited frame queue */
812*4882a593Smuzhiyun 	frame_queue_get_value = (
813*4882a593Smuzhiyun 		SCU_UFQGP_GEN_VAL(POINTER, 0)
814*4882a593Smuzhiyun 		|  SCU_UFQGP_GEN_BIT(ENABLE_BIT)
815*4882a593Smuzhiyun 		);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	writel(frame_queue_get_value,
818*4882a593Smuzhiyun 	       &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
819*4882a593Smuzhiyun 	/* Setup the put pointer for the unsolicited frame queue */
820*4882a593Smuzhiyun 	frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
821*4882a593Smuzhiyun 	writel(frame_queue_put_value,
822*4882a593Smuzhiyun 	       &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun 
sci_controller_transition_to_ready(struct isci_host * ihost,enum sci_status status)825*4882a593Smuzhiyun void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	if (ihost->sm.current_state_id == SCIC_STARTING) {
828*4882a593Smuzhiyun 		/*
829*4882a593Smuzhiyun 		 * We move into the ready state, because some of the phys/ports
830*4882a593Smuzhiyun 		 * may be up and operational.
831*4882a593Smuzhiyun 		 */
832*4882a593Smuzhiyun 		sci_change_state(&ihost->sm, SCIC_READY);
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 		isci_host_start_complete(ihost, status);
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun 
is_phy_starting(struct isci_phy * iphy)838*4882a593Smuzhiyun static bool is_phy_starting(struct isci_phy *iphy)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun 	enum sci_phy_states state;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	state = iphy->sm.current_state_id;
843*4882a593Smuzhiyun 	switch (state) {
844*4882a593Smuzhiyun 	case SCI_PHY_STARTING:
845*4882a593Smuzhiyun 	case SCI_PHY_SUB_INITIAL:
846*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
847*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_IAF_UF:
848*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
849*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
850*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
851*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
852*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_OSSP_EN:
853*4882a593Smuzhiyun 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
854*4882a593Smuzhiyun 	case SCI_PHY_SUB_FINAL:
855*4882a593Smuzhiyun 		return true;
856*4882a593Smuzhiyun 	default:
857*4882a593Smuzhiyun 		return false;
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
is_controller_start_complete(struct isci_host * ihost)861*4882a593Smuzhiyun bool is_controller_start_complete(struct isci_host *ihost)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun 	int i;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PHYS; i++) {
866*4882a593Smuzhiyun 		struct isci_phy *iphy = &ihost->phys[i];
867*4882a593Smuzhiyun 		u32 state = iphy->sm.current_state_id;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 		/* in apc mode we need to check every phy, in
870*4882a593Smuzhiyun 		 * mpc mode we only need to check phys that have
871*4882a593Smuzhiyun 		 * been configured into a port
872*4882a593Smuzhiyun 		 */
873*4882a593Smuzhiyun 		if (is_port_config_apc(ihost))
874*4882a593Smuzhiyun 			/* pass */;
875*4882a593Smuzhiyun 		else if (!phy_get_non_dummy_port(iphy))
876*4882a593Smuzhiyun 			continue;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 		/* The controller start operation is complete iff:
879*4882a593Smuzhiyun 		 * - all links have been given an opportunity to start
880*4882a593Smuzhiyun 		 * - have no indication of a connected device
881*4882a593Smuzhiyun 		 * - have an indication of a connected device and it has
882*4882a593Smuzhiyun 		 *   finished the link training process.
883*4882a593Smuzhiyun 		 */
884*4882a593Smuzhiyun 		if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
885*4882a593Smuzhiyun 		    (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
886*4882a593Smuzhiyun 		    (iphy->is_in_link_training == true && is_phy_starting(iphy)) ||
887*4882a593Smuzhiyun 		    (ihost->port_agent.phy_ready_mask != ihost->port_agent.phy_configured_mask))
888*4882a593Smuzhiyun 			return false;
889*4882a593Smuzhiyun 	}
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	return true;
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun /**
895*4882a593Smuzhiyun  * sci_controller_start_next_phy - start phy
896*4882a593Smuzhiyun  * @scic: controller
897*4882a593Smuzhiyun  *
898*4882a593Smuzhiyun  * If all the phys have been started, then attempt to transition the
899*4882a593Smuzhiyun  * controller to the READY state and inform the user
900*4882a593Smuzhiyun  * (sci_cb_controller_start_complete()).
901*4882a593Smuzhiyun  */
sci_controller_start_next_phy(struct isci_host * ihost)902*4882a593Smuzhiyun static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	struct sci_oem_params *oem = &ihost->oem_parameters;
905*4882a593Smuzhiyun 	struct isci_phy *iphy;
906*4882a593Smuzhiyun 	enum sci_status status;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	status = SCI_SUCCESS;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	if (ihost->phy_startup_timer_pending)
911*4882a593Smuzhiyun 		return status;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
914*4882a593Smuzhiyun 		if (is_controller_start_complete(ihost)) {
915*4882a593Smuzhiyun 			sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
916*4882a593Smuzhiyun 			sci_del_timer(&ihost->phy_timer);
917*4882a593Smuzhiyun 			ihost->phy_startup_timer_pending = false;
918*4882a593Smuzhiyun 		}
919*4882a593Smuzhiyun 	} else {
920*4882a593Smuzhiyun 		iphy = &ihost->phys[ihost->next_phy_to_start];
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 		if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
923*4882a593Smuzhiyun 			if (phy_get_non_dummy_port(iphy) == NULL) {
924*4882a593Smuzhiyun 				ihost->next_phy_to_start++;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 				/* Caution recursion ahead be forwarned
927*4882a593Smuzhiyun 				 *
928*4882a593Smuzhiyun 				 * The PHY was never added to a PORT in MPC mode
929*4882a593Smuzhiyun 				 * so start the next phy in sequence This phy
930*4882a593Smuzhiyun 				 * will never go link up and will not draw power
931*4882a593Smuzhiyun 				 * the OEM parameters either configured the phy
932*4882a593Smuzhiyun 				 * incorrectly for the PORT or it was never
933*4882a593Smuzhiyun 				 * assigned to a PORT
934*4882a593Smuzhiyun 				 */
935*4882a593Smuzhiyun 				return sci_controller_start_next_phy(ihost);
936*4882a593Smuzhiyun 			}
937*4882a593Smuzhiyun 		}
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 		status = sci_phy_start(iphy);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 		if (status == SCI_SUCCESS) {
942*4882a593Smuzhiyun 			sci_mod_timer(&ihost->phy_timer,
943*4882a593Smuzhiyun 				      SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
944*4882a593Smuzhiyun 			ihost->phy_startup_timer_pending = true;
945*4882a593Smuzhiyun 		} else {
946*4882a593Smuzhiyun 			dev_warn(&ihost->pdev->dev,
947*4882a593Smuzhiyun 				 "%s: Controller stop operation failed "
948*4882a593Smuzhiyun 				 "to stop phy %d because of status "
949*4882a593Smuzhiyun 				 "%d.\n",
950*4882a593Smuzhiyun 				 __func__,
951*4882a593Smuzhiyun 				 ihost->phys[ihost->next_phy_to_start].phy_index,
952*4882a593Smuzhiyun 				 status);
953*4882a593Smuzhiyun 		}
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 		ihost->next_phy_to_start++;
956*4882a593Smuzhiyun 	}
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	return status;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun 
phy_startup_timeout(struct timer_list * t)961*4882a593Smuzhiyun static void phy_startup_timeout(struct timer_list *t)
962*4882a593Smuzhiyun {
963*4882a593Smuzhiyun 	struct sci_timer *tmr = from_timer(tmr, t, timer);
964*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
965*4882a593Smuzhiyun 	unsigned long flags;
966*4882a593Smuzhiyun 	enum sci_status status;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	spin_lock_irqsave(&ihost->scic_lock, flags);
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	if (tmr->cancel)
971*4882a593Smuzhiyun 		goto done;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	ihost->phy_startup_timer_pending = false;
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	do {
976*4882a593Smuzhiyun 		status = sci_controller_start_next_phy(ihost);
977*4882a593Smuzhiyun 	} while (status != SCI_SUCCESS);
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun done:
980*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun 
isci_tci_active(struct isci_host * ihost)983*4882a593Smuzhiyun static u16 isci_tci_active(struct isci_host *ihost)
984*4882a593Smuzhiyun {
985*4882a593Smuzhiyun 	return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
sci_controller_start(struct isci_host * ihost,u32 timeout)988*4882a593Smuzhiyun static enum sci_status sci_controller_start(struct isci_host *ihost,
989*4882a593Smuzhiyun 					     u32 timeout)
990*4882a593Smuzhiyun {
991*4882a593Smuzhiyun 	enum sci_status result;
992*4882a593Smuzhiyun 	u16 index;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
995*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
996*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
997*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
998*4882a593Smuzhiyun 	}
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	/* Build the TCi free pool */
1001*4882a593Smuzhiyun 	BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1002*4882a593Smuzhiyun 	ihost->tci_head = 0;
1003*4882a593Smuzhiyun 	ihost->tci_tail = 0;
1004*4882a593Smuzhiyun 	for (index = 0; index < ihost->task_context_entries; index++)
1005*4882a593Smuzhiyun 		isci_tci_free(ihost, index);
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	/* Build the RNi free pool */
1008*4882a593Smuzhiyun 	sci_remote_node_table_initialize(&ihost->available_remote_nodes,
1009*4882a593Smuzhiyun 					 ihost->remote_node_entries);
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	/*
1012*4882a593Smuzhiyun 	 * Before anything else lets make sure we will not be
1013*4882a593Smuzhiyun 	 * interrupted by the hardware.
1014*4882a593Smuzhiyun 	 */
1015*4882a593Smuzhiyun 	sci_controller_disable_interrupts(ihost);
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	/* Enable the port task scheduler */
1018*4882a593Smuzhiyun 	sci_controller_enable_port_task_scheduler(ihost);
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	/* Assign all the task entries to ihost physical function */
1021*4882a593Smuzhiyun 	sci_controller_assign_task_entries(ihost);
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	/* Now initialize the completion queue */
1024*4882a593Smuzhiyun 	sci_controller_initialize_completion_queue(ihost);
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 	/* Initialize the unsolicited frame queue for use */
1027*4882a593Smuzhiyun 	sci_controller_initialize_unsolicited_frame_queue(ihost);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	/* Start all of the ports on this controller */
1030*4882a593Smuzhiyun 	for (index = 0; index < ihost->logical_port_entries; index++) {
1031*4882a593Smuzhiyun 		struct isci_port *iport = &ihost->ports[index];
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 		result = sci_port_start(iport);
1034*4882a593Smuzhiyun 		if (result)
1035*4882a593Smuzhiyun 			return result;
1036*4882a593Smuzhiyun 	}
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	sci_controller_start_next_phy(ihost);
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	sci_mod_timer(&ihost->timer, timeout);
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	sci_change_state(&ihost->sm, SCIC_STARTING);
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	return SCI_SUCCESS;
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun 
isci_host_start(struct Scsi_Host * shost)1047*4882a593Smuzhiyun void isci_host_start(struct Scsi_Host *shost)
1048*4882a593Smuzhiyun {
1049*4882a593Smuzhiyun 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
1050*4882a593Smuzhiyun 	unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	set_bit(IHOST_START_PENDING, &ihost->flags);
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	spin_lock_irq(&ihost->scic_lock);
1055*4882a593Smuzhiyun 	sci_controller_start(ihost, tmo);
1056*4882a593Smuzhiyun 	sci_controller_enable_interrupts(ihost);
1057*4882a593Smuzhiyun 	spin_unlock_irq(&ihost->scic_lock);
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun 
isci_host_stop_complete(struct isci_host * ihost)1060*4882a593Smuzhiyun static void isci_host_stop_complete(struct isci_host *ihost)
1061*4882a593Smuzhiyun {
1062*4882a593Smuzhiyun 	sci_controller_disable_interrupts(ihost);
1063*4882a593Smuzhiyun 	clear_bit(IHOST_STOP_PENDING, &ihost->flags);
1064*4882a593Smuzhiyun 	wake_up(&ihost->eventq);
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun 
sci_controller_completion_handler(struct isci_host * ihost)1067*4882a593Smuzhiyun static void sci_controller_completion_handler(struct isci_host *ihost)
1068*4882a593Smuzhiyun {
1069*4882a593Smuzhiyun 	/* Empty out the completion queue */
1070*4882a593Smuzhiyun 	if (sci_controller_completion_queue_has_entries(ihost))
1071*4882a593Smuzhiyun 		sci_controller_process_completions(ihost);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	/* Clear the interrupt and enable all interrupts again */
1074*4882a593Smuzhiyun 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
1075*4882a593Smuzhiyun 	/* Could we write the value of SMU_ISR_COMPLETION? */
1076*4882a593Smuzhiyun 	writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
1077*4882a593Smuzhiyun 	writel(0, &ihost->smu_registers->interrupt_mask);
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun 
ireq_done(struct isci_host * ihost,struct isci_request * ireq,struct sas_task * task)1080*4882a593Smuzhiyun void ireq_done(struct isci_host *ihost, struct isci_request *ireq, struct sas_task *task)
1081*4882a593Smuzhiyun {
1082*4882a593Smuzhiyun 	if (!test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags) &&
1083*4882a593Smuzhiyun 	    !(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1084*4882a593Smuzhiyun 		if (test_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags)) {
1085*4882a593Smuzhiyun 			/* Normal notification (task_done) */
1086*4882a593Smuzhiyun 			dev_dbg(&ihost->pdev->dev,
1087*4882a593Smuzhiyun 				"%s: Normal - ireq/task = %p/%p\n",
1088*4882a593Smuzhiyun 				__func__, ireq, task);
1089*4882a593Smuzhiyun 			task->lldd_task = NULL;
1090*4882a593Smuzhiyun 			task->task_done(task);
1091*4882a593Smuzhiyun 		} else {
1092*4882a593Smuzhiyun 			dev_dbg(&ihost->pdev->dev,
1093*4882a593Smuzhiyun 				"%s: Error - ireq/task = %p/%p\n",
1094*4882a593Smuzhiyun 				__func__, ireq, task);
1095*4882a593Smuzhiyun 			if (sas_protocol_ata(task->task_proto))
1096*4882a593Smuzhiyun 				task->lldd_task = NULL;
1097*4882a593Smuzhiyun 			sas_task_abort(task);
1098*4882a593Smuzhiyun 		}
1099*4882a593Smuzhiyun 	} else
1100*4882a593Smuzhiyun 		task->lldd_task = NULL;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
1103*4882a593Smuzhiyun 		wake_up_all(&ihost->eventq);
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
1106*4882a593Smuzhiyun 		isci_free_tag(ihost, ireq->io_tag);
1107*4882a593Smuzhiyun }
1108*4882a593Smuzhiyun /**
1109*4882a593Smuzhiyun  * isci_host_completion_routine() - This function is the delayed service
1110*4882a593Smuzhiyun  *    routine that calls the sci core library's completion handler. It's
1111*4882a593Smuzhiyun  *    scheduled as a tasklet from the interrupt service routine when interrupts
1112*4882a593Smuzhiyun  *    in use, or set as the timeout function in polled mode.
1113*4882a593Smuzhiyun  * @data: This parameter specifies the ISCI host object
1114*4882a593Smuzhiyun  *
1115*4882a593Smuzhiyun  */
isci_host_completion_routine(unsigned long data)1116*4882a593Smuzhiyun void isci_host_completion_routine(unsigned long data)
1117*4882a593Smuzhiyun {
1118*4882a593Smuzhiyun 	struct isci_host *ihost = (struct isci_host *)data;
1119*4882a593Smuzhiyun 	u16 active;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	spin_lock_irq(&ihost->scic_lock);
1122*4882a593Smuzhiyun 	sci_controller_completion_handler(ihost);
1123*4882a593Smuzhiyun 	spin_unlock_irq(&ihost->scic_lock);
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	/*
1126*4882a593Smuzhiyun 	 * we subtract SCI_MAX_PORTS to account for the number of dummy TCs
1127*4882a593Smuzhiyun 	 * issued for hardware issue workaround
1128*4882a593Smuzhiyun 	 */
1129*4882a593Smuzhiyun 	active = isci_tci_active(ihost) - SCI_MAX_PORTS;
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	/*
1132*4882a593Smuzhiyun 	 * the coalesence timeout doubles at each encoding step, so
1133*4882a593Smuzhiyun 	 * update it based on the ilog2 value of the outstanding requests
1134*4882a593Smuzhiyun 	 */
1135*4882a593Smuzhiyun 	writel(SMU_ICC_GEN_VAL(NUMBER, active) |
1136*4882a593Smuzhiyun 	       SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
1137*4882a593Smuzhiyun 	       &ihost->smu_registers->interrupt_coalesce_control);
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun /**
1141*4882a593Smuzhiyun  * sci_controller_stop() - This method will stop an individual controller
1142*4882a593Smuzhiyun  *    object.This method will invoke the associated user callback upon
1143*4882a593Smuzhiyun  *    completion.  The completion callback is called when the following
1144*4882a593Smuzhiyun  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1145*4882a593Smuzhiyun  *    controller has been quiesced. This method will ensure that all IO
1146*4882a593Smuzhiyun  *    requests are quiesced, phys are stopped, and all additional operation by
1147*4882a593Smuzhiyun  *    the hardware is halted.
1148*4882a593Smuzhiyun  * @controller: the handle to the controller object to stop.
1149*4882a593Smuzhiyun  * @timeout: This parameter specifies the number of milliseconds in which the
1150*4882a593Smuzhiyun  *    stop operation should complete.
1151*4882a593Smuzhiyun  *
1152*4882a593Smuzhiyun  * The controller must be in the STARTED or STOPPED state. Indicate if the
1153*4882a593Smuzhiyun  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1154*4882a593Smuzhiyun  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1155*4882a593Smuzhiyun  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1156*4882a593Smuzhiyun  * controller is not either in the STARTED or STOPPED states.
1157*4882a593Smuzhiyun  */
sci_controller_stop(struct isci_host * ihost,u32 timeout)1158*4882a593Smuzhiyun static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_READY) {
1161*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
1162*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
1163*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
1164*4882a593Smuzhiyun 	}
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	sci_mod_timer(&ihost->timer, timeout);
1167*4882a593Smuzhiyun 	sci_change_state(&ihost->sm, SCIC_STOPPING);
1168*4882a593Smuzhiyun 	return SCI_SUCCESS;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /**
1172*4882a593Smuzhiyun  * sci_controller_reset() - This method will reset the supplied core
1173*4882a593Smuzhiyun  *    controller regardless of the state of said controller.  This operation is
1174*4882a593Smuzhiyun  *    considered destructive.  In other words, all current operations are wiped
1175*4882a593Smuzhiyun  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1176*4882a593Smuzhiyun  *    requests are not aborted or completed at the actual remote device.
1177*4882a593Smuzhiyun  * @controller: the handle to the controller object to reset.
1178*4882a593Smuzhiyun  *
1179*4882a593Smuzhiyun  * Indicate if the controller reset method succeeded or failed in some way.
1180*4882a593Smuzhiyun  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1181*4882a593Smuzhiyun  * the controller reset operation is unable to complete.
1182*4882a593Smuzhiyun  */
sci_controller_reset(struct isci_host * ihost)1183*4882a593Smuzhiyun static enum sci_status sci_controller_reset(struct isci_host *ihost)
1184*4882a593Smuzhiyun {
1185*4882a593Smuzhiyun 	switch (ihost->sm.current_state_id) {
1186*4882a593Smuzhiyun 	case SCIC_RESET:
1187*4882a593Smuzhiyun 	case SCIC_READY:
1188*4882a593Smuzhiyun 	case SCIC_STOPPING:
1189*4882a593Smuzhiyun 	case SCIC_FAILED:
1190*4882a593Smuzhiyun 		/*
1191*4882a593Smuzhiyun 		 * The reset operation is not a graceful cleanup, just
1192*4882a593Smuzhiyun 		 * perform the state transition.
1193*4882a593Smuzhiyun 		 */
1194*4882a593Smuzhiyun 		sci_change_state(&ihost->sm, SCIC_RESETTING);
1195*4882a593Smuzhiyun 		return SCI_SUCCESS;
1196*4882a593Smuzhiyun 	default:
1197*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
1198*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
1199*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
1200*4882a593Smuzhiyun 	}
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
sci_controller_stop_phys(struct isci_host * ihost)1203*4882a593Smuzhiyun static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun 	u32 index;
1206*4882a593Smuzhiyun 	enum sci_status status;
1207*4882a593Smuzhiyun 	enum sci_status phy_status;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	status = SCI_SUCCESS;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1212*4882a593Smuzhiyun 		phy_status = sci_phy_stop(&ihost->phys[index]);
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 		if (phy_status != SCI_SUCCESS &&
1215*4882a593Smuzhiyun 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1216*4882a593Smuzhiyun 			status = SCI_FAILURE;
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 			dev_warn(&ihost->pdev->dev,
1219*4882a593Smuzhiyun 				 "%s: Controller stop operation failed to stop "
1220*4882a593Smuzhiyun 				 "phy %d because of status %d.\n",
1221*4882a593Smuzhiyun 				 __func__,
1222*4882a593Smuzhiyun 				 ihost->phys[index].phy_index, phy_status);
1223*4882a593Smuzhiyun 		}
1224*4882a593Smuzhiyun 	}
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	return status;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun /**
1231*4882a593Smuzhiyun  * isci_host_deinit - shutdown frame reception and dma
1232*4882a593Smuzhiyun  * @ihost: host to take down
1233*4882a593Smuzhiyun  *
1234*4882a593Smuzhiyun  * This is called in either the driver shutdown or the suspend path.  In
1235*4882a593Smuzhiyun  * the shutdown case libsas went through port teardown and normal device
1236*4882a593Smuzhiyun  * removal (i.e. physical links stayed up to service scsi_device removal
1237*4882a593Smuzhiyun  * commands).  In the suspend case we disable the hardware without
1238*4882a593Smuzhiyun  * notifying libsas of the link down events since we want libsas to
1239*4882a593Smuzhiyun  * remember the domain across the suspend/resume cycle
1240*4882a593Smuzhiyun  */
isci_host_deinit(struct isci_host * ihost)1241*4882a593Smuzhiyun void isci_host_deinit(struct isci_host *ihost)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun 	int i;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	/* disable output data selects */
1246*4882a593Smuzhiyun 	for (i = 0; i < isci_gpio_count(ihost); i++)
1247*4882a593Smuzhiyun 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	spin_lock_irq(&ihost->scic_lock);
1252*4882a593Smuzhiyun 	sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
1253*4882a593Smuzhiyun 	spin_unlock_irq(&ihost->scic_lock);
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	wait_for_stop(ihost);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	/* phy stop is after controller stop to allow port and device to
1258*4882a593Smuzhiyun 	 * go idle before shutting down the phys, but the expectation is
1259*4882a593Smuzhiyun 	 * that i/o has been shut off well before we reach this
1260*4882a593Smuzhiyun 	 * function.
1261*4882a593Smuzhiyun 	 */
1262*4882a593Smuzhiyun 	sci_controller_stop_phys(ihost);
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	/* disable sgpio: where the above wait should give time for the
1265*4882a593Smuzhiyun 	 * enclosure to sample the gpios going inactive
1266*4882a593Smuzhiyun 	 */
1267*4882a593Smuzhiyun 	writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	spin_lock_irq(&ihost->scic_lock);
1270*4882a593Smuzhiyun 	sci_controller_reset(ihost);
1271*4882a593Smuzhiyun 	spin_unlock_irq(&ihost->scic_lock);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	/* Cancel any/all outstanding port timers */
1274*4882a593Smuzhiyun 	for (i = 0; i < ihost->logical_port_entries; i++) {
1275*4882a593Smuzhiyun 		struct isci_port *iport = &ihost->ports[i];
1276*4882a593Smuzhiyun 		del_timer_sync(&iport->timer.timer);
1277*4882a593Smuzhiyun 	}
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	/* Cancel any/all outstanding phy timers */
1280*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1281*4882a593Smuzhiyun 		struct isci_phy *iphy = &ihost->phys[i];
1282*4882a593Smuzhiyun 		del_timer_sync(&iphy->sata_timer.timer);
1283*4882a593Smuzhiyun 	}
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	del_timer_sync(&ihost->port_agent.timer.timer);
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	del_timer_sync(&ihost->power_control.timer.timer);
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	del_timer_sync(&ihost->timer.timer);
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	del_timer_sync(&ihost->phy_timer.timer);
1292*4882a593Smuzhiyun }
1293*4882a593Smuzhiyun 
scu_base(struct isci_host * isci_host)1294*4882a593Smuzhiyun static void __iomem *scu_base(struct isci_host *isci_host)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	struct pci_dev *pdev = isci_host->pdev;
1297*4882a593Smuzhiyun 	int id = isci_host->id;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
1300*4882a593Smuzhiyun }
1301*4882a593Smuzhiyun 
smu_base(struct isci_host * isci_host)1302*4882a593Smuzhiyun static void __iomem *smu_base(struct isci_host *isci_host)
1303*4882a593Smuzhiyun {
1304*4882a593Smuzhiyun 	struct pci_dev *pdev = isci_host->pdev;
1305*4882a593Smuzhiyun 	int id = isci_host->id;
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun 
sci_controller_initial_state_enter(struct sci_base_state_machine * sm)1310*4882a593Smuzhiyun static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
1311*4882a593Smuzhiyun {
1312*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	sci_change_state(&ihost->sm, SCIC_RESET);
1315*4882a593Smuzhiyun }
1316*4882a593Smuzhiyun 
sci_controller_starting_state_exit(struct sci_base_state_machine * sm)1317*4882a593Smuzhiyun static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
1318*4882a593Smuzhiyun {
1319*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	sci_del_timer(&ihost->timer);
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1325*4882a593Smuzhiyun #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1326*4882a593Smuzhiyun #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1327*4882a593Smuzhiyun #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1328*4882a593Smuzhiyun #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1329*4882a593Smuzhiyun #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun /**
1332*4882a593Smuzhiyun  * sci_controller_set_interrupt_coalescence() - This method allows the user to
1333*4882a593Smuzhiyun  *    configure the interrupt coalescence.
1334*4882a593Smuzhiyun  * @controller: This parameter represents the handle to the controller object
1335*4882a593Smuzhiyun  *    for which its interrupt coalesce register is overridden.
1336*4882a593Smuzhiyun  * @coalesce_number: Used to control the number of entries in the Completion
1337*4882a593Smuzhiyun  *    Queue before an interrupt is generated. If the number of entries exceed
1338*4882a593Smuzhiyun  *    this number, an interrupt will be generated. The valid range of the input
1339*4882a593Smuzhiyun  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1340*4882a593Smuzhiyun  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1341*4882a593Smuzhiyun  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1342*4882a593Smuzhiyun  *    interrupt coalescing timeout.
1343*4882a593Smuzhiyun  *
1344*4882a593Smuzhiyun  * Indicate if the user successfully set the interrupt coalesce parameters.
1345*4882a593Smuzhiyun  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1346*4882a593Smuzhiyun  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1347*4882a593Smuzhiyun  */
1348*4882a593Smuzhiyun static enum sci_status
sci_controller_set_interrupt_coalescence(struct isci_host * ihost,u32 coalesce_number,u32 coalesce_timeout)1349*4882a593Smuzhiyun sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1350*4882a593Smuzhiyun 					 u32 coalesce_number,
1351*4882a593Smuzhiyun 					 u32 coalesce_timeout)
1352*4882a593Smuzhiyun {
1353*4882a593Smuzhiyun 	u8 timeout_encode = 0;
1354*4882a593Smuzhiyun 	u32 min = 0;
1355*4882a593Smuzhiyun 	u32 max = 0;
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	/* Check if the input parameters fall in the range. */
1358*4882a593Smuzhiyun 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1359*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	/*
1362*4882a593Smuzhiyun 	 *  Defined encoding for interrupt coalescing timeout:
1363*4882a593Smuzhiyun 	 *              Value   Min      Max     Units
1364*4882a593Smuzhiyun 	 *              -----   ---      ---     -----
1365*4882a593Smuzhiyun 	 *              0       -        -       Disabled
1366*4882a593Smuzhiyun 	 *              1       13.3     20.0    ns
1367*4882a593Smuzhiyun 	 *              2       26.7     40.0
1368*4882a593Smuzhiyun 	 *              3       53.3     80.0
1369*4882a593Smuzhiyun 	 *              4       106.7    160.0
1370*4882a593Smuzhiyun 	 *              5       213.3    320.0
1371*4882a593Smuzhiyun 	 *              6       426.7    640.0
1372*4882a593Smuzhiyun 	 *              7       853.3    1280.0
1373*4882a593Smuzhiyun 	 *              8       1.7      2.6     us
1374*4882a593Smuzhiyun 	 *              9       3.4      5.1
1375*4882a593Smuzhiyun 	 *              10      6.8      10.2
1376*4882a593Smuzhiyun 	 *              11      13.7     20.5
1377*4882a593Smuzhiyun 	 *              12      27.3     41.0
1378*4882a593Smuzhiyun 	 *              13      54.6     81.9
1379*4882a593Smuzhiyun 	 *              14      109.2    163.8
1380*4882a593Smuzhiyun 	 *              15      218.5    327.7
1381*4882a593Smuzhiyun 	 *              16      436.9    655.4
1382*4882a593Smuzhiyun 	 *              17      873.8    1310.7
1383*4882a593Smuzhiyun 	 *              18      1.7      2.6     ms
1384*4882a593Smuzhiyun 	 *              19      3.5      5.2
1385*4882a593Smuzhiyun 	 *              20      7.0      10.5
1386*4882a593Smuzhiyun 	 *              21      14.0     21.0
1387*4882a593Smuzhiyun 	 *              22      28.0     41.9
1388*4882a593Smuzhiyun 	 *              23      55.9     83.9
1389*4882a593Smuzhiyun 	 *              24      111.8    167.8
1390*4882a593Smuzhiyun 	 *              25      223.7    335.5
1391*4882a593Smuzhiyun 	 *              26      447.4    671.1
1392*4882a593Smuzhiyun 	 *              27      894.8    1342.2
1393*4882a593Smuzhiyun 	 *              28      1.8      2.7     s
1394*4882a593Smuzhiyun 	 *              Others Undefined */
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	/*
1397*4882a593Smuzhiyun 	 * Use the table above to decide the encode of interrupt coalescing timeout
1398*4882a593Smuzhiyun 	 * value for register writing. */
1399*4882a593Smuzhiyun 	if (coalesce_timeout == 0)
1400*4882a593Smuzhiyun 		timeout_encode = 0;
1401*4882a593Smuzhiyun 	else{
1402*4882a593Smuzhiyun 		/* make the timeout value in unit of (10 ns). */
1403*4882a593Smuzhiyun 		coalesce_timeout = coalesce_timeout * 100;
1404*4882a593Smuzhiyun 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1405*4882a593Smuzhiyun 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1406*4882a593Smuzhiyun 
1407*4882a593Smuzhiyun 		/* get the encode of timeout for register writing. */
1408*4882a593Smuzhiyun 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1409*4882a593Smuzhiyun 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1410*4882a593Smuzhiyun 		      timeout_encode++) {
1411*4882a593Smuzhiyun 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1412*4882a593Smuzhiyun 				break;
1413*4882a593Smuzhiyun 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1414*4882a593Smuzhiyun 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1415*4882a593Smuzhiyun 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1416*4882a593Smuzhiyun 					break;
1417*4882a593Smuzhiyun 				else{
1418*4882a593Smuzhiyun 					timeout_encode++;
1419*4882a593Smuzhiyun 					break;
1420*4882a593Smuzhiyun 				}
1421*4882a593Smuzhiyun 			} else {
1422*4882a593Smuzhiyun 				max = max * 2;
1423*4882a593Smuzhiyun 				min = min * 2;
1424*4882a593Smuzhiyun 			}
1425*4882a593Smuzhiyun 		}
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1428*4882a593Smuzhiyun 			/* the value is out of range. */
1429*4882a593Smuzhiyun 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1430*4882a593Smuzhiyun 	}
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1433*4882a593Smuzhiyun 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1434*4882a593Smuzhiyun 	       &ihost->smu_registers->interrupt_coalesce_control);
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 	ihost->interrupt_coalesce_number = (u16)coalesce_number;
1438*4882a593Smuzhiyun 	ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	return SCI_SUCCESS;
1441*4882a593Smuzhiyun }
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 
sci_controller_ready_state_enter(struct sci_base_state_machine * sm)1444*4882a593Smuzhiyun static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
1445*4882a593Smuzhiyun {
1446*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1447*4882a593Smuzhiyun 	u32 val;
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 	/* enable clock gating for power control of the scu unit */
1450*4882a593Smuzhiyun 	val = readl(&ihost->smu_registers->clock_gating_control);
1451*4882a593Smuzhiyun 	val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
1452*4882a593Smuzhiyun 		 SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
1453*4882a593Smuzhiyun 		 SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
1454*4882a593Smuzhiyun 	val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
1455*4882a593Smuzhiyun 	writel(val, &ihost->smu_registers->clock_gating_control);
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	/* set the default interrupt coalescence number and timeout value. */
1458*4882a593Smuzhiyun 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun 
sci_controller_ready_state_exit(struct sci_base_state_machine * sm)1461*4882a593Smuzhiyun static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
1462*4882a593Smuzhiyun {
1463*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	/* disable interrupt coalescence. */
1466*4882a593Smuzhiyun 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun 
sci_controller_stop_ports(struct isci_host * ihost)1469*4882a593Smuzhiyun static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
1470*4882a593Smuzhiyun {
1471*4882a593Smuzhiyun 	u32 index;
1472*4882a593Smuzhiyun 	enum sci_status port_status;
1473*4882a593Smuzhiyun 	enum sci_status status = SCI_SUCCESS;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	for (index = 0; index < ihost->logical_port_entries; index++) {
1476*4882a593Smuzhiyun 		struct isci_port *iport = &ihost->ports[index];
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 		port_status = sci_port_stop(iport);
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 		if ((port_status != SCI_SUCCESS) &&
1481*4882a593Smuzhiyun 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1482*4882a593Smuzhiyun 			status = SCI_FAILURE;
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun 			dev_warn(&ihost->pdev->dev,
1485*4882a593Smuzhiyun 				 "%s: Controller stop operation failed to "
1486*4882a593Smuzhiyun 				 "stop port %d because of status %d.\n",
1487*4882a593Smuzhiyun 				 __func__,
1488*4882a593Smuzhiyun 				 iport->logical_port_index,
1489*4882a593Smuzhiyun 				 port_status);
1490*4882a593Smuzhiyun 		}
1491*4882a593Smuzhiyun 	}
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	return status;
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun 
sci_controller_stop_devices(struct isci_host * ihost)1496*4882a593Smuzhiyun static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
1497*4882a593Smuzhiyun {
1498*4882a593Smuzhiyun 	u32 index;
1499*4882a593Smuzhiyun 	enum sci_status status;
1500*4882a593Smuzhiyun 	enum sci_status device_status;
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	status = SCI_SUCCESS;
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	for (index = 0; index < ihost->remote_node_entries; index++) {
1505*4882a593Smuzhiyun 		if (ihost->device_table[index] != NULL) {
1506*4882a593Smuzhiyun 			/* / @todo What timeout value do we want to provide to this request? */
1507*4882a593Smuzhiyun 			device_status = sci_remote_device_stop(ihost->device_table[index], 0);
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun 			if ((device_status != SCI_SUCCESS) &&
1510*4882a593Smuzhiyun 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1511*4882a593Smuzhiyun 				dev_warn(&ihost->pdev->dev,
1512*4882a593Smuzhiyun 					 "%s: Controller stop operation failed "
1513*4882a593Smuzhiyun 					 "to stop device 0x%p because of "
1514*4882a593Smuzhiyun 					 "status %d.\n",
1515*4882a593Smuzhiyun 					 __func__,
1516*4882a593Smuzhiyun 					 ihost->device_table[index], device_status);
1517*4882a593Smuzhiyun 			}
1518*4882a593Smuzhiyun 		}
1519*4882a593Smuzhiyun 	}
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	return status;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun 
sci_controller_stopping_state_enter(struct sci_base_state_machine * sm)1524*4882a593Smuzhiyun static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1525*4882a593Smuzhiyun {
1526*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	sci_controller_stop_devices(ihost);
1529*4882a593Smuzhiyun 	sci_controller_stop_ports(ihost);
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	if (!sci_controller_has_remote_devices_stopping(ihost))
1532*4882a593Smuzhiyun 		isci_host_stop_complete(ihost);
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun 
sci_controller_stopping_state_exit(struct sci_base_state_machine * sm)1535*4882a593Smuzhiyun static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	sci_del_timer(&ihost->timer);
1540*4882a593Smuzhiyun }
1541*4882a593Smuzhiyun 
sci_controller_reset_hardware(struct isci_host * ihost)1542*4882a593Smuzhiyun static void sci_controller_reset_hardware(struct isci_host *ihost)
1543*4882a593Smuzhiyun {
1544*4882a593Smuzhiyun 	/* Disable interrupts so we dont take any spurious interrupts */
1545*4882a593Smuzhiyun 	sci_controller_disable_interrupts(ihost);
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	/* Reset the SCU */
1548*4882a593Smuzhiyun 	writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1551*4882a593Smuzhiyun 	udelay(1000);
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	/* The write to the CQGR clears the CQP */
1554*4882a593Smuzhiyun 	writel(0x00000000, &ihost->smu_registers->completion_queue_get);
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 	/* The write to the UFQGP clears the UFQPR */
1557*4882a593Smuzhiyun 	writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 	/* clear all interrupts */
1560*4882a593Smuzhiyun 	writel(~SMU_INTERRUPT_STATUS_RESERVED_MASK, &ihost->smu_registers->interrupt_status);
1561*4882a593Smuzhiyun }
1562*4882a593Smuzhiyun 
sci_controller_resetting_state_enter(struct sci_base_state_machine * sm)1563*4882a593Smuzhiyun static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1564*4882a593Smuzhiyun {
1565*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 	sci_controller_reset_hardware(ihost);
1568*4882a593Smuzhiyun 	sci_change_state(&ihost->sm, SCIC_RESET);
1569*4882a593Smuzhiyun }
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun static const struct sci_base_state sci_controller_state_table[] = {
1572*4882a593Smuzhiyun 	[SCIC_INITIAL] = {
1573*4882a593Smuzhiyun 		.enter_state = sci_controller_initial_state_enter,
1574*4882a593Smuzhiyun 	},
1575*4882a593Smuzhiyun 	[SCIC_RESET] = {},
1576*4882a593Smuzhiyun 	[SCIC_INITIALIZING] = {},
1577*4882a593Smuzhiyun 	[SCIC_INITIALIZED] = {},
1578*4882a593Smuzhiyun 	[SCIC_STARTING] = {
1579*4882a593Smuzhiyun 		.exit_state  = sci_controller_starting_state_exit,
1580*4882a593Smuzhiyun 	},
1581*4882a593Smuzhiyun 	[SCIC_READY] = {
1582*4882a593Smuzhiyun 		.enter_state = sci_controller_ready_state_enter,
1583*4882a593Smuzhiyun 		.exit_state  = sci_controller_ready_state_exit,
1584*4882a593Smuzhiyun 	},
1585*4882a593Smuzhiyun 	[SCIC_RESETTING] = {
1586*4882a593Smuzhiyun 		.enter_state = sci_controller_resetting_state_enter,
1587*4882a593Smuzhiyun 	},
1588*4882a593Smuzhiyun 	[SCIC_STOPPING] = {
1589*4882a593Smuzhiyun 		.enter_state = sci_controller_stopping_state_enter,
1590*4882a593Smuzhiyun 		.exit_state = sci_controller_stopping_state_exit,
1591*4882a593Smuzhiyun 	},
1592*4882a593Smuzhiyun 	[SCIC_FAILED] = {}
1593*4882a593Smuzhiyun };
1594*4882a593Smuzhiyun 
controller_timeout(struct timer_list * t)1595*4882a593Smuzhiyun static void controller_timeout(struct timer_list *t)
1596*4882a593Smuzhiyun {
1597*4882a593Smuzhiyun 	struct sci_timer *tmr = from_timer(tmr, t, timer);
1598*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1599*4882a593Smuzhiyun 	struct sci_base_state_machine *sm = &ihost->sm;
1600*4882a593Smuzhiyun 	unsigned long flags;
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	spin_lock_irqsave(&ihost->scic_lock, flags);
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	if (tmr->cancel)
1605*4882a593Smuzhiyun 		goto done;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	if (sm->current_state_id == SCIC_STARTING)
1608*4882a593Smuzhiyun 		sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
1609*4882a593Smuzhiyun 	else if (sm->current_state_id == SCIC_STOPPING) {
1610*4882a593Smuzhiyun 		sci_change_state(sm, SCIC_FAILED);
1611*4882a593Smuzhiyun 		isci_host_stop_complete(ihost);
1612*4882a593Smuzhiyun 	} else	/* / @todo Now what do we want to do in this case? */
1613*4882a593Smuzhiyun 		dev_err(&ihost->pdev->dev,
1614*4882a593Smuzhiyun 			"%s: Controller timer fired when controller was not "
1615*4882a593Smuzhiyun 			"in a state being timed.\n",
1616*4882a593Smuzhiyun 			__func__);
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun done:
1619*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1620*4882a593Smuzhiyun }
1621*4882a593Smuzhiyun 
sci_controller_construct(struct isci_host * ihost,void __iomem * scu_base,void __iomem * smu_base)1622*4882a593Smuzhiyun static enum sci_status sci_controller_construct(struct isci_host *ihost,
1623*4882a593Smuzhiyun 						void __iomem *scu_base,
1624*4882a593Smuzhiyun 						void __iomem *smu_base)
1625*4882a593Smuzhiyun {
1626*4882a593Smuzhiyun 	u8 i;
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	ihost->scu_registers = scu_base;
1631*4882a593Smuzhiyun 	ihost->smu_registers = smu_base;
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 	sci_port_configuration_agent_construct(&ihost->port_agent);
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	/* Construct the ports for this controller */
1636*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PORTS; i++)
1637*4882a593Smuzhiyun 		sci_port_construct(&ihost->ports[i], i, ihost);
1638*4882a593Smuzhiyun 	sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	/* Construct the phys for this controller */
1641*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1642*4882a593Smuzhiyun 		/* Add all the PHYs to the dummy port */
1643*4882a593Smuzhiyun 		sci_phy_construct(&ihost->phys[i],
1644*4882a593Smuzhiyun 				  &ihost->ports[SCI_MAX_PORTS], i);
1645*4882a593Smuzhiyun 	}
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 	ihost->invalid_phy_mask = 0;
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun 	sci_init_timer(&ihost->timer, controller_timeout);
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	return sci_controller_reset(ihost);
1652*4882a593Smuzhiyun }
1653*4882a593Smuzhiyun 
sci_oem_parameters_validate(struct sci_oem_params * oem,u8 version)1654*4882a593Smuzhiyun int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
1655*4882a593Smuzhiyun {
1656*4882a593Smuzhiyun 	int i;
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PORTS; i++)
1659*4882a593Smuzhiyun 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1660*4882a593Smuzhiyun 			return -EINVAL;
1661*4882a593Smuzhiyun 
1662*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PHYS; i++)
1663*4882a593Smuzhiyun 		if (oem->phys[i].sas_address.high == 0 &&
1664*4882a593Smuzhiyun 		    oem->phys[i].sas_address.low == 0)
1665*4882a593Smuzhiyun 			return -EINVAL;
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1668*4882a593Smuzhiyun 		for (i = 0; i < SCI_MAX_PHYS; i++)
1669*4882a593Smuzhiyun 			if (oem->ports[i].phy_mask != 0)
1670*4882a593Smuzhiyun 				return -EINVAL;
1671*4882a593Smuzhiyun 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1672*4882a593Smuzhiyun 		u8 phy_mask = 0;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 		for (i = 0; i < SCI_MAX_PHYS; i++)
1675*4882a593Smuzhiyun 			phy_mask |= oem->ports[i].phy_mask;
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 		if (phy_mask == 0)
1678*4882a593Smuzhiyun 			return -EINVAL;
1679*4882a593Smuzhiyun 	} else
1680*4882a593Smuzhiyun 		return -EINVAL;
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
1683*4882a593Smuzhiyun 	    oem->controller.max_concurr_spin_up < 1)
1684*4882a593Smuzhiyun 		return -EINVAL;
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 	if (oem->controller.do_enable_ssc) {
1687*4882a593Smuzhiyun 		if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
1688*4882a593Smuzhiyun 			return -EINVAL;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 		if (version >= ISCI_ROM_VER_1_1) {
1691*4882a593Smuzhiyun 			u8 test = oem->controller.ssc_sata_tx_spread_level;
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 			switch (test) {
1694*4882a593Smuzhiyun 			case 0:
1695*4882a593Smuzhiyun 			case 2:
1696*4882a593Smuzhiyun 			case 3:
1697*4882a593Smuzhiyun 			case 6:
1698*4882a593Smuzhiyun 			case 7:
1699*4882a593Smuzhiyun 				break;
1700*4882a593Smuzhiyun 			default:
1701*4882a593Smuzhiyun 				return -EINVAL;
1702*4882a593Smuzhiyun 			}
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun 			test = oem->controller.ssc_sas_tx_spread_level;
1705*4882a593Smuzhiyun 			if (oem->controller.ssc_sas_tx_type == 0) {
1706*4882a593Smuzhiyun 				switch (test) {
1707*4882a593Smuzhiyun 				case 0:
1708*4882a593Smuzhiyun 				case 2:
1709*4882a593Smuzhiyun 				case 3:
1710*4882a593Smuzhiyun 					break;
1711*4882a593Smuzhiyun 				default:
1712*4882a593Smuzhiyun 					return -EINVAL;
1713*4882a593Smuzhiyun 				}
1714*4882a593Smuzhiyun 			} else if (oem->controller.ssc_sas_tx_type == 1) {
1715*4882a593Smuzhiyun 				switch (test) {
1716*4882a593Smuzhiyun 				case 0:
1717*4882a593Smuzhiyun 				case 3:
1718*4882a593Smuzhiyun 				case 6:
1719*4882a593Smuzhiyun 					break;
1720*4882a593Smuzhiyun 				default:
1721*4882a593Smuzhiyun 					return -EINVAL;
1722*4882a593Smuzhiyun 				}
1723*4882a593Smuzhiyun 			}
1724*4882a593Smuzhiyun 		}
1725*4882a593Smuzhiyun 	}
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun 	return 0;
1728*4882a593Smuzhiyun }
1729*4882a593Smuzhiyun 
max_spin_up(struct isci_host * ihost)1730*4882a593Smuzhiyun static u8 max_spin_up(struct isci_host *ihost)
1731*4882a593Smuzhiyun {
1732*4882a593Smuzhiyun 	if (ihost->user_parameters.max_concurr_spinup)
1733*4882a593Smuzhiyun 		return min_t(u8, ihost->user_parameters.max_concurr_spinup,
1734*4882a593Smuzhiyun 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
1735*4882a593Smuzhiyun 	else
1736*4882a593Smuzhiyun 		return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
1737*4882a593Smuzhiyun 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
1738*4882a593Smuzhiyun }
1739*4882a593Smuzhiyun 
power_control_timeout(struct timer_list * t)1740*4882a593Smuzhiyun static void power_control_timeout(struct timer_list *t)
1741*4882a593Smuzhiyun {
1742*4882a593Smuzhiyun 	struct sci_timer *tmr = from_timer(tmr, t, timer);
1743*4882a593Smuzhiyun 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
1744*4882a593Smuzhiyun 	struct isci_phy *iphy;
1745*4882a593Smuzhiyun 	unsigned long flags;
1746*4882a593Smuzhiyun 	u8 i;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	spin_lock_irqsave(&ihost->scic_lock, flags);
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun 	if (tmr->cancel)
1751*4882a593Smuzhiyun 		goto done;
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 	ihost->power_control.phys_granted_power = 0;
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 	if (ihost->power_control.phys_waiting == 0) {
1756*4882a593Smuzhiyun 		ihost->power_control.timer_started = false;
1757*4882a593Smuzhiyun 		goto done;
1758*4882a593Smuzhiyun 	}
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 		if (ihost->power_control.phys_waiting == 0)
1763*4882a593Smuzhiyun 			break;
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 		iphy = ihost->power_control.requesters[i];
1766*4882a593Smuzhiyun 		if (iphy == NULL)
1767*4882a593Smuzhiyun 			continue;
1768*4882a593Smuzhiyun 
1769*4882a593Smuzhiyun 		if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
1770*4882a593Smuzhiyun 			break;
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 		ihost->power_control.requesters[i] = NULL;
1773*4882a593Smuzhiyun 		ihost->power_control.phys_waiting--;
1774*4882a593Smuzhiyun 		ihost->power_control.phys_granted_power++;
1775*4882a593Smuzhiyun 		sci_phy_consume_power_handler(iphy);
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 		if (iphy->protocol == SAS_PROTOCOL_SSP) {
1778*4882a593Smuzhiyun 			u8 j;
1779*4882a593Smuzhiyun 
1780*4882a593Smuzhiyun 			for (j = 0; j < SCI_MAX_PHYS; j++) {
1781*4882a593Smuzhiyun 				struct isci_phy *requester = ihost->power_control.requesters[j];
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 				/*
1784*4882a593Smuzhiyun 				 * Search the power_control queue to see if there are other phys
1785*4882a593Smuzhiyun 				 * attached to the same remote device. If found, take all of
1786*4882a593Smuzhiyun 				 * them out of await_sas_power state.
1787*4882a593Smuzhiyun 				 */
1788*4882a593Smuzhiyun 				if (requester != NULL && requester != iphy) {
1789*4882a593Smuzhiyun 					u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
1790*4882a593Smuzhiyun 							  iphy->frame_rcvd.iaf.sas_addr,
1791*4882a593Smuzhiyun 							  sizeof(requester->frame_rcvd.iaf.sas_addr));
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 					if (other == 0) {
1794*4882a593Smuzhiyun 						ihost->power_control.requesters[j] = NULL;
1795*4882a593Smuzhiyun 						ihost->power_control.phys_waiting--;
1796*4882a593Smuzhiyun 						sci_phy_consume_power_handler(requester);
1797*4882a593Smuzhiyun 					}
1798*4882a593Smuzhiyun 				}
1799*4882a593Smuzhiyun 			}
1800*4882a593Smuzhiyun 		}
1801*4882a593Smuzhiyun 	}
1802*4882a593Smuzhiyun 
1803*4882a593Smuzhiyun 	/*
1804*4882a593Smuzhiyun 	 * It doesn't matter if the power list is empty, we need to start the
1805*4882a593Smuzhiyun 	 * timer in case another phy becomes ready.
1806*4882a593Smuzhiyun 	 */
1807*4882a593Smuzhiyun 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1808*4882a593Smuzhiyun 	ihost->power_control.timer_started = true;
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun done:
1811*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun 
sci_controller_power_control_queue_insert(struct isci_host * ihost,struct isci_phy * iphy)1814*4882a593Smuzhiyun void sci_controller_power_control_queue_insert(struct isci_host *ihost,
1815*4882a593Smuzhiyun 					       struct isci_phy *iphy)
1816*4882a593Smuzhiyun {
1817*4882a593Smuzhiyun 	BUG_ON(iphy == NULL);
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
1820*4882a593Smuzhiyun 		ihost->power_control.phys_granted_power++;
1821*4882a593Smuzhiyun 		sci_phy_consume_power_handler(iphy);
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun 		/*
1824*4882a593Smuzhiyun 		 * stop and start the power_control timer. When the timer fires, the
1825*4882a593Smuzhiyun 		 * no_of_phys_granted_power will be set to 0
1826*4882a593Smuzhiyun 		 */
1827*4882a593Smuzhiyun 		if (ihost->power_control.timer_started)
1828*4882a593Smuzhiyun 			sci_del_timer(&ihost->power_control.timer);
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 		sci_mod_timer(&ihost->power_control.timer,
1831*4882a593Smuzhiyun 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1832*4882a593Smuzhiyun 		ihost->power_control.timer_started = true;
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun 	} else {
1835*4882a593Smuzhiyun 		/*
1836*4882a593Smuzhiyun 		 * There are phys, attached to the same sas address as this phy, are
1837*4882a593Smuzhiyun 		 * already in READY state, this phy don't need wait.
1838*4882a593Smuzhiyun 		 */
1839*4882a593Smuzhiyun 		u8 i;
1840*4882a593Smuzhiyun 		struct isci_phy *current_phy;
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 		for (i = 0; i < SCI_MAX_PHYS; i++) {
1843*4882a593Smuzhiyun 			u8 other;
1844*4882a593Smuzhiyun 			current_phy = &ihost->phys[i];
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 			other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
1847*4882a593Smuzhiyun 				       iphy->frame_rcvd.iaf.sas_addr,
1848*4882a593Smuzhiyun 				       sizeof(current_phy->frame_rcvd.iaf.sas_addr));
1849*4882a593Smuzhiyun 
1850*4882a593Smuzhiyun 			if (current_phy->sm.current_state_id == SCI_PHY_READY &&
1851*4882a593Smuzhiyun 			    current_phy->protocol == SAS_PROTOCOL_SSP &&
1852*4882a593Smuzhiyun 			    other == 0) {
1853*4882a593Smuzhiyun 				sci_phy_consume_power_handler(iphy);
1854*4882a593Smuzhiyun 				break;
1855*4882a593Smuzhiyun 			}
1856*4882a593Smuzhiyun 		}
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun 		if (i == SCI_MAX_PHYS) {
1859*4882a593Smuzhiyun 			/* Add the phy in the waiting list */
1860*4882a593Smuzhiyun 			ihost->power_control.requesters[iphy->phy_index] = iphy;
1861*4882a593Smuzhiyun 			ihost->power_control.phys_waiting++;
1862*4882a593Smuzhiyun 		}
1863*4882a593Smuzhiyun 	}
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun 
sci_controller_power_control_queue_remove(struct isci_host * ihost,struct isci_phy * iphy)1866*4882a593Smuzhiyun void sci_controller_power_control_queue_remove(struct isci_host *ihost,
1867*4882a593Smuzhiyun 					       struct isci_phy *iphy)
1868*4882a593Smuzhiyun {
1869*4882a593Smuzhiyun 	BUG_ON(iphy == NULL);
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 	if (ihost->power_control.requesters[iphy->phy_index])
1872*4882a593Smuzhiyun 		ihost->power_control.phys_waiting--;
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun 	ihost->power_control.requesters[iphy->phy_index] = NULL;
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun 
is_long_cable(int phy,unsigned char selection_byte)1877*4882a593Smuzhiyun static int is_long_cable(int phy, unsigned char selection_byte)
1878*4882a593Smuzhiyun {
1879*4882a593Smuzhiyun 	return !!(selection_byte & (1 << phy));
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun 
is_medium_cable(int phy,unsigned char selection_byte)1882*4882a593Smuzhiyun static int is_medium_cable(int phy, unsigned char selection_byte)
1883*4882a593Smuzhiyun {
1884*4882a593Smuzhiyun 	return !!(selection_byte & (1 << (phy + 4)));
1885*4882a593Smuzhiyun }
1886*4882a593Smuzhiyun 
decode_selection_byte(int phy,unsigned char selection_byte)1887*4882a593Smuzhiyun static enum cable_selections decode_selection_byte(
1888*4882a593Smuzhiyun 	int phy,
1889*4882a593Smuzhiyun 	unsigned char selection_byte)
1890*4882a593Smuzhiyun {
1891*4882a593Smuzhiyun 	return ((selection_byte & (1 << phy)) ? 1 : 0)
1892*4882a593Smuzhiyun 		+ (selection_byte & (1 << (phy + 4)) ? 2 : 0);
1893*4882a593Smuzhiyun }
1894*4882a593Smuzhiyun 
to_cable_select(struct isci_host * ihost)1895*4882a593Smuzhiyun static unsigned char *to_cable_select(struct isci_host *ihost)
1896*4882a593Smuzhiyun {
1897*4882a593Smuzhiyun 	if (is_cable_select_overridden())
1898*4882a593Smuzhiyun 		return ((unsigned char *)&cable_selection_override)
1899*4882a593Smuzhiyun 			+ ihost->id;
1900*4882a593Smuzhiyun 	else
1901*4882a593Smuzhiyun 		return &ihost->oem_parameters.controller.cable_selection_mask;
1902*4882a593Smuzhiyun }
1903*4882a593Smuzhiyun 
decode_cable_selection(struct isci_host * ihost,int phy)1904*4882a593Smuzhiyun enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
1905*4882a593Smuzhiyun {
1906*4882a593Smuzhiyun 	return decode_selection_byte(phy, *to_cable_select(ihost));
1907*4882a593Smuzhiyun }
1908*4882a593Smuzhiyun 
lookup_cable_names(enum cable_selections selection)1909*4882a593Smuzhiyun char *lookup_cable_names(enum cable_selections selection)
1910*4882a593Smuzhiyun {
1911*4882a593Smuzhiyun 	static char *cable_names[] = {
1912*4882a593Smuzhiyun 		[short_cable]     = "short",
1913*4882a593Smuzhiyun 		[long_cable]      = "long",
1914*4882a593Smuzhiyun 		[medium_cable]    = "medium",
1915*4882a593Smuzhiyun 		[undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
1916*4882a593Smuzhiyun 	};
1917*4882a593Smuzhiyun 	return (selection <= undefined_cable) ? cable_names[selection]
1918*4882a593Smuzhiyun 					      : cable_names[undefined_cable];
1919*4882a593Smuzhiyun }
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun #define AFE_REGISTER_WRITE_DELAY 10
1922*4882a593Smuzhiyun 
sci_controller_afe_initialization(struct isci_host * ihost)1923*4882a593Smuzhiyun static void sci_controller_afe_initialization(struct isci_host *ihost)
1924*4882a593Smuzhiyun {
1925*4882a593Smuzhiyun 	struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
1926*4882a593Smuzhiyun 	const struct sci_oem_params *oem = &ihost->oem_parameters;
1927*4882a593Smuzhiyun 	struct pci_dev *pdev = ihost->pdev;
1928*4882a593Smuzhiyun 	u32 afe_status;
1929*4882a593Smuzhiyun 	u32 phy_id;
1930*4882a593Smuzhiyun 	unsigned char cable_selection_mask = *to_cable_select(ihost);
1931*4882a593Smuzhiyun 
1932*4882a593Smuzhiyun 	/* Clear DFX Status registers */
1933*4882a593Smuzhiyun 	writel(0x0081000f, &afe->afe_dfx_master_control0);
1934*4882a593Smuzhiyun 	udelay(AFE_REGISTER_WRITE_DELAY);
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
1937*4882a593Smuzhiyun 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
1938*4882a593Smuzhiyun 		 * Timer, PM Stagger Timer
1939*4882a593Smuzhiyun 		 */
1940*4882a593Smuzhiyun 		writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
1941*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
1942*4882a593Smuzhiyun 	}
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 	/* Configure bias currents to normal */
1945*4882a593Smuzhiyun 	if (is_a2(pdev))
1946*4882a593Smuzhiyun 		writel(0x00005A00, &afe->afe_bias_control);
1947*4882a593Smuzhiyun 	else if (is_b0(pdev) || is_c0(pdev))
1948*4882a593Smuzhiyun 		writel(0x00005F00, &afe->afe_bias_control);
1949*4882a593Smuzhiyun 	else if (is_c1(pdev))
1950*4882a593Smuzhiyun 		writel(0x00005500, &afe->afe_bias_control);
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 	udelay(AFE_REGISTER_WRITE_DELAY);
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 	/* Enable PLL */
1955*4882a593Smuzhiyun 	if (is_a2(pdev))
1956*4882a593Smuzhiyun 		writel(0x80040908, &afe->afe_pll_control0);
1957*4882a593Smuzhiyun 	else if (is_b0(pdev) || is_c0(pdev))
1958*4882a593Smuzhiyun 		writel(0x80040A08, &afe->afe_pll_control0);
1959*4882a593Smuzhiyun 	else if (is_c1(pdev)) {
1960*4882a593Smuzhiyun 		writel(0x80000B08, &afe->afe_pll_control0);
1961*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
1962*4882a593Smuzhiyun 		writel(0x00000B08, &afe->afe_pll_control0);
1963*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
1964*4882a593Smuzhiyun 		writel(0x80000B08, &afe->afe_pll_control0);
1965*4882a593Smuzhiyun 	}
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	udelay(AFE_REGISTER_WRITE_DELAY);
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun 	/* Wait for the PLL to lock */
1970*4882a593Smuzhiyun 	do {
1971*4882a593Smuzhiyun 		afe_status = readl(&afe->afe_common_block_status);
1972*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
1973*4882a593Smuzhiyun 	} while ((afe_status & 0x00001000) == 0);
1974*4882a593Smuzhiyun 
1975*4882a593Smuzhiyun 	if (is_a2(pdev)) {
1976*4882a593Smuzhiyun 		/* Shorten SAS SNW lock time (RxLock timer value from 76
1977*4882a593Smuzhiyun 		 * us to 50 us)
1978*4882a593Smuzhiyun 		 */
1979*4882a593Smuzhiyun 		writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
1980*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
1981*4882a593Smuzhiyun 	}
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
1984*4882a593Smuzhiyun 		struct scu_afe_transceiver __iomem *xcvr = &afe->scu_afe_xcvr[phy_id];
1985*4882a593Smuzhiyun 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
1986*4882a593Smuzhiyun 		int cable_length_long =
1987*4882a593Smuzhiyun 			is_long_cable(phy_id, cable_selection_mask);
1988*4882a593Smuzhiyun 		int cable_length_medium =
1989*4882a593Smuzhiyun 			is_medium_cable(phy_id, cable_selection_mask);
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun 		if (is_a2(pdev)) {
1992*4882a593Smuzhiyun 			/* All defaults, except the Receive Word
1993*4882a593Smuzhiyun 			 * Alignament/Comma Detect Enable....(0xe800)
1994*4882a593Smuzhiyun 			 */
1995*4882a593Smuzhiyun 			writel(0x00004512, &xcvr->afe_xcvr_control0);
1996*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 			writel(0x0050100F, &xcvr->afe_xcvr_control1);
1999*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2000*4882a593Smuzhiyun 		} else if (is_b0(pdev)) {
2001*4882a593Smuzhiyun 			/* Configure transmitter SSC parameters */
2002*4882a593Smuzhiyun 			writel(0x00030000, &xcvr->afe_tx_ssc_control);
2003*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2004*4882a593Smuzhiyun 		} else if (is_c0(pdev)) {
2005*4882a593Smuzhiyun 			/* Configure transmitter SSC parameters */
2006*4882a593Smuzhiyun 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2007*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 			/* All defaults, except the Receive Word
2010*4882a593Smuzhiyun 			 * Alignament/Comma Detect Enable....(0xe800)
2011*4882a593Smuzhiyun 			 */
2012*4882a593Smuzhiyun 			writel(0x00014500, &xcvr->afe_xcvr_control0);
2013*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2014*4882a593Smuzhiyun 		} else if (is_c1(pdev)) {
2015*4882a593Smuzhiyun 			/* Configure transmitter SSC parameters */
2016*4882a593Smuzhiyun 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2017*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun 			/* All defaults, except the Receive Word
2020*4882a593Smuzhiyun 			 * Alignament/Comma Detect Enable....(0xe800)
2021*4882a593Smuzhiyun 			 */
2022*4882a593Smuzhiyun 			writel(0x0001C500, &xcvr->afe_xcvr_control0);
2023*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2024*4882a593Smuzhiyun 		}
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun 		/* Power up TX and RX out from power down (PWRDNTX and
2027*4882a593Smuzhiyun 		 * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
2028*4882a593Smuzhiyun 		 */
2029*4882a593Smuzhiyun 		if (is_a2(pdev))
2030*4882a593Smuzhiyun 			writel(0x000003F0, &xcvr->afe_channel_control);
2031*4882a593Smuzhiyun 		else if (is_b0(pdev)) {
2032*4882a593Smuzhiyun 			writel(0x000003D7, &xcvr->afe_channel_control);
2033*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 			writel(0x000003D4, &xcvr->afe_channel_control);
2036*4882a593Smuzhiyun 		} else if (is_c0(pdev)) {
2037*4882a593Smuzhiyun 			writel(0x000001E7, &xcvr->afe_channel_control);
2038*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 			writel(0x000001E4, &xcvr->afe_channel_control);
2041*4882a593Smuzhiyun 		} else if (is_c1(pdev)) {
2042*4882a593Smuzhiyun 			writel(cable_length_long ? 0x000002F7 : 0x000001F7,
2043*4882a593Smuzhiyun 			       &xcvr->afe_channel_control);
2044*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun 			writel(cable_length_long ? 0x000002F4 : 0x000001F4,
2047*4882a593Smuzhiyun 			       &xcvr->afe_channel_control);
2048*4882a593Smuzhiyun 		}
2049*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
2050*4882a593Smuzhiyun 
2051*4882a593Smuzhiyun 		if (is_a2(pdev)) {
2052*4882a593Smuzhiyun 			/* Enable TX equalization (0xe824) */
2053*4882a593Smuzhiyun 			writel(0x00040000, &xcvr->afe_tx_control);
2054*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2055*4882a593Smuzhiyun 		}
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun 		if (is_a2(pdev) || is_b0(pdev))
2058*4882a593Smuzhiyun 			/* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
2059*4882a593Smuzhiyun 			 * TPD=0x0(TX Power On), RDD=0x0(RX Detect
2060*4882a593Smuzhiyun 			 * Enabled) ....(0xe800)
2061*4882a593Smuzhiyun 			 */
2062*4882a593Smuzhiyun 			writel(0x00004100, &xcvr->afe_xcvr_control0);
2063*4882a593Smuzhiyun 		else if (is_c0(pdev))
2064*4882a593Smuzhiyun 			writel(0x00014100, &xcvr->afe_xcvr_control0);
2065*4882a593Smuzhiyun 		else if (is_c1(pdev))
2066*4882a593Smuzhiyun 			writel(0x0001C100, &xcvr->afe_xcvr_control0);
2067*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 		/* Leave DFE/FFE on */
2070*4882a593Smuzhiyun 		if (is_a2(pdev))
2071*4882a593Smuzhiyun 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2072*4882a593Smuzhiyun 		else if (is_b0(pdev)) {
2073*4882a593Smuzhiyun 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2074*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2075*4882a593Smuzhiyun 			/* Enable TX equalization (0xe824) */
2076*4882a593Smuzhiyun 			writel(0x00040000, &xcvr->afe_tx_control);
2077*4882a593Smuzhiyun 		} else if (is_c0(pdev)) {
2078*4882a593Smuzhiyun 			writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
2079*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun 			writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
2082*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2083*4882a593Smuzhiyun 
2084*4882a593Smuzhiyun 			/* Enable TX equalization (0xe824) */
2085*4882a593Smuzhiyun 			writel(0x00040000, &xcvr->afe_tx_control);
2086*4882a593Smuzhiyun 		} else if (is_c1(pdev)) {
2087*4882a593Smuzhiyun 			writel(cable_length_long ? 0x01500C0C :
2088*4882a593Smuzhiyun 			       cable_length_medium ? 0x01400C0D : 0x02400C0D,
2089*4882a593Smuzhiyun 			       &xcvr->afe_xcvr_control1);
2090*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 			writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
2093*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun 			writel(cable_length_long ? 0x33091C1F :
2096*4882a593Smuzhiyun 			       cable_length_medium ? 0x3315181F : 0x2B17161F,
2097*4882a593Smuzhiyun 			       &xcvr->afe_rx_ssc_control0);
2098*4882a593Smuzhiyun 			udelay(AFE_REGISTER_WRITE_DELAY);
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 			/* Enable TX equalization (0xe824) */
2101*4882a593Smuzhiyun 			writel(0x00040000, &xcvr->afe_tx_control);
2102*4882a593Smuzhiyun 		}
2103*4882a593Smuzhiyun 
2104*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
2105*4882a593Smuzhiyun 
2106*4882a593Smuzhiyun 		writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
2107*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
2108*4882a593Smuzhiyun 
2109*4882a593Smuzhiyun 		writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
2110*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun 		writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
2113*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun 		writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
2116*4882a593Smuzhiyun 		udelay(AFE_REGISTER_WRITE_DELAY);
2117*4882a593Smuzhiyun 	}
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 	/* Transfer control to the PEs */
2120*4882a593Smuzhiyun 	writel(0x00010f00, &afe->afe_dfx_master_control0);
2121*4882a593Smuzhiyun 	udelay(AFE_REGISTER_WRITE_DELAY);
2122*4882a593Smuzhiyun }
2123*4882a593Smuzhiyun 
sci_controller_initialize_power_control(struct isci_host * ihost)2124*4882a593Smuzhiyun static void sci_controller_initialize_power_control(struct isci_host *ihost)
2125*4882a593Smuzhiyun {
2126*4882a593Smuzhiyun 	sci_init_timer(&ihost->power_control.timer, power_control_timeout);
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun 	memset(ihost->power_control.requesters, 0,
2129*4882a593Smuzhiyun 	       sizeof(ihost->power_control.requesters));
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun 	ihost->power_control.phys_waiting = 0;
2132*4882a593Smuzhiyun 	ihost->power_control.phys_granted_power = 0;
2133*4882a593Smuzhiyun }
2134*4882a593Smuzhiyun 
sci_controller_initialize(struct isci_host * ihost)2135*4882a593Smuzhiyun static enum sci_status sci_controller_initialize(struct isci_host *ihost)
2136*4882a593Smuzhiyun {
2137*4882a593Smuzhiyun 	struct sci_base_state_machine *sm = &ihost->sm;
2138*4882a593Smuzhiyun 	enum sci_status result = SCI_FAILURE;
2139*4882a593Smuzhiyun 	unsigned long i, state, val;
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_RESET) {
2142*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
2143*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
2144*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
2145*4882a593Smuzhiyun 	}
2146*4882a593Smuzhiyun 
2147*4882a593Smuzhiyun 	sci_change_state(sm, SCIC_INITIALIZING);
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun 	sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
2150*4882a593Smuzhiyun 
2151*4882a593Smuzhiyun 	ihost->next_phy_to_start = 0;
2152*4882a593Smuzhiyun 	ihost->phy_startup_timer_pending = false;
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	sci_controller_initialize_power_control(ihost);
2155*4882a593Smuzhiyun 
2156*4882a593Smuzhiyun 	/*
2157*4882a593Smuzhiyun 	 * There is nothing to do here for B0 since we do not have to
2158*4882a593Smuzhiyun 	 * program the AFE registers.
2159*4882a593Smuzhiyun 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2160*4882a593Smuzhiyun 	 * /       presently they seem to be wrong. */
2161*4882a593Smuzhiyun 	sci_controller_afe_initialization(ihost);
2162*4882a593Smuzhiyun 
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	/* Take the hardware out of reset */
2165*4882a593Smuzhiyun 	writel(0, &ihost->smu_registers->soft_reset_control);
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	/*
2168*4882a593Smuzhiyun 	 * / @todo Provide meaningfull error code for hardware failure
2169*4882a593Smuzhiyun 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2170*4882a593Smuzhiyun 	for (i = 100; i >= 1; i--) {
2171*4882a593Smuzhiyun 		u32 status;
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 		/* Loop until the hardware reports success */
2174*4882a593Smuzhiyun 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2175*4882a593Smuzhiyun 		status = readl(&ihost->smu_registers->control_status);
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
2178*4882a593Smuzhiyun 			break;
2179*4882a593Smuzhiyun 	}
2180*4882a593Smuzhiyun 	if (i == 0)
2181*4882a593Smuzhiyun 		goto out;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	/*
2184*4882a593Smuzhiyun 	 * Determine what are the actaul device capacities that the
2185*4882a593Smuzhiyun 	 * hardware will support */
2186*4882a593Smuzhiyun 	val = readl(&ihost->smu_registers->device_context_capacity);
2187*4882a593Smuzhiyun 
2188*4882a593Smuzhiyun 	/* Record the smaller of the two capacity values */
2189*4882a593Smuzhiyun 	ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2190*4882a593Smuzhiyun 	ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2191*4882a593Smuzhiyun 	ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 	/*
2194*4882a593Smuzhiyun 	 * Make all PEs that are unassigned match up with the
2195*4882a593Smuzhiyun 	 * logical ports
2196*4882a593Smuzhiyun 	 */
2197*4882a593Smuzhiyun 	for (i = 0; i < ihost->logical_port_entries; i++) {
2198*4882a593Smuzhiyun 		struct scu_port_task_scheduler_group_registers __iomem
2199*4882a593Smuzhiyun 			*ptsg = &ihost->scu_registers->peg0.ptsg;
2200*4882a593Smuzhiyun 
2201*4882a593Smuzhiyun 		writel(i, &ptsg->protocol_engine[i]);
2202*4882a593Smuzhiyun 	}
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2205*4882a593Smuzhiyun 	val = readl(&ihost->scu_registers->sdma.pdma_configuration);
2206*4882a593Smuzhiyun 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2207*4882a593Smuzhiyun 	writel(val, &ihost->scu_registers->sdma.pdma_configuration);
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 	val = readl(&ihost->scu_registers->sdma.cdma_configuration);
2210*4882a593Smuzhiyun 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2211*4882a593Smuzhiyun 	writel(val, &ihost->scu_registers->sdma.cdma_configuration);
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun 	/*
2214*4882a593Smuzhiyun 	 * Initialize the PHYs before the PORTs because the PHY registers
2215*4882a593Smuzhiyun 	 * are accessed during the port initialization.
2216*4882a593Smuzhiyun 	 */
2217*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_PHYS; i++) {
2218*4882a593Smuzhiyun 		result = sci_phy_initialize(&ihost->phys[i],
2219*4882a593Smuzhiyun 					    &ihost->scu_registers->peg0.pe[i].tl,
2220*4882a593Smuzhiyun 					    &ihost->scu_registers->peg0.pe[i].ll);
2221*4882a593Smuzhiyun 		if (result != SCI_SUCCESS)
2222*4882a593Smuzhiyun 			goto out;
2223*4882a593Smuzhiyun 	}
2224*4882a593Smuzhiyun 
2225*4882a593Smuzhiyun 	for (i = 0; i < ihost->logical_port_entries; i++) {
2226*4882a593Smuzhiyun 		struct isci_port *iport = &ihost->ports[i];
2227*4882a593Smuzhiyun 
2228*4882a593Smuzhiyun 		iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
2229*4882a593Smuzhiyun 		iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
2230*4882a593Smuzhiyun 		iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
2231*4882a593Smuzhiyun 	}
2232*4882a593Smuzhiyun 
2233*4882a593Smuzhiyun 	result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun  out:
2236*4882a593Smuzhiyun 	/* Advance the controller state machine */
2237*4882a593Smuzhiyun 	if (result == SCI_SUCCESS)
2238*4882a593Smuzhiyun 		state = SCIC_INITIALIZED;
2239*4882a593Smuzhiyun 	else
2240*4882a593Smuzhiyun 		state = SCIC_FAILED;
2241*4882a593Smuzhiyun 	sci_change_state(sm, state);
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	return result;
2244*4882a593Smuzhiyun }
2245*4882a593Smuzhiyun 
sci_controller_dma_alloc(struct isci_host * ihost)2246*4882a593Smuzhiyun static int sci_controller_dma_alloc(struct isci_host *ihost)
2247*4882a593Smuzhiyun {
2248*4882a593Smuzhiyun 	struct device *dev = &ihost->pdev->dev;
2249*4882a593Smuzhiyun 	size_t size;
2250*4882a593Smuzhiyun 	int i;
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun 	/* detect re-initialization */
2253*4882a593Smuzhiyun 	if (ihost->completion_queue)
2254*4882a593Smuzhiyun 		return 0;
2255*4882a593Smuzhiyun 
2256*4882a593Smuzhiyun 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2257*4882a593Smuzhiyun 	ihost->completion_queue = dmam_alloc_coherent(dev, size, &ihost->cq_dma,
2258*4882a593Smuzhiyun 						      GFP_KERNEL);
2259*4882a593Smuzhiyun 	if (!ihost->completion_queue)
2260*4882a593Smuzhiyun 		return -ENOMEM;
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun 	size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2263*4882a593Smuzhiyun 	ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &ihost->rnc_dma,
2264*4882a593Smuzhiyun 							       GFP_KERNEL);
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun 	if (!ihost->remote_node_context_table)
2267*4882a593Smuzhiyun 		return -ENOMEM;
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun 	size = ihost->task_context_entries * sizeof(struct scu_task_context),
2270*4882a593Smuzhiyun 	ihost->task_context_table = dmam_alloc_coherent(dev, size, &ihost->tc_dma,
2271*4882a593Smuzhiyun 							GFP_KERNEL);
2272*4882a593Smuzhiyun 	if (!ihost->task_context_table)
2273*4882a593Smuzhiyun 		return -ENOMEM;
2274*4882a593Smuzhiyun 
2275*4882a593Smuzhiyun 	size = SCI_UFI_TOTAL_SIZE;
2276*4882a593Smuzhiyun 	ihost->ufi_buf = dmam_alloc_coherent(dev, size, &ihost->ufi_dma, GFP_KERNEL);
2277*4882a593Smuzhiyun 	if (!ihost->ufi_buf)
2278*4882a593Smuzhiyun 		return -ENOMEM;
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2281*4882a593Smuzhiyun 		struct isci_request *ireq;
2282*4882a593Smuzhiyun 		dma_addr_t dma;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun 		ireq = dmam_alloc_coherent(dev, sizeof(*ireq), &dma, GFP_KERNEL);
2285*4882a593Smuzhiyun 		if (!ireq)
2286*4882a593Smuzhiyun 			return -ENOMEM;
2287*4882a593Smuzhiyun 
2288*4882a593Smuzhiyun 		ireq->tc = &ihost->task_context_table[i];
2289*4882a593Smuzhiyun 		ireq->owning_controller = ihost;
2290*4882a593Smuzhiyun 		ireq->request_daddr = dma;
2291*4882a593Smuzhiyun 		ireq->isci_host = ihost;
2292*4882a593Smuzhiyun 		ihost->reqs[i] = ireq;
2293*4882a593Smuzhiyun 	}
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 	return 0;
2296*4882a593Smuzhiyun }
2297*4882a593Smuzhiyun 
sci_controller_mem_init(struct isci_host * ihost)2298*4882a593Smuzhiyun static int sci_controller_mem_init(struct isci_host *ihost)
2299*4882a593Smuzhiyun {
2300*4882a593Smuzhiyun 	int err = sci_controller_dma_alloc(ihost);
2301*4882a593Smuzhiyun 
2302*4882a593Smuzhiyun 	if (err)
2303*4882a593Smuzhiyun 		return err;
2304*4882a593Smuzhiyun 
2305*4882a593Smuzhiyun 	writel(lower_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_lower);
2306*4882a593Smuzhiyun 	writel(upper_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_upper);
2307*4882a593Smuzhiyun 
2308*4882a593Smuzhiyun 	writel(lower_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_lower);
2309*4882a593Smuzhiyun 	writel(upper_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_upper);
2310*4882a593Smuzhiyun 
2311*4882a593Smuzhiyun 	writel(lower_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_lower);
2312*4882a593Smuzhiyun 	writel(upper_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_upper);
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 	sci_unsolicited_frame_control_construct(ihost);
2315*4882a593Smuzhiyun 
2316*4882a593Smuzhiyun 	/*
2317*4882a593Smuzhiyun 	 * Inform the silicon as to the location of the UF headers and
2318*4882a593Smuzhiyun 	 * address table.
2319*4882a593Smuzhiyun 	 */
2320*4882a593Smuzhiyun 	writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2321*4882a593Smuzhiyun 		&ihost->scu_registers->sdma.uf_header_base_address_lower);
2322*4882a593Smuzhiyun 	writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2323*4882a593Smuzhiyun 		&ihost->scu_registers->sdma.uf_header_base_address_upper);
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 	writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2326*4882a593Smuzhiyun 		&ihost->scu_registers->sdma.uf_address_table_lower);
2327*4882a593Smuzhiyun 	writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2328*4882a593Smuzhiyun 		&ihost->scu_registers->sdma.uf_address_table_upper);
2329*4882a593Smuzhiyun 
2330*4882a593Smuzhiyun 	return 0;
2331*4882a593Smuzhiyun }
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun /**
2334*4882a593Smuzhiyun  * isci_host_init - (re-)initialize hardware and internal (private) state
2335*4882a593Smuzhiyun  * @ihost: host to init
2336*4882a593Smuzhiyun  *
2337*4882a593Smuzhiyun  * Any public facing objects (like asd_sas_port, and asd_sas_phys), or
2338*4882a593Smuzhiyun  * one-time initialization objects like locks and waitqueues, are
2339*4882a593Smuzhiyun  * not touched (they are initialized in isci_host_alloc)
2340*4882a593Smuzhiyun  */
isci_host_init(struct isci_host * ihost)2341*4882a593Smuzhiyun int isci_host_init(struct isci_host *ihost)
2342*4882a593Smuzhiyun {
2343*4882a593Smuzhiyun 	int i, err;
2344*4882a593Smuzhiyun 	enum sci_status status;
2345*4882a593Smuzhiyun 
2346*4882a593Smuzhiyun 	spin_lock_irq(&ihost->scic_lock);
2347*4882a593Smuzhiyun 	status = sci_controller_construct(ihost, scu_base(ihost), smu_base(ihost));
2348*4882a593Smuzhiyun 	spin_unlock_irq(&ihost->scic_lock);
2349*4882a593Smuzhiyun 	if (status != SCI_SUCCESS) {
2350*4882a593Smuzhiyun 		dev_err(&ihost->pdev->dev,
2351*4882a593Smuzhiyun 			"%s: sci_controller_construct failed - status = %x\n",
2352*4882a593Smuzhiyun 			__func__,
2353*4882a593Smuzhiyun 			status);
2354*4882a593Smuzhiyun 		return -ENODEV;
2355*4882a593Smuzhiyun 	}
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun 	spin_lock_irq(&ihost->scic_lock);
2358*4882a593Smuzhiyun 	status = sci_controller_initialize(ihost);
2359*4882a593Smuzhiyun 	spin_unlock_irq(&ihost->scic_lock);
2360*4882a593Smuzhiyun 	if (status != SCI_SUCCESS) {
2361*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev,
2362*4882a593Smuzhiyun 			 "%s: sci_controller_initialize failed -"
2363*4882a593Smuzhiyun 			 " status = 0x%x\n",
2364*4882a593Smuzhiyun 			 __func__, status);
2365*4882a593Smuzhiyun 		return -ENODEV;
2366*4882a593Smuzhiyun 	}
2367*4882a593Smuzhiyun 
2368*4882a593Smuzhiyun 	err = sci_controller_mem_init(ihost);
2369*4882a593Smuzhiyun 	if (err)
2370*4882a593Smuzhiyun 		return err;
2371*4882a593Smuzhiyun 
2372*4882a593Smuzhiyun 	/* enable sgpio */
2373*4882a593Smuzhiyun 	writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
2374*4882a593Smuzhiyun 	for (i = 0; i < isci_gpio_count(ihost); i++)
2375*4882a593Smuzhiyun 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
2376*4882a593Smuzhiyun 	writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun 	return 0;
2379*4882a593Smuzhiyun }
2380*4882a593Smuzhiyun 
sci_controller_link_up(struct isci_host * ihost,struct isci_port * iport,struct isci_phy * iphy)2381*4882a593Smuzhiyun void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
2382*4882a593Smuzhiyun 			    struct isci_phy *iphy)
2383*4882a593Smuzhiyun {
2384*4882a593Smuzhiyun 	switch (ihost->sm.current_state_id) {
2385*4882a593Smuzhiyun 	case SCIC_STARTING:
2386*4882a593Smuzhiyun 		sci_del_timer(&ihost->phy_timer);
2387*4882a593Smuzhiyun 		ihost->phy_startup_timer_pending = false;
2388*4882a593Smuzhiyun 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2389*4882a593Smuzhiyun 						  iport, iphy);
2390*4882a593Smuzhiyun 		sci_controller_start_next_phy(ihost);
2391*4882a593Smuzhiyun 		break;
2392*4882a593Smuzhiyun 	case SCIC_READY:
2393*4882a593Smuzhiyun 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2394*4882a593Smuzhiyun 						  iport, iphy);
2395*4882a593Smuzhiyun 		break;
2396*4882a593Smuzhiyun 	default:
2397*4882a593Smuzhiyun 		dev_dbg(&ihost->pdev->dev,
2398*4882a593Smuzhiyun 			"%s: SCIC Controller linkup event from phy %d in "
2399*4882a593Smuzhiyun 			"unexpected state %d\n", __func__, iphy->phy_index,
2400*4882a593Smuzhiyun 			ihost->sm.current_state_id);
2401*4882a593Smuzhiyun 	}
2402*4882a593Smuzhiyun }
2403*4882a593Smuzhiyun 
sci_controller_link_down(struct isci_host * ihost,struct isci_port * iport,struct isci_phy * iphy)2404*4882a593Smuzhiyun void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
2405*4882a593Smuzhiyun 			      struct isci_phy *iphy)
2406*4882a593Smuzhiyun {
2407*4882a593Smuzhiyun 	switch (ihost->sm.current_state_id) {
2408*4882a593Smuzhiyun 	case SCIC_STARTING:
2409*4882a593Smuzhiyun 	case SCIC_READY:
2410*4882a593Smuzhiyun 		ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
2411*4882a593Smuzhiyun 						   iport, iphy);
2412*4882a593Smuzhiyun 		break;
2413*4882a593Smuzhiyun 	default:
2414*4882a593Smuzhiyun 		dev_dbg(&ihost->pdev->dev,
2415*4882a593Smuzhiyun 			"%s: SCIC Controller linkdown event from phy %d in "
2416*4882a593Smuzhiyun 			"unexpected state %d\n",
2417*4882a593Smuzhiyun 			__func__,
2418*4882a593Smuzhiyun 			iphy->phy_index,
2419*4882a593Smuzhiyun 			ihost->sm.current_state_id);
2420*4882a593Smuzhiyun 	}
2421*4882a593Smuzhiyun }
2422*4882a593Smuzhiyun 
sci_controller_has_remote_devices_stopping(struct isci_host * ihost)2423*4882a593Smuzhiyun bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
2424*4882a593Smuzhiyun {
2425*4882a593Smuzhiyun 	u32 index;
2426*4882a593Smuzhiyun 
2427*4882a593Smuzhiyun 	for (index = 0; index < ihost->remote_node_entries; index++) {
2428*4882a593Smuzhiyun 		if ((ihost->device_table[index] != NULL) &&
2429*4882a593Smuzhiyun 		   (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2430*4882a593Smuzhiyun 			return true;
2431*4882a593Smuzhiyun 	}
2432*4882a593Smuzhiyun 
2433*4882a593Smuzhiyun 	return false;
2434*4882a593Smuzhiyun }
2435*4882a593Smuzhiyun 
sci_controller_remote_device_stopped(struct isci_host * ihost,struct isci_remote_device * idev)2436*4882a593Smuzhiyun void sci_controller_remote_device_stopped(struct isci_host *ihost,
2437*4882a593Smuzhiyun 					  struct isci_remote_device *idev)
2438*4882a593Smuzhiyun {
2439*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_STOPPING) {
2440*4882a593Smuzhiyun 		dev_dbg(&ihost->pdev->dev,
2441*4882a593Smuzhiyun 			"SCIC Controller 0x%p remote device stopped event "
2442*4882a593Smuzhiyun 			"from device 0x%p in unexpected state %d\n",
2443*4882a593Smuzhiyun 			ihost, idev,
2444*4882a593Smuzhiyun 			ihost->sm.current_state_id);
2445*4882a593Smuzhiyun 		return;
2446*4882a593Smuzhiyun 	}
2447*4882a593Smuzhiyun 
2448*4882a593Smuzhiyun 	if (!sci_controller_has_remote_devices_stopping(ihost))
2449*4882a593Smuzhiyun 		isci_host_stop_complete(ihost);
2450*4882a593Smuzhiyun }
2451*4882a593Smuzhiyun 
sci_controller_post_request(struct isci_host * ihost,u32 request)2452*4882a593Smuzhiyun void sci_controller_post_request(struct isci_host *ihost, u32 request)
2453*4882a593Smuzhiyun {
2454*4882a593Smuzhiyun 	dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
2455*4882a593Smuzhiyun 		__func__, ihost->id, request);
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun 	writel(request, &ihost->smu_registers->post_context_port);
2458*4882a593Smuzhiyun }
2459*4882a593Smuzhiyun 
sci_request_by_tag(struct isci_host * ihost,u16 io_tag)2460*4882a593Smuzhiyun struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
2461*4882a593Smuzhiyun {
2462*4882a593Smuzhiyun 	u16 task_index;
2463*4882a593Smuzhiyun 	u16 task_sequence;
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun 	task_index = ISCI_TAG_TCI(io_tag);
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	if (task_index < ihost->task_context_entries) {
2468*4882a593Smuzhiyun 		struct isci_request *ireq = ihost->reqs[task_index];
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2471*4882a593Smuzhiyun 			task_sequence = ISCI_TAG_SEQ(io_tag);
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun 			if (task_sequence == ihost->io_request_sequence[task_index])
2474*4882a593Smuzhiyun 				return ireq;
2475*4882a593Smuzhiyun 		}
2476*4882a593Smuzhiyun 	}
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun 	return NULL;
2479*4882a593Smuzhiyun }
2480*4882a593Smuzhiyun 
2481*4882a593Smuzhiyun /**
2482*4882a593Smuzhiyun  * This method allocates remote node index and the reserves the remote node
2483*4882a593Smuzhiyun  *    context space for use. This method can fail if there are no more remote
2484*4882a593Smuzhiyun  *    node index available.
2485*4882a593Smuzhiyun  * @scic: This is the controller object which contains the set of
2486*4882a593Smuzhiyun  *    free remote node ids
2487*4882a593Smuzhiyun  * @sci_dev: This is the device object which is requesting the a remote node
2488*4882a593Smuzhiyun  *    id
2489*4882a593Smuzhiyun  * @node_id: This is the remote node id that is assinged to the device if one
2490*4882a593Smuzhiyun  *    is available
2491*4882a593Smuzhiyun  *
2492*4882a593Smuzhiyun  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2493*4882a593Smuzhiyun  * node index available.
2494*4882a593Smuzhiyun  */
sci_controller_allocate_remote_node_context(struct isci_host * ihost,struct isci_remote_device * idev,u16 * node_id)2495*4882a593Smuzhiyun enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
2496*4882a593Smuzhiyun 							    struct isci_remote_device *idev,
2497*4882a593Smuzhiyun 							    u16 *node_id)
2498*4882a593Smuzhiyun {
2499*4882a593Smuzhiyun 	u16 node_index;
2500*4882a593Smuzhiyun 	u32 remote_node_count = sci_remote_device_node_count(idev);
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun 	node_index = sci_remote_node_table_allocate_remote_node(
2503*4882a593Smuzhiyun 		&ihost->available_remote_nodes, remote_node_count
2504*4882a593Smuzhiyun 		);
2505*4882a593Smuzhiyun 
2506*4882a593Smuzhiyun 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2507*4882a593Smuzhiyun 		ihost->device_table[node_index] = idev;
2508*4882a593Smuzhiyun 
2509*4882a593Smuzhiyun 		*node_id = node_index;
2510*4882a593Smuzhiyun 
2511*4882a593Smuzhiyun 		return SCI_SUCCESS;
2512*4882a593Smuzhiyun 	}
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2515*4882a593Smuzhiyun }
2516*4882a593Smuzhiyun 
sci_controller_free_remote_node_context(struct isci_host * ihost,struct isci_remote_device * idev,u16 node_id)2517*4882a593Smuzhiyun void sci_controller_free_remote_node_context(struct isci_host *ihost,
2518*4882a593Smuzhiyun 					     struct isci_remote_device *idev,
2519*4882a593Smuzhiyun 					     u16 node_id)
2520*4882a593Smuzhiyun {
2521*4882a593Smuzhiyun 	u32 remote_node_count = sci_remote_device_node_count(idev);
2522*4882a593Smuzhiyun 
2523*4882a593Smuzhiyun 	if (ihost->device_table[node_id] == idev) {
2524*4882a593Smuzhiyun 		ihost->device_table[node_id] = NULL;
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun 		sci_remote_node_table_release_remote_node_index(
2527*4882a593Smuzhiyun 			&ihost->available_remote_nodes, remote_node_count, node_id
2528*4882a593Smuzhiyun 			);
2529*4882a593Smuzhiyun 	}
2530*4882a593Smuzhiyun }
2531*4882a593Smuzhiyun 
sci_controller_copy_sata_response(void * response_buffer,void * frame_header,void * frame_buffer)2532*4882a593Smuzhiyun void sci_controller_copy_sata_response(void *response_buffer,
2533*4882a593Smuzhiyun 				       void *frame_header,
2534*4882a593Smuzhiyun 				       void *frame_buffer)
2535*4882a593Smuzhiyun {
2536*4882a593Smuzhiyun 	/* XXX type safety? */
2537*4882a593Smuzhiyun 	memcpy(response_buffer, frame_header, sizeof(u32));
2538*4882a593Smuzhiyun 
2539*4882a593Smuzhiyun 	memcpy(response_buffer + sizeof(u32),
2540*4882a593Smuzhiyun 	       frame_buffer,
2541*4882a593Smuzhiyun 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2542*4882a593Smuzhiyun }
2543*4882a593Smuzhiyun 
sci_controller_release_frame(struct isci_host * ihost,u32 frame_index)2544*4882a593Smuzhiyun void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
2545*4882a593Smuzhiyun {
2546*4882a593Smuzhiyun 	if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
2547*4882a593Smuzhiyun 		writel(ihost->uf_control.get,
2548*4882a593Smuzhiyun 			&ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
2549*4882a593Smuzhiyun }
2550*4882a593Smuzhiyun 
isci_tci_free(struct isci_host * ihost,u16 tci)2551*4882a593Smuzhiyun void isci_tci_free(struct isci_host *ihost, u16 tci)
2552*4882a593Smuzhiyun {
2553*4882a593Smuzhiyun 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2554*4882a593Smuzhiyun 
2555*4882a593Smuzhiyun 	ihost->tci_pool[tail] = tci;
2556*4882a593Smuzhiyun 	ihost->tci_tail = tail + 1;
2557*4882a593Smuzhiyun }
2558*4882a593Smuzhiyun 
isci_tci_alloc(struct isci_host * ihost)2559*4882a593Smuzhiyun static u16 isci_tci_alloc(struct isci_host *ihost)
2560*4882a593Smuzhiyun {
2561*4882a593Smuzhiyun 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2562*4882a593Smuzhiyun 	u16 tci = ihost->tci_pool[head];
2563*4882a593Smuzhiyun 
2564*4882a593Smuzhiyun 	ihost->tci_head = head + 1;
2565*4882a593Smuzhiyun 	return tci;
2566*4882a593Smuzhiyun }
2567*4882a593Smuzhiyun 
isci_tci_space(struct isci_host * ihost)2568*4882a593Smuzhiyun static u16 isci_tci_space(struct isci_host *ihost)
2569*4882a593Smuzhiyun {
2570*4882a593Smuzhiyun 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2571*4882a593Smuzhiyun }
2572*4882a593Smuzhiyun 
isci_alloc_tag(struct isci_host * ihost)2573*4882a593Smuzhiyun u16 isci_alloc_tag(struct isci_host *ihost)
2574*4882a593Smuzhiyun {
2575*4882a593Smuzhiyun 	if (isci_tci_space(ihost)) {
2576*4882a593Smuzhiyun 		u16 tci = isci_tci_alloc(ihost);
2577*4882a593Smuzhiyun 		u8 seq = ihost->io_request_sequence[tci];
2578*4882a593Smuzhiyun 
2579*4882a593Smuzhiyun 		return ISCI_TAG(seq, tci);
2580*4882a593Smuzhiyun 	}
2581*4882a593Smuzhiyun 
2582*4882a593Smuzhiyun 	return SCI_CONTROLLER_INVALID_IO_TAG;
2583*4882a593Smuzhiyun }
2584*4882a593Smuzhiyun 
isci_free_tag(struct isci_host * ihost,u16 io_tag)2585*4882a593Smuzhiyun enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2586*4882a593Smuzhiyun {
2587*4882a593Smuzhiyun 	u16 tci = ISCI_TAG_TCI(io_tag);
2588*4882a593Smuzhiyun 	u16 seq = ISCI_TAG_SEQ(io_tag);
2589*4882a593Smuzhiyun 
2590*4882a593Smuzhiyun 	/* prevent tail from passing head */
2591*4882a593Smuzhiyun 	if (isci_tci_active(ihost) == 0)
2592*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_IO_TAG;
2593*4882a593Smuzhiyun 
2594*4882a593Smuzhiyun 	if (seq == ihost->io_request_sequence[tci]) {
2595*4882a593Smuzhiyun 		ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2596*4882a593Smuzhiyun 
2597*4882a593Smuzhiyun 		isci_tci_free(ihost, tci);
2598*4882a593Smuzhiyun 
2599*4882a593Smuzhiyun 		return SCI_SUCCESS;
2600*4882a593Smuzhiyun 	}
2601*4882a593Smuzhiyun 	return SCI_FAILURE_INVALID_IO_TAG;
2602*4882a593Smuzhiyun }
2603*4882a593Smuzhiyun 
sci_controller_start_io(struct isci_host * ihost,struct isci_remote_device * idev,struct isci_request * ireq)2604*4882a593Smuzhiyun enum sci_status sci_controller_start_io(struct isci_host *ihost,
2605*4882a593Smuzhiyun 					struct isci_remote_device *idev,
2606*4882a593Smuzhiyun 					struct isci_request *ireq)
2607*4882a593Smuzhiyun {
2608*4882a593Smuzhiyun 	enum sci_status status;
2609*4882a593Smuzhiyun 
2610*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_READY) {
2611*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
2612*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
2613*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
2614*4882a593Smuzhiyun 	}
2615*4882a593Smuzhiyun 
2616*4882a593Smuzhiyun 	status = sci_remote_device_start_io(ihost, idev, ireq);
2617*4882a593Smuzhiyun 	if (status != SCI_SUCCESS)
2618*4882a593Smuzhiyun 		return status;
2619*4882a593Smuzhiyun 
2620*4882a593Smuzhiyun 	set_bit(IREQ_ACTIVE, &ireq->flags);
2621*4882a593Smuzhiyun 	sci_controller_post_request(ihost, ireq->post_context);
2622*4882a593Smuzhiyun 	return SCI_SUCCESS;
2623*4882a593Smuzhiyun }
2624*4882a593Smuzhiyun 
sci_controller_terminate_request(struct isci_host * ihost,struct isci_remote_device * idev,struct isci_request * ireq)2625*4882a593Smuzhiyun enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
2626*4882a593Smuzhiyun 						 struct isci_remote_device *idev,
2627*4882a593Smuzhiyun 						 struct isci_request *ireq)
2628*4882a593Smuzhiyun {
2629*4882a593Smuzhiyun 	/* terminate an ongoing (i.e. started) core IO request.  This does not
2630*4882a593Smuzhiyun 	 * abort the IO request at the target, but rather removes the IO
2631*4882a593Smuzhiyun 	 * request from the host controller.
2632*4882a593Smuzhiyun 	 */
2633*4882a593Smuzhiyun 	enum sci_status status;
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_READY) {
2636*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
2637*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
2638*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
2639*4882a593Smuzhiyun 	}
2640*4882a593Smuzhiyun 	status = sci_io_request_terminate(ireq);
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun 	dev_dbg(&ihost->pdev->dev, "%s: status=%d; ireq=%p; flags=%lx\n",
2643*4882a593Smuzhiyun 		__func__, status, ireq, ireq->flags);
2644*4882a593Smuzhiyun 
2645*4882a593Smuzhiyun 	if ((status == SCI_SUCCESS) &&
2646*4882a593Smuzhiyun 	    !test_bit(IREQ_PENDING_ABORT, &ireq->flags) &&
2647*4882a593Smuzhiyun 	    !test_and_set_bit(IREQ_TC_ABORT_POSTED, &ireq->flags)) {
2648*4882a593Smuzhiyun 		/* Utilize the original post context command and or in the
2649*4882a593Smuzhiyun 		 * POST_TC_ABORT request sub-type.
2650*4882a593Smuzhiyun 		 */
2651*4882a593Smuzhiyun 		sci_controller_post_request(
2652*4882a593Smuzhiyun 			ihost, ireq->post_context |
2653*4882a593Smuzhiyun 				SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2654*4882a593Smuzhiyun 	}
2655*4882a593Smuzhiyun 	return status;
2656*4882a593Smuzhiyun }
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun /**
2659*4882a593Smuzhiyun  * sci_controller_complete_io() - This method will perform core specific
2660*4882a593Smuzhiyun  *    completion operations for an IO request.  After this method is invoked,
2661*4882a593Smuzhiyun  *    the user should consider the IO request as invalid until it is properly
2662*4882a593Smuzhiyun  *    reused (i.e. re-constructed).
2663*4882a593Smuzhiyun  * @ihost: The handle to the controller object for which to complete the
2664*4882a593Smuzhiyun  *    IO request.
2665*4882a593Smuzhiyun  * @idev: The handle to the remote device object for which to complete
2666*4882a593Smuzhiyun  *    the IO request.
2667*4882a593Smuzhiyun  * @ireq: the handle to the io request object to complete.
2668*4882a593Smuzhiyun  */
sci_controller_complete_io(struct isci_host * ihost,struct isci_remote_device * idev,struct isci_request * ireq)2669*4882a593Smuzhiyun enum sci_status sci_controller_complete_io(struct isci_host *ihost,
2670*4882a593Smuzhiyun 					   struct isci_remote_device *idev,
2671*4882a593Smuzhiyun 					   struct isci_request *ireq)
2672*4882a593Smuzhiyun {
2673*4882a593Smuzhiyun 	enum sci_status status;
2674*4882a593Smuzhiyun 
2675*4882a593Smuzhiyun 	switch (ihost->sm.current_state_id) {
2676*4882a593Smuzhiyun 	case SCIC_STOPPING:
2677*4882a593Smuzhiyun 		/* XXX: Implement this function */
2678*4882a593Smuzhiyun 		return SCI_FAILURE;
2679*4882a593Smuzhiyun 	case SCIC_READY:
2680*4882a593Smuzhiyun 		status = sci_remote_device_complete_io(ihost, idev, ireq);
2681*4882a593Smuzhiyun 		if (status != SCI_SUCCESS)
2682*4882a593Smuzhiyun 			return status;
2683*4882a593Smuzhiyun 
2684*4882a593Smuzhiyun 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2685*4882a593Smuzhiyun 		return SCI_SUCCESS;
2686*4882a593Smuzhiyun 	default:
2687*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
2688*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
2689*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
2690*4882a593Smuzhiyun 	}
2691*4882a593Smuzhiyun 
2692*4882a593Smuzhiyun }
2693*4882a593Smuzhiyun 
sci_controller_continue_io(struct isci_request * ireq)2694*4882a593Smuzhiyun enum sci_status sci_controller_continue_io(struct isci_request *ireq)
2695*4882a593Smuzhiyun {
2696*4882a593Smuzhiyun 	struct isci_host *ihost = ireq->owning_controller;
2697*4882a593Smuzhiyun 
2698*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_READY) {
2699*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
2700*4882a593Smuzhiyun 			 __func__, ihost->sm.current_state_id);
2701*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
2702*4882a593Smuzhiyun 	}
2703*4882a593Smuzhiyun 
2704*4882a593Smuzhiyun 	set_bit(IREQ_ACTIVE, &ireq->flags);
2705*4882a593Smuzhiyun 	sci_controller_post_request(ihost, ireq->post_context);
2706*4882a593Smuzhiyun 	return SCI_SUCCESS;
2707*4882a593Smuzhiyun }
2708*4882a593Smuzhiyun 
2709*4882a593Smuzhiyun /**
2710*4882a593Smuzhiyun  * sci_controller_start_task() - This method is called by the SCIC user to
2711*4882a593Smuzhiyun  *    send/start a framework task management request.
2712*4882a593Smuzhiyun  * @controller: the handle to the controller object for which to start the task
2713*4882a593Smuzhiyun  *    management request.
2714*4882a593Smuzhiyun  * @remote_device: the handle to the remote device object for which to start
2715*4882a593Smuzhiyun  *    the task management request.
2716*4882a593Smuzhiyun  * @task_request: the handle to the task request object to start.
2717*4882a593Smuzhiyun  */
sci_controller_start_task(struct isci_host * ihost,struct isci_remote_device * idev,struct isci_request * ireq)2718*4882a593Smuzhiyun enum sci_status sci_controller_start_task(struct isci_host *ihost,
2719*4882a593Smuzhiyun 					  struct isci_remote_device *idev,
2720*4882a593Smuzhiyun 					  struct isci_request *ireq)
2721*4882a593Smuzhiyun {
2722*4882a593Smuzhiyun 	enum sci_status status;
2723*4882a593Smuzhiyun 
2724*4882a593Smuzhiyun 	if (ihost->sm.current_state_id != SCIC_READY) {
2725*4882a593Smuzhiyun 		dev_warn(&ihost->pdev->dev,
2726*4882a593Smuzhiyun 			 "%s: SCIC Controller starting task from invalid "
2727*4882a593Smuzhiyun 			 "state\n",
2728*4882a593Smuzhiyun 			 __func__);
2729*4882a593Smuzhiyun 		return SCI_FAILURE_INVALID_STATE;
2730*4882a593Smuzhiyun 	}
2731*4882a593Smuzhiyun 
2732*4882a593Smuzhiyun 	status = sci_remote_device_start_task(ihost, idev, ireq);
2733*4882a593Smuzhiyun 	switch (status) {
2734*4882a593Smuzhiyun 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2735*4882a593Smuzhiyun 		set_bit(IREQ_ACTIVE, &ireq->flags);
2736*4882a593Smuzhiyun 
2737*4882a593Smuzhiyun 		/*
2738*4882a593Smuzhiyun 		 * We will let framework know this task request started successfully,
2739*4882a593Smuzhiyun 		 * although core is still woring on starting the request (to post tc when
2740*4882a593Smuzhiyun 		 * RNC is resumed.)
2741*4882a593Smuzhiyun 		 */
2742*4882a593Smuzhiyun 		return SCI_SUCCESS;
2743*4882a593Smuzhiyun 	case SCI_SUCCESS:
2744*4882a593Smuzhiyun 		set_bit(IREQ_ACTIVE, &ireq->flags);
2745*4882a593Smuzhiyun 		sci_controller_post_request(ihost, ireq->post_context);
2746*4882a593Smuzhiyun 		break;
2747*4882a593Smuzhiyun 	default:
2748*4882a593Smuzhiyun 		break;
2749*4882a593Smuzhiyun 	}
2750*4882a593Smuzhiyun 
2751*4882a593Smuzhiyun 	return status;
2752*4882a593Smuzhiyun }
2753*4882a593Smuzhiyun 
sci_write_gpio_tx_gp(struct isci_host * ihost,u8 reg_index,u8 reg_count,u8 * write_data)2754*4882a593Smuzhiyun static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
2755*4882a593Smuzhiyun {
2756*4882a593Smuzhiyun 	int d;
2757*4882a593Smuzhiyun 
2758*4882a593Smuzhiyun 	/* no support for TX_GP_CFG */
2759*4882a593Smuzhiyun 	if (reg_index == 0)
2760*4882a593Smuzhiyun 		return -EINVAL;
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun 	for (d = 0; d < isci_gpio_count(ihost); d++) {
2763*4882a593Smuzhiyun 		u32 val = 0x444; /* all ODx.n clear */
2764*4882a593Smuzhiyun 		int i;
2765*4882a593Smuzhiyun 
2766*4882a593Smuzhiyun 		for (i = 0; i < 3; i++) {
2767*4882a593Smuzhiyun 			int bit;
2768*4882a593Smuzhiyun 
2769*4882a593Smuzhiyun 			bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
2770*4882a593Smuzhiyun 						       write_data, reg_index,
2771*4882a593Smuzhiyun 						       reg_count);
2772*4882a593Smuzhiyun 			if (bit < 0)
2773*4882a593Smuzhiyun 				break;
2774*4882a593Smuzhiyun 
2775*4882a593Smuzhiyun 			/* if od is set, clear the 'invert' bit */
2776*4882a593Smuzhiyun 			val &= ~(bit << ((i << 2) + 2));
2777*4882a593Smuzhiyun 		}
2778*4882a593Smuzhiyun 
2779*4882a593Smuzhiyun 		if (i < 3)
2780*4882a593Smuzhiyun 			break;
2781*4882a593Smuzhiyun 		writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
2782*4882a593Smuzhiyun 	}
2783*4882a593Smuzhiyun 
2784*4882a593Smuzhiyun 	/* unless reg_index is > 1, we should always be able to write at
2785*4882a593Smuzhiyun 	 * least one register
2786*4882a593Smuzhiyun 	 */
2787*4882a593Smuzhiyun 	return d > 0;
2788*4882a593Smuzhiyun }
2789*4882a593Smuzhiyun 
isci_gpio_write(struct sas_ha_struct * sas_ha,u8 reg_type,u8 reg_index,u8 reg_count,u8 * write_data)2790*4882a593Smuzhiyun int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
2791*4882a593Smuzhiyun 		    u8 reg_count, u8 *write_data)
2792*4882a593Smuzhiyun {
2793*4882a593Smuzhiyun 	struct isci_host *ihost = sas_ha->lldd_ha;
2794*4882a593Smuzhiyun 	int written;
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun 	switch (reg_type) {
2797*4882a593Smuzhiyun 	case SAS_GPIO_REG_TX_GP:
2798*4882a593Smuzhiyun 		written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
2799*4882a593Smuzhiyun 		break;
2800*4882a593Smuzhiyun 	default:
2801*4882a593Smuzhiyun 		written = -EINVAL;
2802*4882a593Smuzhiyun 	}
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun 	return written;
2805*4882a593Smuzhiyun }
2806