xref: /openbmc/linux/drivers/scsi/isci/host.c (revision be778341812dc75b1c515fab6ebd39c0daf1e2bc)
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"
706f231ddaSDan Williams 
71cc9203bfSDan Williams #define SCU_CONTEXT_RAM_INIT_STALL_TIME      200
72cc9203bfSDan Williams 
737c78da31SDan Williams #define smu_max_ports(dcc_value) \
74cc9203bfSDan Williams 	(\
75cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
76cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
77cc9203bfSDan Williams 	)
78cc9203bfSDan Williams 
797c78da31SDan Williams #define smu_max_task_contexts(dcc_value)	\
80cc9203bfSDan Williams 	(\
81cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
82cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
83cc9203bfSDan Williams 	)
84cc9203bfSDan Williams 
857c78da31SDan Williams #define smu_max_rncs(dcc_value) \
86cc9203bfSDan Williams 	(\
87cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
88cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
89cc9203bfSDan Williams 	)
90cc9203bfSDan Williams 
91cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT      100
92cc9203bfSDan Williams 
93cc9203bfSDan Williams /**
94cc9203bfSDan Williams  *
95cc9203bfSDan Williams  *
96cc9203bfSDan Williams  * The number of milliseconds to wait while a given phy is consuming power
97cc9203bfSDan Williams  * before allowing another set of phys to consume power. Ultimately, this will
98cc9203bfSDan Williams  * be specified by OEM parameter.
99cc9203bfSDan Williams  */
100cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
101cc9203bfSDan Williams 
102cc9203bfSDan Williams /**
103cc9203bfSDan Williams  * NORMALIZE_PUT_POINTER() -
104cc9203bfSDan Williams  *
105cc9203bfSDan Williams  * This macro will normalize the completion queue put pointer so its value can
106cc9203bfSDan Williams  * be used as an array inde
107cc9203bfSDan Williams  */
108cc9203bfSDan Williams #define NORMALIZE_PUT_POINTER(x) \
109cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
110cc9203bfSDan Williams 
111cc9203bfSDan Williams 
112cc9203bfSDan Williams /**
113cc9203bfSDan Williams  * NORMALIZE_EVENT_POINTER() -
114cc9203bfSDan Williams  *
115cc9203bfSDan Williams  * This macro will normalize the completion queue event entry so its value can
116cc9203bfSDan Williams  * be used as an index.
117cc9203bfSDan Williams  */
118cc9203bfSDan Williams #define NORMALIZE_EVENT_POINTER(x) \
119cc9203bfSDan Williams 	(\
120cc9203bfSDan Williams 		((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
121cc9203bfSDan Williams 		>> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT	\
122cc9203bfSDan Williams 	)
123cc9203bfSDan Williams 
124cc9203bfSDan Williams /**
125cc9203bfSDan Williams  * NORMALIZE_GET_POINTER() -
126cc9203bfSDan Williams  *
127cc9203bfSDan Williams  * This macro will normalize the completion queue get pointer so its value can
128cc9203bfSDan Williams  * be used as an index into an array
129cc9203bfSDan Williams  */
130cc9203bfSDan Williams #define NORMALIZE_GET_POINTER(x) \
131cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
132cc9203bfSDan Williams 
133cc9203bfSDan Williams /**
134cc9203bfSDan Williams  * NORMALIZE_GET_POINTER_CYCLE_BIT() -
135cc9203bfSDan Williams  *
136cc9203bfSDan Williams  * This macro will normalize the completion queue cycle pointer so it matches
137cc9203bfSDan Williams  * the completion queue cycle bit
138cc9203bfSDan Williams  */
139cc9203bfSDan Williams #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
140cc9203bfSDan Williams 	((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
141cc9203bfSDan Williams 
142cc9203bfSDan Williams /**
143cc9203bfSDan Williams  * COMPLETION_QUEUE_CYCLE_BIT() -
144cc9203bfSDan Williams  *
145cc9203bfSDan Williams  * This macro will return the cycle bit of the completion queue entry
146cc9203bfSDan Williams  */
147cc9203bfSDan Williams #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
148cc9203bfSDan Williams 
14912ef6544SEdmund Nadolski /* Init the state machine and call the state entry function (if any) */
15012ef6544SEdmund Nadolski void sci_init_sm(struct sci_base_state_machine *sm,
15112ef6544SEdmund Nadolski 		 const struct sci_base_state *state_table, u32 initial_state)
15212ef6544SEdmund Nadolski {
15312ef6544SEdmund Nadolski 	sci_state_transition_t handler;
15412ef6544SEdmund Nadolski 
15512ef6544SEdmund Nadolski 	sm->initial_state_id    = initial_state;
15612ef6544SEdmund Nadolski 	sm->previous_state_id   = initial_state;
15712ef6544SEdmund Nadolski 	sm->current_state_id    = initial_state;
15812ef6544SEdmund Nadolski 	sm->state_table         = state_table;
15912ef6544SEdmund Nadolski 
16012ef6544SEdmund Nadolski 	handler = sm->state_table[initial_state].enter_state;
16112ef6544SEdmund Nadolski 	if (handler)
16212ef6544SEdmund Nadolski 		handler(sm);
16312ef6544SEdmund Nadolski }
16412ef6544SEdmund Nadolski 
16512ef6544SEdmund Nadolski /* Call the state exit fn, update the current state, call the state entry fn */
16612ef6544SEdmund Nadolski void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
16712ef6544SEdmund Nadolski {
16812ef6544SEdmund Nadolski 	sci_state_transition_t handler;
16912ef6544SEdmund Nadolski 
17012ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].exit_state;
17112ef6544SEdmund Nadolski 	if (handler)
17212ef6544SEdmund Nadolski 		handler(sm);
17312ef6544SEdmund Nadolski 
17412ef6544SEdmund Nadolski 	sm->previous_state_id = sm->current_state_id;
17512ef6544SEdmund Nadolski 	sm->current_state_id = next_state;
17612ef6544SEdmund Nadolski 
17712ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].enter_state;
17812ef6544SEdmund Nadolski 	if (handler)
17912ef6544SEdmund Nadolski 		handler(sm);
18012ef6544SEdmund Nadolski }
18112ef6544SEdmund Nadolski 
18289a7301fSDan Williams static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
183cc9203bfSDan Williams {
184d9dcb4baSDan Williams 	u32 get_value = ihost->completion_queue_get;
185cc9203bfSDan Williams 	u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
186cc9203bfSDan Williams 
187cc9203bfSDan Williams 	if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
188d9dcb4baSDan Williams 	    COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
189cc9203bfSDan Williams 		return true;
190cc9203bfSDan Williams 
191cc9203bfSDan Williams 	return false;
192cc9203bfSDan Williams }
193cc9203bfSDan Williams 
19489a7301fSDan Williams static bool sci_controller_isr(struct isci_host *ihost)
195cc9203bfSDan Williams {
19689a7301fSDan Williams 	if (sci_controller_completion_queue_has_entries(ihost)) {
197cc9203bfSDan Williams 		return true;
198cc9203bfSDan Williams 	} else {
199cc9203bfSDan Williams 		/*
200cc9203bfSDan Williams 		 * we have a spurious interrupt it could be that we have already
201cc9203bfSDan Williams 		 * emptied the completion queue from a previous interrupt */
202d9dcb4baSDan Williams 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
203cc9203bfSDan Williams 
204cc9203bfSDan Williams 		/*
205cc9203bfSDan Williams 		 * There is a race in the hardware that could cause us not to be notified
206cc9203bfSDan Williams 		 * of an interrupt completion if we do not take this step.  We will mask
207cc9203bfSDan Williams 		 * then unmask the interrupts so if there is another interrupt pending
208cc9203bfSDan Williams 		 * the clearing of the interrupt source we get the next interrupt message. */
209d9dcb4baSDan Williams 		writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
210d9dcb4baSDan Williams 		writel(0, &ihost->smu_registers->interrupt_mask);
211cc9203bfSDan Williams 	}
212cc9203bfSDan Williams 
213cc9203bfSDan Williams 	return false;
214cc9203bfSDan Williams }
215cc9203bfSDan Williams 
216c7ef4031SDan Williams irqreturn_t isci_msix_isr(int vec, void *data)
2176f231ddaSDan Williams {
218c7ef4031SDan Williams 	struct isci_host *ihost = data;
2196f231ddaSDan Williams 
22089a7301fSDan Williams 	if (sci_controller_isr(ihost))
221c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
2226f231ddaSDan Williams 
223c7ef4031SDan Williams 	return IRQ_HANDLED;
224c7ef4031SDan Williams }
225c7ef4031SDan Williams 
22689a7301fSDan Williams static bool sci_controller_error_isr(struct isci_host *ihost)
227cc9203bfSDan Williams {
228cc9203bfSDan Williams 	u32 interrupt_status;
229cc9203bfSDan Williams 
230cc9203bfSDan Williams 	interrupt_status =
231d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
232cc9203bfSDan Williams 	interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
233cc9203bfSDan Williams 
234cc9203bfSDan Williams 	if (interrupt_status != 0) {
235cc9203bfSDan Williams 		/*
236cc9203bfSDan Williams 		 * There is an error interrupt pending so let it through and handle
237cc9203bfSDan Williams 		 * in the callback */
238cc9203bfSDan Williams 		return true;
239cc9203bfSDan Williams 	}
240cc9203bfSDan Williams 
241cc9203bfSDan Williams 	/*
242cc9203bfSDan Williams 	 * There is a race in the hardware that could cause us not to be notified
243cc9203bfSDan Williams 	 * of an interrupt completion if we do not take this step.  We will mask
244cc9203bfSDan Williams 	 * then unmask the error interrupts so if there was another interrupt
245cc9203bfSDan Williams 	 * pending we will be notified.
246cc9203bfSDan Williams 	 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
247d9dcb4baSDan Williams 	writel(0xff, &ihost->smu_registers->interrupt_mask);
248d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
249cc9203bfSDan Williams 
250cc9203bfSDan Williams 	return false;
251cc9203bfSDan Williams }
252cc9203bfSDan Williams 
25389a7301fSDan Williams static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
254cc9203bfSDan Williams {
25589a7301fSDan Williams 	u32 index = SCU_GET_COMPLETION_INDEX(ent);
256db056250SDan Williams 	struct isci_request *ireq = ihost->reqs[index];
257cc9203bfSDan Williams 
258cc9203bfSDan Williams 	/* Make sure that we really want to process this IO request */
259db056250SDan Williams 	if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
2605076a1a9SDan Williams 	    ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
261d9dcb4baSDan Williams 	    ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
26289a7301fSDan Williams 		/* Yep this is a valid io request pass it along to the
26389a7301fSDan Williams 		 * io request handler
26489a7301fSDan Williams 		 */
26589a7301fSDan Williams 		sci_io_request_tc_completion(ireq, ent);
266cc9203bfSDan Williams }
267cc9203bfSDan Williams 
26889a7301fSDan Williams static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
269cc9203bfSDan Williams {
270cc9203bfSDan Williams 	u32 index;
2715076a1a9SDan Williams 	struct isci_request *ireq;
27278a6f06eSDan Williams 	struct isci_remote_device *idev;
273cc9203bfSDan Williams 
27489a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
275cc9203bfSDan Williams 
27689a7301fSDan Williams 	switch (scu_get_command_request_type(ent)) {
277cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
278cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
279d9dcb4baSDan Williams 		ireq = ihost->reqs[index];
280d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
28189a7301fSDan Williams 			 __func__, ent, ireq);
282cc9203bfSDan Williams 		/* @todo For a post TC operation we need to fail the IO
283cc9203bfSDan Williams 		 * request
284cc9203bfSDan Williams 		 */
285cc9203bfSDan Williams 		break;
286cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
287cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
288cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
289d9dcb4baSDan Williams 		idev = ihost->device_table[index];
290d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
29189a7301fSDan Williams 			 __func__, ent, idev);
292cc9203bfSDan Williams 		/* @todo For a port RNC operation we need to fail the
293cc9203bfSDan Williams 		 * device
294cc9203bfSDan Williams 		 */
295cc9203bfSDan Williams 		break;
296cc9203bfSDan Williams 	default:
297d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
29889a7301fSDan Williams 			 __func__, ent);
299cc9203bfSDan Williams 		break;
300cc9203bfSDan Williams 	}
301cc9203bfSDan Williams }
302cc9203bfSDan Williams 
30389a7301fSDan Williams static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
304cc9203bfSDan Williams {
305cc9203bfSDan Williams 	u32 index;
306cc9203bfSDan Williams 	u32 frame_index;
307cc9203bfSDan Williams 
308cc9203bfSDan Williams 	struct scu_unsolicited_frame_header *frame_header;
30985280955SDan Williams 	struct isci_phy *iphy;
31078a6f06eSDan Williams 	struct isci_remote_device *idev;
311cc9203bfSDan Williams 
312cc9203bfSDan Williams 	enum sci_status result = SCI_FAILURE;
313cc9203bfSDan Williams 
31489a7301fSDan Williams 	frame_index = SCU_GET_FRAME_INDEX(ent);
315cc9203bfSDan Williams 
316d9dcb4baSDan Williams 	frame_header = ihost->uf_control.buffers.array[frame_index].header;
317d9dcb4baSDan Williams 	ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
318cc9203bfSDan Williams 
31989a7301fSDan Williams 	if (SCU_GET_FRAME_ERROR(ent)) {
320cc9203bfSDan Williams 		/*
321cc9203bfSDan Williams 		 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
322cc9203bfSDan Williams 		 * /       this cause a problem? We expect the phy initialization will
323cc9203bfSDan Williams 		 * /       fail if there is an error in the frame. */
32489a7301fSDan Williams 		sci_controller_release_frame(ihost, frame_index);
325cc9203bfSDan Williams 		return;
326cc9203bfSDan Williams 	}
327cc9203bfSDan Williams 
328cc9203bfSDan Williams 	if (frame_header->is_address_frame) {
32989a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
33085280955SDan Williams 		iphy = &ihost->phys[index];
33189a7301fSDan Williams 		result = sci_phy_frame_handler(iphy, frame_index);
332cc9203bfSDan Williams 	} else {
333cc9203bfSDan Williams 
33489a7301fSDan Williams 		index = SCU_GET_COMPLETION_INDEX(ent);
335cc9203bfSDan Williams 
336cc9203bfSDan Williams 		if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
337cc9203bfSDan Williams 			/*
338cc9203bfSDan Williams 			 * This is a signature fis or a frame from a direct attached SATA
339cc9203bfSDan Williams 			 * device that has not yet been created.  In either case forwared
340cc9203bfSDan Williams 			 * the frame to the PE and let it take care of the frame data. */
34189a7301fSDan Williams 			index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
34285280955SDan Williams 			iphy = &ihost->phys[index];
34389a7301fSDan Williams 			result = sci_phy_frame_handler(iphy, frame_index);
344cc9203bfSDan Williams 		} else {
345d9dcb4baSDan Williams 			if (index < ihost->remote_node_entries)
346d9dcb4baSDan Williams 				idev = ihost->device_table[index];
347cc9203bfSDan Williams 			else
34878a6f06eSDan Williams 				idev = NULL;
349cc9203bfSDan Williams 
35078a6f06eSDan Williams 			if (idev != NULL)
35189a7301fSDan Williams 				result = sci_remote_device_frame_handler(idev, frame_index);
352cc9203bfSDan Williams 			else
35389a7301fSDan Williams 				sci_controller_release_frame(ihost, frame_index);
354cc9203bfSDan Williams 		}
355cc9203bfSDan Williams 	}
356cc9203bfSDan Williams 
357cc9203bfSDan Williams 	if (result != SCI_SUCCESS) {
358cc9203bfSDan Williams 		/*
359cc9203bfSDan Williams 		 * / @todo Is there any reason to report some additional error message
360cc9203bfSDan Williams 		 * /       when we get this failure notifiction? */
361cc9203bfSDan Williams 	}
362cc9203bfSDan Williams }
363cc9203bfSDan Williams 
36489a7301fSDan Williams static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
365cc9203bfSDan Williams {
36678a6f06eSDan Williams 	struct isci_remote_device *idev;
3675076a1a9SDan Williams 	struct isci_request *ireq;
36885280955SDan Williams 	struct isci_phy *iphy;
369cc9203bfSDan Williams 	u32 index;
370cc9203bfSDan Williams 
37189a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
372cc9203bfSDan Williams 
37389a7301fSDan Williams 	switch (scu_get_event_type(ent)) {
374cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
375cc9203bfSDan Williams 		/* / @todo The driver did something wrong and we need to fix the condtion. */
376d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
377cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received SMU command error "
378cc9203bfSDan Williams 			"0x%x\n",
379cc9203bfSDan Williams 			__func__,
380d9dcb4baSDan Williams 			ihost,
38189a7301fSDan Williams 			ent);
382cc9203bfSDan Williams 		break;
383cc9203bfSDan Williams 
384cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
385cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_ERROR:
386cc9203bfSDan Williams 	case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
387cc9203bfSDan Williams 		/*
388cc9203bfSDan Williams 		 * / @todo This is a hardware failure and its likely that we want to
389cc9203bfSDan Williams 		 * /       reset the controller. */
390d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
391cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received fatal controller "
392cc9203bfSDan Williams 			"event  0x%x\n",
393cc9203bfSDan Williams 			__func__,
394d9dcb4baSDan Williams 			ihost,
39589a7301fSDan Williams 			ent);
396cc9203bfSDan Williams 		break;
397cc9203bfSDan Williams 
398cc9203bfSDan Williams 	case SCU_EVENT_TYPE_TRANSPORT_ERROR:
3995076a1a9SDan Williams 		ireq = ihost->reqs[index];
40089a7301fSDan Williams 		sci_io_request_event_handler(ireq, ent);
401cc9203bfSDan Williams 		break;
402cc9203bfSDan Williams 
403cc9203bfSDan Williams 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
40489a7301fSDan Williams 		switch (scu_get_event_specifier(ent)) {
405cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
406cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
4075076a1a9SDan Williams 			ireq = ihost->reqs[index];
4085076a1a9SDan Williams 			if (ireq != NULL)
40989a7301fSDan Williams 				sci_io_request_event_handler(ireq, ent);
410cc9203bfSDan Williams 			else
411d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
412cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
413cc9203bfSDan Williams 					 "event 0x%x for io request object "
414cc9203bfSDan Williams 					 "that doesnt exist.\n",
415cc9203bfSDan Williams 					 __func__,
416d9dcb4baSDan Williams 					 ihost,
41789a7301fSDan Williams 					 ent);
418cc9203bfSDan Williams 
419cc9203bfSDan Williams 			break;
420cc9203bfSDan Williams 
421cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
422d9dcb4baSDan Williams 			idev = ihost->device_table[index];
42378a6f06eSDan Williams 			if (idev != NULL)
42489a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
425cc9203bfSDan Williams 			else
426d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
427cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
428cc9203bfSDan Williams 					 "event 0x%x for remote device object "
429cc9203bfSDan Williams 					 "that doesnt exist.\n",
430cc9203bfSDan Williams 					 __func__,
431d9dcb4baSDan Williams 					 ihost,
43289a7301fSDan Williams 					 ent);
433cc9203bfSDan Williams 
434cc9203bfSDan Williams 			break;
435cc9203bfSDan Williams 		}
436cc9203bfSDan Williams 		break;
437cc9203bfSDan Williams 
438cc9203bfSDan Williams 	case SCU_EVENT_TYPE_BROADCAST_CHANGE:
439cc9203bfSDan Williams 	/*
440cc9203bfSDan Williams 	 * direct the broadcast change event to the phy first and then let
441cc9203bfSDan Williams 	 * the phy redirect the broadcast change to the port object */
442cc9203bfSDan Williams 	case SCU_EVENT_TYPE_ERR_CNT_EVENT:
443cc9203bfSDan Williams 	/*
444cc9203bfSDan Williams 	 * direct error counter event to the phy object since that is where
445cc9203bfSDan Williams 	 * we get the event notification.  This is a type 4 event. */
446cc9203bfSDan Williams 	case SCU_EVENT_TYPE_OSSP_EVENT:
44789a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
44885280955SDan Williams 		iphy = &ihost->phys[index];
44989a7301fSDan Williams 		sci_phy_event_handler(iphy, ent);
450cc9203bfSDan Williams 		break;
451cc9203bfSDan Williams 
452cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
453cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
454cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
455d9dcb4baSDan Williams 		if (index < ihost->remote_node_entries) {
456d9dcb4baSDan Williams 			idev = ihost->device_table[index];
457cc9203bfSDan Williams 
45878a6f06eSDan Williams 			if (idev != NULL)
45989a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
460cc9203bfSDan Williams 		} else
461d9dcb4baSDan Williams 			dev_err(&ihost->pdev->dev,
462cc9203bfSDan Williams 				"%s: SCIC Controller 0x%p received event 0x%x "
463cc9203bfSDan Williams 				"for remote device object 0x%0x that doesnt "
464cc9203bfSDan Williams 				"exist.\n",
465cc9203bfSDan Williams 				__func__,
466d9dcb4baSDan Williams 				ihost,
46789a7301fSDan Williams 				ent,
468cc9203bfSDan Williams 				index);
469cc9203bfSDan Williams 
470cc9203bfSDan Williams 		break;
471cc9203bfSDan Williams 
472cc9203bfSDan Williams 	default:
473d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
474cc9203bfSDan Williams 			 "%s: SCIC Controller received unknown event code %x\n",
475cc9203bfSDan Williams 			 __func__,
47689a7301fSDan Williams 			 ent);
477cc9203bfSDan Williams 		break;
478cc9203bfSDan Williams 	}
479cc9203bfSDan Williams }
480cc9203bfSDan Williams 
48189a7301fSDan Williams static void sci_controller_process_completions(struct isci_host *ihost)
482cc9203bfSDan Williams {
483cc9203bfSDan Williams 	u32 completion_count = 0;
48489a7301fSDan Williams 	u32 ent;
485cc9203bfSDan Williams 	u32 get_index;
486cc9203bfSDan Williams 	u32 get_cycle;
487994a9303SDan Williams 	u32 event_get;
488cc9203bfSDan Williams 	u32 event_cycle;
489cc9203bfSDan Williams 
490d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
491cc9203bfSDan Williams 		"%s: completion queue begining get:0x%08x\n",
492cc9203bfSDan Williams 		__func__,
493d9dcb4baSDan Williams 		ihost->completion_queue_get);
494cc9203bfSDan Williams 
495cc9203bfSDan Williams 	/* Get the component parts of the completion queue */
496d9dcb4baSDan Williams 	get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
497d9dcb4baSDan Williams 	get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
498cc9203bfSDan Williams 
499d9dcb4baSDan Williams 	event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
500d9dcb4baSDan Williams 	event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
501cc9203bfSDan Williams 
502cc9203bfSDan Williams 	while (
503cc9203bfSDan Williams 		NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
504d9dcb4baSDan Williams 		== COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
505cc9203bfSDan Williams 		) {
506cc9203bfSDan Williams 		completion_count++;
507cc9203bfSDan Williams 
50889a7301fSDan Williams 		ent = ihost->completion_queue[get_index];
509994a9303SDan Williams 
510994a9303SDan Williams 		/* increment the get pointer and check for rollover to toggle the cycle bit */
511994a9303SDan Williams 		get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
512994a9303SDan Williams 			     (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
513994a9303SDan Williams 		get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
514cc9203bfSDan Williams 
515d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
516cc9203bfSDan Williams 			"%s: completion queue entry:0x%08x\n",
517cc9203bfSDan Williams 			__func__,
51889a7301fSDan Williams 			ent);
519cc9203bfSDan Williams 
52089a7301fSDan Williams 		switch (SCU_GET_COMPLETION_TYPE(ent)) {
521cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_TASK:
52289a7301fSDan Williams 			sci_controller_task_completion(ihost, ent);
523cc9203bfSDan Williams 			break;
524cc9203bfSDan Williams 
525cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_SDMA:
52689a7301fSDan Williams 			sci_controller_sdma_completion(ihost, ent);
527cc9203bfSDan Williams 			break;
528cc9203bfSDan Williams 
529cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_UFI:
53089a7301fSDan Williams 			sci_controller_unsolicited_frame(ihost, ent);
531cc9203bfSDan Williams 			break;
532cc9203bfSDan Williams 
533cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_EVENT:
53477cd72a5SDan Williams 			sci_controller_event_completion(ihost, ent);
53577cd72a5SDan Williams 			break;
53677cd72a5SDan Williams 
537994a9303SDan Williams 		case SCU_COMPLETION_TYPE_NOTIFY: {
538994a9303SDan Williams 			event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
539994a9303SDan Williams 				       (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
540994a9303SDan Williams 			event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
541994a9303SDan Williams 
54289a7301fSDan Williams 			sci_controller_event_completion(ihost, ent);
543cc9203bfSDan Williams 			break;
544994a9303SDan Williams 		}
545cc9203bfSDan Williams 		default:
546d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
547cc9203bfSDan Williams 				 "%s: SCIC Controller received unknown "
548cc9203bfSDan Williams 				 "completion type %x\n",
549cc9203bfSDan Williams 				 __func__,
55089a7301fSDan Williams 				 ent);
551cc9203bfSDan Williams 			break;
552cc9203bfSDan Williams 		}
553cc9203bfSDan Williams 	}
554cc9203bfSDan Williams 
555cc9203bfSDan Williams 	/* Update the get register if we completed one or more entries */
556cc9203bfSDan Williams 	if (completion_count > 0) {
557d9dcb4baSDan Williams 		ihost->completion_queue_get =
558cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(ENABLE) |
559cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
560cc9203bfSDan Williams 			event_cycle |
561994a9303SDan Williams 			SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
562cc9203bfSDan Williams 			get_cycle |
563cc9203bfSDan Williams 			SMU_CQGR_GEN_VAL(POINTER, get_index);
564cc9203bfSDan Williams 
565d9dcb4baSDan Williams 		writel(ihost->completion_queue_get,
566d9dcb4baSDan Williams 		       &ihost->smu_registers->completion_queue_get);
567cc9203bfSDan Williams 
568cc9203bfSDan Williams 	}
569cc9203bfSDan Williams 
570d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
571cc9203bfSDan Williams 		"%s: completion queue ending get:0x%08x\n",
572cc9203bfSDan Williams 		__func__,
573d9dcb4baSDan Williams 		ihost->completion_queue_get);
574cc9203bfSDan Williams 
575cc9203bfSDan Williams }
576cc9203bfSDan Williams 
57789a7301fSDan Williams static void sci_controller_error_handler(struct isci_host *ihost)
578cc9203bfSDan Williams {
579cc9203bfSDan Williams 	u32 interrupt_status;
580cc9203bfSDan Williams 
581cc9203bfSDan Williams 	interrupt_status =
582d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
583cc9203bfSDan Williams 
584cc9203bfSDan Williams 	if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
58589a7301fSDan Williams 	    sci_controller_completion_queue_has_entries(ihost)) {
586cc9203bfSDan Williams 
58789a7301fSDan Williams 		sci_controller_process_completions(ihost);
588d9dcb4baSDan Williams 		writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
589cc9203bfSDan Williams 	} else {
590d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
591cc9203bfSDan Williams 			interrupt_status);
592cc9203bfSDan Williams 
593d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_FAILED);
594cc9203bfSDan Williams 
595cc9203bfSDan Williams 		return;
596cc9203bfSDan Williams 	}
597cc9203bfSDan Williams 
598cc9203bfSDan Williams 	/* If we dont process any completions I am not sure that we want to do this.
599cc9203bfSDan Williams 	 * We are in the middle of a hardware fault and should probably be reset.
600cc9203bfSDan Williams 	 */
601d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
602cc9203bfSDan Williams }
603cc9203bfSDan Williams 
604c7ef4031SDan Williams irqreturn_t isci_intx_isr(int vec, void *data)
6056f231ddaSDan Williams {
6066f231ddaSDan Williams 	irqreturn_t ret = IRQ_NONE;
60731e824edSDan Williams 	struct isci_host *ihost = data;
6086f231ddaSDan Williams 
60989a7301fSDan Williams 	if (sci_controller_isr(ihost)) {
610d9dcb4baSDan Williams 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
611c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
6126f231ddaSDan Williams 		ret = IRQ_HANDLED;
61389a7301fSDan Williams 	} else if (sci_controller_error_isr(ihost)) {
61492f4f0f5SDan Williams 		spin_lock(&ihost->scic_lock);
61589a7301fSDan Williams 		sci_controller_error_handler(ihost);
61692f4f0f5SDan Williams 		spin_unlock(&ihost->scic_lock);
61792f4f0f5SDan Williams 		ret = IRQ_HANDLED;
6186f231ddaSDan Williams 	}
61992f4f0f5SDan Williams 
6206f231ddaSDan Williams 	return ret;
6216f231ddaSDan Williams }
6226f231ddaSDan Williams 
62392f4f0f5SDan Williams irqreturn_t isci_error_isr(int vec, void *data)
62492f4f0f5SDan Williams {
62592f4f0f5SDan Williams 	struct isci_host *ihost = data;
62692f4f0f5SDan Williams 
62789a7301fSDan Williams 	if (sci_controller_error_isr(ihost))
62889a7301fSDan Williams 		sci_controller_error_handler(ihost);
62992f4f0f5SDan Williams 
63092f4f0f5SDan Williams 	return IRQ_HANDLED;
63192f4f0f5SDan Williams }
6326f231ddaSDan Williams 
6336f231ddaSDan Williams /**
6346f231ddaSDan Williams  * isci_host_start_complete() - This function is called by the core library,
6356f231ddaSDan Williams  *    through the ISCI Module, to indicate controller start status.
6366f231ddaSDan Williams  * @isci_host: This parameter specifies the ISCI host object
6376f231ddaSDan Williams  * @completion_status: This parameter specifies the completion status from the
6386f231ddaSDan Williams  *    core library.
6396f231ddaSDan Williams  *
6406f231ddaSDan Williams  */
641cc9203bfSDan Williams static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
6426f231ddaSDan Williams {
6430cf89d1dSDan Williams 	if (completion_status != SCI_SUCCESS)
6440cf89d1dSDan Williams 		dev_info(&ihost->pdev->dev,
6450cf89d1dSDan Williams 			"controller start timed out, continuing...\n");
6460cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_ready);
6470cf89d1dSDan Williams 	clear_bit(IHOST_START_PENDING, &ihost->flags);
6480cf89d1dSDan Williams 	wake_up(&ihost->eventq);
6496f231ddaSDan Williams }
6506f231ddaSDan Williams 
651c7ef4031SDan Williams int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
6526f231ddaSDan Williams {
6534393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
6546f231ddaSDan Williams 
65577950f51SEdmund Nadolski 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
6566f231ddaSDan Williams 		return 0;
6576f231ddaSDan Williams 
65877950f51SEdmund Nadolski 	/* todo: use sas_flush_discovery once it is upstream */
65977950f51SEdmund Nadolski 	scsi_flush_work(shost);
66077950f51SEdmund Nadolski 
66177950f51SEdmund Nadolski 	scsi_flush_work(shost);
6626f231ddaSDan Williams 
6630cf89d1dSDan Williams 	dev_dbg(&ihost->pdev->dev,
6640cf89d1dSDan Williams 		"%s: ihost->status = %d, time = %ld\n",
6650cf89d1dSDan Williams 		 __func__, isci_host_get_state(ihost), time);
6666f231ddaSDan Williams 
6676f231ddaSDan Williams 	return 1;
6686f231ddaSDan Williams 
6696f231ddaSDan Williams }
6706f231ddaSDan Williams 
671cc9203bfSDan Williams /**
67289a7301fSDan Williams  * sci_controller_get_suggested_start_timeout() - This method returns the
67389a7301fSDan Williams  *    suggested sci_controller_start() timeout amount.  The user is free to
674cc9203bfSDan Williams  *    use any timeout value, but this method provides the suggested minimum
675cc9203bfSDan Williams  *    start timeout value.  The returned value is based upon empirical
676cc9203bfSDan Williams  *    information determined as a result of interoperability testing.
677cc9203bfSDan Williams  * @controller: the handle to the controller object for which to return the
678cc9203bfSDan Williams  *    suggested start timeout.
679cc9203bfSDan Williams  *
680cc9203bfSDan Williams  * This method returns the number of milliseconds for the suggested start
681cc9203bfSDan Williams  * operation timeout.
682cc9203bfSDan Williams  */
68389a7301fSDan Williams static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
684cc9203bfSDan Williams {
685cc9203bfSDan Williams 	/* Validate the user supplied parameters. */
686d9dcb4baSDan Williams 	if (!ihost)
687cc9203bfSDan Williams 		return 0;
688cc9203bfSDan Williams 
689cc9203bfSDan Williams 	/*
690cc9203bfSDan Williams 	 * The suggested minimum timeout value for a controller start operation:
691cc9203bfSDan Williams 	 *
692cc9203bfSDan Williams 	 *     Signature FIS Timeout
693cc9203bfSDan Williams 	 *   + Phy Start Timeout
694cc9203bfSDan Williams 	 *   + Number of Phy Spin Up Intervals
695cc9203bfSDan Williams 	 *   ---------------------------------
696cc9203bfSDan Williams 	 *   Number of milliseconds for the controller start operation.
697cc9203bfSDan Williams 	 *
698cc9203bfSDan Williams 	 * NOTE: The number of phy spin up intervals will be equivalent
699cc9203bfSDan Williams 	 *       to the number of phys divided by the number phys allowed
700cc9203bfSDan Williams 	 *       per interval - 1 (once OEM parameters are supported).
701cc9203bfSDan Williams 	 *       Currently we assume only 1 phy per interval. */
702cc9203bfSDan Williams 
703cc9203bfSDan Williams 	return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
704cc9203bfSDan Williams 		+ SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
705cc9203bfSDan Williams 		+ ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
706cc9203bfSDan Williams }
707cc9203bfSDan Williams 
70889a7301fSDan Williams static void sci_controller_enable_interrupts(struct isci_host *ihost)
709cc9203bfSDan Williams {
710d9dcb4baSDan Williams 	BUG_ON(ihost->smu_registers == NULL);
711d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
712cc9203bfSDan Williams }
713cc9203bfSDan Williams 
71489a7301fSDan Williams void sci_controller_disable_interrupts(struct isci_host *ihost)
715cc9203bfSDan Williams {
716d9dcb4baSDan Williams 	BUG_ON(ihost->smu_registers == NULL);
717d9dcb4baSDan Williams 	writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
718cc9203bfSDan Williams }
719cc9203bfSDan Williams 
72089a7301fSDan Williams static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
721cc9203bfSDan Williams {
722cc9203bfSDan Williams 	u32 port_task_scheduler_value;
723cc9203bfSDan Williams 
724cc9203bfSDan Williams 	port_task_scheduler_value =
725d9dcb4baSDan Williams 		readl(&ihost->scu_registers->peg0.ptsg.control);
726cc9203bfSDan Williams 	port_task_scheduler_value |=
727cc9203bfSDan Williams 		(SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
728cc9203bfSDan Williams 		 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
729cc9203bfSDan Williams 	writel(port_task_scheduler_value,
730d9dcb4baSDan Williams 	       &ihost->scu_registers->peg0.ptsg.control);
731cc9203bfSDan Williams }
732cc9203bfSDan Williams 
73389a7301fSDan Williams static void sci_controller_assign_task_entries(struct isci_host *ihost)
734cc9203bfSDan Williams {
735cc9203bfSDan Williams 	u32 task_assignment;
736cc9203bfSDan Williams 
737cc9203bfSDan Williams 	/*
738cc9203bfSDan Williams 	 * Assign all the TCs to function 0
739cc9203bfSDan Williams 	 * TODO: Do we actually need to read this register to write it back?
740cc9203bfSDan Williams 	 */
741cc9203bfSDan Williams 
742cc9203bfSDan Williams 	task_assignment =
743d9dcb4baSDan Williams 		readl(&ihost->smu_registers->task_context_assignment[0]);
744cc9203bfSDan Williams 
745cc9203bfSDan Williams 	task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
746d9dcb4baSDan Williams 		(SMU_TCA_GEN_VAL(ENDING,  ihost->task_context_entries - 1)) |
747cc9203bfSDan Williams 		(SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
748cc9203bfSDan Williams 
749cc9203bfSDan Williams 	writel(task_assignment,
750d9dcb4baSDan Williams 		&ihost->smu_registers->task_context_assignment[0]);
751cc9203bfSDan Williams 
752cc9203bfSDan Williams }
753cc9203bfSDan Williams 
75489a7301fSDan Williams static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
755cc9203bfSDan Williams {
756cc9203bfSDan Williams 	u32 index;
757cc9203bfSDan Williams 	u32 completion_queue_control_value;
758cc9203bfSDan Williams 	u32 completion_queue_get_value;
759cc9203bfSDan Williams 	u32 completion_queue_put_value;
760cc9203bfSDan Williams 
761d9dcb4baSDan Williams 	ihost->completion_queue_get = 0;
762cc9203bfSDan Williams 
7637c78da31SDan Williams 	completion_queue_control_value =
7647c78da31SDan Williams 		(SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
7657c78da31SDan Williams 		 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
766cc9203bfSDan Williams 
767cc9203bfSDan Williams 	writel(completion_queue_control_value,
768d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_control);
769cc9203bfSDan Williams 
770cc9203bfSDan Williams 
771cc9203bfSDan Williams 	/* Set the completion queue get pointer and enable the queue */
772cc9203bfSDan Williams 	completion_queue_get_value = (
773cc9203bfSDan Williams 		(SMU_CQGR_GEN_VAL(POINTER, 0))
774cc9203bfSDan Williams 		| (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
775cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(ENABLE))
776cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
777cc9203bfSDan Williams 		);
778cc9203bfSDan Williams 
779cc9203bfSDan Williams 	writel(completion_queue_get_value,
780d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_get);
781cc9203bfSDan Williams 
782cc9203bfSDan Williams 	/* Set the completion queue put pointer */
783cc9203bfSDan Williams 	completion_queue_put_value = (
784cc9203bfSDan Williams 		(SMU_CQPR_GEN_VAL(POINTER, 0))
785cc9203bfSDan Williams 		| (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
786cc9203bfSDan Williams 		);
787cc9203bfSDan Williams 
788cc9203bfSDan Williams 	writel(completion_queue_put_value,
789d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_put);
790cc9203bfSDan Williams 
791cc9203bfSDan Williams 	/* Initialize the cycle bit of the completion queue entries */
7927c78da31SDan Williams 	for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
793cc9203bfSDan Williams 		/*
794cc9203bfSDan Williams 		 * If get.cycle_bit != completion_queue.cycle_bit
795cc9203bfSDan Williams 		 * its not a valid completion queue entry
796cc9203bfSDan Williams 		 * so at system start all entries are invalid */
797d9dcb4baSDan Williams 		ihost->completion_queue[index] = 0x80000000;
798cc9203bfSDan Williams 	}
799cc9203bfSDan Williams }
800cc9203bfSDan Williams 
80189a7301fSDan Williams static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
802cc9203bfSDan Williams {
803cc9203bfSDan Williams 	u32 frame_queue_control_value;
804cc9203bfSDan Williams 	u32 frame_queue_get_value;
805cc9203bfSDan Williams 	u32 frame_queue_put_value;
806cc9203bfSDan Williams 
807cc9203bfSDan Williams 	/* Write the queue size */
808cc9203bfSDan Williams 	frame_queue_control_value =
8097c78da31SDan Williams 		SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
810cc9203bfSDan Williams 
811cc9203bfSDan Williams 	writel(frame_queue_control_value,
812d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
813cc9203bfSDan Williams 
814cc9203bfSDan Williams 	/* Setup the get pointer for the unsolicited frame queue */
815cc9203bfSDan Williams 	frame_queue_get_value = (
816cc9203bfSDan Williams 		SCU_UFQGP_GEN_VAL(POINTER, 0)
817cc9203bfSDan Williams 		|  SCU_UFQGP_GEN_BIT(ENABLE_BIT)
818cc9203bfSDan Williams 		);
819cc9203bfSDan Williams 
820cc9203bfSDan Williams 	writel(frame_queue_get_value,
821d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
822cc9203bfSDan Williams 	/* Setup the put pointer for the unsolicited frame queue */
823cc9203bfSDan Williams 	frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
824cc9203bfSDan Williams 	writel(frame_queue_put_value,
825d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
826cc9203bfSDan Williams }
827cc9203bfSDan Williams 
82889a7301fSDan Williams static void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
829cc9203bfSDan Williams {
830d9dcb4baSDan Williams 	if (ihost->sm.current_state_id == SCIC_STARTING) {
831cc9203bfSDan Williams 		/*
832cc9203bfSDan Williams 		 * We move into the ready state, because some of the phys/ports
833cc9203bfSDan Williams 		 * may be up and operational.
834cc9203bfSDan Williams 		 */
835d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_READY);
836cc9203bfSDan Williams 
837cc9203bfSDan Williams 		isci_host_start_complete(ihost, status);
838cc9203bfSDan Williams 	}
839cc9203bfSDan Williams }
840cc9203bfSDan Williams 
84185280955SDan Williams static bool is_phy_starting(struct isci_phy *iphy)
8424a33c525SAdam Gruchala {
84389a7301fSDan Williams 	enum sci_phy_states state;
8444a33c525SAdam Gruchala 
84585280955SDan Williams 	state = iphy->sm.current_state_id;
8464a33c525SAdam Gruchala 	switch (state) {
847e301370aSEdmund Nadolski 	case SCI_PHY_STARTING:
848e301370aSEdmund Nadolski 	case SCI_PHY_SUB_INITIAL:
849e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
850e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_IAF_UF:
851e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
852e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
853e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
854e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
855e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
856e301370aSEdmund Nadolski 	case SCI_PHY_SUB_FINAL:
8574a33c525SAdam Gruchala 		return true;
8584a33c525SAdam Gruchala 	default:
8594a33c525SAdam Gruchala 		return false;
8604a33c525SAdam Gruchala 	}
8614a33c525SAdam Gruchala }
8624a33c525SAdam Gruchala 
863cc9203bfSDan Williams /**
86489a7301fSDan Williams  * sci_controller_start_next_phy - start phy
865cc9203bfSDan Williams  * @scic: controller
866cc9203bfSDan Williams  *
867cc9203bfSDan Williams  * If all the phys have been started, then attempt to transition the
868cc9203bfSDan Williams  * controller to the READY state and inform the user
86989a7301fSDan Williams  * (sci_cb_controller_start_complete()).
870cc9203bfSDan Williams  */
87189a7301fSDan Williams static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
872cc9203bfSDan Williams {
87389a7301fSDan Williams 	struct sci_oem_params *oem = &ihost->oem_parameters;
87485280955SDan Williams 	struct isci_phy *iphy;
875cc9203bfSDan Williams 	enum sci_status status;
876cc9203bfSDan Williams 
877cc9203bfSDan Williams 	status = SCI_SUCCESS;
878cc9203bfSDan Williams 
879d9dcb4baSDan Williams 	if (ihost->phy_startup_timer_pending)
880cc9203bfSDan Williams 		return status;
881cc9203bfSDan Williams 
882d9dcb4baSDan Williams 	if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
883cc9203bfSDan Williams 		bool is_controller_start_complete = true;
884cc9203bfSDan Williams 		u32 state;
885cc9203bfSDan Williams 		u8 index;
886cc9203bfSDan Williams 
887cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
88885280955SDan Williams 			iphy = &ihost->phys[index];
88985280955SDan Williams 			state = iphy->sm.current_state_id;
890cc9203bfSDan Williams 
89185280955SDan Williams 			if (!phy_get_non_dummy_port(iphy))
892cc9203bfSDan Williams 				continue;
893cc9203bfSDan Williams 
894cc9203bfSDan Williams 			/* The controller start operation is complete iff:
895cc9203bfSDan Williams 			 * - all links have been given an opportunity to start
896cc9203bfSDan Williams 			 * - have no indication of a connected device
897cc9203bfSDan Williams 			 * - have an indication of a connected device and it has
898cc9203bfSDan Williams 			 *   finished the link training process.
899cc9203bfSDan Williams 			 */
90085280955SDan Williams 			if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
90185280955SDan Williams 			    (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
902*be778341SMarcin Tomczak 			    (iphy->is_in_link_training == true && is_phy_starting(iphy)) ||
903*be778341SMarcin Tomczak 			    (ihost->port_agent.phy_ready_mask != ihost->port_agent.phy_configured_mask)) {
904cc9203bfSDan Williams 				is_controller_start_complete = false;
905cc9203bfSDan Williams 				break;
906cc9203bfSDan Williams 			}
907cc9203bfSDan Williams 		}
908cc9203bfSDan Williams 
909cc9203bfSDan Williams 		/*
910cc9203bfSDan Williams 		 * The controller has successfully finished the start process.
911cc9203bfSDan Williams 		 * Inform the SCI Core user and transition to the READY state. */
912cc9203bfSDan Williams 		if (is_controller_start_complete == true) {
91389a7301fSDan Williams 			sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
914d9dcb4baSDan Williams 			sci_del_timer(&ihost->phy_timer);
915d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = false;
916cc9203bfSDan Williams 		}
917cc9203bfSDan Williams 	} else {
918d9dcb4baSDan Williams 		iphy = &ihost->phys[ihost->next_phy_to_start];
919cc9203bfSDan Williams 
920cc9203bfSDan Williams 		if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
92185280955SDan Williams 			if (phy_get_non_dummy_port(iphy) == NULL) {
922d9dcb4baSDan Williams 				ihost->next_phy_to_start++;
923cc9203bfSDan Williams 
924cc9203bfSDan Williams 				/* Caution recursion ahead be forwarned
925cc9203bfSDan Williams 				 *
926cc9203bfSDan Williams 				 * The PHY was never added to a PORT in MPC mode
927cc9203bfSDan Williams 				 * so start the next phy in sequence This phy
928cc9203bfSDan Williams 				 * will never go link up and will not draw power
929cc9203bfSDan Williams 				 * the OEM parameters either configured the phy
930cc9203bfSDan Williams 				 * incorrectly for the PORT or it was never
931cc9203bfSDan Williams 				 * assigned to a PORT
932cc9203bfSDan Williams 				 */
93389a7301fSDan Williams 				return sci_controller_start_next_phy(ihost);
934cc9203bfSDan Williams 			}
935cc9203bfSDan Williams 		}
936cc9203bfSDan Williams 
93789a7301fSDan Williams 		status = sci_phy_start(iphy);
938cc9203bfSDan Williams 
939cc9203bfSDan Williams 		if (status == SCI_SUCCESS) {
940d9dcb4baSDan Williams 			sci_mod_timer(&ihost->phy_timer,
941bb3dbdf6SEdmund Nadolski 				      SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
942d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = true;
943cc9203bfSDan Williams 		} else {
944d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
945cc9203bfSDan Williams 				 "%s: Controller stop operation failed "
946cc9203bfSDan Williams 				 "to stop phy %d because of status "
947cc9203bfSDan Williams 				 "%d.\n",
948cc9203bfSDan Williams 				 __func__,
949d9dcb4baSDan Williams 				 ihost->phys[ihost->next_phy_to_start].phy_index,
950cc9203bfSDan Williams 				 status);
951cc9203bfSDan Williams 		}
952cc9203bfSDan Williams 
953d9dcb4baSDan Williams 		ihost->next_phy_to_start++;
954cc9203bfSDan Williams 	}
955cc9203bfSDan Williams 
956cc9203bfSDan Williams 	return status;
957cc9203bfSDan Williams }
958cc9203bfSDan Williams 
959bb3dbdf6SEdmund Nadolski static void phy_startup_timeout(unsigned long data)
960cc9203bfSDan Williams {
961bb3dbdf6SEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
962d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
963bb3dbdf6SEdmund Nadolski 	unsigned long flags;
964cc9203bfSDan Williams 	enum sci_status status;
965cc9203bfSDan Williams 
966bb3dbdf6SEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
967bb3dbdf6SEdmund Nadolski 
968bb3dbdf6SEdmund Nadolski 	if (tmr->cancel)
969bb3dbdf6SEdmund Nadolski 		goto done;
970bb3dbdf6SEdmund Nadolski 
971d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
972bb3dbdf6SEdmund Nadolski 
973bb3dbdf6SEdmund Nadolski 	do {
97489a7301fSDan Williams 		status = sci_controller_start_next_phy(ihost);
975bb3dbdf6SEdmund Nadolski 	} while (status != SCI_SUCCESS);
976bb3dbdf6SEdmund Nadolski 
977bb3dbdf6SEdmund Nadolski done:
978bb3dbdf6SEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
979cc9203bfSDan Williams }
980cc9203bfSDan Williams 
981ac668c69SDan Williams static u16 isci_tci_active(struct isci_host *ihost)
982ac668c69SDan Williams {
983ac668c69SDan Williams 	return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
984ac668c69SDan Williams }
985ac668c69SDan Williams 
98689a7301fSDan Williams static enum sci_status sci_controller_start(struct isci_host *ihost,
987cc9203bfSDan Williams 					     u32 timeout)
988cc9203bfSDan Williams {
989cc9203bfSDan Williams 	enum sci_status result;
990cc9203bfSDan Williams 	u16 index;
991cc9203bfSDan Williams 
992d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
993d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
994cc9203bfSDan Williams 			 "SCIC Controller start operation requested in "
995cc9203bfSDan Williams 			 "invalid state\n");
996cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
997cc9203bfSDan Williams 	}
998cc9203bfSDan Williams 
999cc9203bfSDan Williams 	/* Build the TCi free pool */
1000ac668c69SDan Williams 	BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1001ac668c69SDan Williams 	ihost->tci_head = 0;
1002ac668c69SDan Williams 	ihost->tci_tail = 0;
1003d9dcb4baSDan Williams 	for (index = 0; index < ihost->task_context_entries; index++)
1004ac668c69SDan Williams 		isci_tci_free(ihost, index);
1005cc9203bfSDan Williams 
1006cc9203bfSDan Williams 	/* Build the RNi free pool */
100789a7301fSDan Williams 	sci_remote_node_table_initialize(&ihost->available_remote_nodes,
1008d9dcb4baSDan Williams 					 ihost->remote_node_entries);
1009cc9203bfSDan Williams 
1010cc9203bfSDan Williams 	/*
1011cc9203bfSDan Williams 	 * Before anything else lets make sure we will not be
1012cc9203bfSDan Williams 	 * interrupted by the hardware.
1013cc9203bfSDan Williams 	 */
101489a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1015cc9203bfSDan Williams 
1016cc9203bfSDan Williams 	/* Enable the port task scheduler */
101789a7301fSDan Williams 	sci_controller_enable_port_task_scheduler(ihost);
1018cc9203bfSDan Williams 
1019d9dcb4baSDan Williams 	/* Assign all the task entries to ihost physical function */
102089a7301fSDan Williams 	sci_controller_assign_task_entries(ihost);
1021cc9203bfSDan Williams 
1022cc9203bfSDan Williams 	/* Now initialize the completion queue */
102389a7301fSDan Williams 	sci_controller_initialize_completion_queue(ihost);
1024cc9203bfSDan Williams 
1025cc9203bfSDan Williams 	/* Initialize the unsolicited frame queue for use */
102689a7301fSDan Williams 	sci_controller_initialize_unsolicited_frame_queue(ihost);
1027cc9203bfSDan Williams 
1028cc9203bfSDan Williams 	/* Start all of the ports on this controller */
1029d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1030ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1031cc9203bfSDan Williams 
103289a7301fSDan Williams 		result = sci_port_start(iport);
1033cc9203bfSDan Williams 		if (result)
1034cc9203bfSDan Williams 			return result;
1035cc9203bfSDan Williams 	}
1036cc9203bfSDan Williams 
103789a7301fSDan Williams 	sci_controller_start_next_phy(ihost);
1038cc9203bfSDan Williams 
1039d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1040cc9203bfSDan Williams 
1041d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STARTING);
1042cc9203bfSDan Williams 
1043cc9203bfSDan Williams 	return SCI_SUCCESS;
1044cc9203bfSDan Williams }
1045cc9203bfSDan Williams 
10466f231ddaSDan Williams void isci_host_scan_start(struct Scsi_Host *shost)
10476f231ddaSDan Williams {
10484393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
104989a7301fSDan Williams 	unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
10506f231ddaSDan Williams 
10510cf89d1dSDan Williams 	set_bit(IHOST_START_PENDING, &ihost->flags);
105277950f51SEdmund Nadolski 
105377950f51SEdmund Nadolski 	spin_lock_irq(&ihost->scic_lock);
105489a7301fSDan Williams 	sci_controller_start(ihost, tmo);
105589a7301fSDan Williams 	sci_controller_enable_interrupts(ihost);
105677950f51SEdmund Nadolski 	spin_unlock_irq(&ihost->scic_lock);
10576f231ddaSDan Williams }
10586f231ddaSDan Williams 
1059cc9203bfSDan Williams static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
10606f231ddaSDan Williams {
10610cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopped);
106289a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
10630cf89d1dSDan Williams 	clear_bit(IHOST_STOP_PENDING, &ihost->flags);
10640cf89d1dSDan Williams 	wake_up(&ihost->eventq);
10656f231ddaSDan Williams }
10666f231ddaSDan Williams 
106789a7301fSDan Williams static void sci_controller_completion_handler(struct isci_host *ihost)
1068cc9203bfSDan Williams {
1069cc9203bfSDan Williams 	/* Empty out the completion queue */
107089a7301fSDan Williams 	if (sci_controller_completion_queue_has_entries(ihost))
107189a7301fSDan Williams 		sci_controller_process_completions(ihost);
1072cc9203bfSDan Williams 
1073cc9203bfSDan Williams 	/* Clear the interrupt and enable all interrupts again */
1074d9dcb4baSDan Williams 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
1075cc9203bfSDan Williams 	/* Could we write the value of SMU_ISR_COMPLETION? */
1076d9dcb4baSDan Williams 	writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
1077d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
1078cc9203bfSDan Williams }
1079cc9203bfSDan Williams 
10806f231ddaSDan Williams /**
10816f231ddaSDan Williams  * isci_host_completion_routine() - This function is the delayed service
10826f231ddaSDan Williams  *    routine that calls the sci core library's completion handler. It's
10836f231ddaSDan Williams  *    scheduled as a tasklet from the interrupt service routine when interrupts
10846f231ddaSDan Williams  *    in use, or set as the timeout function in polled mode.
10856f231ddaSDan Williams  * @data: This parameter specifies the ISCI host object
10866f231ddaSDan Williams  *
10876f231ddaSDan Williams  */
10886f231ddaSDan Williams static void isci_host_completion_routine(unsigned long data)
10896f231ddaSDan Williams {
1090d9dcb4baSDan Williams 	struct isci_host *ihost = (struct isci_host *)data;
10916f231ddaSDan Williams 	struct list_head    completed_request_list;
109211b00c19SJeff Skirvin 	struct list_head    errored_request_list;
10936f231ddaSDan Williams 	struct list_head    *current_position;
10946f231ddaSDan Williams 	struct list_head    *next_position;
10956f231ddaSDan Williams 	struct isci_request *request;
10966f231ddaSDan Williams 	struct isci_request *next_request;
10976f231ddaSDan Williams 	struct sas_task     *task;
10989b4be528SDan Williams 	u16 active;
10996f231ddaSDan Williams 
11006f231ddaSDan Williams 	INIT_LIST_HEAD(&completed_request_list);
110111b00c19SJeff Skirvin 	INIT_LIST_HEAD(&errored_request_list);
11026f231ddaSDan Williams 
1103d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
11046f231ddaSDan Williams 
110589a7301fSDan Williams 	sci_controller_completion_handler(ihost);
1106c7ef4031SDan Williams 
11076f231ddaSDan Williams 	/* Take the lists of completed I/Os from the host. */
110811b00c19SJeff Skirvin 
1109d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_complete,
11106f231ddaSDan Williams 			 &completed_request_list);
11116f231ddaSDan Williams 
111211b00c19SJeff Skirvin 	/* Take the list of errored I/Os from the host. */
1113d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_errorback,
111411b00c19SJeff Skirvin 			 &errored_request_list);
11156f231ddaSDan Williams 
1116d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
11176f231ddaSDan Williams 
11186f231ddaSDan Williams 	/* Process any completions in the lists. */
11196f231ddaSDan Williams 	list_for_each_safe(current_position, next_position,
11206f231ddaSDan Williams 			   &completed_request_list) {
11216f231ddaSDan Williams 
11226f231ddaSDan Williams 		request = list_entry(current_position, struct isci_request,
11236f231ddaSDan Williams 				     completed_node);
11246f231ddaSDan Williams 		task = isci_request_access_task(request);
11256f231ddaSDan Williams 
11266f231ddaSDan Williams 		/* Normal notification (task_done) */
1127d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
11286f231ddaSDan Williams 			"%s: Normal - request/task = %p/%p\n",
11296f231ddaSDan Williams 			__func__,
11306f231ddaSDan Williams 			request,
11316f231ddaSDan Williams 			task);
11326f231ddaSDan Williams 
113311b00c19SJeff Skirvin 		/* Return the task to libsas */
113411b00c19SJeff Skirvin 		if (task != NULL) {
11356f231ddaSDan Williams 
113611b00c19SJeff Skirvin 			task->lldd_task = NULL;
113711b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
113811b00c19SJeff Skirvin 
113911b00c19SJeff Skirvin 				/* If the task is already in the abort path,
114011b00c19SJeff Skirvin 				* the task_done callback cannot be called.
114111b00c19SJeff Skirvin 				*/
114211b00c19SJeff Skirvin 				task->task_done(task);
114311b00c19SJeff Skirvin 			}
114411b00c19SJeff Skirvin 		}
1145312e0c24SDan Williams 
1146d9dcb4baSDan Williams 		spin_lock_irq(&ihost->scic_lock);
1147d9dcb4baSDan Williams 		isci_free_tag(ihost, request->io_tag);
1148d9dcb4baSDan Williams 		spin_unlock_irq(&ihost->scic_lock);
11496f231ddaSDan Williams 	}
115011b00c19SJeff Skirvin 	list_for_each_entry_safe(request, next_request, &errored_request_list,
11516f231ddaSDan Williams 				 completed_node) {
11526f231ddaSDan Williams 
11536f231ddaSDan Williams 		task = isci_request_access_task(request);
11546f231ddaSDan Williams 
11556f231ddaSDan Williams 		/* Use sas_task_abort */
1156d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
11576f231ddaSDan Williams 			 "%s: Error - request/task = %p/%p\n",
11586f231ddaSDan Williams 			 __func__,
11596f231ddaSDan Williams 			 request,
11606f231ddaSDan Williams 			 task);
11616f231ddaSDan Williams 
116211b00c19SJeff Skirvin 		if (task != NULL) {
116311b00c19SJeff Skirvin 
116411b00c19SJeff Skirvin 			/* Put the task into the abort path if it's not there
116511b00c19SJeff Skirvin 			 * already.
116611b00c19SJeff Skirvin 			 */
116711b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
11686f231ddaSDan Williams 				sas_task_abort(task);
116911b00c19SJeff Skirvin 
117011b00c19SJeff Skirvin 		} else {
117111b00c19SJeff Skirvin 			/* This is a case where the request has completed with a
117211b00c19SJeff Skirvin 			 * status such that it needed further target servicing,
117311b00c19SJeff Skirvin 			 * but the sas_task reference has already been removed
117411b00c19SJeff Skirvin 			 * from the request.  Since it was errored, it was not
117511b00c19SJeff Skirvin 			 * being aborted, so there is nothing to do except free
117611b00c19SJeff Skirvin 			 * it.
117711b00c19SJeff Skirvin 			 */
117811b00c19SJeff Skirvin 
1179d9dcb4baSDan Williams 			spin_lock_irq(&ihost->scic_lock);
118011b00c19SJeff Skirvin 			/* Remove the request from the remote device's list
118111b00c19SJeff Skirvin 			* of pending requests.
118211b00c19SJeff Skirvin 			*/
118311b00c19SJeff Skirvin 			list_del_init(&request->dev_node);
1184d9dcb4baSDan Williams 			isci_free_tag(ihost, request->io_tag);
1185d9dcb4baSDan Williams 			spin_unlock_irq(&ihost->scic_lock);
118611b00c19SJeff Skirvin 		}
11876f231ddaSDan Williams 	}
11886f231ddaSDan Williams 
11899b4be528SDan Williams 	/* the coalesence timeout doubles at each encoding step, so
11909b4be528SDan Williams 	 * update it based on the ilog2 value of the outstanding requests
11919b4be528SDan Williams 	 */
11929b4be528SDan Williams 	active = isci_tci_active(ihost);
11939b4be528SDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, active) |
11949b4be528SDan Williams 	       SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
11959b4be528SDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
11966f231ddaSDan Williams }
11976f231ddaSDan Williams 
1198cc9203bfSDan Williams /**
119989a7301fSDan Williams  * sci_controller_stop() - This method will stop an individual controller
1200cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1201cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1202cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1203cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1204cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1205cc9203bfSDan Williams  *    the hardware is halted.
1206cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1207cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1208cc9203bfSDan Williams  *    stop operation should complete.
1209cc9203bfSDan Williams  *
1210cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1211cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1212cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1213cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1214cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1215cc9203bfSDan Williams  */
121689a7301fSDan Williams static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
1217cc9203bfSDan Williams {
1218d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
1219d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
1220cc9203bfSDan Williams 			 "SCIC Controller stop operation requested in "
1221cc9203bfSDan Williams 			 "invalid state\n");
1222cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1223cc9203bfSDan Williams 	}
1224cc9203bfSDan Williams 
1225d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1226d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STOPPING);
1227cc9203bfSDan Williams 	return SCI_SUCCESS;
1228cc9203bfSDan Williams }
1229cc9203bfSDan Williams 
1230cc9203bfSDan Williams /**
123189a7301fSDan Williams  * sci_controller_reset() - This method will reset the supplied core
1232cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1233cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1234cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1235cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1236cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1237cc9203bfSDan Williams  *
1238cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1239cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1240cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1241cc9203bfSDan Williams  */
124289a7301fSDan Williams static enum sci_status sci_controller_reset(struct isci_host *ihost)
1243cc9203bfSDan Williams {
1244d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
1245e301370aSEdmund Nadolski 	case SCIC_RESET:
1246e301370aSEdmund Nadolski 	case SCIC_READY:
1247e301370aSEdmund Nadolski 	case SCIC_STOPPED:
1248e301370aSEdmund Nadolski 	case SCIC_FAILED:
1249cc9203bfSDan Williams 		/*
1250cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1251cc9203bfSDan Williams 		 * perform the state transition.
1252cc9203bfSDan Williams 		 */
1253d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_RESETTING);
1254cc9203bfSDan Williams 		return SCI_SUCCESS;
1255cc9203bfSDan Williams 	default:
1256d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
1257cc9203bfSDan Williams 			 "SCIC Controller reset operation requested in "
1258cc9203bfSDan Williams 			 "invalid state\n");
1259cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1260cc9203bfSDan Williams 	}
1261cc9203bfSDan Williams }
1262cc9203bfSDan Williams 
12630cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
12646f231ddaSDan Williams {
12656f231ddaSDan Williams 	int i;
12666f231ddaSDan Williams 
1267ad4f4c1dSDan Williams 	/* disable output data selects */
1268ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
1269ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
1270ad4f4c1dSDan Williams 
12710cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopping);
12726f231ddaSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++) {
1273e531381eSDan Williams 		struct isci_port *iport = &ihost->ports[i];
12740cf89d1dSDan Williams 		struct isci_remote_device *idev, *d;
12750cf89d1dSDan Williams 
1276e531381eSDan Williams 		list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
1277209fae14SDan Williams 			if (test_bit(IDEV_ALLOCATED, &idev->flags))
12786ad31fecSDan Williams 				isci_remote_device_stop(ihost, idev);
12796f231ddaSDan Williams 		}
12806f231ddaSDan Williams 	}
12816f231ddaSDan Williams 
12820cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
12837c40a803SDan Williams 
12847c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
128589a7301fSDan Williams 	sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
12867c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
12877c40a803SDan Williams 
12880cf89d1dSDan Williams 	wait_for_stop(ihost);
1289ad4f4c1dSDan Williams 
1290ad4f4c1dSDan Williams 	/* disable sgpio: where the above wait should give time for the
1291ad4f4c1dSDan Williams 	 * enclosure to sample the gpios going inactive
1292ad4f4c1dSDan Williams 	 */
1293ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
1294ad4f4c1dSDan Williams 
129589a7301fSDan Williams 	sci_controller_reset(ihost);
12965553ba2bSEdmund Nadolski 
12975553ba2bSEdmund Nadolski 	/* Cancel any/all outstanding port timers */
1298d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
1299ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[i];
1300ffe191c9SDan Williams 		del_timer_sync(&iport->timer.timer);
13015553ba2bSEdmund Nadolski 	}
13025553ba2bSEdmund Nadolski 
1303a628d478SEdmund Nadolski 	/* Cancel any/all outstanding phy timers */
1304a628d478SEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
130585280955SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
130685280955SDan Williams 		del_timer_sync(&iphy->sata_timer.timer);
1307a628d478SEdmund Nadolski 	}
1308a628d478SEdmund Nadolski 
1309d9dcb4baSDan Williams 	del_timer_sync(&ihost->port_agent.timer.timer);
1310ac0eeb4fSEdmund Nadolski 
1311d9dcb4baSDan Williams 	del_timer_sync(&ihost->power_control.timer.timer);
13120473661aSEdmund Nadolski 
1313d9dcb4baSDan Williams 	del_timer_sync(&ihost->timer.timer);
13146cb5853dSEdmund Nadolski 
1315d9dcb4baSDan Williams 	del_timer_sync(&ihost->phy_timer.timer);
13166f231ddaSDan Williams }
13176f231ddaSDan Williams 
13186f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
13196f231ddaSDan Williams {
13206f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13216f231ddaSDan Williams 	int id = isci_host->id;
13226f231ddaSDan Williams 
13236f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
13246f231ddaSDan Williams }
13256f231ddaSDan Williams 
13266f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
13276f231ddaSDan Williams {
13286f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13296f231ddaSDan Williams 	int id = isci_host->id;
13306f231ddaSDan Williams 
13316f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13326f231ddaSDan Williams }
13336f231ddaSDan Williams 
133489a7301fSDan Williams static void isci_user_parameters_get(struct sci_user_parameters *u)
1335b5f18a20SDave Jiang {
1336b5f18a20SDave Jiang 	int i;
1337b5f18a20SDave Jiang 
1338b5f18a20SDave Jiang 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1339b5f18a20SDave Jiang 		struct sci_phy_user_params *u_phy = &u->phys[i];
1340b5f18a20SDave Jiang 
1341b5f18a20SDave Jiang 		u_phy->max_speed_generation = phy_gen;
1342b5f18a20SDave Jiang 
1343b5f18a20SDave Jiang 		/* we are not exporting these for now */
1344b5f18a20SDave Jiang 		u_phy->align_insertion_frequency = 0x7f;
1345b5f18a20SDave Jiang 		u_phy->in_connection_align_insertion_frequency = 0xff;
1346b5f18a20SDave Jiang 		u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1347b5f18a20SDave Jiang 	}
1348b5f18a20SDave Jiang 
1349b5f18a20SDave Jiang 	u->stp_inactivity_timeout = stp_inactive_to;
1350b5f18a20SDave Jiang 	u->ssp_inactivity_timeout = ssp_inactive_to;
1351b5f18a20SDave Jiang 	u->stp_max_occupancy_timeout = stp_max_occ_to;
1352b5f18a20SDave Jiang 	u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1353b5f18a20SDave Jiang 	u->no_outbound_task_timeout = no_outbound_task_to;
13547000f7c7SAndrzej Jakowski 	u->max_concurr_spinup = max_concurr_spinup;
1355b5f18a20SDave Jiang }
1356b5f18a20SDave Jiang 
135789a7301fSDan Williams static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
1358cc9203bfSDan Williams {
1359d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1360cc9203bfSDan Williams 
1361d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1362cc9203bfSDan Williams }
1363cc9203bfSDan Williams 
136489a7301fSDan Williams static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
1365cc9203bfSDan Williams {
1366d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1367cc9203bfSDan Williams 
1368d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1369cc9203bfSDan Williams }
1370cc9203bfSDan Williams 
1371cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1372cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1373cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1374cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1375cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1376cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1377cc9203bfSDan Williams 
1378cc9203bfSDan Williams /**
137989a7301fSDan Williams  * sci_controller_set_interrupt_coalescence() - This method allows the user to
1380cc9203bfSDan Williams  *    configure the interrupt coalescence.
1381cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1382cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1383cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1384cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1385cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1386cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1387cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1388cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1389cc9203bfSDan Williams  *    interrupt coalescing timeout.
1390cc9203bfSDan Williams  *
1391cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1392cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1393cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1394cc9203bfSDan Williams  */
1395d9dcb4baSDan Williams static enum sci_status
139689a7301fSDan Williams sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1397cc9203bfSDan Williams 					 u32 coalesce_number,
1398cc9203bfSDan Williams 					 u32 coalesce_timeout)
1399cc9203bfSDan Williams {
1400cc9203bfSDan Williams 	u8 timeout_encode = 0;
1401cc9203bfSDan Williams 	u32 min = 0;
1402cc9203bfSDan Williams 	u32 max = 0;
1403cc9203bfSDan Williams 
1404cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1405cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1406cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1407cc9203bfSDan Williams 
1408cc9203bfSDan Williams 	/*
1409cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1410cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1411cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1412cc9203bfSDan Williams 	 *              0       -        -       Disabled
1413cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1414cc9203bfSDan Williams 	 *              2       26.7     40.0
1415cc9203bfSDan Williams 	 *              3       53.3     80.0
1416cc9203bfSDan Williams 	 *              4       106.7    160.0
1417cc9203bfSDan Williams 	 *              5       213.3    320.0
1418cc9203bfSDan Williams 	 *              6       426.7    640.0
1419cc9203bfSDan Williams 	 *              7       853.3    1280.0
1420cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1421cc9203bfSDan Williams 	 *              9       3.4      5.1
1422cc9203bfSDan Williams 	 *              10      6.8      10.2
1423cc9203bfSDan Williams 	 *              11      13.7     20.5
1424cc9203bfSDan Williams 	 *              12      27.3     41.0
1425cc9203bfSDan Williams 	 *              13      54.6     81.9
1426cc9203bfSDan Williams 	 *              14      109.2    163.8
1427cc9203bfSDan Williams 	 *              15      218.5    327.7
1428cc9203bfSDan Williams 	 *              16      436.9    655.4
1429cc9203bfSDan Williams 	 *              17      873.8    1310.7
1430cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1431cc9203bfSDan Williams 	 *              19      3.5      5.2
1432cc9203bfSDan Williams 	 *              20      7.0      10.5
1433cc9203bfSDan Williams 	 *              21      14.0     21.0
1434cc9203bfSDan Williams 	 *              22      28.0     41.9
1435cc9203bfSDan Williams 	 *              23      55.9     83.9
1436cc9203bfSDan Williams 	 *              24      111.8    167.8
1437cc9203bfSDan Williams 	 *              25      223.7    335.5
1438cc9203bfSDan Williams 	 *              26      447.4    671.1
1439cc9203bfSDan Williams 	 *              27      894.8    1342.2
1440cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1441cc9203bfSDan Williams 	 *              Others Undefined */
1442cc9203bfSDan Williams 
1443cc9203bfSDan Williams 	/*
1444cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1445cc9203bfSDan Williams 	 * value for register writing. */
1446cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1447cc9203bfSDan Williams 		timeout_encode = 0;
1448cc9203bfSDan Williams 	else{
1449cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1450cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1451cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1452cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1453cc9203bfSDan Williams 
1454cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1455cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1456cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1457cc9203bfSDan Williams 		      timeout_encode++) {
1458cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1459cc9203bfSDan Williams 				break;
1460cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1461cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1462cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1463cc9203bfSDan Williams 					break;
1464cc9203bfSDan Williams 				else{
1465cc9203bfSDan Williams 					timeout_encode++;
1466cc9203bfSDan Williams 					break;
1467cc9203bfSDan Williams 				}
1468cc9203bfSDan Williams 			} else {
1469cc9203bfSDan Williams 				max = max * 2;
1470cc9203bfSDan Williams 				min = min * 2;
1471cc9203bfSDan Williams 			}
1472cc9203bfSDan Williams 		}
1473cc9203bfSDan Williams 
1474cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1475cc9203bfSDan Williams 			/* the value is out of range. */
1476cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1477cc9203bfSDan Williams 	}
1478cc9203bfSDan Williams 
1479cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1480cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1481d9dcb4baSDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
1482cc9203bfSDan Williams 
1483cc9203bfSDan Williams 
1484d9dcb4baSDan Williams 	ihost->interrupt_coalesce_number = (u16)coalesce_number;
1485d9dcb4baSDan Williams 	ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
1486cc9203bfSDan Williams 
1487cc9203bfSDan Williams 	return SCI_SUCCESS;
1488cc9203bfSDan Williams }
1489cc9203bfSDan Williams 
1490cc9203bfSDan Williams 
149189a7301fSDan Williams static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
1492cc9203bfSDan Williams {
1493d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1494cc9203bfSDan Williams 
1495cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
14969b4be528SDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1497cc9203bfSDan Williams }
1498cc9203bfSDan Williams 
149989a7301fSDan Williams static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
1500cc9203bfSDan Williams {
1501d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1502cc9203bfSDan Williams 
1503cc9203bfSDan Williams 	/* disable interrupt coalescence. */
150489a7301fSDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1505cc9203bfSDan Williams }
1506cc9203bfSDan Williams 
150789a7301fSDan Williams static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
1508cc9203bfSDan Williams {
1509cc9203bfSDan Williams 	u32 index;
1510cc9203bfSDan Williams 	enum sci_status status;
1511cc9203bfSDan Williams 	enum sci_status phy_status;
1512cc9203bfSDan Williams 
1513cc9203bfSDan Williams 	status = SCI_SUCCESS;
1514cc9203bfSDan Williams 
1515cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
151689a7301fSDan Williams 		phy_status = sci_phy_stop(&ihost->phys[index]);
1517cc9203bfSDan Williams 
1518cc9203bfSDan Williams 		if (phy_status != SCI_SUCCESS &&
1519cc9203bfSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1520cc9203bfSDan Williams 			status = SCI_FAILURE;
1521cc9203bfSDan Williams 
1522d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1523cc9203bfSDan Williams 				 "%s: Controller stop operation failed to stop "
1524cc9203bfSDan Williams 				 "phy %d because of status %d.\n",
1525cc9203bfSDan Williams 				 __func__,
152685280955SDan Williams 				 ihost->phys[index].phy_index, phy_status);
1527cc9203bfSDan Williams 		}
1528cc9203bfSDan Williams 	}
1529cc9203bfSDan Williams 
1530cc9203bfSDan Williams 	return status;
1531cc9203bfSDan Williams }
1532cc9203bfSDan Williams 
153389a7301fSDan Williams static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
1534cc9203bfSDan Williams {
1535cc9203bfSDan Williams 	u32 index;
1536cc9203bfSDan Williams 	enum sci_status port_status;
1537cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1538cc9203bfSDan Williams 
1539d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1540ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1541cc9203bfSDan Williams 
154289a7301fSDan Williams 		port_status = sci_port_stop(iport);
1543cc9203bfSDan Williams 
1544cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1545cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1546cc9203bfSDan Williams 			status = SCI_FAILURE;
1547cc9203bfSDan Williams 
1548d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1549cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1550cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1551cc9203bfSDan Williams 				 __func__,
1552ffe191c9SDan Williams 				 iport->logical_port_index,
1553cc9203bfSDan Williams 				 port_status);
1554cc9203bfSDan Williams 		}
1555cc9203bfSDan Williams 	}
1556cc9203bfSDan Williams 
1557cc9203bfSDan Williams 	return status;
1558cc9203bfSDan Williams }
1559cc9203bfSDan Williams 
156089a7301fSDan Williams static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
1561cc9203bfSDan Williams {
1562cc9203bfSDan Williams 	u32 index;
1563cc9203bfSDan Williams 	enum sci_status status;
1564cc9203bfSDan Williams 	enum sci_status device_status;
1565cc9203bfSDan Williams 
1566cc9203bfSDan Williams 	status = SCI_SUCCESS;
1567cc9203bfSDan Williams 
1568d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
1569d9dcb4baSDan Williams 		if (ihost->device_table[index] != NULL) {
1570cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
157189a7301fSDan Williams 			device_status = sci_remote_device_stop(ihost->device_table[index], 0);
1572cc9203bfSDan Williams 
1573cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1574cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1575d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
1576cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1577cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1578cc9203bfSDan Williams 					 "status %d.\n",
1579cc9203bfSDan Williams 					 __func__,
1580d9dcb4baSDan Williams 					 ihost->device_table[index], device_status);
1581cc9203bfSDan Williams 			}
1582cc9203bfSDan Williams 		}
1583cc9203bfSDan Williams 	}
1584cc9203bfSDan Williams 
1585cc9203bfSDan Williams 	return status;
1586cc9203bfSDan Williams }
1587cc9203bfSDan Williams 
158889a7301fSDan Williams static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1589cc9203bfSDan Williams {
1590d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1591cc9203bfSDan Williams 
1592cc9203bfSDan Williams 	/* Stop all of the components for this controller */
159389a7301fSDan Williams 	sci_controller_stop_phys(ihost);
159489a7301fSDan Williams 	sci_controller_stop_ports(ihost);
159589a7301fSDan Williams 	sci_controller_stop_devices(ihost);
1596cc9203bfSDan Williams }
1597cc9203bfSDan Williams 
159889a7301fSDan Williams static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1599cc9203bfSDan Williams {
1600d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1601cc9203bfSDan Williams 
1602d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1603cc9203bfSDan Williams }
1604cc9203bfSDan Williams 
160589a7301fSDan Williams static void sci_controller_reset_hardware(struct isci_host *ihost)
1606cc9203bfSDan Williams {
1607cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
160889a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1609cc9203bfSDan Williams 
1610cc9203bfSDan Williams 	/* Reset the SCU */
1611d9dcb4baSDan Williams 	writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
1612cc9203bfSDan Williams 
1613cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1614cc9203bfSDan Williams 	udelay(1000);
1615cc9203bfSDan Williams 
1616cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1617d9dcb4baSDan Williams 	writel(0x00000000, &ihost->smu_registers->completion_queue_get);
1618cc9203bfSDan Williams 
1619cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1620d9dcb4baSDan Williams 	writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
1621cc9203bfSDan Williams }
1622cc9203bfSDan Williams 
162389a7301fSDan Williams static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1624cc9203bfSDan Williams {
1625d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1626cc9203bfSDan Williams 
162789a7301fSDan Williams 	sci_controller_reset_hardware(ihost);
1628d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1629cc9203bfSDan Williams }
1630cc9203bfSDan Williams 
163189a7301fSDan Williams static const struct sci_base_state sci_controller_state_table[] = {
1632e301370aSEdmund Nadolski 	[SCIC_INITIAL] = {
163389a7301fSDan Williams 		.enter_state = sci_controller_initial_state_enter,
1634cc9203bfSDan Williams 	},
1635e301370aSEdmund Nadolski 	[SCIC_RESET] = {},
1636e301370aSEdmund Nadolski 	[SCIC_INITIALIZING] = {},
1637e301370aSEdmund Nadolski 	[SCIC_INITIALIZED] = {},
1638e301370aSEdmund Nadolski 	[SCIC_STARTING] = {
163989a7301fSDan Williams 		.exit_state  = sci_controller_starting_state_exit,
1640cc9203bfSDan Williams 	},
1641e301370aSEdmund Nadolski 	[SCIC_READY] = {
164289a7301fSDan Williams 		.enter_state = sci_controller_ready_state_enter,
164389a7301fSDan Williams 		.exit_state  = sci_controller_ready_state_exit,
1644cc9203bfSDan Williams 	},
1645e301370aSEdmund Nadolski 	[SCIC_RESETTING] = {
164689a7301fSDan Williams 		.enter_state = sci_controller_resetting_state_enter,
1647cc9203bfSDan Williams 	},
1648e301370aSEdmund Nadolski 	[SCIC_STOPPING] = {
164989a7301fSDan Williams 		.enter_state = sci_controller_stopping_state_enter,
165089a7301fSDan Williams 		.exit_state = sci_controller_stopping_state_exit,
1651cc9203bfSDan Williams 	},
1652e301370aSEdmund Nadolski 	[SCIC_STOPPED] = {},
1653e301370aSEdmund Nadolski 	[SCIC_FAILED] = {}
1654cc9203bfSDan Williams };
1655cc9203bfSDan Williams 
165689a7301fSDan Williams static void sci_controller_set_default_config_parameters(struct isci_host *ihost)
1657cc9203bfSDan Williams {
1658cc9203bfSDan Williams 	/* these defaults are overridden by the platform / firmware */
1659cc9203bfSDan Williams 	u16 index;
1660cc9203bfSDan Williams 
1661cc9203bfSDan Williams 	/* Default to APC mode. */
166289a7301fSDan Williams 	ihost->oem_parameters.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1663cc9203bfSDan Williams 
1664cc9203bfSDan Williams 	/* Default to APC mode. */
16657000f7c7SAndrzej Jakowski 	ihost->oem_parameters.controller.max_concurr_spin_up = 1;
1666cc9203bfSDan Williams 
1667cc9203bfSDan Williams 	/* Default to no SSC operation. */
166889a7301fSDan Williams 	ihost->oem_parameters.controller.do_enable_ssc = false;
1669cc9203bfSDan Williams 
16709fee607fSJeff Skirvin 	/* Default to short cables on all phys. */
16719fee607fSJeff Skirvin 	ihost->oem_parameters.controller.cable_selection_mask = 0;
16729fee607fSJeff Skirvin 
1673cc9203bfSDan Williams 	/* Initialize all of the port parameter information to narrow ports. */
1674cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PORTS; index++) {
167589a7301fSDan Williams 		ihost->oem_parameters.ports[index].phy_mask = 0;
1676cc9203bfSDan Williams 	}
1677cc9203bfSDan Williams 
1678cc9203bfSDan Williams 	/* Initialize all of the phy parameter information. */
1679cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1680be168a3bSJeff Skirvin 		/* Default to 3G (i.e. Gen 2). */
1681be168a3bSJeff Skirvin 		ihost->user_parameters.phys[index].max_speed_generation =
1682be168a3bSJeff Skirvin 			SCIC_SDS_PARM_GEN2_SPEED;
1683cc9203bfSDan Williams 
1684cc9203bfSDan Williams 		/* the frequencies cannot be 0 */
168589a7301fSDan Williams 		ihost->user_parameters.phys[index].align_insertion_frequency = 0x7f;
168689a7301fSDan Williams 		ihost->user_parameters.phys[index].in_connection_align_insertion_frequency = 0xff;
168789a7301fSDan Williams 		ihost->user_parameters.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1688cc9203bfSDan Williams 
1689cc9203bfSDan Williams 		/*
1690cc9203bfSDan Williams 		 * Previous Vitesse based expanders had a arbitration issue that
1691cc9203bfSDan Williams 		 * is worked around by having the upper 32-bits of SAS address
1692cc9203bfSDan Williams 		 * with a value greater then the Vitesse company identifier.
1693cc9203bfSDan Williams 		 * Hence, usage of 0x5FCFFFFF. */
169489a7301fSDan Williams 		ihost->oem_parameters.phys[index].sas_address.low = 0x1 + ihost->id;
169589a7301fSDan Williams 		ihost->oem_parameters.phys[index].sas_address.high = 0x5FCFFFFF;
1696cc9203bfSDan Williams 	}
1697cc9203bfSDan Williams 
169889a7301fSDan Williams 	ihost->user_parameters.stp_inactivity_timeout = 5;
169989a7301fSDan Williams 	ihost->user_parameters.ssp_inactivity_timeout = 5;
170089a7301fSDan Williams 	ihost->user_parameters.stp_max_occupancy_timeout = 5;
170189a7301fSDan Williams 	ihost->user_parameters.ssp_max_occupancy_timeout = 20;
17026024d38bSMarcin Tomczak 	ihost->user_parameters.no_outbound_task_timeout = 2;
1703cc9203bfSDan Williams }
1704cc9203bfSDan Williams 
17056cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data)
17066cb5853dSEdmund Nadolski {
17076cb5853dSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1708d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1709d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
17106cb5853dSEdmund Nadolski 	unsigned long flags;
1711cc9203bfSDan Williams 
17126cb5853dSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
17136cb5853dSEdmund Nadolski 
17146cb5853dSEdmund Nadolski 	if (tmr->cancel)
17156cb5853dSEdmund Nadolski 		goto done;
17166cb5853dSEdmund Nadolski 
1717e301370aSEdmund Nadolski 	if (sm->current_state_id == SCIC_STARTING)
171889a7301fSDan Williams 		sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
1719e301370aSEdmund Nadolski 	else if (sm->current_state_id == SCIC_STOPPING) {
1720e301370aSEdmund Nadolski 		sci_change_state(sm, SCIC_FAILED);
17216cb5853dSEdmund Nadolski 		isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
17226cb5853dSEdmund Nadolski 	} else	/* / @todo Now what do we want to do in this case? */
1723d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
17246cb5853dSEdmund Nadolski 			"%s: Controller timer fired when controller was not "
17256cb5853dSEdmund Nadolski 			"in a state being timed.\n",
17266cb5853dSEdmund Nadolski 			__func__);
17276cb5853dSEdmund Nadolski 
17286cb5853dSEdmund Nadolski done:
17296cb5853dSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
17306cb5853dSEdmund Nadolski }
1731cc9203bfSDan Williams 
173289a7301fSDan Williams static enum sci_status sci_controller_construct(struct isci_host *ihost,
1733cc9203bfSDan Williams 						void __iomem *scu_base,
1734cc9203bfSDan Williams 						void __iomem *smu_base)
1735cc9203bfSDan Williams {
1736cc9203bfSDan Williams 	u8 i;
1737cc9203bfSDan Williams 
173889a7301fSDan Williams 	sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
1739cc9203bfSDan Williams 
1740d9dcb4baSDan Williams 	ihost->scu_registers = scu_base;
1741d9dcb4baSDan Williams 	ihost->smu_registers = smu_base;
1742cc9203bfSDan Williams 
174389a7301fSDan Williams 	sci_port_configuration_agent_construct(&ihost->port_agent);
1744cc9203bfSDan Williams 
1745cc9203bfSDan Williams 	/* Construct the ports for this controller */
1746cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
174789a7301fSDan Williams 		sci_port_construct(&ihost->ports[i], i, ihost);
174889a7301fSDan Williams 	sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
1749cc9203bfSDan Williams 
1750cc9203bfSDan Williams 	/* Construct the phys for this controller */
1751cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1752cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
175389a7301fSDan Williams 		sci_phy_construct(&ihost->phys[i],
1754ffe191c9SDan Williams 				  &ihost->ports[SCI_MAX_PORTS], i);
1755cc9203bfSDan Williams 	}
1756cc9203bfSDan Williams 
1757d9dcb4baSDan Williams 	ihost->invalid_phy_mask = 0;
1758cc9203bfSDan Williams 
1759d9dcb4baSDan Williams 	sci_init_timer(&ihost->timer, controller_timeout);
17606cb5853dSEdmund Nadolski 
1761cc9203bfSDan Williams 	/* Initialize the User and OEM parameters to default values. */
176289a7301fSDan Williams 	sci_controller_set_default_config_parameters(ihost);
1763cc9203bfSDan Williams 
176489a7301fSDan Williams 	return sci_controller_reset(ihost);
1765cc9203bfSDan Williams }
1766cc9203bfSDan Williams 
1767594e566aSDave Jiang int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
1768cc9203bfSDan Williams {
1769cc9203bfSDan Williams 	int i;
1770cc9203bfSDan Williams 
1771cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1772cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1773cc9203bfSDan Williams 			return -EINVAL;
1774cc9203bfSDan Williams 
1775cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1776cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1777cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1778cc9203bfSDan Williams 			return -EINVAL;
1779cc9203bfSDan Williams 
1780cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1781cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1782cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1783cc9203bfSDan Williams 				return -EINVAL;
1784cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1785cc9203bfSDan Williams 		u8 phy_mask = 0;
1786cc9203bfSDan Williams 
1787cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1788cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1789cc9203bfSDan Williams 
1790cc9203bfSDan Williams 		if (phy_mask == 0)
1791cc9203bfSDan Williams 			return -EINVAL;
1792cc9203bfSDan Williams 	} else
1793cc9203bfSDan Williams 		return -EINVAL;
1794cc9203bfSDan Williams 
17957000f7c7SAndrzej Jakowski 	if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
17967000f7c7SAndrzej Jakowski 	    oem->controller.max_concurr_spin_up < 1)
1797cc9203bfSDan Williams 		return -EINVAL;
1798cc9203bfSDan Williams 
1799594e566aSDave Jiang 	if (oem->controller.do_enable_ssc) {
1800594e566aSDave Jiang 		if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
1801594e566aSDave Jiang 			return -EINVAL;
1802594e566aSDave Jiang 
1803594e566aSDave Jiang 		if (version >= ISCI_ROM_VER_1_1) {
1804594e566aSDave Jiang 			u8 test = oem->controller.ssc_sata_tx_spread_level;
1805594e566aSDave Jiang 
1806594e566aSDave Jiang 			switch (test) {
1807594e566aSDave Jiang 			case 0:
1808594e566aSDave Jiang 			case 2:
1809594e566aSDave Jiang 			case 3:
1810594e566aSDave Jiang 			case 6:
1811594e566aSDave Jiang 			case 7:
1812594e566aSDave Jiang 				break;
1813594e566aSDave Jiang 			default:
1814594e566aSDave Jiang 				return -EINVAL;
1815594e566aSDave Jiang 			}
1816594e566aSDave Jiang 
1817594e566aSDave Jiang 			test = oem->controller.ssc_sas_tx_spread_level;
1818594e566aSDave Jiang 			if (oem->controller.ssc_sas_tx_type == 0) {
1819594e566aSDave Jiang 				switch (test) {
1820594e566aSDave Jiang 				case 0:
1821594e566aSDave Jiang 				case 2:
1822594e566aSDave Jiang 				case 3:
1823594e566aSDave Jiang 					break;
1824594e566aSDave Jiang 				default:
1825594e566aSDave Jiang 					return -EINVAL;
1826594e566aSDave Jiang 				}
1827594e566aSDave Jiang 			} else if (oem->controller.ssc_sas_tx_type == 1) {
1828594e566aSDave Jiang 				switch (test) {
1829594e566aSDave Jiang 				case 0:
1830594e566aSDave Jiang 				case 3:
1831594e566aSDave Jiang 				case 6:
1832594e566aSDave Jiang 					break;
1833594e566aSDave Jiang 				default:
1834594e566aSDave Jiang 					return -EINVAL;
1835594e566aSDave Jiang 				}
1836594e566aSDave Jiang 			}
1837594e566aSDave Jiang 		}
1838594e566aSDave Jiang 	}
1839594e566aSDave Jiang 
1840cc9203bfSDan Williams 	return 0;
1841cc9203bfSDan Williams }
1842cc9203bfSDan Williams 
184389a7301fSDan Williams static enum sci_status sci_oem_parameters_set(struct isci_host *ihost)
1844cc9203bfSDan Williams {
1845d9dcb4baSDan Williams 	u32 state = ihost->sm.current_state_id;
1846594e566aSDave Jiang 	struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
1847cc9203bfSDan Williams 
1848e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
1849e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
1850e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
1851cc9203bfSDan Williams 
1852594e566aSDave Jiang 		if (sci_oem_parameters_validate(&ihost->oem_parameters,
1853594e566aSDave Jiang 						pci_info->orom->hdr.version))
1854cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1855cc9203bfSDan Williams 
1856cc9203bfSDan Williams 		return SCI_SUCCESS;
1857cc9203bfSDan Williams 	}
1858cc9203bfSDan Williams 
1859cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
1860cc9203bfSDan Williams }
1861cc9203bfSDan Williams 
18627000f7c7SAndrzej Jakowski static u8 max_spin_up(struct isci_host *ihost)
18637000f7c7SAndrzej Jakowski {
18647000f7c7SAndrzej Jakowski 	if (ihost->user_parameters.max_concurr_spinup)
18657000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->user_parameters.max_concurr_spinup,
18667000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
18677000f7c7SAndrzej Jakowski 	else
18687000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
18697000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
18707000f7c7SAndrzej Jakowski }
18717000f7c7SAndrzej Jakowski 
18720473661aSEdmund Nadolski static void power_control_timeout(unsigned long data)
1873cc9203bfSDan Williams {
18740473661aSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1875d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
187685280955SDan Williams 	struct isci_phy *iphy;
18770473661aSEdmund Nadolski 	unsigned long flags;
18780473661aSEdmund Nadolski 	u8 i;
1879cc9203bfSDan Williams 
18800473661aSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
1881cc9203bfSDan Williams 
18820473661aSEdmund Nadolski 	if (tmr->cancel)
18830473661aSEdmund Nadolski 		goto done;
1884cc9203bfSDan Williams 
1885d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
1886cc9203bfSDan Williams 
1887d9dcb4baSDan Williams 	if (ihost->power_control.phys_waiting == 0) {
1888d9dcb4baSDan Williams 		ihost->power_control.timer_started = false;
18890473661aSEdmund Nadolski 		goto done;
18900473661aSEdmund Nadolski 	}
1891cc9203bfSDan Williams 
18920473661aSEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
18930473661aSEdmund Nadolski 
1894d9dcb4baSDan Williams 		if (ihost->power_control.phys_waiting == 0)
18950473661aSEdmund Nadolski 			break;
18960473661aSEdmund Nadolski 
1897d9dcb4baSDan Williams 		iphy = ihost->power_control.requesters[i];
189885280955SDan Williams 		if (iphy == NULL)
18990473661aSEdmund Nadolski 			continue;
19000473661aSEdmund Nadolski 
19017000f7c7SAndrzej Jakowski 		if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
19020473661aSEdmund Nadolski 			break;
19030473661aSEdmund Nadolski 
1904d9dcb4baSDan Williams 		ihost->power_control.requesters[i] = NULL;
1905d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1906d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
190789a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1908*be778341SMarcin Tomczak 
1909*be778341SMarcin Tomczak 		if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
1910*be778341SMarcin Tomczak 			u8 j;
1911*be778341SMarcin Tomczak 
1912*be778341SMarcin Tomczak 			for (j = 0; j < SCI_MAX_PHYS; j++) {
1913*be778341SMarcin Tomczak 				struct isci_phy *requester = ihost->power_control.requesters[j];
1914*be778341SMarcin Tomczak 
1915*be778341SMarcin Tomczak 				/*
1916*be778341SMarcin Tomczak 				 * Search the power_control queue to see if there are other phys
1917*be778341SMarcin Tomczak 				 * attached to the same remote device. If found, take all of
1918*be778341SMarcin Tomczak 				 * them out of await_sas_power state.
1919*be778341SMarcin Tomczak 				 */
1920*be778341SMarcin Tomczak 				if (requester != NULL && requester != iphy) {
1921*be778341SMarcin Tomczak 					u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
1922*be778341SMarcin Tomczak 							  iphy->frame_rcvd.iaf.sas_addr,
1923*be778341SMarcin Tomczak 							  sizeof(requester->frame_rcvd.iaf.sas_addr));
1924*be778341SMarcin Tomczak 
1925*be778341SMarcin Tomczak 					if (other == 0) {
1926*be778341SMarcin Tomczak 						ihost->power_control.requesters[j] = NULL;
1927*be778341SMarcin Tomczak 						ihost->power_control.phys_waiting--;
1928*be778341SMarcin Tomczak 						sci_phy_consume_power_handler(requester);
1929*be778341SMarcin Tomczak 					}
1930*be778341SMarcin Tomczak 				}
1931*be778341SMarcin Tomczak 			}
1932*be778341SMarcin Tomczak 		}
1933cc9203bfSDan Williams 	}
1934cc9203bfSDan Williams 
1935cc9203bfSDan Williams 	/*
1936cc9203bfSDan Williams 	 * It doesn't matter if the power list is empty, we need to start the
1937cc9203bfSDan Williams 	 * timer in case another phy becomes ready.
1938cc9203bfSDan Williams 	 */
19390473661aSEdmund Nadolski 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1940d9dcb4baSDan Williams 	ihost->power_control.timer_started = true;
19410473661aSEdmund Nadolski 
19420473661aSEdmund Nadolski done:
19430473661aSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1944cc9203bfSDan Williams }
1945cc9203bfSDan Williams 
194689a7301fSDan Williams void sci_controller_power_control_queue_insert(struct isci_host *ihost,
194785280955SDan Williams 					       struct isci_phy *iphy)
1948cc9203bfSDan Williams {
194985280955SDan Williams 	BUG_ON(iphy == NULL);
1950cc9203bfSDan Williams 
19517000f7c7SAndrzej Jakowski 	if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
1952d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
195389a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1954cc9203bfSDan Williams 
1955cc9203bfSDan Williams 		/*
1956cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1957cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1958cc9203bfSDan Williams 		 */
1959d9dcb4baSDan Williams 		if (ihost->power_control.timer_started)
1960d9dcb4baSDan Williams 			sci_del_timer(&ihost->power_control.timer);
19610473661aSEdmund Nadolski 
1962d9dcb4baSDan Williams 		sci_mod_timer(&ihost->power_control.timer,
19630473661aSEdmund Nadolski 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1964d9dcb4baSDan Williams 		ihost->power_control.timer_started = true;
19650473661aSEdmund Nadolski 
1966cc9203bfSDan Williams 	} else {
1967*be778341SMarcin Tomczak 		/*
1968*be778341SMarcin Tomczak 		 * There are phys, attached to the same sas address as this phy, are
1969*be778341SMarcin Tomczak 		 * already in READY state, this phy don't need wait.
1970*be778341SMarcin Tomczak 		 */
1971*be778341SMarcin Tomczak 		u8 i;
1972*be778341SMarcin Tomczak 		struct isci_phy *current_phy;
1973*be778341SMarcin Tomczak 
1974*be778341SMarcin Tomczak 		for (i = 0; i < SCI_MAX_PHYS; i++) {
1975*be778341SMarcin Tomczak 			u8 other;
1976*be778341SMarcin Tomczak 			current_phy = &ihost->phys[i];
1977*be778341SMarcin Tomczak 
1978*be778341SMarcin Tomczak 			other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
1979*be778341SMarcin Tomczak 				       iphy->frame_rcvd.iaf.sas_addr,
1980*be778341SMarcin Tomczak 				       sizeof(current_phy->frame_rcvd.iaf.sas_addr));
1981*be778341SMarcin Tomczak 
1982*be778341SMarcin Tomczak 			if (current_phy->sm.current_state_id == SCI_PHY_READY &&
1983*be778341SMarcin Tomczak 			    current_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS &&
1984*be778341SMarcin Tomczak 			    other == 0) {
1985*be778341SMarcin Tomczak 				sci_phy_consume_power_handler(iphy);
1986*be778341SMarcin Tomczak 				break;
1987*be778341SMarcin Tomczak 			}
1988*be778341SMarcin Tomczak 		}
1989*be778341SMarcin Tomczak 
1990*be778341SMarcin Tomczak 		if (i == SCI_MAX_PHYS) {
1991cc9203bfSDan Williams 			/* Add the phy in the waiting list */
1992d9dcb4baSDan Williams 			ihost->power_control.requesters[iphy->phy_index] = iphy;
1993d9dcb4baSDan Williams 			ihost->power_control.phys_waiting++;
1994cc9203bfSDan Williams 		}
1995cc9203bfSDan Williams 	}
1996*be778341SMarcin Tomczak }
1997cc9203bfSDan Williams 
199889a7301fSDan Williams void sci_controller_power_control_queue_remove(struct isci_host *ihost,
199985280955SDan Williams 					       struct isci_phy *iphy)
2000cc9203bfSDan Williams {
200185280955SDan Williams 	BUG_ON(iphy == NULL);
2002cc9203bfSDan Williams 
200389a7301fSDan Williams 	if (ihost->power_control.requesters[iphy->phy_index])
2004d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
2005cc9203bfSDan Williams 
2006d9dcb4baSDan Williams 	ihost->power_control.requesters[iphy->phy_index] = NULL;
2007cc9203bfSDan Williams }
2008cc9203bfSDan Williams 
2009afd13a1fSJeff Skirvin static int is_long_cable(int phy, unsigned char selection_byte)
2010afd13a1fSJeff Skirvin {
20119fee607fSJeff Skirvin 	return !!(selection_byte & (1 << phy));
2012afd13a1fSJeff Skirvin }
2013afd13a1fSJeff Skirvin 
2014afd13a1fSJeff Skirvin static int is_medium_cable(int phy, unsigned char selection_byte)
2015afd13a1fSJeff Skirvin {
20169fee607fSJeff Skirvin 	return !!(selection_byte & (1 << (phy + 4)));
20179fee607fSJeff Skirvin }
20189fee607fSJeff Skirvin 
20199fee607fSJeff Skirvin static enum cable_selections decode_selection_byte(
20209fee607fSJeff Skirvin 	int phy,
20219fee607fSJeff Skirvin 	unsigned char selection_byte)
20229fee607fSJeff Skirvin {
20239fee607fSJeff Skirvin 	return ((selection_byte & (1 << phy)) ? 1 : 0)
20249fee607fSJeff Skirvin 		+ (selection_byte & (1 << (phy + 4)) ? 2 : 0);
20259fee607fSJeff Skirvin }
20269fee607fSJeff Skirvin 
20279fee607fSJeff Skirvin static unsigned char *to_cable_select(struct isci_host *ihost)
20289fee607fSJeff Skirvin {
20299fee607fSJeff Skirvin 	if (is_cable_select_overridden())
20309fee607fSJeff Skirvin 		return ((unsigned char *)&cable_selection_override)
20319fee607fSJeff Skirvin 			+ ihost->id;
20329fee607fSJeff Skirvin 	else
20339fee607fSJeff Skirvin 		return &ihost->oem_parameters.controller.cable_selection_mask;
20349fee607fSJeff Skirvin }
20359fee607fSJeff Skirvin 
20369fee607fSJeff Skirvin enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
20379fee607fSJeff Skirvin {
20389fee607fSJeff Skirvin 	return decode_selection_byte(phy, *to_cable_select(ihost));
20399fee607fSJeff Skirvin }
20409fee607fSJeff Skirvin 
20419fee607fSJeff Skirvin char *lookup_cable_names(enum cable_selections selection)
20429fee607fSJeff Skirvin {
20439fee607fSJeff Skirvin 	static char *cable_names[] = {
20449fee607fSJeff Skirvin 		[short_cable]     = "short",
20459fee607fSJeff Skirvin 		[long_cable]      = "long",
20469fee607fSJeff Skirvin 		[medium_cable]    = "medium",
20479fee607fSJeff Skirvin 		[undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
20489fee607fSJeff Skirvin 	};
20499fee607fSJeff Skirvin 	return (selection <= undefined_cable) ? cable_names[selection]
20509fee607fSJeff Skirvin 					      : cable_names[undefined_cable];
2051afd13a1fSJeff Skirvin }
2052afd13a1fSJeff Skirvin 
2053cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
2054cc9203bfSDan Williams 
205589a7301fSDan Williams static void sci_controller_afe_initialization(struct isci_host *ihost)
2056cc9203bfSDan Williams {
20572e5da889SDan Williams 	struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
205889a7301fSDan Williams 	const struct sci_oem_params *oem = &ihost->oem_parameters;
2059dc00c8b6SDan Williams 	struct pci_dev *pdev = ihost->pdev;
2060cc9203bfSDan Williams 	u32 afe_status;
2061cc9203bfSDan Williams 	u32 phy_id;
20629fee607fSJeff Skirvin 	unsigned char cable_selection_mask = *to_cable_select(ihost);
2063cc9203bfSDan Williams 
2064cc9203bfSDan Williams 	/* Clear DFX Status registers */
20652e5da889SDan Williams 	writel(0x0081000f, &afe->afe_dfx_master_control0);
2066cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2067cc9203bfSDan Williams 
2068afd13a1fSJeff Skirvin 	if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
2069cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
20702e5da889SDan Williams 		 * Timer, PM Stagger Timer
20712e5da889SDan Williams 		 */
2072afd13a1fSJeff Skirvin 		writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
2073cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2074cc9203bfSDan Williams 	}
2075cc9203bfSDan Williams 
2076cc9203bfSDan Williams 	/* Configure bias currents to normal */
2077dc00c8b6SDan Williams 	if (is_a2(pdev))
20782e5da889SDan Williams 		writel(0x00005A00, &afe->afe_bias_control);
2079dc00c8b6SDan Williams 	else if (is_b0(pdev) || is_c0(pdev))
20802e5da889SDan Williams 		writel(0x00005F00, &afe->afe_bias_control);
2081afd13a1fSJeff Skirvin 	else if (is_c1(pdev))
2082afd13a1fSJeff Skirvin 		writel(0x00005500, &afe->afe_bias_control);
2083cc9203bfSDan Williams 
2084cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2085cc9203bfSDan Williams 
2086cc9203bfSDan Williams 	/* Enable PLL */
2087afd13a1fSJeff Skirvin 	if (is_a2(pdev))
20882e5da889SDan Williams 		writel(0x80040908, &afe->afe_pll_control0);
2089afd13a1fSJeff Skirvin 	else if (is_b0(pdev) || is_c0(pdev))
2090afd13a1fSJeff Skirvin 		writel(0x80040A08, &afe->afe_pll_control0);
2091afd13a1fSJeff Skirvin 	else if (is_c1(pdev)) {
2092afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
2093afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
2094afd13a1fSJeff Skirvin 		writel(0x00000B08, &afe->afe_pll_control0);
2095afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
2096afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
2097afd13a1fSJeff Skirvin 	}
2098cc9203bfSDan Williams 
2099cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2100cc9203bfSDan Williams 
2101cc9203bfSDan Williams 	/* Wait for the PLL to lock */
2102cc9203bfSDan Williams 	do {
21032e5da889SDan Williams 		afe_status = readl(&afe->afe_common_block_status);
2104cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2105cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
2106cc9203bfSDan Williams 
2107dc00c8b6SDan Williams 	if (is_a2(pdev)) {
21082e5da889SDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76
21092e5da889SDan Williams 		 * us to 50 us)
21102e5da889SDan Williams 		 */
21112e5da889SDan Williams 		writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
2112cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2113cc9203bfSDan Williams 	}
2114cc9203bfSDan Williams 
2115cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
21162e5da889SDan Williams 		struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id];
2117cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2118afd13a1fSJeff Skirvin 		int cable_length_long =
2119afd13a1fSJeff Skirvin 			is_long_cable(phy_id, cable_selection_mask);
2120afd13a1fSJeff Skirvin 		int cable_length_medium =
2121afd13a1fSJeff Skirvin 			is_medium_cable(phy_id, cable_selection_mask);
2122cc9203bfSDan Williams 
2123afd13a1fSJeff Skirvin 		if (is_a2(pdev)) {
21242e5da889SDan Williams 			/* All defaults, except the Receive Word
21252e5da889SDan Williams 			 * Alignament/Comma Detect Enable....(0xe800)
21262e5da889SDan Williams 			 */
21272e5da889SDan Williams 			writel(0x00004512, &xcvr->afe_xcvr_control0);
2128cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2129cc9203bfSDan Williams 
21302e5da889SDan Williams 			writel(0x0050100F, &xcvr->afe_xcvr_control1);
2131cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2132afd13a1fSJeff Skirvin 		} else if (is_b0(pdev)) {
2133afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2134afd13a1fSJeff Skirvin 			writel(0x00030000, &xcvr->afe_tx_ssc_control);
2135afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2136afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2137afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2138afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2139afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2140afd13a1fSJeff Skirvin 
2141afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2142afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2143afd13a1fSJeff Skirvin 			 */
2144afd13a1fSJeff Skirvin 			writel(0x00014500, &xcvr->afe_xcvr_control0);
2145afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2146afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2147afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2148afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2149afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2150afd13a1fSJeff Skirvin 
2151afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2152afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2153afd13a1fSJeff Skirvin 			 */
2154afd13a1fSJeff Skirvin 			writel(0x0001C500, &xcvr->afe_xcvr_control0);
2155afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2156cc9203bfSDan Williams 		}
2157cc9203bfSDan Williams 
2158afd13a1fSJeff Skirvin 		/* Power up TX and RX out from power down (PWRDNTX and
2159afd13a1fSJeff Skirvin 		 * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
21602e5da889SDan Williams 		 */
2161dc00c8b6SDan Williams 		if (is_a2(pdev))
21622e5da889SDan Williams 			writel(0x000003F0, &xcvr->afe_channel_control);
2163dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
21642e5da889SDan Williams 			writel(0x000003D7, &xcvr->afe_channel_control);
2165cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2166afd13a1fSJeff Skirvin 
21672e5da889SDan Williams 			writel(0x000003D4, &xcvr->afe_channel_control);
2168afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
21692e5da889SDan Williams 			writel(0x000001E7, &xcvr->afe_channel_control);
2170dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2171afd13a1fSJeff Skirvin 
21722e5da889SDan Williams 			writel(0x000001E4, &xcvr->afe_channel_control);
2173afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2174afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F7 : 0x000001F7,
2175afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2176afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2177afd13a1fSJeff Skirvin 
2178afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F4 : 0x000001F4,
2179afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2180cc9203bfSDan Williams 		}
2181cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2182cc9203bfSDan Williams 
2183dc00c8b6SDan Williams 		if (is_a2(pdev)) {
2184cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
21852e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2186cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2187cc9203bfSDan Williams 		}
2188cc9203bfSDan Williams 
2189afd13a1fSJeff Skirvin 		if (is_a2(pdev) || is_b0(pdev))
2190afd13a1fSJeff Skirvin 			/* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
2191afd13a1fSJeff Skirvin 			 * TPD=0x0(TX Power On), RDD=0x0(RX Detect
2192afd13a1fSJeff Skirvin 			 * Enabled) ....(0xe800)
2193afd13a1fSJeff Skirvin 			 */
21942e5da889SDan Williams 			writel(0x00004100, &xcvr->afe_xcvr_control0);
2195afd13a1fSJeff Skirvin 		else if (is_c0(pdev))
2196afd13a1fSJeff Skirvin 			writel(0x00014100, &xcvr->afe_xcvr_control0);
2197afd13a1fSJeff Skirvin 		else if (is_c1(pdev))
2198afd13a1fSJeff Skirvin 			writel(0x0001C100, &xcvr->afe_xcvr_control0);
2199cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2200cc9203bfSDan Williams 
2201cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2202dc00c8b6SDan Williams 		if (is_a2(pdev))
22032e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2204dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
22052e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2206cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2207cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
22082e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2209afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2210afd13a1fSJeff Skirvin 			writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
2211dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2212dbb0743aSAdam Gruchala 
22132e5da889SDan Williams 			writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
2214dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2215dbb0743aSAdam Gruchala 
2216dbb0743aSAdam Gruchala 			/* Enable TX equalization (0xe824) */
22172e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2218afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2219afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x01500C0C :
2220afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x01400C0D : 0x02400C0D,
2221afd13a1fSJeff Skirvin 			       &xcvr->afe_xcvr_control1);
2222afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2223afd13a1fSJeff Skirvin 
2224afd13a1fSJeff Skirvin 			writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
2225afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2226afd13a1fSJeff Skirvin 
2227afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x33091C1F :
2228afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x3315181F : 0x2B17161F,
2229afd13a1fSJeff Skirvin 			       &xcvr->afe_rx_ssc_control0);
2230afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2231afd13a1fSJeff Skirvin 
2232afd13a1fSJeff Skirvin 			/* Enable TX equalization (0xe824) */
2233afd13a1fSJeff Skirvin 			writel(0x00040000, &xcvr->afe_tx_control);
2234cc9203bfSDan Williams 		}
2235dbb0743aSAdam Gruchala 
2236cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2237cc9203bfSDan Williams 
22382e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
2239cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2240cc9203bfSDan Williams 
22412e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
2242cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2243cc9203bfSDan Williams 
22442e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
2245cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2246cc9203bfSDan Williams 
22472e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
2248cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2249cc9203bfSDan Williams 	}
2250cc9203bfSDan Williams 
2251cc9203bfSDan Williams 	/* Transfer control to the PEs */
22522e5da889SDan Williams 	writel(0x00010f00, &afe->afe_dfx_master_control0);
2253cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2254cc9203bfSDan Williams }
2255cc9203bfSDan Williams 
225689a7301fSDan Williams static void sci_controller_initialize_power_control(struct isci_host *ihost)
2257cc9203bfSDan Williams {
2258d9dcb4baSDan Williams 	sci_init_timer(&ihost->power_control.timer, power_control_timeout);
2259cc9203bfSDan Williams 
2260d9dcb4baSDan Williams 	memset(ihost->power_control.requesters, 0,
2261d9dcb4baSDan Williams 	       sizeof(ihost->power_control.requesters));
2262cc9203bfSDan Williams 
2263d9dcb4baSDan Williams 	ihost->power_control.phys_waiting = 0;
2264d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
2265cc9203bfSDan Williams }
2266cc9203bfSDan Williams 
226789a7301fSDan Williams static enum sci_status sci_controller_initialize(struct isci_host *ihost)
2268cc9203bfSDan Williams {
2269d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
22707c78da31SDan Williams 	enum sci_status result = SCI_FAILURE;
22717c78da31SDan Williams 	unsigned long i, state, val;
2272cc9203bfSDan Williams 
2273d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_RESET) {
2274d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2275cc9203bfSDan Williams 			 "SCIC Controller initialize operation requested "
2276cc9203bfSDan Williams 			 "in invalid state\n");
2277cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2278cc9203bfSDan Williams 	}
2279cc9203bfSDan Williams 
2280e301370aSEdmund Nadolski 	sci_change_state(sm, SCIC_INITIALIZING);
2281cc9203bfSDan Williams 
2282d9dcb4baSDan Williams 	sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
2283bb3dbdf6SEdmund Nadolski 
2284d9dcb4baSDan Williams 	ihost->next_phy_to_start = 0;
2285d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
2286cc9203bfSDan Williams 
228789a7301fSDan Williams 	sci_controller_initialize_power_control(ihost);
2288cc9203bfSDan Williams 
2289cc9203bfSDan Williams 	/*
2290cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2291cc9203bfSDan Williams 	 * program the AFE registers.
2292cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2293cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
229489a7301fSDan Williams 	sci_controller_afe_initialization(ihost);
2295cc9203bfSDan Williams 
2296cc9203bfSDan Williams 
2297cc9203bfSDan Williams 	/* Take the hardware out of reset */
2298d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->soft_reset_control);
2299cc9203bfSDan Williams 
2300cc9203bfSDan Williams 	/*
2301cc9203bfSDan Williams 	 * / @todo Provide meaningfull error code for hardware failure
2302cc9203bfSDan Williams 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
23037c78da31SDan Williams 	for (i = 100; i >= 1; i--) {
23047c78da31SDan Williams 		u32 status;
2305cc9203bfSDan Williams 
2306cc9203bfSDan Williams 		/* Loop until the hardware reports success */
2307cc9203bfSDan Williams 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2308d9dcb4baSDan Williams 		status = readl(&ihost->smu_registers->control_status);
2309cc9203bfSDan Williams 
23107c78da31SDan Williams 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
23117c78da31SDan Williams 			break;
2312cc9203bfSDan Williams 	}
23137c78da31SDan Williams 	if (i == 0)
23147c78da31SDan Williams 		goto out;
2315cc9203bfSDan Williams 
2316cc9203bfSDan Williams 	/*
2317cc9203bfSDan Williams 	 * Determine what are the actaul device capacities that the
2318cc9203bfSDan Williams 	 * hardware will support */
2319d9dcb4baSDan Williams 	val = readl(&ihost->smu_registers->device_context_capacity);
2320cc9203bfSDan Williams 
23217c78da31SDan Williams 	/* Record the smaller of the two capacity values */
2322d9dcb4baSDan Williams 	ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2323d9dcb4baSDan Williams 	ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2324d9dcb4baSDan Williams 	ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2325cc9203bfSDan Williams 
2326cc9203bfSDan Williams 	/*
2327cc9203bfSDan Williams 	 * Make all PEs that are unassigned match up with the
2328cc9203bfSDan Williams 	 * logical ports
2329cc9203bfSDan Williams 	 */
2330d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
2331cc9203bfSDan Williams 		struct scu_port_task_scheduler_group_registers __iomem
2332d9dcb4baSDan Williams 			*ptsg = &ihost->scu_registers->peg0.ptsg;
2333cc9203bfSDan Williams 
23347c78da31SDan Williams 		writel(i, &ptsg->protocol_engine[i]);
2335cc9203bfSDan Williams 	}
2336cc9203bfSDan Williams 
2337cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2338d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.pdma_configuration);
23397c78da31SDan Williams 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2340d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.pdma_configuration);
2341cc9203bfSDan Williams 
2342d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.cdma_configuration);
23437c78da31SDan Williams 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2344d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.cdma_configuration);
2345cc9203bfSDan Williams 
2346cc9203bfSDan Williams 	/*
2347cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2348cc9203bfSDan Williams 	 * are accessed during the port initialization.
2349cc9203bfSDan Williams 	 */
23507c78da31SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
235189a7301fSDan Williams 		result = sci_phy_initialize(&ihost->phys[i],
2352d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].tl,
2353d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].ll);
23547c78da31SDan Williams 		if (result != SCI_SUCCESS)
23557c78da31SDan Williams 			goto out;
2356cc9203bfSDan Williams 	}
2357cc9203bfSDan Williams 
2358d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
235989a7301fSDan Williams 		struct isci_port *iport = &ihost->ports[i];
23607c78da31SDan Williams 
236189a7301fSDan Williams 		iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
236289a7301fSDan Williams 		iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
236389a7301fSDan Williams 		iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
2364cc9203bfSDan Williams 	}
2365cc9203bfSDan Williams 
236689a7301fSDan Williams 	result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
2367cc9203bfSDan Williams 
23687c78da31SDan Williams  out:
2369cc9203bfSDan Williams 	/* Advance the controller state machine */
2370cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2371e301370aSEdmund Nadolski 		state = SCIC_INITIALIZED;
2372cc9203bfSDan Williams 	else
2373e301370aSEdmund Nadolski 		state = SCIC_FAILED;
2374e301370aSEdmund Nadolski 	sci_change_state(sm, state);
2375cc9203bfSDan Williams 
2376cc9203bfSDan Williams 	return result;
2377cc9203bfSDan Williams }
2378cc9203bfSDan Williams 
237989a7301fSDan Williams static enum sci_status sci_user_parameters_set(struct isci_host *ihost,
238089a7301fSDan Williams 					       struct sci_user_parameters *sci_parms)
2381cc9203bfSDan Williams {
2382d9dcb4baSDan Williams 	u32 state = ihost->sm.current_state_id;
2383cc9203bfSDan Williams 
2384e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
2385e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
2386e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
2387cc9203bfSDan Williams 		u16 index;
2388cc9203bfSDan Williams 
2389cc9203bfSDan Williams 		/*
2390cc9203bfSDan Williams 		 * Validate the user parameters.  If they are not legal, then
2391cc9203bfSDan Williams 		 * return a failure.
2392cc9203bfSDan Williams 		 */
2393cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
2394cc9203bfSDan Williams 			struct sci_phy_user_params *user_phy;
2395cc9203bfSDan Williams 
239689a7301fSDan Williams 			user_phy = &sci_parms->phys[index];
2397cc9203bfSDan Williams 
2398cc9203bfSDan Williams 			if (!((user_phy->max_speed_generation <=
2399cc9203bfSDan Williams 						SCIC_SDS_PARM_MAX_SPEED) &&
2400cc9203bfSDan Williams 			      (user_phy->max_speed_generation >
2401cc9203bfSDan Williams 						SCIC_SDS_PARM_NO_SPEED)))
2402cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2403cc9203bfSDan Williams 
2404cc9203bfSDan Williams 			if (user_phy->in_connection_align_insertion_frequency <
2405cc9203bfSDan Williams 					3)
2406cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2407cc9203bfSDan Williams 
2408cc9203bfSDan Williams 			if ((user_phy->in_connection_align_insertion_frequency <
2409cc9203bfSDan Williams 						3) ||
2410cc9203bfSDan Williams 			    (user_phy->align_insertion_frequency == 0) ||
2411cc9203bfSDan Williams 			    (user_phy->
2412cc9203bfSDan Williams 				notify_enable_spin_up_insertion_frequency ==
2413cc9203bfSDan Williams 						0))
2414cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2415cc9203bfSDan Williams 		}
2416cc9203bfSDan Williams 
241789a7301fSDan Williams 		if ((sci_parms->stp_inactivity_timeout == 0) ||
241889a7301fSDan Williams 		    (sci_parms->ssp_inactivity_timeout == 0) ||
241989a7301fSDan Williams 		    (sci_parms->stp_max_occupancy_timeout == 0) ||
242089a7301fSDan Williams 		    (sci_parms->ssp_max_occupancy_timeout == 0) ||
242189a7301fSDan Williams 		    (sci_parms->no_outbound_task_timeout == 0))
2422cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2423cc9203bfSDan Williams 
242489a7301fSDan Williams 		memcpy(&ihost->user_parameters, sci_parms, sizeof(*sci_parms));
2425cc9203bfSDan Williams 
2426cc9203bfSDan Williams 		return SCI_SUCCESS;
2427cc9203bfSDan Williams 	}
2428cc9203bfSDan Williams 
2429cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
2430cc9203bfSDan Williams }
2431cc9203bfSDan Williams 
243289a7301fSDan Williams static int sci_controller_mem_init(struct isci_host *ihost)
2433cc9203bfSDan Williams {
2434d9dcb4baSDan Williams 	struct device *dev = &ihost->pdev->dev;
24357c78da31SDan Williams 	dma_addr_t dma;
24367c78da31SDan Williams 	size_t size;
24377c78da31SDan Williams 	int err;
2438cc9203bfSDan Williams 
24397c78da31SDan Williams 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2440d9dcb4baSDan Williams 	ihost->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2441d9dcb4baSDan Williams 	if (!ihost->completion_queue)
2442cc9203bfSDan Williams 		return -ENOMEM;
2443cc9203bfSDan Williams 
2444d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->completion_queue_lower);
2445d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->completion_queue_upper);
2446cc9203bfSDan Williams 
2447d9dcb4baSDan Williams 	size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2448d9dcb4baSDan Williams 	ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
24497c78da31SDan Williams 							       GFP_KERNEL);
2450d9dcb4baSDan Williams 	if (!ihost->remote_node_context_table)
2451cc9203bfSDan Williams 		return -ENOMEM;
2452cc9203bfSDan Williams 
2453d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->remote_node_context_lower);
2454d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->remote_node_context_upper);
2455cc9203bfSDan Williams 
2456d9dcb4baSDan Williams 	size = ihost->task_context_entries * sizeof(struct scu_task_context),
2457d9dcb4baSDan Williams 	ihost->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2458d9dcb4baSDan Williams 	if (!ihost->task_context_table)
2459cc9203bfSDan Williams 		return -ENOMEM;
2460cc9203bfSDan Williams 
2461d9dcb4baSDan Williams 	ihost->task_context_dma = dma;
2462d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->host_task_table_lower);
2463d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->host_task_table_upper);
2464cc9203bfSDan Williams 
246589a7301fSDan Williams 	err = sci_unsolicited_frame_control_construct(ihost);
24667c78da31SDan Williams 	if (err)
24677c78da31SDan Williams 		return err;
2468cc9203bfSDan Williams 
2469cc9203bfSDan Williams 	/*
2470cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2471cc9203bfSDan Williams 	 * address table.
2472cc9203bfSDan Williams 	 */
2473d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2474d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_lower);
2475d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2476d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_upper);
2477cc9203bfSDan Williams 
2478d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2479d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_lower);
2480d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2481d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_upper);
2482cc9203bfSDan Williams 
2483cc9203bfSDan Williams 	return 0;
2484cc9203bfSDan Williams }
2485cc9203bfSDan Williams 
2486d9dcb4baSDan Williams int isci_host_init(struct isci_host *ihost)
24876f231ddaSDan Williams {
2488d9c37390SDan Williams 	int err = 0, i;
24896f231ddaSDan Williams 	enum sci_status status;
249089a7301fSDan Williams 	struct sci_user_parameters sci_user_params;
2491d9dcb4baSDan Williams 	struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
24926f231ddaSDan Williams 
2493d9dcb4baSDan Williams 	spin_lock_init(&ihost->state_lock);
2494d9dcb4baSDan Williams 	spin_lock_init(&ihost->scic_lock);
2495d9dcb4baSDan Williams 	init_waitqueue_head(&ihost->eventq);
24966f231ddaSDan Williams 
2497d9dcb4baSDan Williams 	isci_host_change_state(ihost, isci_starting);
24986f231ddaSDan Williams 
249989a7301fSDan Williams 	status = sci_controller_construct(ihost, scu_base(ihost),
2500d9dcb4baSDan Williams 					  smu_base(ihost));
25016f231ddaSDan Williams 
25026f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2503d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
250489a7301fSDan Williams 			"%s: sci_controller_construct failed - status = %x\n",
25056f231ddaSDan Williams 			__func__,
25066f231ddaSDan Williams 			status);
2507858d4aa7SDave Jiang 		return -ENODEV;
25086f231ddaSDan Williams 	}
25096f231ddaSDan Williams 
2510d9dcb4baSDan Williams 	ihost->sas_ha.dev = &ihost->pdev->dev;
2511d9dcb4baSDan Williams 	ihost->sas_ha.lldd_ha = ihost;
25126f231ddaSDan Williams 
2513d044af17SDan Williams 	/*
2514d044af17SDan Williams 	 * grab initial values stored in the controller object for OEM and USER
2515d044af17SDan Williams 	 * parameters
2516d044af17SDan Williams 	 */
251789a7301fSDan Williams 	isci_user_parameters_get(&sci_user_params);
251889a7301fSDan Williams 	status = sci_user_parameters_set(ihost, &sci_user_params);
2519d044af17SDan Williams 	if (status != SCI_SUCCESS) {
2520d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
252189a7301fSDan Williams 			 "%s: sci_user_parameters_set failed\n",
2522d044af17SDan Williams 			 __func__);
2523d044af17SDan Williams 		return -ENODEV;
2524d044af17SDan Williams 	}
25256f231ddaSDan Williams 
2526d044af17SDan Williams 	/* grab any OEM parameters specified in orom */
2527d044af17SDan Williams 	if (pci_info->orom) {
252889a7301fSDan Williams 		status = isci_parse_oem_parameters(&ihost->oem_parameters,
2529d044af17SDan Williams 						   pci_info->orom,
2530d9dcb4baSDan Williams 						   ihost->id);
25316f231ddaSDan Williams 		if (status != SCI_SUCCESS) {
2532d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
25336f231ddaSDan Williams 				 "parsing firmware oem parameters failed\n");
2534858d4aa7SDave Jiang 			return -EINVAL;
25356f231ddaSDan Williams 		}
25364711ba10SDan Williams 	}
25374711ba10SDan Williams 
253889a7301fSDan Williams 	status = sci_oem_parameters_set(ihost);
25396f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2540d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
254189a7301fSDan Williams 				"%s: sci_oem_parameters_set failed\n",
25426f231ddaSDan Williams 				__func__);
2543858d4aa7SDave Jiang 		return -ENODEV;
25446f231ddaSDan Williams 	}
25456f231ddaSDan Williams 
2546d9dcb4baSDan Williams 	tasklet_init(&ihost->completion_tasklet,
2547d9dcb4baSDan Williams 		     isci_host_completion_routine, (unsigned long)ihost);
25486f231ddaSDan Williams 
2549d9dcb4baSDan Williams 	INIT_LIST_HEAD(&ihost->requests_to_complete);
2550d9dcb4baSDan Williams 	INIT_LIST_HEAD(&ihost->requests_to_errorback);
25516f231ddaSDan Williams 
2552d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
255389a7301fSDan Williams 	status = sci_controller_initialize(ihost);
2554d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
25557c40a803SDan Williams 	if (status != SCI_SUCCESS) {
2556d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
255789a7301fSDan Williams 			 "%s: sci_controller_initialize failed -"
25587c40a803SDan Williams 			 " status = 0x%x\n",
25597c40a803SDan Williams 			 __func__, status);
25607c40a803SDan Williams 		return -ENODEV;
25617c40a803SDan Williams 	}
25627c40a803SDan Williams 
256389a7301fSDan Williams 	err = sci_controller_mem_init(ihost);
25646f231ddaSDan Williams 	if (err)
2565858d4aa7SDave Jiang 		return err;
25666f231ddaSDan Williams 
2567d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
2568d9dcb4baSDan Williams 		isci_port_init(&ihost->ports[i], ihost, i);
25696f231ddaSDan Williams 
2570d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
2571d9dcb4baSDan Williams 		isci_phy_init(&ihost->phys[i], ihost, i);
2572d9c37390SDan Williams 
2573ad4f4c1dSDan Williams 	/* enable sgpio */
2574ad4f4c1dSDan Williams 	writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
2575ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
2576ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
2577ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
2578ad4f4c1dSDan Williams 
2579d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
2580d9dcb4baSDan Williams 		struct isci_remote_device *idev = &ihost->devices[i];
2581d9c37390SDan Williams 
2582d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->reqs_in_process);
2583d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->node);
2584d9c37390SDan Williams 	}
25856f231ddaSDan Williams 
2586db056250SDan Williams 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2587db056250SDan Williams 		struct isci_request *ireq;
2588db056250SDan Williams 		dma_addr_t dma;
2589db056250SDan Williams 
2590d9dcb4baSDan Williams 		ireq = dmam_alloc_coherent(&ihost->pdev->dev,
2591db056250SDan Williams 					   sizeof(struct isci_request), &dma,
2592db056250SDan Williams 					   GFP_KERNEL);
2593db056250SDan Williams 		if (!ireq)
2594db056250SDan Williams 			return -ENOMEM;
2595db056250SDan Williams 
2596d9dcb4baSDan Williams 		ireq->tc = &ihost->task_context_table[i];
2597d9dcb4baSDan Williams 		ireq->owning_controller = ihost;
2598db056250SDan Williams 		spin_lock_init(&ireq->state_lock);
2599db056250SDan Williams 		ireq->request_daddr = dma;
2600d9dcb4baSDan Williams 		ireq->isci_host = ihost;
2601d9dcb4baSDan Williams 		ihost->reqs[i] = ireq;
2602db056250SDan Williams 	}
2603db056250SDan Williams 
2604858d4aa7SDave Jiang 	return 0;
26056f231ddaSDan Williams }
2606cc9203bfSDan Williams 
260789a7301fSDan Williams void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
260889a7301fSDan Williams 			    struct isci_phy *iphy)
2609cc9203bfSDan Williams {
2610d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2611e301370aSEdmund Nadolski 	case SCIC_STARTING:
2612d9dcb4baSDan Williams 		sci_del_timer(&ihost->phy_timer);
2613d9dcb4baSDan Williams 		ihost->phy_startup_timer_pending = false;
2614d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2615ffe191c9SDan Williams 						  iport, iphy);
261689a7301fSDan Williams 		sci_controller_start_next_phy(ihost);
2617cc9203bfSDan Williams 		break;
2618e301370aSEdmund Nadolski 	case SCIC_READY:
2619d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2620ffe191c9SDan Williams 						  iport, iphy);
2621cc9203bfSDan Williams 		break;
2622cc9203bfSDan Williams 	default:
2623d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2624cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
262585280955SDan Williams 			"unexpected state %d\n", __func__, iphy->phy_index,
2626d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2627cc9203bfSDan Williams 	}
2628cc9203bfSDan Williams }
2629cc9203bfSDan Williams 
263089a7301fSDan Williams void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
263189a7301fSDan Williams 			      struct isci_phy *iphy)
2632cc9203bfSDan Williams {
2633d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2634e301370aSEdmund Nadolski 	case SCIC_STARTING:
2635e301370aSEdmund Nadolski 	case SCIC_READY:
2636d9dcb4baSDan Williams 		ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
2637ffe191c9SDan Williams 						   iport, iphy);
2638cc9203bfSDan Williams 		break;
2639cc9203bfSDan Williams 	default:
2640d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2641cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2642cc9203bfSDan Williams 			"unexpected state %d\n",
2643cc9203bfSDan Williams 			__func__,
264485280955SDan Williams 			iphy->phy_index,
2645d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2646cc9203bfSDan Williams 	}
2647cc9203bfSDan Williams }
2648cc9203bfSDan Williams 
264989a7301fSDan Williams static bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
2650cc9203bfSDan Williams {
2651cc9203bfSDan Williams 	u32 index;
2652cc9203bfSDan Williams 
2653d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
2654d9dcb4baSDan Williams 		if ((ihost->device_table[index] != NULL) &&
2655d9dcb4baSDan Williams 		   (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2656cc9203bfSDan Williams 			return true;
2657cc9203bfSDan Williams 	}
2658cc9203bfSDan Williams 
2659cc9203bfSDan Williams 	return false;
2660cc9203bfSDan Williams }
2661cc9203bfSDan Williams 
266289a7301fSDan Williams void sci_controller_remote_device_stopped(struct isci_host *ihost,
266378a6f06eSDan Williams 					  struct isci_remote_device *idev)
2664cc9203bfSDan Williams {
2665d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_STOPPING) {
2666d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2667cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2668cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2669d9dcb4baSDan Williams 			ihost, idev,
2670d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2671cc9203bfSDan Williams 		return;
2672cc9203bfSDan Williams 	}
2673cc9203bfSDan Williams 
267489a7301fSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
2675d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_STOPPED);
2676cc9203bfSDan Williams }
2677cc9203bfSDan Williams 
267889a7301fSDan Williams void sci_controller_post_request(struct isci_host *ihost, u32 request)
2679cc9203bfSDan Williams {
268089a7301fSDan Williams 	dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
268189a7301fSDan Williams 		__func__, ihost->id, request);
2682cc9203bfSDan Williams 
2683d9dcb4baSDan Williams 	writel(request, &ihost->smu_registers->post_context_port);
2684cc9203bfSDan Williams }
2685cc9203bfSDan Williams 
268689a7301fSDan Williams struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
2687cc9203bfSDan Williams {
2688cc9203bfSDan Williams 	u16 task_index;
2689cc9203bfSDan Williams 	u16 task_sequence;
2690cc9203bfSDan Williams 
2691dd047c8eSDan Williams 	task_index = ISCI_TAG_TCI(io_tag);
2692cc9203bfSDan Williams 
2693d9dcb4baSDan Williams 	if (task_index < ihost->task_context_entries) {
2694d9dcb4baSDan Williams 		struct isci_request *ireq = ihost->reqs[task_index];
2695db056250SDan Williams 
2696db056250SDan Williams 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2697dd047c8eSDan Williams 			task_sequence = ISCI_TAG_SEQ(io_tag);
2698cc9203bfSDan Williams 
2699d9dcb4baSDan Williams 			if (task_sequence == ihost->io_request_sequence[task_index])
27005076a1a9SDan Williams 				return ireq;
2701cc9203bfSDan Williams 		}
2702cc9203bfSDan Williams 	}
2703cc9203bfSDan Williams 
2704cc9203bfSDan Williams 	return NULL;
2705cc9203bfSDan Williams }
2706cc9203bfSDan Williams 
2707cc9203bfSDan Williams /**
2708cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2709cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2710cc9203bfSDan Williams  *    node index available.
2711cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2712cc9203bfSDan Williams  *    free remote node ids
2713cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2714cc9203bfSDan Williams  *    id
2715cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2716cc9203bfSDan Williams  *    is available
2717cc9203bfSDan Williams  *
2718cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2719cc9203bfSDan Williams  * node index available.
2720cc9203bfSDan Williams  */
272189a7301fSDan Williams enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
272278a6f06eSDan Williams 							    struct isci_remote_device *idev,
2723cc9203bfSDan Williams 							    u16 *node_id)
2724cc9203bfSDan Williams {
2725cc9203bfSDan Williams 	u16 node_index;
272689a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2727cc9203bfSDan Williams 
272889a7301fSDan Williams 	node_index = sci_remote_node_table_allocate_remote_node(
2729d9dcb4baSDan Williams 		&ihost->available_remote_nodes, remote_node_count
2730cc9203bfSDan Williams 		);
2731cc9203bfSDan Williams 
2732cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2733d9dcb4baSDan Williams 		ihost->device_table[node_index] = idev;
2734cc9203bfSDan Williams 
2735cc9203bfSDan Williams 		*node_id = node_index;
2736cc9203bfSDan Williams 
2737cc9203bfSDan Williams 		return SCI_SUCCESS;
2738cc9203bfSDan Williams 	}
2739cc9203bfSDan Williams 
2740cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2741cc9203bfSDan Williams }
2742cc9203bfSDan Williams 
274389a7301fSDan Williams void sci_controller_free_remote_node_context(struct isci_host *ihost,
274478a6f06eSDan Williams 					     struct isci_remote_device *idev,
2745cc9203bfSDan Williams 					     u16 node_id)
2746cc9203bfSDan Williams {
274789a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2748cc9203bfSDan Williams 
2749d9dcb4baSDan Williams 	if (ihost->device_table[node_id] == idev) {
2750d9dcb4baSDan Williams 		ihost->device_table[node_id] = NULL;
2751cc9203bfSDan Williams 
275289a7301fSDan Williams 		sci_remote_node_table_release_remote_node_index(
2753d9dcb4baSDan Williams 			&ihost->available_remote_nodes, remote_node_count, node_id
2754cc9203bfSDan Williams 			);
2755cc9203bfSDan Williams 	}
2756cc9203bfSDan Williams }
2757cc9203bfSDan Williams 
275889a7301fSDan Williams void sci_controller_copy_sata_response(void *response_buffer,
2759cc9203bfSDan Williams 				       void *frame_header,
2760cc9203bfSDan Williams 				       void *frame_buffer)
2761cc9203bfSDan Williams {
276289a7301fSDan Williams 	/* XXX type safety? */
2763cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2764cc9203bfSDan Williams 
2765cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2766cc9203bfSDan Williams 	       frame_buffer,
2767cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2768cc9203bfSDan Williams }
2769cc9203bfSDan Williams 
277089a7301fSDan Williams void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
2771cc9203bfSDan Williams {
277289a7301fSDan Williams 	if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
2773d9dcb4baSDan Williams 		writel(ihost->uf_control.get,
2774d9dcb4baSDan Williams 			&ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
2775cc9203bfSDan Williams }
2776cc9203bfSDan Williams 
2777312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci)
2778312e0c24SDan Williams {
2779312e0c24SDan Williams 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2780312e0c24SDan Williams 
2781312e0c24SDan Williams 	ihost->tci_pool[tail] = tci;
2782312e0c24SDan Williams 	ihost->tci_tail = tail + 1;
2783312e0c24SDan Williams }
2784312e0c24SDan Williams 
2785312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost)
2786312e0c24SDan Williams {
2787312e0c24SDan Williams 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2788312e0c24SDan Williams 	u16 tci = ihost->tci_pool[head];
2789312e0c24SDan Williams 
2790312e0c24SDan Williams 	ihost->tci_head = head + 1;
2791312e0c24SDan Williams 	return tci;
2792312e0c24SDan Williams }
2793312e0c24SDan Williams 
2794312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost)
2795312e0c24SDan Williams {
2796312e0c24SDan Williams 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2797312e0c24SDan Williams }
2798312e0c24SDan Williams 
2799312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost)
2800312e0c24SDan Williams {
2801312e0c24SDan Williams 	if (isci_tci_space(ihost)) {
2802312e0c24SDan Williams 		u16 tci = isci_tci_alloc(ihost);
2803d9dcb4baSDan Williams 		u8 seq = ihost->io_request_sequence[tci];
2804312e0c24SDan Williams 
2805312e0c24SDan Williams 		return ISCI_TAG(seq, tci);
2806312e0c24SDan Williams 	}
2807312e0c24SDan Williams 
2808312e0c24SDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
2809312e0c24SDan Williams }
2810312e0c24SDan Williams 
2811312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2812312e0c24SDan Williams {
2813312e0c24SDan Williams 	u16 tci = ISCI_TAG_TCI(io_tag);
2814312e0c24SDan Williams 	u16 seq = ISCI_TAG_SEQ(io_tag);
2815312e0c24SDan Williams 
2816312e0c24SDan Williams 	/* prevent tail from passing head */
2817312e0c24SDan Williams 	if (isci_tci_active(ihost) == 0)
2818312e0c24SDan Williams 		return SCI_FAILURE_INVALID_IO_TAG;
2819312e0c24SDan Williams 
2820d9dcb4baSDan Williams 	if (seq == ihost->io_request_sequence[tci]) {
2821d9dcb4baSDan Williams 		ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2822312e0c24SDan Williams 
2823312e0c24SDan Williams 		isci_tci_free(ihost, tci);
2824312e0c24SDan Williams 
2825312e0c24SDan Williams 		return SCI_SUCCESS;
2826312e0c24SDan Williams 	}
2827312e0c24SDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
2828312e0c24SDan Williams }
2829312e0c24SDan Williams 
283089a7301fSDan Williams enum sci_status sci_controller_start_io(struct isci_host *ihost,
283178a6f06eSDan Williams 					struct isci_remote_device *idev,
28325076a1a9SDan Williams 					struct isci_request *ireq)
2833cc9203bfSDan Williams {
2834cc9203bfSDan Williams 	enum sci_status status;
2835cc9203bfSDan Williams 
2836d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2837d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "invalid state to start I/O");
2838cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2839cc9203bfSDan Williams 	}
2840cc9203bfSDan Williams 
284189a7301fSDan Williams 	status = sci_remote_device_start_io(ihost, idev, ireq);
2842cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2843cc9203bfSDan Williams 		return status;
2844cc9203bfSDan Williams 
28455076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
284634a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2847cc9203bfSDan Williams 	return SCI_SUCCESS;
2848cc9203bfSDan Williams }
2849cc9203bfSDan Williams 
285089a7301fSDan Williams enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
285178a6f06eSDan Williams 						 struct isci_remote_device *idev,
28525076a1a9SDan Williams 						 struct isci_request *ireq)
2853cc9203bfSDan Williams {
285489a7301fSDan Williams 	/* terminate an ongoing (i.e. started) core IO request.  This does not
285589a7301fSDan Williams 	 * abort the IO request at the target, but rather removes the IO
285689a7301fSDan Williams 	 * request from the host controller.
285789a7301fSDan Williams 	 */
2858cc9203bfSDan Williams 	enum sci_status status;
2859cc9203bfSDan Williams 
2860d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2861d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2862cc9203bfSDan Williams 			 "invalid state to terminate request\n");
2863cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2864cc9203bfSDan Williams 	}
2865cc9203bfSDan Williams 
286689a7301fSDan Williams 	status = sci_io_request_terminate(ireq);
2867cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2868cc9203bfSDan Williams 		return status;
2869cc9203bfSDan Williams 
2870cc9203bfSDan Williams 	/*
2871cc9203bfSDan Williams 	 * Utilize the original post context command and or in the POST_TC_ABORT
2872cc9203bfSDan Williams 	 * request sub-type.
2873cc9203bfSDan Williams 	 */
287489a7301fSDan Williams 	sci_controller_post_request(ihost,
287589a7301fSDan Williams 				    ireq->post_context | SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2876cc9203bfSDan Williams 	return SCI_SUCCESS;
2877cc9203bfSDan Williams }
2878cc9203bfSDan Williams 
2879cc9203bfSDan Williams /**
288089a7301fSDan Williams  * sci_controller_complete_io() - This method will perform core specific
2881cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
2882cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
2883cc9203bfSDan Williams  *    reused (i.e. re-constructed).
288489a7301fSDan Williams  * @ihost: The handle to the controller object for which to complete the
2885cc9203bfSDan Williams  *    IO request.
288689a7301fSDan Williams  * @idev: The handle to the remote device object for which to complete
2887cc9203bfSDan Williams  *    the IO request.
288889a7301fSDan Williams  * @ireq: the handle to the io request object to complete.
2889cc9203bfSDan Williams  */
289089a7301fSDan Williams enum sci_status sci_controller_complete_io(struct isci_host *ihost,
289178a6f06eSDan Williams 					   struct isci_remote_device *idev,
28925076a1a9SDan Williams 					   struct isci_request *ireq)
2893cc9203bfSDan Williams {
2894cc9203bfSDan Williams 	enum sci_status status;
2895cc9203bfSDan Williams 	u16 index;
2896cc9203bfSDan Williams 
2897d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2898e301370aSEdmund Nadolski 	case SCIC_STOPPING:
2899cc9203bfSDan Williams 		/* XXX: Implement this function */
2900cc9203bfSDan Williams 		return SCI_FAILURE;
2901e301370aSEdmund Nadolski 	case SCIC_READY:
290289a7301fSDan Williams 		status = sci_remote_device_complete_io(ihost, idev, ireq);
2903cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
2904cc9203bfSDan Williams 			return status;
2905cc9203bfSDan Williams 
29065076a1a9SDan Williams 		index = ISCI_TAG_TCI(ireq->io_tag);
29075076a1a9SDan Williams 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2908cc9203bfSDan Williams 		return SCI_SUCCESS;
2909cc9203bfSDan Williams 	default:
2910d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "invalid state to complete I/O");
2911cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2912cc9203bfSDan Williams 	}
2913cc9203bfSDan Williams 
2914cc9203bfSDan Williams }
2915cc9203bfSDan Williams 
291689a7301fSDan Williams enum sci_status sci_controller_continue_io(struct isci_request *ireq)
2917cc9203bfSDan Williams {
2918d9dcb4baSDan Williams 	struct isci_host *ihost = ireq->owning_controller;
2919cc9203bfSDan Williams 
2920d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2921d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "invalid state to continue I/O");
2922cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2923cc9203bfSDan Williams 	}
2924cc9203bfSDan Williams 
29255076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
292634a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2927cc9203bfSDan Williams 	return SCI_SUCCESS;
2928cc9203bfSDan Williams }
2929cc9203bfSDan Williams 
2930cc9203bfSDan Williams /**
293189a7301fSDan Williams  * sci_controller_start_task() - This method is called by the SCIC user to
2932cc9203bfSDan Williams  *    send/start a framework task management request.
2933cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
2934cc9203bfSDan Williams  *    management request.
2935cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
2936cc9203bfSDan Williams  *    the task management request.
2937cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
2938cc9203bfSDan Williams  */
293989a7301fSDan Williams enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
294078a6f06eSDan Williams 					       struct isci_remote_device *idev,
29415076a1a9SDan Williams 					       struct isci_request *ireq)
2942cc9203bfSDan Williams {
2943cc9203bfSDan Williams 	enum sci_status status;
2944cc9203bfSDan Williams 
2945d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2946d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2947cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
2948cc9203bfSDan Williams 			 "state\n",
2949cc9203bfSDan Williams 			 __func__);
2950cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
2951cc9203bfSDan Williams 	}
2952cc9203bfSDan Williams 
295389a7301fSDan Williams 	status = sci_remote_device_start_task(ihost, idev, ireq);
2954cc9203bfSDan Williams 	switch (status) {
2955cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2956db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2957cc9203bfSDan Williams 
2958cc9203bfSDan Williams 		/*
2959cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
2960cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
2961cc9203bfSDan Williams 		 * RNC is resumed.)
2962cc9203bfSDan Williams 		 */
2963cc9203bfSDan Williams 		return SCI_SUCCESS;
2964cc9203bfSDan Williams 	case SCI_SUCCESS:
2965db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
296634a99158SDan Williams 		sci_controller_post_request(ihost, ireq->post_context);
2967cc9203bfSDan Williams 		break;
2968cc9203bfSDan Williams 	default:
2969cc9203bfSDan Williams 		break;
2970cc9203bfSDan Williams 	}
2971cc9203bfSDan Williams 
2972cc9203bfSDan Williams 	return status;
2973cc9203bfSDan Williams }
2974ad4f4c1dSDan Williams 
2975ad4f4c1dSDan Williams static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
2976ad4f4c1dSDan Williams {
2977ad4f4c1dSDan Williams 	int d;
2978ad4f4c1dSDan Williams 
2979ad4f4c1dSDan Williams 	/* no support for TX_GP_CFG */
2980ad4f4c1dSDan Williams 	if (reg_index == 0)
2981ad4f4c1dSDan Williams 		return -EINVAL;
2982ad4f4c1dSDan Williams 
2983ad4f4c1dSDan Williams 	for (d = 0; d < isci_gpio_count(ihost); d++) {
2984ad4f4c1dSDan Williams 		u32 val = 0x444; /* all ODx.n clear */
2985ad4f4c1dSDan Williams 		int i;
2986ad4f4c1dSDan Williams 
2987ad4f4c1dSDan Williams 		for (i = 0; i < 3; i++) {
2988ad4f4c1dSDan Williams 			int bit = (i << 2) + 2;
2989ad4f4c1dSDan Williams 
2990ad4f4c1dSDan Williams 			bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
2991ad4f4c1dSDan Williams 						       write_data, reg_index,
2992ad4f4c1dSDan Williams 						       reg_count);
2993ad4f4c1dSDan Williams 			if (bit < 0)
2994ad4f4c1dSDan Williams 				break;
2995ad4f4c1dSDan Williams 
2996ad4f4c1dSDan Williams 			/* if od is set, clear the 'invert' bit */
2997ad4f4c1dSDan Williams 			val &= ~(bit << ((i << 2) + 2));
2998ad4f4c1dSDan Williams 		}
2999ad4f4c1dSDan Williams 
3000ad4f4c1dSDan Williams 		if (i < 3)
3001ad4f4c1dSDan Williams 			break;
3002ad4f4c1dSDan Williams 		writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
3003ad4f4c1dSDan Williams 	}
3004ad4f4c1dSDan Williams 
3005ad4f4c1dSDan Williams 	/* unless reg_index is > 1, we should always be able to write at
3006ad4f4c1dSDan Williams 	 * least one register
3007ad4f4c1dSDan Williams 	 */
3008ad4f4c1dSDan Williams 	return d > 0;
3009ad4f4c1dSDan Williams }
3010ad4f4c1dSDan Williams 
3011ad4f4c1dSDan Williams int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
3012ad4f4c1dSDan Williams 		    u8 reg_count, u8 *write_data)
3013ad4f4c1dSDan Williams {
3014ad4f4c1dSDan Williams 	struct isci_host *ihost = sas_ha->lldd_ha;
3015ad4f4c1dSDan Williams 	int written;
3016ad4f4c1dSDan Williams 
3017ad4f4c1dSDan Williams 	switch (reg_type) {
3018ad4f4c1dSDan Williams 	case SAS_GPIO_REG_TX_GP:
3019ad4f4c1dSDan Williams 		written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
3020ad4f4c1dSDan Williams 		break;
3021ad4f4c1dSDan Williams 	default:
3022ad4f4c1dSDan Williams 		written = -EINVAL;
3023ad4f4c1dSDan Williams 	}
3024ad4f4c1dSDan Williams 
3025ad4f4c1dSDan Williams 	return written;
3026ad4f4c1dSDan Williams }
3027