xref: /openbmc/linux/drivers/scsi/isci/host.c (revision 2396a2650a5a39634e3ad6b29e1104944e5ab88f)
16f231ddaSDan Williams /*
26f231ddaSDan Williams  * This file is provided under a dual BSD/GPLv2 license.  When using or
36f231ddaSDan Williams  * redistributing this file, you may do so under either license.
46f231ddaSDan Williams  *
56f231ddaSDan Williams  * GPL LICENSE SUMMARY
66f231ddaSDan Williams  *
76f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
86f231ddaSDan Williams  *
96f231ddaSDan Williams  * This program is free software; you can redistribute it and/or modify
106f231ddaSDan Williams  * it under the terms of version 2 of the GNU General Public License as
116f231ddaSDan Williams  * published by the Free Software Foundation.
126f231ddaSDan Williams  *
136f231ddaSDan Williams  * This program is distributed in the hope that it will be useful, but
146f231ddaSDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
156f231ddaSDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
166f231ddaSDan Williams  * General Public License for more details.
176f231ddaSDan Williams  *
186f231ddaSDan Williams  * You should have received a copy of the GNU General Public License
196f231ddaSDan Williams  * along with this program; if not, write to the Free Software
206f231ddaSDan Williams  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
216f231ddaSDan Williams  * The full GNU General Public License is included in this distribution
226f231ddaSDan Williams  * in the file called LICENSE.GPL.
236f231ddaSDan Williams  *
246f231ddaSDan Williams  * BSD LICENSE
256f231ddaSDan Williams  *
266f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
276f231ddaSDan Williams  * All rights reserved.
286f231ddaSDan Williams  *
296f231ddaSDan Williams  * Redistribution and use in source and binary forms, with or without
306f231ddaSDan Williams  * modification, are permitted provided that the following conditions
316f231ddaSDan Williams  * are met:
326f231ddaSDan Williams  *
336f231ddaSDan Williams  *   * Redistributions of source code must retain the above copyright
346f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer.
356f231ddaSDan Williams  *   * Redistributions in binary form must reproduce the above copyright
366f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer in
376f231ddaSDan Williams  *     the documentation and/or other materials provided with the
386f231ddaSDan Williams  *     distribution.
396f231ddaSDan Williams  *   * Neither the name of Intel Corporation nor the names of its
406f231ddaSDan Williams  *     contributors may be used to endorse or promote products derived
416f231ddaSDan Williams  *     from this software without specific prior written permission.
426f231ddaSDan Williams  *
436f231ddaSDan Williams  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
446f231ddaSDan Williams  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
456f231ddaSDan Williams  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
466f231ddaSDan Williams  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
476f231ddaSDan Williams  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
486f231ddaSDan Williams  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
496f231ddaSDan Williams  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
506f231ddaSDan Williams  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
516f231ddaSDan Williams  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
526f231ddaSDan Williams  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
536f231ddaSDan Williams  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
546f231ddaSDan Williams  */
55ac668c69SDan Williams #include <linux/circ_buf.h>
56cc9203bfSDan Williams #include <linux/device.h>
57cc9203bfSDan Williams #include <scsi/sas.h>
58cc9203bfSDan Williams #include "host.h"
596f231ddaSDan Williams #include "isci.h"
606f231ddaSDan Williams #include "port.h"
61d044af17SDan Williams #include "probe_roms.h"
62cc9203bfSDan Williams #include "remote_device.h"
63cc9203bfSDan Williams #include "request.h"
64cc9203bfSDan Williams #include "scu_completion_codes.h"
65cc9203bfSDan Williams #include "scu_event_codes.h"
6663a3a15fSDan Williams #include "registers.h"
67cc9203bfSDan Williams #include "scu_remote_node_context.h"
68cc9203bfSDan Williams #include "scu_task_context.h"
696f231ddaSDan Williams 
70cc9203bfSDan Williams #define SCU_CONTEXT_RAM_INIT_STALL_TIME      200
71cc9203bfSDan Williams 
727c78da31SDan Williams #define smu_max_ports(dcc_value) \
73cc9203bfSDan Williams 	(\
74cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
75cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
76cc9203bfSDan Williams 	)
77cc9203bfSDan Williams 
787c78da31SDan Williams #define smu_max_task_contexts(dcc_value)	\
79cc9203bfSDan Williams 	(\
80cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
81cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
82cc9203bfSDan Williams 	)
83cc9203bfSDan Williams 
847c78da31SDan Williams #define smu_max_rncs(dcc_value) \
85cc9203bfSDan Williams 	(\
86cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
87cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
88cc9203bfSDan Williams 	)
89cc9203bfSDan Williams 
90cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT      100
91cc9203bfSDan Williams 
92cc9203bfSDan Williams /**
93cc9203bfSDan Williams  *
94cc9203bfSDan Williams  *
95cc9203bfSDan Williams  * The number of milliseconds to wait while a given phy is consuming power
96cc9203bfSDan Williams  * before allowing another set of phys to consume power. Ultimately, this will
97cc9203bfSDan Williams  * be specified by OEM parameter.
98cc9203bfSDan Williams  */
99cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
100cc9203bfSDan Williams 
101cc9203bfSDan Williams /**
102cc9203bfSDan Williams  * NORMALIZE_PUT_POINTER() -
103cc9203bfSDan Williams  *
104cc9203bfSDan Williams  * This macro will normalize the completion queue put pointer so its value can
105cc9203bfSDan Williams  * be used as an array inde
106cc9203bfSDan Williams  */
107cc9203bfSDan Williams #define NORMALIZE_PUT_POINTER(x) \
108cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
109cc9203bfSDan Williams 
110cc9203bfSDan Williams 
111cc9203bfSDan Williams /**
112cc9203bfSDan Williams  * NORMALIZE_EVENT_POINTER() -
113cc9203bfSDan Williams  *
114cc9203bfSDan Williams  * This macro will normalize the completion queue event entry so its value can
115cc9203bfSDan Williams  * be used as an index.
116cc9203bfSDan Williams  */
117cc9203bfSDan Williams #define NORMALIZE_EVENT_POINTER(x) \
118cc9203bfSDan Williams 	(\
119cc9203bfSDan Williams 		((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
120cc9203bfSDan Williams 		>> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT	\
121cc9203bfSDan Williams 	)
122cc9203bfSDan Williams 
123cc9203bfSDan Williams /**
124cc9203bfSDan Williams  * NORMALIZE_GET_POINTER() -
125cc9203bfSDan Williams  *
126cc9203bfSDan Williams  * This macro will normalize the completion queue get pointer so its value can
127cc9203bfSDan Williams  * be used as an index into an array
128cc9203bfSDan Williams  */
129cc9203bfSDan Williams #define NORMALIZE_GET_POINTER(x) \
130cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
131cc9203bfSDan Williams 
132cc9203bfSDan Williams /**
133cc9203bfSDan Williams  * NORMALIZE_GET_POINTER_CYCLE_BIT() -
134cc9203bfSDan Williams  *
135cc9203bfSDan Williams  * This macro will normalize the completion queue cycle pointer so it matches
136cc9203bfSDan Williams  * the completion queue cycle bit
137cc9203bfSDan Williams  */
138cc9203bfSDan Williams #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
139cc9203bfSDan Williams 	((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
140cc9203bfSDan Williams 
141cc9203bfSDan Williams /**
142cc9203bfSDan Williams  * COMPLETION_QUEUE_CYCLE_BIT() -
143cc9203bfSDan Williams  *
144cc9203bfSDan Williams  * This macro will return the cycle bit of the completion queue entry
145cc9203bfSDan Williams  */
146cc9203bfSDan Williams #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
147cc9203bfSDan Williams 
14812ef6544SEdmund Nadolski /* Init the state machine and call the state entry function (if any) */
14912ef6544SEdmund Nadolski void sci_init_sm(struct sci_base_state_machine *sm,
15012ef6544SEdmund Nadolski 		 const struct sci_base_state *state_table, u32 initial_state)
15112ef6544SEdmund Nadolski {
15212ef6544SEdmund Nadolski 	sci_state_transition_t handler;
15312ef6544SEdmund Nadolski 
15412ef6544SEdmund Nadolski 	sm->initial_state_id    = initial_state;
15512ef6544SEdmund Nadolski 	sm->previous_state_id   = initial_state;
15612ef6544SEdmund Nadolski 	sm->current_state_id    = initial_state;
15712ef6544SEdmund Nadolski 	sm->state_table         = state_table;
15812ef6544SEdmund Nadolski 
15912ef6544SEdmund Nadolski 	handler = sm->state_table[initial_state].enter_state;
16012ef6544SEdmund Nadolski 	if (handler)
16112ef6544SEdmund Nadolski 		handler(sm);
16212ef6544SEdmund Nadolski }
16312ef6544SEdmund Nadolski 
16412ef6544SEdmund Nadolski /* Call the state exit fn, update the current state, call the state entry fn */
16512ef6544SEdmund Nadolski void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
16612ef6544SEdmund Nadolski {
16712ef6544SEdmund Nadolski 	sci_state_transition_t handler;
16812ef6544SEdmund Nadolski 
16912ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].exit_state;
17012ef6544SEdmund Nadolski 	if (handler)
17112ef6544SEdmund Nadolski 		handler(sm);
17212ef6544SEdmund Nadolski 
17312ef6544SEdmund Nadolski 	sm->previous_state_id = sm->current_state_id;
17412ef6544SEdmund Nadolski 	sm->current_state_id = next_state;
17512ef6544SEdmund Nadolski 
17612ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].enter_state;
17712ef6544SEdmund Nadolski 	if (handler)
17812ef6544SEdmund Nadolski 		handler(sm);
17912ef6544SEdmund Nadolski }
18012ef6544SEdmund Nadolski 
18189a7301fSDan Williams static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
182cc9203bfSDan Williams {
183d9dcb4baSDan Williams 	u32 get_value = ihost->completion_queue_get;
184cc9203bfSDan Williams 	u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
185cc9203bfSDan Williams 
186cc9203bfSDan Williams 	if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
187d9dcb4baSDan Williams 	    COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
188cc9203bfSDan Williams 		return true;
189cc9203bfSDan Williams 
190cc9203bfSDan Williams 	return false;
191cc9203bfSDan Williams }
192cc9203bfSDan Williams 
19389a7301fSDan Williams static bool sci_controller_isr(struct isci_host *ihost)
194cc9203bfSDan Williams {
195*2396a265SDan Williams 	if (sci_controller_completion_queue_has_entries(ihost))
196cc9203bfSDan Williams 		return true;
197*2396a265SDan Williams 
198*2396a265SDan Williams 	/* we have a spurious interrupt it could be that we have already
199*2396a265SDan Williams 	 * emptied the completion queue from a previous interrupt
200*2396a265SDan Williams 	 * FIXME: really!?
201*2396a265SDan Williams 	 */
202d9dcb4baSDan Williams 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
203cc9203bfSDan Williams 
204*2396a265SDan Williams 	/* There is a race in the hardware that could cause us not to be
205*2396a265SDan Williams 	 * notified of an interrupt completion if we do not take this
206*2396a265SDan Williams 	 * step.  We will mask then unmask the interrupts so if there is
207*2396a265SDan Williams 	 * another interrupt pending the clearing of the interrupt
208*2396a265SDan Williams 	 * source we get the next interrupt message.
209*2396a265SDan Williams 	 */
210*2396a265SDan Williams 	spin_lock(&ihost->scic_lock);
211*2396a265SDan Williams 	if (test_bit(IHOST_IRQ_ENABLED, &ihost->flags)) {
212d9dcb4baSDan Williams 		writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
213d9dcb4baSDan Williams 		writel(0, &ihost->smu_registers->interrupt_mask);
214cc9203bfSDan Williams 	}
215*2396a265SDan Williams 	spin_unlock(&ihost->scic_lock);
216cc9203bfSDan Williams 
217cc9203bfSDan Williams 	return false;
218cc9203bfSDan Williams }
219cc9203bfSDan Williams 
220c7ef4031SDan Williams irqreturn_t isci_msix_isr(int vec, void *data)
2216f231ddaSDan Williams {
222c7ef4031SDan Williams 	struct isci_host *ihost = data;
2236f231ddaSDan Williams 
22489a7301fSDan Williams 	if (sci_controller_isr(ihost))
225c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
2266f231ddaSDan Williams 
227c7ef4031SDan Williams 	return IRQ_HANDLED;
228c7ef4031SDan Williams }
229c7ef4031SDan Williams 
23089a7301fSDan Williams static bool sci_controller_error_isr(struct isci_host *ihost)
231cc9203bfSDan Williams {
232cc9203bfSDan Williams 	u32 interrupt_status;
233cc9203bfSDan Williams 
234cc9203bfSDan Williams 	interrupt_status =
235d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
236cc9203bfSDan Williams 	interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
237cc9203bfSDan Williams 
238cc9203bfSDan Williams 	if (interrupt_status != 0) {
239cc9203bfSDan Williams 		/*
240cc9203bfSDan Williams 		 * There is an error interrupt pending so let it through and handle
241cc9203bfSDan Williams 		 * in the callback */
242cc9203bfSDan Williams 		return true;
243cc9203bfSDan Williams 	}
244cc9203bfSDan Williams 
245cc9203bfSDan Williams 	/*
246cc9203bfSDan Williams 	 * There is a race in the hardware that could cause us not to be notified
247cc9203bfSDan Williams 	 * of an interrupt completion if we do not take this step.  We will mask
248cc9203bfSDan Williams 	 * then unmask the error interrupts so if there was another interrupt
249cc9203bfSDan Williams 	 * pending we will be notified.
250cc9203bfSDan Williams 	 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
251d9dcb4baSDan Williams 	writel(0xff, &ihost->smu_registers->interrupt_mask);
252d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
253cc9203bfSDan Williams 
254cc9203bfSDan Williams 	return false;
255cc9203bfSDan Williams }
256cc9203bfSDan Williams 
25789a7301fSDan Williams static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
258cc9203bfSDan Williams {
25989a7301fSDan Williams 	u32 index = SCU_GET_COMPLETION_INDEX(ent);
260db056250SDan Williams 	struct isci_request *ireq = ihost->reqs[index];
261cc9203bfSDan Williams 
262cc9203bfSDan Williams 	/* Make sure that we really want to process this IO request */
263db056250SDan Williams 	if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
2645076a1a9SDan Williams 	    ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
265d9dcb4baSDan Williams 	    ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
26689a7301fSDan Williams 		/* Yep this is a valid io request pass it along to the
26789a7301fSDan Williams 		 * io request handler
26889a7301fSDan Williams 		 */
26989a7301fSDan Williams 		sci_io_request_tc_completion(ireq, ent);
270cc9203bfSDan Williams }
271cc9203bfSDan Williams 
27289a7301fSDan Williams static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
273cc9203bfSDan Williams {
274cc9203bfSDan Williams 	u32 index;
2755076a1a9SDan Williams 	struct isci_request *ireq;
27678a6f06eSDan Williams 	struct isci_remote_device *idev;
277cc9203bfSDan Williams 
27889a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
279cc9203bfSDan Williams 
28089a7301fSDan Williams 	switch (scu_get_command_request_type(ent)) {
281cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
282cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
283d9dcb4baSDan Williams 		ireq = ihost->reqs[index];
284d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
28589a7301fSDan Williams 			 __func__, ent, ireq);
286cc9203bfSDan Williams 		/* @todo For a post TC operation we need to fail the IO
287cc9203bfSDan Williams 		 * request
288cc9203bfSDan Williams 		 */
289cc9203bfSDan Williams 		break;
290cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
291cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
292cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
293d9dcb4baSDan Williams 		idev = ihost->device_table[index];
294d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
29589a7301fSDan Williams 			 __func__, ent, idev);
296cc9203bfSDan Williams 		/* @todo For a port RNC operation we need to fail the
297cc9203bfSDan Williams 		 * device
298cc9203bfSDan Williams 		 */
299cc9203bfSDan Williams 		break;
300cc9203bfSDan Williams 	default:
301d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
30289a7301fSDan Williams 			 __func__, ent);
303cc9203bfSDan Williams 		break;
304cc9203bfSDan Williams 	}
305cc9203bfSDan Williams }
306cc9203bfSDan Williams 
30789a7301fSDan Williams static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
308cc9203bfSDan Williams {
309cc9203bfSDan Williams 	u32 index;
310cc9203bfSDan Williams 	u32 frame_index;
311cc9203bfSDan Williams 
312cc9203bfSDan Williams 	struct scu_unsolicited_frame_header *frame_header;
31385280955SDan Williams 	struct isci_phy *iphy;
31478a6f06eSDan Williams 	struct isci_remote_device *idev;
315cc9203bfSDan Williams 
316cc9203bfSDan Williams 	enum sci_status result = SCI_FAILURE;
317cc9203bfSDan Williams 
31889a7301fSDan Williams 	frame_index = SCU_GET_FRAME_INDEX(ent);
319cc9203bfSDan Williams 
320d9dcb4baSDan Williams 	frame_header = ihost->uf_control.buffers.array[frame_index].header;
321d9dcb4baSDan Williams 	ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
322cc9203bfSDan Williams 
32389a7301fSDan Williams 	if (SCU_GET_FRAME_ERROR(ent)) {
324cc9203bfSDan Williams 		/*
325cc9203bfSDan Williams 		 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
326cc9203bfSDan Williams 		 * /       this cause a problem? We expect the phy initialization will
327cc9203bfSDan Williams 		 * /       fail if there is an error in the frame. */
32889a7301fSDan Williams 		sci_controller_release_frame(ihost, frame_index);
329cc9203bfSDan Williams 		return;
330cc9203bfSDan Williams 	}
331cc9203bfSDan Williams 
332cc9203bfSDan Williams 	if (frame_header->is_address_frame) {
33389a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
33485280955SDan Williams 		iphy = &ihost->phys[index];
33589a7301fSDan Williams 		result = sci_phy_frame_handler(iphy, frame_index);
336cc9203bfSDan Williams 	} else {
337cc9203bfSDan Williams 
33889a7301fSDan Williams 		index = SCU_GET_COMPLETION_INDEX(ent);
339cc9203bfSDan Williams 
340cc9203bfSDan Williams 		if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
341cc9203bfSDan Williams 			/*
342cc9203bfSDan Williams 			 * This is a signature fis or a frame from a direct attached SATA
343cc9203bfSDan Williams 			 * device that has not yet been created.  In either case forwared
344cc9203bfSDan Williams 			 * the frame to the PE and let it take care of the frame data. */
34589a7301fSDan Williams 			index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
34685280955SDan Williams 			iphy = &ihost->phys[index];
34789a7301fSDan Williams 			result = sci_phy_frame_handler(iphy, frame_index);
348cc9203bfSDan Williams 		} else {
349d9dcb4baSDan Williams 			if (index < ihost->remote_node_entries)
350d9dcb4baSDan Williams 				idev = ihost->device_table[index];
351cc9203bfSDan Williams 			else
35278a6f06eSDan Williams 				idev = NULL;
353cc9203bfSDan Williams 
35478a6f06eSDan Williams 			if (idev != NULL)
35589a7301fSDan Williams 				result = sci_remote_device_frame_handler(idev, frame_index);
356cc9203bfSDan Williams 			else
35789a7301fSDan Williams 				sci_controller_release_frame(ihost, frame_index);
358cc9203bfSDan Williams 		}
359cc9203bfSDan Williams 	}
360cc9203bfSDan Williams 
361cc9203bfSDan Williams 	if (result != SCI_SUCCESS) {
362cc9203bfSDan Williams 		/*
363cc9203bfSDan Williams 		 * / @todo Is there any reason to report some additional error message
364cc9203bfSDan Williams 		 * /       when we get this failure notifiction? */
365cc9203bfSDan Williams 	}
366cc9203bfSDan Williams }
367cc9203bfSDan Williams 
36889a7301fSDan Williams static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
369cc9203bfSDan Williams {
37078a6f06eSDan Williams 	struct isci_remote_device *idev;
3715076a1a9SDan Williams 	struct isci_request *ireq;
37285280955SDan Williams 	struct isci_phy *iphy;
373cc9203bfSDan Williams 	u32 index;
374cc9203bfSDan Williams 
37589a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
376cc9203bfSDan Williams 
37789a7301fSDan Williams 	switch (scu_get_event_type(ent)) {
378cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
379cc9203bfSDan Williams 		/* / @todo The driver did something wrong and we need to fix the condtion. */
380d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
381cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received SMU command error "
382cc9203bfSDan Williams 			"0x%x\n",
383cc9203bfSDan Williams 			__func__,
384d9dcb4baSDan Williams 			ihost,
38589a7301fSDan Williams 			ent);
386cc9203bfSDan Williams 		break;
387cc9203bfSDan Williams 
388cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
389cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_ERROR:
390cc9203bfSDan Williams 	case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
391cc9203bfSDan Williams 		/*
392cc9203bfSDan Williams 		 * / @todo This is a hardware failure and its likely that we want to
393cc9203bfSDan Williams 		 * /       reset the controller. */
394d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
395cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received fatal controller "
396cc9203bfSDan Williams 			"event  0x%x\n",
397cc9203bfSDan Williams 			__func__,
398d9dcb4baSDan Williams 			ihost,
39989a7301fSDan Williams 			ent);
400cc9203bfSDan Williams 		break;
401cc9203bfSDan Williams 
402cc9203bfSDan Williams 	case SCU_EVENT_TYPE_TRANSPORT_ERROR:
4035076a1a9SDan Williams 		ireq = ihost->reqs[index];
40489a7301fSDan Williams 		sci_io_request_event_handler(ireq, ent);
405cc9203bfSDan Williams 		break;
406cc9203bfSDan Williams 
407cc9203bfSDan Williams 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
40889a7301fSDan Williams 		switch (scu_get_event_specifier(ent)) {
409cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
410cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
4115076a1a9SDan Williams 			ireq = ihost->reqs[index];
4125076a1a9SDan Williams 			if (ireq != NULL)
41389a7301fSDan Williams 				sci_io_request_event_handler(ireq, ent);
414cc9203bfSDan Williams 			else
415d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
416cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
417cc9203bfSDan Williams 					 "event 0x%x for io request object "
418cc9203bfSDan Williams 					 "that doesnt exist.\n",
419cc9203bfSDan Williams 					 __func__,
420d9dcb4baSDan Williams 					 ihost,
42189a7301fSDan Williams 					 ent);
422cc9203bfSDan Williams 
423cc9203bfSDan Williams 			break;
424cc9203bfSDan Williams 
425cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
426d9dcb4baSDan Williams 			idev = ihost->device_table[index];
42778a6f06eSDan Williams 			if (idev != NULL)
42889a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
429cc9203bfSDan Williams 			else
430d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
431cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
432cc9203bfSDan Williams 					 "event 0x%x for remote device object "
433cc9203bfSDan Williams 					 "that doesnt exist.\n",
434cc9203bfSDan Williams 					 __func__,
435d9dcb4baSDan Williams 					 ihost,
43689a7301fSDan Williams 					 ent);
437cc9203bfSDan Williams 
438cc9203bfSDan Williams 			break;
439cc9203bfSDan Williams 		}
440cc9203bfSDan Williams 		break;
441cc9203bfSDan Williams 
442cc9203bfSDan Williams 	case SCU_EVENT_TYPE_BROADCAST_CHANGE:
443cc9203bfSDan Williams 	/*
444cc9203bfSDan Williams 	 * direct the broadcast change event to the phy first and then let
445cc9203bfSDan Williams 	 * the phy redirect the broadcast change to the port object */
446cc9203bfSDan Williams 	case SCU_EVENT_TYPE_ERR_CNT_EVENT:
447cc9203bfSDan Williams 	/*
448cc9203bfSDan Williams 	 * direct error counter event to the phy object since that is where
449cc9203bfSDan Williams 	 * we get the event notification.  This is a type 4 event. */
450cc9203bfSDan Williams 	case SCU_EVENT_TYPE_OSSP_EVENT:
45189a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
45285280955SDan Williams 		iphy = &ihost->phys[index];
45389a7301fSDan Williams 		sci_phy_event_handler(iphy, ent);
454cc9203bfSDan Williams 		break;
455cc9203bfSDan Williams 
456cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
457cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
458cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
459d9dcb4baSDan Williams 		if (index < ihost->remote_node_entries) {
460d9dcb4baSDan Williams 			idev = ihost->device_table[index];
461cc9203bfSDan Williams 
46278a6f06eSDan Williams 			if (idev != NULL)
46389a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
464cc9203bfSDan Williams 		} else
465d9dcb4baSDan Williams 			dev_err(&ihost->pdev->dev,
466cc9203bfSDan Williams 				"%s: SCIC Controller 0x%p received event 0x%x "
467cc9203bfSDan Williams 				"for remote device object 0x%0x that doesnt "
468cc9203bfSDan Williams 				"exist.\n",
469cc9203bfSDan Williams 				__func__,
470d9dcb4baSDan Williams 				ihost,
47189a7301fSDan Williams 				ent,
472cc9203bfSDan Williams 				index);
473cc9203bfSDan Williams 
474cc9203bfSDan Williams 		break;
475cc9203bfSDan Williams 
476cc9203bfSDan Williams 	default:
477d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
478cc9203bfSDan Williams 			 "%s: SCIC Controller received unknown event code %x\n",
479cc9203bfSDan Williams 			 __func__,
48089a7301fSDan Williams 			 ent);
481cc9203bfSDan Williams 		break;
482cc9203bfSDan Williams 	}
483cc9203bfSDan Williams }
484cc9203bfSDan Williams 
48589a7301fSDan Williams static void sci_controller_process_completions(struct isci_host *ihost)
486cc9203bfSDan Williams {
487cc9203bfSDan Williams 	u32 completion_count = 0;
48889a7301fSDan Williams 	u32 ent;
489cc9203bfSDan Williams 	u32 get_index;
490cc9203bfSDan Williams 	u32 get_cycle;
491994a9303SDan Williams 	u32 event_get;
492cc9203bfSDan Williams 	u32 event_cycle;
493cc9203bfSDan Williams 
494d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
495cc9203bfSDan Williams 		"%s: completion queue begining get:0x%08x\n",
496cc9203bfSDan Williams 		__func__,
497d9dcb4baSDan Williams 		ihost->completion_queue_get);
498cc9203bfSDan Williams 
499cc9203bfSDan Williams 	/* Get the component parts of the completion queue */
500d9dcb4baSDan Williams 	get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
501d9dcb4baSDan Williams 	get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
502cc9203bfSDan Williams 
503d9dcb4baSDan Williams 	event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
504d9dcb4baSDan Williams 	event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
505cc9203bfSDan Williams 
506cc9203bfSDan Williams 	while (
507cc9203bfSDan Williams 		NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
508d9dcb4baSDan Williams 		== COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
509cc9203bfSDan Williams 		) {
510cc9203bfSDan Williams 		completion_count++;
511cc9203bfSDan Williams 
51289a7301fSDan Williams 		ent = ihost->completion_queue[get_index];
513994a9303SDan Williams 
514994a9303SDan Williams 		/* increment the get pointer and check for rollover to toggle the cycle bit */
515994a9303SDan Williams 		get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
516994a9303SDan Williams 			     (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
517994a9303SDan Williams 		get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
518cc9203bfSDan Williams 
519d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
520cc9203bfSDan Williams 			"%s: completion queue entry:0x%08x\n",
521cc9203bfSDan Williams 			__func__,
52289a7301fSDan Williams 			ent);
523cc9203bfSDan Williams 
52489a7301fSDan Williams 		switch (SCU_GET_COMPLETION_TYPE(ent)) {
525cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_TASK:
52689a7301fSDan Williams 			sci_controller_task_completion(ihost, ent);
527cc9203bfSDan Williams 			break;
528cc9203bfSDan Williams 
529cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_SDMA:
53089a7301fSDan Williams 			sci_controller_sdma_completion(ihost, ent);
531cc9203bfSDan Williams 			break;
532cc9203bfSDan Williams 
533cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_UFI:
53489a7301fSDan Williams 			sci_controller_unsolicited_frame(ihost, ent);
535cc9203bfSDan Williams 			break;
536cc9203bfSDan Williams 
537cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_EVENT:
53877cd72a5SDan Williams 			sci_controller_event_completion(ihost, ent);
53977cd72a5SDan Williams 			break;
54077cd72a5SDan Williams 
541994a9303SDan Williams 		case SCU_COMPLETION_TYPE_NOTIFY: {
542994a9303SDan Williams 			event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
543994a9303SDan Williams 				       (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
544994a9303SDan Williams 			event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
545994a9303SDan Williams 
54689a7301fSDan Williams 			sci_controller_event_completion(ihost, ent);
547cc9203bfSDan Williams 			break;
548994a9303SDan Williams 		}
549cc9203bfSDan Williams 		default:
550d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
551cc9203bfSDan Williams 				 "%s: SCIC Controller received unknown "
552cc9203bfSDan Williams 				 "completion type %x\n",
553cc9203bfSDan Williams 				 __func__,
55489a7301fSDan Williams 				 ent);
555cc9203bfSDan Williams 			break;
556cc9203bfSDan Williams 		}
557cc9203bfSDan Williams 	}
558cc9203bfSDan Williams 
559cc9203bfSDan Williams 	/* Update the get register if we completed one or more entries */
560cc9203bfSDan Williams 	if (completion_count > 0) {
561d9dcb4baSDan Williams 		ihost->completion_queue_get =
562cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(ENABLE) |
563cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
564cc9203bfSDan Williams 			event_cycle |
565994a9303SDan Williams 			SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
566cc9203bfSDan Williams 			get_cycle |
567cc9203bfSDan Williams 			SMU_CQGR_GEN_VAL(POINTER, get_index);
568cc9203bfSDan Williams 
569d9dcb4baSDan Williams 		writel(ihost->completion_queue_get,
570d9dcb4baSDan Williams 		       &ihost->smu_registers->completion_queue_get);
571cc9203bfSDan Williams 
572cc9203bfSDan Williams 	}
573cc9203bfSDan Williams 
574d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
575cc9203bfSDan Williams 		"%s: completion queue ending get:0x%08x\n",
576cc9203bfSDan Williams 		__func__,
577d9dcb4baSDan Williams 		ihost->completion_queue_get);
578cc9203bfSDan Williams 
579cc9203bfSDan Williams }
580cc9203bfSDan Williams 
58189a7301fSDan Williams static void sci_controller_error_handler(struct isci_host *ihost)
582cc9203bfSDan Williams {
583cc9203bfSDan Williams 	u32 interrupt_status;
584cc9203bfSDan Williams 
585cc9203bfSDan Williams 	interrupt_status =
586d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
587cc9203bfSDan Williams 
588cc9203bfSDan Williams 	if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
58989a7301fSDan Williams 	    sci_controller_completion_queue_has_entries(ihost)) {
590cc9203bfSDan Williams 
59189a7301fSDan Williams 		sci_controller_process_completions(ihost);
592d9dcb4baSDan Williams 		writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
593cc9203bfSDan Williams 	} else {
594d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
595cc9203bfSDan Williams 			interrupt_status);
596cc9203bfSDan Williams 
597d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_FAILED);
598cc9203bfSDan Williams 
599cc9203bfSDan Williams 		return;
600cc9203bfSDan Williams 	}
601cc9203bfSDan Williams 
602cc9203bfSDan Williams 	/* If we dont process any completions I am not sure that we want to do this.
603cc9203bfSDan Williams 	 * We are in the middle of a hardware fault and should probably be reset.
604cc9203bfSDan Williams 	 */
605d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
606cc9203bfSDan Williams }
607cc9203bfSDan Williams 
608c7ef4031SDan Williams irqreturn_t isci_intx_isr(int vec, void *data)
6096f231ddaSDan Williams {
6106f231ddaSDan Williams 	irqreturn_t ret = IRQ_NONE;
61131e824edSDan Williams 	struct isci_host *ihost = data;
6126f231ddaSDan Williams 
61389a7301fSDan Williams 	if (sci_controller_isr(ihost)) {
614d9dcb4baSDan Williams 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
615c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
6166f231ddaSDan Williams 		ret = IRQ_HANDLED;
61789a7301fSDan Williams 	} else if (sci_controller_error_isr(ihost)) {
61892f4f0f5SDan Williams 		spin_lock(&ihost->scic_lock);
61989a7301fSDan Williams 		sci_controller_error_handler(ihost);
62092f4f0f5SDan Williams 		spin_unlock(&ihost->scic_lock);
62192f4f0f5SDan Williams 		ret = IRQ_HANDLED;
6226f231ddaSDan Williams 	}
62392f4f0f5SDan Williams 
6246f231ddaSDan Williams 	return ret;
6256f231ddaSDan Williams }
6266f231ddaSDan Williams 
62792f4f0f5SDan Williams irqreturn_t isci_error_isr(int vec, void *data)
62892f4f0f5SDan Williams {
62992f4f0f5SDan Williams 	struct isci_host *ihost = data;
63092f4f0f5SDan Williams 
63189a7301fSDan Williams 	if (sci_controller_error_isr(ihost))
63289a7301fSDan Williams 		sci_controller_error_handler(ihost);
63392f4f0f5SDan Williams 
63492f4f0f5SDan Williams 	return IRQ_HANDLED;
63592f4f0f5SDan Williams }
6366f231ddaSDan Williams 
6376f231ddaSDan Williams /**
6386f231ddaSDan Williams  * isci_host_start_complete() - This function is called by the core library,
6396f231ddaSDan Williams  *    through the ISCI Module, to indicate controller start status.
6406f231ddaSDan Williams  * @isci_host: This parameter specifies the ISCI host object
6416f231ddaSDan Williams  * @completion_status: This parameter specifies the completion status from the
6426f231ddaSDan Williams  *    core library.
6436f231ddaSDan Williams  *
6446f231ddaSDan Williams  */
645cc9203bfSDan Williams static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
6466f231ddaSDan Williams {
6470cf89d1dSDan Williams 	if (completion_status != SCI_SUCCESS)
6480cf89d1dSDan Williams 		dev_info(&ihost->pdev->dev,
6490cf89d1dSDan Williams 			"controller start timed out, continuing...\n");
6500cf89d1dSDan Williams 	clear_bit(IHOST_START_PENDING, &ihost->flags);
6510cf89d1dSDan Williams 	wake_up(&ihost->eventq);
6526f231ddaSDan Williams }
6536f231ddaSDan Williams 
654c7ef4031SDan Williams int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
6556f231ddaSDan Williams {
656b1124cd3SDan Williams 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
657b1124cd3SDan Williams 	struct isci_host *ihost = ha->lldd_ha;
6586f231ddaSDan Williams 
65977950f51SEdmund Nadolski 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
6606f231ddaSDan Williams 		return 0;
6616f231ddaSDan Williams 
662b1124cd3SDan Williams 	sas_drain_work(ha);
6636f231ddaSDan Williams 
6646f231ddaSDan Williams 	return 1;
6656f231ddaSDan Williams }
6666f231ddaSDan Williams 
667cc9203bfSDan Williams /**
66889a7301fSDan Williams  * sci_controller_get_suggested_start_timeout() - This method returns the
66989a7301fSDan Williams  *    suggested sci_controller_start() timeout amount.  The user is free to
670cc9203bfSDan Williams  *    use any timeout value, but this method provides the suggested minimum
671cc9203bfSDan Williams  *    start timeout value.  The returned value is based upon empirical
672cc9203bfSDan Williams  *    information determined as a result of interoperability testing.
673cc9203bfSDan Williams  * @controller: the handle to the controller object for which to return the
674cc9203bfSDan Williams  *    suggested start timeout.
675cc9203bfSDan Williams  *
676cc9203bfSDan Williams  * This method returns the number of milliseconds for the suggested start
677cc9203bfSDan Williams  * operation timeout.
678cc9203bfSDan Williams  */
67989a7301fSDan Williams static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
680cc9203bfSDan Williams {
681cc9203bfSDan Williams 	/* Validate the user supplied parameters. */
682d9dcb4baSDan Williams 	if (!ihost)
683cc9203bfSDan Williams 		return 0;
684cc9203bfSDan Williams 
685cc9203bfSDan Williams 	/*
686cc9203bfSDan Williams 	 * The suggested minimum timeout value for a controller start operation:
687cc9203bfSDan Williams 	 *
688cc9203bfSDan Williams 	 *     Signature FIS Timeout
689cc9203bfSDan Williams 	 *   + Phy Start Timeout
690cc9203bfSDan Williams 	 *   + Number of Phy Spin Up Intervals
691cc9203bfSDan Williams 	 *   ---------------------------------
692cc9203bfSDan Williams 	 *   Number of milliseconds for the controller start operation.
693cc9203bfSDan Williams 	 *
694cc9203bfSDan Williams 	 * NOTE: The number of phy spin up intervals will be equivalent
695cc9203bfSDan Williams 	 *       to the number of phys divided by the number phys allowed
696cc9203bfSDan Williams 	 *       per interval - 1 (once OEM parameters are supported).
697cc9203bfSDan Williams 	 *       Currently we assume only 1 phy per interval. */
698cc9203bfSDan Williams 
699cc9203bfSDan Williams 	return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
700cc9203bfSDan Williams 		+ SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
701cc9203bfSDan Williams 		+ ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
702cc9203bfSDan Williams }
703cc9203bfSDan Williams 
70489a7301fSDan Williams static void sci_controller_enable_interrupts(struct isci_host *ihost)
705cc9203bfSDan Williams {
706*2396a265SDan Williams 	set_bit(IHOST_IRQ_ENABLED, &ihost->flags);
707d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
708cc9203bfSDan Williams }
709cc9203bfSDan Williams 
71089a7301fSDan Williams void sci_controller_disable_interrupts(struct isci_host *ihost)
711cc9203bfSDan Williams {
712*2396a265SDan Williams 	clear_bit(IHOST_IRQ_ENABLED, &ihost->flags);
713d9dcb4baSDan Williams 	writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
714*2396a265SDan Williams 	readl(&ihost->smu_registers->interrupt_mask); /* flush */
715cc9203bfSDan Williams }
716cc9203bfSDan Williams 
71789a7301fSDan Williams static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
718cc9203bfSDan Williams {
719cc9203bfSDan Williams 	u32 port_task_scheduler_value;
720cc9203bfSDan Williams 
721cc9203bfSDan Williams 	port_task_scheduler_value =
722d9dcb4baSDan Williams 		readl(&ihost->scu_registers->peg0.ptsg.control);
723cc9203bfSDan Williams 	port_task_scheduler_value |=
724cc9203bfSDan Williams 		(SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
725cc9203bfSDan Williams 		 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
726cc9203bfSDan Williams 	writel(port_task_scheduler_value,
727d9dcb4baSDan Williams 	       &ihost->scu_registers->peg0.ptsg.control);
728cc9203bfSDan Williams }
729cc9203bfSDan Williams 
73089a7301fSDan Williams static void sci_controller_assign_task_entries(struct isci_host *ihost)
731cc9203bfSDan Williams {
732cc9203bfSDan Williams 	u32 task_assignment;
733cc9203bfSDan Williams 
734cc9203bfSDan Williams 	/*
735cc9203bfSDan Williams 	 * Assign all the TCs to function 0
736cc9203bfSDan Williams 	 * TODO: Do we actually need to read this register to write it back?
737cc9203bfSDan Williams 	 */
738cc9203bfSDan Williams 
739cc9203bfSDan Williams 	task_assignment =
740d9dcb4baSDan Williams 		readl(&ihost->smu_registers->task_context_assignment[0]);
741cc9203bfSDan Williams 
742cc9203bfSDan Williams 	task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
743d9dcb4baSDan Williams 		(SMU_TCA_GEN_VAL(ENDING,  ihost->task_context_entries - 1)) |
744cc9203bfSDan Williams 		(SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
745cc9203bfSDan Williams 
746cc9203bfSDan Williams 	writel(task_assignment,
747d9dcb4baSDan Williams 		&ihost->smu_registers->task_context_assignment[0]);
748cc9203bfSDan Williams 
749cc9203bfSDan Williams }
750cc9203bfSDan Williams 
75189a7301fSDan Williams static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
752cc9203bfSDan Williams {
753cc9203bfSDan Williams 	u32 index;
754cc9203bfSDan Williams 	u32 completion_queue_control_value;
755cc9203bfSDan Williams 	u32 completion_queue_get_value;
756cc9203bfSDan Williams 	u32 completion_queue_put_value;
757cc9203bfSDan Williams 
758d9dcb4baSDan Williams 	ihost->completion_queue_get = 0;
759cc9203bfSDan Williams 
7607c78da31SDan Williams 	completion_queue_control_value =
7617c78da31SDan Williams 		(SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
7627c78da31SDan Williams 		 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
763cc9203bfSDan Williams 
764cc9203bfSDan Williams 	writel(completion_queue_control_value,
765d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_control);
766cc9203bfSDan Williams 
767cc9203bfSDan Williams 
768cc9203bfSDan Williams 	/* Set the completion queue get pointer and enable the queue */
769cc9203bfSDan Williams 	completion_queue_get_value = (
770cc9203bfSDan Williams 		(SMU_CQGR_GEN_VAL(POINTER, 0))
771cc9203bfSDan Williams 		| (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
772cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(ENABLE))
773cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
774cc9203bfSDan Williams 		);
775cc9203bfSDan Williams 
776cc9203bfSDan Williams 	writel(completion_queue_get_value,
777d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_get);
778cc9203bfSDan Williams 
779cc9203bfSDan Williams 	/* Set the completion queue put pointer */
780cc9203bfSDan Williams 	completion_queue_put_value = (
781cc9203bfSDan Williams 		(SMU_CQPR_GEN_VAL(POINTER, 0))
782cc9203bfSDan Williams 		| (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
783cc9203bfSDan Williams 		);
784cc9203bfSDan Williams 
785cc9203bfSDan Williams 	writel(completion_queue_put_value,
786d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_put);
787cc9203bfSDan Williams 
788cc9203bfSDan Williams 	/* Initialize the cycle bit of the completion queue entries */
7897c78da31SDan Williams 	for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
790cc9203bfSDan Williams 		/*
791cc9203bfSDan Williams 		 * If get.cycle_bit != completion_queue.cycle_bit
792cc9203bfSDan Williams 		 * its not a valid completion queue entry
793cc9203bfSDan Williams 		 * so at system start all entries are invalid */
794d9dcb4baSDan Williams 		ihost->completion_queue[index] = 0x80000000;
795cc9203bfSDan Williams 	}
796cc9203bfSDan Williams }
797cc9203bfSDan Williams 
79889a7301fSDan Williams static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
799cc9203bfSDan Williams {
800cc9203bfSDan Williams 	u32 frame_queue_control_value;
801cc9203bfSDan Williams 	u32 frame_queue_get_value;
802cc9203bfSDan Williams 	u32 frame_queue_put_value;
803cc9203bfSDan Williams 
804cc9203bfSDan Williams 	/* Write the queue size */
805cc9203bfSDan Williams 	frame_queue_control_value =
8067c78da31SDan Williams 		SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
807cc9203bfSDan Williams 
808cc9203bfSDan Williams 	writel(frame_queue_control_value,
809d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
810cc9203bfSDan Williams 
811cc9203bfSDan Williams 	/* Setup the get pointer for the unsolicited frame queue */
812cc9203bfSDan Williams 	frame_queue_get_value = (
813cc9203bfSDan Williams 		SCU_UFQGP_GEN_VAL(POINTER, 0)
814cc9203bfSDan Williams 		|  SCU_UFQGP_GEN_BIT(ENABLE_BIT)
815cc9203bfSDan Williams 		);
816cc9203bfSDan Williams 
817cc9203bfSDan Williams 	writel(frame_queue_get_value,
818d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
819cc9203bfSDan Williams 	/* Setup the put pointer for the unsolicited frame queue */
820cc9203bfSDan Williams 	frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
821cc9203bfSDan Williams 	writel(frame_queue_put_value,
822d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
823cc9203bfSDan Williams }
824cc9203bfSDan Williams 
82550a92d93SDan Williams void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
826cc9203bfSDan Williams {
827d9dcb4baSDan Williams 	if (ihost->sm.current_state_id == SCIC_STARTING) {
828cc9203bfSDan Williams 		/*
829cc9203bfSDan Williams 		 * We move into the ready state, because some of the phys/ports
830cc9203bfSDan Williams 		 * may be up and operational.
831cc9203bfSDan Williams 		 */
832d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_READY);
833cc9203bfSDan Williams 
834cc9203bfSDan Williams 		isci_host_start_complete(ihost, status);
835cc9203bfSDan Williams 	}
836cc9203bfSDan Williams }
837cc9203bfSDan Williams 
83885280955SDan Williams static bool is_phy_starting(struct isci_phy *iphy)
8394a33c525SAdam Gruchala {
84089a7301fSDan Williams 	enum sci_phy_states state;
8414a33c525SAdam Gruchala 
84285280955SDan Williams 	state = iphy->sm.current_state_id;
8434a33c525SAdam Gruchala 	switch (state) {
844e301370aSEdmund Nadolski 	case SCI_PHY_STARTING:
845e301370aSEdmund Nadolski 	case SCI_PHY_SUB_INITIAL:
846e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
847e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_IAF_UF:
848e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
849e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
850e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
851e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
85250a92d93SDan Williams 	case SCI_PHY_SUB_AWAIT_OSSP_EN:
853e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
854e301370aSEdmund Nadolski 	case SCI_PHY_SUB_FINAL:
8554a33c525SAdam Gruchala 		return true;
8564a33c525SAdam Gruchala 	default:
8574a33c525SAdam Gruchala 		return false;
8584a33c525SAdam Gruchala 	}
8594a33c525SAdam Gruchala }
8604a33c525SAdam Gruchala 
86150a92d93SDan Williams bool is_controller_start_complete(struct isci_host *ihost)
86250a92d93SDan Williams {
86350a92d93SDan Williams 	int i;
86450a92d93SDan Williams 
86550a92d93SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
86650a92d93SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
86750a92d93SDan Williams 		u32 state = iphy->sm.current_state_id;
86850a92d93SDan Williams 
86950a92d93SDan Williams 		/* in apc mode we need to check every phy, in
87050a92d93SDan Williams 		 * mpc mode we only need to check phys that have
87150a92d93SDan Williams 		 * been configured into a port
87250a92d93SDan Williams 		 */
87350a92d93SDan Williams 		if (is_port_config_apc(ihost))
87450a92d93SDan Williams 			/* pass */;
87550a92d93SDan Williams 		else if (!phy_get_non_dummy_port(iphy))
87650a92d93SDan Williams 			continue;
87750a92d93SDan Williams 
87850a92d93SDan Williams 		/* The controller start operation is complete iff:
87950a92d93SDan Williams 		 * - all links have been given an opportunity to start
88050a92d93SDan Williams 		 * - have no indication of a connected device
88150a92d93SDan Williams 		 * - have an indication of a connected device and it has
88250a92d93SDan Williams 		 *   finished the link training process.
88350a92d93SDan Williams 		 */
88450a92d93SDan Williams 		if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
88550a92d93SDan Williams 		    (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
88650a92d93SDan Williams 		    (iphy->is_in_link_training == true && is_phy_starting(iphy)) ||
88750a92d93SDan Williams 		    (ihost->port_agent.phy_ready_mask != ihost->port_agent.phy_configured_mask))
88850a92d93SDan Williams 			return false;
88950a92d93SDan Williams 	}
89050a92d93SDan Williams 
89150a92d93SDan Williams 	return true;
89250a92d93SDan Williams }
89350a92d93SDan Williams 
894cc9203bfSDan Williams /**
89589a7301fSDan Williams  * sci_controller_start_next_phy - start phy
896cc9203bfSDan Williams  * @scic: controller
897cc9203bfSDan Williams  *
898cc9203bfSDan Williams  * If all the phys have been started, then attempt to transition the
899cc9203bfSDan Williams  * controller to the READY state and inform the user
90089a7301fSDan Williams  * (sci_cb_controller_start_complete()).
901cc9203bfSDan Williams  */
90289a7301fSDan Williams static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
903cc9203bfSDan Williams {
90489a7301fSDan Williams 	struct sci_oem_params *oem = &ihost->oem_parameters;
90585280955SDan Williams 	struct isci_phy *iphy;
906cc9203bfSDan Williams 	enum sci_status status;
907cc9203bfSDan Williams 
908cc9203bfSDan Williams 	status = SCI_SUCCESS;
909cc9203bfSDan Williams 
910d9dcb4baSDan Williams 	if (ihost->phy_startup_timer_pending)
911cc9203bfSDan Williams 		return status;
912cc9203bfSDan Williams 
913d9dcb4baSDan Williams 	if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
91450a92d93SDan Williams 		if (is_controller_start_complete(ihost)) {
91589a7301fSDan Williams 			sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
916d9dcb4baSDan Williams 			sci_del_timer(&ihost->phy_timer);
917d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = false;
918cc9203bfSDan Williams 		}
919cc9203bfSDan Williams 	} else {
920d9dcb4baSDan Williams 		iphy = &ihost->phys[ihost->next_phy_to_start];
921cc9203bfSDan Williams 
922cc9203bfSDan Williams 		if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
92385280955SDan Williams 			if (phy_get_non_dummy_port(iphy) == NULL) {
924d9dcb4baSDan Williams 				ihost->next_phy_to_start++;
925cc9203bfSDan Williams 
926cc9203bfSDan Williams 				/* Caution recursion ahead be forwarned
927cc9203bfSDan Williams 				 *
928cc9203bfSDan Williams 				 * The PHY was never added to a PORT in MPC mode
929cc9203bfSDan Williams 				 * so start the next phy in sequence This phy
930cc9203bfSDan Williams 				 * will never go link up and will not draw power
931cc9203bfSDan Williams 				 * the OEM parameters either configured the phy
932cc9203bfSDan Williams 				 * incorrectly for the PORT or it was never
933cc9203bfSDan Williams 				 * assigned to a PORT
934cc9203bfSDan Williams 				 */
93589a7301fSDan Williams 				return sci_controller_start_next_phy(ihost);
936cc9203bfSDan Williams 			}
937cc9203bfSDan Williams 		}
938cc9203bfSDan Williams 
93989a7301fSDan Williams 		status = sci_phy_start(iphy);
940cc9203bfSDan Williams 
941cc9203bfSDan Williams 		if (status == SCI_SUCCESS) {
942d9dcb4baSDan Williams 			sci_mod_timer(&ihost->phy_timer,
943bb3dbdf6SEdmund Nadolski 				      SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
944d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = true;
945cc9203bfSDan Williams 		} else {
946d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
947cc9203bfSDan Williams 				 "%s: Controller stop operation failed "
948cc9203bfSDan Williams 				 "to stop phy %d because of status "
949cc9203bfSDan Williams 				 "%d.\n",
950cc9203bfSDan Williams 				 __func__,
951d9dcb4baSDan Williams 				 ihost->phys[ihost->next_phy_to_start].phy_index,
952cc9203bfSDan Williams 				 status);
953cc9203bfSDan Williams 		}
954cc9203bfSDan Williams 
955d9dcb4baSDan Williams 		ihost->next_phy_to_start++;
956cc9203bfSDan Williams 	}
957cc9203bfSDan Williams 
958cc9203bfSDan Williams 	return status;
959cc9203bfSDan Williams }
960cc9203bfSDan Williams 
961bb3dbdf6SEdmund Nadolski static void phy_startup_timeout(unsigned long data)
962cc9203bfSDan Williams {
963bb3dbdf6SEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
964d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
965bb3dbdf6SEdmund Nadolski 	unsigned long flags;
966cc9203bfSDan Williams 	enum sci_status status;
967cc9203bfSDan Williams 
968bb3dbdf6SEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
969bb3dbdf6SEdmund Nadolski 
970bb3dbdf6SEdmund Nadolski 	if (tmr->cancel)
971bb3dbdf6SEdmund Nadolski 		goto done;
972bb3dbdf6SEdmund Nadolski 
973d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
974bb3dbdf6SEdmund Nadolski 
975bb3dbdf6SEdmund Nadolski 	do {
97689a7301fSDan Williams 		status = sci_controller_start_next_phy(ihost);
977bb3dbdf6SEdmund Nadolski 	} while (status != SCI_SUCCESS);
978bb3dbdf6SEdmund Nadolski 
979bb3dbdf6SEdmund Nadolski done:
980bb3dbdf6SEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
981cc9203bfSDan Williams }
982cc9203bfSDan Williams 
983ac668c69SDan Williams static u16 isci_tci_active(struct isci_host *ihost)
984ac668c69SDan Williams {
985ac668c69SDan Williams 	return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
986ac668c69SDan Williams }
987ac668c69SDan Williams 
98889a7301fSDan Williams static enum sci_status sci_controller_start(struct isci_host *ihost,
989cc9203bfSDan Williams 					     u32 timeout)
990cc9203bfSDan Williams {
991cc9203bfSDan Williams 	enum sci_status result;
992cc9203bfSDan Williams 	u16 index;
993cc9203bfSDan Williams 
994d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
99514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
99614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
997cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
998cc9203bfSDan Williams 	}
999cc9203bfSDan Williams 
1000cc9203bfSDan Williams 	/* Build the TCi free pool */
1001ac668c69SDan Williams 	BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1002ac668c69SDan Williams 	ihost->tci_head = 0;
1003ac668c69SDan Williams 	ihost->tci_tail = 0;
1004d9dcb4baSDan Williams 	for (index = 0; index < ihost->task_context_entries; index++)
1005ac668c69SDan Williams 		isci_tci_free(ihost, index);
1006cc9203bfSDan Williams 
1007cc9203bfSDan Williams 	/* Build the RNi free pool */
100889a7301fSDan Williams 	sci_remote_node_table_initialize(&ihost->available_remote_nodes,
1009d9dcb4baSDan Williams 					 ihost->remote_node_entries);
1010cc9203bfSDan Williams 
1011cc9203bfSDan Williams 	/*
1012cc9203bfSDan Williams 	 * Before anything else lets make sure we will not be
1013cc9203bfSDan Williams 	 * interrupted by the hardware.
1014cc9203bfSDan Williams 	 */
101589a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1016cc9203bfSDan Williams 
1017cc9203bfSDan Williams 	/* Enable the port task scheduler */
101889a7301fSDan Williams 	sci_controller_enable_port_task_scheduler(ihost);
1019cc9203bfSDan Williams 
1020d9dcb4baSDan Williams 	/* Assign all the task entries to ihost physical function */
102189a7301fSDan Williams 	sci_controller_assign_task_entries(ihost);
1022cc9203bfSDan Williams 
1023cc9203bfSDan Williams 	/* Now initialize the completion queue */
102489a7301fSDan Williams 	sci_controller_initialize_completion_queue(ihost);
1025cc9203bfSDan Williams 
1026cc9203bfSDan Williams 	/* Initialize the unsolicited frame queue for use */
102789a7301fSDan Williams 	sci_controller_initialize_unsolicited_frame_queue(ihost);
1028cc9203bfSDan Williams 
1029cc9203bfSDan Williams 	/* Start all of the ports on this controller */
1030d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1031ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1032cc9203bfSDan Williams 
103389a7301fSDan Williams 		result = sci_port_start(iport);
1034cc9203bfSDan Williams 		if (result)
1035cc9203bfSDan Williams 			return result;
1036cc9203bfSDan Williams 	}
1037cc9203bfSDan Williams 
103889a7301fSDan Williams 	sci_controller_start_next_phy(ihost);
1039cc9203bfSDan Williams 
1040d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1041cc9203bfSDan Williams 
1042d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STARTING);
1043cc9203bfSDan Williams 
1044cc9203bfSDan Williams 	return SCI_SUCCESS;
1045cc9203bfSDan Williams }
1046cc9203bfSDan Williams 
10476f231ddaSDan Williams void isci_host_scan_start(struct Scsi_Host *shost)
10486f231ddaSDan Williams {
10494393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
105089a7301fSDan Williams 	unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
10516f231ddaSDan Williams 
10520cf89d1dSDan Williams 	set_bit(IHOST_START_PENDING, &ihost->flags);
105377950f51SEdmund Nadolski 
105477950f51SEdmund Nadolski 	spin_lock_irq(&ihost->scic_lock);
105589a7301fSDan Williams 	sci_controller_start(ihost, tmo);
105689a7301fSDan Williams 	sci_controller_enable_interrupts(ihost);
105777950f51SEdmund Nadolski 	spin_unlock_irq(&ihost->scic_lock);
10586f231ddaSDan Williams }
10596f231ddaSDan Williams 
1060eb608c3cSDan Williams static void isci_host_stop_complete(struct isci_host *ihost)
10616f231ddaSDan Williams {
106289a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
10630cf89d1dSDan Williams 	clear_bit(IHOST_STOP_PENDING, &ihost->flags);
10640cf89d1dSDan Williams 	wake_up(&ihost->eventq);
10656f231ddaSDan Williams }
10666f231ddaSDan Williams 
106789a7301fSDan Williams static void sci_controller_completion_handler(struct isci_host *ihost)
1068cc9203bfSDan Williams {
1069cc9203bfSDan Williams 	/* Empty out the completion queue */
107089a7301fSDan Williams 	if (sci_controller_completion_queue_has_entries(ihost))
107189a7301fSDan Williams 		sci_controller_process_completions(ihost);
1072cc9203bfSDan Williams 
1073cc9203bfSDan Williams 	/* Clear the interrupt and enable all interrupts again */
1074d9dcb4baSDan Williams 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
1075cc9203bfSDan Williams 	/* Could we write the value of SMU_ISR_COMPLETION? */
1076d9dcb4baSDan Williams 	writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
1077d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
1078cc9203bfSDan Williams }
1079cc9203bfSDan Williams 
10806f231ddaSDan Williams /**
10816f231ddaSDan Williams  * isci_host_completion_routine() - This function is the delayed service
10826f231ddaSDan Williams  *    routine that calls the sci core library's completion handler. It's
10836f231ddaSDan Williams  *    scheduled as a tasklet from the interrupt service routine when interrupts
10846f231ddaSDan Williams  *    in use, or set as the timeout function in polled mode.
10856f231ddaSDan Williams  * @data: This parameter specifies the ISCI host object
10866f231ddaSDan Williams  *
10876f231ddaSDan Williams  */
1088abec912dSDan Williams void isci_host_completion_routine(unsigned long data)
10896f231ddaSDan Williams {
1090d9dcb4baSDan Williams 	struct isci_host *ihost = (struct isci_host *)data;
10916f231ddaSDan Williams 	struct list_head    completed_request_list;
109211b00c19SJeff Skirvin 	struct list_head    errored_request_list;
10936f231ddaSDan Williams 	struct list_head    *current_position;
10946f231ddaSDan Williams 	struct list_head    *next_position;
10956f231ddaSDan Williams 	struct isci_request *request;
10966f231ddaSDan Williams 	struct isci_request *next_request;
10976f231ddaSDan Williams 	struct sas_task     *task;
10989b4be528SDan Williams 	u16 active;
10996f231ddaSDan Williams 
11006f231ddaSDan Williams 	INIT_LIST_HEAD(&completed_request_list);
110111b00c19SJeff Skirvin 	INIT_LIST_HEAD(&errored_request_list);
11026f231ddaSDan Williams 
1103d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
11046f231ddaSDan Williams 
110589a7301fSDan Williams 	sci_controller_completion_handler(ihost);
1106c7ef4031SDan Williams 
11076f231ddaSDan Williams 	/* Take the lists of completed I/Os from the host. */
110811b00c19SJeff Skirvin 
1109d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_complete,
11106f231ddaSDan Williams 			 &completed_request_list);
11116f231ddaSDan Williams 
111211b00c19SJeff Skirvin 	/* Take the list of errored I/Os from the host. */
1113d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_errorback,
111411b00c19SJeff Skirvin 			 &errored_request_list);
11156f231ddaSDan Williams 
1116d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
11176f231ddaSDan Williams 
11186f231ddaSDan Williams 	/* Process any completions in the lists. */
11196f231ddaSDan Williams 	list_for_each_safe(current_position, next_position,
11206f231ddaSDan Williams 			   &completed_request_list) {
11216f231ddaSDan Williams 
11226f231ddaSDan Williams 		request = list_entry(current_position, struct isci_request,
11236f231ddaSDan Williams 				     completed_node);
11246f231ddaSDan Williams 		task = isci_request_access_task(request);
11256f231ddaSDan Williams 
11266f231ddaSDan Williams 		/* Normal notification (task_done) */
1127d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
11286f231ddaSDan Williams 			"%s: Normal - request/task = %p/%p\n",
11296f231ddaSDan Williams 			__func__,
11306f231ddaSDan Williams 			request,
11316f231ddaSDan Williams 			task);
11326f231ddaSDan Williams 
113311b00c19SJeff Skirvin 		/* Return the task to libsas */
113411b00c19SJeff Skirvin 		if (task != NULL) {
11356f231ddaSDan Williams 
113611b00c19SJeff Skirvin 			task->lldd_task = NULL;
113711b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
113811b00c19SJeff Skirvin 
113911b00c19SJeff Skirvin 				/* If the task is already in the abort path,
114011b00c19SJeff Skirvin 				* the task_done callback cannot be called.
114111b00c19SJeff Skirvin 				*/
114211b00c19SJeff Skirvin 				task->task_done(task);
114311b00c19SJeff Skirvin 			}
114411b00c19SJeff Skirvin 		}
1145312e0c24SDan Williams 
1146d9dcb4baSDan Williams 		spin_lock_irq(&ihost->scic_lock);
1147d9dcb4baSDan Williams 		isci_free_tag(ihost, request->io_tag);
1148d9dcb4baSDan Williams 		spin_unlock_irq(&ihost->scic_lock);
11496f231ddaSDan Williams 	}
115011b00c19SJeff Skirvin 	list_for_each_entry_safe(request, next_request, &errored_request_list,
11516f231ddaSDan Williams 				 completed_node) {
11526f231ddaSDan Williams 
11536f231ddaSDan Williams 		task = isci_request_access_task(request);
11546f231ddaSDan Williams 
11556f231ddaSDan Williams 		/* Use sas_task_abort */
1156d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
11576f231ddaSDan Williams 			 "%s: Error - request/task = %p/%p\n",
11586f231ddaSDan Williams 			 __func__,
11596f231ddaSDan Williams 			 request,
11606f231ddaSDan Williams 			 task);
11616f231ddaSDan Williams 
116211b00c19SJeff Skirvin 		if (task != NULL) {
116311b00c19SJeff Skirvin 
116411b00c19SJeff Skirvin 			/* Put the task into the abort path if it's not there
116511b00c19SJeff Skirvin 			 * already.
116611b00c19SJeff Skirvin 			 */
116711b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
11686f231ddaSDan Williams 				sas_task_abort(task);
116911b00c19SJeff Skirvin 
117011b00c19SJeff Skirvin 		} else {
117111b00c19SJeff Skirvin 			/* This is a case where the request has completed with a
117211b00c19SJeff Skirvin 			 * status such that it needed further target servicing,
117311b00c19SJeff Skirvin 			 * but the sas_task reference has already been removed
117411b00c19SJeff Skirvin 			 * from the request.  Since it was errored, it was not
117511b00c19SJeff Skirvin 			 * being aborted, so there is nothing to do except free
117611b00c19SJeff Skirvin 			 * it.
117711b00c19SJeff Skirvin 			 */
117811b00c19SJeff Skirvin 
1179d9dcb4baSDan Williams 			spin_lock_irq(&ihost->scic_lock);
118011b00c19SJeff Skirvin 			/* Remove the request from the remote device's list
118111b00c19SJeff Skirvin 			* of pending requests.
118211b00c19SJeff Skirvin 			*/
118311b00c19SJeff Skirvin 			list_del_init(&request->dev_node);
1184d9dcb4baSDan Williams 			isci_free_tag(ihost, request->io_tag);
1185d9dcb4baSDan Williams 			spin_unlock_irq(&ihost->scic_lock);
118611b00c19SJeff Skirvin 		}
11876f231ddaSDan Williams 	}
11886f231ddaSDan Williams 
11899b4be528SDan Williams 	/* the coalesence timeout doubles at each encoding step, so
11909b4be528SDan Williams 	 * update it based on the ilog2 value of the outstanding requests
11919b4be528SDan Williams 	 */
11929b4be528SDan Williams 	active = isci_tci_active(ihost);
11939b4be528SDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, active) |
11949b4be528SDan Williams 	       SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
11959b4be528SDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
11966f231ddaSDan Williams }
11976f231ddaSDan Williams 
1198cc9203bfSDan Williams /**
119989a7301fSDan Williams  * sci_controller_stop() - This method will stop an individual controller
1200cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1201cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1202cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1203cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1204cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1205cc9203bfSDan Williams  *    the hardware is halted.
1206cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1207cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1208cc9203bfSDan Williams  *    stop operation should complete.
1209cc9203bfSDan Williams  *
1210cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1211cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1212cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1213cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1214cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1215cc9203bfSDan Williams  */
121689a7301fSDan Williams static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
1217cc9203bfSDan Williams {
1218d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
121914e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
122014e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1221cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1222cc9203bfSDan Williams 	}
1223cc9203bfSDan Williams 
1224d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1225d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STOPPING);
1226cc9203bfSDan Williams 	return SCI_SUCCESS;
1227cc9203bfSDan Williams }
1228cc9203bfSDan Williams 
1229cc9203bfSDan Williams /**
123089a7301fSDan Williams  * sci_controller_reset() - This method will reset the supplied core
1231cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1232cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1233cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1234cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1235cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1236cc9203bfSDan Williams  *
1237cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1238cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1239cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1240cc9203bfSDan Williams  */
124189a7301fSDan Williams static enum sci_status sci_controller_reset(struct isci_host *ihost)
1242cc9203bfSDan Williams {
1243d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
1244e301370aSEdmund Nadolski 	case SCIC_RESET:
1245e301370aSEdmund Nadolski 	case SCIC_READY:
1246eb608c3cSDan Williams 	case SCIC_STOPPING:
1247e301370aSEdmund Nadolski 	case SCIC_FAILED:
1248cc9203bfSDan Williams 		/*
1249cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1250cc9203bfSDan Williams 		 * perform the state transition.
1251cc9203bfSDan Williams 		 */
1252d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_RESETTING);
1253cc9203bfSDan Williams 		return SCI_SUCCESS;
1254cc9203bfSDan Williams 	default:
125514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
125614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1257cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1258cc9203bfSDan Williams 	}
1259cc9203bfSDan Williams }
1260cc9203bfSDan Williams 
1261eb608c3cSDan Williams static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
1262eb608c3cSDan Williams {
1263eb608c3cSDan Williams 	u32 index;
1264eb608c3cSDan Williams 	enum sci_status status;
1265eb608c3cSDan Williams 	enum sci_status phy_status;
1266eb608c3cSDan Williams 
1267eb608c3cSDan Williams 	status = SCI_SUCCESS;
1268eb608c3cSDan Williams 
1269eb608c3cSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1270eb608c3cSDan Williams 		phy_status = sci_phy_stop(&ihost->phys[index]);
1271eb608c3cSDan Williams 
1272eb608c3cSDan Williams 		if (phy_status != SCI_SUCCESS &&
1273eb608c3cSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1274eb608c3cSDan Williams 			status = SCI_FAILURE;
1275eb608c3cSDan Williams 
1276eb608c3cSDan Williams 			dev_warn(&ihost->pdev->dev,
1277eb608c3cSDan Williams 				 "%s: Controller stop operation failed to stop "
1278eb608c3cSDan Williams 				 "phy %d because of status %d.\n",
1279eb608c3cSDan Williams 				 __func__,
1280eb608c3cSDan Williams 				 ihost->phys[index].phy_index, phy_status);
1281eb608c3cSDan Williams 		}
1282eb608c3cSDan Williams 	}
1283eb608c3cSDan Williams 
1284eb608c3cSDan Williams 	return status;
1285eb608c3cSDan Williams }
1286eb608c3cSDan Williams 
1287eb608c3cSDan Williams 
1288eb608c3cSDan Williams /**
1289eb608c3cSDan Williams  * isci_host_deinit - shutdown frame reception and dma
1290eb608c3cSDan Williams  * @ihost: host to take down
1291eb608c3cSDan Williams  *
1292eb608c3cSDan Williams  * This is called in either the driver shutdown or the suspend path.  In
1293eb608c3cSDan Williams  * the shutdown case libsas went through port teardown and normal device
1294eb608c3cSDan Williams  * removal (i.e. physical links stayed up to service scsi_device removal
1295eb608c3cSDan Williams  * commands).  In the suspend case we disable the hardware without
1296eb608c3cSDan Williams  * notifying libsas of the link down events since we want libsas to
1297eb608c3cSDan Williams  * remember the domain across the suspend/resume cycle
1298eb608c3cSDan Williams  */
12990cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
13006f231ddaSDan Williams {
13016f231ddaSDan Williams 	int i;
13026f231ddaSDan Williams 
1303ad4f4c1dSDan Williams 	/* disable output data selects */
1304ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
1305ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
1306ad4f4c1dSDan Williams 
13070cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
13087c40a803SDan Williams 
13097c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
131089a7301fSDan Williams 	sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
13117c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
13127c40a803SDan Williams 
13130cf89d1dSDan Williams 	wait_for_stop(ihost);
1314ad4f4c1dSDan Williams 
1315eb608c3cSDan Williams 	/* phy stop is after controller stop to allow port and device to
1316eb608c3cSDan Williams 	 * go idle before shutting down the phys, but the expectation is
1317eb608c3cSDan Williams 	 * that i/o has been shut off well before we reach this
1318eb608c3cSDan Williams 	 * function.
1319eb608c3cSDan Williams 	 */
1320eb608c3cSDan Williams 	sci_controller_stop_phys(ihost);
1321eb608c3cSDan Williams 
1322ad4f4c1dSDan Williams 	/* disable sgpio: where the above wait should give time for the
1323ad4f4c1dSDan Williams 	 * enclosure to sample the gpios going inactive
1324ad4f4c1dSDan Williams 	 */
1325ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
1326ad4f4c1dSDan Williams 
1327*2396a265SDan Williams 	spin_lock_irq(&ihost->scic_lock);
132889a7301fSDan Williams 	sci_controller_reset(ihost);
1329*2396a265SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
13305553ba2bSEdmund Nadolski 
13315553ba2bSEdmund Nadolski 	/* Cancel any/all outstanding port timers */
1332d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
1333ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[i];
1334ffe191c9SDan Williams 		del_timer_sync(&iport->timer.timer);
13355553ba2bSEdmund Nadolski 	}
13365553ba2bSEdmund Nadolski 
1337a628d478SEdmund Nadolski 	/* Cancel any/all outstanding phy timers */
1338a628d478SEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
133985280955SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
134085280955SDan Williams 		del_timer_sync(&iphy->sata_timer.timer);
1341a628d478SEdmund Nadolski 	}
1342a628d478SEdmund Nadolski 
1343d9dcb4baSDan Williams 	del_timer_sync(&ihost->port_agent.timer.timer);
1344ac0eeb4fSEdmund Nadolski 
1345d9dcb4baSDan Williams 	del_timer_sync(&ihost->power_control.timer.timer);
13460473661aSEdmund Nadolski 
1347d9dcb4baSDan Williams 	del_timer_sync(&ihost->timer.timer);
13486cb5853dSEdmund Nadolski 
1349d9dcb4baSDan Williams 	del_timer_sync(&ihost->phy_timer.timer);
13506f231ddaSDan Williams }
13516f231ddaSDan Williams 
13526f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
13536f231ddaSDan Williams {
13546f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13556f231ddaSDan Williams 	int id = isci_host->id;
13566f231ddaSDan Williams 
13576f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
13586f231ddaSDan Williams }
13596f231ddaSDan Williams 
13606f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
13616f231ddaSDan Williams {
13626f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13636f231ddaSDan Williams 	int id = isci_host->id;
13646f231ddaSDan Williams 
13656f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13666f231ddaSDan Williams }
13676f231ddaSDan Williams 
136889a7301fSDan Williams static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
1369cc9203bfSDan Williams {
1370d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1371cc9203bfSDan Williams 
1372d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1373cc9203bfSDan Williams }
1374cc9203bfSDan Williams 
137589a7301fSDan Williams static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
1376cc9203bfSDan Williams {
1377d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1378cc9203bfSDan Williams 
1379d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1380cc9203bfSDan Williams }
1381cc9203bfSDan Williams 
1382cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1383cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1384cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1385cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1386cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1387cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1388cc9203bfSDan Williams 
1389cc9203bfSDan Williams /**
139089a7301fSDan Williams  * sci_controller_set_interrupt_coalescence() - This method allows the user to
1391cc9203bfSDan Williams  *    configure the interrupt coalescence.
1392cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1393cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1394cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1395cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1396cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1397cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1398cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1399cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1400cc9203bfSDan Williams  *    interrupt coalescing timeout.
1401cc9203bfSDan Williams  *
1402cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1403cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1404cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1405cc9203bfSDan Williams  */
1406d9dcb4baSDan Williams static enum sci_status
140789a7301fSDan Williams sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1408cc9203bfSDan Williams 					 u32 coalesce_number,
1409cc9203bfSDan Williams 					 u32 coalesce_timeout)
1410cc9203bfSDan Williams {
1411cc9203bfSDan Williams 	u8 timeout_encode = 0;
1412cc9203bfSDan Williams 	u32 min = 0;
1413cc9203bfSDan Williams 	u32 max = 0;
1414cc9203bfSDan Williams 
1415cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1416cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1417cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1418cc9203bfSDan Williams 
1419cc9203bfSDan Williams 	/*
1420cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1421cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1422cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1423cc9203bfSDan Williams 	 *              0       -        -       Disabled
1424cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1425cc9203bfSDan Williams 	 *              2       26.7     40.0
1426cc9203bfSDan Williams 	 *              3       53.3     80.0
1427cc9203bfSDan Williams 	 *              4       106.7    160.0
1428cc9203bfSDan Williams 	 *              5       213.3    320.0
1429cc9203bfSDan Williams 	 *              6       426.7    640.0
1430cc9203bfSDan Williams 	 *              7       853.3    1280.0
1431cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1432cc9203bfSDan Williams 	 *              9       3.4      5.1
1433cc9203bfSDan Williams 	 *              10      6.8      10.2
1434cc9203bfSDan Williams 	 *              11      13.7     20.5
1435cc9203bfSDan Williams 	 *              12      27.3     41.0
1436cc9203bfSDan Williams 	 *              13      54.6     81.9
1437cc9203bfSDan Williams 	 *              14      109.2    163.8
1438cc9203bfSDan Williams 	 *              15      218.5    327.7
1439cc9203bfSDan Williams 	 *              16      436.9    655.4
1440cc9203bfSDan Williams 	 *              17      873.8    1310.7
1441cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1442cc9203bfSDan Williams 	 *              19      3.5      5.2
1443cc9203bfSDan Williams 	 *              20      7.0      10.5
1444cc9203bfSDan Williams 	 *              21      14.0     21.0
1445cc9203bfSDan Williams 	 *              22      28.0     41.9
1446cc9203bfSDan Williams 	 *              23      55.9     83.9
1447cc9203bfSDan Williams 	 *              24      111.8    167.8
1448cc9203bfSDan Williams 	 *              25      223.7    335.5
1449cc9203bfSDan Williams 	 *              26      447.4    671.1
1450cc9203bfSDan Williams 	 *              27      894.8    1342.2
1451cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1452cc9203bfSDan Williams 	 *              Others Undefined */
1453cc9203bfSDan Williams 
1454cc9203bfSDan Williams 	/*
1455cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1456cc9203bfSDan Williams 	 * value for register writing. */
1457cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1458cc9203bfSDan Williams 		timeout_encode = 0;
1459cc9203bfSDan Williams 	else{
1460cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1461cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1462cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1463cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1464cc9203bfSDan Williams 
1465cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1466cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1467cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1468cc9203bfSDan Williams 		      timeout_encode++) {
1469cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1470cc9203bfSDan Williams 				break;
1471cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1472cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1473cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1474cc9203bfSDan Williams 					break;
1475cc9203bfSDan Williams 				else{
1476cc9203bfSDan Williams 					timeout_encode++;
1477cc9203bfSDan Williams 					break;
1478cc9203bfSDan Williams 				}
1479cc9203bfSDan Williams 			} else {
1480cc9203bfSDan Williams 				max = max * 2;
1481cc9203bfSDan Williams 				min = min * 2;
1482cc9203bfSDan Williams 			}
1483cc9203bfSDan Williams 		}
1484cc9203bfSDan Williams 
1485cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1486cc9203bfSDan Williams 			/* the value is out of range. */
1487cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1488cc9203bfSDan Williams 	}
1489cc9203bfSDan Williams 
1490cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1491cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1492d9dcb4baSDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
1493cc9203bfSDan Williams 
1494cc9203bfSDan Williams 
1495d9dcb4baSDan Williams 	ihost->interrupt_coalesce_number = (u16)coalesce_number;
1496d9dcb4baSDan Williams 	ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
1497cc9203bfSDan Williams 
1498cc9203bfSDan Williams 	return SCI_SUCCESS;
1499cc9203bfSDan Williams }
1500cc9203bfSDan Williams 
1501cc9203bfSDan Williams 
150289a7301fSDan Williams static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
1503cc9203bfSDan Williams {
1504d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1505e5cc6aa4SMarcin Tomczak 	u32 val;
1506e5cc6aa4SMarcin Tomczak 
1507e5cc6aa4SMarcin Tomczak 	/* enable clock gating for power control of the scu unit */
1508e5cc6aa4SMarcin Tomczak 	val = readl(&ihost->smu_registers->clock_gating_control);
1509e5cc6aa4SMarcin Tomczak 	val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
1510e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
1511e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
1512e5cc6aa4SMarcin Tomczak 	val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
1513e5cc6aa4SMarcin Tomczak 	writel(val, &ihost->smu_registers->clock_gating_control);
1514cc9203bfSDan Williams 
1515cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
15169b4be528SDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1517cc9203bfSDan Williams }
1518cc9203bfSDan Williams 
151989a7301fSDan Williams static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
1520cc9203bfSDan Williams {
1521d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1522cc9203bfSDan Williams 
1523cc9203bfSDan Williams 	/* disable interrupt coalescence. */
152489a7301fSDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1525cc9203bfSDan Williams }
1526cc9203bfSDan Williams 
152789a7301fSDan Williams static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
1528cc9203bfSDan Williams {
1529cc9203bfSDan Williams 	u32 index;
1530cc9203bfSDan Williams 	enum sci_status port_status;
1531cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1532cc9203bfSDan Williams 
1533d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1534ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1535cc9203bfSDan Williams 
153689a7301fSDan Williams 		port_status = sci_port_stop(iport);
1537cc9203bfSDan Williams 
1538cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1539cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1540cc9203bfSDan Williams 			status = SCI_FAILURE;
1541cc9203bfSDan Williams 
1542d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1543cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1544cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1545cc9203bfSDan Williams 				 __func__,
1546ffe191c9SDan Williams 				 iport->logical_port_index,
1547cc9203bfSDan Williams 				 port_status);
1548cc9203bfSDan Williams 		}
1549cc9203bfSDan Williams 	}
1550cc9203bfSDan Williams 
1551cc9203bfSDan Williams 	return status;
1552cc9203bfSDan Williams }
1553cc9203bfSDan Williams 
155489a7301fSDan Williams static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
1555cc9203bfSDan Williams {
1556cc9203bfSDan Williams 	u32 index;
1557cc9203bfSDan Williams 	enum sci_status status;
1558cc9203bfSDan Williams 	enum sci_status device_status;
1559cc9203bfSDan Williams 
1560cc9203bfSDan Williams 	status = SCI_SUCCESS;
1561cc9203bfSDan Williams 
1562d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
1563d9dcb4baSDan Williams 		if (ihost->device_table[index] != NULL) {
1564cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
156589a7301fSDan Williams 			device_status = sci_remote_device_stop(ihost->device_table[index], 0);
1566cc9203bfSDan Williams 
1567cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1568cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1569d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
1570cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1571cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1572cc9203bfSDan Williams 					 "status %d.\n",
1573cc9203bfSDan Williams 					 __func__,
1574d9dcb4baSDan Williams 					 ihost->device_table[index], device_status);
1575cc9203bfSDan Williams 			}
1576cc9203bfSDan Williams 		}
1577cc9203bfSDan Williams 	}
1578cc9203bfSDan Williams 
1579cc9203bfSDan Williams 	return status;
1580cc9203bfSDan Williams }
1581cc9203bfSDan Williams 
158289a7301fSDan Williams static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1583cc9203bfSDan Williams {
1584d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1585cc9203bfSDan Williams 
158689a7301fSDan Williams 	sci_controller_stop_devices(ihost);
1587eb608c3cSDan Williams 	sci_controller_stop_ports(ihost);
1588eb608c3cSDan Williams 
1589eb608c3cSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
1590eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
1591cc9203bfSDan Williams }
1592cc9203bfSDan Williams 
159389a7301fSDan Williams static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1594cc9203bfSDan Williams {
1595d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1596cc9203bfSDan Williams 
1597d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1598cc9203bfSDan Williams }
1599cc9203bfSDan Williams 
160089a7301fSDan Williams static void sci_controller_reset_hardware(struct isci_host *ihost)
1601cc9203bfSDan Williams {
1602cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
160389a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1604cc9203bfSDan Williams 
1605cc9203bfSDan Williams 	/* Reset the SCU */
1606d9dcb4baSDan Williams 	writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
1607cc9203bfSDan Williams 
1608cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1609cc9203bfSDan Williams 	udelay(1000);
1610cc9203bfSDan Williams 
1611cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1612d9dcb4baSDan Williams 	writel(0x00000000, &ihost->smu_registers->completion_queue_get);
1613cc9203bfSDan Williams 
1614cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1615d9dcb4baSDan Williams 	writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
1616*2396a265SDan Williams 
1617*2396a265SDan Williams 	/* clear all interrupts */
1618*2396a265SDan Williams 	writel(~SMU_INTERRUPT_STATUS_RESERVED_MASK, &ihost->smu_registers->interrupt_status);
1619cc9203bfSDan Williams }
1620cc9203bfSDan Williams 
162189a7301fSDan Williams static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1622cc9203bfSDan Williams {
1623d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1624cc9203bfSDan Williams 
162589a7301fSDan Williams 	sci_controller_reset_hardware(ihost);
1626d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1627cc9203bfSDan Williams }
1628cc9203bfSDan Williams 
162989a7301fSDan Williams static const struct sci_base_state sci_controller_state_table[] = {
1630e301370aSEdmund Nadolski 	[SCIC_INITIAL] = {
163189a7301fSDan Williams 		.enter_state = sci_controller_initial_state_enter,
1632cc9203bfSDan Williams 	},
1633e301370aSEdmund Nadolski 	[SCIC_RESET] = {},
1634e301370aSEdmund Nadolski 	[SCIC_INITIALIZING] = {},
1635e301370aSEdmund Nadolski 	[SCIC_INITIALIZED] = {},
1636e301370aSEdmund Nadolski 	[SCIC_STARTING] = {
163789a7301fSDan Williams 		.exit_state  = sci_controller_starting_state_exit,
1638cc9203bfSDan Williams 	},
1639e301370aSEdmund Nadolski 	[SCIC_READY] = {
164089a7301fSDan Williams 		.enter_state = sci_controller_ready_state_enter,
164189a7301fSDan Williams 		.exit_state  = sci_controller_ready_state_exit,
1642cc9203bfSDan Williams 	},
1643e301370aSEdmund Nadolski 	[SCIC_RESETTING] = {
164489a7301fSDan Williams 		.enter_state = sci_controller_resetting_state_enter,
1645cc9203bfSDan Williams 	},
1646e301370aSEdmund Nadolski 	[SCIC_STOPPING] = {
164789a7301fSDan Williams 		.enter_state = sci_controller_stopping_state_enter,
164889a7301fSDan Williams 		.exit_state = sci_controller_stopping_state_exit,
1649cc9203bfSDan Williams 	},
1650e301370aSEdmund Nadolski 	[SCIC_FAILED] = {}
1651cc9203bfSDan Williams };
1652cc9203bfSDan Williams 
16536cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data)
16546cb5853dSEdmund Nadolski {
16556cb5853dSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1656d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1657d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
16586cb5853dSEdmund Nadolski 	unsigned long flags;
1659cc9203bfSDan Williams 
16606cb5853dSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
16616cb5853dSEdmund Nadolski 
16626cb5853dSEdmund Nadolski 	if (tmr->cancel)
16636cb5853dSEdmund Nadolski 		goto done;
16646cb5853dSEdmund Nadolski 
1665e301370aSEdmund Nadolski 	if (sm->current_state_id == SCIC_STARTING)
166689a7301fSDan Williams 		sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
1667e301370aSEdmund Nadolski 	else if (sm->current_state_id == SCIC_STOPPING) {
1668e301370aSEdmund Nadolski 		sci_change_state(sm, SCIC_FAILED);
1669eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
16706cb5853dSEdmund Nadolski 	} else	/* / @todo Now what do we want to do in this case? */
1671d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
16726cb5853dSEdmund Nadolski 			"%s: Controller timer fired when controller was not "
16736cb5853dSEdmund Nadolski 			"in a state being timed.\n",
16746cb5853dSEdmund Nadolski 			__func__);
16756cb5853dSEdmund Nadolski 
16766cb5853dSEdmund Nadolski done:
16776cb5853dSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
16786cb5853dSEdmund Nadolski }
1679cc9203bfSDan Williams 
168089a7301fSDan Williams static enum sci_status sci_controller_construct(struct isci_host *ihost,
1681cc9203bfSDan Williams 						void __iomem *scu_base,
1682cc9203bfSDan Williams 						void __iomem *smu_base)
1683cc9203bfSDan Williams {
1684cc9203bfSDan Williams 	u8 i;
1685cc9203bfSDan Williams 
168689a7301fSDan Williams 	sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
1687cc9203bfSDan Williams 
1688d9dcb4baSDan Williams 	ihost->scu_registers = scu_base;
1689d9dcb4baSDan Williams 	ihost->smu_registers = smu_base;
1690cc9203bfSDan Williams 
169189a7301fSDan Williams 	sci_port_configuration_agent_construct(&ihost->port_agent);
1692cc9203bfSDan Williams 
1693cc9203bfSDan Williams 	/* Construct the ports for this controller */
1694cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
169589a7301fSDan Williams 		sci_port_construct(&ihost->ports[i], i, ihost);
169689a7301fSDan Williams 	sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
1697cc9203bfSDan Williams 
1698cc9203bfSDan Williams 	/* Construct the phys for this controller */
1699cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1700cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
170189a7301fSDan Williams 		sci_phy_construct(&ihost->phys[i],
1702ffe191c9SDan Williams 				  &ihost->ports[SCI_MAX_PORTS], i);
1703cc9203bfSDan Williams 	}
1704cc9203bfSDan Williams 
1705d9dcb4baSDan Williams 	ihost->invalid_phy_mask = 0;
1706cc9203bfSDan Williams 
1707d9dcb4baSDan Williams 	sci_init_timer(&ihost->timer, controller_timeout);
17086cb5853dSEdmund Nadolski 
170989a7301fSDan Williams 	return sci_controller_reset(ihost);
1710cc9203bfSDan Williams }
1711cc9203bfSDan Williams 
1712594e566aSDave Jiang int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
1713cc9203bfSDan Williams {
1714cc9203bfSDan Williams 	int i;
1715cc9203bfSDan Williams 
1716cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1717cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1718cc9203bfSDan Williams 			return -EINVAL;
1719cc9203bfSDan Williams 
1720cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1721cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1722cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1723cc9203bfSDan Williams 			return -EINVAL;
1724cc9203bfSDan Williams 
1725cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1726cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1727cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1728cc9203bfSDan Williams 				return -EINVAL;
1729cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1730cc9203bfSDan Williams 		u8 phy_mask = 0;
1731cc9203bfSDan Williams 
1732cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1733cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1734cc9203bfSDan Williams 
1735cc9203bfSDan Williams 		if (phy_mask == 0)
1736cc9203bfSDan Williams 			return -EINVAL;
1737cc9203bfSDan Williams 	} else
1738cc9203bfSDan Williams 		return -EINVAL;
1739cc9203bfSDan Williams 
17407000f7c7SAndrzej Jakowski 	if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
17417000f7c7SAndrzej Jakowski 	    oem->controller.max_concurr_spin_up < 1)
1742cc9203bfSDan Williams 		return -EINVAL;
1743cc9203bfSDan Williams 
1744594e566aSDave Jiang 	if (oem->controller.do_enable_ssc) {
1745594e566aSDave Jiang 		if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
1746594e566aSDave Jiang 			return -EINVAL;
1747594e566aSDave Jiang 
1748594e566aSDave Jiang 		if (version >= ISCI_ROM_VER_1_1) {
1749594e566aSDave Jiang 			u8 test = oem->controller.ssc_sata_tx_spread_level;
1750594e566aSDave Jiang 
1751594e566aSDave Jiang 			switch (test) {
1752594e566aSDave Jiang 			case 0:
1753594e566aSDave Jiang 			case 2:
1754594e566aSDave Jiang 			case 3:
1755594e566aSDave Jiang 			case 6:
1756594e566aSDave Jiang 			case 7:
1757594e566aSDave Jiang 				break;
1758594e566aSDave Jiang 			default:
1759594e566aSDave Jiang 				return -EINVAL;
1760594e566aSDave Jiang 			}
1761594e566aSDave Jiang 
1762594e566aSDave Jiang 			test = oem->controller.ssc_sas_tx_spread_level;
1763594e566aSDave Jiang 			if (oem->controller.ssc_sas_tx_type == 0) {
1764594e566aSDave Jiang 				switch (test) {
1765594e566aSDave Jiang 				case 0:
1766594e566aSDave Jiang 				case 2:
1767594e566aSDave Jiang 				case 3:
1768594e566aSDave Jiang 					break;
1769594e566aSDave Jiang 				default:
1770594e566aSDave Jiang 					return -EINVAL;
1771594e566aSDave Jiang 				}
1772594e566aSDave Jiang 			} else if (oem->controller.ssc_sas_tx_type == 1) {
1773594e566aSDave Jiang 				switch (test) {
1774594e566aSDave Jiang 				case 0:
1775594e566aSDave Jiang 				case 3:
1776594e566aSDave Jiang 				case 6:
1777594e566aSDave Jiang 					break;
1778594e566aSDave Jiang 				default:
1779594e566aSDave Jiang 					return -EINVAL;
1780594e566aSDave Jiang 				}
1781594e566aSDave Jiang 			}
1782594e566aSDave Jiang 		}
1783594e566aSDave Jiang 	}
1784594e566aSDave Jiang 
1785cc9203bfSDan Williams 	return 0;
1786cc9203bfSDan Williams }
1787cc9203bfSDan Williams 
17887000f7c7SAndrzej Jakowski static u8 max_spin_up(struct isci_host *ihost)
17897000f7c7SAndrzej Jakowski {
17907000f7c7SAndrzej Jakowski 	if (ihost->user_parameters.max_concurr_spinup)
17917000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->user_parameters.max_concurr_spinup,
17927000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
17937000f7c7SAndrzej Jakowski 	else
17947000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
17957000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
17967000f7c7SAndrzej Jakowski }
17977000f7c7SAndrzej Jakowski 
17980473661aSEdmund Nadolski static void power_control_timeout(unsigned long data)
1799cc9203bfSDan Williams {
18000473661aSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1801d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
180285280955SDan Williams 	struct isci_phy *iphy;
18030473661aSEdmund Nadolski 	unsigned long flags;
18040473661aSEdmund Nadolski 	u8 i;
1805cc9203bfSDan Williams 
18060473661aSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
1807cc9203bfSDan Williams 
18080473661aSEdmund Nadolski 	if (tmr->cancel)
18090473661aSEdmund Nadolski 		goto done;
1810cc9203bfSDan Williams 
1811d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
1812cc9203bfSDan Williams 
1813d9dcb4baSDan Williams 	if (ihost->power_control.phys_waiting == 0) {
1814d9dcb4baSDan Williams 		ihost->power_control.timer_started = false;
18150473661aSEdmund Nadolski 		goto done;
18160473661aSEdmund Nadolski 	}
1817cc9203bfSDan Williams 
18180473661aSEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
18190473661aSEdmund Nadolski 
1820d9dcb4baSDan Williams 		if (ihost->power_control.phys_waiting == 0)
18210473661aSEdmund Nadolski 			break;
18220473661aSEdmund Nadolski 
1823d9dcb4baSDan Williams 		iphy = ihost->power_control.requesters[i];
182485280955SDan Williams 		if (iphy == NULL)
18250473661aSEdmund Nadolski 			continue;
18260473661aSEdmund Nadolski 
18277000f7c7SAndrzej Jakowski 		if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
18280473661aSEdmund Nadolski 			break;
18290473661aSEdmund Nadolski 
1830d9dcb4baSDan Williams 		ihost->power_control.requesters[i] = NULL;
1831d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1832d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
183389a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1834be778341SMarcin Tomczak 
1835c79dd80dSDan Williams 		if (iphy->protocol == SAS_PROTOCOL_SSP) {
1836be778341SMarcin Tomczak 			u8 j;
1837be778341SMarcin Tomczak 
1838be778341SMarcin Tomczak 			for (j = 0; j < SCI_MAX_PHYS; j++) {
1839be778341SMarcin Tomczak 				struct isci_phy *requester = ihost->power_control.requesters[j];
1840be778341SMarcin Tomczak 
1841be778341SMarcin Tomczak 				/*
1842be778341SMarcin Tomczak 				 * Search the power_control queue to see if there are other phys
1843be778341SMarcin Tomczak 				 * attached to the same remote device. If found, take all of
1844be778341SMarcin Tomczak 				 * them out of await_sas_power state.
1845be778341SMarcin Tomczak 				 */
1846be778341SMarcin Tomczak 				if (requester != NULL && requester != iphy) {
1847be778341SMarcin Tomczak 					u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
1848be778341SMarcin Tomczak 							  iphy->frame_rcvd.iaf.sas_addr,
1849be778341SMarcin Tomczak 							  sizeof(requester->frame_rcvd.iaf.sas_addr));
1850be778341SMarcin Tomczak 
1851be778341SMarcin Tomczak 					if (other == 0) {
1852be778341SMarcin Tomczak 						ihost->power_control.requesters[j] = NULL;
1853be778341SMarcin Tomczak 						ihost->power_control.phys_waiting--;
1854be778341SMarcin Tomczak 						sci_phy_consume_power_handler(requester);
1855be778341SMarcin Tomczak 					}
1856be778341SMarcin Tomczak 				}
1857be778341SMarcin Tomczak 			}
1858be778341SMarcin Tomczak 		}
1859cc9203bfSDan Williams 	}
1860cc9203bfSDan Williams 
1861cc9203bfSDan Williams 	/*
1862cc9203bfSDan Williams 	 * It doesn't matter if the power list is empty, we need to start the
1863cc9203bfSDan Williams 	 * timer in case another phy becomes ready.
1864cc9203bfSDan Williams 	 */
18650473661aSEdmund Nadolski 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1866d9dcb4baSDan Williams 	ihost->power_control.timer_started = true;
18670473661aSEdmund Nadolski 
18680473661aSEdmund Nadolski done:
18690473661aSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1870cc9203bfSDan Williams }
1871cc9203bfSDan Williams 
187289a7301fSDan Williams void sci_controller_power_control_queue_insert(struct isci_host *ihost,
187385280955SDan Williams 					       struct isci_phy *iphy)
1874cc9203bfSDan Williams {
187585280955SDan Williams 	BUG_ON(iphy == NULL);
1876cc9203bfSDan Williams 
18777000f7c7SAndrzej Jakowski 	if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
1878d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
187989a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1880cc9203bfSDan Williams 
1881cc9203bfSDan Williams 		/*
1882cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1883cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1884cc9203bfSDan Williams 		 */
1885d9dcb4baSDan Williams 		if (ihost->power_control.timer_started)
1886d9dcb4baSDan Williams 			sci_del_timer(&ihost->power_control.timer);
18870473661aSEdmund Nadolski 
1888d9dcb4baSDan Williams 		sci_mod_timer(&ihost->power_control.timer,
18890473661aSEdmund Nadolski 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1890d9dcb4baSDan Williams 		ihost->power_control.timer_started = true;
18910473661aSEdmund Nadolski 
1892cc9203bfSDan Williams 	} else {
1893be778341SMarcin Tomczak 		/*
1894be778341SMarcin Tomczak 		 * There are phys, attached to the same sas address as this phy, are
1895be778341SMarcin Tomczak 		 * already in READY state, this phy don't need wait.
1896be778341SMarcin Tomczak 		 */
1897be778341SMarcin Tomczak 		u8 i;
1898be778341SMarcin Tomczak 		struct isci_phy *current_phy;
1899be778341SMarcin Tomczak 
1900be778341SMarcin Tomczak 		for (i = 0; i < SCI_MAX_PHYS; i++) {
1901be778341SMarcin Tomczak 			u8 other;
1902be778341SMarcin Tomczak 			current_phy = &ihost->phys[i];
1903be778341SMarcin Tomczak 
1904be778341SMarcin Tomczak 			other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
1905be778341SMarcin Tomczak 				       iphy->frame_rcvd.iaf.sas_addr,
1906be778341SMarcin Tomczak 				       sizeof(current_phy->frame_rcvd.iaf.sas_addr));
1907be778341SMarcin Tomczak 
1908be778341SMarcin Tomczak 			if (current_phy->sm.current_state_id == SCI_PHY_READY &&
1909c79dd80dSDan Williams 			    current_phy->protocol == SAS_PROTOCOL_SSP &&
1910be778341SMarcin Tomczak 			    other == 0) {
1911be778341SMarcin Tomczak 				sci_phy_consume_power_handler(iphy);
1912be778341SMarcin Tomczak 				break;
1913be778341SMarcin Tomczak 			}
1914be778341SMarcin Tomczak 		}
1915be778341SMarcin Tomczak 
1916be778341SMarcin Tomczak 		if (i == SCI_MAX_PHYS) {
1917cc9203bfSDan Williams 			/* Add the phy in the waiting list */
1918d9dcb4baSDan Williams 			ihost->power_control.requesters[iphy->phy_index] = iphy;
1919d9dcb4baSDan Williams 			ihost->power_control.phys_waiting++;
1920cc9203bfSDan Williams 		}
1921cc9203bfSDan Williams 	}
1922be778341SMarcin Tomczak }
1923cc9203bfSDan Williams 
192489a7301fSDan Williams void sci_controller_power_control_queue_remove(struct isci_host *ihost,
192585280955SDan Williams 					       struct isci_phy *iphy)
1926cc9203bfSDan Williams {
192785280955SDan Williams 	BUG_ON(iphy == NULL);
1928cc9203bfSDan Williams 
192989a7301fSDan Williams 	if (ihost->power_control.requesters[iphy->phy_index])
1930d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1931cc9203bfSDan Williams 
1932d9dcb4baSDan Williams 	ihost->power_control.requesters[iphy->phy_index] = NULL;
1933cc9203bfSDan Williams }
1934cc9203bfSDan Williams 
1935afd13a1fSJeff Skirvin static int is_long_cable(int phy, unsigned char selection_byte)
1936afd13a1fSJeff Skirvin {
19379fee607fSJeff Skirvin 	return !!(selection_byte & (1 << phy));
1938afd13a1fSJeff Skirvin }
1939afd13a1fSJeff Skirvin 
1940afd13a1fSJeff Skirvin static int is_medium_cable(int phy, unsigned char selection_byte)
1941afd13a1fSJeff Skirvin {
19429fee607fSJeff Skirvin 	return !!(selection_byte & (1 << (phy + 4)));
19439fee607fSJeff Skirvin }
19449fee607fSJeff Skirvin 
19459fee607fSJeff Skirvin static enum cable_selections decode_selection_byte(
19469fee607fSJeff Skirvin 	int phy,
19479fee607fSJeff Skirvin 	unsigned char selection_byte)
19489fee607fSJeff Skirvin {
19499fee607fSJeff Skirvin 	return ((selection_byte & (1 << phy)) ? 1 : 0)
19509fee607fSJeff Skirvin 		+ (selection_byte & (1 << (phy + 4)) ? 2 : 0);
19519fee607fSJeff Skirvin }
19529fee607fSJeff Skirvin 
19539fee607fSJeff Skirvin static unsigned char *to_cable_select(struct isci_host *ihost)
19549fee607fSJeff Skirvin {
19559fee607fSJeff Skirvin 	if (is_cable_select_overridden())
19569fee607fSJeff Skirvin 		return ((unsigned char *)&cable_selection_override)
19579fee607fSJeff Skirvin 			+ ihost->id;
19589fee607fSJeff Skirvin 	else
19599fee607fSJeff Skirvin 		return &ihost->oem_parameters.controller.cable_selection_mask;
19609fee607fSJeff Skirvin }
19619fee607fSJeff Skirvin 
19629fee607fSJeff Skirvin enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
19639fee607fSJeff Skirvin {
19649fee607fSJeff Skirvin 	return decode_selection_byte(phy, *to_cable_select(ihost));
19659fee607fSJeff Skirvin }
19669fee607fSJeff Skirvin 
19679fee607fSJeff Skirvin char *lookup_cable_names(enum cable_selections selection)
19689fee607fSJeff Skirvin {
19699fee607fSJeff Skirvin 	static char *cable_names[] = {
19709fee607fSJeff Skirvin 		[short_cable]     = "short",
19719fee607fSJeff Skirvin 		[long_cable]      = "long",
19729fee607fSJeff Skirvin 		[medium_cable]    = "medium",
19739fee607fSJeff Skirvin 		[undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
19749fee607fSJeff Skirvin 	};
19759fee607fSJeff Skirvin 	return (selection <= undefined_cable) ? cable_names[selection]
19769fee607fSJeff Skirvin 					      : cable_names[undefined_cable];
1977afd13a1fSJeff Skirvin }
1978afd13a1fSJeff Skirvin 
1979cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
1980cc9203bfSDan Williams 
198189a7301fSDan Williams static void sci_controller_afe_initialization(struct isci_host *ihost)
1982cc9203bfSDan Williams {
19832e5da889SDan Williams 	struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
198489a7301fSDan Williams 	const struct sci_oem_params *oem = &ihost->oem_parameters;
1985dc00c8b6SDan Williams 	struct pci_dev *pdev = ihost->pdev;
1986cc9203bfSDan Williams 	u32 afe_status;
1987cc9203bfSDan Williams 	u32 phy_id;
19889fee607fSJeff Skirvin 	unsigned char cable_selection_mask = *to_cable_select(ihost);
1989cc9203bfSDan Williams 
1990cc9203bfSDan Williams 	/* Clear DFX Status registers */
19912e5da889SDan Williams 	writel(0x0081000f, &afe->afe_dfx_master_control0);
1992cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1993cc9203bfSDan Williams 
1994afd13a1fSJeff Skirvin 	if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
1995cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
19962e5da889SDan Williams 		 * Timer, PM Stagger Timer
19972e5da889SDan Williams 		 */
1998afd13a1fSJeff Skirvin 		writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
1999cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2000cc9203bfSDan Williams 	}
2001cc9203bfSDan Williams 
2002cc9203bfSDan Williams 	/* Configure bias currents to normal */
2003dc00c8b6SDan Williams 	if (is_a2(pdev))
20042e5da889SDan Williams 		writel(0x00005A00, &afe->afe_bias_control);
2005dc00c8b6SDan Williams 	else if (is_b0(pdev) || is_c0(pdev))
20062e5da889SDan Williams 		writel(0x00005F00, &afe->afe_bias_control);
2007afd13a1fSJeff Skirvin 	else if (is_c1(pdev))
2008afd13a1fSJeff Skirvin 		writel(0x00005500, &afe->afe_bias_control);
2009cc9203bfSDan Williams 
2010cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2011cc9203bfSDan Williams 
2012cc9203bfSDan Williams 	/* Enable PLL */
2013afd13a1fSJeff Skirvin 	if (is_a2(pdev))
20142e5da889SDan Williams 		writel(0x80040908, &afe->afe_pll_control0);
2015afd13a1fSJeff Skirvin 	else if (is_b0(pdev) || is_c0(pdev))
2016afd13a1fSJeff Skirvin 		writel(0x80040A08, &afe->afe_pll_control0);
2017afd13a1fSJeff Skirvin 	else if (is_c1(pdev)) {
2018afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
2019afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
2020afd13a1fSJeff Skirvin 		writel(0x00000B08, &afe->afe_pll_control0);
2021afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
2022afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
2023afd13a1fSJeff Skirvin 	}
2024cc9203bfSDan Williams 
2025cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2026cc9203bfSDan Williams 
2027cc9203bfSDan Williams 	/* Wait for the PLL to lock */
2028cc9203bfSDan Williams 	do {
20292e5da889SDan Williams 		afe_status = readl(&afe->afe_common_block_status);
2030cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2031cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
2032cc9203bfSDan Williams 
2033dc00c8b6SDan Williams 	if (is_a2(pdev)) {
20342e5da889SDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76
20352e5da889SDan Williams 		 * us to 50 us)
20362e5da889SDan Williams 		 */
20372e5da889SDan Williams 		writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
2038cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2039cc9203bfSDan Williams 	}
2040cc9203bfSDan Williams 
2041cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
20422e5da889SDan Williams 		struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id];
2043cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2044afd13a1fSJeff Skirvin 		int cable_length_long =
2045afd13a1fSJeff Skirvin 			is_long_cable(phy_id, cable_selection_mask);
2046afd13a1fSJeff Skirvin 		int cable_length_medium =
2047afd13a1fSJeff Skirvin 			is_medium_cable(phy_id, cable_selection_mask);
2048cc9203bfSDan Williams 
2049afd13a1fSJeff Skirvin 		if (is_a2(pdev)) {
20502e5da889SDan Williams 			/* All defaults, except the Receive Word
20512e5da889SDan Williams 			 * Alignament/Comma Detect Enable....(0xe800)
20522e5da889SDan Williams 			 */
20532e5da889SDan Williams 			writel(0x00004512, &xcvr->afe_xcvr_control0);
2054cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2055cc9203bfSDan Williams 
20562e5da889SDan Williams 			writel(0x0050100F, &xcvr->afe_xcvr_control1);
2057cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2058afd13a1fSJeff Skirvin 		} else if (is_b0(pdev)) {
2059afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2060afd13a1fSJeff Skirvin 			writel(0x00030000, &xcvr->afe_tx_ssc_control);
2061afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2062afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2063afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2064afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2065afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2066afd13a1fSJeff Skirvin 
2067afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2068afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2069afd13a1fSJeff Skirvin 			 */
2070afd13a1fSJeff Skirvin 			writel(0x00014500, &xcvr->afe_xcvr_control0);
2071afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2072afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2073afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2074afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2075afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2076afd13a1fSJeff Skirvin 
2077afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2078afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2079afd13a1fSJeff Skirvin 			 */
2080afd13a1fSJeff Skirvin 			writel(0x0001C500, &xcvr->afe_xcvr_control0);
2081afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2082cc9203bfSDan Williams 		}
2083cc9203bfSDan Williams 
2084afd13a1fSJeff Skirvin 		/* Power up TX and RX out from power down (PWRDNTX and
2085afd13a1fSJeff Skirvin 		 * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
20862e5da889SDan Williams 		 */
2087dc00c8b6SDan Williams 		if (is_a2(pdev))
20882e5da889SDan Williams 			writel(0x000003F0, &xcvr->afe_channel_control);
2089dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
20902e5da889SDan Williams 			writel(0x000003D7, &xcvr->afe_channel_control);
2091cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2092afd13a1fSJeff Skirvin 
20932e5da889SDan Williams 			writel(0x000003D4, &xcvr->afe_channel_control);
2094afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
20952e5da889SDan Williams 			writel(0x000001E7, &xcvr->afe_channel_control);
2096dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2097afd13a1fSJeff Skirvin 
20982e5da889SDan Williams 			writel(0x000001E4, &xcvr->afe_channel_control);
2099afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2100afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F7 : 0x000001F7,
2101afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2102afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2103afd13a1fSJeff Skirvin 
2104afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F4 : 0x000001F4,
2105afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2106cc9203bfSDan Williams 		}
2107cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2108cc9203bfSDan Williams 
2109dc00c8b6SDan Williams 		if (is_a2(pdev)) {
2110cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
21112e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2112cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2113cc9203bfSDan Williams 		}
2114cc9203bfSDan Williams 
2115afd13a1fSJeff Skirvin 		if (is_a2(pdev) || is_b0(pdev))
2116afd13a1fSJeff Skirvin 			/* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
2117afd13a1fSJeff Skirvin 			 * TPD=0x0(TX Power On), RDD=0x0(RX Detect
2118afd13a1fSJeff Skirvin 			 * Enabled) ....(0xe800)
2119afd13a1fSJeff Skirvin 			 */
21202e5da889SDan Williams 			writel(0x00004100, &xcvr->afe_xcvr_control0);
2121afd13a1fSJeff Skirvin 		else if (is_c0(pdev))
2122afd13a1fSJeff Skirvin 			writel(0x00014100, &xcvr->afe_xcvr_control0);
2123afd13a1fSJeff Skirvin 		else if (is_c1(pdev))
2124afd13a1fSJeff Skirvin 			writel(0x0001C100, &xcvr->afe_xcvr_control0);
2125cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2126cc9203bfSDan Williams 
2127cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2128dc00c8b6SDan Williams 		if (is_a2(pdev))
21292e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2130dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
21312e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2132cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2133cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
21342e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2135afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2136afd13a1fSJeff Skirvin 			writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
2137dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2138dbb0743aSAdam Gruchala 
21392e5da889SDan Williams 			writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
2140dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2141dbb0743aSAdam Gruchala 
2142dbb0743aSAdam Gruchala 			/* Enable TX equalization (0xe824) */
21432e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2144afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2145afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x01500C0C :
2146afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x01400C0D : 0x02400C0D,
2147afd13a1fSJeff Skirvin 			       &xcvr->afe_xcvr_control1);
2148afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2149afd13a1fSJeff Skirvin 
2150afd13a1fSJeff Skirvin 			writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
2151afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2152afd13a1fSJeff Skirvin 
2153afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x33091C1F :
2154afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x3315181F : 0x2B17161F,
2155afd13a1fSJeff Skirvin 			       &xcvr->afe_rx_ssc_control0);
2156afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2157afd13a1fSJeff Skirvin 
2158afd13a1fSJeff Skirvin 			/* Enable TX equalization (0xe824) */
2159afd13a1fSJeff Skirvin 			writel(0x00040000, &xcvr->afe_tx_control);
2160cc9203bfSDan Williams 		}
2161dbb0743aSAdam Gruchala 
2162cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2163cc9203bfSDan Williams 
21642e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
2165cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2166cc9203bfSDan Williams 
21672e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
2168cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2169cc9203bfSDan Williams 
21702e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
2171cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2172cc9203bfSDan Williams 
21732e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
2174cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2175cc9203bfSDan Williams 	}
2176cc9203bfSDan Williams 
2177cc9203bfSDan Williams 	/* Transfer control to the PEs */
21782e5da889SDan Williams 	writel(0x00010f00, &afe->afe_dfx_master_control0);
2179cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2180cc9203bfSDan Williams }
2181cc9203bfSDan Williams 
218289a7301fSDan Williams static void sci_controller_initialize_power_control(struct isci_host *ihost)
2183cc9203bfSDan Williams {
2184d9dcb4baSDan Williams 	sci_init_timer(&ihost->power_control.timer, power_control_timeout);
2185cc9203bfSDan Williams 
2186d9dcb4baSDan Williams 	memset(ihost->power_control.requesters, 0,
2187d9dcb4baSDan Williams 	       sizeof(ihost->power_control.requesters));
2188cc9203bfSDan Williams 
2189d9dcb4baSDan Williams 	ihost->power_control.phys_waiting = 0;
2190d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
2191cc9203bfSDan Williams }
2192cc9203bfSDan Williams 
219389a7301fSDan Williams static enum sci_status sci_controller_initialize(struct isci_host *ihost)
2194cc9203bfSDan Williams {
2195d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
21967c78da31SDan Williams 	enum sci_status result = SCI_FAILURE;
21977c78da31SDan Williams 	unsigned long i, state, val;
2198cc9203bfSDan Williams 
2199d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_RESET) {
220014e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
220114e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2202cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2203cc9203bfSDan Williams 	}
2204cc9203bfSDan Williams 
2205e301370aSEdmund Nadolski 	sci_change_state(sm, SCIC_INITIALIZING);
2206cc9203bfSDan Williams 
2207d9dcb4baSDan Williams 	sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
2208bb3dbdf6SEdmund Nadolski 
2209d9dcb4baSDan Williams 	ihost->next_phy_to_start = 0;
2210d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
2211cc9203bfSDan Williams 
221289a7301fSDan Williams 	sci_controller_initialize_power_control(ihost);
2213cc9203bfSDan Williams 
2214cc9203bfSDan Williams 	/*
2215cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2216cc9203bfSDan Williams 	 * program the AFE registers.
2217cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2218cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
221989a7301fSDan Williams 	sci_controller_afe_initialization(ihost);
2220cc9203bfSDan Williams 
2221cc9203bfSDan Williams 
2222cc9203bfSDan Williams 	/* Take the hardware out of reset */
2223d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->soft_reset_control);
2224cc9203bfSDan Williams 
2225cc9203bfSDan Williams 	/*
2226cc9203bfSDan Williams 	 * / @todo Provide meaningfull error code for hardware failure
2227cc9203bfSDan Williams 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
22287c78da31SDan Williams 	for (i = 100; i >= 1; i--) {
22297c78da31SDan Williams 		u32 status;
2230cc9203bfSDan Williams 
2231cc9203bfSDan Williams 		/* Loop until the hardware reports success */
2232cc9203bfSDan Williams 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2233d9dcb4baSDan Williams 		status = readl(&ihost->smu_registers->control_status);
2234cc9203bfSDan Williams 
22357c78da31SDan Williams 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
22367c78da31SDan Williams 			break;
2237cc9203bfSDan Williams 	}
22387c78da31SDan Williams 	if (i == 0)
22397c78da31SDan Williams 		goto out;
2240cc9203bfSDan Williams 
2241cc9203bfSDan Williams 	/*
2242cc9203bfSDan Williams 	 * Determine what are the actaul device capacities that the
2243cc9203bfSDan Williams 	 * hardware will support */
2244d9dcb4baSDan Williams 	val = readl(&ihost->smu_registers->device_context_capacity);
2245cc9203bfSDan Williams 
22467c78da31SDan Williams 	/* Record the smaller of the two capacity values */
2247d9dcb4baSDan Williams 	ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2248d9dcb4baSDan Williams 	ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2249d9dcb4baSDan Williams 	ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2250cc9203bfSDan Williams 
2251cc9203bfSDan Williams 	/*
2252cc9203bfSDan Williams 	 * Make all PEs that are unassigned match up with the
2253cc9203bfSDan Williams 	 * logical ports
2254cc9203bfSDan Williams 	 */
2255d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
2256cc9203bfSDan Williams 		struct scu_port_task_scheduler_group_registers __iomem
2257d9dcb4baSDan Williams 			*ptsg = &ihost->scu_registers->peg0.ptsg;
2258cc9203bfSDan Williams 
22597c78da31SDan Williams 		writel(i, &ptsg->protocol_engine[i]);
2260cc9203bfSDan Williams 	}
2261cc9203bfSDan Williams 
2262cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2263d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.pdma_configuration);
22647c78da31SDan Williams 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2265d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.pdma_configuration);
2266cc9203bfSDan Williams 
2267d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.cdma_configuration);
22687c78da31SDan Williams 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2269d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.cdma_configuration);
2270cc9203bfSDan Williams 
2271cc9203bfSDan Williams 	/*
2272cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2273cc9203bfSDan Williams 	 * are accessed during the port initialization.
2274cc9203bfSDan Williams 	 */
22757c78da31SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
227689a7301fSDan Williams 		result = sci_phy_initialize(&ihost->phys[i],
2277d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].tl,
2278d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].ll);
22797c78da31SDan Williams 		if (result != SCI_SUCCESS)
22807c78da31SDan Williams 			goto out;
2281cc9203bfSDan Williams 	}
2282cc9203bfSDan Williams 
2283d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
228489a7301fSDan Williams 		struct isci_port *iport = &ihost->ports[i];
22857c78da31SDan Williams 
228689a7301fSDan Williams 		iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
228789a7301fSDan Williams 		iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
228889a7301fSDan Williams 		iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
2289cc9203bfSDan Williams 	}
2290cc9203bfSDan Williams 
229189a7301fSDan Williams 	result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
2292cc9203bfSDan Williams 
22937c78da31SDan Williams  out:
2294cc9203bfSDan Williams 	/* Advance the controller state machine */
2295cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2296e301370aSEdmund Nadolski 		state = SCIC_INITIALIZED;
2297cc9203bfSDan Williams 	else
2298e301370aSEdmund Nadolski 		state = SCIC_FAILED;
2299e301370aSEdmund Nadolski 	sci_change_state(sm, state);
2300cc9203bfSDan Williams 
2301cc9203bfSDan Williams 	return result;
2302cc9203bfSDan Williams }
2303cc9203bfSDan Williams 
2304abec912dSDan Williams static int sci_controller_dma_alloc(struct isci_host *ihost)
2305cc9203bfSDan Williams {
2306abec912dSDan Williams 	struct device *dev = &ihost->pdev->dev;
2307abec912dSDan Williams 	size_t size;
2308abec912dSDan Williams 	int i;
2309cc9203bfSDan Williams 
2310abec912dSDan Williams 	/* detect re-initialization */
2311abec912dSDan Williams 	if (ihost->completion_queue)
2312abec912dSDan Williams 		return 0;
2313cc9203bfSDan Williams 
2314abec912dSDan Williams 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2315abec912dSDan Williams 	ihost->completion_queue = dmam_alloc_coherent(dev, size, &ihost->cq_dma,
2316abec912dSDan Williams 						      GFP_KERNEL);
2317abec912dSDan Williams 	if (!ihost->completion_queue)
2318abec912dSDan Williams 		return -ENOMEM;
2319cc9203bfSDan Williams 
2320abec912dSDan Williams 	size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2321abec912dSDan Williams 	ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &ihost->rnc_dma,
2322abec912dSDan Williams 							       GFP_KERNEL);
2323cc9203bfSDan Williams 
2324abec912dSDan Williams 	if (!ihost->remote_node_context_table)
2325abec912dSDan Williams 		return -ENOMEM;
2326cc9203bfSDan Williams 
2327abec912dSDan Williams 	size = ihost->task_context_entries * sizeof(struct scu_task_context),
2328abec912dSDan Williams 	ihost->task_context_table = dmam_alloc_coherent(dev, size, &ihost->tc_dma,
2329abec912dSDan Williams 							GFP_KERNEL);
2330abec912dSDan Williams 	if (!ihost->task_context_table)
2331abec912dSDan Williams 		return -ENOMEM;
2332cc9203bfSDan Williams 
2333abec912dSDan Williams 	size = SCI_UFI_TOTAL_SIZE;
2334abec912dSDan Williams 	ihost->ufi_buf = dmam_alloc_coherent(dev, size, &ihost->ufi_dma, GFP_KERNEL);
2335abec912dSDan Williams 	if (!ihost->ufi_buf)
2336abec912dSDan Williams 		return -ENOMEM;
2337abec912dSDan Williams 
2338abec912dSDan Williams 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2339abec912dSDan Williams 		struct isci_request *ireq;
2340abec912dSDan Williams 		dma_addr_t dma;
2341abec912dSDan Williams 
2342abec912dSDan Williams 		ireq = dmam_alloc_coherent(dev, sizeof(*ireq), &dma, GFP_KERNEL);
2343abec912dSDan Williams 		if (!ireq)
2344abec912dSDan Williams 			return -ENOMEM;
2345abec912dSDan Williams 
2346abec912dSDan Williams 		ireq->tc = &ihost->task_context_table[i];
2347abec912dSDan Williams 		ireq->owning_controller = ihost;
2348abec912dSDan Williams 		spin_lock_init(&ireq->state_lock);
2349abec912dSDan Williams 		ireq->request_daddr = dma;
2350abec912dSDan Williams 		ireq->isci_host = ihost;
2351abec912dSDan Williams 		ihost->reqs[i] = ireq;
2352cc9203bfSDan Williams 	}
2353cc9203bfSDan Williams 
2354abec912dSDan Williams 	return 0;
2355cc9203bfSDan Williams }
2356cc9203bfSDan Williams 
235789a7301fSDan Williams static int sci_controller_mem_init(struct isci_host *ihost)
2358cc9203bfSDan Williams {
2359abec912dSDan Williams 	int err = sci_controller_dma_alloc(ihost);
2360cc9203bfSDan Williams 
23617c78da31SDan Williams 	if (err)
23627c78da31SDan Williams 		return err;
2363cc9203bfSDan Williams 
2364abec912dSDan Williams 	writel(lower_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_lower);
2365abec912dSDan Williams 	writel(upper_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_upper);
2366abec912dSDan Williams 
2367abec912dSDan Williams 	writel(lower_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_lower);
2368abec912dSDan Williams 	writel(upper_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_upper);
2369abec912dSDan Williams 
2370abec912dSDan Williams 	writel(lower_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_lower);
2371abec912dSDan Williams 	writel(upper_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_upper);
2372abec912dSDan Williams 
2373abec912dSDan Williams 	sci_unsolicited_frame_control_construct(ihost);
2374abec912dSDan Williams 
2375cc9203bfSDan Williams 	/*
2376cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2377cc9203bfSDan Williams 	 * address table.
2378cc9203bfSDan Williams 	 */
2379d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2380d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_lower);
2381d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2382d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_upper);
2383cc9203bfSDan Williams 
2384d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2385d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_lower);
2386d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2387d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_upper);
2388cc9203bfSDan Williams 
2389cc9203bfSDan Williams 	return 0;
2390cc9203bfSDan Williams }
2391cc9203bfSDan Williams 
2392abec912dSDan Williams /**
2393abec912dSDan Williams  * isci_host_init - (re-)initialize hardware and internal (private) state
2394abec912dSDan Williams  * @ihost: host to init
2395abec912dSDan Williams  *
2396abec912dSDan Williams  * Any public facing objects (like asd_sas_port, and asd_sas_phys), or
2397abec912dSDan Williams  * one-time initialization objects like locks and waitqueues, are
2398abec912dSDan Williams  * not touched (they are initialized in isci_host_alloc)
2399abec912dSDan Williams  */
2400d9dcb4baSDan Williams int isci_host_init(struct isci_host *ihost)
24016f231ddaSDan Williams {
2402abec912dSDan Williams 	int i, err;
24036f231ddaSDan Williams 	enum sci_status status;
24046f231ddaSDan Williams 
2405*2396a265SDan Williams 	spin_lock_irq(&ihost->scic_lock);
2406abec912dSDan Williams 	status = sci_controller_construct(ihost, scu_base(ihost), smu_base(ihost));
2407*2396a265SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
24086f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2409d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
241089a7301fSDan Williams 			"%s: sci_controller_construct failed - status = %x\n",
24116f231ddaSDan Williams 			__func__,
24126f231ddaSDan Williams 			status);
2413858d4aa7SDave Jiang 		return -ENODEV;
24146f231ddaSDan Williams 	}
24156f231ddaSDan Williams 
2416d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
241789a7301fSDan Williams 	status = sci_controller_initialize(ihost);
2418d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
24197c40a803SDan Williams 	if (status != SCI_SUCCESS) {
2420d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
242189a7301fSDan Williams 			 "%s: sci_controller_initialize failed -"
24227c40a803SDan Williams 			 " status = 0x%x\n",
24237c40a803SDan Williams 			 __func__, status);
24247c40a803SDan Williams 		return -ENODEV;
24257c40a803SDan Williams 	}
24267c40a803SDan Williams 
242789a7301fSDan Williams 	err = sci_controller_mem_init(ihost);
24286f231ddaSDan Williams 	if (err)
2429858d4aa7SDave Jiang 		return err;
24306f231ddaSDan Williams 
2431ad4f4c1dSDan Williams 	/* enable sgpio */
2432ad4f4c1dSDan Williams 	writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
2433ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
2434ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
2435ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
2436ad4f4c1dSDan Williams 
2437858d4aa7SDave Jiang 	return 0;
24386f231ddaSDan Williams }
2439cc9203bfSDan Williams 
244089a7301fSDan Williams void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
244189a7301fSDan Williams 			    struct isci_phy *iphy)
2442cc9203bfSDan Williams {
2443d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2444e301370aSEdmund Nadolski 	case SCIC_STARTING:
2445d9dcb4baSDan Williams 		sci_del_timer(&ihost->phy_timer);
2446d9dcb4baSDan Williams 		ihost->phy_startup_timer_pending = false;
2447d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2448ffe191c9SDan Williams 						  iport, iphy);
244989a7301fSDan Williams 		sci_controller_start_next_phy(ihost);
2450cc9203bfSDan Williams 		break;
2451e301370aSEdmund Nadolski 	case SCIC_READY:
2452d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2453ffe191c9SDan Williams 						  iport, iphy);
2454cc9203bfSDan Williams 		break;
2455cc9203bfSDan Williams 	default:
2456d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2457cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
245885280955SDan Williams 			"unexpected state %d\n", __func__, iphy->phy_index,
2459d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2460cc9203bfSDan Williams 	}
2461cc9203bfSDan Williams }
2462cc9203bfSDan Williams 
246389a7301fSDan Williams void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
246489a7301fSDan Williams 			      struct isci_phy *iphy)
2465cc9203bfSDan Williams {
2466d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2467e301370aSEdmund Nadolski 	case SCIC_STARTING:
2468e301370aSEdmund Nadolski 	case SCIC_READY:
2469d9dcb4baSDan Williams 		ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
2470ffe191c9SDan Williams 						   iport, iphy);
2471cc9203bfSDan Williams 		break;
2472cc9203bfSDan Williams 	default:
2473d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2474cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2475cc9203bfSDan Williams 			"unexpected state %d\n",
2476cc9203bfSDan Williams 			__func__,
247785280955SDan Williams 			iphy->phy_index,
2478d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2479cc9203bfSDan Williams 	}
2480cc9203bfSDan Williams }
2481cc9203bfSDan Williams 
2482eb608c3cSDan Williams bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
2483cc9203bfSDan Williams {
2484cc9203bfSDan Williams 	u32 index;
2485cc9203bfSDan Williams 
2486d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
2487d9dcb4baSDan Williams 		if ((ihost->device_table[index] != NULL) &&
2488d9dcb4baSDan Williams 		   (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2489cc9203bfSDan Williams 			return true;
2490cc9203bfSDan Williams 	}
2491cc9203bfSDan Williams 
2492cc9203bfSDan Williams 	return false;
2493cc9203bfSDan Williams }
2494cc9203bfSDan Williams 
249589a7301fSDan Williams void sci_controller_remote_device_stopped(struct isci_host *ihost,
249678a6f06eSDan Williams 					  struct isci_remote_device *idev)
2497cc9203bfSDan Williams {
2498d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_STOPPING) {
2499d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2500cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2501cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2502d9dcb4baSDan Williams 			ihost, idev,
2503d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2504cc9203bfSDan Williams 		return;
2505cc9203bfSDan Williams 	}
2506cc9203bfSDan Williams 
250789a7301fSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
2508eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
2509cc9203bfSDan Williams }
2510cc9203bfSDan Williams 
251189a7301fSDan Williams void sci_controller_post_request(struct isci_host *ihost, u32 request)
2512cc9203bfSDan Williams {
251389a7301fSDan Williams 	dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
251489a7301fSDan Williams 		__func__, ihost->id, request);
2515cc9203bfSDan Williams 
2516d9dcb4baSDan Williams 	writel(request, &ihost->smu_registers->post_context_port);
2517cc9203bfSDan Williams }
2518cc9203bfSDan Williams 
251989a7301fSDan Williams struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
2520cc9203bfSDan Williams {
2521cc9203bfSDan Williams 	u16 task_index;
2522cc9203bfSDan Williams 	u16 task_sequence;
2523cc9203bfSDan Williams 
2524dd047c8eSDan Williams 	task_index = ISCI_TAG_TCI(io_tag);
2525cc9203bfSDan Williams 
2526d9dcb4baSDan Williams 	if (task_index < ihost->task_context_entries) {
2527d9dcb4baSDan Williams 		struct isci_request *ireq = ihost->reqs[task_index];
2528db056250SDan Williams 
2529db056250SDan Williams 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2530dd047c8eSDan Williams 			task_sequence = ISCI_TAG_SEQ(io_tag);
2531cc9203bfSDan Williams 
2532d9dcb4baSDan Williams 			if (task_sequence == ihost->io_request_sequence[task_index])
25335076a1a9SDan Williams 				return ireq;
2534cc9203bfSDan Williams 		}
2535cc9203bfSDan Williams 	}
2536cc9203bfSDan Williams 
2537cc9203bfSDan Williams 	return NULL;
2538cc9203bfSDan Williams }
2539cc9203bfSDan Williams 
2540cc9203bfSDan Williams /**
2541cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2542cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2543cc9203bfSDan Williams  *    node index available.
2544cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2545cc9203bfSDan Williams  *    free remote node ids
2546cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2547cc9203bfSDan Williams  *    id
2548cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2549cc9203bfSDan Williams  *    is available
2550cc9203bfSDan Williams  *
2551cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2552cc9203bfSDan Williams  * node index available.
2553cc9203bfSDan Williams  */
255489a7301fSDan Williams enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
255578a6f06eSDan Williams 							    struct isci_remote_device *idev,
2556cc9203bfSDan Williams 							    u16 *node_id)
2557cc9203bfSDan Williams {
2558cc9203bfSDan Williams 	u16 node_index;
255989a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2560cc9203bfSDan Williams 
256189a7301fSDan Williams 	node_index = sci_remote_node_table_allocate_remote_node(
2562d9dcb4baSDan Williams 		&ihost->available_remote_nodes, remote_node_count
2563cc9203bfSDan Williams 		);
2564cc9203bfSDan Williams 
2565cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2566d9dcb4baSDan Williams 		ihost->device_table[node_index] = idev;
2567cc9203bfSDan Williams 
2568cc9203bfSDan Williams 		*node_id = node_index;
2569cc9203bfSDan Williams 
2570cc9203bfSDan Williams 		return SCI_SUCCESS;
2571cc9203bfSDan Williams 	}
2572cc9203bfSDan Williams 
2573cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2574cc9203bfSDan Williams }
2575cc9203bfSDan Williams 
257689a7301fSDan Williams void sci_controller_free_remote_node_context(struct isci_host *ihost,
257778a6f06eSDan Williams 					     struct isci_remote_device *idev,
2578cc9203bfSDan Williams 					     u16 node_id)
2579cc9203bfSDan Williams {
258089a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2581cc9203bfSDan Williams 
2582d9dcb4baSDan Williams 	if (ihost->device_table[node_id] == idev) {
2583d9dcb4baSDan Williams 		ihost->device_table[node_id] = NULL;
2584cc9203bfSDan Williams 
258589a7301fSDan Williams 		sci_remote_node_table_release_remote_node_index(
2586d9dcb4baSDan Williams 			&ihost->available_remote_nodes, remote_node_count, node_id
2587cc9203bfSDan Williams 			);
2588cc9203bfSDan Williams 	}
2589cc9203bfSDan Williams }
2590cc9203bfSDan Williams 
259189a7301fSDan Williams void sci_controller_copy_sata_response(void *response_buffer,
2592cc9203bfSDan Williams 				       void *frame_header,
2593cc9203bfSDan Williams 				       void *frame_buffer)
2594cc9203bfSDan Williams {
259589a7301fSDan Williams 	/* XXX type safety? */
2596cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2597cc9203bfSDan Williams 
2598cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2599cc9203bfSDan Williams 	       frame_buffer,
2600cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2601cc9203bfSDan Williams }
2602cc9203bfSDan Williams 
260389a7301fSDan Williams void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
2604cc9203bfSDan Williams {
260589a7301fSDan Williams 	if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
2606d9dcb4baSDan Williams 		writel(ihost->uf_control.get,
2607d9dcb4baSDan Williams 			&ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
2608cc9203bfSDan Williams }
2609cc9203bfSDan Williams 
2610312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci)
2611312e0c24SDan Williams {
2612312e0c24SDan Williams 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2613312e0c24SDan Williams 
2614312e0c24SDan Williams 	ihost->tci_pool[tail] = tci;
2615312e0c24SDan Williams 	ihost->tci_tail = tail + 1;
2616312e0c24SDan Williams }
2617312e0c24SDan Williams 
2618312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost)
2619312e0c24SDan Williams {
2620312e0c24SDan Williams 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2621312e0c24SDan Williams 	u16 tci = ihost->tci_pool[head];
2622312e0c24SDan Williams 
2623312e0c24SDan Williams 	ihost->tci_head = head + 1;
2624312e0c24SDan Williams 	return tci;
2625312e0c24SDan Williams }
2626312e0c24SDan Williams 
2627312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost)
2628312e0c24SDan Williams {
2629312e0c24SDan Williams 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2630312e0c24SDan Williams }
2631312e0c24SDan Williams 
2632312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost)
2633312e0c24SDan Williams {
2634312e0c24SDan Williams 	if (isci_tci_space(ihost)) {
2635312e0c24SDan Williams 		u16 tci = isci_tci_alloc(ihost);
2636d9dcb4baSDan Williams 		u8 seq = ihost->io_request_sequence[tci];
2637312e0c24SDan Williams 
2638312e0c24SDan Williams 		return ISCI_TAG(seq, tci);
2639312e0c24SDan Williams 	}
2640312e0c24SDan Williams 
2641312e0c24SDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
2642312e0c24SDan Williams }
2643312e0c24SDan Williams 
2644312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2645312e0c24SDan Williams {
2646312e0c24SDan Williams 	u16 tci = ISCI_TAG_TCI(io_tag);
2647312e0c24SDan Williams 	u16 seq = ISCI_TAG_SEQ(io_tag);
2648312e0c24SDan Williams 
2649312e0c24SDan Williams 	/* prevent tail from passing head */
2650312e0c24SDan Williams 	if (isci_tci_active(ihost) == 0)
2651312e0c24SDan Williams 		return SCI_FAILURE_INVALID_IO_TAG;
2652312e0c24SDan Williams 
2653d9dcb4baSDan Williams 	if (seq == ihost->io_request_sequence[tci]) {
2654d9dcb4baSDan Williams 		ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2655312e0c24SDan Williams 
2656312e0c24SDan Williams 		isci_tci_free(ihost, tci);
2657312e0c24SDan Williams 
2658312e0c24SDan Williams 		return SCI_SUCCESS;
2659312e0c24SDan Williams 	}
2660312e0c24SDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
2661312e0c24SDan Williams }
2662312e0c24SDan Williams 
266389a7301fSDan Williams enum sci_status sci_controller_start_io(struct isci_host *ihost,
266478a6f06eSDan Williams 					struct isci_remote_device *idev,
26655076a1a9SDan Williams 					struct isci_request *ireq)
2666cc9203bfSDan Williams {
2667cc9203bfSDan Williams 	enum sci_status status;
2668cc9203bfSDan Williams 
2669d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
267014e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
267114e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2672cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2673cc9203bfSDan Williams 	}
2674cc9203bfSDan Williams 
267589a7301fSDan Williams 	status = sci_remote_device_start_io(ihost, idev, ireq);
2676cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2677cc9203bfSDan Williams 		return status;
2678cc9203bfSDan Williams 
26795076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
268034a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2681cc9203bfSDan Williams 	return SCI_SUCCESS;
2682cc9203bfSDan Williams }
2683cc9203bfSDan Williams 
268489a7301fSDan Williams enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
268578a6f06eSDan Williams 						 struct isci_remote_device *idev,
26865076a1a9SDan Williams 						 struct isci_request *ireq)
2687cc9203bfSDan Williams {
268889a7301fSDan Williams 	/* terminate an ongoing (i.e. started) core IO request.  This does not
268989a7301fSDan Williams 	 * abort the IO request at the target, but rather removes the IO
269089a7301fSDan Williams 	 * request from the host controller.
269189a7301fSDan Williams 	 */
2692cc9203bfSDan Williams 	enum sci_status status;
2693cc9203bfSDan Williams 
2694d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
269514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
269614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2697cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2698cc9203bfSDan Williams 	}
2699cc9203bfSDan Williams 
270089a7301fSDan Williams 	status = sci_io_request_terminate(ireq);
2701cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2702cc9203bfSDan Williams 		return status;
2703cc9203bfSDan Williams 
2704cc9203bfSDan Williams 	/*
2705cc9203bfSDan Williams 	 * Utilize the original post context command and or in the POST_TC_ABORT
2706cc9203bfSDan Williams 	 * request sub-type.
2707cc9203bfSDan Williams 	 */
270889a7301fSDan Williams 	sci_controller_post_request(ihost,
270989a7301fSDan Williams 				    ireq->post_context | SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2710cc9203bfSDan Williams 	return SCI_SUCCESS;
2711cc9203bfSDan Williams }
2712cc9203bfSDan Williams 
2713cc9203bfSDan Williams /**
271489a7301fSDan Williams  * sci_controller_complete_io() - This method will perform core specific
2715cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
2716cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
2717cc9203bfSDan Williams  *    reused (i.e. re-constructed).
271889a7301fSDan Williams  * @ihost: The handle to the controller object for which to complete the
2719cc9203bfSDan Williams  *    IO request.
272089a7301fSDan Williams  * @idev: The handle to the remote device object for which to complete
2721cc9203bfSDan Williams  *    the IO request.
272289a7301fSDan Williams  * @ireq: the handle to the io request object to complete.
2723cc9203bfSDan Williams  */
272489a7301fSDan Williams enum sci_status sci_controller_complete_io(struct isci_host *ihost,
272578a6f06eSDan Williams 					   struct isci_remote_device *idev,
27265076a1a9SDan Williams 					   struct isci_request *ireq)
2727cc9203bfSDan Williams {
2728cc9203bfSDan Williams 	enum sci_status status;
2729cc9203bfSDan Williams 	u16 index;
2730cc9203bfSDan Williams 
2731d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2732e301370aSEdmund Nadolski 	case SCIC_STOPPING:
2733cc9203bfSDan Williams 		/* XXX: Implement this function */
2734cc9203bfSDan Williams 		return SCI_FAILURE;
2735e301370aSEdmund Nadolski 	case SCIC_READY:
273689a7301fSDan Williams 		status = sci_remote_device_complete_io(ihost, idev, ireq);
2737cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
2738cc9203bfSDan Williams 			return status;
2739cc9203bfSDan Williams 
27405076a1a9SDan Williams 		index = ISCI_TAG_TCI(ireq->io_tag);
27415076a1a9SDan Williams 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2742cc9203bfSDan Williams 		return SCI_SUCCESS;
2743cc9203bfSDan Williams 	default:
274414e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
274514e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2746cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2747cc9203bfSDan Williams 	}
2748cc9203bfSDan Williams 
2749cc9203bfSDan Williams }
2750cc9203bfSDan Williams 
275189a7301fSDan Williams enum sci_status sci_controller_continue_io(struct isci_request *ireq)
2752cc9203bfSDan Williams {
2753d9dcb4baSDan Williams 	struct isci_host *ihost = ireq->owning_controller;
2754cc9203bfSDan Williams 
2755d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
275614e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
275714e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2758cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2759cc9203bfSDan Williams 	}
2760cc9203bfSDan Williams 
27615076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
276234a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2763cc9203bfSDan Williams 	return SCI_SUCCESS;
2764cc9203bfSDan Williams }
2765cc9203bfSDan Williams 
2766cc9203bfSDan Williams /**
276789a7301fSDan Williams  * sci_controller_start_task() - This method is called by the SCIC user to
2768cc9203bfSDan Williams  *    send/start a framework task management request.
2769cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
2770cc9203bfSDan Williams  *    management request.
2771cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
2772cc9203bfSDan Williams  *    the task management request.
2773cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
2774cc9203bfSDan Williams  */
277589a7301fSDan Williams enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
277678a6f06eSDan Williams 					       struct isci_remote_device *idev,
27775076a1a9SDan Williams 					       struct isci_request *ireq)
2778cc9203bfSDan Williams {
2779cc9203bfSDan Williams 	enum sci_status status;
2780cc9203bfSDan Williams 
2781d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2782d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2783cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
2784cc9203bfSDan Williams 			 "state\n",
2785cc9203bfSDan Williams 			 __func__);
2786cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
2787cc9203bfSDan Williams 	}
2788cc9203bfSDan Williams 
278989a7301fSDan Williams 	status = sci_remote_device_start_task(ihost, idev, ireq);
2790cc9203bfSDan Williams 	switch (status) {
2791cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2792db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2793cc9203bfSDan Williams 
2794cc9203bfSDan Williams 		/*
2795cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
2796cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
2797cc9203bfSDan Williams 		 * RNC is resumed.)
2798cc9203bfSDan Williams 		 */
2799cc9203bfSDan Williams 		return SCI_SUCCESS;
2800cc9203bfSDan Williams 	case SCI_SUCCESS:
2801db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
280234a99158SDan Williams 		sci_controller_post_request(ihost, ireq->post_context);
2803cc9203bfSDan Williams 		break;
2804cc9203bfSDan Williams 	default:
2805cc9203bfSDan Williams 		break;
2806cc9203bfSDan Williams 	}
2807cc9203bfSDan Williams 
2808cc9203bfSDan Williams 	return status;
2809cc9203bfSDan Williams }
2810ad4f4c1dSDan Williams 
2811ad4f4c1dSDan Williams static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
2812ad4f4c1dSDan Williams {
2813ad4f4c1dSDan Williams 	int d;
2814ad4f4c1dSDan Williams 
2815ad4f4c1dSDan Williams 	/* no support for TX_GP_CFG */
2816ad4f4c1dSDan Williams 	if (reg_index == 0)
2817ad4f4c1dSDan Williams 		return -EINVAL;
2818ad4f4c1dSDan Williams 
2819ad4f4c1dSDan Williams 	for (d = 0; d < isci_gpio_count(ihost); d++) {
2820ad4f4c1dSDan Williams 		u32 val = 0x444; /* all ODx.n clear */
2821ad4f4c1dSDan Williams 		int i;
2822ad4f4c1dSDan Williams 
2823ad4f4c1dSDan Williams 		for (i = 0; i < 3; i++) {
2824ad4f4c1dSDan Williams 			int bit = (i << 2) + 2;
2825ad4f4c1dSDan Williams 
2826ad4f4c1dSDan Williams 			bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
2827ad4f4c1dSDan Williams 						       write_data, reg_index,
2828ad4f4c1dSDan Williams 						       reg_count);
2829ad4f4c1dSDan Williams 			if (bit < 0)
2830ad4f4c1dSDan Williams 				break;
2831ad4f4c1dSDan Williams 
2832ad4f4c1dSDan Williams 			/* if od is set, clear the 'invert' bit */
2833ad4f4c1dSDan Williams 			val &= ~(bit << ((i << 2) + 2));
2834ad4f4c1dSDan Williams 		}
2835ad4f4c1dSDan Williams 
2836ad4f4c1dSDan Williams 		if (i < 3)
2837ad4f4c1dSDan Williams 			break;
2838ad4f4c1dSDan Williams 		writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
2839ad4f4c1dSDan Williams 	}
2840ad4f4c1dSDan Williams 
2841ad4f4c1dSDan Williams 	/* unless reg_index is > 1, we should always be able to write at
2842ad4f4c1dSDan Williams 	 * least one register
2843ad4f4c1dSDan Williams 	 */
2844ad4f4c1dSDan Williams 	return d > 0;
2845ad4f4c1dSDan Williams }
2846ad4f4c1dSDan Williams 
2847ad4f4c1dSDan Williams int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
2848ad4f4c1dSDan Williams 		    u8 reg_count, u8 *write_data)
2849ad4f4c1dSDan Williams {
2850ad4f4c1dSDan Williams 	struct isci_host *ihost = sas_ha->lldd_ha;
2851ad4f4c1dSDan Williams 	int written;
2852ad4f4c1dSDan Williams 
2853ad4f4c1dSDan Williams 	switch (reg_type) {
2854ad4f4c1dSDan Williams 	case SAS_GPIO_REG_TX_GP:
2855ad4f4c1dSDan Williams 		written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
2856ad4f4c1dSDan Williams 		break;
2857ad4f4c1dSDan Williams 	default:
2858ad4f4c1dSDan Williams 		written = -EINVAL;
2859ad4f4c1dSDan Williams 	}
2860ad4f4c1dSDan Williams 
2861ad4f4c1dSDan Williams 	return written;
2862ad4f4c1dSDan Williams }
2863