xref: /openbmc/linux/drivers/scsi/isci/host.c (revision 44ef2bf72df34ef2628dc3475b4507ab984d6744)
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 {
1952396a265SDan Williams 	if (sci_controller_completion_queue_has_entries(ihost))
196cc9203bfSDan Williams 		return true;
1972396a265SDan Williams 
1982396a265SDan Williams 	/* we have a spurious interrupt it could be that we have already
1992396a265SDan Williams 	 * emptied the completion queue from a previous interrupt
2002396a265SDan Williams 	 * FIXME: really!?
2012396a265SDan Williams 	 */
202d9dcb4baSDan Williams 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
203cc9203bfSDan Williams 
2042396a265SDan Williams 	/* There is a race in the hardware that could cause us not to be
2052396a265SDan Williams 	 * notified of an interrupt completion if we do not take this
2062396a265SDan Williams 	 * step.  We will mask then unmask the interrupts so if there is
2072396a265SDan Williams 	 * another interrupt pending the clearing of the interrupt
2082396a265SDan Williams 	 * source we get the next interrupt message.
2092396a265SDan Williams 	 */
2102396a265SDan Williams 	spin_lock(&ihost->scic_lock);
2112396a265SDan 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 	}
2152396a265SDan 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 {
7062396a265SDan 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 {
7122396a265SDan Williams 	clear_bit(IHOST_IRQ_ENABLED, &ihost->flags);
713d9dcb4baSDan Williams 	writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
7142396a265SDan 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 
1047e468dc11SArtur Wojcik void isci_host_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 
1080f8381807SJeff Skirvin void ireq_done(struct isci_host *ihost, struct isci_request *ireq, struct sas_task *task)
1081f8381807SJeff Skirvin {
1082f8381807SJeff Skirvin 	if (!test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags) &&
1083f8381807SJeff Skirvin 	    !(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1084f8381807SJeff Skirvin 		if (test_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags)) {
1085f8381807SJeff Skirvin 			/* Normal notification (task_done) */
1086f8381807SJeff Skirvin 			dev_dbg(&ihost->pdev->dev,
1087f8381807SJeff Skirvin 				"%s: Normal - ireq/task = %p/%p\n",
1088f8381807SJeff Skirvin 				__func__, ireq, task);
108954b46677SJeff Skirvin 			task->lldd_task = NULL;
1090f8381807SJeff Skirvin 			task->task_done(task);
1091f8381807SJeff Skirvin 		} else {
1092f8381807SJeff Skirvin 			dev_dbg(&ihost->pdev->dev,
1093f8381807SJeff Skirvin 				"%s: Error - ireq/task = %p/%p\n",
1094f8381807SJeff Skirvin 				__func__, ireq, task);
109554b46677SJeff Skirvin 			if (sas_protocol_ata(task->task_proto))
109654b46677SJeff Skirvin 				task->lldd_task = NULL;
1097f8381807SJeff Skirvin 			sas_task_abort(task);
1098f8381807SJeff Skirvin 		}
109954b46677SJeff Skirvin 	} else
110054b46677SJeff Skirvin 		task->lldd_task = NULL;
110154b46677SJeff Skirvin 
1102f8381807SJeff Skirvin 	if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
1103f8381807SJeff Skirvin 		wake_up_all(&ihost->eventq);
1104f8381807SJeff Skirvin 
1105f8381807SJeff Skirvin 	if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
1106f8381807SJeff Skirvin 		isci_free_tag(ihost, ireq->io_tag);
1107f8381807SJeff Skirvin }
11086f231ddaSDan Williams /**
11096f231ddaSDan Williams  * isci_host_completion_routine() - This function is the delayed service
11106f231ddaSDan Williams  *    routine that calls the sci core library's completion handler. It's
11116f231ddaSDan Williams  *    scheduled as a tasklet from the interrupt service routine when interrupts
11126f231ddaSDan Williams  *    in use, or set as the timeout function in polled mode.
11136f231ddaSDan Williams  * @data: This parameter specifies the ISCI host object
11146f231ddaSDan Williams  *
11156f231ddaSDan Williams  */
1116abec912dSDan Williams void isci_host_completion_routine(unsigned long data)
11176f231ddaSDan Williams {
1118d9dcb4baSDan Williams 	struct isci_host *ihost = (struct isci_host *)data;
11199b4be528SDan Williams 	u16 active;
11206f231ddaSDan Williams 
1121d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
112289a7301fSDan Williams 	sci_controller_completion_handler(ihost);
1123033d19d2SJeff Skirvin 	spin_unlock_irq(&ihost->scic_lock);
11246f231ddaSDan Williams 
1125*44ef2bf7SDave Jiang 	/*
1126*44ef2bf7SDave Jiang 	 * we subtract SCI_MAX_PORTS to account for the number of dummy TCs
1127*44ef2bf7SDave Jiang 	 * issued for hardware issue workaround
1128*44ef2bf7SDave Jiang 	 */
1129*44ef2bf7SDave Jiang 	active = isci_tci_active(ihost) - SCI_MAX_PORTS;
1130*44ef2bf7SDave Jiang 
1131*44ef2bf7SDave Jiang 	/*
1132*44ef2bf7SDave Jiang 	 * the coalesence timeout doubles at each encoding step, so
11339b4be528SDan Williams 	 * update it based on the ilog2 value of the outstanding requests
11349b4be528SDan Williams 	 */
11359b4be528SDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, active) |
11369b4be528SDan Williams 	       SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
11379b4be528SDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
11386f231ddaSDan Williams }
11396f231ddaSDan Williams 
1140cc9203bfSDan Williams /**
114189a7301fSDan Williams  * sci_controller_stop() - This method will stop an individual controller
1142cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1143cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1144cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1145cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1146cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1147cc9203bfSDan Williams  *    the hardware is halted.
1148cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1149cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1150cc9203bfSDan Williams  *    stop operation should complete.
1151cc9203bfSDan Williams  *
1152cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1153cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1154cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1155cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1156cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1157cc9203bfSDan Williams  */
115889a7301fSDan Williams static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
1159cc9203bfSDan Williams {
1160d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
116114e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
116214e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1163cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1164cc9203bfSDan Williams 	}
1165cc9203bfSDan Williams 
1166d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1167d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STOPPING);
1168cc9203bfSDan Williams 	return SCI_SUCCESS;
1169cc9203bfSDan Williams }
1170cc9203bfSDan Williams 
1171cc9203bfSDan Williams /**
117289a7301fSDan Williams  * sci_controller_reset() - This method will reset the supplied core
1173cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1174cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1175cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1176cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1177cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1178cc9203bfSDan Williams  *
1179cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1180cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1181cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1182cc9203bfSDan Williams  */
118389a7301fSDan Williams static enum sci_status sci_controller_reset(struct isci_host *ihost)
1184cc9203bfSDan Williams {
1185d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
1186e301370aSEdmund Nadolski 	case SCIC_RESET:
1187e301370aSEdmund Nadolski 	case SCIC_READY:
1188eb608c3cSDan Williams 	case SCIC_STOPPING:
1189e301370aSEdmund Nadolski 	case SCIC_FAILED:
1190cc9203bfSDan Williams 		/*
1191cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1192cc9203bfSDan Williams 		 * perform the state transition.
1193cc9203bfSDan Williams 		 */
1194d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_RESETTING);
1195cc9203bfSDan Williams 		return SCI_SUCCESS;
1196cc9203bfSDan Williams 	default:
119714e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
119814e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1199cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1200cc9203bfSDan Williams 	}
1201cc9203bfSDan Williams }
1202cc9203bfSDan Williams 
1203eb608c3cSDan Williams static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
1204eb608c3cSDan Williams {
1205eb608c3cSDan Williams 	u32 index;
1206eb608c3cSDan Williams 	enum sci_status status;
1207eb608c3cSDan Williams 	enum sci_status phy_status;
1208eb608c3cSDan Williams 
1209eb608c3cSDan Williams 	status = SCI_SUCCESS;
1210eb608c3cSDan Williams 
1211eb608c3cSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1212eb608c3cSDan Williams 		phy_status = sci_phy_stop(&ihost->phys[index]);
1213eb608c3cSDan Williams 
1214eb608c3cSDan Williams 		if (phy_status != SCI_SUCCESS &&
1215eb608c3cSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1216eb608c3cSDan Williams 			status = SCI_FAILURE;
1217eb608c3cSDan Williams 
1218eb608c3cSDan Williams 			dev_warn(&ihost->pdev->dev,
1219eb608c3cSDan Williams 				 "%s: Controller stop operation failed to stop "
1220eb608c3cSDan Williams 				 "phy %d because of status %d.\n",
1221eb608c3cSDan Williams 				 __func__,
1222eb608c3cSDan Williams 				 ihost->phys[index].phy_index, phy_status);
1223eb608c3cSDan Williams 		}
1224eb608c3cSDan Williams 	}
1225eb608c3cSDan Williams 
1226eb608c3cSDan Williams 	return status;
1227eb608c3cSDan Williams }
1228eb608c3cSDan Williams 
1229eb608c3cSDan Williams 
1230eb608c3cSDan Williams /**
1231eb608c3cSDan Williams  * isci_host_deinit - shutdown frame reception and dma
1232eb608c3cSDan Williams  * @ihost: host to take down
1233eb608c3cSDan Williams  *
1234eb608c3cSDan Williams  * This is called in either the driver shutdown or the suspend path.  In
1235eb608c3cSDan Williams  * the shutdown case libsas went through port teardown and normal device
1236eb608c3cSDan Williams  * removal (i.e. physical links stayed up to service scsi_device removal
1237eb608c3cSDan Williams  * commands).  In the suspend case we disable the hardware without
1238eb608c3cSDan Williams  * notifying libsas of the link down events since we want libsas to
1239eb608c3cSDan Williams  * remember the domain across the suspend/resume cycle
1240eb608c3cSDan Williams  */
12410cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
12426f231ddaSDan Williams {
12436f231ddaSDan Williams 	int i;
12446f231ddaSDan Williams 
1245ad4f4c1dSDan Williams 	/* disable output data selects */
1246ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
1247ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
1248ad4f4c1dSDan Williams 
12490cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
12507c40a803SDan Williams 
12517c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
125289a7301fSDan Williams 	sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
12537c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
12547c40a803SDan Williams 
12550cf89d1dSDan Williams 	wait_for_stop(ihost);
1256ad4f4c1dSDan Williams 
1257eb608c3cSDan Williams 	/* phy stop is after controller stop to allow port and device to
1258eb608c3cSDan Williams 	 * go idle before shutting down the phys, but the expectation is
1259eb608c3cSDan Williams 	 * that i/o has been shut off well before we reach this
1260eb608c3cSDan Williams 	 * function.
1261eb608c3cSDan Williams 	 */
1262eb608c3cSDan Williams 	sci_controller_stop_phys(ihost);
1263eb608c3cSDan Williams 
1264ad4f4c1dSDan Williams 	/* disable sgpio: where the above wait should give time for the
1265ad4f4c1dSDan Williams 	 * enclosure to sample the gpios going inactive
1266ad4f4c1dSDan Williams 	 */
1267ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
1268ad4f4c1dSDan Williams 
12692396a265SDan Williams 	spin_lock_irq(&ihost->scic_lock);
127089a7301fSDan Williams 	sci_controller_reset(ihost);
12712396a265SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
12725553ba2bSEdmund Nadolski 
12735553ba2bSEdmund Nadolski 	/* Cancel any/all outstanding port timers */
1274d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
1275ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[i];
1276ffe191c9SDan Williams 		del_timer_sync(&iport->timer.timer);
12775553ba2bSEdmund Nadolski 	}
12785553ba2bSEdmund Nadolski 
1279a628d478SEdmund Nadolski 	/* Cancel any/all outstanding phy timers */
1280a628d478SEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
128185280955SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
128285280955SDan Williams 		del_timer_sync(&iphy->sata_timer.timer);
1283a628d478SEdmund Nadolski 	}
1284a628d478SEdmund Nadolski 
1285d9dcb4baSDan Williams 	del_timer_sync(&ihost->port_agent.timer.timer);
1286ac0eeb4fSEdmund Nadolski 
1287d9dcb4baSDan Williams 	del_timer_sync(&ihost->power_control.timer.timer);
12880473661aSEdmund Nadolski 
1289d9dcb4baSDan Williams 	del_timer_sync(&ihost->timer.timer);
12906cb5853dSEdmund Nadolski 
1291d9dcb4baSDan Williams 	del_timer_sync(&ihost->phy_timer.timer);
12926f231ddaSDan Williams }
12936f231ddaSDan Williams 
12946f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
12956f231ddaSDan Williams {
12966f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
12976f231ddaSDan Williams 	int id = isci_host->id;
12986f231ddaSDan Williams 
12996f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
13006f231ddaSDan Williams }
13016f231ddaSDan Williams 
13026f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
13036f231ddaSDan Williams {
13046f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13056f231ddaSDan Williams 	int id = isci_host->id;
13066f231ddaSDan Williams 
13076f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13086f231ddaSDan Williams }
13096f231ddaSDan Williams 
131089a7301fSDan Williams static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
1311cc9203bfSDan Williams {
1312d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1313cc9203bfSDan Williams 
1314d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1315cc9203bfSDan Williams }
1316cc9203bfSDan Williams 
131789a7301fSDan Williams static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
1318cc9203bfSDan Williams {
1319d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1320cc9203bfSDan Williams 
1321d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1322cc9203bfSDan Williams }
1323cc9203bfSDan Williams 
1324cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1325cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1326cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1327cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1328cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1329cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1330cc9203bfSDan Williams 
1331cc9203bfSDan Williams /**
133289a7301fSDan Williams  * sci_controller_set_interrupt_coalescence() - This method allows the user to
1333cc9203bfSDan Williams  *    configure the interrupt coalescence.
1334cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1335cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1336cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1337cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1338cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1339cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1340cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1341cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1342cc9203bfSDan Williams  *    interrupt coalescing timeout.
1343cc9203bfSDan Williams  *
1344cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1345cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1346cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1347cc9203bfSDan Williams  */
1348d9dcb4baSDan Williams static enum sci_status
134989a7301fSDan Williams sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1350cc9203bfSDan Williams 					 u32 coalesce_number,
1351cc9203bfSDan Williams 					 u32 coalesce_timeout)
1352cc9203bfSDan Williams {
1353cc9203bfSDan Williams 	u8 timeout_encode = 0;
1354cc9203bfSDan Williams 	u32 min = 0;
1355cc9203bfSDan Williams 	u32 max = 0;
1356cc9203bfSDan Williams 
1357cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1358cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1359cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1360cc9203bfSDan Williams 
1361cc9203bfSDan Williams 	/*
1362cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1363cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1364cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1365cc9203bfSDan Williams 	 *              0       -        -       Disabled
1366cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1367cc9203bfSDan Williams 	 *              2       26.7     40.0
1368cc9203bfSDan Williams 	 *              3       53.3     80.0
1369cc9203bfSDan Williams 	 *              4       106.7    160.0
1370cc9203bfSDan Williams 	 *              5       213.3    320.0
1371cc9203bfSDan Williams 	 *              6       426.7    640.0
1372cc9203bfSDan Williams 	 *              7       853.3    1280.0
1373cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1374cc9203bfSDan Williams 	 *              9       3.4      5.1
1375cc9203bfSDan Williams 	 *              10      6.8      10.2
1376cc9203bfSDan Williams 	 *              11      13.7     20.5
1377cc9203bfSDan Williams 	 *              12      27.3     41.0
1378cc9203bfSDan Williams 	 *              13      54.6     81.9
1379cc9203bfSDan Williams 	 *              14      109.2    163.8
1380cc9203bfSDan Williams 	 *              15      218.5    327.7
1381cc9203bfSDan Williams 	 *              16      436.9    655.4
1382cc9203bfSDan Williams 	 *              17      873.8    1310.7
1383cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1384cc9203bfSDan Williams 	 *              19      3.5      5.2
1385cc9203bfSDan Williams 	 *              20      7.0      10.5
1386cc9203bfSDan Williams 	 *              21      14.0     21.0
1387cc9203bfSDan Williams 	 *              22      28.0     41.9
1388cc9203bfSDan Williams 	 *              23      55.9     83.9
1389cc9203bfSDan Williams 	 *              24      111.8    167.8
1390cc9203bfSDan Williams 	 *              25      223.7    335.5
1391cc9203bfSDan Williams 	 *              26      447.4    671.1
1392cc9203bfSDan Williams 	 *              27      894.8    1342.2
1393cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1394cc9203bfSDan Williams 	 *              Others Undefined */
1395cc9203bfSDan Williams 
1396cc9203bfSDan Williams 	/*
1397cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1398cc9203bfSDan Williams 	 * value for register writing. */
1399cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1400cc9203bfSDan Williams 		timeout_encode = 0;
1401cc9203bfSDan Williams 	else{
1402cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1403cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1404cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1405cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1406cc9203bfSDan Williams 
1407cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1408cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1409cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1410cc9203bfSDan Williams 		      timeout_encode++) {
1411cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1412cc9203bfSDan Williams 				break;
1413cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1414cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1415cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1416cc9203bfSDan Williams 					break;
1417cc9203bfSDan Williams 				else{
1418cc9203bfSDan Williams 					timeout_encode++;
1419cc9203bfSDan Williams 					break;
1420cc9203bfSDan Williams 				}
1421cc9203bfSDan Williams 			} else {
1422cc9203bfSDan Williams 				max = max * 2;
1423cc9203bfSDan Williams 				min = min * 2;
1424cc9203bfSDan Williams 			}
1425cc9203bfSDan Williams 		}
1426cc9203bfSDan Williams 
1427cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1428cc9203bfSDan Williams 			/* the value is out of range. */
1429cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1430cc9203bfSDan Williams 	}
1431cc9203bfSDan Williams 
1432cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1433cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1434d9dcb4baSDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
1435cc9203bfSDan Williams 
1436cc9203bfSDan Williams 
1437d9dcb4baSDan Williams 	ihost->interrupt_coalesce_number = (u16)coalesce_number;
1438d9dcb4baSDan Williams 	ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
1439cc9203bfSDan Williams 
1440cc9203bfSDan Williams 	return SCI_SUCCESS;
1441cc9203bfSDan Williams }
1442cc9203bfSDan Williams 
1443cc9203bfSDan Williams 
144489a7301fSDan Williams static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
1445cc9203bfSDan Williams {
1446d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1447e5cc6aa4SMarcin Tomczak 	u32 val;
1448e5cc6aa4SMarcin Tomczak 
1449e5cc6aa4SMarcin Tomczak 	/* enable clock gating for power control of the scu unit */
1450e5cc6aa4SMarcin Tomczak 	val = readl(&ihost->smu_registers->clock_gating_control);
1451e5cc6aa4SMarcin Tomczak 	val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
1452e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
1453e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
1454e5cc6aa4SMarcin Tomczak 	val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
1455e5cc6aa4SMarcin Tomczak 	writel(val, &ihost->smu_registers->clock_gating_control);
1456cc9203bfSDan Williams 
1457cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
14589b4be528SDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1459cc9203bfSDan Williams }
1460cc9203bfSDan Williams 
146189a7301fSDan Williams static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
1462cc9203bfSDan Williams {
1463d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1464cc9203bfSDan Williams 
1465cc9203bfSDan Williams 	/* disable interrupt coalescence. */
146689a7301fSDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1467cc9203bfSDan Williams }
1468cc9203bfSDan Williams 
146989a7301fSDan Williams static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
1470cc9203bfSDan Williams {
1471cc9203bfSDan Williams 	u32 index;
1472cc9203bfSDan Williams 	enum sci_status port_status;
1473cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1474cc9203bfSDan Williams 
1475d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1476ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1477cc9203bfSDan Williams 
147889a7301fSDan Williams 		port_status = sci_port_stop(iport);
1479cc9203bfSDan Williams 
1480cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1481cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1482cc9203bfSDan Williams 			status = SCI_FAILURE;
1483cc9203bfSDan Williams 
1484d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1485cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1486cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1487cc9203bfSDan Williams 				 __func__,
1488ffe191c9SDan Williams 				 iport->logical_port_index,
1489cc9203bfSDan Williams 				 port_status);
1490cc9203bfSDan Williams 		}
1491cc9203bfSDan Williams 	}
1492cc9203bfSDan Williams 
1493cc9203bfSDan Williams 	return status;
1494cc9203bfSDan Williams }
1495cc9203bfSDan Williams 
149689a7301fSDan Williams static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
1497cc9203bfSDan Williams {
1498cc9203bfSDan Williams 	u32 index;
1499cc9203bfSDan Williams 	enum sci_status status;
1500cc9203bfSDan Williams 	enum sci_status device_status;
1501cc9203bfSDan Williams 
1502cc9203bfSDan Williams 	status = SCI_SUCCESS;
1503cc9203bfSDan Williams 
1504d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
1505d9dcb4baSDan Williams 		if (ihost->device_table[index] != NULL) {
1506cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
150789a7301fSDan Williams 			device_status = sci_remote_device_stop(ihost->device_table[index], 0);
1508cc9203bfSDan Williams 
1509cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1510cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1511d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
1512cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1513cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1514cc9203bfSDan Williams 					 "status %d.\n",
1515cc9203bfSDan Williams 					 __func__,
1516d9dcb4baSDan Williams 					 ihost->device_table[index], device_status);
1517cc9203bfSDan Williams 			}
1518cc9203bfSDan Williams 		}
1519cc9203bfSDan Williams 	}
1520cc9203bfSDan Williams 
1521cc9203bfSDan Williams 	return status;
1522cc9203bfSDan Williams }
1523cc9203bfSDan Williams 
152489a7301fSDan Williams static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1525cc9203bfSDan Williams {
1526d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1527cc9203bfSDan Williams 
152889a7301fSDan Williams 	sci_controller_stop_devices(ihost);
1529eb608c3cSDan Williams 	sci_controller_stop_ports(ihost);
1530eb608c3cSDan Williams 
1531eb608c3cSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
1532eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
1533cc9203bfSDan Williams }
1534cc9203bfSDan Williams 
153589a7301fSDan Williams static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1536cc9203bfSDan Williams {
1537d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1538cc9203bfSDan Williams 
1539d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1540cc9203bfSDan Williams }
1541cc9203bfSDan Williams 
154289a7301fSDan Williams static void sci_controller_reset_hardware(struct isci_host *ihost)
1543cc9203bfSDan Williams {
1544cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
154589a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1546cc9203bfSDan Williams 
1547cc9203bfSDan Williams 	/* Reset the SCU */
1548d9dcb4baSDan Williams 	writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
1549cc9203bfSDan Williams 
1550cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1551cc9203bfSDan Williams 	udelay(1000);
1552cc9203bfSDan Williams 
1553cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1554d9dcb4baSDan Williams 	writel(0x00000000, &ihost->smu_registers->completion_queue_get);
1555cc9203bfSDan Williams 
1556cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1557d9dcb4baSDan Williams 	writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
15582396a265SDan Williams 
15592396a265SDan Williams 	/* clear all interrupts */
15602396a265SDan Williams 	writel(~SMU_INTERRUPT_STATUS_RESERVED_MASK, &ihost->smu_registers->interrupt_status);
1561cc9203bfSDan Williams }
1562cc9203bfSDan Williams 
156389a7301fSDan Williams static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1564cc9203bfSDan Williams {
1565d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1566cc9203bfSDan Williams 
156789a7301fSDan Williams 	sci_controller_reset_hardware(ihost);
1568d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1569cc9203bfSDan Williams }
1570cc9203bfSDan Williams 
157189a7301fSDan Williams static const struct sci_base_state sci_controller_state_table[] = {
1572e301370aSEdmund Nadolski 	[SCIC_INITIAL] = {
157389a7301fSDan Williams 		.enter_state = sci_controller_initial_state_enter,
1574cc9203bfSDan Williams 	},
1575e301370aSEdmund Nadolski 	[SCIC_RESET] = {},
1576e301370aSEdmund Nadolski 	[SCIC_INITIALIZING] = {},
1577e301370aSEdmund Nadolski 	[SCIC_INITIALIZED] = {},
1578e301370aSEdmund Nadolski 	[SCIC_STARTING] = {
157989a7301fSDan Williams 		.exit_state  = sci_controller_starting_state_exit,
1580cc9203bfSDan Williams 	},
1581e301370aSEdmund Nadolski 	[SCIC_READY] = {
158289a7301fSDan Williams 		.enter_state = sci_controller_ready_state_enter,
158389a7301fSDan Williams 		.exit_state  = sci_controller_ready_state_exit,
1584cc9203bfSDan Williams 	},
1585e301370aSEdmund Nadolski 	[SCIC_RESETTING] = {
158689a7301fSDan Williams 		.enter_state = sci_controller_resetting_state_enter,
1587cc9203bfSDan Williams 	},
1588e301370aSEdmund Nadolski 	[SCIC_STOPPING] = {
158989a7301fSDan Williams 		.enter_state = sci_controller_stopping_state_enter,
159089a7301fSDan Williams 		.exit_state = sci_controller_stopping_state_exit,
1591cc9203bfSDan Williams 	},
1592e301370aSEdmund Nadolski 	[SCIC_FAILED] = {}
1593cc9203bfSDan Williams };
1594cc9203bfSDan Williams 
15956cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data)
15966cb5853dSEdmund Nadolski {
15976cb5853dSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1598d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1599d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
16006cb5853dSEdmund Nadolski 	unsigned long flags;
1601cc9203bfSDan Williams 
16026cb5853dSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
16036cb5853dSEdmund Nadolski 
16046cb5853dSEdmund Nadolski 	if (tmr->cancel)
16056cb5853dSEdmund Nadolski 		goto done;
16066cb5853dSEdmund Nadolski 
1607e301370aSEdmund Nadolski 	if (sm->current_state_id == SCIC_STARTING)
160889a7301fSDan Williams 		sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
1609e301370aSEdmund Nadolski 	else if (sm->current_state_id == SCIC_STOPPING) {
1610e301370aSEdmund Nadolski 		sci_change_state(sm, SCIC_FAILED);
1611eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
16126cb5853dSEdmund Nadolski 	} else	/* / @todo Now what do we want to do in this case? */
1613d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
16146cb5853dSEdmund Nadolski 			"%s: Controller timer fired when controller was not "
16156cb5853dSEdmund Nadolski 			"in a state being timed.\n",
16166cb5853dSEdmund Nadolski 			__func__);
16176cb5853dSEdmund Nadolski 
16186cb5853dSEdmund Nadolski done:
16196cb5853dSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
16206cb5853dSEdmund Nadolski }
1621cc9203bfSDan Williams 
162289a7301fSDan Williams static enum sci_status sci_controller_construct(struct isci_host *ihost,
1623cc9203bfSDan Williams 						void __iomem *scu_base,
1624cc9203bfSDan Williams 						void __iomem *smu_base)
1625cc9203bfSDan Williams {
1626cc9203bfSDan Williams 	u8 i;
1627cc9203bfSDan Williams 
162889a7301fSDan Williams 	sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
1629cc9203bfSDan Williams 
1630d9dcb4baSDan Williams 	ihost->scu_registers = scu_base;
1631d9dcb4baSDan Williams 	ihost->smu_registers = smu_base;
1632cc9203bfSDan Williams 
163389a7301fSDan Williams 	sci_port_configuration_agent_construct(&ihost->port_agent);
1634cc9203bfSDan Williams 
1635cc9203bfSDan Williams 	/* Construct the ports for this controller */
1636cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
163789a7301fSDan Williams 		sci_port_construct(&ihost->ports[i], i, ihost);
163889a7301fSDan Williams 	sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
1639cc9203bfSDan Williams 
1640cc9203bfSDan Williams 	/* Construct the phys for this controller */
1641cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1642cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
164389a7301fSDan Williams 		sci_phy_construct(&ihost->phys[i],
1644ffe191c9SDan Williams 				  &ihost->ports[SCI_MAX_PORTS], i);
1645cc9203bfSDan Williams 	}
1646cc9203bfSDan Williams 
1647d9dcb4baSDan Williams 	ihost->invalid_phy_mask = 0;
1648cc9203bfSDan Williams 
1649d9dcb4baSDan Williams 	sci_init_timer(&ihost->timer, controller_timeout);
16506cb5853dSEdmund Nadolski 
165189a7301fSDan Williams 	return sci_controller_reset(ihost);
1652cc9203bfSDan Williams }
1653cc9203bfSDan Williams 
1654594e566aSDave Jiang int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
1655cc9203bfSDan Williams {
1656cc9203bfSDan Williams 	int i;
1657cc9203bfSDan Williams 
1658cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1659cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1660cc9203bfSDan Williams 			return -EINVAL;
1661cc9203bfSDan Williams 
1662cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1663cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1664cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1665cc9203bfSDan Williams 			return -EINVAL;
1666cc9203bfSDan Williams 
1667cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1668cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1669cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1670cc9203bfSDan Williams 				return -EINVAL;
1671cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1672cc9203bfSDan Williams 		u8 phy_mask = 0;
1673cc9203bfSDan Williams 
1674cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1675cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1676cc9203bfSDan Williams 
1677cc9203bfSDan Williams 		if (phy_mask == 0)
1678cc9203bfSDan Williams 			return -EINVAL;
1679cc9203bfSDan Williams 	} else
1680cc9203bfSDan Williams 		return -EINVAL;
1681cc9203bfSDan Williams 
16827000f7c7SAndrzej Jakowski 	if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
16837000f7c7SAndrzej Jakowski 	    oem->controller.max_concurr_spin_up < 1)
1684cc9203bfSDan Williams 		return -EINVAL;
1685cc9203bfSDan Williams 
1686594e566aSDave Jiang 	if (oem->controller.do_enable_ssc) {
1687594e566aSDave Jiang 		if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
1688594e566aSDave Jiang 			return -EINVAL;
1689594e566aSDave Jiang 
1690594e566aSDave Jiang 		if (version >= ISCI_ROM_VER_1_1) {
1691594e566aSDave Jiang 			u8 test = oem->controller.ssc_sata_tx_spread_level;
1692594e566aSDave Jiang 
1693594e566aSDave Jiang 			switch (test) {
1694594e566aSDave Jiang 			case 0:
1695594e566aSDave Jiang 			case 2:
1696594e566aSDave Jiang 			case 3:
1697594e566aSDave Jiang 			case 6:
1698594e566aSDave Jiang 			case 7:
1699594e566aSDave Jiang 				break;
1700594e566aSDave Jiang 			default:
1701594e566aSDave Jiang 				return -EINVAL;
1702594e566aSDave Jiang 			}
1703594e566aSDave Jiang 
1704594e566aSDave Jiang 			test = oem->controller.ssc_sas_tx_spread_level;
1705594e566aSDave Jiang 			if (oem->controller.ssc_sas_tx_type == 0) {
1706594e566aSDave Jiang 				switch (test) {
1707594e566aSDave Jiang 				case 0:
1708594e566aSDave Jiang 				case 2:
1709594e566aSDave Jiang 				case 3:
1710594e566aSDave Jiang 					break;
1711594e566aSDave Jiang 				default:
1712594e566aSDave Jiang 					return -EINVAL;
1713594e566aSDave Jiang 				}
1714594e566aSDave Jiang 			} else if (oem->controller.ssc_sas_tx_type == 1) {
1715594e566aSDave Jiang 				switch (test) {
1716594e566aSDave Jiang 				case 0:
1717594e566aSDave Jiang 				case 3:
1718594e566aSDave Jiang 				case 6:
1719594e566aSDave Jiang 					break;
1720594e566aSDave Jiang 				default:
1721594e566aSDave Jiang 					return -EINVAL;
1722594e566aSDave Jiang 				}
1723594e566aSDave Jiang 			}
1724594e566aSDave Jiang 		}
1725594e566aSDave Jiang 	}
1726594e566aSDave Jiang 
1727cc9203bfSDan Williams 	return 0;
1728cc9203bfSDan Williams }
1729cc9203bfSDan Williams 
17307000f7c7SAndrzej Jakowski static u8 max_spin_up(struct isci_host *ihost)
17317000f7c7SAndrzej Jakowski {
17327000f7c7SAndrzej Jakowski 	if (ihost->user_parameters.max_concurr_spinup)
17337000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->user_parameters.max_concurr_spinup,
17347000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
17357000f7c7SAndrzej Jakowski 	else
17367000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
17377000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
17387000f7c7SAndrzej Jakowski }
17397000f7c7SAndrzej Jakowski 
17400473661aSEdmund Nadolski static void power_control_timeout(unsigned long data)
1741cc9203bfSDan Williams {
17420473661aSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1743d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
174485280955SDan Williams 	struct isci_phy *iphy;
17450473661aSEdmund Nadolski 	unsigned long flags;
17460473661aSEdmund Nadolski 	u8 i;
1747cc9203bfSDan Williams 
17480473661aSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
1749cc9203bfSDan Williams 
17500473661aSEdmund Nadolski 	if (tmr->cancel)
17510473661aSEdmund Nadolski 		goto done;
1752cc9203bfSDan Williams 
1753d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
1754cc9203bfSDan Williams 
1755d9dcb4baSDan Williams 	if (ihost->power_control.phys_waiting == 0) {
1756d9dcb4baSDan Williams 		ihost->power_control.timer_started = false;
17570473661aSEdmund Nadolski 		goto done;
17580473661aSEdmund Nadolski 	}
1759cc9203bfSDan Williams 
17600473661aSEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
17610473661aSEdmund Nadolski 
1762d9dcb4baSDan Williams 		if (ihost->power_control.phys_waiting == 0)
17630473661aSEdmund Nadolski 			break;
17640473661aSEdmund Nadolski 
1765d9dcb4baSDan Williams 		iphy = ihost->power_control.requesters[i];
176685280955SDan Williams 		if (iphy == NULL)
17670473661aSEdmund Nadolski 			continue;
17680473661aSEdmund Nadolski 
17697000f7c7SAndrzej Jakowski 		if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
17700473661aSEdmund Nadolski 			break;
17710473661aSEdmund Nadolski 
1772d9dcb4baSDan Williams 		ihost->power_control.requesters[i] = NULL;
1773d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1774d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
177589a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1776be778341SMarcin Tomczak 
1777c79dd80dSDan Williams 		if (iphy->protocol == SAS_PROTOCOL_SSP) {
1778be778341SMarcin Tomczak 			u8 j;
1779be778341SMarcin Tomczak 
1780be778341SMarcin Tomczak 			for (j = 0; j < SCI_MAX_PHYS; j++) {
1781be778341SMarcin Tomczak 				struct isci_phy *requester = ihost->power_control.requesters[j];
1782be778341SMarcin Tomczak 
1783be778341SMarcin Tomczak 				/*
1784be778341SMarcin Tomczak 				 * Search the power_control queue to see if there are other phys
1785be778341SMarcin Tomczak 				 * attached to the same remote device. If found, take all of
1786be778341SMarcin Tomczak 				 * them out of await_sas_power state.
1787be778341SMarcin Tomczak 				 */
1788be778341SMarcin Tomczak 				if (requester != NULL && requester != iphy) {
1789be778341SMarcin Tomczak 					u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
1790be778341SMarcin Tomczak 							  iphy->frame_rcvd.iaf.sas_addr,
1791be778341SMarcin Tomczak 							  sizeof(requester->frame_rcvd.iaf.sas_addr));
1792be778341SMarcin Tomczak 
1793be778341SMarcin Tomczak 					if (other == 0) {
1794be778341SMarcin Tomczak 						ihost->power_control.requesters[j] = NULL;
1795be778341SMarcin Tomczak 						ihost->power_control.phys_waiting--;
1796be778341SMarcin Tomczak 						sci_phy_consume_power_handler(requester);
1797be778341SMarcin Tomczak 					}
1798be778341SMarcin Tomczak 				}
1799be778341SMarcin Tomczak 			}
1800be778341SMarcin Tomczak 		}
1801cc9203bfSDan Williams 	}
1802cc9203bfSDan Williams 
1803cc9203bfSDan Williams 	/*
1804cc9203bfSDan Williams 	 * It doesn't matter if the power list is empty, we need to start the
1805cc9203bfSDan Williams 	 * timer in case another phy becomes ready.
1806cc9203bfSDan Williams 	 */
18070473661aSEdmund Nadolski 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1808d9dcb4baSDan Williams 	ihost->power_control.timer_started = true;
18090473661aSEdmund Nadolski 
18100473661aSEdmund Nadolski done:
18110473661aSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1812cc9203bfSDan Williams }
1813cc9203bfSDan Williams 
181489a7301fSDan Williams void sci_controller_power_control_queue_insert(struct isci_host *ihost,
181585280955SDan Williams 					       struct isci_phy *iphy)
1816cc9203bfSDan Williams {
181785280955SDan Williams 	BUG_ON(iphy == NULL);
1818cc9203bfSDan Williams 
18197000f7c7SAndrzej Jakowski 	if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
1820d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
182189a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1822cc9203bfSDan Williams 
1823cc9203bfSDan Williams 		/*
1824cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1825cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1826cc9203bfSDan Williams 		 */
1827d9dcb4baSDan Williams 		if (ihost->power_control.timer_started)
1828d9dcb4baSDan Williams 			sci_del_timer(&ihost->power_control.timer);
18290473661aSEdmund Nadolski 
1830d9dcb4baSDan Williams 		sci_mod_timer(&ihost->power_control.timer,
18310473661aSEdmund Nadolski 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1832d9dcb4baSDan Williams 		ihost->power_control.timer_started = true;
18330473661aSEdmund Nadolski 
1834cc9203bfSDan Williams 	} else {
1835be778341SMarcin Tomczak 		/*
1836be778341SMarcin Tomczak 		 * There are phys, attached to the same sas address as this phy, are
1837be778341SMarcin Tomczak 		 * already in READY state, this phy don't need wait.
1838be778341SMarcin Tomczak 		 */
1839be778341SMarcin Tomczak 		u8 i;
1840be778341SMarcin Tomczak 		struct isci_phy *current_phy;
1841be778341SMarcin Tomczak 
1842be778341SMarcin Tomczak 		for (i = 0; i < SCI_MAX_PHYS; i++) {
1843be778341SMarcin Tomczak 			u8 other;
1844be778341SMarcin Tomczak 			current_phy = &ihost->phys[i];
1845be778341SMarcin Tomczak 
1846be778341SMarcin Tomczak 			other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
1847be778341SMarcin Tomczak 				       iphy->frame_rcvd.iaf.sas_addr,
1848be778341SMarcin Tomczak 				       sizeof(current_phy->frame_rcvd.iaf.sas_addr));
1849be778341SMarcin Tomczak 
1850be778341SMarcin Tomczak 			if (current_phy->sm.current_state_id == SCI_PHY_READY &&
1851c79dd80dSDan Williams 			    current_phy->protocol == SAS_PROTOCOL_SSP &&
1852be778341SMarcin Tomczak 			    other == 0) {
1853be778341SMarcin Tomczak 				sci_phy_consume_power_handler(iphy);
1854be778341SMarcin Tomczak 				break;
1855be778341SMarcin Tomczak 			}
1856be778341SMarcin Tomczak 		}
1857be778341SMarcin Tomczak 
1858be778341SMarcin Tomczak 		if (i == SCI_MAX_PHYS) {
1859cc9203bfSDan Williams 			/* Add the phy in the waiting list */
1860d9dcb4baSDan Williams 			ihost->power_control.requesters[iphy->phy_index] = iphy;
1861d9dcb4baSDan Williams 			ihost->power_control.phys_waiting++;
1862cc9203bfSDan Williams 		}
1863cc9203bfSDan Williams 	}
1864be778341SMarcin Tomczak }
1865cc9203bfSDan Williams 
186689a7301fSDan Williams void sci_controller_power_control_queue_remove(struct isci_host *ihost,
186785280955SDan Williams 					       struct isci_phy *iphy)
1868cc9203bfSDan Williams {
186985280955SDan Williams 	BUG_ON(iphy == NULL);
1870cc9203bfSDan Williams 
187189a7301fSDan Williams 	if (ihost->power_control.requesters[iphy->phy_index])
1872d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1873cc9203bfSDan Williams 
1874d9dcb4baSDan Williams 	ihost->power_control.requesters[iphy->phy_index] = NULL;
1875cc9203bfSDan Williams }
1876cc9203bfSDan Williams 
1877afd13a1fSJeff Skirvin static int is_long_cable(int phy, unsigned char selection_byte)
1878afd13a1fSJeff Skirvin {
18799fee607fSJeff Skirvin 	return !!(selection_byte & (1 << phy));
1880afd13a1fSJeff Skirvin }
1881afd13a1fSJeff Skirvin 
1882afd13a1fSJeff Skirvin static int is_medium_cable(int phy, unsigned char selection_byte)
1883afd13a1fSJeff Skirvin {
18849fee607fSJeff Skirvin 	return !!(selection_byte & (1 << (phy + 4)));
18859fee607fSJeff Skirvin }
18869fee607fSJeff Skirvin 
18879fee607fSJeff Skirvin static enum cable_selections decode_selection_byte(
18889fee607fSJeff Skirvin 	int phy,
18899fee607fSJeff Skirvin 	unsigned char selection_byte)
18909fee607fSJeff Skirvin {
18919fee607fSJeff Skirvin 	return ((selection_byte & (1 << phy)) ? 1 : 0)
18929fee607fSJeff Skirvin 		+ (selection_byte & (1 << (phy + 4)) ? 2 : 0);
18939fee607fSJeff Skirvin }
18949fee607fSJeff Skirvin 
18959fee607fSJeff Skirvin static unsigned char *to_cable_select(struct isci_host *ihost)
18969fee607fSJeff Skirvin {
18979fee607fSJeff Skirvin 	if (is_cable_select_overridden())
18989fee607fSJeff Skirvin 		return ((unsigned char *)&cable_selection_override)
18999fee607fSJeff Skirvin 			+ ihost->id;
19009fee607fSJeff Skirvin 	else
19019fee607fSJeff Skirvin 		return &ihost->oem_parameters.controller.cable_selection_mask;
19029fee607fSJeff Skirvin }
19039fee607fSJeff Skirvin 
19049fee607fSJeff Skirvin enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
19059fee607fSJeff Skirvin {
19069fee607fSJeff Skirvin 	return decode_selection_byte(phy, *to_cable_select(ihost));
19079fee607fSJeff Skirvin }
19089fee607fSJeff Skirvin 
19099fee607fSJeff Skirvin char *lookup_cable_names(enum cable_selections selection)
19109fee607fSJeff Skirvin {
19119fee607fSJeff Skirvin 	static char *cable_names[] = {
19129fee607fSJeff Skirvin 		[short_cable]     = "short",
19139fee607fSJeff Skirvin 		[long_cable]      = "long",
19149fee607fSJeff Skirvin 		[medium_cable]    = "medium",
19159fee607fSJeff Skirvin 		[undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
19169fee607fSJeff Skirvin 	};
19179fee607fSJeff Skirvin 	return (selection <= undefined_cable) ? cable_names[selection]
19189fee607fSJeff Skirvin 					      : cable_names[undefined_cable];
1919afd13a1fSJeff Skirvin }
1920afd13a1fSJeff Skirvin 
1921cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
1922cc9203bfSDan Williams 
192389a7301fSDan Williams static void sci_controller_afe_initialization(struct isci_host *ihost)
1924cc9203bfSDan Williams {
19252e5da889SDan Williams 	struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
192689a7301fSDan Williams 	const struct sci_oem_params *oem = &ihost->oem_parameters;
1927dc00c8b6SDan Williams 	struct pci_dev *pdev = ihost->pdev;
1928cc9203bfSDan Williams 	u32 afe_status;
1929cc9203bfSDan Williams 	u32 phy_id;
19309fee607fSJeff Skirvin 	unsigned char cable_selection_mask = *to_cable_select(ihost);
1931cc9203bfSDan Williams 
1932cc9203bfSDan Williams 	/* Clear DFX Status registers */
19332e5da889SDan Williams 	writel(0x0081000f, &afe->afe_dfx_master_control0);
1934cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1935cc9203bfSDan Williams 
1936afd13a1fSJeff Skirvin 	if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
1937cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
19382e5da889SDan Williams 		 * Timer, PM Stagger Timer
19392e5da889SDan Williams 		 */
1940afd13a1fSJeff Skirvin 		writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
1941cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1942cc9203bfSDan Williams 	}
1943cc9203bfSDan Williams 
1944cc9203bfSDan Williams 	/* Configure bias currents to normal */
1945dc00c8b6SDan Williams 	if (is_a2(pdev))
19462e5da889SDan Williams 		writel(0x00005A00, &afe->afe_bias_control);
1947dc00c8b6SDan Williams 	else if (is_b0(pdev) || is_c0(pdev))
19482e5da889SDan Williams 		writel(0x00005F00, &afe->afe_bias_control);
1949afd13a1fSJeff Skirvin 	else if (is_c1(pdev))
1950afd13a1fSJeff Skirvin 		writel(0x00005500, &afe->afe_bias_control);
1951cc9203bfSDan Williams 
1952cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1953cc9203bfSDan Williams 
1954cc9203bfSDan Williams 	/* Enable PLL */
1955afd13a1fSJeff Skirvin 	if (is_a2(pdev))
19562e5da889SDan Williams 		writel(0x80040908, &afe->afe_pll_control0);
1957afd13a1fSJeff Skirvin 	else if (is_b0(pdev) || is_c0(pdev))
1958afd13a1fSJeff Skirvin 		writel(0x80040A08, &afe->afe_pll_control0);
1959afd13a1fSJeff Skirvin 	else if (is_c1(pdev)) {
1960afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
1961afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
1962afd13a1fSJeff Skirvin 		writel(0x00000B08, &afe->afe_pll_control0);
1963afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
1964afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
1965afd13a1fSJeff Skirvin 	}
1966cc9203bfSDan Williams 
1967cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1968cc9203bfSDan Williams 
1969cc9203bfSDan Williams 	/* Wait for the PLL to lock */
1970cc9203bfSDan Williams 	do {
19712e5da889SDan Williams 		afe_status = readl(&afe->afe_common_block_status);
1972cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1973cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
1974cc9203bfSDan Williams 
1975dc00c8b6SDan Williams 	if (is_a2(pdev)) {
19762e5da889SDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76
19772e5da889SDan Williams 		 * us to 50 us)
19782e5da889SDan Williams 		 */
19792e5da889SDan Williams 		writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
1980cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1981cc9203bfSDan Williams 	}
1982cc9203bfSDan Williams 
1983cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
19842e5da889SDan Williams 		struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id];
1985cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
1986afd13a1fSJeff Skirvin 		int cable_length_long =
1987afd13a1fSJeff Skirvin 			is_long_cable(phy_id, cable_selection_mask);
1988afd13a1fSJeff Skirvin 		int cable_length_medium =
1989afd13a1fSJeff Skirvin 			is_medium_cable(phy_id, cable_selection_mask);
1990cc9203bfSDan Williams 
1991afd13a1fSJeff Skirvin 		if (is_a2(pdev)) {
19922e5da889SDan Williams 			/* All defaults, except the Receive Word
19932e5da889SDan Williams 			 * Alignament/Comma Detect Enable....(0xe800)
19942e5da889SDan Williams 			 */
19952e5da889SDan Williams 			writel(0x00004512, &xcvr->afe_xcvr_control0);
1996cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1997cc9203bfSDan Williams 
19982e5da889SDan Williams 			writel(0x0050100F, &xcvr->afe_xcvr_control1);
1999cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2000afd13a1fSJeff Skirvin 		} else if (is_b0(pdev)) {
2001afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2002afd13a1fSJeff Skirvin 			writel(0x00030000, &xcvr->afe_tx_ssc_control);
2003afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2004afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2005afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2006afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2007afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2008afd13a1fSJeff Skirvin 
2009afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2010afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2011afd13a1fSJeff Skirvin 			 */
2012afd13a1fSJeff Skirvin 			writel(0x00014500, &xcvr->afe_xcvr_control0);
2013afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2014afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2015afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2016afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2017afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2018afd13a1fSJeff Skirvin 
2019afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2020afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2021afd13a1fSJeff Skirvin 			 */
2022afd13a1fSJeff Skirvin 			writel(0x0001C500, &xcvr->afe_xcvr_control0);
2023afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2024cc9203bfSDan Williams 		}
2025cc9203bfSDan Williams 
2026afd13a1fSJeff Skirvin 		/* Power up TX and RX out from power down (PWRDNTX and
2027afd13a1fSJeff Skirvin 		 * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
20282e5da889SDan Williams 		 */
2029dc00c8b6SDan Williams 		if (is_a2(pdev))
20302e5da889SDan Williams 			writel(0x000003F0, &xcvr->afe_channel_control);
2031dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
20322e5da889SDan Williams 			writel(0x000003D7, &xcvr->afe_channel_control);
2033cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2034afd13a1fSJeff Skirvin 
20352e5da889SDan Williams 			writel(0x000003D4, &xcvr->afe_channel_control);
2036afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
20372e5da889SDan Williams 			writel(0x000001E7, &xcvr->afe_channel_control);
2038dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2039afd13a1fSJeff Skirvin 
20402e5da889SDan Williams 			writel(0x000001E4, &xcvr->afe_channel_control);
2041afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2042afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F7 : 0x000001F7,
2043afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2044afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2045afd13a1fSJeff Skirvin 
2046afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F4 : 0x000001F4,
2047afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2048cc9203bfSDan Williams 		}
2049cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2050cc9203bfSDan Williams 
2051dc00c8b6SDan Williams 		if (is_a2(pdev)) {
2052cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
20532e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2054cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2055cc9203bfSDan Williams 		}
2056cc9203bfSDan Williams 
2057afd13a1fSJeff Skirvin 		if (is_a2(pdev) || is_b0(pdev))
2058afd13a1fSJeff Skirvin 			/* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
2059afd13a1fSJeff Skirvin 			 * TPD=0x0(TX Power On), RDD=0x0(RX Detect
2060afd13a1fSJeff Skirvin 			 * Enabled) ....(0xe800)
2061afd13a1fSJeff Skirvin 			 */
20622e5da889SDan Williams 			writel(0x00004100, &xcvr->afe_xcvr_control0);
2063afd13a1fSJeff Skirvin 		else if (is_c0(pdev))
2064afd13a1fSJeff Skirvin 			writel(0x00014100, &xcvr->afe_xcvr_control0);
2065afd13a1fSJeff Skirvin 		else if (is_c1(pdev))
2066afd13a1fSJeff Skirvin 			writel(0x0001C100, &xcvr->afe_xcvr_control0);
2067cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2068cc9203bfSDan Williams 
2069cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2070dc00c8b6SDan Williams 		if (is_a2(pdev))
20712e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2072dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
20732e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2074cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2075cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
20762e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2077afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2078afd13a1fSJeff Skirvin 			writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
2079dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2080dbb0743aSAdam Gruchala 
20812e5da889SDan Williams 			writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
2082dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2083dbb0743aSAdam Gruchala 
2084dbb0743aSAdam Gruchala 			/* Enable TX equalization (0xe824) */
20852e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2086afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2087afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x01500C0C :
2088afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x01400C0D : 0x02400C0D,
2089afd13a1fSJeff Skirvin 			       &xcvr->afe_xcvr_control1);
2090afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2091afd13a1fSJeff Skirvin 
2092afd13a1fSJeff Skirvin 			writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
2093afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2094afd13a1fSJeff Skirvin 
2095afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x33091C1F :
2096afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x3315181F : 0x2B17161F,
2097afd13a1fSJeff Skirvin 			       &xcvr->afe_rx_ssc_control0);
2098afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2099afd13a1fSJeff Skirvin 
2100afd13a1fSJeff Skirvin 			/* Enable TX equalization (0xe824) */
2101afd13a1fSJeff Skirvin 			writel(0x00040000, &xcvr->afe_tx_control);
2102cc9203bfSDan Williams 		}
2103dbb0743aSAdam Gruchala 
2104cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2105cc9203bfSDan Williams 
21062e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
2107cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2108cc9203bfSDan Williams 
21092e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
2110cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2111cc9203bfSDan Williams 
21122e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
2113cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2114cc9203bfSDan Williams 
21152e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
2116cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2117cc9203bfSDan Williams 	}
2118cc9203bfSDan Williams 
2119cc9203bfSDan Williams 	/* Transfer control to the PEs */
21202e5da889SDan Williams 	writel(0x00010f00, &afe->afe_dfx_master_control0);
2121cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2122cc9203bfSDan Williams }
2123cc9203bfSDan Williams 
212489a7301fSDan Williams static void sci_controller_initialize_power_control(struct isci_host *ihost)
2125cc9203bfSDan Williams {
2126d9dcb4baSDan Williams 	sci_init_timer(&ihost->power_control.timer, power_control_timeout);
2127cc9203bfSDan Williams 
2128d9dcb4baSDan Williams 	memset(ihost->power_control.requesters, 0,
2129d9dcb4baSDan Williams 	       sizeof(ihost->power_control.requesters));
2130cc9203bfSDan Williams 
2131d9dcb4baSDan Williams 	ihost->power_control.phys_waiting = 0;
2132d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
2133cc9203bfSDan Williams }
2134cc9203bfSDan Williams 
213589a7301fSDan Williams static enum sci_status sci_controller_initialize(struct isci_host *ihost)
2136cc9203bfSDan Williams {
2137d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
21387c78da31SDan Williams 	enum sci_status result = SCI_FAILURE;
21397c78da31SDan Williams 	unsigned long i, state, val;
2140cc9203bfSDan Williams 
2141d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_RESET) {
214214e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
214314e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2144cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2145cc9203bfSDan Williams 	}
2146cc9203bfSDan Williams 
2147e301370aSEdmund Nadolski 	sci_change_state(sm, SCIC_INITIALIZING);
2148cc9203bfSDan Williams 
2149d9dcb4baSDan Williams 	sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
2150bb3dbdf6SEdmund Nadolski 
2151d9dcb4baSDan Williams 	ihost->next_phy_to_start = 0;
2152d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
2153cc9203bfSDan Williams 
215489a7301fSDan Williams 	sci_controller_initialize_power_control(ihost);
2155cc9203bfSDan Williams 
2156cc9203bfSDan Williams 	/*
2157cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2158cc9203bfSDan Williams 	 * program the AFE registers.
2159cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2160cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
216189a7301fSDan Williams 	sci_controller_afe_initialization(ihost);
2162cc9203bfSDan Williams 
2163cc9203bfSDan Williams 
2164cc9203bfSDan Williams 	/* Take the hardware out of reset */
2165d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->soft_reset_control);
2166cc9203bfSDan Williams 
2167cc9203bfSDan Williams 	/*
2168cc9203bfSDan Williams 	 * / @todo Provide meaningfull error code for hardware failure
2169cc9203bfSDan Williams 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
21707c78da31SDan Williams 	for (i = 100; i >= 1; i--) {
21717c78da31SDan Williams 		u32 status;
2172cc9203bfSDan Williams 
2173cc9203bfSDan Williams 		/* Loop until the hardware reports success */
2174cc9203bfSDan Williams 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2175d9dcb4baSDan Williams 		status = readl(&ihost->smu_registers->control_status);
2176cc9203bfSDan Williams 
21777c78da31SDan Williams 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
21787c78da31SDan Williams 			break;
2179cc9203bfSDan Williams 	}
21807c78da31SDan Williams 	if (i == 0)
21817c78da31SDan Williams 		goto out;
2182cc9203bfSDan Williams 
2183cc9203bfSDan Williams 	/*
2184cc9203bfSDan Williams 	 * Determine what are the actaul device capacities that the
2185cc9203bfSDan Williams 	 * hardware will support */
2186d9dcb4baSDan Williams 	val = readl(&ihost->smu_registers->device_context_capacity);
2187cc9203bfSDan Williams 
21887c78da31SDan Williams 	/* Record the smaller of the two capacity values */
2189d9dcb4baSDan Williams 	ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2190d9dcb4baSDan Williams 	ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2191d9dcb4baSDan Williams 	ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2192cc9203bfSDan Williams 
2193cc9203bfSDan Williams 	/*
2194cc9203bfSDan Williams 	 * Make all PEs that are unassigned match up with the
2195cc9203bfSDan Williams 	 * logical ports
2196cc9203bfSDan Williams 	 */
2197d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
2198cc9203bfSDan Williams 		struct scu_port_task_scheduler_group_registers __iomem
2199d9dcb4baSDan Williams 			*ptsg = &ihost->scu_registers->peg0.ptsg;
2200cc9203bfSDan Williams 
22017c78da31SDan Williams 		writel(i, &ptsg->protocol_engine[i]);
2202cc9203bfSDan Williams 	}
2203cc9203bfSDan Williams 
2204cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2205d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.pdma_configuration);
22067c78da31SDan Williams 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2207d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.pdma_configuration);
2208cc9203bfSDan Williams 
2209d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.cdma_configuration);
22107c78da31SDan Williams 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2211d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.cdma_configuration);
2212cc9203bfSDan Williams 
2213cc9203bfSDan Williams 	/*
2214cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2215cc9203bfSDan Williams 	 * are accessed during the port initialization.
2216cc9203bfSDan Williams 	 */
22177c78da31SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
221889a7301fSDan Williams 		result = sci_phy_initialize(&ihost->phys[i],
2219d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].tl,
2220d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].ll);
22217c78da31SDan Williams 		if (result != SCI_SUCCESS)
22227c78da31SDan Williams 			goto out;
2223cc9203bfSDan Williams 	}
2224cc9203bfSDan Williams 
2225d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
222689a7301fSDan Williams 		struct isci_port *iport = &ihost->ports[i];
22277c78da31SDan Williams 
222889a7301fSDan Williams 		iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
222989a7301fSDan Williams 		iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
223089a7301fSDan Williams 		iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
2231cc9203bfSDan Williams 	}
2232cc9203bfSDan Williams 
223389a7301fSDan Williams 	result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
2234cc9203bfSDan Williams 
22357c78da31SDan Williams  out:
2236cc9203bfSDan Williams 	/* Advance the controller state machine */
2237cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2238e301370aSEdmund Nadolski 		state = SCIC_INITIALIZED;
2239cc9203bfSDan Williams 	else
2240e301370aSEdmund Nadolski 		state = SCIC_FAILED;
2241e301370aSEdmund Nadolski 	sci_change_state(sm, state);
2242cc9203bfSDan Williams 
2243cc9203bfSDan Williams 	return result;
2244cc9203bfSDan Williams }
2245cc9203bfSDan Williams 
2246abec912dSDan Williams static int sci_controller_dma_alloc(struct isci_host *ihost)
2247cc9203bfSDan Williams {
2248abec912dSDan Williams 	struct device *dev = &ihost->pdev->dev;
2249abec912dSDan Williams 	size_t size;
2250abec912dSDan Williams 	int i;
2251cc9203bfSDan Williams 
2252abec912dSDan Williams 	/* detect re-initialization */
2253abec912dSDan Williams 	if (ihost->completion_queue)
2254abec912dSDan Williams 		return 0;
2255cc9203bfSDan Williams 
2256abec912dSDan Williams 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2257abec912dSDan Williams 	ihost->completion_queue = dmam_alloc_coherent(dev, size, &ihost->cq_dma,
2258abec912dSDan Williams 						      GFP_KERNEL);
2259abec912dSDan Williams 	if (!ihost->completion_queue)
2260abec912dSDan Williams 		return -ENOMEM;
2261cc9203bfSDan Williams 
2262abec912dSDan Williams 	size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2263abec912dSDan Williams 	ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &ihost->rnc_dma,
2264abec912dSDan Williams 							       GFP_KERNEL);
2265cc9203bfSDan Williams 
2266abec912dSDan Williams 	if (!ihost->remote_node_context_table)
2267abec912dSDan Williams 		return -ENOMEM;
2268cc9203bfSDan Williams 
2269abec912dSDan Williams 	size = ihost->task_context_entries * sizeof(struct scu_task_context),
2270abec912dSDan Williams 	ihost->task_context_table = dmam_alloc_coherent(dev, size, &ihost->tc_dma,
2271abec912dSDan Williams 							GFP_KERNEL);
2272abec912dSDan Williams 	if (!ihost->task_context_table)
2273abec912dSDan Williams 		return -ENOMEM;
2274cc9203bfSDan Williams 
2275abec912dSDan Williams 	size = SCI_UFI_TOTAL_SIZE;
2276abec912dSDan Williams 	ihost->ufi_buf = dmam_alloc_coherent(dev, size, &ihost->ufi_dma, GFP_KERNEL);
2277abec912dSDan Williams 	if (!ihost->ufi_buf)
2278abec912dSDan Williams 		return -ENOMEM;
2279abec912dSDan Williams 
2280abec912dSDan Williams 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2281abec912dSDan Williams 		struct isci_request *ireq;
2282abec912dSDan Williams 		dma_addr_t dma;
2283abec912dSDan Williams 
2284abec912dSDan Williams 		ireq = dmam_alloc_coherent(dev, sizeof(*ireq), &dma, GFP_KERNEL);
2285abec912dSDan Williams 		if (!ireq)
2286abec912dSDan Williams 			return -ENOMEM;
2287abec912dSDan Williams 
2288abec912dSDan Williams 		ireq->tc = &ihost->task_context_table[i];
2289abec912dSDan Williams 		ireq->owning_controller = ihost;
2290abec912dSDan Williams 		ireq->request_daddr = dma;
2291abec912dSDan Williams 		ireq->isci_host = ihost;
2292abec912dSDan Williams 		ihost->reqs[i] = ireq;
2293cc9203bfSDan Williams 	}
2294cc9203bfSDan Williams 
2295abec912dSDan Williams 	return 0;
2296cc9203bfSDan Williams }
2297cc9203bfSDan Williams 
229889a7301fSDan Williams static int sci_controller_mem_init(struct isci_host *ihost)
2299cc9203bfSDan Williams {
2300abec912dSDan Williams 	int err = sci_controller_dma_alloc(ihost);
2301cc9203bfSDan Williams 
23027c78da31SDan Williams 	if (err)
23037c78da31SDan Williams 		return err;
2304cc9203bfSDan Williams 
2305abec912dSDan Williams 	writel(lower_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_lower);
2306abec912dSDan Williams 	writel(upper_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_upper);
2307abec912dSDan Williams 
2308abec912dSDan Williams 	writel(lower_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_lower);
2309abec912dSDan Williams 	writel(upper_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_upper);
2310abec912dSDan Williams 
2311abec912dSDan Williams 	writel(lower_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_lower);
2312abec912dSDan Williams 	writel(upper_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_upper);
2313abec912dSDan Williams 
2314abec912dSDan Williams 	sci_unsolicited_frame_control_construct(ihost);
2315abec912dSDan Williams 
2316cc9203bfSDan Williams 	/*
2317cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2318cc9203bfSDan Williams 	 * address table.
2319cc9203bfSDan Williams 	 */
2320d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2321d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_lower);
2322d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2323d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_upper);
2324cc9203bfSDan Williams 
2325d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2326d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_lower);
2327d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2328d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_upper);
2329cc9203bfSDan Williams 
2330cc9203bfSDan Williams 	return 0;
2331cc9203bfSDan Williams }
2332cc9203bfSDan Williams 
2333abec912dSDan Williams /**
2334abec912dSDan Williams  * isci_host_init - (re-)initialize hardware and internal (private) state
2335abec912dSDan Williams  * @ihost: host to init
2336abec912dSDan Williams  *
2337abec912dSDan Williams  * Any public facing objects (like asd_sas_port, and asd_sas_phys), or
2338abec912dSDan Williams  * one-time initialization objects like locks and waitqueues, are
2339abec912dSDan Williams  * not touched (they are initialized in isci_host_alloc)
2340abec912dSDan Williams  */
2341d9dcb4baSDan Williams int isci_host_init(struct isci_host *ihost)
23426f231ddaSDan Williams {
2343abec912dSDan Williams 	int i, err;
23446f231ddaSDan Williams 	enum sci_status status;
23456f231ddaSDan Williams 
23462396a265SDan Williams 	spin_lock_irq(&ihost->scic_lock);
2347abec912dSDan Williams 	status = sci_controller_construct(ihost, scu_base(ihost), smu_base(ihost));
23482396a265SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
23496f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2350d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
235189a7301fSDan Williams 			"%s: sci_controller_construct failed - status = %x\n",
23526f231ddaSDan Williams 			__func__,
23536f231ddaSDan Williams 			status);
2354858d4aa7SDave Jiang 		return -ENODEV;
23556f231ddaSDan Williams 	}
23566f231ddaSDan Williams 
2357d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
235889a7301fSDan Williams 	status = sci_controller_initialize(ihost);
2359d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
23607c40a803SDan Williams 	if (status != SCI_SUCCESS) {
2361d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
236289a7301fSDan Williams 			 "%s: sci_controller_initialize failed -"
23637c40a803SDan Williams 			 " status = 0x%x\n",
23647c40a803SDan Williams 			 __func__, status);
23657c40a803SDan Williams 		return -ENODEV;
23667c40a803SDan Williams 	}
23677c40a803SDan Williams 
236889a7301fSDan Williams 	err = sci_controller_mem_init(ihost);
23696f231ddaSDan Williams 	if (err)
2370858d4aa7SDave Jiang 		return err;
23716f231ddaSDan Williams 
2372ad4f4c1dSDan Williams 	/* enable sgpio */
2373ad4f4c1dSDan Williams 	writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
2374ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
2375ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
2376ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
2377ad4f4c1dSDan Williams 
2378858d4aa7SDave Jiang 	return 0;
23796f231ddaSDan Williams }
2380cc9203bfSDan Williams 
238189a7301fSDan Williams void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
238289a7301fSDan Williams 			    struct isci_phy *iphy)
2383cc9203bfSDan Williams {
2384d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2385e301370aSEdmund Nadolski 	case SCIC_STARTING:
2386d9dcb4baSDan Williams 		sci_del_timer(&ihost->phy_timer);
2387d9dcb4baSDan Williams 		ihost->phy_startup_timer_pending = false;
2388d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2389ffe191c9SDan Williams 						  iport, iphy);
239089a7301fSDan Williams 		sci_controller_start_next_phy(ihost);
2391cc9203bfSDan Williams 		break;
2392e301370aSEdmund Nadolski 	case SCIC_READY:
2393d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2394ffe191c9SDan Williams 						  iport, iphy);
2395cc9203bfSDan Williams 		break;
2396cc9203bfSDan Williams 	default:
2397d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2398cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
239985280955SDan Williams 			"unexpected state %d\n", __func__, iphy->phy_index,
2400d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2401cc9203bfSDan Williams 	}
2402cc9203bfSDan Williams }
2403cc9203bfSDan Williams 
240489a7301fSDan Williams void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
240589a7301fSDan Williams 			      struct isci_phy *iphy)
2406cc9203bfSDan Williams {
2407d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2408e301370aSEdmund Nadolski 	case SCIC_STARTING:
2409e301370aSEdmund Nadolski 	case SCIC_READY:
2410d9dcb4baSDan Williams 		ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
2411ffe191c9SDan Williams 						   iport, iphy);
2412cc9203bfSDan Williams 		break;
2413cc9203bfSDan Williams 	default:
2414d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2415cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2416cc9203bfSDan Williams 			"unexpected state %d\n",
2417cc9203bfSDan Williams 			__func__,
241885280955SDan Williams 			iphy->phy_index,
2419d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2420cc9203bfSDan Williams 	}
2421cc9203bfSDan Williams }
2422cc9203bfSDan Williams 
2423eb608c3cSDan Williams bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
2424cc9203bfSDan Williams {
2425cc9203bfSDan Williams 	u32 index;
2426cc9203bfSDan Williams 
2427d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
2428d9dcb4baSDan Williams 		if ((ihost->device_table[index] != NULL) &&
2429d9dcb4baSDan Williams 		   (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2430cc9203bfSDan Williams 			return true;
2431cc9203bfSDan Williams 	}
2432cc9203bfSDan Williams 
2433cc9203bfSDan Williams 	return false;
2434cc9203bfSDan Williams }
2435cc9203bfSDan Williams 
243689a7301fSDan Williams void sci_controller_remote_device_stopped(struct isci_host *ihost,
243778a6f06eSDan Williams 					  struct isci_remote_device *idev)
2438cc9203bfSDan Williams {
2439d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_STOPPING) {
2440d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2441cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2442cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2443d9dcb4baSDan Williams 			ihost, idev,
2444d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2445cc9203bfSDan Williams 		return;
2446cc9203bfSDan Williams 	}
2447cc9203bfSDan Williams 
244889a7301fSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
2449eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
2450cc9203bfSDan Williams }
2451cc9203bfSDan Williams 
245289a7301fSDan Williams void sci_controller_post_request(struct isci_host *ihost, u32 request)
2453cc9203bfSDan Williams {
245489a7301fSDan Williams 	dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
245589a7301fSDan Williams 		__func__, ihost->id, request);
2456cc9203bfSDan Williams 
2457d9dcb4baSDan Williams 	writel(request, &ihost->smu_registers->post_context_port);
2458cc9203bfSDan Williams }
2459cc9203bfSDan Williams 
246089a7301fSDan Williams struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
2461cc9203bfSDan Williams {
2462cc9203bfSDan Williams 	u16 task_index;
2463cc9203bfSDan Williams 	u16 task_sequence;
2464cc9203bfSDan Williams 
2465dd047c8eSDan Williams 	task_index = ISCI_TAG_TCI(io_tag);
2466cc9203bfSDan Williams 
2467d9dcb4baSDan Williams 	if (task_index < ihost->task_context_entries) {
2468d9dcb4baSDan Williams 		struct isci_request *ireq = ihost->reqs[task_index];
2469db056250SDan Williams 
2470db056250SDan Williams 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2471dd047c8eSDan Williams 			task_sequence = ISCI_TAG_SEQ(io_tag);
2472cc9203bfSDan Williams 
2473d9dcb4baSDan Williams 			if (task_sequence == ihost->io_request_sequence[task_index])
24745076a1a9SDan Williams 				return ireq;
2475cc9203bfSDan Williams 		}
2476cc9203bfSDan Williams 	}
2477cc9203bfSDan Williams 
2478cc9203bfSDan Williams 	return NULL;
2479cc9203bfSDan Williams }
2480cc9203bfSDan Williams 
2481cc9203bfSDan Williams /**
2482cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2483cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2484cc9203bfSDan Williams  *    node index available.
2485cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2486cc9203bfSDan Williams  *    free remote node ids
2487cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2488cc9203bfSDan Williams  *    id
2489cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2490cc9203bfSDan Williams  *    is available
2491cc9203bfSDan Williams  *
2492cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2493cc9203bfSDan Williams  * node index available.
2494cc9203bfSDan Williams  */
249589a7301fSDan Williams enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
249678a6f06eSDan Williams 							    struct isci_remote_device *idev,
2497cc9203bfSDan Williams 							    u16 *node_id)
2498cc9203bfSDan Williams {
2499cc9203bfSDan Williams 	u16 node_index;
250089a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2501cc9203bfSDan Williams 
250289a7301fSDan Williams 	node_index = sci_remote_node_table_allocate_remote_node(
2503d9dcb4baSDan Williams 		&ihost->available_remote_nodes, remote_node_count
2504cc9203bfSDan Williams 		);
2505cc9203bfSDan Williams 
2506cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2507d9dcb4baSDan Williams 		ihost->device_table[node_index] = idev;
2508cc9203bfSDan Williams 
2509cc9203bfSDan Williams 		*node_id = node_index;
2510cc9203bfSDan Williams 
2511cc9203bfSDan Williams 		return SCI_SUCCESS;
2512cc9203bfSDan Williams 	}
2513cc9203bfSDan Williams 
2514cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2515cc9203bfSDan Williams }
2516cc9203bfSDan Williams 
251789a7301fSDan Williams void sci_controller_free_remote_node_context(struct isci_host *ihost,
251878a6f06eSDan Williams 					     struct isci_remote_device *idev,
2519cc9203bfSDan Williams 					     u16 node_id)
2520cc9203bfSDan Williams {
252189a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2522cc9203bfSDan Williams 
2523d9dcb4baSDan Williams 	if (ihost->device_table[node_id] == idev) {
2524d9dcb4baSDan Williams 		ihost->device_table[node_id] = NULL;
2525cc9203bfSDan Williams 
252689a7301fSDan Williams 		sci_remote_node_table_release_remote_node_index(
2527d9dcb4baSDan Williams 			&ihost->available_remote_nodes, remote_node_count, node_id
2528cc9203bfSDan Williams 			);
2529cc9203bfSDan Williams 	}
2530cc9203bfSDan Williams }
2531cc9203bfSDan Williams 
253289a7301fSDan Williams void sci_controller_copy_sata_response(void *response_buffer,
2533cc9203bfSDan Williams 				       void *frame_header,
2534cc9203bfSDan Williams 				       void *frame_buffer)
2535cc9203bfSDan Williams {
253689a7301fSDan Williams 	/* XXX type safety? */
2537cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2538cc9203bfSDan Williams 
2539cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2540cc9203bfSDan Williams 	       frame_buffer,
2541cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2542cc9203bfSDan Williams }
2543cc9203bfSDan Williams 
254489a7301fSDan Williams void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
2545cc9203bfSDan Williams {
254689a7301fSDan Williams 	if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
2547d9dcb4baSDan Williams 		writel(ihost->uf_control.get,
2548d9dcb4baSDan Williams 			&ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
2549cc9203bfSDan Williams }
2550cc9203bfSDan Williams 
2551312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci)
2552312e0c24SDan Williams {
2553312e0c24SDan Williams 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2554312e0c24SDan Williams 
2555312e0c24SDan Williams 	ihost->tci_pool[tail] = tci;
2556312e0c24SDan Williams 	ihost->tci_tail = tail + 1;
2557312e0c24SDan Williams }
2558312e0c24SDan Williams 
2559312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost)
2560312e0c24SDan Williams {
2561312e0c24SDan Williams 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2562312e0c24SDan Williams 	u16 tci = ihost->tci_pool[head];
2563312e0c24SDan Williams 
2564312e0c24SDan Williams 	ihost->tci_head = head + 1;
2565312e0c24SDan Williams 	return tci;
2566312e0c24SDan Williams }
2567312e0c24SDan Williams 
2568312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost)
2569312e0c24SDan Williams {
2570312e0c24SDan Williams 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2571312e0c24SDan Williams }
2572312e0c24SDan Williams 
2573312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost)
2574312e0c24SDan Williams {
2575312e0c24SDan Williams 	if (isci_tci_space(ihost)) {
2576312e0c24SDan Williams 		u16 tci = isci_tci_alloc(ihost);
2577d9dcb4baSDan Williams 		u8 seq = ihost->io_request_sequence[tci];
2578312e0c24SDan Williams 
2579312e0c24SDan Williams 		return ISCI_TAG(seq, tci);
2580312e0c24SDan Williams 	}
2581312e0c24SDan Williams 
2582312e0c24SDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
2583312e0c24SDan Williams }
2584312e0c24SDan Williams 
2585312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2586312e0c24SDan Williams {
2587312e0c24SDan Williams 	u16 tci = ISCI_TAG_TCI(io_tag);
2588312e0c24SDan Williams 	u16 seq = ISCI_TAG_SEQ(io_tag);
2589312e0c24SDan Williams 
2590312e0c24SDan Williams 	/* prevent tail from passing head */
2591312e0c24SDan Williams 	if (isci_tci_active(ihost) == 0)
2592312e0c24SDan Williams 		return SCI_FAILURE_INVALID_IO_TAG;
2593312e0c24SDan Williams 
2594d9dcb4baSDan Williams 	if (seq == ihost->io_request_sequence[tci]) {
2595d9dcb4baSDan Williams 		ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2596312e0c24SDan Williams 
2597312e0c24SDan Williams 		isci_tci_free(ihost, tci);
2598312e0c24SDan Williams 
2599312e0c24SDan Williams 		return SCI_SUCCESS;
2600312e0c24SDan Williams 	}
2601312e0c24SDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
2602312e0c24SDan Williams }
2603312e0c24SDan Williams 
260489a7301fSDan Williams enum sci_status sci_controller_start_io(struct isci_host *ihost,
260578a6f06eSDan Williams 					struct isci_remote_device *idev,
26065076a1a9SDan Williams 					struct isci_request *ireq)
2607cc9203bfSDan Williams {
2608cc9203bfSDan Williams 	enum sci_status status;
2609cc9203bfSDan Williams 
2610d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
261114e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
261214e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2613cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2614cc9203bfSDan Williams 	}
2615cc9203bfSDan Williams 
261689a7301fSDan Williams 	status = sci_remote_device_start_io(ihost, idev, ireq);
2617cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2618cc9203bfSDan Williams 		return status;
2619cc9203bfSDan Williams 
26205076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
262134a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2622cc9203bfSDan Williams 	return SCI_SUCCESS;
2623cc9203bfSDan Williams }
2624cc9203bfSDan Williams 
262589a7301fSDan Williams enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
262678a6f06eSDan Williams 						 struct isci_remote_device *idev,
26275076a1a9SDan Williams 						 struct isci_request *ireq)
2628cc9203bfSDan Williams {
262989a7301fSDan Williams 	/* terminate an ongoing (i.e. started) core IO request.  This does not
263089a7301fSDan Williams 	 * abort the IO request at the target, but rather removes the IO
263189a7301fSDan Williams 	 * request from the host controller.
263289a7301fSDan Williams 	 */
2633cc9203bfSDan Williams 	enum sci_status status;
2634cc9203bfSDan Williams 
2635d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
263614e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
263714e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2638cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2639cc9203bfSDan Williams 	}
264089a7301fSDan Williams 	status = sci_io_request_terminate(ireq);
264114aaa9f0SJeff Skirvin 
264214aaa9f0SJeff Skirvin 	dev_dbg(&ihost->pdev->dev, "%s: status=%d; ireq=%p; flags=%lx\n",
264314aaa9f0SJeff Skirvin 		__func__, status, ireq, ireq->flags);
264414aaa9f0SJeff Skirvin 
2645726980d5SJeff Skirvin 	if ((status == SCI_SUCCESS) &&
2646726980d5SJeff Skirvin 	    !test_bit(IREQ_PENDING_ABORT, &ireq->flags) &&
2647726980d5SJeff Skirvin 	    !test_and_set_bit(IREQ_TC_ABORT_POSTED, &ireq->flags)) {
2648726980d5SJeff Skirvin 		/* Utilize the original post context command and or in the
2649726980d5SJeff Skirvin 		 * POST_TC_ABORT request sub-type.
2650cc9203bfSDan Williams 		 */
2651726980d5SJeff Skirvin 		sci_controller_post_request(
2652726980d5SJeff Skirvin 			ihost, ireq->post_context |
2653726980d5SJeff Skirvin 				SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2654726980d5SJeff Skirvin 	}
2655726980d5SJeff Skirvin 	return status;
2656cc9203bfSDan Williams }
2657cc9203bfSDan Williams 
2658cc9203bfSDan Williams /**
265989a7301fSDan Williams  * sci_controller_complete_io() - This method will perform core specific
2660cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
2661cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
2662cc9203bfSDan Williams  *    reused (i.e. re-constructed).
266389a7301fSDan Williams  * @ihost: The handle to the controller object for which to complete the
2664cc9203bfSDan Williams  *    IO request.
266589a7301fSDan Williams  * @idev: The handle to the remote device object for which to complete
2666cc9203bfSDan Williams  *    the IO request.
266789a7301fSDan Williams  * @ireq: the handle to the io request object to complete.
2668cc9203bfSDan Williams  */
266989a7301fSDan Williams enum sci_status sci_controller_complete_io(struct isci_host *ihost,
267078a6f06eSDan Williams 					   struct isci_remote_device *idev,
26715076a1a9SDan Williams 					   struct isci_request *ireq)
2672cc9203bfSDan Williams {
2673cc9203bfSDan Williams 	enum sci_status status;
2674cc9203bfSDan Williams 	u16 index;
2675cc9203bfSDan Williams 
2676d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2677e301370aSEdmund Nadolski 	case SCIC_STOPPING:
2678cc9203bfSDan Williams 		/* XXX: Implement this function */
2679cc9203bfSDan Williams 		return SCI_FAILURE;
2680e301370aSEdmund Nadolski 	case SCIC_READY:
268189a7301fSDan Williams 		status = sci_remote_device_complete_io(ihost, idev, ireq);
2682cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
2683cc9203bfSDan Williams 			return status;
2684cc9203bfSDan Williams 
26855076a1a9SDan Williams 		index = ISCI_TAG_TCI(ireq->io_tag);
26865076a1a9SDan Williams 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2687cc9203bfSDan Williams 		return SCI_SUCCESS;
2688cc9203bfSDan Williams 	default:
268914e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
269014e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2691cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2692cc9203bfSDan Williams 	}
2693cc9203bfSDan Williams 
2694cc9203bfSDan Williams }
2695cc9203bfSDan Williams 
269689a7301fSDan Williams enum sci_status sci_controller_continue_io(struct isci_request *ireq)
2697cc9203bfSDan Williams {
2698d9dcb4baSDan Williams 	struct isci_host *ihost = ireq->owning_controller;
2699cc9203bfSDan Williams 
2700d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
270114e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
270214e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2703cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2704cc9203bfSDan Williams 	}
2705cc9203bfSDan Williams 
27065076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
270734a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2708cc9203bfSDan Williams 	return SCI_SUCCESS;
2709cc9203bfSDan Williams }
2710cc9203bfSDan Williams 
2711cc9203bfSDan Williams /**
271289a7301fSDan Williams  * sci_controller_start_task() - This method is called by the SCIC user to
2713cc9203bfSDan Williams  *    send/start a framework task management request.
2714cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
2715cc9203bfSDan Williams  *    management request.
2716cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
2717cc9203bfSDan Williams  *    the task management request.
2718cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
2719cc9203bfSDan Williams  */
272089a7301fSDan Williams enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
272178a6f06eSDan Williams 					       struct isci_remote_device *idev,
27225076a1a9SDan Williams 					       struct isci_request *ireq)
2723cc9203bfSDan Williams {
2724cc9203bfSDan Williams 	enum sci_status status;
2725cc9203bfSDan Williams 
2726d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2727d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2728cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
2729cc9203bfSDan Williams 			 "state\n",
2730cc9203bfSDan Williams 			 __func__);
2731cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
2732cc9203bfSDan Williams 	}
2733cc9203bfSDan Williams 
273489a7301fSDan Williams 	status = sci_remote_device_start_task(ihost, idev, ireq);
2735cc9203bfSDan Williams 	switch (status) {
2736cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2737db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2738cc9203bfSDan Williams 
2739cc9203bfSDan Williams 		/*
2740cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
2741cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
2742cc9203bfSDan Williams 		 * RNC is resumed.)
2743cc9203bfSDan Williams 		 */
2744cc9203bfSDan Williams 		return SCI_SUCCESS;
2745cc9203bfSDan Williams 	case SCI_SUCCESS:
2746db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
274734a99158SDan Williams 		sci_controller_post_request(ihost, ireq->post_context);
2748cc9203bfSDan Williams 		break;
2749cc9203bfSDan Williams 	default:
2750cc9203bfSDan Williams 		break;
2751cc9203bfSDan Williams 	}
2752cc9203bfSDan Williams 
2753cc9203bfSDan Williams 	return status;
2754cc9203bfSDan Williams }
2755ad4f4c1dSDan Williams 
2756ad4f4c1dSDan Williams static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
2757ad4f4c1dSDan Williams {
2758ad4f4c1dSDan Williams 	int d;
2759ad4f4c1dSDan Williams 
2760ad4f4c1dSDan Williams 	/* no support for TX_GP_CFG */
2761ad4f4c1dSDan Williams 	if (reg_index == 0)
2762ad4f4c1dSDan Williams 		return -EINVAL;
2763ad4f4c1dSDan Williams 
2764ad4f4c1dSDan Williams 	for (d = 0; d < isci_gpio_count(ihost); d++) {
2765ad4f4c1dSDan Williams 		u32 val = 0x444; /* all ODx.n clear */
2766ad4f4c1dSDan Williams 		int i;
2767ad4f4c1dSDan Williams 
2768ad4f4c1dSDan Williams 		for (i = 0; i < 3; i++) {
2769ad4f4c1dSDan Williams 			int bit = (i << 2) + 2;
2770ad4f4c1dSDan Williams 
2771ad4f4c1dSDan Williams 			bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
2772ad4f4c1dSDan Williams 						       write_data, reg_index,
2773ad4f4c1dSDan Williams 						       reg_count);
2774ad4f4c1dSDan Williams 			if (bit < 0)
2775ad4f4c1dSDan Williams 				break;
2776ad4f4c1dSDan Williams 
2777ad4f4c1dSDan Williams 			/* if od is set, clear the 'invert' bit */
2778ad4f4c1dSDan Williams 			val &= ~(bit << ((i << 2) + 2));
2779ad4f4c1dSDan Williams 		}
2780ad4f4c1dSDan Williams 
2781ad4f4c1dSDan Williams 		if (i < 3)
2782ad4f4c1dSDan Williams 			break;
2783ad4f4c1dSDan Williams 		writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
2784ad4f4c1dSDan Williams 	}
2785ad4f4c1dSDan Williams 
2786ad4f4c1dSDan Williams 	/* unless reg_index is > 1, we should always be able to write at
2787ad4f4c1dSDan Williams 	 * least one register
2788ad4f4c1dSDan Williams 	 */
2789ad4f4c1dSDan Williams 	return d > 0;
2790ad4f4c1dSDan Williams }
2791ad4f4c1dSDan Williams 
2792ad4f4c1dSDan Williams int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
2793ad4f4c1dSDan Williams 		    u8 reg_count, u8 *write_data)
2794ad4f4c1dSDan Williams {
2795ad4f4c1dSDan Williams 	struct isci_host *ihost = sas_ha->lldd_ha;
2796ad4f4c1dSDan Williams 	int written;
2797ad4f4c1dSDan Williams 
2798ad4f4c1dSDan Williams 	switch (reg_type) {
2799ad4f4c1dSDan Williams 	case SAS_GPIO_REG_TX_GP:
2800ad4f4c1dSDan Williams 		written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
2801ad4f4c1dSDan Williams 		break;
2802ad4f4c1dSDan Williams 	default:
2803ad4f4c1dSDan Williams 		written = -EINVAL;
2804ad4f4c1dSDan Williams 	}
2805ad4f4c1dSDan Williams 
2806ad4f4c1dSDan Williams 	return written;
2807ad4f4c1dSDan Williams }
2808