xref: /openbmc/linux/drivers/scsi/isci/host.c (revision 54b46677757ff8d6c282305fc7710f466b63d6dc)
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);
1089*54b46677SJeff 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);
1095*54b46677SJeff Skirvin 			if (sas_protocol_ata(task->task_proto))
1096*54b46677SJeff Skirvin 				task->lldd_task = NULL;
1097f8381807SJeff Skirvin 			sas_task_abort(task);
1098f8381807SJeff Skirvin 		}
1099*54b46677SJeff Skirvin 	} else
1100*54b46677SJeff Skirvin 		task->lldd_task = NULL;
1101*54b46677SJeff 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 
11259b4be528SDan Williams 	/* the coalesence timeout doubles at each encoding step, so
11269b4be528SDan Williams 	 * update it based on the ilog2 value of the outstanding requests
11279b4be528SDan Williams 	 */
11289b4be528SDan Williams 	active = isci_tci_active(ihost);
11299b4be528SDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, active) |
11309b4be528SDan Williams 	       SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
11319b4be528SDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
11326f231ddaSDan Williams }
11336f231ddaSDan Williams 
1134cc9203bfSDan Williams /**
113589a7301fSDan Williams  * sci_controller_stop() - This method will stop an individual controller
1136cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1137cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1138cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1139cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1140cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1141cc9203bfSDan Williams  *    the hardware is halted.
1142cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1143cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1144cc9203bfSDan Williams  *    stop operation should complete.
1145cc9203bfSDan Williams  *
1146cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1147cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1148cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1149cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1150cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1151cc9203bfSDan Williams  */
115289a7301fSDan Williams static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
1153cc9203bfSDan Williams {
1154d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
115514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
115614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1157cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1158cc9203bfSDan Williams 	}
1159cc9203bfSDan Williams 
1160d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1161d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STOPPING);
1162cc9203bfSDan Williams 	return SCI_SUCCESS;
1163cc9203bfSDan Williams }
1164cc9203bfSDan Williams 
1165cc9203bfSDan Williams /**
116689a7301fSDan Williams  * sci_controller_reset() - This method will reset the supplied core
1167cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1168cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1169cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1170cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1171cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1172cc9203bfSDan Williams  *
1173cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1174cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1175cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1176cc9203bfSDan Williams  */
117789a7301fSDan Williams static enum sci_status sci_controller_reset(struct isci_host *ihost)
1178cc9203bfSDan Williams {
1179d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
1180e301370aSEdmund Nadolski 	case SCIC_RESET:
1181e301370aSEdmund Nadolski 	case SCIC_READY:
1182eb608c3cSDan Williams 	case SCIC_STOPPING:
1183e301370aSEdmund Nadolski 	case SCIC_FAILED:
1184cc9203bfSDan Williams 		/*
1185cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1186cc9203bfSDan Williams 		 * perform the state transition.
1187cc9203bfSDan Williams 		 */
1188d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_RESETTING);
1189cc9203bfSDan Williams 		return SCI_SUCCESS;
1190cc9203bfSDan Williams 	default:
119114e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
119214e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1193cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1194cc9203bfSDan Williams 	}
1195cc9203bfSDan Williams }
1196cc9203bfSDan Williams 
1197eb608c3cSDan Williams static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
1198eb608c3cSDan Williams {
1199eb608c3cSDan Williams 	u32 index;
1200eb608c3cSDan Williams 	enum sci_status status;
1201eb608c3cSDan Williams 	enum sci_status phy_status;
1202eb608c3cSDan Williams 
1203eb608c3cSDan Williams 	status = SCI_SUCCESS;
1204eb608c3cSDan Williams 
1205eb608c3cSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1206eb608c3cSDan Williams 		phy_status = sci_phy_stop(&ihost->phys[index]);
1207eb608c3cSDan Williams 
1208eb608c3cSDan Williams 		if (phy_status != SCI_SUCCESS &&
1209eb608c3cSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1210eb608c3cSDan Williams 			status = SCI_FAILURE;
1211eb608c3cSDan Williams 
1212eb608c3cSDan Williams 			dev_warn(&ihost->pdev->dev,
1213eb608c3cSDan Williams 				 "%s: Controller stop operation failed to stop "
1214eb608c3cSDan Williams 				 "phy %d because of status %d.\n",
1215eb608c3cSDan Williams 				 __func__,
1216eb608c3cSDan Williams 				 ihost->phys[index].phy_index, phy_status);
1217eb608c3cSDan Williams 		}
1218eb608c3cSDan Williams 	}
1219eb608c3cSDan Williams 
1220eb608c3cSDan Williams 	return status;
1221eb608c3cSDan Williams }
1222eb608c3cSDan Williams 
1223eb608c3cSDan Williams 
1224eb608c3cSDan Williams /**
1225eb608c3cSDan Williams  * isci_host_deinit - shutdown frame reception and dma
1226eb608c3cSDan Williams  * @ihost: host to take down
1227eb608c3cSDan Williams  *
1228eb608c3cSDan Williams  * This is called in either the driver shutdown or the suspend path.  In
1229eb608c3cSDan Williams  * the shutdown case libsas went through port teardown and normal device
1230eb608c3cSDan Williams  * removal (i.e. physical links stayed up to service scsi_device removal
1231eb608c3cSDan Williams  * commands).  In the suspend case we disable the hardware without
1232eb608c3cSDan Williams  * notifying libsas of the link down events since we want libsas to
1233eb608c3cSDan Williams  * remember the domain across the suspend/resume cycle
1234eb608c3cSDan Williams  */
12350cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
12366f231ddaSDan Williams {
12376f231ddaSDan Williams 	int i;
12386f231ddaSDan Williams 
1239ad4f4c1dSDan Williams 	/* disable output data selects */
1240ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
1241ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
1242ad4f4c1dSDan Williams 
12430cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
12447c40a803SDan Williams 
12457c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
124689a7301fSDan Williams 	sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
12477c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
12487c40a803SDan Williams 
12490cf89d1dSDan Williams 	wait_for_stop(ihost);
1250ad4f4c1dSDan Williams 
1251eb608c3cSDan Williams 	/* phy stop is after controller stop to allow port and device to
1252eb608c3cSDan Williams 	 * go idle before shutting down the phys, but the expectation is
1253eb608c3cSDan Williams 	 * that i/o has been shut off well before we reach this
1254eb608c3cSDan Williams 	 * function.
1255eb608c3cSDan Williams 	 */
1256eb608c3cSDan Williams 	sci_controller_stop_phys(ihost);
1257eb608c3cSDan Williams 
1258ad4f4c1dSDan Williams 	/* disable sgpio: where the above wait should give time for the
1259ad4f4c1dSDan Williams 	 * enclosure to sample the gpios going inactive
1260ad4f4c1dSDan Williams 	 */
1261ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
1262ad4f4c1dSDan Williams 
12632396a265SDan Williams 	spin_lock_irq(&ihost->scic_lock);
126489a7301fSDan Williams 	sci_controller_reset(ihost);
12652396a265SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
12665553ba2bSEdmund Nadolski 
12675553ba2bSEdmund Nadolski 	/* Cancel any/all outstanding port timers */
1268d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
1269ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[i];
1270ffe191c9SDan Williams 		del_timer_sync(&iport->timer.timer);
12715553ba2bSEdmund Nadolski 	}
12725553ba2bSEdmund Nadolski 
1273a628d478SEdmund Nadolski 	/* Cancel any/all outstanding phy timers */
1274a628d478SEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
127585280955SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
127685280955SDan Williams 		del_timer_sync(&iphy->sata_timer.timer);
1277a628d478SEdmund Nadolski 	}
1278a628d478SEdmund Nadolski 
1279d9dcb4baSDan Williams 	del_timer_sync(&ihost->port_agent.timer.timer);
1280ac0eeb4fSEdmund Nadolski 
1281d9dcb4baSDan Williams 	del_timer_sync(&ihost->power_control.timer.timer);
12820473661aSEdmund Nadolski 
1283d9dcb4baSDan Williams 	del_timer_sync(&ihost->timer.timer);
12846cb5853dSEdmund Nadolski 
1285d9dcb4baSDan Williams 	del_timer_sync(&ihost->phy_timer.timer);
12866f231ddaSDan Williams }
12876f231ddaSDan Williams 
12886f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
12896f231ddaSDan Williams {
12906f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
12916f231ddaSDan Williams 	int id = isci_host->id;
12926f231ddaSDan Williams 
12936f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
12946f231ddaSDan Williams }
12956f231ddaSDan Williams 
12966f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
12976f231ddaSDan Williams {
12986f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
12996f231ddaSDan Williams 	int id = isci_host->id;
13006f231ddaSDan Williams 
13016f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13026f231ddaSDan Williams }
13036f231ddaSDan Williams 
130489a7301fSDan Williams static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
1305cc9203bfSDan Williams {
1306d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1307cc9203bfSDan Williams 
1308d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1309cc9203bfSDan Williams }
1310cc9203bfSDan Williams 
131189a7301fSDan Williams static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
1312cc9203bfSDan Williams {
1313d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1314cc9203bfSDan Williams 
1315d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1316cc9203bfSDan Williams }
1317cc9203bfSDan Williams 
1318cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1319cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1320cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1321cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1322cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1323cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1324cc9203bfSDan Williams 
1325cc9203bfSDan Williams /**
132689a7301fSDan Williams  * sci_controller_set_interrupt_coalescence() - This method allows the user to
1327cc9203bfSDan Williams  *    configure the interrupt coalescence.
1328cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1329cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1330cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1331cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1332cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1333cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1334cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1335cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1336cc9203bfSDan Williams  *    interrupt coalescing timeout.
1337cc9203bfSDan Williams  *
1338cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1339cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1340cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1341cc9203bfSDan Williams  */
1342d9dcb4baSDan Williams static enum sci_status
134389a7301fSDan Williams sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1344cc9203bfSDan Williams 					 u32 coalesce_number,
1345cc9203bfSDan Williams 					 u32 coalesce_timeout)
1346cc9203bfSDan Williams {
1347cc9203bfSDan Williams 	u8 timeout_encode = 0;
1348cc9203bfSDan Williams 	u32 min = 0;
1349cc9203bfSDan Williams 	u32 max = 0;
1350cc9203bfSDan Williams 
1351cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1352cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1353cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1354cc9203bfSDan Williams 
1355cc9203bfSDan Williams 	/*
1356cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1357cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1358cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1359cc9203bfSDan Williams 	 *              0       -        -       Disabled
1360cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1361cc9203bfSDan Williams 	 *              2       26.7     40.0
1362cc9203bfSDan Williams 	 *              3       53.3     80.0
1363cc9203bfSDan Williams 	 *              4       106.7    160.0
1364cc9203bfSDan Williams 	 *              5       213.3    320.0
1365cc9203bfSDan Williams 	 *              6       426.7    640.0
1366cc9203bfSDan Williams 	 *              7       853.3    1280.0
1367cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1368cc9203bfSDan Williams 	 *              9       3.4      5.1
1369cc9203bfSDan Williams 	 *              10      6.8      10.2
1370cc9203bfSDan Williams 	 *              11      13.7     20.5
1371cc9203bfSDan Williams 	 *              12      27.3     41.0
1372cc9203bfSDan Williams 	 *              13      54.6     81.9
1373cc9203bfSDan Williams 	 *              14      109.2    163.8
1374cc9203bfSDan Williams 	 *              15      218.5    327.7
1375cc9203bfSDan Williams 	 *              16      436.9    655.4
1376cc9203bfSDan Williams 	 *              17      873.8    1310.7
1377cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1378cc9203bfSDan Williams 	 *              19      3.5      5.2
1379cc9203bfSDan Williams 	 *              20      7.0      10.5
1380cc9203bfSDan Williams 	 *              21      14.0     21.0
1381cc9203bfSDan Williams 	 *              22      28.0     41.9
1382cc9203bfSDan Williams 	 *              23      55.9     83.9
1383cc9203bfSDan Williams 	 *              24      111.8    167.8
1384cc9203bfSDan Williams 	 *              25      223.7    335.5
1385cc9203bfSDan Williams 	 *              26      447.4    671.1
1386cc9203bfSDan Williams 	 *              27      894.8    1342.2
1387cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1388cc9203bfSDan Williams 	 *              Others Undefined */
1389cc9203bfSDan Williams 
1390cc9203bfSDan Williams 	/*
1391cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1392cc9203bfSDan Williams 	 * value for register writing. */
1393cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1394cc9203bfSDan Williams 		timeout_encode = 0;
1395cc9203bfSDan Williams 	else{
1396cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1397cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1398cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1399cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1400cc9203bfSDan Williams 
1401cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1402cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1403cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1404cc9203bfSDan Williams 		      timeout_encode++) {
1405cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1406cc9203bfSDan Williams 				break;
1407cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1408cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1409cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1410cc9203bfSDan Williams 					break;
1411cc9203bfSDan Williams 				else{
1412cc9203bfSDan Williams 					timeout_encode++;
1413cc9203bfSDan Williams 					break;
1414cc9203bfSDan Williams 				}
1415cc9203bfSDan Williams 			} else {
1416cc9203bfSDan Williams 				max = max * 2;
1417cc9203bfSDan Williams 				min = min * 2;
1418cc9203bfSDan Williams 			}
1419cc9203bfSDan Williams 		}
1420cc9203bfSDan Williams 
1421cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1422cc9203bfSDan Williams 			/* the value is out of range. */
1423cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1424cc9203bfSDan Williams 	}
1425cc9203bfSDan Williams 
1426cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1427cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1428d9dcb4baSDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
1429cc9203bfSDan Williams 
1430cc9203bfSDan Williams 
1431d9dcb4baSDan Williams 	ihost->interrupt_coalesce_number = (u16)coalesce_number;
1432d9dcb4baSDan Williams 	ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
1433cc9203bfSDan Williams 
1434cc9203bfSDan Williams 	return SCI_SUCCESS;
1435cc9203bfSDan Williams }
1436cc9203bfSDan Williams 
1437cc9203bfSDan Williams 
143889a7301fSDan Williams static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
1439cc9203bfSDan Williams {
1440d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1441e5cc6aa4SMarcin Tomczak 	u32 val;
1442e5cc6aa4SMarcin Tomczak 
1443e5cc6aa4SMarcin Tomczak 	/* enable clock gating for power control of the scu unit */
1444e5cc6aa4SMarcin Tomczak 	val = readl(&ihost->smu_registers->clock_gating_control);
1445e5cc6aa4SMarcin Tomczak 	val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
1446e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
1447e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
1448e5cc6aa4SMarcin Tomczak 	val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
1449e5cc6aa4SMarcin Tomczak 	writel(val, &ihost->smu_registers->clock_gating_control);
1450cc9203bfSDan Williams 
1451cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
14529b4be528SDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1453cc9203bfSDan Williams }
1454cc9203bfSDan Williams 
145589a7301fSDan Williams static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
1456cc9203bfSDan Williams {
1457d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1458cc9203bfSDan Williams 
1459cc9203bfSDan Williams 	/* disable interrupt coalescence. */
146089a7301fSDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1461cc9203bfSDan Williams }
1462cc9203bfSDan Williams 
146389a7301fSDan Williams static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
1464cc9203bfSDan Williams {
1465cc9203bfSDan Williams 	u32 index;
1466cc9203bfSDan Williams 	enum sci_status port_status;
1467cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1468cc9203bfSDan Williams 
1469d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1470ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1471cc9203bfSDan Williams 
147289a7301fSDan Williams 		port_status = sci_port_stop(iport);
1473cc9203bfSDan Williams 
1474cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1475cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1476cc9203bfSDan Williams 			status = SCI_FAILURE;
1477cc9203bfSDan Williams 
1478d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1479cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1480cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1481cc9203bfSDan Williams 				 __func__,
1482ffe191c9SDan Williams 				 iport->logical_port_index,
1483cc9203bfSDan Williams 				 port_status);
1484cc9203bfSDan Williams 		}
1485cc9203bfSDan Williams 	}
1486cc9203bfSDan Williams 
1487cc9203bfSDan Williams 	return status;
1488cc9203bfSDan Williams }
1489cc9203bfSDan Williams 
149089a7301fSDan Williams static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
1491cc9203bfSDan Williams {
1492cc9203bfSDan Williams 	u32 index;
1493cc9203bfSDan Williams 	enum sci_status status;
1494cc9203bfSDan Williams 	enum sci_status device_status;
1495cc9203bfSDan Williams 
1496cc9203bfSDan Williams 	status = SCI_SUCCESS;
1497cc9203bfSDan Williams 
1498d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
1499d9dcb4baSDan Williams 		if (ihost->device_table[index] != NULL) {
1500cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
150189a7301fSDan Williams 			device_status = sci_remote_device_stop(ihost->device_table[index], 0);
1502cc9203bfSDan Williams 
1503cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1504cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1505d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
1506cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1507cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1508cc9203bfSDan Williams 					 "status %d.\n",
1509cc9203bfSDan Williams 					 __func__,
1510d9dcb4baSDan Williams 					 ihost->device_table[index], device_status);
1511cc9203bfSDan Williams 			}
1512cc9203bfSDan Williams 		}
1513cc9203bfSDan Williams 	}
1514cc9203bfSDan Williams 
1515cc9203bfSDan Williams 	return status;
1516cc9203bfSDan Williams }
1517cc9203bfSDan Williams 
151889a7301fSDan Williams static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1519cc9203bfSDan Williams {
1520d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1521cc9203bfSDan Williams 
152289a7301fSDan Williams 	sci_controller_stop_devices(ihost);
1523eb608c3cSDan Williams 	sci_controller_stop_ports(ihost);
1524eb608c3cSDan Williams 
1525eb608c3cSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
1526eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
1527cc9203bfSDan Williams }
1528cc9203bfSDan Williams 
152989a7301fSDan Williams static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1530cc9203bfSDan Williams {
1531d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1532cc9203bfSDan Williams 
1533d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1534cc9203bfSDan Williams }
1535cc9203bfSDan Williams 
153689a7301fSDan Williams static void sci_controller_reset_hardware(struct isci_host *ihost)
1537cc9203bfSDan Williams {
1538cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
153989a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1540cc9203bfSDan Williams 
1541cc9203bfSDan Williams 	/* Reset the SCU */
1542d9dcb4baSDan Williams 	writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
1543cc9203bfSDan Williams 
1544cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1545cc9203bfSDan Williams 	udelay(1000);
1546cc9203bfSDan Williams 
1547cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1548d9dcb4baSDan Williams 	writel(0x00000000, &ihost->smu_registers->completion_queue_get);
1549cc9203bfSDan Williams 
1550cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1551d9dcb4baSDan Williams 	writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
15522396a265SDan Williams 
15532396a265SDan Williams 	/* clear all interrupts */
15542396a265SDan Williams 	writel(~SMU_INTERRUPT_STATUS_RESERVED_MASK, &ihost->smu_registers->interrupt_status);
1555cc9203bfSDan Williams }
1556cc9203bfSDan Williams 
155789a7301fSDan Williams static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1558cc9203bfSDan Williams {
1559d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1560cc9203bfSDan Williams 
156189a7301fSDan Williams 	sci_controller_reset_hardware(ihost);
1562d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1563cc9203bfSDan Williams }
1564cc9203bfSDan Williams 
156589a7301fSDan Williams static const struct sci_base_state sci_controller_state_table[] = {
1566e301370aSEdmund Nadolski 	[SCIC_INITIAL] = {
156789a7301fSDan Williams 		.enter_state = sci_controller_initial_state_enter,
1568cc9203bfSDan Williams 	},
1569e301370aSEdmund Nadolski 	[SCIC_RESET] = {},
1570e301370aSEdmund Nadolski 	[SCIC_INITIALIZING] = {},
1571e301370aSEdmund Nadolski 	[SCIC_INITIALIZED] = {},
1572e301370aSEdmund Nadolski 	[SCIC_STARTING] = {
157389a7301fSDan Williams 		.exit_state  = sci_controller_starting_state_exit,
1574cc9203bfSDan Williams 	},
1575e301370aSEdmund Nadolski 	[SCIC_READY] = {
157689a7301fSDan Williams 		.enter_state = sci_controller_ready_state_enter,
157789a7301fSDan Williams 		.exit_state  = sci_controller_ready_state_exit,
1578cc9203bfSDan Williams 	},
1579e301370aSEdmund Nadolski 	[SCIC_RESETTING] = {
158089a7301fSDan Williams 		.enter_state = sci_controller_resetting_state_enter,
1581cc9203bfSDan Williams 	},
1582e301370aSEdmund Nadolski 	[SCIC_STOPPING] = {
158389a7301fSDan Williams 		.enter_state = sci_controller_stopping_state_enter,
158489a7301fSDan Williams 		.exit_state = sci_controller_stopping_state_exit,
1585cc9203bfSDan Williams 	},
1586e301370aSEdmund Nadolski 	[SCIC_FAILED] = {}
1587cc9203bfSDan Williams };
1588cc9203bfSDan Williams 
15896cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data)
15906cb5853dSEdmund Nadolski {
15916cb5853dSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1592d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1593d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
15946cb5853dSEdmund Nadolski 	unsigned long flags;
1595cc9203bfSDan Williams 
15966cb5853dSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
15976cb5853dSEdmund Nadolski 
15986cb5853dSEdmund Nadolski 	if (tmr->cancel)
15996cb5853dSEdmund Nadolski 		goto done;
16006cb5853dSEdmund Nadolski 
1601e301370aSEdmund Nadolski 	if (sm->current_state_id == SCIC_STARTING)
160289a7301fSDan Williams 		sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
1603e301370aSEdmund Nadolski 	else if (sm->current_state_id == SCIC_STOPPING) {
1604e301370aSEdmund Nadolski 		sci_change_state(sm, SCIC_FAILED);
1605eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
16066cb5853dSEdmund Nadolski 	} else	/* / @todo Now what do we want to do in this case? */
1607d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
16086cb5853dSEdmund Nadolski 			"%s: Controller timer fired when controller was not "
16096cb5853dSEdmund Nadolski 			"in a state being timed.\n",
16106cb5853dSEdmund Nadolski 			__func__);
16116cb5853dSEdmund Nadolski 
16126cb5853dSEdmund Nadolski done:
16136cb5853dSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
16146cb5853dSEdmund Nadolski }
1615cc9203bfSDan Williams 
161689a7301fSDan Williams static enum sci_status sci_controller_construct(struct isci_host *ihost,
1617cc9203bfSDan Williams 						void __iomem *scu_base,
1618cc9203bfSDan Williams 						void __iomem *smu_base)
1619cc9203bfSDan Williams {
1620cc9203bfSDan Williams 	u8 i;
1621cc9203bfSDan Williams 
162289a7301fSDan Williams 	sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
1623cc9203bfSDan Williams 
1624d9dcb4baSDan Williams 	ihost->scu_registers = scu_base;
1625d9dcb4baSDan Williams 	ihost->smu_registers = smu_base;
1626cc9203bfSDan Williams 
162789a7301fSDan Williams 	sci_port_configuration_agent_construct(&ihost->port_agent);
1628cc9203bfSDan Williams 
1629cc9203bfSDan Williams 	/* Construct the ports for this controller */
1630cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
163189a7301fSDan Williams 		sci_port_construct(&ihost->ports[i], i, ihost);
163289a7301fSDan Williams 	sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
1633cc9203bfSDan Williams 
1634cc9203bfSDan Williams 	/* Construct the phys for this controller */
1635cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1636cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
163789a7301fSDan Williams 		sci_phy_construct(&ihost->phys[i],
1638ffe191c9SDan Williams 				  &ihost->ports[SCI_MAX_PORTS], i);
1639cc9203bfSDan Williams 	}
1640cc9203bfSDan Williams 
1641d9dcb4baSDan Williams 	ihost->invalid_phy_mask = 0;
1642cc9203bfSDan Williams 
1643d9dcb4baSDan Williams 	sci_init_timer(&ihost->timer, controller_timeout);
16446cb5853dSEdmund Nadolski 
164589a7301fSDan Williams 	return sci_controller_reset(ihost);
1646cc9203bfSDan Williams }
1647cc9203bfSDan Williams 
1648594e566aSDave Jiang int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
1649cc9203bfSDan Williams {
1650cc9203bfSDan Williams 	int i;
1651cc9203bfSDan Williams 
1652cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1653cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1654cc9203bfSDan Williams 			return -EINVAL;
1655cc9203bfSDan Williams 
1656cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1657cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1658cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1659cc9203bfSDan Williams 			return -EINVAL;
1660cc9203bfSDan Williams 
1661cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1662cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1663cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1664cc9203bfSDan Williams 				return -EINVAL;
1665cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1666cc9203bfSDan Williams 		u8 phy_mask = 0;
1667cc9203bfSDan Williams 
1668cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1669cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1670cc9203bfSDan Williams 
1671cc9203bfSDan Williams 		if (phy_mask == 0)
1672cc9203bfSDan Williams 			return -EINVAL;
1673cc9203bfSDan Williams 	} else
1674cc9203bfSDan Williams 		return -EINVAL;
1675cc9203bfSDan Williams 
16767000f7c7SAndrzej Jakowski 	if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
16777000f7c7SAndrzej Jakowski 	    oem->controller.max_concurr_spin_up < 1)
1678cc9203bfSDan Williams 		return -EINVAL;
1679cc9203bfSDan Williams 
1680594e566aSDave Jiang 	if (oem->controller.do_enable_ssc) {
1681594e566aSDave Jiang 		if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
1682594e566aSDave Jiang 			return -EINVAL;
1683594e566aSDave Jiang 
1684594e566aSDave Jiang 		if (version >= ISCI_ROM_VER_1_1) {
1685594e566aSDave Jiang 			u8 test = oem->controller.ssc_sata_tx_spread_level;
1686594e566aSDave Jiang 
1687594e566aSDave Jiang 			switch (test) {
1688594e566aSDave Jiang 			case 0:
1689594e566aSDave Jiang 			case 2:
1690594e566aSDave Jiang 			case 3:
1691594e566aSDave Jiang 			case 6:
1692594e566aSDave Jiang 			case 7:
1693594e566aSDave Jiang 				break;
1694594e566aSDave Jiang 			default:
1695594e566aSDave Jiang 				return -EINVAL;
1696594e566aSDave Jiang 			}
1697594e566aSDave Jiang 
1698594e566aSDave Jiang 			test = oem->controller.ssc_sas_tx_spread_level;
1699594e566aSDave Jiang 			if (oem->controller.ssc_sas_tx_type == 0) {
1700594e566aSDave Jiang 				switch (test) {
1701594e566aSDave Jiang 				case 0:
1702594e566aSDave Jiang 				case 2:
1703594e566aSDave Jiang 				case 3:
1704594e566aSDave Jiang 					break;
1705594e566aSDave Jiang 				default:
1706594e566aSDave Jiang 					return -EINVAL;
1707594e566aSDave Jiang 				}
1708594e566aSDave Jiang 			} else if (oem->controller.ssc_sas_tx_type == 1) {
1709594e566aSDave Jiang 				switch (test) {
1710594e566aSDave Jiang 				case 0:
1711594e566aSDave Jiang 				case 3:
1712594e566aSDave Jiang 				case 6:
1713594e566aSDave Jiang 					break;
1714594e566aSDave Jiang 				default:
1715594e566aSDave Jiang 					return -EINVAL;
1716594e566aSDave Jiang 				}
1717594e566aSDave Jiang 			}
1718594e566aSDave Jiang 		}
1719594e566aSDave Jiang 	}
1720594e566aSDave Jiang 
1721cc9203bfSDan Williams 	return 0;
1722cc9203bfSDan Williams }
1723cc9203bfSDan Williams 
17247000f7c7SAndrzej Jakowski static u8 max_spin_up(struct isci_host *ihost)
17257000f7c7SAndrzej Jakowski {
17267000f7c7SAndrzej Jakowski 	if (ihost->user_parameters.max_concurr_spinup)
17277000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->user_parameters.max_concurr_spinup,
17287000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
17297000f7c7SAndrzej Jakowski 	else
17307000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
17317000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
17327000f7c7SAndrzej Jakowski }
17337000f7c7SAndrzej Jakowski 
17340473661aSEdmund Nadolski static void power_control_timeout(unsigned long data)
1735cc9203bfSDan Williams {
17360473661aSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1737d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
173885280955SDan Williams 	struct isci_phy *iphy;
17390473661aSEdmund Nadolski 	unsigned long flags;
17400473661aSEdmund Nadolski 	u8 i;
1741cc9203bfSDan Williams 
17420473661aSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
1743cc9203bfSDan Williams 
17440473661aSEdmund Nadolski 	if (tmr->cancel)
17450473661aSEdmund Nadolski 		goto done;
1746cc9203bfSDan Williams 
1747d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
1748cc9203bfSDan Williams 
1749d9dcb4baSDan Williams 	if (ihost->power_control.phys_waiting == 0) {
1750d9dcb4baSDan Williams 		ihost->power_control.timer_started = false;
17510473661aSEdmund Nadolski 		goto done;
17520473661aSEdmund Nadolski 	}
1753cc9203bfSDan Williams 
17540473661aSEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
17550473661aSEdmund Nadolski 
1756d9dcb4baSDan Williams 		if (ihost->power_control.phys_waiting == 0)
17570473661aSEdmund Nadolski 			break;
17580473661aSEdmund Nadolski 
1759d9dcb4baSDan Williams 		iphy = ihost->power_control.requesters[i];
176085280955SDan Williams 		if (iphy == NULL)
17610473661aSEdmund Nadolski 			continue;
17620473661aSEdmund Nadolski 
17637000f7c7SAndrzej Jakowski 		if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
17640473661aSEdmund Nadolski 			break;
17650473661aSEdmund Nadolski 
1766d9dcb4baSDan Williams 		ihost->power_control.requesters[i] = NULL;
1767d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1768d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
176989a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1770be778341SMarcin Tomczak 
1771c79dd80dSDan Williams 		if (iphy->protocol == SAS_PROTOCOL_SSP) {
1772be778341SMarcin Tomczak 			u8 j;
1773be778341SMarcin Tomczak 
1774be778341SMarcin Tomczak 			for (j = 0; j < SCI_MAX_PHYS; j++) {
1775be778341SMarcin Tomczak 				struct isci_phy *requester = ihost->power_control.requesters[j];
1776be778341SMarcin Tomczak 
1777be778341SMarcin Tomczak 				/*
1778be778341SMarcin Tomczak 				 * Search the power_control queue to see if there are other phys
1779be778341SMarcin Tomczak 				 * attached to the same remote device. If found, take all of
1780be778341SMarcin Tomczak 				 * them out of await_sas_power state.
1781be778341SMarcin Tomczak 				 */
1782be778341SMarcin Tomczak 				if (requester != NULL && requester != iphy) {
1783be778341SMarcin Tomczak 					u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
1784be778341SMarcin Tomczak 							  iphy->frame_rcvd.iaf.sas_addr,
1785be778341SMarcin Tomczak 							  sizeof(requester->frame_rcvd.iaf.sas_addr));
1786be778341SMarcin Tomczak 
1787be778341SMarcin Tomczak 					if (other == 0) {
1788be778341SMarcin Tomczak 						ihost->power_control.requesters[j] = NULL;
1789be778341SMarcin Tomczak 						ihost->power_control.phys_waiting--;
1790be778341SMarcin Tomczak 						sci_phy_consume_power_handler(requester);
1791be778341SMarcin Tomczak 					}
1792be778341SMarcin Tomczak 				}
1793be778341SMarcin Tomczak 			}
1794be778341SMarcin Tomczak 		}
1795cc9203bfSDan Williams 	}
1796cc9203bfSDan Williams 
1797cc9203bfSDan Williams 	/*
1798cc9203bfSDan Williams 	 * It doesn't matter if the power list is empty, we need to start the
1799cc9203bfSDan Williams 	 * timer in case another phy becomes ready.
1800cc9203bfSDan Williams 	 */
18010473661aSEdmund Nadolski 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1802d9dcb4baSDan Williams 	ihost->power_control.timer_started = true;
18030473661aSEdmund Nadolski 
18040473661aSEdmund Nadolski done:
18050473661aSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1806cc9203bfSDan Williams }
1807cc9203bfSDan Williams 
180889a7301fSDan Williams void sci_controller_power_control_queue_insert(struct isci_host *ihost,
180985280955SDan Williams 					       struct isci_phy *iphy)
1810cc9203bfSDan Williams {
181185280955SDan Williams 	BUG_ON(iphy == NULL);
1812cc9203bfSDan Williams 
18137000f7c7SAndrzej Jakowski 	if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
1814d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
181589a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1816cc9203bfSDan Williams 
1817cc9203bfSDan Williams 		/*
1818cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1819cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1820cc9203bfSDan Williams 		 */
1821d9dcb4baSDan Williams 		if (ihost->power_control.timer_started)
1822d9dcb4baSDan Williams 			sci_del_timer(&ihost->power_control.timer);
18230473661aSEdmund Nadolski 
1824d9dcb4baSDan Williams 		sci_mod_timer(&ihost->power_control.timer,
18250473661aSEdmund Nadolski 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1826d9dcb4baSDan Williams 		ihost->power_control.timer_started = true;
18270473661aSEdmund Nadolski 
1828cc9203bfSDan Williams 	} else {
1829be778341SMarcin Tomczak 		/*
1830be778341SMarcin Tomczak 		 * There are phys, attached to the same sas address as this phy, are
1831be778341SMarcin Tomczak 		 * already in READY state, this phy don't need wait.
1832be778341SMarcin Tomczak 		 */
1833be778341SMarcin Tomczak 		u8 i;
1834be778341SMarcin Tomczak 		struct isci_phy *current_phy;
1835be778341SMarcin Tomczak 
1836be778341SMarcin Tomczak 		for (i = 0; i < SCI_MAX_PHYS; i++) {
1837be778341SMarcin Tomczak 			u8 other;
1838be778341SMarcin Tomczak 			current_phy = &ihost->phys[i];
1839be778341SMarcin Tomczak 
1840be778341SMarcin Tomczak 			other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
1841be778341SMarcin Tomczak 				       iphy->frame_rcvd.iaf.sas_addr,
1842be778341SMarcin Tomczak 				       sizeof(current_phy->frame_rcvd.iaf.sas_addr));
1843be778341SMarcin Tomczak 
1844be778341SMarcin Tomczak 			if (current_phy->sm.current_state_id == SCI_PHY_READY &&
1845c79dd80dSDan Williams 			    current_phy->protocol == SAS_PROTOCOL_SSP &&
1846be778341SMarcin Tomczak 			    other == 0) {
1847be778341SMarcin Tomczak 				sci_phy_consume_power_handler(iphy);
1848be778341SMarcin Tomczak 				break;
1849be778341SMarcin Tomczak 			}
1850be778341SMarcin Tomczak 		}
1851be778341SMarcin Tomczak 
1852be778341SMarcin Tomczak 		if (i == SCI_MAX_PHYS) {
1853cc9203bfSDan Williams 			/* Add the phy in the waiting list */
1854d9dcb4baSDan Williams 			ihost->power_control.requesters[iphy->phy_index] = iphy;
1855d9dcb4baSDan Williams 			ihost->power_control.phys_waiting++;
1856cc9203bfSDan Williams 		}
1857cc9203bfSDan Williams 	}
1858be778341SMarcin Tomczak }
1859cc9203bfSDan Williams 
186089a7301fSDan Williams void sci_controller_power_control_queue_remove(struct isci_host *ihost,
186185280955SDan Williams 					       struct isci_phy *iphy)
1862cc9203bfSDan Williams {
186385280955SDan Williams 	BUG_ON(iphy == NULL);
1864cc9203bfSDan Williams 
186589a7301fSDan Williams 	if (ihost->power_control.requesters[iphy->phy_index])
1866d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1867cc9203bfSDan Williams 
1868d9dcb4baSDan Williams 	ihost->power_control.requesters[iphy->phy_index] = NULL;
1869cc9203bfSDan Williams }
1870cc9203bfSDan Williams 
1871afd13a1fSJeff Skirvin static int is_long_cable(int phy, unsigned char selection_byte)
1872afd13a1fSJeff Skirvin {
18739fee607fSJeff Skirvin 	return !!(selection_byte & (1 << phy));
1874afd13a1fSJeff Skirvin }
1875afd13a1fSJeff Skirvin 
1876afd13a1fSJeff Skirvin static int is_medium_cable(int phy, unsigned char selection_byte)
1877afd13a1fSJeff Skirvin {
18789fee607fSJeff Skirvin 	return !!(selection_byte & (1 << (phy + 4)));
18799fee607fSJeff Skirvin }
18809fee607fSJeff Skirvin 
18819fee607fSJeff Skirvin static enum cable_selections decode_selection_byte(
18829fee607fSJeff Skirvin 	int phy,
18839fee607fSJeff Skirvin 	unsigned char selection_byte)
18849fee607fSJeff Skirvin {
18859fee607fSJeff Skirvin 	return ((selection_byte & (1 << phy)) ? 1 : 0)
18869fee607fSJeff Skirvin 		+ (selection_byte & (1 << (phy + 4)) ? 2 : 0);
18879fee607fSJeff Skirvin }
18889fee607fSJeff Skirvin 
18899fee607fSJeff Skirvin static unsigned char *to_cable_select(struct isci_host *ihost)
18909fee607fSJeff Skirvin {
18919fee607fSJeff Skirvin 	if (is_cable_select_overridden())
18929fee607fSJeff Skirvin 		return ((unsigned char *)&cable_selection_override)
18939fee607fSJeff Skirvin 			+ ihost->id;
18949fee607fSJeff Skirvin 	else
18959fee607fSJeff Skirvin 		return &ihost->oem_parameters.controller.cable_selection_mask;
18969fee607fSJeff Skirvin }
18979fee607fSJeff Skirvin 
18989fee607fSJeff Skirvin enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
18999fee607fSJeff Skirvin {
19009fee607fSJeff Skirvin 	return decode_selection_byte(phy, *to_cable_select(ihost));
19019fee607fSJeff Skirvin }
19029fee607fSJeff Skirvin 
19039fee607fSJeff Skirvin char *lookup_cable_names(enum cable_selections selection)
19049fee607fSJeff Skirvin {
19059fee607fSJeff Skirvin 	static char *cable_names[] = {
19069fee607fSJeff Skirvin 		[short_cable]     = "short",
19079fee607fSJeff Skirvin 		[long_cable]      = "long",
19089fee607fSJeff Skirvin 		[medium_cable]    = "medium",
19099fee607fSJeff Skirvin 		[undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
19109fee607fSJeff Skirvin 	};
19119fee607fSJeff Skirvin 	return (selection <= undefined_cable) ? cable_names[selection]
19129fee607fSJeff Skirvin 					      : cable_names[undefined_cable];
1913afd13a1fSJeff Skirvin }
1914afd13a1fSJeff Skirvin 
1915cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
1916cc9203bfSDan Williams 
191789a7301fSDan Williams static void sci_controller_afe_initialization(struct isci_host *ihost)
1918cc9203bfSDan Williams {
19192e5da889SDan Williams 	struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
192089a7301fSDan Williams 	const struct sci_oem_params *oem = &ihost->oem_parameters;
1921dc00c8b6SDan Williams 	struct pci_dev *pdev = ihost->pdev;
1922cc9203bfSDan Williams 	u32 afe_status;
1923cc9203bfSDan Williams 	u32 phy_id;
19249fee607fSJeff Skirvin 	unsigned char cable_selection_mask = *to_cable_select(ihost);
1925cc9203bfSDan Williams 
1926cc9203bfSDan Williams 	/* Clear DFX Status registers */
19272e5da889SDan Williams 	writel(0x0081000f, &afe->afe_dfx_master_control0);
1928cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1929cc9203bfSDan Williams 
1930afd13a1fSJeff Skirvin 	if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
1931cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
19322e5da889SDan Williams 		 * Timer, PM Stagger Timer
19332e5da889SDan Williams 		 */
1934afd13a1fSJeff Skirvin 		writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
1935cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1936cc9203bfSDan Williams 	}
1937cc9203bfSDan Williams 
1938cc9203bfSDan Williams 	/* Configure bias currents to normal */
1939dc00c8b6SDan Williams 	if (is_a2(pdev))
19402e5da889SDan Williams 		writel(0x00005A00, &afe->afe_bias_control);
1941dc00c8b6SDan Williams 	else if (is_b0(pdev) || is_c0(pdev))
19422e5da889SDan Williams 		writel(0x00005F00, &afe->afe_bias_control);
1943afd13a1fSJeff Skirvin 	else if (is_c1(pdev))
1944afd13a1fSJeff Skirvin 		writel(0x00005500, &afe->afe_bias_control);
1945cc9203bfSDan Williams 
1946cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1947cc9203bfSDan Williams 
1948cc9203bfSDan Williams 	/* Enable PLL */
1949afd13a1fSJeff Skirvin 	if (is_a2(pdev))
19502e5da889SDan Williams 		writel(0x80040908, &afe->afe_pll_control0);
1951afd13a1fSJeff Skirvin 	else if (is_b0(pdev) || is_c0(pdev))
1952afd13a1fSJeff Skirvin 		writel(0x80040A08, &afe->afe_pll_control0);
1953afd13a1fSJeff Skirvin 	else if (is_c1(pdev)) {
1954afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
1955afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
1956afd13a1fSJeff Skirvin 		writel(0x00000B08, &afe->afe_pll_control0);
1957afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
1958afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
1959afd13a1fSJeff Skirvin 	}
1960cc9203bfSDan Williams 
1961cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1962cc9203bfSDan Williams 
1963cc9203bfSDan Williams 	/* Wait for the PLL to lock */
1964cc9203bfSDan Williams 	do {
19652e5da889SDan Williams 		afe_status = readl(&afe->afe_common_block_status);
1966cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1967cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
1968cc9203bfSDan Williams 
1969dc00c8b6SDan Williams 	if (is_a2(pdev)) {
19702e5da889SDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76
19712e5da889SDan Williams 		 * us to 50 us)
19722e5da889SDan Williams 		 */
19732e5da889SDan Williams 		writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
1974cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1975cc9203bfSDan Williams 	}
1976cc9203bfSDan Williams 
1977cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
19782e5da889SDan Williams 		struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id];
1979cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
1980afd13a1fSJeff Skirvin 		int cable_length_long =
1981afd13a1fSJeff Skirvin 			is_long_cable(phy_id, cable_selection_mask);
1982afd13a1fSJeff Skirvin 		int cable_length_medium =
1983afd13a1fSJeff Skirvin 			is_medium_cable(phy_id, cable_selection_mask);
1984cc9203bfSDan Williams 
1985afd13a1fSJeff Skirvin 		if (is_a2(pdev)) {
19862e5da889SDan Williams 			/* All defaults, except the Receive Word
19872e5da889SDan Williams 			 * Alignament/Comma Detect Enable....(0xe800)
19882e5da889SDan Williams 			 */
19892e5da889SDan Williams 			writel(0x00004512, &xcvr->afe_xcvr_control0);
1990cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1991cc9203bfSDan Williams 
19922e5da889SDan Williams 			writel(0x0050100F, &xcvr->afe_xcvr_control1);
1993cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1994afd13a1fSJeff Skirvin 		} else if (is_b0(pdev)) {
1995afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
1996afd13a1fSJeff Skirvin 			writel(0x00030000, &xcvr->afe_tx_ssc_control);
1997afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
1998afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
1999afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2000afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2001afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2002afd13a1fSJeff Skirvin 
2003afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2004afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2005afd13a1fSJeff Skirvin 			 */
2006afd13a1fSJeff Skirvin 			writel(0x00014500, &xcvr->afe_xcvr_control0);
2007afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2008afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2009afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2010afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2011afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2012afd13a1fSJeff Skirvin 
2013afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2014afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2015afd13a1fSJeff Skirvin 			 */
2016afd13a1fSJeff Skirvin 			writel(0x0001C500, &xcvr->afe_xcvr_control0);
2017afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2018cc9203bfSDan Williams 		}
2019cc9203bfSDan Williams 
2020afd13a1fSJeff Skirvin 		/* Power up TX and RX out from power down (PWRDNTX and
2021afd13a1fSJeff Skirvin 		 * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
20222e5da889SDan Williams 		 */
2023dc00c8b6SDan Williams 		if (is_a2(pdev))
20242e5da889SDan Williams 			writel(0x000003F0, &xcvr->afe_channel_control);
2025dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
20262e5da889SDan Williams 			writel(0x000003D7, &xcvr->afe_channel_control);
2027cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2028afd13a1fSJeff Skirvin 
20292e5da889SDan Williams 			writel(0x000003D4, &xcvr->afe_channel_control);
2030afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
20312e5da889SDan Williams 			writel(0x000001E7, &xcvr->afe_channel_control);
2032dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2033afd13a1fSJeff Skirvin 
20342e5da889SDan Williams 			writel(0x000001E4, &xcvr->afe_channel_control);
2035afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2036afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F7 : 0x000001F7,
2037afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2038afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2039afd13a1fSJeff Skirvin 
2040afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F4 : 0x000001F4,
2041afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2042cc9203bfSDan Williams 		}
2043cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2044cc9203bfSDan Williams 
2045dc00c8b6SDan Williams 		if (is_a2(pdev)) {
2046cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
20472e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2048cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2049cc9203bfSDan Williams 		}
2050cc9203bfSDan Williams 
2051afd13a1fSJeff Skirvin 		if (is_a2(pdev) || is_b0(pdev))
2052afd13a1fSJeff Skirvin 			/* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
2053afd13a1fSJeff Skirvin 			 * TPD=0x0(TX Power On), RDD=0x0(RX Detect
2054afd13a1fSJeff Skirvin 			 * Enabled) ....(0xe800)
2055afd13a1fSJeff Skirvin 			 */
20562e5da889SDan Williams 			writel(0x00004100, &xcvr->afe_xcvr_control0);
2057afd13a1fSJeff Skirvin 		else if (is_c0(pdev))
2058afd13a1fSJeff Skirvin 			writel(0x00014100, &xcvr->afe_xcvr_control0);
2059afd13a1fSJeff Skirvin 		else if (is_c1(pdev))
2060afd13a1fSJeff Skirvin 			writel(0x0001C100, &xcvr->afe_xcvr_control0);
2061cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2062cc9203bfSDan Williams 
2063cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2064dc00c8b6SDan Williams 		if (is_a2(pdev))
20652e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2066dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
20672e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2068cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2069cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
20702e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2071afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2072afd13a1fSJeff Skirvin 			writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
2073dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2074dbb0743aSAdam Gruchala 
20752e5da889SDan Williams 			writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
2076dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2077dbb0743aSAdam Gruchala 
2078dbb0743aSAdam Gruchala 			/* Enable TX equalization (0xe824) */
20792e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2080afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2081afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x01500C0C :
2082afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x01400C0D : 0x02400C0D,
2083afd13a1fSJeff Skirvin 			       &xcvr->afe_xcvr_control1);
2084afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2085afd13a1fSJeff Skirvin 
2086afd13a1fSJeff Skirvin 			writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
2087afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2088afd13a1fSJeff Skirvin 
2089afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x33091C1F :
2090afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x3315181F : 0x2B17161F,
2091afd13a1fSJeff Skirvin 			       &xcvr->afe_rx_ssc_control0);
2092afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2093afd13a1fSJeff Skirvin 
2094afd13a1fSJeff Skirvin 			/* Enable TX equalization (0xe824) */
2095afd13a1fSJeff Skirvin 			writel(0x00040000, &xcvr->afe_tx_control);
2096cc9203bfSDan Williams 		}
2097dbb0743aSAdam Gruchala 
2098cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2099cc9203bfSDan Williams 
21002e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
2101cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2102cc9203bfSDan Williams 
21032e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
2104cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2105cc9203bfSDan Williams 
21062e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
2107cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2108cc9203bfSDan Williams 
21092e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
2110cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2111cc9203bfSDan Williams 	}
2112cc9203bfSDan Williams 
2113cc9203bfSDan Williams 	/* Transfer control to the PEs */
21142e5da889SDan Williams 	writel(0x00010f00, &afe->afe_dfx_master_control0);
2115cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2116cc9203bfSDan Williams }
2117cc9203bfSDan Williams 
211889a7301fSDan Williams static void sci_controller_initialize_power_control(struct isci_host *ihost)
2119cc9203bfSDan Williams {
2120d9dcb4baSDan Williams 	sci_init_timer(&ihost->power_control.timer, power_control_timeout);
2121cc9203bfSDan Williams 
2122d9dcb4baSDan Williams 	memset(ihost->power_control.requesters, 0,
2123d9dcb4baSDan Williams 	       sizeof(ihost->power_control.requesters));
2124cc9203bfSDan Williams 
2125d9dcb4baSDan Williams 	ihost->power_control.phys_waiting = 0;
2126d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
2127cc9203bfSDan Williams }
2128cc9203bfSDan Williams 
212989a7301fSDan Williams static enum sci_status sci_controller_initialize(struct isci_host *ihost)
2130cc9203bfSDan Williams {
2131d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
21327c78da31SDan Williams 	enum sci_status result = SCI_FAILURE;
21337c78da31SDan Williams 	unsigned long i, state, val;
2134cc9203bfSDan Williams 
2135d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_RESET) {
213614e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
213714e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2138cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2139cc9203bfSDan Williams 	}
2140cc9203bfSDan Williams 
2141e301370aSEdmund Nadolski 	sci_change_state(sm, SCIC_INITIALIZING);
2142cc9203bfSDan Williams 
2143d9dcb4baSDan Williams 	sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
2144bb3dbdf6SEdmund Nadolski 
2145d9dcb4baSDan Williams 	ihost->next_phy_to_start = 0;
2146d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
2147cc9203bfSDan Williams 
214889a7301fSDan Williams 	sci_controller_initialize_power_control(ihost);
2149cc9203bfSDan Williams 
2150cc9203bfSDan Williams 	/*
2151cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2152cc9203bfSDan Williams 	 * program the AFE registers.
2153cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2154cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
215589a7301fSDan Williams 	sci_controller_afe_initialization(ihost);
2156cc9203bfSDan Williams 
2157cc9203bfSDan Williams 
2158cc9203bfSDan Williams 	/* Take the hardware out of reset */
2159d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->soft_reset_control);
2160cc9203bfSDan Williams 
2161cc9203bfSDan Williams 	/*
2162cc9203bfSDan Williams 	 * / @todo Provide meaningfull error code for hardware failure
2163cc9203bfSDan Williams 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
21647c78da31SDan Williams 	for (i = 100; i >= 1; i--) {
21657c78da31SDan Williams 		u32 status;
2166cc9203bfSDan Williams 
2167cc9203bfSDan Williams 		/* Loop until the hardware reports success */
2168cc9203bfSDan Williams 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2169d9dcb4baSDan Williams 		status = readl(&ihost->smu_registers->control_status);
2170cc9203bfSDan Williams 
21717c78da31SDan Williams 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
21727c78da31SDan Williams 			break;
2173cc9203bfSDan Williams 	}
21747c78da31SDan Williams 	if (i == 0)
21757c78da31SDan Williams 		goto out;
2176cc9203bfSDan Williams 
2177cc9203bfSDan Williams 	/*
2178cc9203bfSDan Williams 	 * Determine what are the actaul device capacities that the
2179cc9203bfSDan Williams 	 * hardware will support */
2180d9dcb4baSDan Williams 	val = readl(&ihost->smu_registers->device_context_capacity);
2181cc9203bfSDan Williams 
21827c78da31SDan Williams 	/* Record the smaller of the two capacity values */
2183d9dcb4baSDan Williams 	ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2184d9dcb4baSDan Williams 	ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2185d9dcb4baSDan Williams 	ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2186cc9203bfSDan Williams 
2187cc9203bfSDan Williams 	/*
2188cc9203bfSDan Williams 	 * Make all PEs that are unassigned match up with the
2189cc9203bfSDan Williams 	 * logical ports
2190cc9203bfSDan Williams 	 */
2191d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
2192cc9203bfSDan Williams 		struct scu_port_task_scheduler_group_registers __iomem
2193d9dcb4baSDan Williams 			*ptsg = &ihost->scu_registers->peg0.ptsg;
2194cc9203bfSDan Williams 
21957c78da31SDan Williams 		writel(i, &ptsg->protocol_engine[i]);
2196cc9203bfSDan Williams 	}
2197cc9203bfSDan Williams 
2198cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2199d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.pdma_configuration);
22007c78da31SDan Williams 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2201d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.pdma_configuration);
2202cc9203bfSDan Williams 
2203d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.cdma_configuration);
22047c78da31SDan Williams 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2205d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.cdma_configuration);
2206cc9203bfSDan Williams 
2207cc9203bfSDan Williams 	/*
2208cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2209cc9203bfSDan Williams 	 * are accessed during the port initialization.
2210cc9203bfSDan Williams 	 */
22117c78da31SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
221289a7301fSDan Williams 		result = sci_phy_initialize(&ihost->phys[i],
2213d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].tl,
2214d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].ll);
22157c78da31SDan Williams 		if (result != SCI_SUCCESS)
22167c78da31SDan Williams 			goto out;
2217cc9203bfSDan Williams 	}
2218cc9203bfSDan Williams 
2219d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
222089a7301fSDan Williams 		struct isci_port *iport = &ihost->ports[i];
22217c78da31SDan Williams 
222289a7301fSDan Williams 		iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
222389a7301fSDan Williams 		iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
222489a7301fSDan Williams 		iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
2225cc9203bfSDan Williams 	}
2226cc9203bfSDan Williams 
222789a7301fSDan Williams 	result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
2228cc9203bfSDan Williams 
22297c78da31SDan Williams  out:
2230cc9203bfSDan Williams 	/* Advance the controller state machine */
2231cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2232e301370aSEdmund Nadolski 		state = SCIC_INITIALIZED;
2233cc9203bfSDan Williams 	else
2234e301370aSEdmund Nadolski 		state = SCIC_FAILED;
2235e301370aSEdmund Nadolski 	sci_change_state(sm, state);
2236cc9203bfSDan Williams 
2237cc9203bfSDan Williams 	return result;
2238cc9203bfSDan Williams }
2239cc9203bfSDan Williams 
2240abec912dSDan Williams static int sci_controller_dma_alloc(struct isci_host *ihost)
2241cc9203bfSDan Williams {
2242abec912dSDan Williams 	struct device *dev = &ihost->pdev->dev;
2243abec912dSDan Williams 	size_t size;
2244abec912dSDan Williams 	int i;
2245cc9203bfSDan Williams 
2246abec912dSDan Williams 	/* detect re-initialization */
2247abec912dSDan Williams 	if (ihost->completion_queue)
2248abec912dSDan Williams 		return 0;
2249cc9203bfSDan Williams 
2250abec912dSDan Williams 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2251abec912dSDan Williams 	ihost->completion_queue = dmam_alloc_coherent(dev, size, &ihost->cq_dma,
2252abec912dSDan Williams 						      GFP_KERNEL);
2253abec912dSDan Williams 	if (!ihost->completion_queue)
2254abec912dSDan Williams 		return -ENOMEM;
2255cc9203bfSDan Williams 
2256abec912dSDan Williams 	size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2257abec912dSDan Williams 	ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &ihost->rnc_dma,
2258abec912dSDan Williams 							       GFP_KERNEL);
2259cc9203bfSDan Williams 
2260abec912dSDan Williams 	if (!ihost->remote_node_context_table)
2261abec912dSDan Williams 		return -ENOMEM;
2262cc9203bfSDan Williams 
2263abec912dSDan Williams 	size = ihost->task_context_entries * sizeof(struct scu_task_context),
2264abec912dSDan Williams 	ihost->task_context_table = dmam_alloc_coherent(dev, size, &ihost->tc_dma,
2265abec912dSDan Williams 							GFP_KERNEL);
2266abec912dSDan Williams 	if (!ihost->task_context_table)
2267abec912dSDan Williams 		return -ENOMEM;
2268cc9203bfSDan Williams 
2269abec912dSDan Williams 	size = SCI_UFI_TOTAL_SIZE;
2270abec912dSDan Williams 	ihost->ufi_buf = dmam_alloc_coherent(dev, size, &ihost->ufi_dma, GFP_KERNEL);
2271abec912dSDan Williams 	if (!ihost->ufi_buf)
2272abec912dSDan Williams 		return -ENOMEM;
2273abec912dSDan Williams 
2274abec912dSDan Williams 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2275abec912dSDan Williams 		struct isci_request *ireq;
2276abec912dSDan Williams 		dma_addr_t dma;
2277abec912dSDan Williams 
2278abec912dSDan Williams 		ireq = dmam_alloc_coherent(dev, sizeof(*ireq), &dma, GFP_KERNEL);
2279abec912dSDan Williams 		if (!ireq)
2280abec912dSDan Williams 			return -ENOMEM;
2281abec912dSDan Williams 
2282abec912dSDan Williams 		ireq->tc = &ihost->task_context_table[i];
2283abec912dSDan Williams 		ireq->owning_controller = ihost;
2284abec912dSDan Williams 		ireq->request_daddr = dma;
2285abec912dSDan Williams 		ireq->isci_host = ihost;
2286abec912dSDan Williams 		ihost->reqs[i] = ireq;
2287cc9203bfSDan Williams 	}
2288cc9203bfSDan Williams 
2289abec912dSDan Williams 	return 0;
2290cc9203bfSDan Williams }
2291cc9203bfSDan Williams 
229289a7301fSDan Williams static int sci_controller_mem_init(struct isci_host *ihost)
2293cc9203bfSDan Williams {
2294abec912dSDan Williams 	int err = sci_controller_dma_alloc(ihost);
2295cc9203bfSDan Williams 
22967c78da31SDan Williams 	if (err)
22977c78da31SDan Williams 		return err;
2298cc9203bfSDan Williams 
2299abec912dSDan Williams 	writel(lower_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_lower);
2300abec912dSDan Williams 	writel(upper_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_upper);
2301abec912dSDan Williams 
2302abec912dSDan Williams 	writel(lower_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_lower);
2303abec912dSDan Williams 	writel(upper_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_upper);
2304abec912dSDan Williams 
2305abec912dSDan Williams 	writel(lower_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_lower);
2306abec912dSDan Williams 	writel(upper_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_upper);
2307abec912dSDan Williams 
2308abec912dSDan Williams 	sci_unsolicited_frame_control_construct(ihost);
2309abec912dSDan Williams 
2310cc9203bfSDan Williams 	/*
2311cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2312cc9203bfSDan Williams 	 * address table.
2313cc9203bfSDan Williams 	 */
2314d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2315d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_lower);
2316d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2317d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_upper);
2318cc9203bfSDan Williams 
2319d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2320d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_lower);
2321d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2322d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_upper);
2323cc9203bfSDan Williams 
2324cc9203bfSDan Williams 	return 0;
2325cc9203bfSDan Williams }
2326cc9203bfSDan Williams 
2327abec912dSDan Williams /**
2328abec912dSDan Williams  * isci_host_init - (re-)initialize hardware and internal (private) state
2329abec912dSDan Williams  * @ihost: host to init
2330abec912dSDan Williams  *
2331abec912dSDan Williams  * Any public facing objects (like asd_sas_port, and asd_sas_phys), or
2332abec912dSDan Williams  * one-time initialization objects like locks and waitqueues, are
2333abec912dSDan Williams  * not touched (they are initialized in isci_host_alloc)
2334abec912dSDan Williams  */
2335d9dcb4baSDan Williams int isci_host_init(struct isci_host *ihost)
23366f231ddaSDan Williams {
2337abec912dSDan Williams 	int i, err;
23386f231ddaSDan Williams 	enum sci_status status;
23396f231ddaSDan Williams 
23402396a265SDan Williams 	spin_lock_irq(&ihost->scic_lock);
2341abec912dSDan Williams 	status = sci_controller_construct(ihost, scu_base(ihost), smu_base(ihost));
23422396a265SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
23436f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2344d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
234589a7301fSDan Williams 			"%s: sci_controller_construct failed - status = %x\n",
23466f231ddaSDan Williams 			__func__,
23476f231ddaSDan Williams 			status);
2348858d4aa7SDave Jiang 		return -ENODEV;
23496f231ddaSDan Williams 	}
23506f231ddaSDan Williams 
2351d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
235289a7301fSDan Williams 	status = sci_controller_initialize(ihost);
2353d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
23547c40a803SDan Williams 	if (status != SCI_SUCCESS) {
2355d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
235689a7301fSDan Williams 			 "%s: sci_controller_initialize failed -"
23577c40a803SDan Williams 			 " status = 0x%x\n",
23587c40a803SDan Williams 			 __func__, status);
23597c40a803SDan Williams 		return -ENODEV;
23607c40a803SDan Williams 	}
23617c40a803SDan Williams 
236289a7301fSDan Williams 	err = sci_controller_mem_init(ihost);
23636f231ddaSDan Williams 	if (err)
2364858d4aa7SDave Jiang 		return err;
23656f231ddaSDan Williams 
2366ad4f4c1dSDan Williams 	/* enable sgpio */
2367ad4f4c1dSDan Williams 	writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
2368ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
2369ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
2370ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
2371ad4f4c1dSDan Williams 
2372858d4aa7SDave Jiang 	return 0;
23736f231ddaSDan Williams }
2374cc9203bfSDan Williams 
237589a7301fSDan Williams void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
237689a7301fSDan Williams 			    struct isci_phy *iphy)
2377cc9203bfSDan Williams {
2378d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2379e301370aSEdmund Nadolski 	case SCIC_STARTING:
2380d9dcb4baSDan Williams 		sci_del_timer(&ihost->phy_timer);
2381d9dcb4baSDan Williams 		ihost->phy_startup_timer_pending = false;
2382d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2383ffe191c9SDan Williams 						  iport, iphy);
238489a7301fSDan Williams 		sci_controller_start_next_phy(ihost);
2385cc9203bfSDan Williams 		break;
2386e301370aSEdmund Nadolski 	case SCIC_READY:
2387d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2388ffe191c9SDan Williams 						  iport, iphy);
2389cc9203bfSDan Williams 		break;
2390cc9203bfSDan Williams 	default:
2391d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2392cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
239385280955SDan Williams 			"unexpected state %d\n", __func__, iphy->phy_index,
2394d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2395cc9203bfSDan Williams 	}
2396cc9203bfSDan Williams }
2397cc9203bfSDan Williams 
239889a7301fSDan Williams void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
239989a7301fSDan Williams 			      struct isci_phy *iphy)
2400cc9203bfSDan Williams {
2401d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2402e301370aSEdmund Nadolski 	case SCIC_STARTING:
2403e301370aSEdmund Nadolski 	case SCIC_READY:
2404d9dcb4baSDan Williams 		ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
2405ffe191c9SDan Williams 						   iport, iphy);
2406cc9203bfSDan Williams 		break;
2407cc9203bfSDan Williams 	default:
2408d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2409cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2410cc9203bfSDan Williams 			"unexpected state %d\n",
2411cc9203bfSDan Williams 			__func__,
241285280955SDan Williams 			iphy->phy_index,
2413d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2414cc9203bfSDan Williams 	}
2415cc9203bfSDan Williams }
2416cc9203bfSDan Williams 
2417eb608c3cSDan Williams bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
2418cc9203bfSDan Williams {
2419cc9203bfSDan Williams 	u32 index;
2420cc9203bfSDan Williams 
2421d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
2422d9dcb4baSDan Williams 		if ((ihost->device_table[index] != NULL) &&
2423d9dcb4baSDan Williams 		   (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2424cc9203bfSDan Williams 			return true;
2425cc9203bfSDan Williams 	}
2426cc9203bfSDan Williams 
2427cc9203bfSDan Williams 	return false;
2428cc9203bfSDan Williams }
2429cc9203bfSDan Williams 
243089a7301fSDan Williams void sci_controller_remote_device_stopped(struct isci_host *ihost,
243178a6f06eSDan Williams 					  struct isci_remote_device *idev)
2432cc9203bfSDan Williams {
2433d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_STOPPING) {
2434d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2435cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2436cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2437d9dcb4baSDan Williams 			ihost, idev,
2438d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2439cc9203bfSDan Williams 		return;
2440cc9203bfSDan Williams 	}
2441cc9203bfSDan Williams 
244289a7301fSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
2443eb608c3cSDan Williams 		isci_host_stop_complete(ihost);
2444cc9203bfSDan Williams }
2445cc9203bfSDan Williams 
244689a7301fSDan Williams void sci_controller_post_request(struct isci_host *ihost, u32 request)
2447cc9203bfSDan Williams {
244889a7301fSDan Williams 	dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
244989a7301fSDan Williams 		__func__, ihost->id, request);
2450cc9203bfSDan Williams 
2451d9dcb4baSDan Williams 	writel(request, &ihost->smu_registers->post_context_port);
2452cc9203bfSDan Williams }
2453cc9203bfSDan Williams 
245489a7301fSDan Williams struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
2455cc9203bfSDan Williams {
2456cc9203bfSDan Williams 	u16 task_index;
2457cc9203bfSDan Williams 	u16 task_sequence;
2458cc9203bfSDan Williams 
2459dd047c8eSDan Williams 	task_index = ISCI_TAG_TCI(io_tag);
2460cc9203bfSDan Williams 
2461d9dcb4baSDan Williams 	if (task_index < ihost->task_context_entries) {
2462d9dcb4baSDan Williams 		struct isci_request *ireq = ihost->reqs[task_index];
2463db056250SDan Williams 
2464db056250SDan Williams 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2465dd047c8eSDan Williams 			task_sequence = ISCI_TAG_SEQ(io_tag);
2466cc9203bfSDan Williams 
2467d9dcb4baSDan Williams 			if (task_sequence == ihost->io_request_sequence[task_index])
24685076a1a9SDan Williams 				return ireq;
2469cc9203bfSDan Williams 		}
2470cc9203bfSDan Williams 	}
2471cc9203bfSDan Williams 
2472cc9203bfSDan Williams 	return NULL;
2473cc9203bfSDan Williams }
2474cc9203bfSDan Williams 
2475cc9203bfSDan Williams /**
2476cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2477cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2478cc9203bfSDan Williams  *    node index available.
2479cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2480cc9203bfSDan Williams  *    free remote node ids
2481cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2482cc9203bfSDan Williams  *    id
2483cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2484cc9203bfSDan Williams  *    is available
2485cc9203bfSDan Williams  *
2486cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2487cc9203bfSDan Williams  * node index available.
2488cc9203bfSDan Williams  */
248989a7301fSDan Williams enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
249078a6f06eSDan Williams 							    struct isci_remote_device *idev,
2491cc9203bfSDan Williams 							    u16 *node_id)
2492cc9203bfSDan Williams {
2493cc9203bfSDan Williams 	u16 node_index;
249489a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2495cc9203bfSDan Williams 
249689a7301fSDan Williams 	node_index = sci_remote_node_table_allocate_remote_node(
2497d9dcb4baSDan Williams 		&ihost->available_remote_nodes, remote_node_count
2498cc9203bfSDan Williams 		);
2499cc9203bfSDan Williams 
2500cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2501d9dcb4baSDan Williams 		ihost->device_table[node_index] = idev;
2502cc9203bfSDan Williams 
2503cc9203bfSDan Williams 		*node_id = node_index;
2504cc9203bfSDan Williams 
2505cc9203bfSDan Williams 		return SCI_SUCCESS;
2506cc9203bfSDan Williams 	}
2507cc9203bfSDan Williams 
2508cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2509cc9203bfSDan Williams }
2510cc9203bfSDan Williams 
251189a7301fSDan Williams void sci_controller_free_remote_node_context(struct isci_host *ihost,
251278a6f06eSDan Williams 					     struct isci_remote_device *idev,
2513cc9203bfSDan Williams 					     u16 node_id)
2514cc9203bfSDan Williams {
251589a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2516cc9203bfSDan Williams 
2517d9dcb4baSDan Williams 	if (ihost->device_table[node_id] == idev) {
2518d9dcb4baSDan Williams 		ihost->device_table[node_id] = NULL;
2519cc9203bfSDan Williams 
252089a7301fSDan Williams 		sci_remote_node_table_release_remote_node_index(
2521d9dcb4baSDan Williams 			&ihost->available_remote_nodes, remote_node_count, node_id
2522cc9203bfSDan Williams 			);
2523cc9203bfSDan Williams 	}
2524cc9203bfSDan Williams }
2525cc9203bfSDan Williams 
252689a7301fSDan Williams void sci_controller_copy_sata_response(void *response_buffer,
2527cc9203bfSDan Williams 				       void *frame_header,
2528cc9203bfSDan Williams 				       void *frame_buffer)
2529cc9203bfSDan Williams {
253089a7301fSDan Williams 	/* XXX type safety? */
2531cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2532cc9203bfSDan Williams 
2533cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2534cc9203bfSDan Williams 	       frame_buffer,
2535cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2536cc9203bfSDan Williams }
2537cc9203bfSDan Williams 
253889a7301fSDan Williams void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
2539cc9203bfSDan Williams {
254089a7301fSDan Williams 	if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
2541d9dcb4baSDan Williams 		writel(ihost->uf_control.get,
2542d9dcb4baSDan Williams 			&ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
2543cc9203bfSDan Williams }
2544cc9203bfSDan Williams 
2545312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci)
2546312e0c24SDan Williams {
2547312e0c24SDan Williams 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2548312e0c24SDan Williams 
2549312e0c24SDan Williams 	ihost->tci_pool[tail] = tci;
2550312e0c24SDan Williams 	ihost->tci_tail = tail + 1;
2551312e0c24SDan Williams }
2552312e0c24SDan Williams 
2553312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost)
2554312e0c24SDan Williams {
2555312e0c24SDan Williams 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2556312e0c24SDan Williams 	u16 tci = ihost->tci_pool[head];
2557312e0c24SDan Williams 
2558312e0c24SDan Williams 	ihost->tci_head = head + 1;
2559312e0c24SDan Williams 	return tci;
2560312e0c24SDan Williams }
2561312e0c24SDan Williams 
2562312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost)
2563312e0c24SDan Williams {
2564312e0c24SDan Williams 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2565312e0c24SDan Williams }
2566312e0c24SDan Williams 
2567312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost)
2568312e0c24SDan Williams {
2569312e0c24SDan Williams 	if (isci_tci_space(ihost)) {
2570312e0c24SDan Williams 		u16 tci = isci_tci_alloc(ihost);
2571d9dcb4baSDan Williams 		u8 seq = ihost->io_request_sequence[tci];
2572312e0c24SDan Williams 
2573312e0c24SDan Williams 		return ISCI_TAG(seq, tci);
2574312e0c24SDan Williams 	}
2575312e0c24SDan Williams 
2576312e0c24SDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
2577312e0c24SDan Williams }
2578312e0c24SDan Williams 
2579312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2580312e0c24SDan Williams {
2581312e0c24SDan Williams 	u16 tci = ISCI_TAG_TCI(io_tag);
2582312e0c24SDan Williams 	u16 seq = ISCI_TAG_SEQ(io_tag);
2583312e0c24SDan Williams 
2584312e0c24SDan Williams 	/* prevent tail from passing head */
2585312e0c24SDan Williams 	if (isci_tci_active(ihost) == 0)
2586312e0c24SDan Williams 		return SCI_FAILURE_INVALID_IO_TAG;
2587312e0c24SDan Williams 
2588d9dcb4baSDan Williams 	if (seq == ihost->io_request_sequence[tci]) {
2589d9dcb4baSDan Williams 		ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2590312e0c24SDan Williams 
2591312e0c24SDan Williams 		isci_tci_free(ihost, tci);
2592312e0c24SDan Williams 
2593312e0c24SDan Williams 		return SCI_SUCCESS;
2594312e0c24SDan Williams 	}
2595312e0c24SDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
2596312e0c24SDan Williams }
2597312e0c24SDan Williams 
259889a7301fSDan Williams enum sci_status sci_controller_start_io(struct isci_host *ihost,
259978a6f06eSDan Williams 					struct isci_remote_device *idev,
26005076a1a9SDan Williams 					struct isci_request *ireq)
2601cc9203bfSDan Williams {
2602cc9203bfSDan Williams 	enum sci_status status;
2603cc9203bfSDan Williams 
2604d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
260514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
260614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2607cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2608cc9203bfSDan Williams 	}
2609cc9203bfSDan Williams 
261089a7301fSDan Williams 	status = sci_remote_device_start_io(ihost, idev, ireq);
2611cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2612cc9203bfSDan Williams 		return status;
2613cc9203bfSDan Williams 
26145076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
261534a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2616cc9203bfSDan Williams 	return SCI_SUCCESS;
2617cc9203bfSDan Williams }
2618cc9203bfSDan Williams 
261989a7301fSDan Williams enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
262078a6f06eSDan Williams 						 struct isci_remote_device *idev,
26215076a1a9SDan Williams 						 struct isci_request *ireq)
2622cc9203bfSDan Williams {
262389a7301fSDan Williams 	/* terminate an ongoing (i.e. started) core IO request.  This does not
262489a7301fSDan Williams 	 * abort the IO request at the target, but rather removes the IO
262589a7301fSDan Williams 	 * request from the host controller.
262689a7301fSDan Williams 	 */
2627cc9203bfSDan Williams 	enum sci_status status;
2628cc9203bfSDan Williams 
2629d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
263014e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
263114e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2632cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2633cc9203bfSDan Williams 	}
263489a7301fSDan Williams 	status = sci_io_request_terminate(ireq);
263514aaa9f0SJeff Skirvin 
263614aaa9f0SJeff Skirvin 	dev_dbg(&ihost->pdev->dev, "%s: status=%d; ireq=%p; flags=%lx\n",
263714aaa9f0SJeff Skirvin 		__func__, status, ireq, ireq->flags);
263814aaa9f0SJeff Skirvin 
2639726980d5SJeff Skirvin 	if ((status == SCI_SUCCESS) &&
2640726980d5SJeff Skirvin 	    !test_bit(IREQ_PENDING_ABORT, &ireq->flags) &&
2641726980d5SJeff Skirvin 	    !test_and_set_bit(IREQ_TC_ABORT_POSTED, &ireq->flags)) {
2642726980d5SJeff Skirvin 		/* Utilize the original post context command and or in the
2643726980d5SJeff Skirvin 		 * POST_TC_ABORT request sub-type.
2644cc9203bfSDan Williams 		 */
2645726980d5SJeff Skirvin 		sci_controller_post_request(
2646726980d5SJeff Skirvin 			ihost, ireq->post_context |
2647726980d5SJeff Skirvin 				SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2648726980d5SJeff Skirvin 	}
2649726980d5SJeff Skirvin 	return status;
2650cc9203bfSDan Williams }
2651cc9203bfSDan Williams 
2652cc9203bfSDan Williams /**
265389a7301fSDan Williams  * sci_controller_complete_io() - This method will perform core specific
2654cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
2655cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
2656cc9203bfSDan Williams  *    reused (i.e. re-constructed).
265789a7301fSDan Williams  * @ihost: The handle to the controller object for which to complete the
2658cc9203bfSDan Williams  *    IO request.
265989a7301fSDan Williams  * @idev: The handle to the remote device object for which to complete
2660cc9203bfSDan Williams  *    the IO request.
266189a7301fSDan Williams  * @ireq: the handle to the io request object to complete.
2662cc9203bfSDan Williams  */
266389a7301fSDan Williams enum sci_status sci_controller_complete_io(struct isci_host *ihost,
266478a6f06eSDan Williams 					   struct isci_remote_device *idev,
26655076a1a9SDan Williams 					   struct isci_request *ireq)
2666cc9203bfSDan Williams {
2667cc9203bfSDan Williams 	enum sci_status status;
2668cc9203bfSDan Williams 	u16 index;
2669cc9203bfSDan Williams 
2670d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2671e301370aSEdmund Nadolski 	case SCIC_STOPPING:
2672cc9203bfSDan Williams 		/* XXX: Implement this function */
2673cc9203bfSDan Williams 		return SCI_FAILURE;
2674e301370aSEdmund Nadolski 	case SCIC_READY:
267589a7301fSDan Williams 		status = sci_remote_device_complete_io(ihost, idev, ireq);
2676cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
2677cc9203bfSDan Williams 			return status;
2678cc9203bfSDan Williams 
26795076a1a9SDan Williams 		index = ISCI_TAG_TCI(ireq->io_tag);
26805076a1a9SDan Williams 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2681cc9203bfSDan Williams 		return SCI_SUCCESS;
2682cc9203bfSDan Williams 	default:
268314e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
268414e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2685cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2686cc9203bfSDan Williams 	}
2687cc9203bfSDan Williams 
2688cc9203bfSDan Williams }
2689cc9203bfSDan Williams 
269089a7301fSDan Williams enum sci_status sci_controller_continue_io(struct isci_request *ireq)
2691cc9203bfSDan Williams {
2692d9dcb4baSDan Williams 	struct isci_host *ihost = ireq->owning_controller;
2693cc9203bfSDan Williams 
2694d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
269514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
269614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2697cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2698cc9203bfSDan Williams 	}
2699cc9203bfSDan Williams 
27005076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
270134a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2702cc9203bfSDan Williams 	return SCI_SUCCESS;
2703cc9203bfSDan Williams }
2704cc9203bfSDan Williams 
2705cc9203bfSDan Williams /**
270689a7301fSDan Williams  * sci_controller_start_task() - This method is called by the SCIC user to
2707cc9203bfSDan Williams  *    send/start a framework task management request.
2708cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
2709cc9203bfSDan Williams  *    management request.
2710cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
2711cc9203bfSDan Williams  *    the task management request.
2712cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
2713cc9203bfSDan Williams  */
271489a7301fSDan Williams enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
271578a6f06eSDan Williams 					       struct isci_remote_device *idev,
27165076a1a9SDan Williams 					       struct isci_request *ireq)
2717cc9203bfSDan Williams {
2718cc9203bfSDan Williams 	enum sci_status status;
2719cc9203bfSDan Williams 
2720d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2721d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2722cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
2723cc9203bfSDan Williams 			 "state\n",
2724cc9203bfSDan Williams 			 __func__);
2725cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
2726cc9203bfSDan Williams 	}
2727cc9203bfSDan Williams 
272889a7301fSDan Williams 	status = sci_remote_device_start_task(ihost, idev, ireq);
2729cc9203bfSDan Williams 	switch (status) {
2730cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2731db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2732cc9203bfSDan Williams 
2733cc9203bfSDan Williams 		/*
2734cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
2735cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
2736cc9203bfSDan Williams 		 * RNC is resumed.)
2737cc9203bfSDan Williams 		 */
2738cc9203bfSDan Williams 		return SCI_SUCCESS;
2739cc9203bfSDan Williams 	case SCI_SUCCESS:
2740db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
274134a99158SDan Williams 		sci_controller_post_request(ihost, ireq->post_context);
2742cc9203bfSDan Williams 		break;
2743cc9203bfSDan Williams 	default:
2744cc9203bfSDan Williams 		break;
2745cc9203bfSDan Williams 	}
2746cc9203bfSDan Williams 
2747cc9203bfSDan Williams 	return status;
2748cc9203bfSDan Williams }
2749ad4f4c1dSDan Williams 
2750ad4f4c1dSDan Williams static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
2751ad4f4c1dSDan Williams {
2752ad4f4c1dSDan Williams 	int d;
2753ad4f4c1dSDan Williams 
2754ad4f4c1dSDan Williams 	/* no support for TX_GP_CFG */
2755ad4f4c1dSDan Williams 	if (reg_index == 0)
2756ad4f4c1dSDan Williams 		return -EINVAL;
2757ad4f4c1dSDan Williams 
2758ad4f4c1dSDan Williams 	for (d = 0; d < isci_gpio_count(ihost); d++) {
2759ad4f4c1dSDan Williams 		u32 val = 0x444; /* all ODx.n clear */
2760ad4f4c1dSDan Williams 		int i;
2761ad4f4c1dSDan Williams 
2762ad4f4c1dSDan Williams 		for (i = 0; i < 3; i++) {
2763ad4f4c1dSDan Williams 			int bit = (i << 2) + 2;
2764ad4f4c1dSDan Williams 
2765ad4f4c1dSDan Williams 			bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
2766ad4f4c1dSDan Williams 						       write_data, reg_index,
2767ad4f4c1dSDan Williams 						       reg_count);
2768ad4f4c1dSDan Williams 			if (bit < 0)
2769ad4f4c1dSDan Williams 				break;
2770ad4f4c1dSDan Williams 
2771ad4f4c1dSDan Williams 			/* if od is set, clear the 'invert' bit */
2772ad4f4c1dSDan Williams 			val &= ~(bit << ((i << 2) + 2));
2773ad4f4c1dSDan Williams 		}
2774ad4f4c1dSDan Williams 
2775ad4f4c1dSDan Williams 		if (i < 3)
2776ad4f4c1dSDan Williams 			break;
2777ad4f4c1dSDan Williams 		writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
2778ad4f4c1dSDan Williams 	}
2779ad4f4c1dSDan Williams 
2780ad4f4c1dSDan Williams 	/* unless reg_index is > 1, we should always be able to write at
2781ad4f4c1dSDan Williams 	 * least one register
2782ad4f4c1dSDan Williams 	 */
2783ad4f4c1dSDan Williams 	return d > 0;
2784ad4f4c1dSDan Williams }
2785ad4f4c1dSDan Williams 
2786ad4f4c1dSDan Williams int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
2787ad4f4c1dSDan Williams 		    u8 reg_count, u8 *write_data)
2788ad4f4c1dSDan Williams {
2789ad4f4c1dSDan Williams 	struct isci_host *ihost = sas_ha->lldd_ha;
2790ad4f4c1dSDan Williams 	int written;
2791ad4f4c1dSDan Williams 
2792ad4f4c1dSDan Williams 	switch (reg_type) {
2793ad4f4c1dSDan Williams 	case SAS_GPIO_REG_TX_GP:
2794ad4f4c1dSDan Williams 		written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
2795ad4f4c1dSDan Williams 		break;
2796ad4f4c1dSDan Williams 	default:
2797ad4f4c1dSDan Williams 		written = -EINVAL;
2798ad4f4c1dSDan Williams 	}
2799ad4f4c1dSDan Williams 
2800ad4f4c1dSDan Williams 	return written;
2801ad4f4c1dSDan Williams }
2802