xref: /openbmc/linux/drivers/scsi/isci/host.c (revision 89a7301f21fb00e753089671eb9e4132aab8ea08)
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"
616f231ddaSDan Williams #include "host.h"
62d044af17SDan Williams #include "probe_roms.h"
63cc9203bfSDan Williams #include "remote_device.h"
64cc9203bfSDan Williams #include "request.h"
65cc9203bfSDan Williams #include "scu_completion_codes.h"
66cc9203bfSDan Williams #include "scu_event_codes.h"
6763a3a15fSDan Williams #include "registers.h"
68cc9203bfSDan Williams #include "scu_remote_node_context.h"
69cc9203bfSDan Williams #include "scu_task_context.h"
70cc9203bfSDan Williams #include "scu_unsolicited_frame.h"
716f231ddaSDan Williams 
72cc9203bfSDan Williams #define SCU_CONTEXT_RAM_INIT_STALL_TIME      200
73cc9203bfSDan Williams 
747c78da31SDan Williams #define smu_max_ports(dcc_value) \
75cc9203bfSDan Williams 	(\
76cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
77cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
78cc9203bfSDan Williams 	)
79cc9203bfSDan Williams 
807c78da31SDan Williams #define smu_max_task_contexts(dcc_value)	\
81cc9203bfSDan Williams 	(\
82cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
83cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
84cc9203bfSDan Williams 	)
85cc9203bfSDan Williams 
867c78da31SDan Williams #define smu_max_rncs(dcc_value) \
87cc9203bfSDan Williams 	(\
88cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
89cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
90cc9203bfSDan Williams 	)
91cc9203bfSDan Williams 
92cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT      100
93cc9203bfSDan Williams 
94cc9203bfSDan Williams /**
95cc9203bfSDan Williams  *
96cc9203bfSDan Williams  *
97cc9203bfSDan Williams  * The number of milliseconds to wait while a given phy is consuming power
98cc9203bfSDan Williams  * before allowing another set of phys to consume power. Ultimately, this will
99cc9203bfSDan Williams  * be specified by OEM parameter.
100cc9203bfSDan Williams  */
101cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
102cc9203bfSDan Williams 
103cc9203bfSDan Williams /**
104cc9203bfSDan Williams  * NORMALIZE_PUT_POINTER() -
105cc9203bfSDan Williams  *
106cc9203bfSDan Williams  * This macro will normalize the completion queue put pointer so its value can
107cc9203bfSDan Williams  * be used as an array inde
108cc9203bfSDan Williams  */
109cc9203bfSDan Williams #define NORMALIZE_PUT_POINTER(x) \
110cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
111cc9203bfSDan Williams 
112cc9203bfSDan Williams 
113cc9203bfSDan Williams /**
114cc9203bfSDan Williams  * NORMALIZE_EVENT_POINTER() -
115cc9203bfSDan Williams  *
116cc9203bfSDan Williams  * This macro will normalize the completion queue event entry so its value can
117cc9203bfSDan Williams  * be used as an index.
118cc9203bfSDan Williams  */
119cc9203bfSDan Williams #define NORMALIZE_EVENT_POINTER(x) \
120cc9203bfSDan Williams 	(\
121cc9203bfSDan Williams 		((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
122cc9203bfSDan Williams 		>> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT	\
123cc9203bfSDan Williams 	)
124cc9203bfSDan Williams 
125cc9203bfSDan Williams /**
126cc9203bfSDan Williams  * NORMALIZE_GET_POINTER() -
127cc9203bfSDan Williams  *
128cc9203bfSDan Williams  * This macro will normalize the completion queue get pointer so its value can
129cc9203bfSDan Williams  * be used as an index into an array
130cc9203bfSDan Williams  */
131cc9203bfSDan Williams #define NORMALIZE_GET_POINTER(x) \
132cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
133cc9203bfSDan Williams 
134cc9203bfSDan Williams /**
135cc9203bfSDan Williams  * NORMALIZE_GET_POINTER_CYCLE_BIT() -
136cc9203bfSDan Williams  *
137cc9203bfSDan Williams  * This macro will normalize the completion queue cycle pointer so it matches
138cc9203bfSDan Williams  * the completion queue cycle bit
139cc9203bfSDan Williams  */
140cc9203bfSDan Williams #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
141cc9203bfSDan Williams 	((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
142cc9203bfSDan Williams 
143cc9203bfSDan Williams /**
144cc9203bfSDan Williams  * COMPLETION_QUEUE_CYCLE_BIT() -
145cc9203bfSDan Williams  *
146cc9203bfSDan Williams  * This macro will return the cycle bit of the completion queue entry
147cc9203bfSDan Williams  */
148cc9203bfSDan Williams #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
149cc9203bfSDan Williams 
15012ef6544SEdmund Nadolski /* Init the state machine and call the state entry function (if any) */
15112ef6544SEdmund Nadolski void sci_init_sm(struct sci_base_state_machine *sm,
15212ef6544SEdmund Nadolski 		 const struct sci_base_state *state_table, u32 initial_state)
15312ef6544SEdmund Nadolski {
15412ef6544SEdmund Nadolski 	sci_state_transition_t handler;
15512ef6544SEdmund Nadolski 
15612ef6544SEdmund Nadolski 	sm->initial_state_id    = initial_state;
15712ef6544SEdmund Nadolski 	sm->previous_state_id   = initial_state;
15812ef6544SEdmund Nadolski 	sm->current_state_id    = initial_state;
15912ef6544SEdmund Nadolski 	sm->state_table         = state_table;
16012ef6544SEdmund Nadolski 
16112ef6544SEdmund Nadolski 	handler = sm->state_table[initial_state].enter_state;
16212ef6544SEdmund Nadolski 	if (handler)
16312ef6544SEdmund Nadolski 		handler(sm);
16412ef6544SEdmund Nadolski }
16512ef6544SEdmund Nadolski 
16612ef6544SEdmund Nadolski /* Call the state exit fn, update the current state, call the state entry fn */
16712ef6544SEdmund Nadolski void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
16812ef6544SEdmund Nadolski {
16912ef6544SEdmund Nadolski 	sci_state_transition_t handler;
17012ef6544SEdmund Nadolski 
17112ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].exit_state;
17212ef6544SEdmund Nadolski 	if (handler)
17312ef6544SEdmund Nadolski 		handler(sm);
17412ef6544SEdmund Nadolski 
17512ef6544SEdmund Nadolski 	sm->previous_state_id = sm->current_state_id;
17612ef6544SEdmund Nadolski 	sm->current_state_id = next_state;
17712ef6544SEdmund Nadolski 
17812ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].enter_state;
17912ef6544SEdmund Nadolski 	if (handler)
18012ef6544SEdmund Nadolski 		handler(sm);
18112ef6544SEdmund Nadolski }
18212ef6544SEdmund Nadolski 
183*89a7301fSDan Williams static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
184cc9203bfSDan Williams {
185d9dcb4baSDan Williams 	u32 get_value = ihost->completion_queue_get;
186cc9203bfSDan Williams 	u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
187cc9203bfSDan Williams 
188cc9203bfSDan Williams 	if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
189d9dcb4baSDan Williams 	    COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
190cc9203bfSDan Williams 		return true;
191cc9203bfSDan Williams 
192cc9203bfSDan Williams 	return false;
193cc9203bfSDan Williams }
194cc9203bfSDan Williams 
195*89a7301fSDan Williams static bool sci_controller_isr(struct isci_host *ihost)
196cc9203bfSDan Williams {
197*89a7301fSDan Williams 	if (sci_controller_completion_queue_has_entries(ihost)) {
198cc9203bfSDan Williams 		return true;
199cc9203bfSDan Williams 	} else {
200cc9203bfSDan Williams 		/*
201cc9203bfSDan Williams 		 * we have a spurious interrupt it could be that we have already
202cc9203bfSDan Williams 		 * emptied the completion queue from a previous interrupt */
203d9dcb4baSDan Williams 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
204cc9203bfSDan Williams 
205cc9203bfSDan Williams 		/*
206cc9203bfSDan Williams 		 * There is a race in the hardware that could cause us not to be notified
207cc9203bfSDan Williams 		 * of an interrupt completion if we do not take this step.  We will mask
208cc9203bfSDan Williams 		 * then unmask the interrupts so if there is another interrupt pending
209cc9203bfSDan Williams 		 * the clearing of the interrupt source we get the next interrupt message. */
210d9dcb4baSDan Williams 		writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
211d9dcb4baSDan Williams 		writel(0, &ihost->smu_registers->interrupt_mask);
212cc9203bfSDan Williams 	}
213cc9203bfSDan Williams 
214cc9203bfSDan Williams 	return false;
215cc9203bfSDan Williams }
216cc9203bfSDan Williams 
217c7ef4031SDan Williams irqreturn_t isci_msix_isr(int vec, void *data)
2186f231ddaSDan Williams {
219c7ef4031SDan Williams 	struct isci_host *ihost = data;
2206f231ddaSDan Williams 
221*89a7301fSDan Williams 	if (sci_controller_isr(ihost))
222c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
2236f231ddaSDan Williams 
224c7ef4031SDan Williams 	return IRQ_HANDLED;
225c7ef4031SDan Williams }
226c7ef4031SDan Williams 
227*89a7301fSDan Williams static bool sci_controller_error_isr(struct isci_host *ihost)
228cc9203bfSDan Williams {
229cc9203bfSDan Williams 	u32 interrupt_status;
230cc9203bfSDan Williams 
231cc9203bfSDan Williams 	interrupt_status =
232d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
233cc9203bfSDan Williams 	interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
234cc9203bfSDan Williams 
235cc9203bfSDan Williams 	if (interrupt_status != 0) {
236cc9203bfSDan Williams 		/*
237cc9203bfSDan Williams 		 * There is an error interrupt pending so let it through and handle
238cc9203bfSDan Williams 		 * in the callback */
239cc9203bfSDan Williams 		return true;
240cc9203bfSDan Williams 	}
241cc9203bfSDan Williams 
242cc9203bfSDan Williams 	/*
243cc9203bfSDan Williams 	 * There is a race in the hardware that could cause us not to be notified
244cc9203bfSDan Williams 	 * of an interrupt completion if we do not take this step.  We will mask
245cc9203bfSDan Williams 	 * then unmask the error interrupts so if there was another interrupt
246cc9203bfSDan Williams 	 * pending we will be notified.
247cc9203bfSDan Williams 	 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
248d9dcb4baSDan Williams 	writel(0xff, &ihost->smu_registers->interrupt_mask);
249d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
250cc9203bfSDan Williams 
251cc9203bfSDan Williams 	return false;
252cc9203bfSDan Williams }
253cc9203bfSDan Williams 
254*89a7301fSDan Williams static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
255cc9203bfSDan Williams {
256*89a7301fSDan Williams 	u32 index = SCU_GET_COMPLETION_INDEX(ent);
257db056250SDan Williams 	struct isci_request *ireq = ihost->reqs[index];
258cc9203bfSDan Williams 
259cc9203bfSDan Williams 	/* Make sure that we really want to process this IO request */
260db056250SDan Williams 	if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
2615076a1a9SDan Williams 	    ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
262d9dcb4baSDan Williams 	    ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
263*89a7301fSDan Williams 		/* Yep this is a valid io request pass it along to the
264*89a7301fSDan Williams 		 * io request handler
265*89a7301fSDan Williams 		 */
266*89a7301fSDan Williams 		sci_io_request_tc_completion(ireq, ent);
267cc9203bfSDan Williams }
268cc9203bfSDan Williams 
269*89a7301fSDan Williams static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
270cc9203bfSDan Williams {
271cc9203bfSDan Williams 	u32 index;
2725076a1a9SDan Williams 	struct isci_request *ireq;
27378a6f06eSDan Williams 	struct isci_remote_device *idev;
274cc9203bfSDan Williams 
275*89a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
276cc9203bfSDan Williams 
277*89a7301fSDan Williams 	switch (scu_get_command_request_type(ent)) {
278cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
279cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
280d9dcb4baSDan Williams 		ireq = ihost->reqs[index];
281d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
282*89a7301fSDan Williams 			 __func__, ent, ireq);
283cc9203bfSDan Williams 		/* @todo For a post TC operation we need to fail the IO
284cc9203bfSDan Williams 		 * request
285cc9203bfSDan Williams 		 */
286cc9203bfSDan Williams 		break;
287cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
288cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
289cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
290d9dcb4baSDan Williams 		idev = ihost->device_table[index];
291d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
292*89a7301fSDan Williams 			 __func__, ent, idev);
293cc9203bfSDan Williams 		/* @todo For a port RNC operation we need to fail the
294cc9203bfSDan Williams 		 * device
295cc9203bfSDan Williams 		 */
296cc9203bfSDan Williams 		break;
297cc9203bfSDan Williams 	default:
298d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
299*89a7301fSDan Williams 			 __func__, ent);
300cc9203bfSDan Williams 		break;
301cc9203bfSDan Williams 	}
302cc9203bfSDan Williams }
303cc9203bfSDan Williams 
304*89a7301fSDan Williams static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
305cc9203bfSDan Williams {
306cc9203bfSDan Williams 	u32 index;
307cc9203bfSDan Williams 	u32 frame_index;
308cc9203bfSDan Williams 
309cc9203bfSDan Williams 	struct scu_unsolicited_frame_header *frame_header;
31085280955SDan Williams 	struct isci_phy *iphy;
31178a6f06eSDan Williams 	struct isci_remote_device *idev;
312cc9203bfSDan Williams 
313cc9203bfSDan Williams 	enum sci_status result = SCI_FAILURE;
314cc9203bfSDan Williams 
315*89a7301fSDan Williams 	frame_index = SCU_GET_FRAME_INDEX(ent);
316cc9203bfSDan Williams 
317d9dcb4baSDan Williams 	frame_header = ihost->uf_control.buffers.array[frame_index].header;
318d9dcb4baSDan Williams 	ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
319cc9203bfSDan Williams 
320*89a7301fSDan Williams 	if (SCU_GET_FRAME_ERROR(ent)) {
321cc9203bfSDan Williams 		/*
322cc9203bfSDan Williams 		 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
323cc9203bfSDan Williams 		 * /       this cause a problem? We expect the phy initialization will
324cc9203bfSDan Williams 		 * /       fail if there is an error in the frame. */
325*89a7301fSDan Williams 		sci_controller_release_frame(ihost, frame_index);
326cc9203bfSDan Williams 		return;
327cc9203bfSDan Williams 	}
328cc9203bfSDan Williams 
329cc9203bfSDan Williams 	if (frame_header->is_address_frame) {
330*89a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
33185280955SDan Williams 		iphy = &ihost->phys[index];
332*89a7301fSDan Williams 		result = sci_phy_frame_handler(iphy, frame_index);
333cc9203bfSDan Williams 	} else {
334cc9203bfSDan Williams 
335*89a7301fSDan Williams 		index = SCU_GET_COMPLETION_INDEX(ent);
336cc9203bfSDan Williams 
337cc9203bfSDan Williams 		if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
338cc9203bfSDan Williams 			/*
339cc9203bfSDan Williams 			 * This is a signature fis or a frame from a direct attached SATA
340cc9203bfSDan Williams 			 * device that has not yet been created.  In either case forwared
341cc9203bfSDan Williams 			 * the frame to the PE and let it take care of the frame data. */
342*89a7301fSDan Williams 			index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
34385280955SDan Williams 			iphy = &ihost->phys[index];
344*89a7301fSDan Williams 			result = sci_phy_frame_handler(iphy, frame_index);
345cc9203bfSDan Williams 		} else {
346d9dcb4baSDan Williams 			if (index < ihost->remote_node_entries)
347d9dcb4baSDan Williams 				idev = ihost->device_table[index];
348cc9203bfSDan Williams 			else
34978a6f06eSDan Williams 				idev = NULL;
350cc9203bfSDan Williams 
35178a6f06eSDan Williams 			if (idev != NULL)
352*89a7301fSDan Williams 				result = sci_remote_device_frame_handler(idev, frame_index);
353cc9203bfSDan Williams 			else
354*89a7301fSDan Williams 				sci_controller_release_frame(ihost, frame_index);
355cc9203bfSDan Williams 		}
356cc9203bfSDan Williams 	}
357cc9203bfSDan Williams 
358cc9203bfSDan Williams 	if (result != SCI_SUCCESS) {
359cc9203bfSDan Williams 		/*
360cc9203bfSDan Williams 		 * / @todo Is there any reason to report some additional error message
361cc9203bfSDan Williams 		 * /       when we get this failure notifiction? */
362cc9203bfSDan Williams 	}
363cc9203bfSDan Williams }
364cc9203bfSDan Williams 
365*89a7301fSDan Williams static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
366cc9203bfSDan Williams {
36778a6f06eSDan Williams 	struct isci_remote_device *idev;
3685076a1a9SDan Williams 	struct isci_request *ireq;
36985280955SDan Williams 	struct isci_phy *iphy;
370cc9203bfSDan Williams 	u32 index;
371cc9203bfSDan Williams 
372*89a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
373cc9203bfSDan Williams 
374*89a7301fSDan Williams 	switch (scu_get_event_type(ent)) {
375cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
376cc9203bfSDan Williams 		/* / @todo The driver did something wrong and we need to fix the condtion. */
377d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
378cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received SMU command error "
379cc9203bfSDan Williams 			"0x%x\n",
380cc9203bfSDan Williams 			__func__,
381d9dcb4baSDan Williams 			ihost,
382*89a7301fSDan Williams 			ent);
383cc9203bfSDan Williams 		break;
384cc9203bfSDan Williams 
385cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
386cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_ERROR:
387cc9203bfSDan Williams 	case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
388cc9203bfSDan Williams 		/*
389cc9203bfSDan Williams 		 * / @todo This is a hardware failure and its likely that we want to
390cc9203bfSDan Williams 		 * /       reset the controller. */
391d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
392cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received fatal controller "
393cc9203bfSDan Williams 			"event  0x%x\n",
394cc9203bfSDan Williams 			__func__,
395d9dcb4baSDan Williams 			ihost,
396*89a7301fSDan Williams 			ent);
397cc9203bfSDan Williams 		break;
398cc9203bfSDan Williams 
399cc9203bfSDan Williams 	case SCU_EVENT_TYPE_TRANSPORT_ERROR:
4005076a1a9SDan Williams 		ireq = ihost->reqs[index];
401*89a7301fSDan Williams 		sci_io_request_event_handler(ireq, ent);
402cc9203bfSDan Williams 		break;
403cc9203bfSDan Williams 
404cc9203bfSDan Williams 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
405*89a7301fSDan Williams 		switch (scu_get_event_specifier(ent)) {
406cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
407cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
4085076a1a9SDan Williams 			ireq = ihost->reqs[index];
4095076a1a9SDan Williams 			if (ireq != NULL)
410*89a7301fSDan Williams 				sci_io_request_event_handler(ireq, ent);
411cc9203bfSDan Williams 			else
412d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
413cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
414cc9203bfSDan Williams 					 "event 0x%x for io request object "
415cc9203bfSDan Williams 					 "that doesnt exist.\n",
416cc9203bfSDan Williams 					 __func__,
417d9dcb4baSDan Williams 					 ihost,
418*89a7301fSDan Williams 					 ent);
419cc9203bfSDan Williams 
420cc9203bfSDan Williams 			break;
421cc9203bfSDan Williams 
422cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
423d9dcb4baSDan Williams 			idev = ihost->device_table[index];
42478a6f06eSDan Williams 			if (idev != NULL)
425*89a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
426cc9203bfSDan Williams 			else
427d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
428cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
429cc9203bfSDan Williams 					 "event 0x%x for remote device object "
430cc9203bfSDan Williams 					 "that doesnt exist.\n",
431cc9203bfSDan Williams 					 __func__,
432d9dcb4baSDan Williams 					 ihost,
433*89a7301fSDan Williams 					 ent);
434cc9203bfSDan Williams 
435cc9203bfSDan Williams 			break;
436cc9203bfSDan Williams 		}
437cc9203bfSDan Williams 		break;
438cc9203bfSDan Williams 
439cc9203bfSDan Williams 	case SCU_EVENT_TYPE_BROADCAST_CHANGE:
440cc9203bfSDan Williams 	/*
441cc9203bfSDan Williams 	 * direct the broadcast change event to the phy first and then let
442cc9203bfSDan Williams 	 * the phy redirect the broadcast change to the port object */
443cc9203bfSDan Williams 	case SCU_EVENT_TYPE_ERR_CNT_EVENT:
444cc9203bfSDan Williams 	/*
445cc9203bfSDan Williams 	 * direct error counter event to the phy object since that is where
446cc9203bfSDan Williams 	 * we get the event notification.  This is a type 4 event. */
447cc9203bfSDan Williams 	case SCU_EVENT_TYPE_OSSP_EVENT:
448*89a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
44985280955SDan Williams 		iphy = &ihost->phys[index];
450*89a7301fSDan Williams 		sci_phy_event_handler(iphy, ent);
451cc9203bfSDan Williams 		break;
452cc9203bfSDan Williams 
453cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
454cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
455cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
456d9dcb4baSDan Williams 		if (index < ihost->remote_node_entries) {
457d9dcb4baSDan Williams 			idev = ihost->device_table[index];
458cc9203bfSDan Williams 
45978a6f06eSDan Williams 			if (idev != NULL)
460*89a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
461cc9203bfSDan Williams 		} else
462d9dcb4baSDan Williams 			dev_err(&ihost->pdev->dev,
463cc9203bfSDan Williams 				"%s: SCIC Controller 0x%p received event 0x%x "
464cc9203bfSDan Williams 				"for remote device object 0x%0x that doesnt "
465cc9203bfSDan Williams 				"exist.\n",
466cc9203bfSDan Williams 				__func__,
467d9dcb4baSDan Williams 				ihost,
468*89a7301fSDan Williams 				ent,
469cc9203bfSDan Williams 				index);
470cc9203bfSDan Williams 
471cc9203bfSDan Williams 		break;
472cc9203bfSDan Williams 
473cc9203bfSDan Williams 	default:
474d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
475cc9203bfSDan Williams 			 "%s: SCIC Controller received unknown event code %x\n",
476cc9203bfSDan Williams 			 __func__,
477*89a7301fSDan Williams 			 ent);
478cc9203bfSDan Williams 		break;
479cc9203bfSDan Williams 	}
480cc9203bfSDan Williams }
481cc9203bfSDan Williams 
482*89a7301fSDan Williams static void sci_controller_process_completions(struct isci_host *ihost)
483cc9203bfSDan Williams {
484cc9203bfSDan Williams 	u32 completion_count = 0;
485*89a7301fSDan Williams 	u32 ent;
486cc9203bfSDan Williams 	u32 get_index;
487cc9203bfSDan Williams 	u32 get_cycle;
488994a9303SDan Williams 	u32 event_get;
489cc9203bfSDan Williams 	u32 event_cycle;
490cc9203bfSDan Williams 
491d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
492cc9203bfSDan Williams 		"%s: completion queue begining get:0x%08x\n",
493cc9203bfSDan Williams 		__func__,
494d9dcb4baSDan Williams 		ihost->completion_queue_get);
495cc9203bfSDan Williams 
496cc9203bfSDan Williams 	/* Get the component parts of the completion queue */
497d9dcb4baSDan Williams 	get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
498d9dcb4baSDan Williams 	get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
499cc9203bfSDan Williams 
500d9dcb4baSDan Williams 	event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
501d9dcb4baSDan Williams 	event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
502cc9203bfSDan Williams 
503cc9203bfSDan Williams 	while (
504cc9203bfSDan Williams 		NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
505d9dcb4baSDan Williams 		== COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
506cc9203bfSDan Williams 		) {
507cc9203bfSDan Williams 		completion_count++;
508cc9203bfSDan Williams 
509*89a7301fSDan Williams 		ent = ihost->completion_queue[get_index];
510994a9303SDan Williams 
511994a9303SDan Williams 		/* increment the get pointer and check for rollover to toggle the cycle bit */
512994a9303SDan Williams 		get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
513994a9303SDan Williams 			     (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
514994a9303SDan Williams 		get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
515cc9203bfSDan Williams 
516d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
517cc9203bfSDan Williams 			"%s: completion queue entry:0x%08x\n",
518cc9203bfSDan Williams 			__func__,
519*89a7301fSDan Williams 			ent);
520cc9203bfSDan Williams 
521*89a7301fSDan Williams 		switch (SCU_GET_COMPLETION_TYPE(ent)) {
522cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_TASK:
523*89a7301fSDan Williams 			sci_controller_task_completion(ihost, ent);
524cc9203bfSDan Williams 			break;
525cc9203bfSDan Williams 
526cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_SDMA:
527*89a7301fSDan Williams 			sci_controller_sdma_completion(ihost, ent);
528cc9203bfSDan Williams 			break;
529cc9203bfSDan Williams 
530cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_UFI:
531*89a7301fSDan Williams 			sci_controller_unsolicited_frame(ihost, ent);
532cc9203bfSDan Williams 			break;
533cc9203bfSDan Williams 
534cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_EVENT:
535994a9303SDan Williams 		case SCU_COMPLETION_TYPE_NOTIFY: {
536994a9303SDan Williams 			event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
537994a9303SDan Williams 				       (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
538994a9303SDan Williams 			event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
539994a9303SDan Williams 
540*89a7301fSDan Williams 			sci_controller_event_completion(ihost, ent);
541cc9203bfSDan Williams 			break;
542994a9303SDan Williams 		}
543cc9203bfSDan Williams 		default:
544d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
545cc9203bfSDan Williams 				 "%s: SCIC Controller received unknown "
546cc9203bfSDan Williams 				 "completion type %x\n",
547cc9203bfSDan Williams 				 __func__,
548*89a7301fSDan Williams 				 ent);
549cc9203bfSDan Williams 			break;
550cc9203bfSDan Williams 		}
551cc9203bfSDan Williams 	}
552cc9203bfSDan Williams 
553cc9203bfSDan Williams 	/* Update the get register if we completed one or more entries */
554cc9203bfSDan Williams 	if (completion_count > 0) {
555d9dcb4baSDan Williams 		ihost->completion_queue_get =
556cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(ENABLE) |
557cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
558cc9203bfSDan Williams 			event_cycle |
559994a9303SDan Williams 			SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
560cc9203bfSDan Williams 			get_cycle |
561cc9203bfSDan Williams 			SMU_CQGR_GEN_VAL(POINTER, get_index);
562cc9203bfSDan Williams 
563d9dcb4baSDan Williams 		writel(ihost->completion_queue_get,
564d9dcb4baSDan Williams 		       &ihost->smu_registers->completion_queue_get);
565cc9203bfSDan Williams 
566cc9203bfSDan Williams 	}
567cc9203bfSDan Williams 
568d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
569cc9203bfSDan Williams 		"%s: completion queue ending get:0x%08x\n",
570cc9203bfSDan Williams 		__func__,
571d9dcb4baSDan Williams 		ihost->completion_queue_get);
572cc9203bfSDan Williams 
573cc9203bfSDan Williams }
574cc9203bfSDan Williams 
575*89a7301fSDan Williams static void sci_controller_error_handler(struct isci_host *ihost)
576cc9203bfSDan Williams {
577cc9203bfSDan Williams 	u32 interrupt_status;
578cc9203bfSDan Williams 
579cc9203bfSDan Williams 	interrupt_status =
580d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
581cc9203bfSDan Williams 
582cc9203bfSDan Williams 	if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
583*89a7301fSDan Williams 	    sci_controller_completion_queue_has_entries(ihost)) {
584cc9203bfSDan Williams 
585*89a7301fSDan Williams 		sci_controller_process_completions(ihost);
586d9dcb4baSDan Williams 		writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
587cc9203bfSDan Williams 	} else {
588d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
589cc9203bfSDan Williams 			interrupt_status);
590cc9203bfSDan Williams 
591d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_FAILED);
592cc9203bfSDan Williams 
593cc9203bfSDan Williams 		return;
594cc9203bfSDan Williams 	}
595cc9203bfSDan Williams 
596cc9203bfSDan Williams 	/* If we dont process any completions I am not sure that we want to do this.
597cc9203bfSDan Williams 	 * We are in the middle of a hardware fault and should probably be reset.
598cc9203bfSDan Williams 	 */
599d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
600cc9203bfSDan Williams }
601cc9203bfSDan Williams 
602c7ef4031SDan Williams irqreturn_t isci_intx_isr(int vec, void *data)
6036f231ddaSDan Williams {
6046f231ddaSDan Williams 	irqreturn_t ret = IRQ_NONE;
60531e824edSDan Williams 	struct isci_host *ihost = data;
6066f231ddaSDan Williams 
607*89a7301fSDan Williams 	if (sci_controller_isr(ihost)) {
608d9dcb4baSDan Williams 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
609c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
6106f231ddaSDan Williams 		ret = IRQ_HANDLED;
611*89a7301fSDan Williams 	} else if (sci_controller_error_isr(ihost)) {
61292f4f0f5SDan Williams 		spin_lock(&ihost->scic_lock);
613*89a7301fSDan Williams 		sci_controller_error_handler(ihost);
61492f4f0f5SDan Williams 		spin_unlock(&ihost->scic_lock);
61592f4f0f5SDan Williams 		ret = IRQ_HANDLED;
6166f231ddaSDan Williams 	}
61792f4f0f5SDan Williams 
6186f231ddaSDan Williams 	return ret;
6196f231ddaSDan Williams }
6206f231ddaSDan Williams 
62192f4f0f5SDan Williams irqreturn_t isci_error_isr(int vec, void *data)
62292f4f0f5SDan Williams {
62392f4f0f5SDan Williams 	struct isci_host *ihost = data;
62492f4f0f5SDan Williams 
625*89a7301fSDan Williams 	if (sci_controller_error_isr(ihost))
626*89a7301fSDan Williams 		sci_controller_error_handler(ihost);
62792f4f0f5SDan Williams 
62892f4f0f5SDan Williams 	return IRQ_HANDLED;
62992f4f0f5SDan Williams }
6306f231ddaSDan Williams 
6316f231ddaSDan Williams /**
6326f231ddaSDan Williams  * isci_host_start_complete() - This function is called by the core library,
6336f231ddaSDan Williams  *    through the ISCI Module, to indicate controller start status.
6346f231ddaSDan Williams  * @isci_host: This parameter specifies the ISCI host object
6356f231ddaSDan Williams  * @completion_status: This parameter specifies the completion status from the
6366f231ddaSDan Williams  *    core library.
6376f231ddaSDan Williams  *
6386f231ddaSDan Williams  */
639cc9203bfSDan Williams static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
6406f231ddaSDan Williams {
6410cf89d1dSDan Williams 	if (completion_status != SCI_SUCCESS)
6420cf89d1dSDan Williams 		dev_info(&ihost->pdev->dev,
6430cf89d1dSDan Williams 			"controller start timed out, continuing...\n");
6440cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_ready);
6450cf89d1dSDan Williams 	clear_bit(IHOST_START_PENDING, &ihost->flags);
6460cf89d1dSDan Williams 	wake_up(&ihost->eventq);
6476f231ddaSDan Williams }
6486f231ddaSDan Williams 
649c7ef4031SDan Williams int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
6506f231ddaSDan Williams {
6514393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
6526f231ddaSDan Williams 
65377950f51SEdmund Nadolski 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
6546f231ddaSDan Williams 		return 0;
6556f231ddaSDan Williams 
65677950f51SEdmund Nadolski 	/* todo: use sas_flush_discovery once it is upstream */
65777950f51SEdmund Nadolski 	scsi_flush_work(shost);
65877950f51SEdmund Nadolski 
65977950f51SEdmund Nadolski 	scsi_flush_work(shost);
6606f231ddaSDan Williams 
6610cf89d1dSDan Williams 	dev_dbg(&ihost->pdev->dev,
6620cf89d1dSDan Williams 		"%s: ihost->status = %d, time = %ld\n",
6630cf89d1dSDan Williams 		 __func__, isci_host_get_state(ihost), time);
6646f231ddaSDan Williams 
6656f231ddaSDan Williams 	return 1;
6666f231ddaSDan Williams 
6676f231ddaSDan Williams }
6686f231ddaSDan Williams 
669cc9203bfSDan Williams /**
670*89a7301fSDan Williams  * sci_controller_get_suggested_start_timeout() - This method returns the
671*89a7301fSDan Williams  *    suggested sci_controller_start() timeout amount.  The user is free to
672cc9203bfSDan Williams  *    use any timeout value, but this method provides the suggested minimum
673cc9203bfSDan Williams  *    start timeout value.  The returned value is based upon empirical
674cc9203bfSDan Williams  *    information determined as a result of interoperability testing.
675cc9203bfSDan Williams  * @controller: the handle to the controller object for which to return the
676cc9203bfSDan Williams  *    suggested start timeout.
677cc9203bfSDan Williams  *
678cc9203bfSDan Williams  * This method returns the number of milliseconds for the suggested start
679cc9203bfSDan Williams  * operation timeout.
680cc9203bfSDan Williams  */
681*89a7301fSDan Williams static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
682cc9203bfSDan Williams {
683cc9203bfSDan Williams 	/* Validate the user supplied parameters. */
684d9dcb4baSDan Williams 	if (!ihost)
685cc9203bfSDan Williams 		return 0;
686cc9203bfSDan Williams 
687cc9203bfSDan Williams 	/*
688cc9203bfSDan Williams 	 * The suggested minimum timeout value for a controller start operation:
689cc9203bfSDan Williams 	 *
690cc9203bfSDan Williams 	 *     Signature FIS Timeout
691cc9203bfSDan Williams 	 *   + Phy Start Timeout
692cc9203bfSDan Williams 	 *   + Number of Phy Spin Up Intervals
693cc9203bfSDan Williams 	 *   ---------------------------------
694cc9203bfSDan Williams 	 *   Number of milliseconds for the controller start operation.
695cc9203bfSDan Williams 	 *
696cc9203bfSDan Williams 	 * NOTE: The number of phy spin up intervals will be equivalent
697cc9203bfSDan Williams 	 *       to the number of phys divided by the number phys allowed
698cc9203bfSDan Williams 	 *       per interval - 1 (once OEM parameters are supported).
699cc9203bfSDan Williams 	 *       Currently we assume only 1 phy per interval. */
700cc9203bfSDan Williams 
701cc9203bfSDan Williams 	return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
702cc9203bfSDan Williams 		+ SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
703cc9203bfSDan Williams 		+ ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
704cc9203bfSDan Williams }
705cc9203bfSDan Williams 
706*89a7301fSDan Williams static void sci_controller_enable_interrupts(struct isci_host *ihost)
707cc9203bfSDan Williams {
708d9dcb4baSDan Williams 	BUG_ON(ihost->smu_registers == NULL);
709d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
710cc9203bfSDan Williams }
711cc9203bfSDan Williams 
712*89a7301fSDan Williams void sci_controller_disable_interrupts(struct isci_host *ihost)
713cc9203bfSDan Williams {
714d9dcb4baSDan Williams 	BUG_ON(ihost->smu_registers == NULL);
715d9dcb4baSDan Williams 	writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
716cc9203bfSDan Williams }
717cc9203bfSDan Williams 
718*89a7301fSDan Williams static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
719cc9203bfSDan Williams {
720cc9203bfSDan Williams 	u32 port_task_scheduler_value;
721cc9203bfSDan Williams 
722cc9203bfSDan Williams 	port_task_scheduler_value =
723d9dcb4baSDan Williams 		readl(&ihost->scu_registers->peg0.ptsg.control);
724cc9203bfSDan Williams 	port_task_scheduler_value |=
725cc9203bfSDan Williams 		(SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
726cc9203bfSDan Williams 		 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
727cc9203bfSDan Williams 	writel(port_task_scheduler_value,
728d9dcb4baSDan Williams 	       &ihost->scu_registers->peg0.ptsg.control);
729cc9203bfSDan Williams }
730cc9203bfSDan Williams 
731*89a7301fSDan Williams static void sci_controller_assign_task_entries(struct isci_host *ihost)
732cc9203bfSDan Williams {
733cc9203bfSDan Williams 	u32 task_assignment;
734cc9203bfSDan Williams 
735cc9203bfSDan Williams 	/*
736cc9203bfSDan Williams 	 * Assign all the TCs to function 0
737cc9203bfSDan Williams 	 * TODO: Do we actually need to read this register to write it back?
738cc9203bfSDan Williams 	 */
739cc9203bfSDan Williams 
740cc9203bfSDan Williams 	task_assignment =
741d9dcb4baSDan Williams 		readl(&ihost->smu_registers->task_context_assignment[0]);
742cc9203bfSDan Williams 
743cc9203bfSDan Williams 	task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
744d9dcb4baSDan Williams 		(SMU_TCA_GEN_VAL(ENDING,  ihost->task_context_entries - 1)) |
745cc9203bfSDan Williams 		(SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
746cc9203bfSDan Williams 
747cc9203bfSDan Williams 	writel(task_assignment,
748d9dcb4baSDan Williams 		&ihost->smu_registers->task_context_assignment[0]);
749cc9203bfSDan Williams 
750cc9203bfSDan Williams }
751cc9203bfSDan Williams 
752*89a7301fSDan Williams static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
753cc9203bfSDan Williams {
754cc9203bfSDan Williams 	u32 index;
755cc9203bfSDan Williams 	u32 completion_queue_control_value;
756cc9203bfSDan Williams 	u32 completion_queue_get_value;
757cc9203bfSDan Williams 	u32 completion_queue_put_value;
758cc9203bfSDan Williams 
759d9dcb4baSDan Williams 	ihost->completion_queue_get = 0;
760cc9203bfSDan Williams 
7617c78da31SDan Williams 	completion_queue_control_value =
7627c78da31SDan Williams 		(SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
7637c78da31SDan Williams 		 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
764cc9203bfSDan Williams 
765cc9203bfSDan Williams 	writel(completion_queue_control_value,
766d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_control);
767cc9203bfSDan Williams 
768cc9203bfSDan Williams 
769cc9203bfSDan Williams 	/* Set the completion queue get pointer and enable the queue */
770cc9203bfSDan Williams 	completion_queue_get_value = (
771cc9203bfSDan Williams 		(SMU_CQGR_GEN_VAL(POINTER, 0))
772cc9203bfSDan Williams 		| (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
773cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(ENABLE))
774cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
775cc9203bfSDan Williams 		);
776cc9203bfSDan Williams 
777cc9203bfSDan Williams 	writel(completion_queue_get_value,
778d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_get);
779cc9203bfSDan Williams 
780cc9203bfSDan Williams 	/* Set the completion queue put pointer */
781cc9203bfSDan Williams 	completion_queue_put_value = (
782cc9203bfSDan Williams 		(SMU_CQPR_GEN_VAL(POINTER, 0))
783cc9203bfSDan Williams 		| (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
784cc9203bfSDan Williams 		);
785cc9203bfSDan Williams 
786cc9203bfSDan Williams 	writel(completion_queue_put_value,
787d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_put);
788cc9203bfSDan Williams 
789cc9203bfSDan Williams 	/* Initialize the cycle bit of the completion queue entries */
7907c78da31SDan Williams 	for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
791cc9203bfSDan Williams 		/*
792cc9203bfSDan Williams 		 * If get.cycle_bit != completion_queue.cycle_bit
793cc9203bfSDan Williams 		 * its not a valid completion queue entry
794cc9203bfSDan Williams 		 * so at system start all entries are invalid */
795d9dcb4baSDan Williams 		ihost->completion_queue[index] = 0x80000000;
796cc9203bfSDan Williams 	}
797cc9203bfSDan Williams }
798cc9203bfSDan Williams 
799*89a7301fSDan Williams static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
800cc9203bfSDan Williams {
801cc9203bfSDan Williams 	u32 frame_queue_control_value;
802cc9203bfSDan Williams 	u32 frame_queue_get_value;
803cc9203bfSDan Williams 	u32 frame_queue_put_value;
804cc9203bfSDan Williams 
805cc9203bfSDan Williams 	/* Write the queue size */
806cc9203bfSDan Williams 	frame_queue_control_value =
8077c78da31SDan Williams 		SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
808cc9203bfSDan Williams 
809cc9203bfSDan Williams 	writel(frame_queue_control_value,
810d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
811cc9203bfSDan Williams 
812cc9203bfSDan Williams 	/* Setup the get pointer for the unsolicited frame queue */
813cc9203bfSDan Williams 	frame_queue_get_value = (
814cc9203bfSDan Williams 		SCU_UFQGP_GEN_VAL(POINTER, 0)
815cc9203bfSDan Williams 		|  SCU_UFQGP_GEN_BIT(ENABLE_BIT)
816cc9203bfSDan Williams 		);
817cc9203bfSDan Williams 
818cc9203bfSDan Williams 	writel(frame_queue_get_value,
819d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
820cc9203bfSDan Williams 	/* Setup the put pointer for the unsolicited frame queue */
821cc9203bfSDan Williams 	frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
822cc9203bfSDan Williams 	writel(frame_queue_put_value,
823d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
824cc9203bfSDan Williams }
825cc9203bfSDan Williams 
826*89a7301fSDan Williams static void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
827cc9203bfSDan Williams {
828d9dcb4baSDan Williams 	if (ihost->sm.current_state_id == SCIC_STARTING) {
829cc9203bfSDan Williams 		/*
830cc9203bfSDan Williams 		 * We move into the ready state, because some of the phys/ports
831cc9203bfSDan Williams 		 * may be up and operational.
832cc9203bfSDan Williams 		 */
833d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_READY);
834cc9203bfSDan Williams 
835cc9203bfSDan Williams 		isci_host_start_complete(ihost, status);
836cc9203bfSDan Williams 	}
837cc9203bfSDan Williams }
838cc9203bfSDan Williams 
83985280955SDan Williams static bool is_phy_starting(struct isci_phy *iphy)
8404a33c525SAdam Gruchala {
841*89a7301fSDan Williams 	enum sci_phy_states state;
8424a33c525SAdam Gruchala 
84385280955SDan Williams 	state = iphy->sm.current_state_id;
8444a33c525SAdam Gruchala 	switch (state) {
845e301370aSEdmund Nadolski 	case SCI_PHY_STARTING:
846e301370aSEdmund Nadolski 	case SCI_PHY_SUB_INITIAL:
847e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
848e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_IAF_UF:
849e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
850e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
851e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
852e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_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 
861cc9203bfSDan Williams /**
862*89a7301fSDan Williams  * sci_controller_start_next_phy - start phy
863cc9203bfSDan Williams  * @scic: controller
864cc9203bfSDan Williams  *
865cc9203bfSDan Williams  * If all the phys have been started, then attempt to transition the
866cc9203bfSDan Williams  * controller to the READY state and inform the user
867*89a7301fSDan Williams  * (sci_cb_controller_start_complete()).
868cc9203bfSDan Williams  */
869*89a7301fSDan Williams static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
870cc9203bfSDan Williams {
871*89a7301fSDan Williams 	struct sci_oem_params *oem = &ihost->oem_parameters;
87285280955SDan Williams 	struct isci_phy *iphy;
873cc9203bfSDan Williams 	enum sci_status status;
874cc9203bfSDan Williams 
875cc9203bfSDan Williams 	status = SCI_SUCCESS;
876cc9203bfSDan Williams 
877d9dcb4baSDan Williams 	if (ihost->phy_startup_timer_pending)
878cc9203bfSDan Williams 		return status;
879cc9203bfSDan Williams 
880d9dcb4baSDan Williams 	if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
881cc9203bfSDan Williams 		bool is_controller_start_complete = true;
882cc9203bfSDan Williams 		u32 state;
883cc9203bfSDan Williams 		u8 index;
884cc9203bfSDan Williams 
885cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
88685280955SDan Williams 			iphy = &ihost->phys[index];
88785280955SDan Williams 			state = iphy->sm.current_state_id;
888cc9203bfSDan Williams 
88985280955SDan Williams 			if (!phy_get_non_dummy_port(iphy))
890cc9203bfSDan Williams 				continue;
891cc9203bfSDan Williams 
892cc9203bfSDan Williams 			/* The controller start operation is complete iff:
893cc9203bfSDan Williams 			 * - all links have been given an opportunity to start
894cc9203bfSDan Williams 			 * - have no indication of a connected device
895cc9203bfSDan Williams 			 * - have an indication of a connected device and it has
896cc9203bfSDan Williams 			 *   finished the link training process.
897cc9203bfSDan Williams 			 */
89885280955SDan Williams 			if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
89985280955SDan Williams 			    (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
90085280955SDan Williams 			    (iphy->is_in_link_training == true && is_phy_starting(iphy))) {
901cc9203bfSDan Williams 				is_controller_start_complete = false;
902cc9203bfSDan Williams 				break;
903cc9203bfSDan Williams 			}
904cc9203bfSDan Williams 		}
905cc9203bfSDan Williams 
906cc9203bfSDan Williams 		/*
907cc9203bfSDan Williams 		 * The controller has successfully finished the start process.
908cc9203bfSDan Williams 		 * Inform the SCI Core user and transition to the READY state. */
909cc9203bfSDan Williams 		if (is_controller_start_complete == true) {
910*89a7301fSDan Williams 			sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
911d9dcb4baSDan Williams 			sci_del_timer(&ihost->phy_timer);
912d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = false;
913cc9203bfSDan Williams 		}
914cc9203bfSDan Williams 	} else {
915d9dcb4baSDan Williams 		iphy = &ihost->phys[ihost->next_phy_to_start];
916cc9203bfSDan Williams 
917cc9203bfSDan Williams 		if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
91885280955SDan Williams 			if (phy_get_non_dummy_port(iphy) == NULL) {
919d9dcb4baSDan Williams 				ihost->next_phy_to_start++;
920cc9203bfSDan Williams 
921cc9203bfSDan Williams 				/* Caution recursion ahead be forwarned
922cc9203bfSDan Williams 				 *
923cc9203bfSDan Williams 				 * The PHY was never added to a PORT in MPC mode
924cc9203bfSDan Williams 				 * so start the next phy in sequence This phy
925cc9203bfSDan Williams 				 * will never go link up and will not draw power
926cc9203bfSDan Williams 				 * the OEM parameters either configured the phy
927cc9203bfSDan Williams 				 * incorrectly for the PORT or it was never
928cc9203bfSDan Williams 				 * assigned to a PORT
929cc9203bfSDan Williams 				 */
930*89a7301fSDan Williams 				return sci_controller_start_next_phy(ihost);
931cc9203bfSDan Williams 			}
932cc9203bfSDan Williams 		}
933cc9203bfSDan Williams 
934*89a7301fSDan Williams 		status = sci_phy_start(iphy);
935cc9203bfSDan Williams 
936cc9203bfSDan Williams 		if (status == SCI_SUCCESS) {
937d9dcb4baSDan Williams 			sci_mod_timer(&ihost->phy_timer,
938bb3dbdf6SEdmund Nadolski 				      SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
939d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = true;
940cc9203bfSDan Williams 		} else {
941d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
942cc9203bfSDan Williams 				 "%s: Controller stop operation failed "
943cc9203bfSDan Williams 				 "to stop phy %d because of status "
944cc9203bfSDan Williams 				 "%d.\n",
945cc9203bfSDan Williams 				 __func__,
946d9dcb4baSDan Williams 				 ihost->phys[ihost->next_phy_to_start].phy_index,
947cc9203bfSDan Williams 				 status);
948cc9203bfSDan Williams 		}
949cc9203bfSDan Williams 
950d9dcb4baSDan Williams 		ihost->next_phy_to_start++;
951cc9203bfSDan Williams 	}
952cc9203bfSDan Williams 
953cc9203bfSDan Williams 	return status;
954cc9203bfSDan Williams }
955cc9203bfSDan Williams 
956bb3dbdf6SEdmund Nadolski static void phy_startup_timeout(unsigned long data)
957cc9203bfSDan Williams {
958bb3dbdf6SEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
959d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
960bb3dbdf6SEdmund Nadolski 	unsigned long flags;
961cc9203bfSDan Williams 	enum sci_status status;
962cc9203bfSDan Williams 
963bb3dbdf6SEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
964bb3dbdf6SEdmund Nadolski 
965bb3dbdf6SEdmund Nadolski 	if (tmr->cancel)
966bb3dbdf6SEdmund Nadolski 		goto done;
967bb3dbdf6SEdmund Nadolski 
968d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
969bb3dbdf6SEdmund Nadolski 
970bb3dbdf6SEdmund Nadolski 	do {
971*89a7301fSDan Williams 		status = sci_controller_start_next_phy(ihost);
972bb3dbdf6SEdmund Nadolski 	} while (status != SCI_SUCCESS);
973bb3dbdf6SEdmund Nadolski 
974bb3dbdf6SEdmund Nadolski done:
975bb3dbdf6SEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
976cc9203bfSDan Williams }
977cc9203bfSDan Williams 
978ac668c69SDan Williams static u16 isci_tci_active(struct isci_host *ihost)
979ac668c69SDan Williams {
980ac668c69SDan Williams 	return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
981ac668c69SDan Williams }
982ac668c69SDan Williams 
983*89a7301fSDan Williams static enum sci_status sci_controller_start(struct isci_host *ihost,
984cc9203bfSDan Williams 					     u32 timeout)
985cc9203bfSDan Williams {
986cc9203bfSDan Williams 	enum sci_status result;
987cc9203bfSDan Williams 	u16 index;
988cc9203bfSDan Williams 
989d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
990d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
991cc9203bfSDan Williams 			 "SCIC Controller start operation requested in "
992cc9203bfSDan Williams 			 "invalid state\n");
993cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
994cc9203bfSDan Williams 	}
995cc9203bfSDan Williams 
996cc9203bfSDan Williams 	/* Build the TCi free pool */
997ac668c69SDan Williams 	BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
998ac668c69SDan Williams 	ihost->tci_head = 0;
999ac668c69SDan Williams 	ihost->tci_tail = 0;
1000d9dcb4baSDan Williams 	for (index = 0; index < ihost->task_context_entries; index++)
1001ac668c69SDan Williams 		isci_tci_free(ihost, index);
1002cc9203bfSDan Williams 
1003cc9203bfSDan Williams 	/* Build the RNi free pool */
1004*89a7301fSDan Williams 	sci_remote_node_table_initialize(&ihost->available_remote_nodes,
1005d9dcb4baSDan Williams 					 ihost->remote_node_entries);
1006cc9203bfSDan Williams 
1007cc9203bfSDan Williams 	/*
1008cc9203bfSDan Williams 	 * Before anything else lets make sure we will not be
1009cc9203bfSDan Williams 	 * interrupted by the hardware.
1010cc9203bfSDan Williams 	 */
1011*89a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1012cc9203bfSDan Williams 
1013cc9203bfSDan Williams 	/* Enable the port task scheduler */
1014*89a7301fSDan Williams 	sci_controller_enable_port_task_scheduler(ihost);
1015cc9203bfSDan Williams 
1016d9dcb4baSDan Williams 	/* Assign all the task entries to ihost physical function */
1017*89a7301fSDan Williams 	sci_controller_assign_task_entries(ihost);
1018cc9203bfSDan Williams 
1019cc9203bfSDan Williams 	/* Now initialize the completion queue */
1020*89a7301fSDan Williams 	sci_controller_initialize_completion_queue(ihost);
1021cc9203bfSDan Williams 
1022cc9203bfSDan Williams 	/* Initialize the unsolicited frame queue for use */
1023*89a7301fSDan Williams 	sci_controller_initialize_unsolicited_frame_queue(ihost);
1024cc9203bfSDan Williams 
1025cc9203bfSDan Williams 	/* Start all of the ports on this controller */
1026d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1027ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1028cc9203bfSDan Williams 
1029*89a7301fSDan Williams 		result = sci_port_start(iport);
1030cc9203bfSDan Williams 		if (result)
1031cc9203bfSDan Williams 			return result;
1032cc9203bfSDan Williams 	}
1033cc9203bfSDan Williams 
1034*89a7301fSDan Williams 	sci_controller_start_next_phy(ihost);
1035cc9203bfSDan Williams 
1036d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1037cc9203bfSDan Williams 
1038d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STARTING);
1039cc9203bfSDan Williams 
1040cc9203bfSDan Williams 	return SCI_SUCCESS;
1041cc9203bfSDan Williams }
1042cc9203bfSDan Williams 
10436f231ddaSDan Williams void isci_host_scan_start(struct Scsi_Host *shost)
10446f231ddaSDan Williams {
10454393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
1046*89a7301fSDan Williams 	unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
10476f231ddaSDan Williams 
10480cf89d1dSDan Williams 	set_bit(IHOST_START_PENDING, &ihost->flags);
104977950f51SEdmund Nadolski 
105077950f51SEdmund Nadolski 	spin_lock_irq(&ihost->scic_lock);
1051*89a7301fSDan Williams 	sci_controller_start(ihost, tmo);
1052*89a7301fSDan Williams 	sci_controller_enable_interrupts(ihost);
105377950f51SEdmund Nadolski 	spin_unlock_irq(&ihost->scic_lock);
10546f231ddaSDan Williams }
10556f231ddaSDan Williams 
1056cc9203bfSDan Williams static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
10576f231ddaSDan Williams {
10580cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopped);
1059*89a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
10600cf89d1dSDan Williams 	clear_bit(IHOST_STOP_PENDING, &ihost->flags);
10610cf89d1dSDan Williams 	wake_up(&ihost->eventq);
10626f231ddaSDan Williams }
10636f231ddaSDan Williams 
1064*89a7301fSDan Williams static void sci_controller_completion_handler(struct isci_host *ihost)
1065cc9203bfSDan Williams {
1066cc9203bfSDan Williams 	/* Empty out the completion queue */
1067*89a7301fSDan Williams 	if (sci_controller_completion_queue_has_entries(ihost))
1068*89a7301fSDan Williams 		sci_controller_process_completions(ihost);
1069cc9203bfSDan Williams 
1070cc9203bfSDan Williams 	/* Clear the interrupt and enable all interrupts again */
1071d9dcb4baSDan Williams 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
1072cc9203bfSDan Williams 	/* Could we write the value of SMU_ISR_COMPLETION? */
1073d9dcb4baSDan Williams 	writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
1074d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
1075cc9203bfSDan Williams }
1076cc9203bfSDan Williams 
10776f231ddaSDan Williams /**
10786f231ddaSDan Williams  * isci_host_completion_routine() - This function is the delayed service
10796f231ddaSDan Williams  *    routine that calls the sci core library's completion handler. It's
10806f231ddaSDan Williams  *    scheduled as a tasklet from the interrupt service routine when interrupts
10816f231ddaSDan Williams  *    in use, or set as the timeout function in polled mode.
10826f231ddaSDan Williams  * @data: This parameter specifies the ISCI host object
10836f231ddaSDan Williams  *
10846f231ddaSDan Williams  */
10856f231ddaSDan Williams static void isci_host_completion_routine(unsigned long data)
10866f231ddaSDan Williams {
1087d9dcb4baSDan Williams 	struct isci_host *ihost = (struct isci_host *)data;
10886f231ddaSDan Williams 	struct list_head    completed_request_list;
108911b00c19SJeff Skirvin 	struct list_head    errored_request_list;
10906f231ddaSDan Williams 	struct list_head    *current_position;
10916f231ddaSDan Williams 	struct list_head    *next_position;
10926f231ddaSDan Williams 	struct isci_request *request;
10936f231ddaSDan Williams 	struct isci_request *next_request;
10946f231ddaSDan Williams 	struct sas_task     *task;
10956f231ddaSDan Williams 
10966f231ddaSDan Williams 	INIT_LIST_HEAD(&completed_request_list);
109711b00c19SJeff Skirvin 	INIT_LIST_HEAD(&errored_request_list);
10986f231ddaSDan Williams 
1099d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
11006f231ddaSDan Williams 
1101*89a7301fSDan Williams 	sci_controller_completion_handler(ihost);
1102c7ef4031SDan Williams 
11036f231ddaSDan Williams 	/* Take the lists of completed I/Os from the host. */
110411b00c19SJeff Skirvin 
1105d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_complete,
11066f231ddaSDan Williams 			 &completed_request_list);
11076f231ddaSDan Williams 
110811b00c19SJeff Skirvin 	/* Take the list of errored I/Os from the host. */
1109d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_errorback,
111011b00c19SJeff Skirvin 			 &errored_request_list);
11116f231ddaSDan Williams 
1112d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
11136f231ddaSDan Williams 
11146f231ddaSDan Williams 	/* Process any completions in the lists. */
11156f231ddaSDan Williams 	list_for_each_safe(current_position, next_position,
11166f231ddaSDan Williams 			   &completed_request_list) {
11176f231ddaSDan Williams 
11186f231ddaSDan Williams 		request = list_entry(current_position, struct isci_request,
11196f231ddaSDan Williams 				     completed_node);
11206f231ddaSDan Williams 		task = isci_request_access_task(request);
11216f231ddaSDan Williams 
11226f231ddaSDan Williams 		/* Normal notification (task_done) */
1123d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
11246f231ddaSDan Williams 			"%s: Normal - request/task = %p/%p\n",
11256f231ddaSDan Williams 			__func__,
11266f231ddaSDan Williams 			request,
11276f231ddaSDan Williams 			task);
11286f231ddaSDan Williams 
112911b00c19SJeff Skirvin 		/* Return the task to libsas */
113011b00c19SJeff Skirvin 		if (task != NULL) {
11316f231ddaSDan Williams 
113211b00c19SJeff Skirvin 			task->lldd_task = NULL;
113311b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
113411b00c19SJeff Skirvin 
113511b00c19SJeff Skirvin 				/* If the task is already in the abort path,
113611b00c19SJeff Skirvin 				* the task_done callback cannot be called.
113711b00c19SJeff Skirvin 				*/
113811b00c19SJeff Skirvin 				task->task_done(task);
113911b00c19SJeff Skirvin 			}
114011b00c19SJeff Skirvin 		}
1141312e0c24SDan Williams 
1142d9dcb4baSDan Williams 		spin_lock_irq(&ihost->scic_lock);
1143d9dcb4baSDan Williams 		isci_free_tag(ihost, request->io_tag);
1144d9dcb4baSDan Williams 		spin_unlock_irq(&ihost->scic_lock);
11456f231ddaSDan Williams 	}
114611b00c19SJeff Skirvin 	list_for_each_entry_safe(request, next_request, &errored_request_list,
11476f231ddaSDan Williams 				 completed_node) {
11486f231ddaSDan Williams 
11496f231ddaSDan Williams 		task = isci_request_access_task(request);
11506f231ddaSDan Williams 
11516f231ddaSDan Williams 		/* Use sas_task_abort */
1152d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
11536f231ddaSDan Williams 			 "%s: Error - request/task = %p/%p\n",
11546f231ddaSDan Williams 			 __func__,
11556f231ddaSDan Williams 			 request,
11566f231ddaSDan Williams 			 task);
11576f231ddaSDan Williams 
115811b00c19SJeff Skirvin 		if (task != NULL) {
115911b00c19SJeff Skirvin 
116011b00c19SJeff Skirvin 			/* Put the task into the abort path if it's not there
116111b00c19SJeff Skirvin 			 * already.
116211b00c19SJeff Skirvin 			 */
116311b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
11646f231ddaSDan Williams 				sas_task_abort(task);
116511b00c19SJeff Skirvin 
116611b00c19SJeff Skirvin 		} else {
116711b00c19SJeff Skirvin 			/* This is a case where the request has completed with a
116811b00c19SJeff Skirvin 			 * status such that it needed further target servicing,
116911b00c19SJeff Skirvin 			 * but the sas_task reference has already been removed
117011b00c19SJeff Skirvin 			 * from the request.  Since it was errored, it was not
117111b00c19SJeff Skirvin 			 * being aborted, so there is nothing to do except free
117211b00c19SJeff Skirvin 			 * it.
117311b00c19SJeff Skirvin 			 */
117411b00c19SJeff Skirvin 
1175d9dcb4baSDan Williams 			spin_lock_irq(&ihost->scic_lock);
117611b00c19SJeff Skirvin 			/* Remove the request from the remote device's list
117711b00c19SJeff Skirvin 			* of pending requests.
117811b00c19SJeff Skirvin 			*/
117911b00c19SJeff Skirvin 			list_del_init(&request->dev_node);
1180d9dcb4baSDan Williams 			isci_free_tag(ihost, request->io_tag);
1181d9dcb4baSDan Williams 			spin_unlock_irq(&ihost->scic_lock);
118211b00c19SJeff Skirvin 		}
11836f231ddaSDan Williams 	}
11846f231ddaSDan Williams 
11856f231ddaSDan Williams }
11866f231ddaSDan Williams 
1187cc9203bfSDan Williams /**
1188*89a7301fSDan Williams  * sci_controller_stop() - This method will stop an individual controller
1189cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1190cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1191cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1192cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1193cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1194cc9203bfSDan Williams  *    the hardware is halted.
1195cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1196cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1197cc9203bfSDan Williams  *    stop operation should complete.
1198cc9203bfSDan Williams  *
1199cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1200cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1201cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1202cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1203cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1204cc9203bfSDan Williams  */
1205*89a7301fSDan Williams static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
1206cc9203bfSDan Williams {
1207d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
1208d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
1209cc9203bfSDan Williams 			 "SCIC Controller stop operation requested in "
1210cc9203bfSDan Williams 			 "invalid state\n");
1211cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1212cc9203bfSDan Williams 	}
1213cc9203bfSDan Williams 
1214d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1215d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STOPPING);
1216cc9203bfSDan Williams 	return SCI_SUCCESS;
1217cc9203bfSDan Williams }
1218cc9203bfSDan Williams 
1219cc9203bfSDan Williams /**
1220*89a7301fSDan Williams  * sci_controller_reset() - This method will reset the supplied core
1221cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1222cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1223cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1224cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1225cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1226cc9203bfSDan Williams  *
1227cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1228cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1229cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1230cc9203bfSDan Williams  */
1231*89a7301fSDan Williams static enum sci_status sci_controller_reset(struct isci_host *ihost)
1232cc9203bfSDan Williams {
1233d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
1234e301370aSEdmund Nadolski 	case SCIC_RESET:
1235e301370aSEdmund Nadolski 	case SCIC_READY:
1236e301370aSEdmund Nadolski 	case SCIC_STOPPED:
1237e301370aSEdmund Nadolski 	case SCIC_FAILED:
1238cc9203bfSDan Williams 		/*
1239cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1240cc9203bfSDan Williams 		 * perform the state transition.
1241cc9203bfSDan Williams 		 */
1242d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_RESETTING);
1243cc9203bfSDan Williams 		return SCI_SUCCESS;
1244cc9203bfSDan Williams 	default:
1245d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
1246cc9203bfSDan Williams 			 "SCIC Controller reset operation requested in "
1247cc9203bfSDan Williams 			 "invalid state\n");
1248cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1249cc9203bfSDan Williams 	}
1250cc9203bfSDan Williams }
1251cc9203bfSDan Williams 
12520cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
12536f231ddaSDan Williams {
12546f231ddaSDan Williams 	int i;
12556f231ddaSDan Williams 
12560cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopping);
12576f231ddaSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++) {
1258e531381eSDan Williams 		struct isci_port *iport = &ihost->ports[i];
12590cf89d1dSDan Williams 		struct isci_remote_device *idev, *d;
12600cf89d1dSDan Williams 
1261e531381eSDan Williams 		list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
1262209fae14SDan Williams 			if (test_bit(IDEV_ALLOCATED, &idev->flags))
12636ad31fecSDan Williams 				isci_remote_device_stop(ihost, idev);
12646f231ddaSDan Williams 		}
12656f231ddaSDan Williams 	}
12666f231ddaSDan Williams 
12670cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
12687c40a803SDan Williams 
12697c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
1270*89a7301fSDan Williams 	sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
12717c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
12727c40a803SDan Williams 
12730cf89d1dSDan Williams 	wait_for_stop(ihost);
1274*89a7301fSDan Williams 	sci_controller_reset(ihost);
12755553ba2bSEdmund Nadolski 
12765553ba2bSEdmund Nadolski 	/* Cancel any/all outstanding port timers */
1277d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
1278ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[i];
1279ffe191c9SDan Williams 		del_timer_sync(&iport->timer.timer);
12805553ba2bSEdmund Nadolski 	}
12815553ba2bSEdmund Nadolski 
1282a628d478SEdmund Nadolski 	/* Cancel any/all outstanding phy timers */
1283a628d478SEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
128485280955SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
128585280955SDan Williams 		del_timer_sync(&iphy->sata_timer.timer);
1286a628d478SEdmund Nadolski 	}
1287a628d478SEdmund Nadolski 
1288d9dcb4baSDan Williams 	del_timer_sync(&ihost->port_agent.timer.timer);
1289ac0eeb4fSEdmund Nadolski 
1290d9dcb4baSDan Williams 	del_timer_sync(&ihost->power_control.timer.timer);
12910473661aSEdmund Nadolski 
1292d9dcb4baSDan Williams 	del_timer_sync(&ihost->timer.timer);
12936cb5853dSEdmund Nadolski 
1294d9dcb4baSDan Williams 	del_timer_sync(&ihost->phy_timer.timer);
12956f231ddaSDan Williams }
12966f231ddaSDan Williams 
12976f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
12986f231ddaSDan Williams {
12996f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13006f231ddaSDan Williams 	int id = isci_host->id;
13016f231ddaSDan Williams 
13026f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
13036f231ddaSDan Williams }
13046f231ddaSDan Williams 
13056f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
13066f231ddaSDan Williams {
13076f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13086f231ddaSDan Williams 	int id = isci_host->id;
13096f231ddaSDan Williams 
13106f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13116f231ddaSDan Williams }
13126f231ddaSDan Williams 
1313*89a7301fSDan Williams static void isci_user_parameters_get(struct sci_user_parameters *u)
1314b5f18a20SDave Jiang {
1315b5f18a20SDave Jiang 	int i;
1316b5f18a20SDave Jiang 
1317b5f18a20SDave Jiang 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1318b5f18a20SDave Jiang 		struct sci_phy_user_params *u_phy = &u->phys[i];
1319b5f18a20SDave Jiang 
1320b5f18a20SDave Jiang 		u_phy->max_speed_generation = phy_gen;
1321b5f18a20SDave Jiang 
1322b5f18a20SDave Jiang 		/* we are not exporting these for now */
1323b5f18a20SDave Jiang 		u_phy->align_insertion_frequency = 0x7f;
1324b5f18a20SDave Jiang 		u_phy->in_connection_align_insertion_frequency = 0xff;
1325b5f18a20SDave Jiang 		u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1326b5f18a20SDave Jiang 	}
1327b5f18a20SDave Jiang 
1328b5f18a20SDave Jiang 	u->stp_inactivity_timeout = stp_inactive_to;
1329b5f18a20SDave Jiang 	u->ssp_inactivity_timeout = ssp_inactive_to;
1330b5f18a20SDave Jiang 	u->stp_max_occupancy_timeout = stp_max_occ_to;
1331b5f18a20SDave Jiang 	u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1332b5f18a20SDave Jiang 	u->no_outbound_task_timeout = no_outbound_task_to;
1333b5f18a20SDave Jiang 	u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1334b5f18a20SDave Jiang }
1335b5f18a20SDave Jiang 
1336*89a7301fSDan Williams static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
1337cc9203bfSDan Williams {
1338d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1339cc9203bfSDan Williams 
1340d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1341cc9203bfSDan Williams }
1342cc9203bfSDan Williams 
1343*89a7301fSDan Williams static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
1344cc9203bfSDan Williams {
1345d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1346cc9203bfSDan Williams 
1347d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1348cc9203bfSDan Williams }
1349cc9203bfSDan Williams 
1350cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1351cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1352cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1353cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1354cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1355cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1356cc9203bfSDan Williams 
1357cc9203bfSDan Williams /**
1358*89a7301fSDan Williams  * sci_controller_set_interrupt_coalescence() - This method allows the user to
1359cc9203bfSDan Williams  *    configure the interrupt coalescence.
1360cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1361cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1362cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1363cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1364cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1365cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1366cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1367cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1368cc9203bfSDan Williams  *    interrupt coalescing timeout.
1369cc9203bfSDan Williams  *
1370cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1371cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1372cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1373cc9203bfSDan Williams  */
1374d9dcb4baSDan Williams static enum sci_status
1375*89a7301fSDan Williams sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1376cc9203bfSDan Williams 					 u32 coalesce_number,
1377cc9203bfSDan Williams 					 u32 coalesce_timeout)
1378cc9203bfSDan Williams {
1379cc9203bfSDan Williams 	u8 timeout_encode = 0;
1380cc9203bfSDan Williams 	u32 min = 0;
1381cc9203bfSDan Williams 	u32 max = 0;
1382cc9203bfSDan Williams 
1383cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1384cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1385cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1386cc9203bfSDan Williams 
1387cc9203bfSDan Williams 	/*
1388cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1389cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1390cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1391cc9203bfSDan Williams 	 *              0       -        -       Disabled
1392cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1393cc9203bfSDan Williams 	 *              2       26.7     40.0
1394cc9203bfSDan Williams 	 *              3       53.3     80.0
1395cc9203bfSDan Williams 	 *              4       106.7    160.0
1396cc9203bfSDan Williams 	 *              5       213.3    320.0
1397cc9203bfSDan Williams 	 *              6       426.7    640.0
1398cc9203bfSDan Williams 	 *              7       853.3    1280.0
1399cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1400cc9203bfSDan Williams 	 *              9       3.4      5.1
1401cc9203bfSDan Williams 	 *              10      6.8      10.2
1402cc9203bfSDan Williams 	 *              11      13.7     20.5
1403cc9203bfSDan Williams 	 *              12      27.3     41.0
1404cc9203bfSDan Williams 	 *              13      54.6     81.9
1405cc9203bfSDan Williams 	 *              14      109.2    163.8
1406cc9203bfSDan Williams 	 *              15      218.5    327.7
1407cc9203bfSDan Williams 	 *              16      436.9    655.4
1408cc9203bfSDan Williams 	 *              17      873.8    1310.7
1409cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1410cc9203bfSDan Williams 	 *              19      3.5      5.2
1411cc9203bfSDan Williams 	 *              20      7.0      10.5
1412cc9203bfSDan Williams 	 *              21      14.0     21.0
1413cc9203bfSDan Williams 	 *              22      28.0     41.9
1414cc9203bfSDan Williams 	 *              23      55.9     83.9
1415cc9203bfSDan Williams 	 *              24      111.8    167.8
1416cc9203bfSDan Williams 	 *              25      223.7    335.5
1417cc9203bfSDan Williams 	 *              26      447.4    671.1
1418cc9203bfSDan Williams 	 *              27      894.8    1342.2
1419cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1420cc9203bfSDan Williams 	 *              Others Undefined */
1421cc9203bfSDan Williams 
1422cc9203bfSDan Williams 	/*
1423cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1424cc9203bfSDan Williams 	 * value for register writing. */
1425cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1426cc9203bfSDan Williams 		timeout_encode = 0;
1427cc9203bfSDan Williams 	else{
1428cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1429cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1430cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1431cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1432cc9203bfSDan Williams 
1433cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1434cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1435cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1436cc9203bfSDan Williams 		      timeout_encode++) {
1437cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1438cc9203bfSDan Williams 				break;
1439cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1440cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1441cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1442cc9203bfSDan Williams 					break;
1443cc9203bfSDan Williams 				else{
1444cc9203bfSDan Williams 					timeout_encode++;
1445cc9203bfSDan Williams 					break;
1446cc9203bfSDan Williams 				}
1447cc9203bfSDan Williams 			} else {
1448cc9203bfSDan Williams 				max = max * 2;
1449cc9203bfSDan Williams 				min = min * 2;
1450cc9203bfSDan Williams 			}
1451cc9203bfSDan Williams 		}
1452cc9203bfSDan Williams 
1453cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1454cc9203bfSDan Williams 			/* the value is out of range. */
1455cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1456cc9203bfSDan Williams 	}
1457cc9203bfSDan Williams 
1458cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1459cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1460d9dcb4baSDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
1461cc9203bfSDan Williams 
1462cc9203bfSDan Williams 
1463d9dcb4baSDan Williams 	ihost->interrupt_coalesce_number = (u16)coalesce_number;
1464d9dcb4baSDan Williams 	ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
1465cc9203bfSDan Williams 
1466cc9203bfSDan Williams 	return SCI_SUCCESS;
1467cc9203bfSDan Williams }
1468cc9203bfSDan Williams 
1469cc9203bfSDan Williams 
1470*89a7301fSDan Williams static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
1471cc9203bfSDan Williams {
1472d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1473cc9203bfSDan Williams 
1474cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
1475*89a7301fSDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0x10, 250);
1476cc9203bfSDan Williams }
1477cc9203bfSDan Williams 
1478*89a7301fSDan Williams static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
1479cc9203bfSDan Williams {
1480d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1481cc9203bfSDan Williams 
1482cc9203bfSDan Williams 	/* disable interrupt coalescence. */
1483*89a7301fSDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1484cc9203bfSDan Williams }
1485cc9203bfSDan Williams 
1486*89a7301fSDan Williams static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
1487cc9203bfSDan Williams {
1488cc9203bfSDan Williams 	u32 index;
1489cc9203bfSDan Williams 	enum sci_status status;
1490cc9203bfSDan Williams 	enum sci_status phy_status;
1491cc9203bfSDan Williams 
1492cc9203bfSDan Williams 	status = SCI_SUCCESS;
1493cc9203bfSDan Williams 
1494cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1495*89a7301fSDan Williams 		phy_status = sci_phy_stop(&ihost->phys[index]);
1496cc9203bfSDan Williams 
1497cc9203bfSDan Williams 		if (phy_status != SCI_SUCCESS &&
1498cc9203bfSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1499cc9203bfSDan Williams 			status = SCI_FAILURE;
1500cc9203bfSDan Williams 
1501d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1502cc9203bfSDan Williams 				 "%s: Controller stop operation failed to stop "
1503cc9203bfSDan Williams 				 "phy %d because of status %d.\n",
1504cc9203bfSDan Williams 				 __func__,
150585280955SDan Williams 				 ihost->phys[index].phy_index, phy_status);
1506cc9203bfSDan Williams 		}
1507cc9203bfSDan Williams 	}
1508cc9203bfSDan Williams 
1509cc9203bfSDan Williams 	return status;
1510cc9203bfSDan Williams }
1511cc9203bfSDan Williams 
1512*89a7301fSDan Williams static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
1513cc9203bfSDan Williams {
1514cc9203bfSDan Williams 	u32 index;
1515cc9203bfSDan Williams 	enum sci_status port_status;
1516cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1517cc9203bfSDan Williams 
1518d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1519ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1520cc9203bfSDan Williams 
1521*89a7301fSDan Williams 		port_status = sci_port_stop(iport);
1522cc9203bfSDan Williams 
1523cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1524cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1525cc9203bfSDan Williams 			status = SCI_FAILURE;
1526cc9203bfSDan Williams 
1527d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1528cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1529cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1530cc9203bfSDan Williams 				 __func__,
1531ffe191c9SDan Williams 				 iport->logical_port_index,
1532cc9203bfSDan Williams 				 port_status);
1533cc9203bfSDan Williams 		}
1534cc9203bfSDan Williams 	}
1535cc9203bfSDan Williams 
1536cc9203bfSDan Williams 	return status;
1537cc9203bfSDan Williams }
1538cc9203bfSDan Williams 
1539*89a7301fSDan Williams static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
1540cc9203bfSDan Williams {
1541cc9203bfSDan Williams 	u32 index;
1542cc9203bfSDan Williams 	enum sci_status status;
1543cc9203bfSDan Williams 	enum sci_status device_status;
1544cc9203bfSDan Williams 
1545cc9203bfSDan Williams 	status = SCI_SUCCESS;
1546cc9203bfSDan Williams 
1547d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
1548d9dcb4baSDan Williams 		if (ihost->device_table[index] != NULL) {
1549cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
1550*89a7301fSDan Williams 			device_status = sci_remote_device_stop(ihost->device_table[index], 0);
1551cc9203bfSDan Williams 
1552cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1553cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1554d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
1555cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1556cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1557cc9203bfSDan Williams 					 "status %d.\n",
1558cc9203bfSDan Williams 					 __func__,
1559d9dcb4baSDan Williams 					 ihost->device_table[index], device_status);
1560cc9203bfSDan Williams 			}
1561cc9203bfSDan Williams 		}
1562cc9203bfSDan Williams 	}
1563cc9203bfSDan Williams 
1564cc9203bfSDan Williams 	return status;
1565cc9203bfSDan Williams }
1566cc9203bfSDan Williams 
1567*89a7301fSDan Williams static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1568cc9203bfSDan Williams {
1569d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1570cc9203bfSDan Williams 
1571cc9203bfSDan Williams 	/* Stop all of the components for this controller */
1572*89a7301fSDan Williams 	sci_controller_stop_phys(ihost);
1573*89a7301fSDan Williams 	sci_controller_stop_ports(ihost);
1574*89a7301fSDan Williams 	sci_controller_stop_devices(ihost);
1575cc9203bfSDan Williams }
1576cc9203bfSDan Williams 
1577*89a7301fSDan Williams static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1578cc9203bfSDan Williams {
1579d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1580cc9203bfSDan Williams 
1581d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1582cc9203bfSDan Williams }
1583cc9203bfSDan Williams 
1584*89a7301fSDan Williams static void sci_controller_reset_hardware(struct isci_host *ihost)
1585cc9203bfSDan Williams {
1586cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
1587*89a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1588cc9203bfSDan Williams 
1589cc9203bfSDan Williams 	/* Reset the SCU */
1590d9dcb4baSDan Williams 	writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
1591cc9203bfSDan Williams 
1592cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1593cc9203bfSDan Williams 	udelay(1000);
1594cc9203bfSDan Williams 
1595cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1596d9dcb4baSDan Williams 	writel(0x00000000, &ihost->smu_registers->completion_queue_get);
1597cc9203bfSDan Williams 
1598cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1599d9dcb4baSDan Williams 	writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
1600cc9203bfSDan Williams }
1601cc9203bfSDan Williams 
1602*89a7301fSDan Williams static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1603cc9203bfSDan Williams {
1604d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1605cc9203bfSDan Williams 
1606*89a7301fSDan Williams 	sci_controller_reset_hardware(ihost);
1607d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1608cc9203bfSDan Williams }
1609cc9203bfSDan Williams 
1610*89a7301fSDan Williams static const struct sci_base_state sci_controller_state_table[] = {
1611e301370aSEdmund Nadolski 	[SCIC_INITIAL] = {
1612*89a7301fSDan Williams 		.enter_state = sci_controller_initial_state_enter,
1613cc9203bfSDan Williams 	},
1614e301370aSEdmund Nadolski 	[SCIC_RESET] = {},
1615e301370aSEdmund Nadolski 	[SCIC_INITIALIZING] = {},
1616e301370aSEdmund Nadolski 	[SCIC_INITIALIZED] = {},
1617e301370aSEdmund Nadolski 	[SCIC_STARTING] = {
1618*89a7301fSDan Williams 		.exit_state  = sci_controller_starting_state_exit,
1619cc9203bfSDan Williams 	},
1620e301370aSEdmund Nadolski 	[SCIC_READY] = {
1621*89a7301fSDan Williams 		.enter_state = sci_controller_ready_state_enter,
1622*89a7301fSDan Williams 		.exit_state  = sci_controller_ready_state_exit,
1623cc9203bfSDan Williams 	},
1624e301370aSEdmund Nadolski 	[SCIC_RESETTING] = {
1625*89a7301fSDan Williams 		.enter_state = sci_controller_resetting_state_enter,
1626cc9203bfSDan Williams 	},
1627e301370aSEdmund Nadolski 	[SCIC_STOPPING] = {
1628*89a7301fSDan Williams 		.enter_state = sci_controller_stopping_state_enter,
1629*89a7301fSDan Williams 		.exit_state = sci_controller_stopping_state_exit,
1630cc9203bfSDan Williams 	},
1631e301370aSEdmund Nadolski 	[SCIC_STOPPED] = {},
1632e301370aSEdmund Nadolski 	[SCIC_FAILED] = {}
1633cc9203bfSDan Williams };
1634cc9203bfSDan Williams 
1635*89a7301fSDan Williams static void sci_controller_set_default_config_parameters(struct isci_host *ihost)
1636cc9203bfSDan Williams {
1637cc9203bfSDan Williams 	/* these defaults are overridden by the platform / firmware */
1638cc9203bfSDan Williams 	u16 index;
1639cc9203bfSDan Williams 
1640cc9203bfSDan Williams 	/* Default to APC mode. */
1641*89a7301fSDan Williams 	ihost->oem_parameters.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1642cc9203bfSDan Williams 
1643cc9203bfSDan Williams 	/* Default to APC mode. */
1644*89a7301fSDan Williams 	ihost->oem_parameters.controller.max_concurrent_dev_spin_up = 1;
1645cc9203bfSDan Williams 
1646cc9203bfSDan Williams 	/* Default to no SSC operation. */
1647*89a7301fSDan Williams 	ihost->oem_parameters.controller.do_enable_ssc = false;
1648cc9203bfSDan Williams 
1649cc9203bfSDan Williams 	/* Initialize all of the port parameter information to narrow ports. */
1650cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PORTS; index++) {
1651*89a7301fSDan Williams 		ihost->oem_parameters.ports[index].phy_mask = 0;
1652cc9203bfSDan Williams 	}
1653cc9203bfSDan Williams 
1654cc9203bfSDan Williams 	/* Initialize all of the phy parameter information. */
1655cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1656cc9203bfSDan Williams 		/* Default to 6G (i.e. Gen 3) for now. */
1657*89a7301fSDan Williams 		ihost->user_parameters.phys[index].max_speed_generation = 3;
1658cc9203bfSDan Williams 
1659cc9203bfSDan Williams 		/* the frequencies cannot be 0 */
1660*89a7301fSDan Williams 		ihost->user_parameters.phys[index].align_insertion_frequency = 0x7f;
1661*89a7301fSDan Williams 		ihost->user_parameters.phys[index].in_connection_align_insertion_frequency = 0xff;
1662*89a7301fSDan Williams 		ihost->user_parameters.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1663cc9203bfSDan Williams 
1664cc9203bfSDan Williams 		/*
1665cc9203bfSDan Williams 		 * Previous Vitesse based expanders had a arbitration issue that
1666cc9203bfSDan Williams 		 * is worked around by having the upper 32-bits of SAS address
1667cc9203bfSDan Williams 		 * with a value greater then the Vitesse company identifier.
1668cc9203bfSDan Williams 		 * Hence, usage of 0x5FCFFFFF. */
1669*89a7301fSDan Williams 		ihost->oem_parameters.phys[index].sas_address.low = 0x1 + ihost->id;
1670*89a7301fSDan Williams 		ihost->oem_parameters.phys[index].sas_address.high = 0x5FCFFFFF;
1671cc9203bfSDan Williams 	}
1672cc9203bfSDan Williams 
1673*89a7301fSDan Williams 	ihost->user_parameters.stp_inactivity_timeout = 5;
1674*89a7301fSDan Williams 	ihost->user_parameters.ssp_inactivity_timeout = 5;
1675*89a7301fSDan Williams 	ihost->user_parameters.stp_max_occupancy_timeout = 5;
1676*89a7301fSDan Williams 	ihost->user_parameters.ssp_max_occupancy_timeout = 20;
1677*89a7301fSDan Williams 	ihost->user_parameters.no_outbound_task_timeout = 20;
1678cc9203bfSDan Williams }
1679cc9203bfSDan Williams 
16806cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data)
16816cb5853dSEdmund Nadolski {
16826cb5853dSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1683d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1684d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
16856cb5853dSEdmund Nadolski 	unsigned long flags;
1686cc9203bfSDan Williams 
16876cb5853dSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
16886cb5853dSEdmund Nadolski 
16896cb5853dSEdmund Nadolski 	if (tmr->cancel)
16906cb5853dSEdmund Nadolski 		goto done;
16916cb5853dSEdmund Nadolski 
1692e301370aSEdmund Nadolski 	if (sm->current_state_id == SCIC_STARTING)
1693*89a7301fSDan Williams 		sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
1694e301370aSEdmund Nadolski 	else if (sm->current_state_id == SCIC_STOPPING) {
1695e301370aSEdmund Nadolski 		sci_change_state(sm, SCIC_FAILED);
16966cb5853dSEdmund Nadolski 		isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
16976cb5853dSEdmund Nadolski 	} else	/* / @todo Now what do we want to do in this case? */
1698d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
16996cb5853dSEdmund Nadolski 			"%s: Controller timer fired when controller was not "
17006cb5853dSEdmund Nadolski 			"in a state being timed.\n",
17016cb5853dSEdmund Nadolski 			__func__);
17026cb5853dSEdmund Nadolski 
17036cb5853dSEdmund Nadolski done:
17046cb5853dSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
17056cb5853dSEdmund Nadolski }
1706cc9203bfSDan Williams 
1707*89a7301fSDan Williams static enum sci_status sci_controller_construct(struct isci_host *ihost,
1708cc9203bfSDan Williams 						void __iomem *scu_base,
1709cc9203bfSDan Williams 						void __iomem *smu_base)
1710cc9203bfSDan Williams {
1711cc9203bfSDan Williams 	u8 i;
1712cc9203bfSDan Williams 
1713*89a7301fSDan Williams 	sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
1714cc9203bfSDan Williams 
1715d9dcb4baSDan Williams 	ihost->scu_registers = scu_base;
1716d9dcb4baSDan Williams 	ihost->smu_registers = smu_base;
1717cc9203bfSDan Williams 
1718*89a7301fSDan Williams 	sci_port_configuration_agent_construct(&ihost->port_agent);
1719cc9203bfSDan Williams 
1720cc9203bfSDan Williams 	/* Construct the ports for this controller */
1721cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1722*89a7301fSDan Williams 		sci_port_construct(&ihost->ports[i], i, ihost);
1723*89a7301fSDan Williams 	sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
1724cc9203bfSDan Williams 
1725cc9203bfSDan Williams 	/* Construct the phys for this controller */
1726cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1727cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
1728*89a7301fSDan Williams 		sci_phy_construct(&ihost->phys[i],
1729ffe191c9SDan Williams 				  &ihost->ports[SCI_MAX_PORTS], i);
1730cc9203bfSDan Williams 	}
1731cc9203bfSDan Williams 
1732d9dcb4baSDan Williams 	ihost->invalid_phy_mask = 0;
1733cc9203bfSDan Williams 
1734d9dcb4baSDan Williams 	sci_init_timer(&ihost->timer, controller_timeout);
17356cb5853dSEdmund Nadolski 
1736cc9203bfSDan Williams 	/* Initialize the User and OEM parameters to default values. */
1737*89a7301fSDan Williams 	sci_controller_set_default_config_parameters(ihost);
1738cc9203bfSDan Williams 
1739*89a7301fSDan Williams 	return sci_controller_reset(ihost);
1740cc9203bfSDan Williams }
1741cc9203bfSDan Williams 
1742*89a7301fSDan Williams int sci_oem_parameters_validate(struct sci_oem_params *oem)
1743cc9203bfSDan Williams {
1744cc9203bfSDan Williams 	int i;
1745cc9203bfSDan Williams 
1746cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1747cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1748cc9203bfSDan Williams 			return -EINVAL;
1749cc9203bfSDan Williams 
1750cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1751cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1752cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1753cc9203bfSDan Williams 			return -EINVAL;
1754cc9203bfSDan Williams 
1755cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1756cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1757cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1758cc9203bfSDan Williams 				return -EINVAL;
1759cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1760cc9203bfSDan Williams 		u8 phy_mask = 0;
1761cc9203bfSDan Williams 
1762cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1763cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1764cc9203bfSDan Williams 
1765cc9203bfSDan Williams 		if (phy_mask == 0)
1766cc9203bfSDan Williams 			return -EINVAL;
1767cc9203bfSDan Williams 	} else
1768cc9203bfSDan Williams 		return -EINVAL;
1769cc9203bfSDan Williams 
1770cc9203bfSDan Williams 	if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1771cc9203bfSDan Williams 		return -EINVAL;
1772cc9203bfSDan Williams 
1773cc9203bfSDan Williams 	return 0;
1774cc9203bfSDan Williams }
1775cc9203bfSDan Williams 
1776*89a7301fSDan Williams static enum sci_status sci_oem_parameters_set(struct isci_host *ihost)
1777cc9203bfSDan Williams {
1778d9dcb4baSDan Williams 	u32 state = ihost->sm.current_state_id;
1779cc9203bfSDan Williams 
1780e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
1781e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
1782e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
1783cc9203bfSDan Williams 
1784*89a7301fSDan Williams 		if (sci_oem_parameters_validate(&ihost->oem_parameters))
1785cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1786cc9203bfSDan Williams 
1787cc9203bfSDan Williams 		return SCI_SUCCESS;
1788cc9203bfSDan Williams 	}
1789cc9203bfSDan Williams 
1790cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
1791cc9203bfSDan Williams }
1792cc9203bfSDan Williams 
17930473661aSEdmund Nadolski static void power_control_timeout(unsigned long data)
1794cc9203bfSDan Williams {
17950473661aSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1796d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
179785280955SDan Williams 	struct isci_phy *iphy;
17980473661aSEdmund Nadolski 	unsigned long flags;
17990473661aSEdmund Nadolski 	u8 i;
1800cc9203bfSDan Williams 
18010473661aSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
1802cc9203bfSDan Williams 
18030473661aSEdmund Nadolski 	if (tmr->cancel)
18040473661aSEdmund Nadolski 		goto done;
1805cc9203bfSDan Williams 
1806d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
1807cc9203bfSDan Williams 
1808d9dcb4baSDan Williams 	if (ihost->power_control.phys_waiting == 0) {
1809d9dcb4baSDan Williams 		ihost->power_control.timer_started = false;
18100473661aSEdmund Nadolski 		goto done;
18110473661aSEdmund Nadolski 	}
1812cc9203bfSDan Williams 
18130473661aSEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
18140473661aSEdmund Nadolski 
1815d9dcb4baSDan Williams 		if (ihost->power_control.phys_waiting == 0)
18160473661aSEdmund Nadolski 			break;
18170473661aSEdmund Nadolski 
1818d9dcb4baSDan Williams 		iphy = ihost->power_control.requesters[i];
181985280955SDan Williams 		if (iphy == NULL)
18200473661aSEdmund Nadolski 			continue;
18210473661aSEdmund Nadolski 
1822d9dcb4baSDan Williams 		if (ihost->power_control.phys_granted_power >=
1823*89a7301fSDan Williams 		    ihost->oem_parameters.controller.max_concurrent_dev_spin_up)
18240473661aSEdmund Nadolski 			break;
18250473661aSEdmund Nadolski 
1826d9dcb4baSDan Williams 		ihost->power_control.requesters[i] = NULL;
1827d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1828d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
1829*89a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1830cc9203bfSDan Williams 	}
1831cc9203bfSDan Williams 
1832cc9203bfSDan Williams 	/*
1833cc9203bfSDan Williams 	 * It doesn't matter if the power list is empty, we need to start the
1834cc9203bfSDan Williams 	 * timer in case another phy becomes ready.
1835cc9203bfSDan Williams 	 */
18360473661aSEdmund Nadolski 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1837d9dcb4baSDan Williams 	ihost->power_control.timer_started = true;
18380473661aSEdmund Nadolski 
18390473661aSEdmund Nadolski done:
18400473661aSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1841cc9203bfSDan Williams }
1842cc9203bfSDan Williams 
1843*89a7301fSDan Williams void sci_controller_power_control_queue_insert(struct isci_host *ihost,
184485280955SDan Williams 					       struct isci_phy *iphy)
1845cc9203bfSDan Williams {
184685280955SDan Williams 	BUG_ON(iphy == NULL);
1847cc9203bfSDan Williams 
1848d9dcb4baSDan Williams 	if (ihost->power_control.phys_granted_power <
1849*89a7301fSDan Williams 	    ihost->oem_parameters.controller.max_concurrent_dev_spin_up) {
1850d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
1851*89a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1852cc9203bfSDan Williams 
1853cc9203bfSDan Williams 		/*
1854cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1855cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1856cc9203bfSDan Williams 		 */
1857d9dcb4baSDan Williams 		if (ihost->power_control.timer_started)
1858d9dcb4baSDan Williams 			sci_del_timer(&ihost->power_control.timer);
18590473661aSEdmund Nadolski 
1860d9dcb4baSDan Williams 		sci_mod_timer(&ihost->power_control.timer,
18610473661aSEdmund Nadolski 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1862d9dcb4baSDan Williams 		ihost->power_control.timer_started = true;
18630473661aSEdmund Nadolski 
1864cc9203bfSDan Williams 	} else {
1865cc9203bfSDan Williams 		/* Add the phy in the waiting list */
1866d9dcb4baSDan Williams 		ihost->power_control.requesters[iphy->phy_index] = iphy;
1867d9dcb4baSDan Williams 		ihost->power_control.phys_waiting++;
1868cc9203bfSDan Williams 	}
1869cc9203bfSDan Williams }
1870cc9203bfSDan Williams 
1871*89a7301fSDan Williams void sci_controller_power_control_queue_remove(struct isci_host *ihost,
187285280955SDan Williams 					       struct isci_phy *iphy)
1873cc9203bfSDan Williams {
187485280955SDan Williams 	BUG_ON(iphy == NULL);
1875cc9203bfSDan Williams 
1876*89a7301fSDan Williams 	if (ihost->power_control.requesters[iphy->phy_index])
1877d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1878cc9203bfSDan Williams 
1879d9dcb4baSDan Williams 	ihost->power_control.requesters[iphy->phy_index] = NULL;
1880cc9203bfSDan Williams }
1881cc9203bfSDan Williams 
1882cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
1883cc9203bfSDan Williams 
1884cc9203bfSDan Williams /* Initialize the AFE for this phy index. We need to read the AFE setup from
1885cc9203bfSDan Williams  * the OEM parameters
1886cc9203bfSDan Williams  */
1887*89a7301fSDan Williams static void sci_controller_afe_initialization(struct isci_host *ihost)
1888cc9203bfSDan Williams {
1889*89a7301fSDan Williams 	const struct sci_oem_params *oem = &ihost->oem_parameters;
1890cc9203bfSDan Williams 	u32 afe_status;
1891cc9203bfSDan Williams 	u32 phy_id;
1892cc9203bfSDan Williams 
1893cc9203bfSDan Williams 	/* Clear DFX Status registers */
1894d9dcb4baSDan Williams 	writel(0x0081000f, &ihost->scu_registers->afe.afe_dfx_master_control0);
1895cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1896cc9203bfSDan Williams 
1897cc9203bfSDan Williams 	if (is_b0()) {
1898cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
1899cc9203bfSDan Williams 		 * Timer, PM Stagger Timer */
1900d9dcb4baSDan Williams 		writel(0x0007BFFF, &ihost->scu_registers->afe.afe_pmsn_master_control2);
1901cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1902cc9203bfSDan Williams 	}
1903cc9203bfSDan Williams 
1904cc9203bfSDan Williams 	/* Configure bias currents to normal */
1905cc9203bfSDan Williams 	if (is_a0())
1906d9dcb4baSDan Williams 		writel(0x00005500, &ihost->scu_registers->afe.afe_bias_control);
1907cc9203bfSDan Williams 	else if (is_a2())
1908d9dcb4baSDan Williams 		writel(0x00005A00, &ihost->scu_registers->afe.afe_bias_control);
1909dbb0743aSAdam Gruchala 	else if (is_b0() || is_c0())
1910d9dcb4baSDan Williams 		writel(0x00005F00, &ihost->scu_registers->afe.afe_bias_control);
1911cc9203bfSDan Williams 
1912cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1913cc9203bfSDan Williams 
1914cc9203bfSDan Williams 	/* Enable PLL */
1915dbb0743aSAdam Gruchala 	if (is_b0() || is_c0())
1916d9dcb4baSDan Williams 		writel(0x80040A08, &ihost->scu_registers->afe.afe_pll_control0);
1917cc9203bfSDan Williams 	else
1918d9dcb4baSDan Williams 		writel(0x80040908, &ihost->scu_registers->afe.afe_pll_control0);
1919cc9203bfSDan Williams 
1920cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1921cc9203bfSDan Williams 
1922cc9203bfSDan Williams 	/* Wait for the PLL to lock */
1923cc9203bfSDan Williams 	do {
1924d9dcb4baSDan Williams 		afe_status = readl(&ihost->scu_registers->afe.afe_common_block_status);
1925cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1926cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
1927cc9203bfSDan Williams 
1928cc9203bfSDan Williams 	if (is_a0() || is_a2()) {
1929cc9203bfSDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
1930d9dcb4baSDan Williams 		writel(0x7bcc96ad, &ihost->scu_registers->afe.afe_pmsn_master_control0);
1931cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1932cc9203bfSDan Williams 	}
1933cc9203bfSDan Williams 
1934cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
1935cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
1936cc9203bfSDan Williams 
1937cc9203bfSDan Williams 		if (is_b0()) {
1938cc9203bfSDan Williams 			 /* Configure transmitter SSC parameters */
1939d9dcb4baSDan Williams 			writel(0x00030000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
1940cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1941dbb0743aSAdam Gruchala 		} else if (is_c0()) {
1942dbb0743aSAdam Gruchala 			 /* Configure transmitter SSC parameters */
1943d9dcb4baSDan Williams 			writel(0x0003000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
1944dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
1945dbb0743aSAdam Gruchala 
1946dbb0743aSAdam Gruchala 			/*
1947dbb0743aSAdam Gruchala 			 * All defaults, except the Receive Word Alignament/Comma Detect
1948dbb0743aSAdam Gruchala 			 * Enable....(0xe800) */
1949d9dcb4baSDan Williams 			writel(0x00004500, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
1950dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
1951cc9203bfSDan Williams 		} else {
1952cc9203bfSDan Williams 			/*
1953cc9203bfSDan Williams 			 * All defaults, except the Receive Word Alignament/Comma Detect
1954cc9203bfSDan Williams 			 * Enable....(0xe800) */
1955d9dcb4baSDan Williams 			writel(0x00004512, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
1956cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1957cc9203bfSDan Williams 
1958d9dcb4baSDan Williams 			writel(0x0050100F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
1959cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1960cc9203bfSDan Williams 		}
1961cc9203bfSDan Williams 
1962cc9203bfSDan Williams 		/*
1963cc9203bfSDan Williams 		 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
1964cc9203bfSDan Williams 		 * & increase TX int & ext bias 20%....(0xe85c) */
1965cc9203bfSDan Williams 		if (is_a0())
1966d9dcb4baSDan Williams 			writel(0x000003D4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
1967cc9203bfSDan Williams 		else if (is_a2())
1968d9dcb4baSDan Williams 			writel(0x000003F0, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
1969dbb0743aSAdam Gruchala 		else if (is_b0()) {
1970cc9203bfSDan Williams 			 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
1971d9dcb4baSDan Williams 			writel(0x000003D7, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
1972cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1973cc9203bfSDan Williams 
1974cc9203bfSDan Williams 			/*
1975cc9203bfSDan Williams 			 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
1976cc9203bfSDan Williams 			 * & increase TX int & ext bias 20%....(0xe85c) */
1977d9dcb4baSDan Williams 			writel(0x000003D4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
1978dbb0743aSAdam Gruchala 		} else {
1979d9dcb4baSDan Williams 			writel(0x000001E7, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
1980dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
1981dbb0743aSAdam Gruchala 
1982dbb0743aSAdam Gruchala 			/*
1983dbb0743aSAdam Gruchala 			 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
1984dbb0743aSAdam Gruchala 			 * & increase TX int & ext bias 20%....(0xe85c) */
1985d9dcb4baSDan Williams 			writel(0x000001E4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
1986cc9203bfSDan Williams 		}
1987cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1988cc9203bfSDan Williams 
1989cc9203bfSDan Williams 		if (is_a0() || is_a2()) {
1990cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
1991d9dcb4baSDan Williams 			writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
1992cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
1993cc9203bfSDan Williams 		}
1994cc9203bfSDan Williams 
1995cc9203bfSDan Williams 		/*
1996cc9203bfSDan Williams 		 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
1997cc9203bfSDan Williams 		 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
1998d9dcb4baSDan Williams 		writel(0x00004100, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
1999cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2000cc9203bfSDan Williams 
2001cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2002cc9203bfSDan Williams 		if (is_a0())
2003d9dcb4baSDan Williams 			writel(0x3F09983F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2004cc9203bfSDan Williams 		else if (is_a2())
2005d9dcb4baSDan Williams 			writel(0x3F11103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2006dbb0743aSAdam Gruchala 		else if (is_b0()) {
2007d9dcb4baSDan Williams 			writel(0x3F11103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2008cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2009cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
2010d9dcb4baSDan Williams 			writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2011dbb0743aSAdam Gruchala 		} else {
2012d9dcb4baSDan Williams 			writel(0x0140DF0F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
2013dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2014dbb0743aSAdam Gruchala 
2015d9dcb4baSDan Williams 			writel(0x3F6F103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2016dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2017dbb0743aSAdam Gruchala 
2018dbb0743aSAdam Gruchala 			/* Enable TX equalization (0xe824) */
2019d9dcb4baSDan Williams 			writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2020cc9203bfSDan Williams 		}
2021dbb0743aSAdam Gruchala 
2022cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2023cc9203bfSDan Williams 
2024cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control0,
2025d9dcb4baSDan Williams 			&ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
2026cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2027cc9203bfSDan Williams 
2028cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control1,
2029d9dcb4baSDan Williams 			&ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
2030cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2031cc9203bfSDan Williams 
2032cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control2,
2033d9dcb4baSDan Williams 			&ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
2034cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2035cc9203bfSDan Williams 
2036cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control3,
2037d9dcb4baSDan Williams 			&ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
2038cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2039cc9203bfSDan Williams 	}
2040cc9203bfSDan Williams 
2041cc9203bfSDan Williams 	/* Transfer control to the PEs */
2042d9dcb4baSDan Williams 	writel(0x00010f00, &ihost->scu_registers->afe.afe_dfx_master_control0);
2043cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2044cc9203bfSDan Williams }
2045cc9203bfSDan Williams 
2046*89a7301fSDan Williams static void sci_controller_initialize_power_control(struct isci_host *ihost)
2047cc9203bfSDan Williams {
2048d9dcb4baSDan Williams 	sci_init_timer(&ihost->power_control.timer, power_control_timeout);
2049cc9203bfSDan Williams 
2050d9dcb4baSDan Williams 	memset(ihost->power_control.requesters, 0,
2051d9dcb4baSDan Williams 	       sizeof(ihost->power_control.requesters));
2052cc9203bfSDan Williams 
2053d9dcb4baSDan Williams 	ihost->power_control.phys_waiting = 0;
2054d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
2055cc9203bfSDan Williams }
2056cc9203bfSDan Williams 
2057*89a7301fSDan Williams static enum sci_status sci_controller_initialize(struct isci_host *ihost)
2058cc9203bfSDan Williams {
2059d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
20607c78da31SDan Williams 	enum sci_status result = SCI_FAILURE;
20617c78da31SDan Williams 	unsigned long i, state, val;
2062cc9203bfSDan Williams 
2063d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_RESET) {
2064d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2065cc9203bfSDan Williams 			 "SCIC Controller initialize operation requested "
2066cc9203bfSDan Williams 			 "in invalid state\n");
2067cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2068cc9203bfSDan Williams 	}
2069cc9203bfSDan Williams 
2070e301370aSEdmund Nadolski 	sci_change_state(sm, SCIC_INITIALIZING);
2071cc9203bfSDan Williams 
2072d9dcb4baSDan Williams 	sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
2073bb3dbdf6SEdmund Nadolski 
2074d9dcb4baSDan Williams 	ihost->next_phy_to_start = 0;
2075d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
2076cc9203bfSDan Williams 
2077*89a7301fSDan Williams 	sci_controller_initialize_power_control(ihost);
2078cc9203bfSDan Williams 
2079cc9203bfSDan Williams 	/*
2080cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2081cc9203bfSDan Williams 	 * program the AFE registers.
2082cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2083cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
2084*89a7301fSDan Williams 	sci_controller_afe_initialization(ihost);
2085cc9203bfSDan Williams 
2086cc9203bfSDan Williams 
2087cc9203bfSDan Williams 	/* Take the hardware out of reset */
2088d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->soft_reset_control);
2089cc9203bfSDan Williams 
2090cc9203bfSDan Williams 	/*
2091cc9203bfSDan Williams 	 * / @todo Provide meaningfull error code for hardware failure
2092cc9203bfSDan Williams 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
20937c78da31SDan Williams 	for (i = 100; i >= 1; i--) {
20947c78da31SDan Williams 		u32 status;
2095cc9203bfSDan Williams 
2096cc9203bfSDan Williams 		/* Loop until the hardware reports success */
2097cc9203bfSDan Williams 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2098d9dcb4baSDan Williams 		status = readl(&ihost->smu_registers->control_status);
2099cc9203bfSDan Williams 
21007c78da31SDan Williams 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
21017c78da31SDan Williams 			break;
2102cc9203bfSDan Williams 	}
21037c78da31SDan Williams 	if (i == 0)
21047c78da31SDan Williams 		goto out;
2105cc9203bfSDan Williams 
2106cc9203bfSDan Williams 	/*
2107cc9203bfSDan Williams 	 * Determine what are the actaul device capacities that the
2108cc9203bfSDan Williams 	 * hardware will support */
2109d9dcb4baSDan Williams 	val = readl(&ihost->smu_registers->device_context_capacity);
2110cc9203bfSDan Williams 
21117c78da31SDan Williams 	/* Record the smaller of the two capacity values */
2112d9dcb4baSDan Williams 	ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2113d9dcb4baSDan Williams 	ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2114d9dcb4baSDan Williams 	ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2115cc9203bfSDan Williams 
2116cc9203bfSDan Williams 	/*
2117cc9203bfSDan Williams 	 * Make all PEs that are unassigned match up with the
2118cc9203bfSDan Williams 	 * logical ports
2119cc9203bfSDan Williams 	 */
2120d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
2121cc9203bfSDan Williams 		struct scu_port_task_scheduler_group_registers __iomem
2122d9dcb4baSDan Williams 			*ptsg = &ihost->scu_registers->peg0.ptsg;
2123cc9203bfSDan Williams 
21247c78da31SDan Williams 		writel(i, &ptsg->protocol_engine[i]);
2125cc9203bfSDan Williams 	}
2126cc9203bfSDan Williams 
2127cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2128d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.pdma_configuration);
21297c78da31SDan Williams 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2130d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.pdma_configuration);
2131cc9203bfSDan Williams 
2132d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.cdma_configuration);
21337c78da31SDan Williams 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2134d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.cdma_configuration);
2135cc9203bfSDan Williams 
2136cc9203bfSDan Williams 	/*
2137cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2138cc9203bfSDan Williams 	 * are accessed during the port initialization.
2139cc9203bfSDan Williams 	 */
21407c78da31SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
2141*89a7301fSDan Williams 		result = sci_phy_initialize(&ihost->phys[i],
2142d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].tl,
2143d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].ll);
21447c78da31SDan Williams 		if (result != SCI_SUCCESS)
21457c78da31SDan Williams 			goto out;
2146cc9203bfSDan Williams 	}
2147cc9203bfSDan Williams 
2148d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
2149*89a7301fSDan Williams 		struct isci_port *iport = &ihost->ports[i];
21507c78da31SDan Williams 
2151*89a7301fSDan Williams 		iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
2152*89a7301fSDan Williams 		iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
2153*89a7301fSDan Williams 		iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
2154cc9203bfSDan Williams 	}
2155cc9203bfSDan Williams 
2156*89a7301fSDan Williams 	result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
2157cc9203bfSDan Williams 
21587c78da31SDan Williams  out:
2159cc9203bfSDan Williams 	/* Advance the controller state machine */
2160cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2161e301370aSEdmund Nadolski 		state = SCIC_INITIALIZED;
2162cc9203bfSDan Williams 	else
2163e301370aSEdmund Nadolski 		state = SCIC_FAILED;
2164e301370aSEdmund Nadolski 	sci_change_state(sm, state);
2165cc9203bfSDan Williams 
2166cc9203bfSDan Williams 	return result;
2167cc9203bfSDan Williams }
2168cc9203bfSDan Williams 
2169*89a7301fSDan Williams static enum sci_status sci_user_parameters_set(struct isci_host *ihost,
2170*89a7301fSDan Williams 					       struct sci_user_parameters *sci_parms)
2171cc9203bfSDan Williams {
2172d9dcb4baSDan Williams 	u32 state = ihost->sm.current_state_id;
2173cc9203bfSDan Williams 
2174e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
2175e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
2176e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
2177cc9203bfSDan Williams 		u16 index;
2178cc9203bfSDan Williams 
2179cc9203bfSDan Williams 		/*
2180cc9203bfSDan Williams 		 * Validate the user parameters.  If they are not legal, then
2181cc9203bfSDan Williams 		 * return a failure.
2182cc9203bfSDan Williams 		 */
2183cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
2184cc9203bfSDan Williams 			struct sci_phy_user_params *user_phy;
2185cc9203bfSDan Williams 
2186*89a7301fSDan Williams 			user_phy = &sci_parms->phys[index];
2187cc9203bfSDan Williams 
2188cc9203bfSDan Williams 			if (!((user_phy->max_speed_generation <=
2189cc9203bfSDan Williams 						SCIC_SDS_PARM_MAX_SPEED) &&
2190cc9203bfSDan Williams 			      (user_phy->max_speed_generation >
2191cc9203bfSDan Williams 						SCIC_SDS_PARM_NO_SPEED)))
2192cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2193cc9203bfSDan Williams 
2194cc9203bfSDan Williams 			if (user_phy->in_connection_align_insertion_frequency <
2195cc9203bfSDan Williams 					3)
2196cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2197cc9203bfSDan Williams 
2198cc9203bfSDan Williams 			if ((user_phy->in_connection_align_insertion_frequency <
2199cc9203bfSDan Williams 						3) ||
2200cc9203bfSDan Williams 			    (user_phy->align_insertion_frequency == 0) ||
2201cc9203bfSDan Williams 			    (user_phy->
2202cc9203bfSDan Williams 				notify_enable_spin_up_insertion_frequency ==
2203cc9203bfSDan Williams 						0))
2204cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2205cc9203bfSDan Williams 		}
2206cc9203bfSDan Williams 
2207*89a7301fSDan Williams 		if ((sci_parms->stp_inactivity_timeout == 0) ||
2208*89a7301fSDan Williams 		    (sci_parms->ssp_inactivity_timeout == 0) ||
2209*89a7301fSDan Williams 		    (sci_parms->stp_max_occupancy_timeout == 0) ||
2210*89a7301fSDan Williams 		    (sci_parms->ssp_max_occupancy_timeout == 0) ||
2211*89a7301fSDan Williams 		    (sci_parms->no_outbound_task_timeout == 0))
2212cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2213cc9203bfSDan Williams 
2214*89a7301fSDan Williams 		memcpy(&ihost->user_parameters, sci_parms, sizeof(*sci_parms));
2215cc9203bfSDan Williams 
2216cc9203bfSDan Williams 		return SCI_SUCCESS;
2217cc9203bfSDan Williams 	}
2218cc9203bfSDan Williams 
2219cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
2220cc9203bfSDan Williams }
2221cc9203bfSDan Williams 
2222*89a7301fSDan Williams static int sci_controller_mem_init(struct isci_host *ihost)
2223cc9203bfSDan Williams {
2224d9dcb4baSDan Williams 	struct device *dev = &ihost->pdev->dev;
22257c78da31SDan Williams 	dma_addr_t dma;
22267c78da31SDan Williams 	size_t size;
22277c78da31SDan Williams 	int err;
2228cc9203bfSDan Williams 
22297c78da31SDan Williams 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2230d9dcb4baSDan Williams 	ihost->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2231d9dcb4baSDan Williams 	if (!ihost->completion_queue)
2232cc9203bfSDan Williams 		return -ENOMEM;
2233cc9203bfSDan Williams 
2234d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->completion_queue_lower);
2235d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->completion_queue_upper);
2236cc9203bfSDan Williams 
2237d9dcb4baSDan Williams 	size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2238d9dcb4baSDan Williams 	ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
22397c78da31SDan Williams 							       GFP_KERNEL);
2240d9dcb4baSDan Williams 	if (!ihost->remote_node_context_table)
2241cc9203bfSDan Williams 		return -ENOMEM;
2242cc9203bfSDan Williams 
2243d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->remote_node_context_lower);
2244d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->remote_node_context_upper);
2245cc9203bfSDan Williams 
2246d9dcb4baSDan Williams 	size = ihost->task_context_entries * sizeof(struct scu_task_context),
2247d9dcb4baSDan Williams 	ihost->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2248d9dcb4baSDan Williams 	if (!ihost->task_context_table)
2249cc9203bfSDan Williams 		return -ENOMEM;
2250cc9203bfSDan Williams 
2251d9dcb4baSDan Williams 	ihost->task_context_dma = dma;
2252d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->host_task_table_lower);
2253d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->host_task_table_upper);
2254cc9203bfSDan Williams 
2255*89a7301fSDan Williams 	err = sci_unsolicited_frame_control_construct(ihost);
22567c78da31SDan Williams 	if (err)
22577c78da31SDan Williams 		return err;
2258cc9203bfSDan Williams 
2259cc9203bfSDan Williams 	/*
2260cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2261cc9203bfSDan Williams 	 * address table.
2262cc9203bfSDan Williams 	 */
2263d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2264d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_lower);
2265d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2266d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_upper);
2267cc9203bfSDan Williams 
2268d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2269d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_lower);
2270d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2271d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_upper);
2272cc9203bfSDan Williams 
2273cc9203bfSDan Williams 	return 0;
2274cc9203bfSDan Williams }
2275cc9203bfSDan Williams 
2276d9dcb4baSDan Williams int isci_host_init(struct isci_host *ihost)
22776f231ddaSDan Williams {
2278d9c37390SDan Williams 	int err = 0, i;
22796f231ddaSDan Williams 	enum sci_status status;
2280*89a7301fSDan Williams 	struct sci_user_parameters sci_user_params;
2281d9dcb4baSDan Williams 	struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
22826f231ddaSDan Williams 
2283d9dcb4baSDan Williams 	spin_lock_init(&ihost->state_lock);
2284d9dcb4baSDan Williams 	spin_lock_init(&ihost->scic_lock);
2285d9dcb4baSDan Williams 	init_waitqueue_head(&ihost->eventq);
22866f231ddaSDan Williams 
2287d9dcb4baSDan Williams 	isci_host_change_state(ihost, isci_starting);
22886f231ddaSDan Williams 
2289*89a7301fSDan Williams 	status = sci_controller_construct(ihost, scu_base(ihost),
2290d9dcb4baSDan Williams 					  smu_base(ihost));
22916f231ddaSDan Williams 
22926f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2293d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
2294*89a7301fSDan Williams 			"%s: sci_controller_construct failed - status = %x\n",
22956f231ddaSDan Williams 			__func__,
22966f231ddaSDan Williams 			status);
2297858d4aa7SDave Jiang 		return -ENODEV;
22986f231ddaSDan Williams 	}
22996f231ddaSDan Williams 
2300d9dcb4baSDan Williams 	ihost->sas_ha.dev = &ihost->pdev->dev;
2301d9dcb4baSDan Williams 	ihost->sas_ha.lldd_ha = ihost;
23026f231ddaSDan Williams 
2303d044af17SDan Williams 	/*
2304d044af17SDan Williams 	 * grab initial values stored in the controller object for OEM and USER
2305d044af17SDan Williams 	 * parameters
2306d044af17SDan Williams 	 */
2307*89a7301fSDan Williams 	isci_user_parameters_get(&sci_user_params);
2308*89a7301fSDan Williams 	status = sci_user_parameters_set(ihost, &sci_user_params);
2309d044af17SDan Williams 	if (status != SCI_SUCCESS) {
2310d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2311*89a7301fSDan Williams 			 "%s: sci_user_parameters_set failed\n",
2312d044af17SDan Williams 			 __func__);
2313d044af17SDan Williams 		return -ENODEV;
2314d044af17SDan Williams 	}
23156f231ddaSDan Williams 
2316d044af17SDan Williams 	/* grab any OEM parameters specified in orom */
2317d044af17SDan Williams 	if (pci_info->orom) {
2318*89a7301fSDan Williams 		status = isci_parse_oem_parameters(&ihost->oem_parameters,
2319d044af17SDan Williams 						   pci_info->orom,
2320d9dcb4baSDan Williams 						   ihost->id);
23216f231ddaSDan Williams 		if (status != SCI_SUCCESS) {
2322d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
23236f231ddaSDan Williams 				 "parsing firmware oem parameters failed\n");
2324858d4aa7SDave Jiang 			return -EINVAL;
23256f231ddaSDan Williams 		}
23264711ba10SDan Williams 	}
23274711ba10SDan Williams 
2328*89a7301fSDan Williams 	status = sci_oem_parameters_set(ihost);
23296f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2330d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2331*89a7301fSDan Williams 				"%s: sci_oem_parameters_set failed\n",
23326f231ddaSDan Williams 				__func__);
2333858d4aa7SDave Jiang 		return -ENODEV;
23346f231ddaSDan Williams 	}
23356f231ddaSDan Williams 
2336d9dcb4baSDan Williams 	tasklet_init(&ihost->completion_tasklet,
2337d9dcb4baSDan Williams 		     isci_host_completion_routine, (unsigned long)ihost);
23386f231ddaSDan Williams 
2339d9dcb4baSDan Williams 	INIT_LIST_HEAD(&ihost->requests_to_complete);
2340d9dcb4baSDan Williams 	INIT_LIST_HEAD(&ihost->requests_to_errorback);
23416f231ddaSDan Williams 
2342d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
2343*89a7301fSDan Williams 	status = sci_controller_initialize(ihost);
2344d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
23457c40a803SDan Williams 	if (status != SCI_SUCCESS) {
2346d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2347*89a7301fSDan Williams 			 "%s: sci_controller_initialize failed -"
23487c40a803SDan Williams 			 " status = 0x%x\n",
23497c40a803SDan Williams 			 __func__, status);
23507c40a803SDan Williams 		return -ENODEV;
23517c40a803SDan Williams 	}
23527c40a803SDan Williams 
2353*89a7301fSDan Williams 	err = sci_controller_mem_init(ihost);
23546f231ddaSDan Williams 	if (err)
2355858d4aa7SDave Jiang 		return err;
23566f231ddaSDan Williams 
2357d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
2358d9dcb4baSDan Williams 		isci_port_init(&ihost->ports[i], ihost, i);
23596f231ddaSDan Williams 
2360d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
2361d9dcb4baSDan Williams 		isci_phy_init(&ihost->phys[i], ihost, i);
2362d9c37390SDan Williams 
2363d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
2364d9dcb4baSDan Williams 		struct isci_remote_device *idev = &ihost->devices[i];
2365d9c37390SDan Williams 
2366d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->reqs_in_process);
2367d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->node);
2368d9c37390SDan Williams 	}
23696f231ddaSDan Williams 
2370db056250SDan Williams 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2371db056250SDan Williams 		struct isci_request *ireq;
2372db056250SDan Williams 		dma_addr_t dma;
2373db056250SDan Williams 
2374d9dcb4baSDan Williams 		ireq = dmam_alloc_coherent(&ihost->pdev->dev,
2375db056250SDan Williams 					   sizeof(struct isci_request), &dma,
2376db056250SDan Williams 					   GFP_KERNEL);
2377db056250SDan Williams 		if (!ireq)
2378db056250SDan Williams 			return -ENOMEM;
2379db056250SDan Williams 
2380d9dcb4baSDan Williams 		ireq->tc = &ihost->task_context_table[i];
2381d9dcb4baSDan Williams 		ireq->owning_controller = ihost;
2382db056250SDan Williams 		spin_lock_init(&ireq->state_lock);
2383db056250SDan Williams 		ireq->request_daddr = dma;
2384d9dcb4baSDan Williams 		ireq->isci_host = ihost;
2385d9dcb4baSDan Williams 		ihost->reqs[i] = ireq;
2386db056250SDan Williams 	}
2387db056250SDan Williams 
2388858d4aa7SDave Jiang 	return 0;
23896f231ddaSDan Williams }
2390cc9203bfSDan Williams 
2391*89a7301fSDan Williams void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
2392*89a7301fSDan Williams 			    struct isci_phy *iphy)
2393cc9203bfSDan Williams {
2394d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2395e301370aSEdmund Nadolski 	case SCIC_STARTING:
2396d9dcb4baSDan Williams 		sci_del_timer(&ihost->phy_timer);
2397d9dcb4baSDan Williams 		ihost->phy_startup_timer_pending = false;
2398d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2399ffe191c9SDan Williams 						  iport, iphy);
2400*89a7301fSDan Williams 		sci_controller_start_next_phy(ihost);
2401cc9203bfSDan Williams 		break;
2402e301370aSEdmund Nadolski 	case SCIC_READY:
2403d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2404ffe191c9SDan Williams 						  iport, iphy);
2405cc9203bfSDan Williams 		break;
2406cc9203bfSDan Williams 	default:
2407d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2408cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
240985280955SDan Williams 			"unexpected state %d\n", __func__, iphy->phy_index,
2410d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2411cc9203bfSDan Williams 	}
2412cc9203bfSDan Williams }
2413cc9203bfSDan Williams 
2414*89a7301fSDan Williams void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
2415*89a7301fSDan Williams 			      struct isci_phy *iphy)
2416cc9203bfSDan Williams {
2417d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2418e301370aSEdmund Nadolski 	case SCIC_STARTING:
2419e301370aSEdmund Nadolski 	case SCIC_READY:
2420d9dcb4baSDan Williams 		ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
2421ffe191c9SDan Williams 						   iport, iphy);
2422cc9203bfSDan Williams 		break;
2423cc9203bfSDan Williams 	default:
2424d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2425cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2426cc9203bfSDan Williams 			"unexpected state %d\n",
2427cc9203bfSDan Williams 			__func__,
242885280955SDan Williams 			iphy->phy_index,
2429d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2430cc9203bfSDan Williams 	}
2431cc9203bfSDan Williams }
2432cc9203bfSDan Williams 
2433*89a7301fSDan Williams static bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
2434cc9203bfSDan Williams {
2435cc9203bfSDan Williams 	u32 index;
2436cc9203bfSDan Williams 
2437d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
2438d9dcb4baSDan Williams 		if ((ihost->device_table[index] != NULL) &&
2439d9dcb4baSDan Williams 		   (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2440cc9203bfSDan Williams 			return true;
2441cc9203bfSDan Williams 	}
2442cc9203bfSDan Williams 
2443cc9203bfSDan Williams 	return false;
2444cc9203bfSDan Williams }
2445cc9203bfSDan Williams 
2446*89a7301fSDan Williams void sci_controller_remote_device_stopped(struct isci_host *ihost,
244778a6f06eSDan Williams 					  struct isci_remote_device *idev)
2448cc9203bfSDan Williams {
2449d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_STOPPING) {
2450d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2451cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2452cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2453d9dcb4baSDan Williams 			ihost, idev,
2454d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2455cc9203bfSDan Williams 		return;
2456cc9203bfSDan Williams 	}
2457cc9203bfSDan Williams 
2458*89a7301fSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
2459d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_STOPPED);
2460cc9203bfSDan Williams }
2461cc9203bfSDan Williams 
2462*89a7301fSDan Williams void sci_controller_post_request(struct isci_host *ihost, u32 request)
2463cc9203bfSDan Williams {
2464*89a7301fSDan Williams 	dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
2465*89a7301fSDan Williams 		__func__, ihost->id, request);
2466cc9203bfSDan Williams 
2467d9dcb4baSDan Williams 	writel(request, &ihost->smu_registers->post_context_port);
2468cc9203bfSDan Williams }
2469cc9203bfSDan Williams 
2470*89a7301fSDan Williams struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
2471cc9203bfSDan Williams {
2472cc9203bfSDan Williams 	u16 task_index;
2473cc9203bfSDan Williams 	u16 task_sequence;
2474cc9203bfSDan Williams 
2475dd047c8eSDan Williams 	task_index = ISCI_TAG_TCI(io_tag);
2476cc9203bfSDan Williams 
2477d9dcb4baSDan Williams 	if (task_index < ihost->task_context_entries) {
2478d9dcb4baSDan Williams 		struct isci_request *ireq = ihost->reqs[task_index];
2479db056250SDan Williams 
2480db056250SDan Williams 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2481dd047c8eSDan Williams 			task_sequence = ISCI_TAG_SEQ(io_tag);
2482cc9203bfSDan Williams 
2483d9dcb4baSDan Williams 			if (task_sequence == ihost->io_request_sequence[task_index])
24845076a1a9SDan Williams 				return ireq;
2485cc9203bfSDan Williams 		}
2486cc9203bfSDan Williams 	}
2487cc9203bfSDan Williams 
2488cc9203bfSDan Williams 	return NULL;
2489cc9203bfSDan Williams }
2490cc9203bfSDan Williams 
2491cc9203bfSDan Williams /**
2492cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2493cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2494cc9203bfSDan Williams  *    node index available.
2495cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2496cc9203bfSDan Williams  *    free remote node ids
2497cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2498cc9203bfSDan Williams  *    id
2499cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2500cc9203bfSDan Williams  *    is available
2501cc9203bfSDan Williams  *
2502cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2503cc9203bfSDan Williams  * node index available.
2504cc9203bfSDan Williams  */
2505*89a7301fSDan Williams enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
250678a6f06eSDan Williams 							    struct isci_remote_device *idev,
2507cc9203bfSDan Williams 							    u16 *node_id)
2508cc9203bfSDan Williams {
2509cc9203bfSDan Williams 	u16 node_index;
2510*89a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2511cc9203bfSDan Williams 
2512*89a7301fSDan Williams 	node_index = sci_remote_node_table_allocate_remote_node(
2513d9dcb4baSDan Williams 		&ihost->available_remote_nodes, remote_node_count
2514cc9203bfSDan Williams 		);
2515cc9203bfSDan Williams 
2516cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2517d9dcb4baSDan Williams 		ihost->device_table[node_index] = idev;
2518cc9203bfSDan Williams 
2519cc9203bfSDan Williams 		*node_id = node_index;
2520cc9203bfSDan Williams 
2521cc9203bfSDan Williams 		return SCI_SUCCESS;
2522cc9203bfSDan Williams 	}
2523cc9203bfSDan Williams 
2524cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2525cc9203bfSDan Williams }
2526cc9203bfSDan Williams 
2527*89a7301fSDan Williams void sci_controller_free_remote_node_context(struct isci_host *ihost,
252878a6f06eSDan Williams 					     struct isci_remote_device *idev,
2529cc9203bfSDan Williams 					     u16 node_id)
2530cc9203bfSDan Williams {
2531*89a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2532cc9203bfSDan Williams 
2533d9dcb4baSDan Williams 	if (ihost->device_table[node_id] == idev) {
2534d9dcb4baSDan Williams 		ihost->device_table[node_id] = NULL;
2535cc9203bfSDan Williams 
2536*89a7301fSDan Williams 		sci_remote_node_table_release_remote_node_index(
2537d9dcb4baSDan Williams 			&ihost->available_remote_nodes, remote_node_count, node_id
2538cc9203bfSDan Williams 			);
2539cc9203bfSDan Williams 	}
2540cc9203bfSDan Williams }
2541cc9203bfSDan Williams 
2542*89a7301fSDan Williams void sci_controller_copy_sata_response(void *response_buffer,
2543cc9203bfSDan Williams 				       void *frame_header,
2544cc9203bfSDan Williams 				       void *frame_buffer)
2545cc9203bfSDan Williams {
2546*89a7301fSDan Williams 	/* XXX type safety? */
2547cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2548cc9203bfSDan Williams 
2549cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2550cc9203bfSDan Williams 	       frame_buffer,
2551cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2552cc9203bfSDan Williams }
2553cc9203bfSDan Williams 
2554*89a7301fSDan Williams void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
2555cc9203bfSDan Williams {
2556*89a7301fSDan Williams 	if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
2557d9dcb4baSDan Williams 		writel(ihost->uf_control.get,
2558d9dcb4baSDan Williams 			&ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
2559cc9203bfSDan Williams }
2560cc9203bfSDan Williams 
2561312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci)
2562312e0c24SDan Williams {
2563312e0c24SDan Williams 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2564312e0c24SDan Williams 
2565312e0c24SDan Williams 	ihost->tci_pool[tail] = tci;
2566312e0c24SDan Williams 	ihost->tci_tail = tail + 1;
2567312e0c24SDan Williams }
2568312e0c24SDan Williams 
2569312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost)
2570312e0c24SDan Williams {
2571312e0c24SDan Williams 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2572312e0c24SDan Williams 	u16 tci = ihost->tci_pool[head];
2573312e0c24SDan Williams 
2574312e0c24SDan Williams 	ihost->tci_head = head + 1;
2575312e0c24SDan Williams 	return tci;
2576312e0c24SDan Williams }
2577312e0c24SDan Williams 
2578312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost)
2579312e0c24SDan Williams {
2580312e0c24SDan Williams 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2581312e0c24SDan Williams }
2582312e0c24SDan Williams 
2583312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost)
2584312e0c24SDan Williams {
2585312e0c24SDan Williams 	if (isci_tci_space(ihost)) {
2586312e0c24SDan Williams 		u16 tci = isci_tci_alloc(ihost);
2587d9dcb4baSDan Williams 		u8 seq = ihost->io_request_sequence[tci];
2588312e0c24SDan Williams 
2589312e0c24SDan Williams 		return ISCI_TAG(seq, tci);
2590312e0c24SDan Williams 	}
2591312e0c24SDan Williams 
2592312e0c24SDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
2593312e0c24SDan Williams }
2594312e0c24SDan Williams 
2595312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2596312e0c24SDan Williams {
2597312e0c24SDan Williams 	u16 tci = ISCI_TAG_TCI(io_tag);
2598312e0c24SDan Williams 	u16 seq = ISCI_TAG_SEQ(io_tag);
2599312e0c24SDan Williams 
2600312e0c24SDan Williams 	/* prevent tail from passing head */
2601312e0c24SDan Williams 	if (isci_tci_active(ihost) == 0)
2602312e0c24SDan Williams 		return SCI_FAILURE_INVALID_IO_TAG;
2603312e0c24SDan Williams 
2604d9dcb4baSDan Williams 	if (seq == ihost->io_request_sequence[tci]) {
2605d9dcb4baSDan Williams 		ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2606312e0c24SDan Williams 
2607312e0c24SDan Williams 		isci_tci_free(ihost, tci);
2608312e0c24SDan Williams 
2609312e0c24SDan Williams 		return SCI_SUCCESS;
2610312e0c24SDan Williams 	}
2611312e0c24SDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
2612312e0c24SDan Williams }
2613312e0c24SDan Williams 
2614*89a7301fSDan Williams enum sci_status sci_controller_start_io(struct isci_host *ihost,
261578a6f06eSDan Williams 					struct isci_remote_device *idev,
26165076a1a9SDan Williams 					struct isci_request *ireq)
2617cc9203bfSDan Williams {
2618cc9203bfSDan Williams 	enum sci_status status;
2619cc9203bfSDan Williams 
2620d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2621d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "invalid state to start I/O");
2622cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2623cc9203bfSDan Williams 	}
2624cc9203bfSDan Williams 
2625*89a7301fSDan Williams 	status = sci_remote_device_start_io(ihost, idev, ireq);
2626cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2627cc9203bfSDan Williams 		return status;
2628cc9203bfSDan Williams 
26295076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
2630*89a7301fSDan Williams 	sci_controller_post_request(ihost, sci_request_get_post_context(ireq));
2631cc9203bfSDan Williams 	return SCI_SUCCESS;
2632cc9203bfSDan Williams }
2633cc9203bfSDan Williams 
2634*89a7301fSDan Williams enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
263578a6f06eSDan Williams 						 struct isci_remote_device *idev,
26365076a1a9SDan Williams 						 struct isci_request *ireq)
2637cc9203bfSDan Williams {
2638*89a7301fSDan Williams 	/* terminate an ongoing (i.e. started) core IO request.  This does not
2639*89a7301fSDan Williams 	 * abort the IO request at the target, but rather removes the IO
2640*89a7301fSDan Williams 	 * request from the host controller.
2641*89a7301fSDan Williams 	 */
2642cc9203bfSDan Williams 	enum sci_status status;
2643cc9203bfSDan Williams 
2644d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2645d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2646cc9203bfSDan Williams 			 "invalid state to terminate request\n");
2647cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2648cc9203bfSDan Williams 	}
2649cc9203bfSDan Williams 
2650*89a7301fSDan Williams 	status = sci_io_request_terminate(ireq);
2651cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2652cc9203bfSDan Williams 		return status;
2653cc9203bfSDan Williams 
2654cc9203bfSDan Williams 	/*
2655cc9203bfSDan Williams 	 * Utilize the original post context command and or in the POST_TC_ABORT
2656cc9203bfSDan Williams 	 * request sub-type.
2657cc9203bfSDan Williams 	 */
2658*89a7301fSDan Williams 	sci_controller_post_request(ihost,
2659*89a7301fSDan Williams 				    ireq->post_context | SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2660cc9203bfSDan Williams 	return SCI_SUCCESS;
2661cc9203bfSDan Williams }
2662cc9203bfSDan Williams 
2663cc9203bfSDan Williams /**
2664*89a7301fSDan Williams  * sci_controller_complete_io() - This method will perform core specific
2665cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
2666cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
2667cc9203bfSDan Williams  *    reused (i.e. re-constructed).
2668*89a7301fSDan Williams  * @ihost: The handle to the controller object for which to complete the
2669cc9203bfSDan Williams  *    IO request.
2670*89a7301fSDan Williams  * @idev: The handle to the remote device object for which to complete
2671cc9203bfSDan Williams  *    the IO request.
2672*89a7301fSDan Williams  * @ireq: the handle to the io request object to complete.
2673cc9203bfSDan Williams  */
2674*89a7301fSDan Williams enum sci_status sci_controller_complete_io(struct isci_host *ihost,
267578a6f06eSDan Williams 					   struct isci_remote_device *idev,
26765076a1a9SDan Williams 					   struct isci_request *ireq)
2677cc9203bfSDan Williams {
2678cc9203bfSDan Williams 	enum sci_status status;
2679cc9203bfSDan Williams 	u16 index;
2680cc9203bfSDan Williams 
2681d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2682e301370aSEdmund Nadolski 	case SCIC_STOPPING:
2683cc9203bfSDan Williams 		/* XXX: Implement this function */
2684cc9203bfSDan Williams 		return SCI_FAILURE;
2685e301370aSEdmund Nadolski 	case SCIC_READY:
2686*89a7301fSDan Williams 		status = sci_remote_device_complete_io(ihost, idev, ireq);
2687cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
2688cc9203bfSDan Williams 			return status;
2689cc9203bfSDan Williams 
26905076a1a9SDan Williams 		index = ISCI_TAG_TCI(ireq->io_tag);
26915076a1a9SDan Williams 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2692cc9203bfSDan Williams 		return SCI_SUCCESS;
2693cc9203bfSDan Williams 	default:
2694d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "invalid state to complete I/O");
2695cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2696cc9203bfSDan Williams 	}
2697cc9203bfSDan Williams 
2698cc9203bfSDan Williams }
2699cc9203bfSDan Williams 
2700*89a7301fSDan Williams enum sci_status sci_controller_continue_io(struct isci_request *ireq)
2701cc9203bfSDan Williams {
2702d9dcb4baSDan Williams 	struct isci_host *ihost = ireq->owning_controller;
2703cc9203bfSDan Williams 
2704d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2705d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "invalid state to continue I/O");
2706cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2707cc9203bfSDan Williams 	}
2708cc9203bfSDan Williams 
27095076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
2710*89a7301fSDan Williams 	sci_controller_post_request(ihost, sci_request_get_post_context(ireq));
2711cc9203bfSDan Williams 	return SCI_SUCCESS;
2712cc9203bfSDan Williams }
2713cc9203bfSDan Williams 
2714cc9203bfSDan Williams /**
2715*89a7301fSDan Williams  * sci_controller_start_task() - This method is called by the SCIC user to
2716cc9203bfSDan Williams  *    send/start a framework task management request.
2717cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
2718cc9203bfSDan Williams  *    management request.
2719cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
2720cc9203bfSDan Williams  *    the task management request.
2721cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
2722cc9203bfSDan Williams  */
2723*89a7301fSDan Williams enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
272478a6f06eSDan Williams 					       struct isci_remote_device *idev,
27255076a1a9SDan Williams 					       struct isci_request *ireq)
2726cc9203bfSDan Williams {
2727cc9203bfSDan Williams 	enum sci_status status;
2728cc9203bfSDan Williams 
2729d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2730d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2731cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
2732cc9203bfSDan Williams 			 "state\n",
2733cc9203bfSDan Williams 			 __func__);
2734cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
2735cc9203bfSDan Williams 	}
2736cc9203bfSDan Williams 
2737*89a7301fSDan Williams 	status = sci_remote_device_start_task(ihost, idev, ireq);
2738cc9203bfSDan Williams 	switch (status) {
2739cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2740db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2741cc9203bfSDan Williams 
2742cc9203bfSDan Williams 		/*
2743cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
2744cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
2745cc9203bfSDan Williams 		 * RNC is resumed.)
2746cc9203bfSDan Williams 		 */
2747cc9203bfSDan Williams 		return SCI_SUCCESS;
2748cc9203bfSDan Williams 	case SCI_SUCCESS:
2749db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2750cc9203bfSDan Williams 
2751*89a7301fSDan Williams 		sci_controller_post_request(ihost,
2752*89a7301fSDan Williams 			sci_request_get_post_context(ireq));
2753cc9203bfSDan Williams 		break;
2754cc9203bfSDan Williams 	default:
2755cc9203bfSDan Williams 		break;
2756cc9203bfSDan Williams 	}
2757cc9203bfSDan Williams 
2758cc9203bfSDan Williams 	return status;
2759cc9203bfSDan Williams }
2760