xref: /openbmc/linux/drivers/scsi/isci/host.c (revision c79dd80d73017a88a2c2ae46e7d5303cba6a32e0)
16f231ddaSDan Williams /*
26f231ddaSDan Williams  * This file is provided under a dual BSD/GPLv2 license.  When using or
36f231ddaSDan Williams  * redistributing this file, you may do so under either license.
46f231ddaSDan Williams  *
56f231ddaSDan Williams  * GPL LICENSE SUMMARY
66f231ddaSDan Williams  *
76f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
86f231ddaSDan Williams  *
96f231ddaSDan Williams  * This program is free software; you can redistribute it and/or modify
106f231ddaSDan Williams  * it under the terms of version 2 of the GNU General Public License as
116f231ddaSDan Williams  * published by the Free Software Foundation.
126f231ddaSDan Williams  *
136f231ddaSDan Williams  * This program is distributed in the hope that it will be useful, but
146f231ddaSDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
156f231ddaSDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
166f231ddaSDan Williams  * General Public License for more details.
176f231ddaSDan Williams  *
186f231ddaSDan Williams  * You should have received a copy of the GNU General Public License
196f231ddaSDan Williams  * along with this program; if not, write to the Free Software
206f231ddaSDan Williams  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
216f231ddaSDan Williams  * The full GNU General Public License is included in this distribution
226f231ddaSDan Williams  * in the file called LICENSE.GPL.
236f231ddaSDan Williams  *
246f231ddaSDan Williams  * BSD LICENSE
256f231ddaSDan Williams  *
266f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
276f231ddaSDan Williams  * All rights reserved.
286f231ddaSDan Williams  *
296f231ddaSDan Williams  * Redistribution and use in source and binary forms, with or without
306f231ddaSDan Williams  * modification, are permitted provided that the following conditions
316f231ddaSDan Williams  * are met:
326f231ddaSDan Williams  *
336f231ddaSDan Williams  *   * Redistributions of source code must retain the above copyright
346f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer.
356f231ddaSDan Williams  *   * Redistributions in binary form must reproduce the above copyright
366f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer in
376f231ddaSDan Williams  *     the documentation and/or other materials provided with the
386f231ddaSDan Williams  *     distribution.
396f231ddaSDan Williams  *   * Neither the name of Intel Corporation nor the names of its
406f231ddaSDan Williams  *     contributors may be used to endorse or promote products derived
416f231ddaSDan Williams  *     from this software without specific prior written permission.
426f231ddaSDan Williams  *
436f231ddaSDan Williams  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
446f231ddaSDan Williams  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
456f231ddaSDan Williams  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
466f231ddaSDan Williams  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
476f231ddaSDan Williams  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
486f231ddaSDan Williams  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
496f231ddaSDan Williams  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
506f231ddaSDan Williams  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
516f231ddaSDan Williams  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
526f231ddaSDan Williams  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
536f231ddaSDan Williams  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
546f231ddaSDan Williams  */
55ac668c69SDan Williams #include <linux/circ_buf.h>
56cc9203bfSDan Williams #include <linux/device.h>
57cc9203bfSDan Williams #include <scsi/sas.h>
58cc9203bfSDan Williams #include "host.h"
596f231ddaSDan Williams #include "isci.h"
606f231ddaSDan Williams #include "port.h"
61d044af17SDan Williams #include "probe_roms.h"
62cc9203bfSDan Williams #include "remote_device.h"
63cc9203bfSDan Williams #include "request.h"
64cc9203bfSDan Williams #include "scu_completion_codes.h"
65cc9203bfSDan Williams #include "scu_event_codes.h"
6663a3a15fSDan Williams #include "registers.h"
67cc9203bfSDan Williams #include "scu_remote_node_context.h"
68cc9203bfSDan Williams #include "scu_task_context.h"
696f231ddaSDan Williams 
70cc9203bfSDan Williams #define SCU_CONTEXT_RAM_INIT_STALL_TIME      200
71cc9203bfSDan Williams 
727c78da31SDan Williams #define smu_max_ports(dcc_value) \
73cc9203bfSDan Williams 	(\
74cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
75cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
76cc9203bfSDan Williams 	)
77cc9203bfSDan Williams 
787c78da31SDan Williams #define smu_max_task_contexts(dcc_value)	\
79cc9203bfSDan Williams 	(\
80cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
81cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
82cc9203bfSDan Williams 	)
83cc9203bfSDan Williams 
847c78da31SDan Williams #define smu_max_rncs(dcc_value) \
85cc9203bfSDan Williams 	(\
86cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
87cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
88cc9203bfSDan Williams 	)
89cc9203bfSDan Williams 
90cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT      100
91cc9203bfSDan Williams 
92cc9203bfSDan Williams /**
93cc9203bfSDan Williams  *
94cc9203bfSDan Williams  *
95cc9203bfSDan Williams  * The number of milliseconds to wait while a given phy is consuming power
96cc9203bfSDan Williams  * before allowing another set of phys to consume power. Ultimately, this will
97cc9203bfSDan Williams  * be specified by OEM parameter.
98cc9203bfSDan Williams  */
99cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
100cc9203bfSDan Williams 
101cc9203bfSDan Williams /**
102cc9203bfSDan Williams  * NORMALIZE_PUT_POINTER() -
103cc9203bfSDan Williams  *
104cc9203bfSDan Williams  * This macro will normalize the completion queue put pointer so its value can
105cc9203bfSDan Williams  * be used as an array inde
106cc9203bfSDan Williams  */
107cc9203bfSDan Williams #define NORMALIZE_PUT_POINTER(x) \
108cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
109cc9203bfSDan Williams 
110cc9203bfSDan Williams 
111cc9203bfSDan Williams /**
112cc9203bfSDan Williams  * NORMALIZE_EVENT_POINTER() -
113cc9203bfSDan Williams  *
114cc9203bfSDan Williams  * This macro will normalize the completion queue event entry so its value can
115cc9203bfSDan Williams  * be used as an index.
116cc9203bfSDan Williams  */
117cc9203bfSDan Williams #define NORMALIZE_EVENT_POINTER(x) \
118cc9203bfSDan Williams 	(\
119cc9203bfSDan Williams 		((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
120cc9203bfSDan Williams 		>> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT	\
121cc9203bfSDan Williams 	)
122cc9203bfSDan Williams 
123cc9203bfSDan Williams /**
124cc9203bfSDan Williams  * NORMALIZE_GET_POINTER() -
125cc9203bfSDan Williams  *
126cc9203bfSDan Williams  * This macro will normalize the completion queue get pointer so its value can
127cc9203bfSDan Williams  * be used as an index into an array
128cc9203bfSDan Williams  */
129cc9203bfSDan Williams #define NORMALIZE_GET_POINTER(x) \
130cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
131cc9203bfSDan Williams 
132cc9203bfSDan Williams /**
133cc9203bfSDan Williams  * NORMALIZE_GET_POINTER_CYCLE_BIT() -
134cc9203bfSDan Williams  *
135cc9203bfSDan Williams  * This macro will normalize the completion queue cycle pointer so it matches
136cc9203bfSDan Williams  * the completion queue cycle bit
137cc9203bfSDan Williams  */
138cc9203bfSDan Williams #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
139cc9203bfSDan Williams 	((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
140cc9203bfSDan Williams 
141cc9203bfSDan Williams /**
142cc9203bfSDan Williams  * COMPLETION_QUEUE_CYCLE_BIT() -
143cc9203bfSDan Williams  *
144cc9203bfSDan Williams  * This macro will return the cycle bit of the completion queue entry
145cc9203bfSDan Williams  */
146cc9203bfSDan Williams #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
147cc9203bfSDan Williams 
14812ef6544SEdmund Nadolski /* Init the state machine and call the state entry function (if any) */
14912ef6544SEdmund Nadolski void sci_init_sm(struct sci_base_state_machine *sm,
15012ef6544SEdmund Nadolski 		 const struct sci_base_state *state_table, u32 initial_state)
15112ef6544SEdmund Nadolski {
15212ef6544SEdmund Nadolski 	sci_state_transition_t handler;
15312ef6544SEdmund Nadolski 
15412ef6544SEdmund Nadolski 	sm->initial_state_id    = initial_state;
15512ef6544SEdmund Nadolski 	sm->previous_state_id   = initial_state;
15612ef6544SEdmund Nadolski 	sm->current_state_id    = initial_state;
15712ef6544SEdmund Nadolski 	sm->state_table         = state_table;
15812ef6544SEdmund Nadolski 
15912ef6544SEdmund Nadolski 	handler = sm->state_table[initial_state].enter_state;
16012ef6544SEdmund Nadolski 	if (handler)
16112ef6544SEdmund Nadolski 		handler(sm);
16212ef6544SEdmund Nadolski }
16312ef6544SEdmund Nadolski 
16412ef6544SEdmund Nadolski /* Call the state exit fn, update the current state, call the state entry fn */
16512ef6544SEdmund Nadolski void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
16612ef6544SEdmund Nadolski {
16712ef6544SEdmund Nadolski 	sci_state_transition_t handler;
16812ef6544SEdmund Nadolski 
16912ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].exit_state;
17012ef6544SEdmund Nadolski 	if (handler)
17112ef6544SEdmund Nadolski 		handler(sm);
17212ef6544SEdmund Nadolski 
17312ef6544SEdmund Nadolski 	sm->previous_state_id = sm->current_state_id;
17412ef6544SEdmund Nadolski 	sm->current_state_id = next_state;
17512ef6544SEdmund Nadolski 
17612ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].enter_state;
17712ef6544SEdmund Nadolski 	if (handler)
17812ef6544SEdmund Nadolski 		handler(sm);
17912ef6544SEdmund Nadolski }
18012ef6544SEdmund Nadolski 
18189a7301fSDan Williams static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
182cc9203bfSDan Williams {
183d9dcb4baSDan Williams 	u32 get_value = ihost->completion_queue_get;
184cc9203bfSDan Williams 	u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
185cc9203bfSDan Williams 
186cc9203bfSDan Williams 	if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
187d9dcb4baSDan Williams 	    COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
188cc9203bfSDan Williams 		return true;
189cc9203bfSDan Williams 
190cc9203bfSDan Williams 	return false;
191cc9203bfSDan Williams }
192cc9203bfSDan Williams 
19389a7301fSDan Williams static bool sci_controller_isr(struct isci_host *ihost)
194cc9203bfSDan Williams {
19589a7301fSDan Williams 	if (sci_controller_completion_queue_has_entries(ihost)) {
196cc9203bfSDan Williams 		return true;
197cc9203bfSDan Williams 	} else {
198cc9203bfSDan Williams 		/*
199cc9203bfSDan Williams 		 * we have a spurious interrupt it could be that we have already
200cc9203bfSDan Williams 		 * emptied the completion queue from a previous interrupt */
201d9dcb4baSDan Williams 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
202cc9203bfSDan Williams 
203cc9203bfSDan Williams 		/*
204cc9203bfSDan Williams 		 * There is a race in the hardware that could cause us not to be notified
205cc9203bfSDan Williams 		 * of an interrupt completion if we do not take this step.  We will mask
206cc9203bfSDan Williams 		 * then unmask the interrupts so if there is another interrupt pending
207cc9203bfSDan Williams 		 * the clearing of the interrupt source we get the next interrupt message. */
208d9dcb4baSDan Williams 		writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
209d9dcb4baSDan Williams 		writel(0, &ihost->smu_registers->interrupt_mask);
210cc9203bfSDan Williams 	}
211cc9203bfSDan Williams 
212cc9203bfSDan Williams 	return false;
213cc9203bfSDan Williams }
214cc9203bfSDan Williams 
215c7ef4031SDan Williams irqreturn_t isci_msix_isr(int vec, void *data)
2166f231ddaSDan Williams {
217c7ef4031SDan Williams 	struct isci_host *ihost = data;
2186f231ddaSDan Williams 
21989a7301fSDan Williams 	if (sci_controller_isr(ihost))
220c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
2216f231ddaSDan Williams 
222c7ef4031SDan Williams 	return IRQ_HANDLED;
223c7ef4031SDan Williams }
224c7ef4031SDan Williams 
22589a7301fSDan Williams static bool sci_controller_error_isr(struct isci_host *ihost)
226cc9203bfSDan Williams {
227cc9203bfSDan Williams 	u32 interrupt_status;
228cc9203bfSDan Williams 
229cc9203bfSDan Williams 	interrupt_status =
230d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
231cc9203bfSDan Williams 	interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
232cc9203bfSDan Williams 
233cc9203bfSDan Williams 	if (interrupt_status != 0) {
234cc9203bfSDan Williams 		/*
235cc9203bfSDan Williams 		 * There is an error interrupt pending so let it through and handle
236cc9203bfSDan Williams 		 * in the callback */
237cc9203bfSDan Williams 		return true;
238cc9203bfSDan Williams 	}
239cc9203bfSDan Williams 
240cc9203bfSDan Williams 	/*
241cc9203bfSDan Williams 	 * There is a race in the hardware that could cause us not to be notified
242cc9203bfSDan Williams 	 * of an interrupt completion if we do not take this step.  We will mask
243cc9203bfSDan Williams 	 * then unmask the error interrupts so if there was another interrupt
244cc9203bfSDan Williams 	 * pending we will be notified.
245cc9203bfSDan Williams 	 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
246d9dcb4baSDan Williams 	writel(0xff, &ihost->smu_registers->interrupt_mask);
247d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
248cc9203bfSDan Williams 
249cc9203bfSDan Williams 	return false;
250cc9203bfSDan Williams }
251cc9203bfSDan Williams 
25289a7301fSDan Williams static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
253cc9203bfSDan Williams {
25489a7301fSDan Williams 	u32 index = SCU_GET_COMPLETION_INDEX(ent);
255db056250SDan Williams 	struct isci_request *ireq = ihost->reqs[index];
256cc9203bfSDan Williams 
257cc9203bfSDan Williams 	/* Make sure that we really want to process this IO request */
258db056250SDan Williams 	if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
2595076a1a9SDan Williams 	    ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
260d9dcb4baSDan Williams 	    ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
26189a7301fSDan Williams 		/* Yep this is a valid io request pass it along to the
26289a7301fSDan Williams 		 * io request handler
26389a7301fSDan Williams 		 */
26489a7301fSDan Williams 		sci_io_request_tc_completion(ireq, ent);
265cc9203bfSDan Williams }
266cc9203bfSDan Williams 
26789a7301fSDan Williams static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
268cc9203bfSDan Williams {
269cc9203bfSDan Williams 	u32 index;
2705076a1a9SDan Williams 	struct isci_request *ireq;
27178a6f06eSDan Williams 	struct isci_remote_device *idev;
272cc9203bfSDan Williams 
27389a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
274cc9203bfSDan Williams 
27589a7301fSDan Williams 	switch (scu_get_command_request_type(ent)) {
276cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
277cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
278d9dcb4baSDan Williams 		ireq = ihost->reqs[index];
279d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
28089a7301fSDan Williams 			 __func__, ent, ireq);
281cc9203bfSDan Williams 		/* @todo For a post TC operation we need to fail the IO
282cc9203bfSDan Williams 		 * request
283cc9203bfSDan Williams 		 */
284cc9203bfSDan Williams 		break;
285cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
286cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
287cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
288d9dcb4baSDan Williams 		idev = ihost->device_table[index];
289d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
29089a7301fSDan Williams 			 __func__, ent, idev);
291cc9203bfSDan Williams 		/* @todo For a port RNC operation we need to fail the
292cc9203bfSDan Williams 		 * device
293cc9203bfSDan Williams 		 */
294cc9203bfSDan Williams 		break;
295cc9203bfSDan Williams 	default:
296d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
29789a7301fSDan Williams 			 __func__, ent);
298cc9203bfSDan Williams 		break;
299cc9203bfSDan Williams 	}
300cc9203bfSDan Williams }
301cc9203bfSDan Williams 
30289a7301fSDan Williams static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
303cc9203bfSDan Williams {
304cc9203bfSDan Williams 	u32 index;
305cc9203bfSDan Williams 	u32 frame_index;
306cc9203bfSDan Williams 
307cc9203bfSDan Williams 	struct scu_unsolicited_frame_header *frame_header;
30885280955SDan Williams 	struct isci_phy *iphy;
30978a6f06eSDan Williams 	struct isci_remote_device *idev;
310cc9203bfSDan Williams 
311cc9203bfSDan Williams 	enum sci_status result = SCI_FAILURE;
312cc9203bfSDan Williams 
31389a7301fSDan Williams 	frame_index = SCU_GET_FRAME_INDEX(ent);
314cc9203bfSDan Williams 
315d9dcb4baSDan Williams 	frame_header = ihost->uf_control.buffers.array[frame_index].header;
316d9dcb4baSDan Williams 	ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
317cc9203bfSDan Williams 
31889a7301fSDan Williams 	if (SCU_GET_FRAME_ERROR(ent)) {
319cc9203bfSDan Williams 		/*
320cc9203bfSDan Williams 		 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
321cc9203bfSDan Williams 		 * /       this cause a problem? We expect the phy initialization will
322cc9203bfSDan Williams 		 * /       fail if there is an error in the frame. */
32389a7301fSDan Williams 		sci_controller_release_frame(ihost, frame_index);
324cc9203bfSDan Williams 		return;
325cc9203bfSDan Williams 	}
326cc9203bfSDan Williams 
327cc9203bfSDan Williams 	if (frame_header->is_address_frame) {
32889a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
32985280955SDan Williams 		iphy = &ihost->phys[index];
33089a7301fSDan Williams 		result = sci_phy_frame_handler(iphy, frame_index);
331cc9203bfSDan Williams 	} else {
332cc9203bfSDan Williams 
33389a7301fSDan Williams 		index = SCU_GET_COMPLETION_INDEX(ent);
334cc9203bfSDan Williams 
335cc9203bfSDan Williams 		if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
336cc9203bfSDan Williams 			/*
337cc9203bfSDan Williams 			 * This is a signature fis or a frame from a direct attached SATA
338cc9203bfSDan Williams 			 * device that has not yet been created.  In either case forwared
339cc9203bfSDan Williams 			 * the frame to the PE and let it take care of the frame data. */
34089a7301fSDan Williams 			index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
34185280955SDan Williams 			iphy = &ihost->phys[index];
34289a7301fSDan Williams 			result = sci_phy_frame_handler(iphy, frame_index);
343cc9203bfSDan Williams 		} else {
344d9dcb4baSDan Williams 			if (index < ihost->remote_node_entries)
345d9dcb4baSDan Williams 				idev = ihost->device_table[index];
346cc9203bfSDan Williams 			else
34778a6f06eSDan Williams 				idev = NULL;
348cc9203bfSDan Williams 
34978a6f06eSDan Williams 			if (idev != NULL)
35089a7301fSDan Williams 				result = sci_remote_device_frame_handler(idev, frame_index);
351cc9203bfSDan Williams 			else
35289a7301fSDan Williams 				sci_controller_release_frame(ihost, frame_index);
353cc9203bfSDan Williams 		}
354cc9203bfSDan Williams 	}
355cc9203bfSDan Williams 
356cc9203bfSDan Williams 	if (result != SCI_SUCCESS) {
357cc9203bfSDan Williams 		/*
358cc9203bfSDan Williams 		 * / @todo Is there any reason to report some additional error message
359cc9203bfSDan Williams 		 * /       when we get this failure notifiction? */
360cc9203bfSDan Williams 	}
361cc9203bfSDan Williams }
362cc9203bfSDan Williams 
36389a7301fSDan Williams static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
364cc9203bfSDan Williams {
36578a6f06eSDan Williams 	struct isci_remote_device *idev;
3665076a1a9SDan Williams 	struct isci_request *ireq;
36785280955SDan Williams 	struct isci_phy *iphy;
368cc9203bfSDan Williams 	u32 index;
369cc9203bfSDan Williams 
37089a7301fSDan Williams 	index = SCU_GET_COMPLETION_INDEX(ent);
371cc9203bfSDan Williams 
37289a7301fSDan Williams 	switch (scu_get_event_type(ent)) {
373cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
374cc9203bfSDan Williams 		/* / @todo The driver did something wrong and we need to fix the condtion. */
375d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
376cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received SMU command error "
377cc9203bfSDan Williams 			"0x%x\n",
378cc9203bfSDan Williams 			__func__,
379d9dcb4baSDan Williams 			ihost,
38089a7301fSDan Williams 			ent);
381cc9203bfSDan Williams 		break;
382cc9203bfSDan Williams 
383cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
384cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_ERROR:
385cc9203bfSDan Williams 	case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
386cc9203bfSDan Williams 		/*
387cc9203bfSDan Williams 		 * / @todo This is a hardware failure and its likely that we want to
388cc9203bfSDan Williams 		 * /       reset the controller. */
389d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
390cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received fatal controller "
391cc9203bfSDan Williams 			"event  0x%x\n",
392cc9203bfSDan Williams 			__func__,
393d9dcb4baSDan Williams 			ihost,
39489a7301fSDan Williams 			ent);
395cc9203bfSDan Williams 		break;
396cc9203bfSDan Williams 
397cc9203bfSDan Williams 	case SCU_EVENT_TYPE_TRANSPORT_ERROR:
3985076a1a9SDan Williams 		ireq = ihost->reqs[index];
39989a7301fSDan Williams 		sci_io_request_event_handler(ireq, ent);
400cc9203bfSDan Williams 		break;
401cc9203bfSDan Williams 
402cc9203bfSDan Williams 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
40389a7301fSDan Williams 		switch (scu_get_event_specifier(ent)) {
404cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
405cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
4065076a1a9SDan Williams 			ireq = ihost->reqs[index];
4075076a1a9SDan Williams 			if (ireq != NULL)
40889a7301fSDan Williams 				sci_io_request_event_handler(ireq, ent);
409cc9203bfSDan Williams 			else
410d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
411cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
412cc9203bfSDan Williams 					 "event 0x%x for io request object "
413cc9203bfSDan Williams 					 "that doesnt exist.\n",
414cc9203bfSDan Williams 					 __func__,
415d9dcb4baSDan Williams 					 ihost,
41689a7301fSDan Williams 					 ent);
417cc9203bfSDan Williams 
418cc9203bfSDan Williams 			break;
419cc9203bfSDan Williams 
420cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
421d9dcb4baSDan Williams 			idev = ihost->device_table[index];
42278a6f06eSDan Williams 			if (idev != NULL)
42389a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
424cc9203bfSDan Williams 			else
425d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
426cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
427cc9203bfSDan Williams 					 "event 0x%x for remote device object "
428cc9203bfSDan Williams 					 "that doesnt exist.\n",
429cc9203bfSDan Williams 					 __func__,
430d9dcb4baSDan Williams 					 ihost,
43189a7301fSDan Williams 					 ent);
432cc9203bfSDan Williams 
433cc9203bfSDan Williams 			break;
434cc9203bfSDan Williams 		}
435cc9203bfSDan Williams 		break;
436cc9203bfSDan Williams 
437cc9203bfSDan Williams 	case SCU_EVENT_TYPE_BROADCAST_CHANGE:
438cc9203bfSDan Williams 	/*
439cc9203bfSDan Williams 	 * direct the broadcast change event to the phy first and then let
440cc9203bfSDan Williams 	 * the phy redirect the broadcast change to the port object */
441cc9203bfSDan Williams 	case SCU_EVENT_TYPE_ERR_CNT_EVENT:
442cc9203bfSDan Williams 	/*
443cc9203bfSDan Williams 	 * direct error counter event to the phy object since that is where
444cc9203bfSDan Williams 	 * we get the event notification.  This is a type 4 event. */
445cc9203bfSDan Williams 	case SCU_EVENT_TYPE_OSSP_EVENT:
44689a7301fSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
44785280955SDan Williams 		iphy = &ihost->phys[index];
44889a7301fSDan Williams 		sci_phy_event_handler(iphy, ent);
449cc9203bfSDan Williams 		break;
450cc9203bfSDan Williams 
451cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
452cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
453cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
454d9dcb4baSDan Williams 		if (index < ihost->remote_node_entries) {
455d9dcb4baSDan Williams 			idev = ihost->device_table[index];
456cc9203bfSDan Williams 
45778a6f06eSDan Williams 			if (idev != NULL)
45889a7301fSDan Williams 				sci_remote_device_event_handler(idev, ent);
459cc9203bfSDan Williams 		} else
460d9dcb4baSDan Williams 			dev_err(&ihost->pdev->dev,
461cc9203bfSDan Williams 				"%s: SCIC Controller 0x%p received event 0x%x "
462cc9203bfSDan Williams 				"for remote device object 0x%0x that doesnt "
463cc9203bfSDan Williams 				"exist.\n",
464cc9203bfSDan Williams 				__func__,
465d9dcb4baSDan Williams 				ihost,
46689a7301fSDan Williams 				ent,
467cc9203bfSDan Williams 				index);
468cc9203bfSDan Williams 
469cc9203bfSDan Williams 		break;
470cc9203bfSDan Williams 
471cc9203bfSDan Williams 	default:
472d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
473cc9203bfSDan Williams 			 "%s: SCIC Controller received unknown event code %x\n",
474cc9203bfSDan Williams 			 __func__,
47589a7301fSDan Williams 			 ent);
476cc9203bfSDan Williams 		break;
477cc9203bfSDan Williams 	}
478cc9203bfSDan Williams }
479cc9203bfSDan Williams 
48089a7301fSDan Williams static void sci_controller_process_completions(struct isci_host *ihost)
481cc9203bfSDan Williams {
482cc9203bfSDan Williams 	u32 completion_count = 0;
48389a7301fSDan Williams 	u32 ent;
484cc9203bfSDan Williams 	u32 get_index;
485cc9203bfSDan Williams 	u32 get_cycle;
486994a9303SDan Williams 	u32 event_get;
487cc9203bfSDan Williams 	u32 event_cycle;
488cc9203bfSDan Williams 
489d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
490cc9203bfSDan Williams 		"%s: completion queue begining get:0x%08x\n",
491cc9203bfSDan Williams 		__func__,
492d9dcb4baSDan Williams 		ihost->completion_queue_get);
493cc9203bfSDan Williams 
494cc9203bfSDan Williams 	/* Get the component parts of the completion queue */
495d9dcb4baSDan Williams 	get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
496d9dcb4baSDan Williams 	get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
497cc9203bfSDan Williams 
498d9dcb4baSDan Williams 	event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
499d9dcb4baSDan Williams 	event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
500cc9203bfSDan Williams 
501cc9203bfSDan Williams 	while (
502cc9203bfSDan Williams 		NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
503d9dcb4baSDan Williams 		== COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
504cc9203bfSDan Williams 		) {
505cc9203bfSDan Williams 		completion_count++;
506cc9203bfSDan Williams 
50789a7301fSDan Williams 		ent = ihost->completion_queue[get_index];
508994a9303SDan Williams 
509994a9303SDan Williams 		/* increment the get pointer and check for rollover to toggle the cycle bit */
510994a9303SDan Williams 		get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
511994a9303SDan Williams 			     (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
512994a9303SDan Williams 		get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
513cc9203bfSDan Williams 
514d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
515cc9203bfSDan Williams 			"%s: completion queue entry:0x%08x\n",
516cc9203bfSDan Williams 			__func__,
51789a7301fSDan Williams 			ent);
518cc9203bfSDan Williams 
51989a7301fSDan Williams 		switch (SCU_GET_COMPLETION_TYPE(ent)) {
520cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_TASK:
52189a7301fSDan Williams 			sci_controller_task_completion(ihost, ent);
522cc9203bfSDan Williams 			break;
523cc9203bfSDan Williams 
524cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_SDMA:
52589a7301fSDan Williams 			sci_controller_sdma_completion(ihost, ent);
526cc9203bfSDan Williams 			break;
527cc9203bfSDan Williams 
528cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_UFI:
52989a7301fSDan Williams 			sci_controller_unsolicited_frame(ihost, ent);
530cc9203bfSDan Williams 			break;
531cc9203bfSDan Williams 
532cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_EVENT:
53377cd72a5SDan Williams 			sci_controller_event_completion(ihost, ent);
53477cd72a5SDan Williams 			break;
53577cd72a5SDan Williams 
536994a9303SDan Williams 		case SCU_COMPLETION_TYPE_NOTIFY: {
537994a9303SDan Williams 			event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
538994a9303SDan Williams 				       (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
539994a9303SDan Williams 			event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
540994a9303SDan Williams 
54189a7301fSDan Williams 			sci_controller_event_completion(ihost, ent);
542cc9203bfSDan Williams 			break;
543994a9303SDan Williams 		}
544cc9203bfSDan Williams 		default:
545d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
546cc9203bfSDan Williams 				 "%s: SCIC Controller received unknown "
547cc9203bfSDan Williams 				 "completion type %x\n",
548cc9203bfSDan Williams 				 __func__,
54989a7301fSDan Williams 				 ent);
550cc9203bfSDan Williams 			break;
551cc9203bfSDan Williams 		}
552cc9203bfSDan Williams 	}
553cc9203bfSDan Williams 
554cc9203bfSDan Williams 	/* Update the get register if we completed one or more entries */
555cc9203bfSDan Williams 	if (completion_count > 0) {
556d9dcb4baSDan Williams 		ihost->completion_queue_get =
557cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(ENABLE) |
558cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
559cc9203bfSDan Williams 			event_cycle |
560994a9303SDan Williams 			SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
561cc9203bfSDan Williams 			get_cycle |
562cc9203bfSDan Williams 			SMU_CQGR_GEN_VAL(POINTER, get_index);
563cc9203bfSDan Williams 
564d9dcb4baSDan Williams 		writel(ihost->completion_queue_get,
565d9dcb4baSDan Williams 		       &ihost->smu_registers->completion_queue_get);
566cc9203bfSDan Williams 
567cc9203bfSDan Williams 	}
568cc9203bfSDan Williams 
569d9dcb4baSDan Williams 	dev_dbg(&ihost->pdev->dev,
570cc9203bfSDan Williams 		"%s: completion queue ending get:0x%08x\n",
571cc9203bfSDan Williams 		__func__,
572d9dcb4baSDan Williams 		ihost->completion_queue_get);
573cc9203bfSDan Williams 
574cc9203bfSDan Williams }
575cc9203bfSDan Williams 
57689a7301fSDan Williams static void sci_controller_error_handler(struct isci_host *ihost)
577cc9203bfSDan Williams {
578cc9203bfSDan Williams 	u32 interrupt_status;
579cc9203bfSDan Williams 
580cc9203bfSDan Williams 	interrupt_status =
581d9dcb4baSDan Williams 		readl(&ihost->smu_registers->interrupt_status);
582cc9203bfSDan Williams 
583cc9203bfSDan Williams 	if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
58489a7301fSDan Williams 	    sci_controller_completion_queue_has_entries(ihost)) {
585cc9203bfSDan Williams 
58689a7301fSDan Williams 		sci_controller_process_completions(ihost);
587d9dcb4baSDan Williams 		writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
588cc9203bfSDan Williams 	} else {
589d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
590cc9203bfSDan Williams 			interrupt_status);
591cc9203bfSDan Williams 
592d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_FAILED);
593cc9203bfSDan Williams 
594cc9203bfSDan Williams 		return;
595cc9203bfSDan Williams 	}
596cc9203bfSDan Williams 
597cc9203bfSDan Williams 	/* If we dont process any completions I am not sure that we want to do this.
598cc9203bfSDan Williams 	 * We are in the middle of a hardware fault and should probably be reset.
599cc9203bfSDan Williams 	 */
600d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
601cc9203bfSDan Williams }
602cc9203bfSDan Williams 
603c7ef4031SDan Williams irqreturn_t isci_intx_isr(int vec, void *data)
6046f231ddaSDan Williams {
6056f231ddaSDan Williams 	irqreturn_t ret = IRQ_NONE;
60631e824edSDan Williams 	struct isci_host *ihost = data;
6076f231ddaSDan Williams 
60889a7301fSDan Williams 	if (sci_controller_isr(ihost)) {
609d9dcb4baSDan Williams 		writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
610c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
6116f231ddaSDan Williams 		ret = IRQ_HANDLED;
61289a7301fSDan Williams 	} else if (sci_controller_error_isr(ihost)) {
61392f4f0f5SDan Williams 		spin_lock(&ihost->scic_lock);
61489a7301fSDan Williams 		sci_controller_error_handler(ihost);
61592f4f0f5SDan Williams 		spin_unlock(&ihost->scic_lock);
61692f4f0f5SDan Williams 		ret = IRQ_HANDLED;
6176f231ddaSDan Williams 	}
61892f4f0f5SDan Williams 
6196f231ddaSDan Williams 	return ret;
6206f231ddaSDan Williams }
6216f231ddaSDan Williams 
62292f4f0f5SDan Williams irqreturn_t isci_error_isr(int vec, void *data)
62392f4f0f5SDan Williams {
62492f4f0f5SDan Williams 	struct isci_host *ihost = data;
62592f4f0f5SDan Williams 
62689a7301fSDan Williams 	if (sci_controller_error_isr(ihost))
62789a7301fSDan Williams 		sci_controller_error_handler(ihost);
62892f4f0f5SDan Williams 
62992f4f0f5SDan Williams 	return IRQ_HANDLED;
63092f4f0f5SDan Williams }
6316f231ddaSDan Williams 
6326f231ddaSDan Williams /**
6336f231ddaSDan Williams  * isci_host_start_complete() - This function is called by the core library,
6346f231ddaSDan Williams  *    through the ISCI Module, to indicate controller start status.
6356f231ddaSDan Williams  * @isci_host: This parameter specifies the ISCI host object
6366f231ddaSDan Williams  * @completion_status: This parameter specifies the completion status from the
6376f231ddaSDan Williams  *    core library.
6386f231ddaSDan Williams  *
6396f231ddaSDan Williams  */
640cc9203bfSDan Williams static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
6416f231ddaSDan Williams {
6420cf89d1dSDan Williams 	if (completion_status != SCI_SUCCESS)
6430cf89d1dSDan Williams 		dev_info(&ihost->pdev->dev,
6440cf89d1dSDan Williams 			"controller start timed out, continuing...\n");
6450cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_ready);
6460cf89d1dSDan Williams 	clear_bit(IHOST_START_PENDING, &ihost->flags);
6470cf89d1dSDan Williams 	wake_up(&ihost->eventq);
6486f231ddaSDan Williams }
6496f231ddaSDan Williams 
650c7ef4031SDan Williams int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
6516f231ddaSDan Williams {
652b1124cd3SDan Williams 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
653b1124cd3SDan Williams 	struct isci_host *ihost = ha->lldd_ha;
6546f231ddaSDan Williams 
65577950f51SEdmund Nadolski 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
6566f231ddaSDan Williams 		return 0;
6576f231ddaSDan Williams 
658b1124cd3SDan Williams 	sas_drain_work(ha);
6596f231ddaSDan Williams 
6600cf89d1dSDan Williams 	dev_dbg(&ihost->pdev->dev,
6610cf89d1dSDan Williams 		"%s: ihost->status = %d, time = %ld\n",
6620cf89d1dSDan Williams 		 __func__, isci_host_get_state(ihost), time);
6636f231ddaSDan Williams 
6646f231ddaSDan Williams 	return 1;
6656f231ddaSDan Williams 
6666f231ddaSDan Williams }
6676f231ddaSDan Williams 
668cc9203bfSDan Williams /**
66989a7301fSDan Williams  * sci_controller_get_suggested_start_timeout() - This method returns the
67089a7301fSDan Williams  *    suggested sci_controller_start() timeout amount.  The user is free to
671cc9203bfSDan Williams  *    use any timeout value, but this method provides the suggested minimum
672cc9203bfSDan Williams  *    start timeout value.  The returned value is based upon empirical
673cc9203bfSDan Williams  *    information determined as a result of interoperability testing.
674cc9203bfSDan Williams  * @controller: the handle to the controller object for which to return the
675cc9203bfSDan Williams  *    suggested start timeout.
676cc9203bfSDan Williams  *
677cc9203bfSDan Williams  * This method returns the number of milliseconds for the suggested start
678cc9203bfSDan Williams  * operation timeout.
679cc9203bfSDan Williams  */
68089a7301fSDan Williams static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
681cc9203bfSDan Williams {
682cc9203bfSDan Williams 	/* Validate the user supplied parameters. */
683d9dcb4baSDan Williams 	if (!ihost)
684cc9203bfSDan Williams 		return 0;
685cc9203bfSDan Williams 
686cc9203bfSDan Williams 	/*
687cc9203bfSDan Williams 	 * The suggested minimum timeout value for a controller start operation:
688cc9203bfSDan Williams 	 *
689cc9203bfSDan Williams 	 *     Signature FIS Timeout
690cc9203bfSDan Williams 	 *   + Phy Start Timeout
691cc9203bfSDan Williams 	 *   + Number of Phy Spin Up Intervals
692cc9203bfSDan Williams 	 *   ---------------------------------
693cc9203bfSDan Williams 	 *   Number of milliseconds for the controller start operation.
694cc9203bfSDan Williams 	 *
695cc9203bfSDan Williams 	 * NOTE: The number of phy spin up intervals will be equivalent
696cc9203bfSDan Williams 	 *       to the number of phys divided by the number phys allowed
697cc9203bfSDan Williams 	 *       per interval - 1 (once OEM parameters are supported).
698cc9203bfSDan Williams 	 *       Currently we assume only 1 phy per interval. */
699cc9203bfSDan Williams 
700cc9203bfSDan Williams 	return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
701cc9203bfSDan Williams 		+ SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
702cc9203bfSDan Williams 		+ ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
703cc9203bfSDan Williams }
704cc9203bfSDan Williams 
70589a7301fSDan Williams static void sci_controller_enable_interrupts(struct isci_host *ihost)
706cc9203bfSDan Williams {
707d9dcb4baSDan Williams 	BUG_ON(ihost->smu_registers == NULL);
708d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
709cc9203bfSDan Williams }
710cc9203bfSDan Williams 
71189a7301fSDan Williams void sci_controller_disable_interrupts(struct isci_host *ihost)
712cc9203bfSDan Williams {
713d9dcb4baSDan Williams 	BUG_ON(ihost->smu_registers == NULL);
714d9dcb4baSDan Williams 	writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
715cc9203bfSDan Williams }
716cc9203bfSDan Williams 
71789a7301fSDan Williams static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
718cc9203bfSDan Williams {
719cc9203bfSDan Williams 	u32 port_task_scheduler_value;
720cc9203bfSDan Williams 
721cc9203bfSDan Williams 	port_task_scheduler_value =
722d9dcb4baSDan Williams 		readl(&ihost->scu_registers->peg0.ptsg.control);
723cc9203bfSDan Williams 	port_task_scheduler_value |=
724cc9203bfSDan Williams 		(SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
725cc9203bfSDan Williams 		 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
726cc9203bfSDan Williams 	writel(port_task_scheduler_value,
727d9dcb4baSDan Williams 	       &ihost->scu_registers->peg0.ptsg.control);
728cc9203bfSDan Williams }
729cc9203bfSDan Williams 
73089a7301fSDan Williams static void sci_controller_assign_task_entries(struct isci_host *ihost)
731cc9203bfSDan Williams {
732cc9203bfSDan Williams 	u32 task_assignment;
733cc9203bfSDan Williams 
734cc9203bfSDan Williams 	/*
735cc9203bfSDan Williams 	 * Assign all the TCs to function 0
736cc9203bfSDan Williams 	 * TODO: Do we actually need to read this register to write it back?
737cc9203bfSDan Williams 	 */
738cc9203bfSDan Williams 
739cc9203bfSDan Williams 	task_assignment =
740d9dcb4baSDan Williams 		readl(&ihost->smu_registers->task_context_assignment[0]);
741cc9203bfSDan Williams 
742cc9203bfSDan Williams 	task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
743d9dcb4baSDan Williams 		(SMU_TCA_GEN_VAL(ENDING,  ihost->task_context_entries - 1)) |
744cc9203bfSDan Williams 		(SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
745cc9203bfSDan Williams 
746cc9203bfSDan Williams 	writel(task_assignment,
747d9dcb4baSDan Williams 		&ihost->smu_registers->task_context_assignment[0]);
748cc9203bfSDan Williams 
749cc9203bfSDan Williams }
750cc9203bfSDan Williams 
75189a7301fSDan Williams static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
752cc9203bfSDan Williams {
753cc9203bfSDan Williams 	u32 index;
754cc9203bfSDan Williams 	u32 completion_queue_control_value;
755cc9203bfSDan Williams 	u32 completion_queue_get_value;
756cc9203bfSDan Williams 	u32 completion_queue_put_value;
757cc9203bfSDan Williams 
758d9dcb4baSDan Williams 	ihost->completion_queue_get = 0;
759cc9203bfSDan Williams 
7607c78da31SDan Williams 	completion_queue_control_value =
7617c78da31SDan Williams 		(SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
7627c78da31SDan Williams 		 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
763cc9203bfSDan Williams 
764cc9203bfSDan Williams 	writel(completion_queue_control_value,
765d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_control);
766cc9203bfSDan Williams 
767cc9203bfSDan Williams 
768cc9203bfSDan Williams 	/* Set the completion queue get pointer and enable the queue */
769cc9203bfSDan Williams 	completion_queue_get_value = (
770cc9203bfSDan Williams 		(SMU_CQGR_GEN_VAL(POINTER, 0))
771cc9203bfSDan Williams 		| (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
772cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(ENABLE))
773cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
774cc9203bfSDan Williams 		);
775cc9203bfSDan Williams 
776cc9203bfSDan Williams 	writel(completion_queue_get_value,
777d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_get);
778cc9203bfSDan Williams 
779cc9203bfSDan Williams 	/* Set the completion queue put pointer */
780cc9203bfSDan Williams 	completion_queue_put_value = (
781cc9203bfSDan Williams 		(SMU_CQPR_GEN_VAL(POINTER, 0))
782cc9203bfSDan Williams 		| (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
783cc9203bfSDan Williams 		);
784cc9203bfSDan Williams 
785cc9203bfSDan Williams 	writel(completion_queue_put_value,
786d9dcb4baSDan Williams 	       &ihost->smu_registers->completion_queue_put);
787cc9203bfSDan Williams 
788cc9203bfSDan Williams 	/* Initialize the cycle bit of the completion queue entries */
7897c78da31SDan Williams 	for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
790cc9203bfSDan Williams 		/*
791cc9203bfSDan Williams 		 * If get.cycle_bit != completion_queue.cycle_bit
792cc9203bfSDan Williams 		 * its not a valid completion queue entry
793cc9203bfSDan Williams 		 * so at system start all entries are invalid */
794d9dcb4baSDan Williams 		ihost->completion_queue[index] = 0x80000000;
795cc9203bfSDan Williams 	}
796cc9203bfSDan Williams }
797cc9203bfSDan Williams 
79889a7301fSDan Williams static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
799cc9203bfSDan Williams {
800cc9203bfSDan Williams 	u32 frame_queue_control_value;
801cc9203bfSDan Williams 	u32 frame_queue_get_value;
802cc9203bfSDan Williams 	u32 frame_queue_put_value;
803cc9203bfSDan Williams 
804cc9203bfSDan Williams 	/* Write the queue size */
805cc9203bfSDan Williams 	frame_queue_control_value =
8067c78da31SDan Williams 		SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
807cc9203bfSDan Williams 
808cc9203bfSDan Williams 	writel(frame_queue_control_value,
809d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
810cc9203bfSDan Williams 
811cc9203bfSDan Williams 	/* Setup the get pointer for the unsolicited frame queue */
812cc9203bfSDan Williams 	frame_queue_get_value = (
813cc9203bfSDan Williams 		SCU_UFQGP_GEN_VAL(POINTER, 0)
814cc9203bfSDan Williams 		|  SCU_UFQGP_GEN_BIT(ENABLE_BIT)
815cc9203bfSDan Williams 		);
816cc9203bfSDan Williams 
817cc9203bfSDan Williams 	writel(frame_queue_get_value,
818d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
819cc9203bfSDan Williams 	/* Setup the put pointer for the unsolicited frame queue */
820cc9203bfSDan Williams 	frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
821cc9203bfSDan Williams 	writel(frame_queue_put_value,
822d9dcb4baSDan Williams 	       &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
823cc9203bfSDan Williams }
824cc9203bfSDan Williams 
82589a7301fSDan Williams static void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
826cc9203bfSDan Williams {
827d9dcb4baSDan Williams 	if (ihost->sm.current_state_id == SCIC_STARTING) {
828cc9203bfSDan Williams 		/*
829cc9203bfSDan Williams 		 * We move into the ready state, because some of the phys/ports
830cc9203bfSDan Williams 		 * may be up and operational.
831cc9203bfSDan Williams 		 */
832d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_READY);
833cc9203bfSDan Williams 
834cc9203bfSDan Williams 		isci_host_start_complete(ihost, status);
835cc9203bfSDan Williams 	}
836cc9203bfSDan Williams }
837cc9203bfSDan Williams 
83885280955SDan Williams static bool is_phy_starting(struct isci_phy *iphy)
8394a33c525SAdam Gruchala {
84089a7301fSDan Williams 	enum sci_phy_states state;
8414a33c525SAdam Gruchala 
84285280955SDan Williams 	state = iphy->sm.current_state_id;
8434a33c525SAdam Gruchala 	switch (state) {
844e301370aSEdmund Nadolski 	case SCI_PHY_STARTING:
845e301370aSEdmund Nadolski 	case SCI_PHY_SUB_INITIAL:
846e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
847e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_IAF_UF:
848e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
849e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
850e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
851e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
852e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
853e301370aSEdmund Nadolski 	case SCI_PHY_SUB_FINAL:
8544a33c525SAdam Gruchala 		return true;
8554a33c525SAdam Gruchala 	default:
8564a33c525SAdam Gruchala 		return false;
8574a33c525SAdam Gruchala 	}
8584a33c525SAdam Gruchala }
8594a33c525SAdam Gruchala 
860cc9203bfSDan Williams /**
86189a7301fSDan Williams  * sci_controller_start_next_phy - start phy
862cc9203bfSDan Williams  * @scic: controller
863cc9203bfSDan Williams  *
864cc9203bfSDan Williams  * If all the phys have been started, then attempt to transition the
865cc9203bfSDan Williams  * controller to the READY state and inform the user
86689a7301fSDan Williams  * (sci_cb_controller_start_complete()).
867cc9203bfSDan Williams  */
86889a7301fSDan Williams static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
869cc9203bfSDan Williams {
87089a7301fSDan Williams 	struct sci_oem_params *oem = &ihost->oem_parameters;
87185280955SDan Williams 	struct isci_phy *iphy;
872cc9203bfSDan Williams 	enum sci_status status;
873cc9203bfSDan Williams 
874cc9203bfSDan Williams 	status = SCI_SUCCESS;
875cc9203bfSDan Williams 
876d9dcb4baSDan Williams 	if (ihost->phy_startup_timer_pending)
877cc9203bfSDan Williams 		return status;
878cc9203bfSDan Williams 
879d9dcb4baSDan Williams 	if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
880cc9203bfSDan Williams 		bool is_controller_start_complete = true;
881cc9203bfSDan Williams 		u32 state;
882cc9203bfSDan Williams 		u8 index;
883cc9203bfSDan Williams 
884cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
88585280955SDan Williams 			iphy = &ihost->phys[index];
88685280955SDan Williams 			state = iphy->sm.current_state_id;
887cc9203bfSDan Williams 
88885280955SDan Williams 			if (!phy_get_non_dummy_port(iphy))
889cc9203bfSDan Williams 				continue;
890cc9203bfSDan Williams 
891cc9203bfSDan Williams 			/* The controller start operation is complete iff:
892cc9203bfSDan Williams 			 * - all links have been given an opportunity to start
893cc9203bfSDan Williams 			 * - have no indication of a connected device
894cc9203bfSDan Williams 			 * - have an indication of a connected device and it has
895cc9203bfSDan Williams 			 *   finished the link training process.
896cc9203bfSDan Williams 			 */
89785280955SDan Williams 			if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
89885280955SDan Williams 			    (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
899be778341SMarcin Tomczak 			    (iphy->is_in_link_training == true && is_phy_starting(iphy)) ||
900be778341SMarcin Tomczak 			    (ihost->port_agent.phy_ready_mask != ihost->port_agent.phy_configured_mask)) {
901cc9203bfSDan Williams 				is_controller_start_complete = false;
902cc9203bfSDan Williams 				break;
903cc9203bfSDan Williams 			}
904cc9203bfSDan Williams 		}
905cc9203bfSDan Williams 
906cc9203bfSDan Williams 		/*
907cc9203bfSDan Williams 		 * The controller has successfully finished the start process.
908cc9203bfSDan Williams 		 * Inform the SCI Core user and transition to the READY state. */
909cc9203bfSDan Williams 		if (is_controller_start_complete == true) {
91089a7301fSDan Williams 			sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
911d9dcb4baSDan Williams 			sci_del_timer(&ihost->phy_timer);
912d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = false;
913cc9203bfSDan Williams 		}
914cc9203bfSDan Williams 	} else {
915d9dcb4baSDan Williams 		iphy = &ihost->phys[ihost->next_phy_to_start];
916cc9203bfSDan Williams 
917cc9203bfSDan Williams 		if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
91885280955SDan Williams 			if (phy_get_non_dummy_port(iphy) == NULL) {
919d9dcb4baSDan Williams 				ihost->next_phy_to_start++;
920cc9203bfSDan Williams 
921cc9203bfSDan Williams 				/* Caution recursion ahead be forwarned
922cc9203bfSDan Williams 				 *
923cc9203bfSDan Williams 				 * The PHY was never added to a PORT in MPC mode
924cc9203bfSDan Williams 				 * so start the next phy in sequence This phy
925cc9203bfSDan Williams 				 * will never go link up and will not draw power
926cc9203bfSDan Williams 				 * the OEM parameters either configured the phy
927cc9203bfSDan Williams 				 * incorrectly for the PORT or it was never
928cc9203bfSDan Williams 				 * assigned to a PORT
929cc9203bfSDan Williams 				 */
93089a7301fSDan Williams 				return sci_controller_start_next_phy(ihost);
931cc9203bfSDan Williams 			}
932cc9203bfSDan Williams 		}
933cc9203bfSDan Williams 
93489a7301fSDan Williams 		status = sci_phy_start(iphy);
935cc9203bfSDan Williams 
936cc9203bfSDan Williams 		if (status == SCI_SUCCESS) {
937d9dcb4baSDan Williams 			sci_mod_timer(&ihost->phy_timer,
938bb3dbdf6SEdmund Nadolski 				      SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
939d9dcb4baSDan Williams 			ihost->phy_startup_timer_pending = true;
940cc9203bfSDan Williams 		} else {
941d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
942cc9203bfSDan Williams 				 "%s: Controller stop operation failed "
943cc9203bfSDan Williams 				 "to stop phy %d because of status "
944cc9203bfSDan Williams 				 "%d.\n",
945cc9203bfSDan Williams 				 __func__,
946d9dcb4baSDan Williams 				 ihost->phys[ihost->next_phy_to_start].phy_index,
947cc9203bfSDan Williams 				 status);
948cc9203bfSDan Williams 		}
949cc9203bfSDan Williams 
950d9dcb4baSDan Williams 		ihost->next_phy_to_start++;
951cc9203bfSDan Williams 	}
952cc9203bfSDan Williams 
953cc9203bfSDan Williams 	return status;
954cc9203bfSDan Williams }
955cc9203bfSDan Williams 
956bb3dbdf6SEdmund Nadolski static void phy_startup_timeout(unsigned long data)
957cc9203bfSDan Williams {
958bb3dbdf6SEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
959d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
960bb3dbdf6SEdmund Nadolski 	unsigned long flags;
961cc9203bfSDan Williams 	enum sci_status status;
962cc9203bfSDan Williams 
963bb3dbdf6SEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
964bb3dbdf6SEdmund Nadolski 
965bb3dbdf6SEdmund Nadolski 	if (tmr->cancel)
966bb3dbdf6SEdmund Nadolski 		goto done;
967bb3dbdf6SEdmund Nadolski 
968d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
969bb3dbdf6SEdmund Nadolski 
970bb3dbdf6SEdmund Nadolski 	do {
97189a7301fSDan Williams 		status = sci_controller_start_next_phy(ihost);
972bb3dbdf6SEdmund Nadolski 	} while (status != SCI_SUCCESS);
973bb3dbdf6SEdmund Nadolski 
974bb3dbdf6SEdmund Nadolski done:
975bb3dbdf6SEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
976cc9203bfSDan Williams }
977cc9203bfSDan Williams 
978ac668c69SDan Williams static u16 isci_tci_active(struct isci_host *ihost)
979ac668c69SDan Williams {
980ac668c69SDan Williams 	return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
981ac668c69SDan Williams }
982ac668c69SDan Williams 
98389a7301fSDan Williams static enum sci_status sci_controller_start(struct isci_host *ihost,
984cc9203bfSDan Williams 					     u32 timeout)
985cc9203bfSDan Williams {
986cc9203bfSDan Williams 	enum sci_status result;
987cc9203bfSDan Williams 	u16 index;
988cc9203bfSDan Williams 
989d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
99014e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
99114e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
992cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
993cc9203bfSDan Williams 	}
994cc9203bfSDan Williams 
995cc9203bfSDan Williams 	/* Build the TCi free pool */
996ac668c69SDan Williams 	BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
997ac668c69SDan Williams 	ihost->tci_head = 0;
998ac668c69SDan Williams 	ihost->tci_tail = 0;
999d9dcb4baSDan Williams 	for (index = 0; index < ihost->task_context_entries; index++)
1000ac668c69SDan Williams 		isci_tci_free(ihost, index);
1001cc9203bfSDan Williams 
1002cc9203bfSDan Williams 	/* Build the RNi free pool */
100389a7301fSDan Williams 	sci_remote_node_table_initialize(&ihost->available_remote_nodes,
1004d9dcb4baSDan Williams 					 ihost->remote_node_entries);
1005cc9203bfSDan Williams 
1006cc9203bfSDan Williams 	/*
1007cc9203bfSDan Williams 	 * Before anything else lets make sure we will not be
1008cc9203bfSDan Williams 	 * interrupted by the hardware.
1009cc9203bfSDan Williams 	 */
101089a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1011cc9203bfSDan Williams 
1012cc9203bfSDan Williams 	/* Enable the port task scheduler */
101389a7301fSDan Williams 	sci_controller_enable_port_task_scheduler(ihost);
1014cc9203bfSDan Williams 
1015d9dcb4baSDan Williams 	/* Assign all the task entries to ihost physical function */
101689a7301fSDan Williams 	sci_controller_assign_task_entries(ihost);
1017cc9203bfSDan Williams 
1018cc9203bfSDan Williams 	/* Now initialize the completion queue */
101989a7301fSDan Williams 	sci_controller_initialize_completion_queue(ihost);
1020cc9203bfSDan Williams 
1021cc9203bfSDan Williams 	/* Initialize the unsolicited frame queue for use */
102289a7301fSDan Williams 	sci_controller_initialize_unsolicited_frame_queue(ihost);
1023cc9203bfSDan Williams 
1024cc9203bfSDan Williams 	/* Start all of the ports on this controller */
1025d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1026ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1027cc9203bfSDan Williams 
102889a7301fSDan Williams 		result = sci_port_start(iport);
1029cc9203bfSDan Williams 		if (result)
1030cc9203bfSDan Williams 			return result;
1031cc9203bfSDan Williams 	}
1032cc9203bfSDan Williams 
103389a7301fSDan Williams 	sci_controller_start_next_phy(ihost);
1034cc9203bfSDan Williams 
1035d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1036cc9203bfSDan Williams 
1037d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STARTING);
1038cc9203bfSDan Williams 
1039cc9203bfSDan Williams 	return SCI_SUCCESS;
1040cc9203bfSDan Williams }
1041cc9203bfSDan Williams 
10426f231ddaSDan Williams void isci_host_scan_start(struct Scsi_Host *shost)
10436f231ddaSDan Williams {
10444393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
104589a7301fSDan Williams 	unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
10466f231ddaSDan Williams 
10470cf89d1dSDan Williams 	set_bit(IHOST_START_PENDING, &ihost->flags);
104877950f51SEdmund Nadolski 
104977950f51SEdmund Nadolski 	spin_lock_irq(&ihost->scic_lock);
105089a7301fSDan Williams 	sci_controller_start(ihost, tmo);
105189a7301fSDan Williams 	sci_controller_enable_interrupts(ihost);
105277950f51SEdmund Nadolski 	spin_unlock_irq(&ihost->scic_lock);
10536f231ddaSDan Williams }
10546f231ddaSDan Williams 
1055cc9203bfSDan Williams static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
10566f231ddaSDan Williams {
10570cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopped);
105889a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
10590cf89d1dSDan Williams 	clear_bit(IHOST_STOP_PENDING, &ihost->flags);
10600cf89d1dSDan Williams 	wake_up(&ihost->eventq);
10616f231ddaSDan Williams }
10626f231ddaSDan Williams 
106389a7301fSDan Williams static void sci_controller_completion_handler(struct isci_host *ihost)
1064cc9203bfSDan Williams {
1065cc9203bfSDan Williams 	/* Empty out the completion queue */
106689a7301fSDan Williams 	if (sci_controller_completion_queue_has_entries(ihost))
106789a7301fSDan Williams 		sci_controller_process_completions(ihost);
1068cc9203bfSDan Williams 
1069cc9203bfSDan Williams 	/* Clear the interrupt and enable all interrupts again */
1070d9dcb4baSDan Williams 	writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
1071cc9203bfSDan Williams 	/* Could we write the value of SMU_ISR_COMPLETION? */
1072d9dcb4baSDan Williams 	writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
1073d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->interrupt_mask);
1074cc9203bfSDan Williams }
1075cc9203bfSDan Williams 
10766f231ddaSDan Williams /**
10776f231ddaSDan Williams  * isci_host_completion_routine() - This function is the delayed service
10786f231ddaSDan Williams  *    routine that calls the sci core library's completion handler. It's
10796f231ddaSDan Williams  *    scheduled as a tasklet from the interrupt service routine when interrupts
10806f231ddaSDan Williams  *    in use, or set as the timeout function in polled mode.
10816f231ddaSDan Williams  * @data: This parameter specifies the ISCI host object
10826f231ddaSDan Williams  *
10836f231ddaSDan Williams  */
10846f231ddaSDan Williams static void isci_host_completion_routine(unsigned long data)
10856f231ddaSDan Williams {
1086d9dcb4baSDan Williams 	struct isci_host *ihost = (struct isci_host *)data;
10876f231ddaSDan Williams 	struct list_head    completed_request_list;
108811b00c19SJeff Skirvin 	struct list_head    errored_request_list;
10896f231ddaSDan Williams 	struct list_head    *current_position;
10906f231ddaSDan Williams 	struct list_head    *next_position;
10916f231ddaSDan Williams 	struct isci_request *request;
10926f231ddaSDan Williams 	struct isci_request *next_request;
10936f231ddaSDan Williams 	struct sas_task     *task;
10949b4be528SDan Williams 	u16 active;
10956f231ddaSDan Williams 
10966f231ddaSDan Williams 	INIT_LIST_HEAD(&completed_request_list);
109711b00c19SJeff Skirvin 	INIT_LIST_HEAD(&errored_request_list);
10986f231ddaSDan Williams 
1099d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
11006f231ddaSDan Williams 
110189a7301fSDan Williams 	sci_controller_completion_handler(ihost);
1102c7ef4031SDan Williams 
11036f231ddaSDan Williams 	/* Take the lists of completed I/Os from the host. */
110411b00c19SJeff Skirvin 
1105d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_complete,
11066f231ddaSDan Williams 			 &completed_request_list);
11076f231ddaSDan Williams 
110811b00c19SJeff Skirvin 	/* Take the list of errored I/Os from the host. */
1109d9dcb4baSDan Williams 	list_splice_init(&ihost->requests_to_errorback,
111011b00c19SJeff Skirvin 			 &errored_request_list);
11116f231ddaSDan Williams 
1112d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
11136f231ddaSDan Williams 
11146f231ddaSDan Williams 	/* Process any completions in the lists. */
11156f231ddaSDan Williams 	list_for_each_safe(current_position, next_position,
11166f231ddaSDan Williams 			   &completed_request_list) {
11176f231ddaSDan Williams 
11186f231ddaSDan Williams 		request = list_entry(current_position, struct isci_request,
11196f231ddaSDan Williams 				     completed_node);
11206f231ddaSDan Williams 		task = isci_request_access_task(request);
11216f231ddaSDan Williams 
11226f231ddaSDan Williams 		/* Normal notification (task_done) */
1123d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
11246f231ddaSDan Williams 			"%s: Normal - request/task = %p/%p\n",
11256f231ddaSDan Williams 			__func__,
11266f231ddaSDan Williams 			request,
11276f231ddaSDan Williams 			task);
11286f231ddaSDan Williams 
112911b00c19SJeff Skirvin 		/* Return the task to libsas */
113011b00c19SJeff Skirvin 		if (task != NULL) {
11316f231ddaSDan Williams 
113211b00c19SJeff Skirvin 			task->lldd_task = NULL;
113311b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
113411b00c19SJeff Skirvin 
113511b00c19SJeff Skirvin 				/* If the task is already in the abort path,
113611b00c19SJeff Skirvin 				* the task_done callback cannot be called.
113711b00c19SJeff Skirvin 				*/
113811b00c19SJeff Skirvin 				task->task_done(task);
113911b00c19SJeff Skirvin 			}
114011b00c19SJeff Skirvin 		}
1141312e0c24SDan Williams 
1142d9dcb4baSDan Williams 		spin_lock_irq(&ihost->scic_lock);
1143d9dcb4baSDan Williams 		isci_free_tag(ihost, request->io_tag);
1144d9dcb4baSDan Williams 		spin_unlock_irq(&ihost->scic_lock);
11456f231ddaSDan Williams 	}
114611b00c19SJeff Skirvin 	list_for_each_entry_safe(request, next_request, &errored_request_list,
11476f231ddaSDan Williams 				 completed_node) {
11486f231ddaSDan Williams 
11496f231ddaSDan Williams 		task = isci_request_access_task(request);
11506f231ddaSDan Williams 
11516f231ddaSDan Williams 		/* Use sas_task_abort */
1152d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
11536f231ddaSDan Williams 			 "%s: Error - request/task = %p/%p\n",
11546f231ddaSDan Williams 			 __func__,
11556f231ddaSDan Williams 			 request,
11566f231ddaSDan Williams 			 task);
11576f231ddaSDan Williams 
115811b00c19SJeff Skirvin 		if (task != NULL) {
115911b00c19SJeff Skirvin 
116011b00c19SJeff Skirvin 			/* Put the task into the abort path if it's not there
116111b00c19SJeff Skirvin 			 * already.
116211b00c19SJeff Skirvin 			 */
116311b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
11646f231ddaSDan Williams 				sas_task_abort(task);
116511b00c19SJeff Skirvin 
116611b00c19SJeff Skirvin 		} else {
116711b00c19SJeff Skirvin 			/* This is a case where the request has completed with a
116811b00c19SJeff Skirvin 			 * status such that it needed further target servicing,
116911b00c19SJeff Skirvin 			 * but the sas_task reference has already been removed
117011b00c19SJeff Skirvin 			 * from the request.  Since it was errored, it was not
117111b00c19SJeff Skirvin 			 * being aborted, so there is nothing to do except free
117211b00c19SJeff Skirvin 			 * it.
117311b00c19SJeff Skirvin 			 */
117411b00c19SJeff Skirvin 
1175d9dcb4baSDan Williams 			spin_lock_irq(&ihost->scic_lock);
117611b00c19SJeff Skirvin 			/* Remove the request from the remote device's list
117711b00c19SJeff Skirvin 			* of pending requests.
117811b00c19SJeff Skirvin 			*/
117911b00c19SJeff Skirvin 			list_del_init(&request->dev_node);
1180d9dcb4baSDan Williams 			isci_free_tag(ihost, request->io_tag);
1181d9dcb4baSDan Williams 			spin_unlock_irq(&ihost->scic_lock);
118211b00c19SJeff Skirvin 		}
11836f231ddaSDan Williams 	}
11846f231ddaSDan Williams 
11859b4be528SDan Williams 	/* the coalesence timeout doubles at each encoding step, so
11869b4be528SDan Williams 	 * update it based on the ilog2 value of the outstanding requests
11879b4be528SDan Williams 	 */
11889b4be528SDan Williams 	active = isci_tci_active(ihost);
11899b4be528SDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, active) |
11909b4be528SDan Williams 	       SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
11919b4be528SDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
11926f231ddaSDan Williams }
11936f231ddaSDan Williams 
1194cc9203bfSDan Williams /**
119589a7301fSDan Williams  * sci_controller_stop() - This method will stop an individual controller
1196cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1197cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1198cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1199cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1200cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1201cc9203bfSDan Williams  *    the hardware is halted.
1202cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1203cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1204cc9203bfSDan Williams  *    stop operation should complete.
1205cc9203bfSDan Williams  *
1206cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1207cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1208cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1209cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1210cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1211cc9203bfSDan Williams  */
121289a7301fSDan Williams static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
1213cc9203bfSDan Williams {
1214d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
121514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
121614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1217cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1218cc9203bfSDan Williams 	}
1219cc9203bfSDan Williams 
1220d9dcb4baSDan Williams 	sci_mod_timer(&ihost->timer, timeout);
1221d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_STOPPING);
1222cc9203bfSDan Williams 	return SCI_SUCCESS;
1223cc9203bfSDan Williams }
1224cc9203bfSDan Williams 
1225cc9203bfSDan Williams /**
122689a7301fSDan Williams  * sci_controller_reset() - This method will reset the supplied core
1227cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1228cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1229cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1230cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1231cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1232cc9203bfSDan Williams  *
1233cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1234cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1235cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1236cc9203bfSDan Williams  */
123789a7301fSDan Williams static enum sci_status sci_controller_reset(struct isci_host *ihost)
1238cc9203bfSDan Williams {
1239d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
1240e301370aSEdmund Nadolski 	case SCIC_RESET:
1241e301370aSEdmund Nadolski 	case SCIC_READY:
1242e301370aSEdmund Nadolski 	case SCIC_STOPPED:
1243e301370aSEdmund Nadolski 	case SCIC_FAILED:
1244cc9203bfSDan Williams 		/*
1245cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1246cc9203bfSDan Williams 		 * perform the state transition.
1247cc9203bfSDan Williams 		 */
1248d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_RESETTING);
1249cc9203bfSDan Williams 		return SCI_SUCCESS;
1250cc9203bfSDan Williams 	default:
125114e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
125214e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
1253cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1254cc9203bfSDan Williams 	}
1255cc9203bfSDan Williams }
1256cc9203bfSDan Williams 
12570cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
12586f231ddaSDan Williams {
12596f231ddaSDan Williams 	int i;
12606f231ddaSDan Williams 
1261ad4f4c1dSDan Williams 	/* disable output data selects */
1262ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
1263ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
1264ad4f4c1dSDan Williams 
12650cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopping);
12666f231ddaSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++) {
1267e531381eSDan Williams 		struct isci_port *iport = &ihost->ports[i];
12680cf89d1dSDan Williams 		struct isci_remote_device *idev, *d;
12690cf89d1dSDan Williams 
1270e531381eSDan Williams 		list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
1271209fae14SDan Williams 			if (test_bit(IDEV_ALLOCATED, &idev->flags))
12726ad31fecSDan Williams 				isci_remote_device_stop(ihost, idev);
12736f231ddaSDan Williams 		}
12746f231ddaSDan Williams 	}
12756f231ddaSDan Williams 
12760cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
12777c40a803SDan Williams 
12787c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
127989a7301fSDan Williams 	sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
12807c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
12817c40a803SDan Williams 
12820cf89d1dSDan Williams 	wait_for_stop(ihost);
1283ad4f4c1dSDan Williams 
1284ad4f4c1dSDan Williams 	/* disable sgpio: where the above wait should give time for the
1285ad4f4c1dSDan Williams 	 * enclosure to sample the gpios going inactive
1286ad4f4c1dSDan Williams 	 */
1287ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
1288ad4f4c1dSDan Williams 
128989a7301fSDan Williams 	sci_controller_reset(ihost);
12905553ba2bSEdmund Nadolski 
12915553ba2bSEdmund Nadolski 	/* Cancel any/all outstanding port timers */
1292d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
1293ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[i];
1294ffe191c9SDan Williams 		del_timer_sync(&iport->timer.timer);
12955553ba2bSEdmund Nadolski 	}
12965553ba2bSEdmund Nadolski 
1297a628d478SEdmund Nadolski 	/* Cancel any/all outstanding phy timers */
1298a628d478SEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
129985280955SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
130085280955SDan Williams 		del_timer_sync(&iphy->sata_timer.timer);
1301a628d478SEdmund Nadolski 	}
1302a628d478SEdmund Nadolski 
1303d9dcb4baSDan Williams 	del_timer_sync(&ihost->port_agent.timer.timer);
1304ac0eeb4fSEdmund Nadolski 
1305d9dcb4baSDan Williams 	del_timer_sync(&ihost->power_control.timer.timer);
13060473661aSEdmund Nadolski 
1307d9dcb4baSDan Williams 	del_timer_sync(&ihost->timer.timer);
13086cb5853dSEdmund Nadolski 
1309d9dcb4baSDan Williams 	del_timer_sync(&ihost->phy_timer.timer);
13106f231ddaSDan Williams }
13116f231ddaSDan Williams 
13126f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
13136f231ddaSDan Williams {
13146f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13156f231ddaSDan Williams 	int id = isci_host->id;
13166f231ddaSDan Williams 
13176f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
13186f231ddaSDan Williams }
13196f231ddaSDan Williams 
13206f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
13216f231ddaSDan Williams {
13226f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13236f231ddaSDan Williams 	int id = isci_host->id;
13246f231ddaSDan Williams 
13256f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13266f231ddaSDan Williams }
13276f231ddaSDan Williams 
132889a7301fSDan Williams static void isci_user_parameters_get(struct sci_user_parameters *u)
1329b5f18a20SDave Jiang {
1330b5f18a20SDave Jiang 	int i;
1331b5f18a20SDave Jiang 
1332b5f18a20SDave Jiang 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1333b5f18a20SDave Jiang 		struct sci_phy_user_params *u_phy = &u->phys[i];
1334b5f18a20SDave Jiang 
1335b5f18a20SDave Jiang 		u_phy->max_speed_generation = phy_gen;
1336b5f18a20SDave Jiang 
1337b5f18a20SDave Jiang 		/* we are not exporting these for now */
1338b5f18a20SDave Jiang 		u_phy->align_insertion_frequency = 0x7f;
1339b5f18a20SDave Jiang 		u_phy->in_connection_align_insertion_frequency = 0xff;
1340b5f18a20SDave Jiang 		u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1341b5f18a20SDave Jiang 	}
1342b5f18a20SDave Jiang 
1343b5f18a20SDave Jiang 	u->stp_inactivity_timeout = stp_inactive_to;
1344b5f18a20SDave Jiang 	u->ssp_inactivity_timeout = ssp_inactive_to;
1345b5f18a20SDave Jiang 	u->stp_max_occupancy_timeout = stp_max_occ_to;
1346b5f18a20SDave Jiang 	u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1347b5f18a20SDave Jiang 	u->no_outbound_task_timeout = no_outbound_task_to;
13487000f7c7SAndrzej Jakowski 	u->max_concurr_spinup = max_concurr_spinup;
1349b5f18a20SDave Jiang }
1350b5f18a20SDave Jiang 
135189a7301fSDan Williams static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
1352cc9203bfSDan Williams {
1353d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1354cc9203bfSDan Williams 
1355d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1356cc9203bfSDan Williams }
1357cc9203bfSDan Williams 
135889a7301fSDan Williams static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
1359cc9203bfSDan Williams {
1360d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1361cc9203bfSDan Williams 
1362d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1363cc9203bfSDan Williams }
1364cc9203bfSDan Williams 
1365cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1366cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1367cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1368cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1369cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1370cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1371cc9203bfSDan Williams 
1372cc9203bfSDan Williams /**
137389a7301fSDan Williams  * sci_controller_set_interrupt_coalescence() - This method allows the user to
1374cc9203bfSDan Williams  *    configure the interrupt coalescence.
1375cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1376cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1377cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1378cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1379cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1380cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1381cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1382cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1383cc9203bfSDan Williams  *    interrupt coalescing timeout.
1384cc9203bfSDan Williams  *
1385cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1386cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1387cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1388cc9203bfSDan Williams  */
1389d9dcb4baSDan Williams static enum sci_status
139089a7301fSDan Williams sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1391cc9203bfSDan Williams 					 u32 coalesce_number,
1392cc9203bfSDan Williams 					 u32 coalesce_timeout)
1393cc9203bfSDan Williams {
1394cc9203bfSDan Williams 	u8 timeout_encode = 0;
1395cc9203bfSDan Williams 	u32 min = 0;
1396cc9203bfSDan Williams 	u32 max = 0;
1397cc9203bfSDan Williams 
1398cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1399cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1400cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1401cc9203bfSDan Williams 
1402cc9203bfSDan Williams 	/*
1403cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1404cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1405cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1406cc9203bfSDan Williams 	 *              0       -        -       Disabled
1407cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1408cc9203bfSDan Williams 	 *              2       26.7     40.0
1409cc9203bfSDan Williams 	 *              3       53.3     80.0
1410cc9203bfSDan Williams 	 *              4       106.7    160.0
1411cc9203bfSDan Williams 	 *              5       213.3    320.0
1412cc9203bfSDan Williams 	 *              6       426.7    640.0
1413cc9203bfSDan Williams 	 *              7       853.3    1280.0
1414cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1415cc9203bfSDan Williams 	 *              9       3.4      5.1
1416cc9203bfSDan Williams 	 *              10      6.8      10.2
1417cc9203bfSDan Williams 	 *              11      13.7     20.5
1418cc9203bfSDan Williams 	 *              12      27.3     41.0
1419cc9203bfSDan Williams 	 *              13      54.6     81.9
1420cc9203bfSDan Williams 	 *              14      109.2    163.8
1421cc9203bfSDan Williams 	 *              15      218.5    327.7
1422cc9203bfSDan Williams 	 *              16      436.9    655.4
1423cc9203bfSDan Williams 	 *              17      873.8    1310.7
1424cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1425cc9203bfSDan Williams 	 *              19      3.5      5.2
1426cc9203bfSDan Williams 	 *              20      7.0      10.5
1427cc9203bfSDan Williams 	 *              21      14.0     21.0
1428cc9203bfSDan Williams 	 *              22      28.0     41.9
1429cc9203bfSDan Williams 	 *              23      55.9     83.9
1430cc9203bfSDan Williams 	 *              24      111.8    167.8
1431cc9203bfSDan Williams 	 *              25      223.7    335.5
1432cc9203bfSDan Williams 	 *              26      447.4    671.1
1433cc9203bfSDan Williams 	 *              27      894.8    1342.2
1434cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1435cc9203bfSDan Williams 	 *              Others Undefined */
1436cc9203bfSDan Williams 
1437cc9203bfSDan Williams 	/*
1438cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1439cc9203bfSDan Williams 	 * value for register writing. */
1440cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1441cc9203bfSDan Williams 		timeout_encode = 0;
1442cc9203bfSDan Williams 	else{
1443cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1444cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1445cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1446cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1447cc9203bfSDan Williams 
1448cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1449cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1450cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1451cc9203bfSDan Williams 		      timeout_encode++) {
1452cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1453cc9203bfSDan Williams 				break;
1454cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1455cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1456cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1457cc9203bfSDan Williams 					break;
1458cc9203bfSDan Williams 				else{
1459cc9203bfSDan Williams 					timeout_encode++;
1460cc9203bfSDan Williams 					break;
1461cc9203bfSDan Williams 				}
1462cc9203bfSDan Williams 			} else {
1463cc9203bfSDan Williams 				max = max * 2;
1464cc9203bfSDan Williams 				min = min * 2;
1465cc9203bfSDan Williams 			}
1466cc9203bfSDan Williams 		}
1467cc9203bfSDan Williams 
1468cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1469cc9203bfSDan Williams 			/* the value is out of range. */
1470cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1471cc9203bfSDan Williams 	}
1472cc9203bfSDan Williams 
1473cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1474cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1475d9dcb4baSDan Williams 	       &ihost->smu_registers->interrupt_coalesce_control);
1476cc9203bfSDan Williams 
1477cc9203bfSDan Williams 
1478d9dcb4baSDan Williams 	ihost->interrupt_coalesce_number = (u16)coalesce_number;
1479d9dcb4baSDan Williams 	ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
1480cc9203bfSDan Williams 
1481cc9203bfSDan Williams 	return SCI_SUCCESS;
1482cc9203bfSDan Williams }
1483cc9203bfSDan Williams 
1484cc9203bfSDan Williams 
148589a7301fSDan Williams static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
1486cc9203bfSDan Williams {
1487d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1488e5cc6aa4SMarcin Tomczak 	u32 val;
1489e5cc6aa4SMarcin Tomczak 
1490e5cc6aa4SMarcin Tomczak 	/* enable clock gating for power control of the scu unit */
1491e5cc6aa4SMarcin Tomczak 	val = readl(&ihost->smu_registers->clock_gating_control);
1492e5cc6aa4SMarcin Tomczak 	val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
1493e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
1494e5cc6aa4SMarcin Tomczak 		 SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
1495e5cc6aa4SMarcin Tomczak 	val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
1496e5cc6aa4SMarcin Tomczak 	writel(val, &ihost->smu_registers->clock_gating_control);
1497cc9203bfSDan Williams 
1498cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
14999b4be528SDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1500cc9203bfSDan Williams }
1501cc9203bfSDan Williams 
150289a7301fSDan Williams static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
1503cc9203bfSDan Williams {
1504d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1505cc9203bfSDan Williams 
1506cc9203bfSDan Williams 	/* disable interrupt coalescence. */
150789a7301fSDan Williams 	sci_controller_set_interrupt_coalescence(ihost, 0, 0);
1508cc9203bfSDan Williams }
1509cc9203bfSDan Williams 
151089a7301fSDan Williams static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
1511cc9203bfSDan Williams {
1512cc9203bfSDan Williams 	u32 index;
1513cc9203bfSDan Williams 	enum sci_status status;
1514cc9203bfSDan Williams 	enum sci_status phy_status;
1515cc9203bfSDan Williams 
1516cc9203bfSDan Williams 	status = SCI_SUCCESS;
1517cc9203bfSDan Williams 
1518cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
151989a7301fSDan Williams 		phy_status = sci_phy_stop(&ihost->phys[index]);
1520cc9203bfSDan Williams 
1521cc9203bfSDan Williams 		if (phy_status != SCI_SUCCESS &&
1522cc9203bfSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1523cc9203bfSDan Williams 			status = SCI_FAILURE;
1524cc9203bfSDan Williams 
1525d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1526cc9203bfSDan Williams 				 "%s: Controller stop operation failed to stop "
1527cc9203bfSDan Williams 				 "phy %d because of status %d.\n",
1528cc9203bfSDan Williams 				 __func__,
152985280955SDan Williams 				 ihost->phys[index].phy_index, phy_status);
1530cc9203bfSDan Williams 		}
1531cc9203bfSDan Williams 	}
1532cc9203bfSDan Williams 
1533cc9203bfSDan Williams 	return status;
1534cc9203bfSDan Williams }
1535cc9203bfSDan Williams 
153689a7301fSDan Williams static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
1537cc9203bfSDan Williams {
1538cc9203bfSDan Williams 	u32 index;
1539cc9203bfSDan Williams 	enum sci_status port_status;
1540cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1541cc9203bfSDan Williams 
1542d9dcb4baSDan Williams 	for (index = 0; index < ihost->logical_port_entries; index++) {
1543ffe191c9SDan Williams 		struct isci_port *iport = &ihost->ports[index];
1544cc9203bfSDan Williams 
154589a7301fSDan Williams 		port_status = sci_port_stop(iport);
1546cc9203bfSDan Williams 
1547cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1548cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1549cc9203bfSDan Williams 			status = SCI_FAILURE;
1550cc9203bfSDan Williams 
1551d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
1552cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1553cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1554cc9203bfSDan Williams 				 __func__,
1555ffe191c9SDan Williams 				 iport->logical_port_index,
1556cc9203bfSDan Williams 				 port_status);
1557cc9203bfSDan Williams 		}
1558cc9203bfSDan Williams 	}
1559cc9203bfSDan Williams 
1560cc9203bfSDan Williams 	return status;
1561cc9203bfSDan Williams }
1562cc9203bfSDan Williams 
156389a7301fSDan Williams static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
1564cc9203bfSDan Williams {
1565cc9203bfSDan Williams 	u32 index;
1566cc9203bfSDan Williams 	enum sci_status status;
1567cc9203bfSDan Williams 	enum sci_status device_status;
1568cc9203bfSDan Williams 
1569cc9203bfSDan Williams 	status = SCI_SUCCESS;
1570cc9203bfSDan Williams 
1571d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
1572d9dcb4baSDan Williams 		if (ihost->device_table[index] != NULL) {
1573cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
157489a7301fSDan Williams 			device_status = sci_remote_device_stop(ihost->device_table[index], 0);
1575cc9203bfSDan Williams 
1576cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1577cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1578d9dcb4baSDan Williams 				dev_warn(&ihost->pdev->dev,
1579cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1580cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1581cc9203bfSDan Williams 					 "status %d.\n",
1582cc9203bfSDan Williams 					 __func__,
1583d9dcb4baSDan Williams 					 ihost->device_table[index], device_status);
1584cc9203bfSDan Williams 			}
1585cc9203bfSDan Williams 		}
1586cc9203bfSDan Williams 	}
1587cc9203bfSDan Williams 
1588cc9203bfSDan Williams 	return status;
1589cc9203bfSDan Williams }
1590cc9203bfSDan Williams 
159189a7301fSDan Williams static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1592cc9203bfSDan Williams {
1593d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1594cc9203bfSDan Williams 
1595cc9203bfSDan Williams 	/* Stop all of the components for this controller */
159689a7301fSDan Williams 	sci_controller_stop_phys(ihost);
159789a7301fSDan Williams 	sci_controller_stop_ports(ihost);
159889a7301fSDan Williams 	sci_controller_stop_devices(ihost);
1599cc9203bfSDan Williams }
1600cc9203bfSDan Williams 
160189a7301fSDan Williams static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1602cc9203bfSDan Williams {
1603d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1604cc9203bfSDan Williams 
1605d9dcb4baSDan Williams 	sci_del_timer(&ihost->timer);
1606cc9203bfSDan Williams }
1607cc9203bfSDan Williams 
160889a7301fSDan Williams static void sci_controller_reset_hardware(struct isci_host *ihost)
1609cc9203bfSDan Williams {
1610cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
161189a7301fSDan Williams 	sci_controller_disable_interrupts(ihost);
1612cc9203bfSDan Williams 
1613cc9203bfSDan Williams 	/* Reset the SCU */
1614d9dcb4baSDan Williams 	writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
1615cc9203bfSDan Williams 
1616cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1617cc9203bfSDan Williams 	udelay(1000);
1618cc9203bfSDan Williams 
1619cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1620d9dcb4baSDan Williams 	writel(0x00000000, &ihost->smu_registers->completion_queue_get);
1621cc9203bfSDan Williams 
1622cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1623d9dcb4baSDan Williams 	writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
1624cc9203bfSDan Williams }
1625cc9203bfSDan Williams 
162689a7301fSDan Williams static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1627cc9203bfSDan Williams {
1628d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
1629cc9203bfSDan Williams 
163089a7301fSDan Williams 	sci_controller_reset_hardware(ihost);
1631d9dcb4baSDan Williams 	sci_change_state(&ihost->sm, SCIC_RESET);
1632cc9203bfSDan Williams }
1633cc9203bfSDan Williams 
163489a7301fSDan Williams static const struct sci_base_state sci_controller_state_table[] = {
1635e301370aSEdmund Nadolski 	[SCIC_INITIAL] = {
163689a7301fSDan Williams 		.enter_state = sci_controller_initial_state_enter,
1637cc9203bfSDan Williams 	},
1638e301370aSEdmund Nadolski 	[SCIC_RESET] = {},
1639e301370aSEdmund Nadolski 	[SCIC_INITIALIZING] = {},
1640e301370aSEdmund Nadolski 	[SCIC_INITIALIZED] = {},
1641e301370aSEdmund Nadolski 	[SCIC_STARTING] = {
164289a7301fSDan Williams 		.exit_state  = sci_controller_starting_state_exit,
1643cc9203bfSDan Williams 	},
1644e301370aSEdmund Nadolski 	[SCIC_READY] = {
164589a7301fSDan Williams 		.enter_state = sci_controller_ready_state_enter,
164689a7301fSDan Williams 		.exit_state  = sci_controller_ready_state_exit,
1647cc9203bfSDan Williams 	},
1648e301370aSEdmund Nadolski 	[SCIC_RESETTING] = {
164989a7301fSDan Williams 		.enter_state = sci_controller_resetting_state_enter,
1650cc9203bfSDan Williams 	},
1651e301370aSEdmund Nadolski 	[SCIC_STOPPING] = {
165289a7301fSDan Williams 		.enter_state = sci_controller_stopping_state_enter,
165389a7301fSDan Williams 		.exit_state = sci_controller_stopping_state_exit,
1654cc9203bfSDan Williams 	},
1655e301370aSEdmund Nadolski 	[SCIC_STOPPED] = {},
1656e301370aSEdmund Nadolski 	[SCIC_FAILED] = {}
1657cc9203bfSDan Williams };
1658cc9203bfSDan Williams 
165989a7301fSDan Williams static void sci_controller_set_default_config_parameters(struct isci_host *ihost)
1660cc9203bfSDan Williams {
1661cc9203bfSDan Williams 	/* these defaults are overridden by the platform / firmware */
1662cc9203bfSDan Williams 	u16 index;
1663cc9203bfSDan Williams 
1664cc9203bfSDan Williams 	/* Default to APC mode. */
166589a7301fSDan Williams 	ihost->oem_parameters.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1666cc9203bfSDan Williams 
1667cc9203bfSDan Williams 	/* Default to APC mode. */
16687000f7c7SAndrzej Jakowski 	ihost->oem_parameters.controller.max_concurr_spin_up = 1;
1669cc9203bfSDan Williams 
1670cc9203bfSDan Williams 	/* Default to no SSC operation. */
167189a7301fSDan Williams 	ihost->oem_parameters.controller.do_enable_ssc = false;
1672cc9203bfSDan Williams 
16739fee607fSJeff Skirvin 	/* Default to short cables on all phys. */
16749fee607fSJeff Skirvin 	ihost->oem_parameters.controller.cable_selection_mask = 0;
16759fee607fSJeff Skirvin 
1676cc9203bfSDan Williams 	/* Initialize all of the port parameter information to narrow ports. */
1677cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PORTS; index++) {
167889a7301fSDan Williams 		ihost->oem_parameters.ports[index].phy_mask = 0;
1679cc9203bfSDan Williams 	}
1680cc9203bfSDan Williams 
1681cc9203bfSDan Williams 	/* Initialize all of the phy parameter information. */
1682cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1683be168a3bSJeff Skirvin 		/* Default to 3G (i.e. Gen 2). */
1684be168a3bSJeff Skirvin 		ihost->user_parameters.phys[index].max_speed_generation =
1685be168a3bSJeff Skirvin 			SCIC_SDS_PARM_GEN2_SPEED;
1686cc9203bfSDan Williams 
1687cc9203bfSDan Williams 		/* the frequencies cannot be 0 */
168889a7301fSDan Williams 		ihost->user_parameters.phys[index].align_insertion_frequency = 0x7f;
168989a7301fSDan Williams 		ihost->user_parameters.phys[index].in_connection_align_insertion_frequency = 0xff;
169089a7301fSDan Williams 		ihost->user_parameters.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1691cc9203bfSDan Williams 
1692cc9203bfSDan Williams 		/*
1693cc9203bfSDan Williams 		 * Previous Vitesse based expanders had a arbitration issue that
1694cc9203bfSDan Williams 		 * is worked around by having the upper 32-bits of SAS address
1695cc9203bfSDan Williams 		 * with a value greater then the Vitesse company identifier.
1696cc9203bfSDan Williams 		 * Hence, usage of 0x5FCFFFFF. */
169789a7301fSDan Williams 		ihost->oem_parameters.phys[index].sas_address.low = 0x1 + ihost->id;
169889a7301fSDan Williams 		ihost->oem_parameters.phys[index].sas_address.high = 0x5FCFFFFF;
1699cc9203bfSDan Williams 	}
1700cc9203bfSDan Williams 
170189a7301fSDan Williams 	ihost->user_parameters.stp_inactivity_timeout = 5;
170289a7301fSDan Williams 	ihost->user_parameters.ssp_inactivity_timeout = 5;
170389a7301fSDan Williams 	ihost->user_parameters.stp_max_occupancy_timeout = 5;
170489a7301fSDan Williams 	ihost->user_parameters.ssp_max_occupancy_timeout = 20;
17056024d38bSMarcin Tomczak 	ihost->user_parameters.no_outbound_task_timeout = 2;
1706cc9203bfSDan Williams }
1707cc9203bfSDan Williams 
17086cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data)
17096cb5853dSEdmund Nadolski {
17106cb5853dSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1711d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1712d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
17136cb5853dSEdmund Nadolski 	unsigned long flags;
1714cc9203bfSDan Williams 
17156cb5853dSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
17166cb5853dSEdmund Nadolski 
17176cb5853dSEdmund Nadolski 	if (tmr->cancel)
17186cb5853dSEdmund Nadolski 		goto done;
17196cb5853dSEdmund Nadolski 
1720e301370aSEdmund Nadolski 	if (sm->current_state_id == SCIC_STARTING)
172189a7301fSDan Williams 		sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
1722e301370aSEdmund Nadolski 	else if (sm->current_state_id == SCIC_STOPPING) {
1723e301370aSEdmund Nadolski 		sci_change_state(sm, SCIC_FAILED);
17246cb5853dSEdmund Nadolski 		isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
17256cb5853dSEdmund Nadolski 	} else	/* / @todo Now what do we want to do in this case? */
1726d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
17276cb5853dSEdmund Nadolski 			"%s: Controller timer fired when controller was not "
17286cb5853dSEdmund Nadolski 			"in a state being timed.\n",
17296cb5853dSEdmund Nadolski 			__func__);
17306cb5853dSEdmund Nadolski 
17316cb5853dSEdmund Nadolski done:
17326cb5853dSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
17336cb5853dSEdmund Nadolski }
1734cc9203bfSDan Williams 
173589a7301fSDan Williams static enum sci_status sci_controller_construct(struct isci_host *ihost,
1736cc9203bfSDan Williams 						void __iomem *scu_base,
1737cc9203bfSDan Williams 						void __iomem *smu_base)
1738cc9203bfSDan Williams {
1739cc9203bfSDan Williams 	u8 i;
1740cc9203bfSDan Williams 
174189a7301fSDan Williams 	sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
1742cc9203bfSDan Williams 
1743d9dcb4baSDan Williams 	ihost->scu_registers = scu_base;
1744d9dcb4baSDan Williams 	ihost->smu_registers = smu_base;
1745cc9203bfSDan Williams 
174689a7301fSDan Williams 	sci_port_configuration_agent_construct(&ihost->port_agent);
1747cc9203bfSDan Williams 
1748cc9203bfSDan Williams 	/* Construct the ports for this controller */
1749cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
175089a7301fSDan Williams 		sci_port_construct(&ihost->ports[i], i, ihost);
175189a7301fSDan Williams 	sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
1752cc9203bfSDan Williams 
1753cc9203bfSDan Williams 	/* Construct the phys for this controller */
1754cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1755cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
175689a7301fSDan Williams 		sci_phy_construct(&ihost->phys[i],
1757ffe191c9SDan Williams 				  &ihost->ports[SCI_MAX_PORTS], i);
1758cc9203bfSDan Williams 	}
1759cc9203bfSDan Williams 
1760d9dcb4baSDan Williams 	ihost->invalid_phy_mask = 0;
1761cc9203bfSDan Williams 
1762d9dcb4baSDan Williams 	sci_init_timer(&ihost->timer, controller_timeout);
17636cb5853dSEdmund Nadolski 
1764cc9203bfSDan Williams 	/* Initialize the User and OEM parameters to default values. */
176589a7301fSDan Williams 	sci_controller_set_default_config_parameters(ihost);
1766cc9203bfSDan Williams 
176789a7301fSDan Williams 	return sci_controller_reset(ihost);
1768cc9203bfSDan Williams }
1769cc9203bfSDan Williams 
1770594e566aSDave Jiang int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
1771cc9203bfSDan Williams {
1772cc9203bfSDan Williams 	int i;
1773cc9203bfSDan Williams 
1774cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1775cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1776cc9203bfSDan Williams 			return -EINVAL;
1777cc9203bfSDan Williams 
1778cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1779cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1780cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1781cc9203bfSDan Williams 			return -EINVAL;
1782cc9203bfSDan Williams 
1783cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1784cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1785cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1786cc9203bfSDan Williams 				return -EINVAL;
1787cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1788cc9203bfSDan Williams 		u8 phy_mask = 0;
1789cc9203bfSDan Williams 
1790cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1791cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1792cc9203bfSDan Williams 
1793cc9203bfSDan Williams 		if (phy_mask == 0)
1794cc9203bfSDan Williams 			return -EINVAL;
1795cc9203bfSDan Williams 	} else
1796cc9203bfSDan Williams 		return -EINVAL;
1797cc9203bfSDan Williams 
17987000f7c7SAndrzej Jakowski 	if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
17997000f7c7SAndrzej Jakowski 	    oem->controller.max_concurr_spin_up < 1)
1800cc9203bfSDan Williams 		return -EINVAL;
1801cc9203bfSDan Williams 
1802594e566aSDave Jiang 	if (oem->controller.do_enable_ssc) {
1803594e566aSDave Jiang 		if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
1804594e566aSDave Jiang 			return -EINVAL;
1805594e566aSDave Jiang 
1806594e566aSDave Jiang 		if (version >= ISCI_ROM_VER_1_1) {
1807594e566aSDave Jiang 			u8 test = oem->controller.ssc_sata_tx_spread_level;
1808594e566aSDave Jiang 
1809594e566aSDave Jiang 			switch (test) {
1810594e566aSDave Jiang 			case 0:
1811594e566aSDave Jiang 			case 2:
1812594e566aSDave Jiang 			case 3:
1813594e566aSDave Jiang 			case 6:
1814594e566aSDave Jiang 			case 7:
1815594e566aSDave Jiang 				break;
1816594e566aSDave Jiang 			default:
1817594e566aSDave Jiang 				return -EINVAL;
1818594e566aSDave Jiang 			}
1819594e566aSDave Jiang 
1820594e566aSDave Jiang 			test = oem->controller.ssc_sas_tx_spread_level;
1821594e566aSDave Jiang 			if (oem->controller.ssc_sas_tx_type == 0) {
1822594e566aSDave Jiang 				switch (test) {
1823594e566aSDave Jiang 				case 0:
1824594e566aSDave Jiang 				case 2:
1825594e566aSDave Jiang 				case 3:
1826594e566aSDave Jiang 					break;
1827594e566aSDave Jiang 				default:
1828594e566aSDave Jiang 					return -EINVAL;
1829594e566aSDave Jiang 				}
1830594e566aSDave Jiang 			} else if (oem->controller.ssc_sas_tx_type == 1) {
1831594e566aSDave Jiang 				switch (test) {
1832594e566aSDave Jiang 				case 0:
1833594e566aSDave Jiang 				case 3:
1834594e566aSDave Jiang 				case 6:
1835594e566aSDave Jiang 					break;
1836594e566aSDave Jiang 				default:
1837594e566aSDave Jiang 					return -EINVAL;
1838594e566aSDave Jiang 				}
1839594e566aSDave Jiang 			}
1840594e566aSDave Jiang 		}
1841594e566aSDave Jiang 	}
1842594e566aSDave Jiang 
1843cc9203bfSDan Williams 	return 0;
1844cc9203bfSDan Williams }
1845cc9203bfSDan Williams 
184689a7301fSDan Williams static enum sci_status sci_oem_parameters_set(struct isci_host *ihost)
1847cc9203bfSDan Williams {
1848d9dcb4baSDan Williams 	u32 state = ihost->sm.current_state_id;
1849594e566aSDave Jiang 	struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
1850cc9203bfSDan Williams 
1851e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
1852e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
1853e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
18546d7938f4SDave Jiang 		u8 oem_version = pci_info->orom ? pci_info->orom->hdr.version :
18556d7938f4SDave Jiang 			ISCI_ROM_VER_1_0;
1856cc9203bfSDan Williams 
1857594e566aSDave Jiang 		if (sci_oem_parameters_validate(&ihost->oem_parameters,
18586d7938f4SDave Jiang 						oem_version))
1859cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1860cc9203bfSDan Williams 
1861cc9203bfSDan Williams 		return SCI_SUCCESS;
1862cc9203bfSDan Williams 	}
1863cc9203bfSDan Williams 
1864cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
1865cc9203bfSDan Williams }
1866cc9203bfSDan Williams 
18677000f7c7SAndrzej Jakowski static u8 max_spin_up(struct isci_host *ihost)
18687000f7c7SAndrzej Jakowski {
18697000f7c7SAndrzej Jakowski 	if (ihost->user_parameters.max_concurr_spinup)
18707000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->user_parameters.max_concurr_spinup,
18717000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
18727000f7c7SAndrzej Jakowski 	else
18737000f7c7SAndrzej Jakowski 		return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
18747000f7c7SAndrzej Jakowski 			     MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
18757000f7c7SAndrzej Jakowski }
18767000f7c7SAndrzej Jakowski 
18770473661aSEdmund Nadolski static void power_control_timeout(unsigned long data)
1878cc9203bfSDan Williams {
18790473661aSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
1880d9dcb4baSDan Williams 	struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
188185280955SDan Williams 	struct isci_phy *iphy;
18820473661aSEdmund Nadolski 	unsigned long flags;
18830473661aSEdmund Nadolski 	u8 i;
1884cc9203bfSDan Williams 
18850473661aSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
1886cc9203bfSDan Williams 
18870473661aSEdmund Nadolski 	if (tmr->cancel)
18880473661aSEdmund Nadolski 		goto done;
1889cc9203bfSDan Williams 
1890d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
1891cc9203bfSDan Williams 
1892d9dcb4baSDan Williams 	if (ihost->power_control.phys_waiting == 0) {
1893d9dcb4baSDan Williams 		ihost->power_control.timer_started = false;
18940473661aSEdmund Nadolski 		goto done;
18950473661aSEdmund Nadolski 	}
1896cc9203bfSDan Williams 
18970473661aSEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
18980473661aSEdmund Nadolski 
1899d9dcb4baSDan Williams 		if (ihost->power_control.phys_waiting == 0)
19000473661aSEdmund Nadolski 			break;
19010473661aSEdmund Nadolski 
1902d9dcb4baSDan Williams 		iphy = ihost->power_control.requesters[i];
190385280955SDan Williams 		if (iphy == NULL)
19040473661aSEdmund Nadolski 			continue;
19050473661aSEdmund Nadolski 
19067000f7c7SAndrzej Jakowski 		if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
19070473661aSEdmund Nadolski 			break;
19080473661aSEdmund Nadolski 
1909d9dcb4baSDan Williams 		ihost->power_control.requesters[i] = NULL;
1910d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
1911d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
191289a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1913be778341SMarcin Tomczak 
1914*c79dd80dSDan Williams 		if (iphy->protocol == SAS_PROTOCOL_SSP) {
1915be778341SMarcin Tomczak 			u8 j;
1916be778341SMarcin Tomczak 
1917be778341SMarcin Tomczak 			for (j = 0; j < SCI_MAX_PHYS; j++) {
1918be778341SMarcin Tomczak 				struct isci_phy *requester = ihost->power_control.requesters[j];
1919be778341SMarcin Tomczak 
1920be778341SMarcin Tomczak 				/*
1921be778341SMarcin Tomczak 				 * Search the power_control queue to see if there are other phys
1922be778341SMarcin Tomczak 				 * attached to the same remote device. If found, take all of
1923be778341SMarcin Tomczak 				 * them out of await_sas_power state.
1924be778341SMarcin Tomczak 				 */
1925be778341SMarcin Tomczak 				if (requester != NULL && requester != iphy) {
1926be778341SMarcin Tomczak 					u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
1927be778341SMarcin Tomczak 							  iphy->frame_rcvd.iaf.sas_addr,
1928be778341SMarcin Tomczak 							  sizeof(requester->frame_rcvd.iaf.sas_addr));
1929be778341SMarcin Tomczak 
1930be778341SMarcin Tomczak 					if (other == 0) {
1931be778341SMarcin Tomczak 						ihost->power_control.requesters[j] = NULL;
1932be778341SMarcin Tomczak 						ihost->power_control.phys_waiting--;
1933be778341SMarcin Tomczak 						sci_phy_consume_power_handler(requester);
1934be778341SMarcin Tomczak 					}
1935be778341SMarcin Tomczak 				}
1936be778341SMarcin Tomczak 			}
1937be778341SMarcin Tomczak 		}
1938cc9203bfSDan Williams 	}
1939cc9203bfSDan Williams 
1940cc9203bfSDan Williams 	/*
1941cc9203bfSDan Williams 	 * It doesn't matter if the power list is empty, we need to start the
1942cc9203bfSDan Williams 	 * timer in case another phy becomes ready.
1943cc9203bfSDan Williams 	 */
19440473661aSEdmund Nadolski 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1945d9dcb4baSDan Williams 	ihost->power_control.timer_started = true;
19460473661aSEdmund Nadolski 
19470473661aSEdmund Nadolski done:
19480473661aSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1949cc9203bfSDan Williams }
1950cc9203bfSDan Williams 
195189a7301fSDan Williams void sci_controller_power_control_queue_insert(struct isci_host *ihost,
195285280955SDan Williams 					       struct isci_phy *iphy)
1953cc9203bfSDan Williams {
195485280955SDan Williams 	BUG_ON(iphy == NULL);
1955cc9203bfSDan Williams 
19567000f7c7SAndrzej Jakowski 	if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
1957d9dcb4baSDan Williams 		ihost->power_control.phys_granted_power++;
195889a7301fSDan Williams 		sci_phy_consume_power_handler(iphy);
1959cc9203bfSDan Williams 
1960cc9203bfSDan Williams 		/*
1961cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1962cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1963cc9203bfSDan Williams 		 */
1964d9dcb4baSDan Williams 		if (ihost->power_control.timer_started)
1965d9dcb4baSDan Williams 			sci_del_timer(&ihost->power_control.timer);
19660473661aSEdmund Nadolski 
1967d9dcb4baSDan Williams 		sci_mod_timer(&ihost->power_control.timer,
19680473661aSEdmund Nadolski 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1969d9dcb4baSDan Williams 		ihost->power_control.timer_started = true;
19700473661aSEdmund Nadolski 
1971cc9203bfSDan Williams 	} else {
1972be778341SMarcin Tomczak 		/*
1973be778341SMarcin Tomczak 		 * There are phys, attached to the same sas address as this phy, are
1974be778341SMarcin Tomczak 		 * already in READY state, this phy don't need wait.
1975be778341SMarcin Tomczak 		 */
1976be778341SMarcin Tomczak 		u8 i;
1977be778341SMarcin Tomczak 		struct isci_phy *current_phy;
1978be778341SMarcin Tomczak 
1979be778341SMarcin Tomczak 		for (i = 0; i < SCI_MAX_PHYS; i++) {
1980be778341SMarcin Tomczak 			u8 other;
1981be778341SMarcin Tomczak 			current_phy = &ihost->phys[i];
1982be778341SMarcin Tomczak 
1983be778341SMarcin Tomczak 			other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
1984be778341SMarcin Tomczak 				       iphy->frame_rcvd.iaf.sas_addr,
1985be778341SMarcin Tomczak 				       sizeof(current_phy->frame_rcvd.iaf.sas_addr));
1986be778341SMarcin Tomczak 
1987be778341SMarcin Tomczak 			if (current_phy->sm.current_state_id == SCI_PHY_READY &&
1988*c79dd80dSDan Williams 			    current_phy->protocol == SAS_PROTOCOL_SSP &&
1989be778341SMarcin Tomczak 			    other == 0) {
1990be778341SMarcin Tomczak 				sci_phy_consume_power_handler(iphy);
1991be778341SMarcin Tomczak 				break;
1992be778341SMarcin Tomczak 			}
1993be778341SMarcin Tomczak 		}
1994be778341SMarcin Tomczak 
1995be778341SMarcin Tomczak 		if (i == SCI_MAX_PHYS) {
1996cc9203bfSDan Williams 			/* Add the phy in the waiting list */
1997d9dcb4baSDan Williams 			ihost->power_control.requesters[iphy->phy_index] = iphy;
1998d9dcb4baSDan Williams 			ihost->power_control.phys_waiting++;
1999cc9203bfSDan Williams 		}
2000cc9203bfSDan Williams 	}
2001be778341SMarcin Tomczak }
2002cc9203bfSDan Williams 
200389a7301fSDan Williams void sci_controller_power_control_queue_remove(struct isci_host *ihost,
200485280955SDan Williams 					       struct isci_phy *iphy)
2005cc9203bfSDan Williams {
200685280955SDan Williams 	BUG_ON(iphy == NULL);
2007cc9203bfSDan Williams 
200889a7301fSDan Williams 	if (ihost->power_control.requesters[iphy->phy_index])
2009d9dcb4baSDan Williams 		ihost->power_control.phys_waiting--;
2010cc9203bfSDan Williams 
2011d9dcb4baSDan Williams 	ihost->power_control.requesters[iphy->phy_index] = NULL;
2012cc9203bfSDan Williams }
2013cc9203bfSDan Williams 
2014afd13a1fSJeff Skirvin static int is_long_cable(int phy, unsigned char selection_byte)
2015afd13a1fSJeff Skirvin {
20169fee607fSJeff Skirvin 	return !!(selection_byte & (1 << phy));
2017afd13a1fSJeff Skirvin }
2018afd13a1fSJeff Skirvin 
2019afd13a1fSJeff Skirvin static int is_medium_cable(int phy, unsigned char selection_byte)
2020afd13a1fSJeff Skirvin {
20219fee607fSJeff Skirvin 	return !!(selection_byte & (1 << (phy + 4)));
20229fee607fSJeff Skirvin }
20239fee607fSJeff Skirvin 
20249fee607fSJeff Skirvin static enum cable_selections decode_selection_byte(
20259fee607fSJeff Skirvin 	int phy,
20269fee607fSJeff Skirvin 	unsigned char selection_byte)
20279fee607fSJeff Skirvin {
20289fee607fSJeff Skirvin 	return ((selection_byte & (1 << phy)) ? 1 : 0)
20299fee607fSJeff Skirvin 		+ (selection_byte & (1 << (phy + 4)) ? 2 : 0);
20309fee607fSJeff Skirvin }
20319fee607fSJeff Skirvin 
20329fee607fSJeff Skirvin static unsigned char *to_cable_select(struct isci_host *ihost)
20339fee607fSJeff Skirvin {
20349fee607fSJeff Skirvin 	if (is_cable_select_overridden())
20359fee607fSJeff Skirvin 		return ((unsigned char *)&cable_selection_override)
20369fee607fSJeff Skirvin 			+ ihost->id;
20379fee607fSJeff Skirvin 	else
20389fee607fSJeff Skirvin 		return &ihost->oem_parameters.controller.cable_selection_mask;
20399fee607fSJeff Skirvin }
20409fee607fSJeff Skirvin 
20419fee607fSJeff Skirvin enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
20429fee607fSJeff Skirvin {
20439fee607fSJeff Skirvin 	return decode_selection_byte(phy, *to_cable_select(ihost));
20449fee607fSJeff Skirvin }
20459fee607fSJeff Skirvin 
20469fee607fSJeff Skirvin char *lookup_cable_names(enum cable_selections selection)
20479fee607fSJeff Skirvin {
20489fee607fSJeff Skirvin 	static char *cable_names[] = {
20499fee607fSJeff Skirvin 		[short_cable]     = "short",
20509fee607fSJeff Skirvin 		[long_cable]      = "long",
20519fee607fSJeff Skirvin 		[medium_cable]    = "medium",
20529fee607fSJeff Skirvin 		[undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
20539fee607fSJeff Skirvin 	};
20549fee607fSJeff Skirvin 	return (selection <= undefined_cable) ? cable_names[selection]
20559fee607fSJeff Skirvin 					      : cable_names[undefined_cable];
2056afd13a1fSJeff Skirvin }
2057afd13a1fSJeff Skirvin 
2058cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
2059cc9203bfSDan Williams 
206089a7301fSDan Williams static void sci_controller_afe_initialization(struct isci_host *ihost)
2061cc9203bfSDan Williams {
20622e5da889SDan Williams 	struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
206389a7301fSDan Williams 	const struct sci_oem_params *oem = &ihost->oem_parameters;
2064dc00c8b6SDan Williams 	struct pci_dev *pdev = ihost->pdev;
2065cc9203bfSDan Williams 	u32 afe_status;
2066cc9203bfSDan Williams 	u32 phy_id;
20679fee607fSJeff Skirvin 	unsigned char cable_selection_mask = *to_cable_select(ihost);
2068cc9203bfSDan Williams 
2069cc9203bfSDan Williams 	/* Clear DFX Status registers */
20702e5da889SDan Williams 	writel(0x0081000f, &afe->afe_dfx_master_control0);
2071cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2072cc9203bfSDan Williams 
2073afd13a1fSJeff Skirvin 	if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
2074cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
20752e5da889SDan Williams 		 * Timer, PM Stagger Timer
20762e5da889SDan Williams 		 */
2077afd13a1fSJeff Skirvin 		writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
2078cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2079cc9203bfSDan Williams 	}
2080cc9203bfSDan Williams 
2081cc9203bfSDan Williams 	/* Configure bias currents to normal */
2082dc00c8b6SDan Williams 	if (is_a2(pdev))
20832e5da889SDan Williams 		writel(0x00005A00, &afe->afe_bias_control);
2084dc00c8b6SDan Williams 	else if (is_b0(pdev) || is_c0(pdev))
20852e5da889SDan Williams 		writel(0x00005F00, &afe->afe_bias_control);
2086afd13a1fSJeff Skirvin 	else if (is_c1(pdev))
2087afd13a1fSJeff Skirvin 		writel(0x00005500, &afe->afe_bias_control);
2088cc9203bfSDan Williams 
2089cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2090cc9203bfSDan Williams 
2091cc9203bfSDan Williams 	/* Enable PLL */
2092afd13a1fSJeff Skirvin 	if (is_a2(pdev))
20932e5da889SDan Williams 		writel(0x80040908, &afe->afe_pll_control0);
2094afd13a1fSJeff Skirvin 	else if (is_b0(pdev) || is_c0(pdev))
2095afd13a1fSJeff Skirvin 		writel(0x80040A08, &afe->afe_pll_control0);
2096afd13a1fSJeff Skirvin 	else if (is_c1(pdev)) {
2097afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
2098afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
2099afd13a1fSJeff Skirvin 		writel(0x00000B08, &afe->afe_pll_control0);
2100afd13a1fSJeff Skirvin 		udelay(AFE_REGISTER_WRITE_DELAY);
2101afd13a1fSJeff Skirvin 		writel(0x80000B08, &afe->afe_pll_control0);
2102afd13a1fSJeff Skirvin 	}
2103cc9203bfSDan Williams 
2104cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2105cc9203bfSDan Williams 
2106cc9203bfSDan Williams 	/* Wait for the PLL to lock */
2107cc9203bfSDan Williams 	do {
21082e5da889SDan Williams 		afe_status = readl(&afe->afe_common_block_status);
2109cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2110cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
2111cc9203bfSDan Williams 
2112dc00c8b6SDan Williams 	if (is_a2(pdev)) {
21132e5da889SDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76
21142e5da889SDan Williams 		 * us to 50 us)
21152e5da889SDan Williams 		 */
21162e5da889SDan Williams 		writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
2117cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2118cc9203bfSDan Williams 	}
2119cc9203bfSDan Williams 
2120cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
21212e5da889SDan Williams 		struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id];
2122cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2123afd13a1fSJeff Skirvin 		int cable_length_long =
2124afd13a1fSJeff Skirvin 			is_long_cable(phy_id, cable_selection_mask);
2125afd13a1fSJeff Skirvin 		int cable_length_medium =
2126afd13a1fSJeff Skirvin 			is_medium_cable(phy_id, cable_selection_mask);
2127cc9203bfSDan Williams 
2128afd13a1fSJeff Skirvin 		if (is_a2(pdev)) {
21292e5da889SDan Williams 			/* All defaults, except the Receive Word
21302e5da889SDan Williams 			 * Alignament/Comma Detect Enable....(0xe800)
21312e5da889SDan Williams 			 */
21322e5da889SDan Williams 			writel(0x00004512, &xcvr->afe_xcvr_control0);
2133cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2134cc9203bfSDan Williams 
21352e5da889SDan Williams 			writel(0x0050100F, &xcvr->afe_xcvr_control1);
2136cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2137afd13a1fSJeff Skirvin 		} else if (is_b0(pdev)) {
2138afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2139afd13a1fSJeff Skirvin 			writel(0x00030000, &xcvr->afe_tx_ssc_control);
2140afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2141afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2142afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2143afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2144afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2145afd13a1fSJeff Skirvin 
2146afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2147afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2148afd13a1fSJeff Skirvin 			 */
2149afd13a1fSJeff Skirvin 			writel(0x00014500, &xcvr->afe_xcvr_control0);
2150afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2151afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2152afd13a1fSJeff Skirvin 			/* Configure transmitter SSC parameters */
2153afd13a1fSJeff Skirvin 			writel(0x00010202, &xcvr->afe_tx_ssc_control);
2154afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2155afd13a1fSJeff Skirvin 
2156afd13a1fSJeff Skirvin 			/* All defaults, except the Receive Word
2157afd13a1fSJeff Skirvin 			 * Alignament/Comma Detect Enable....(0xe800)
2158afd13a1fSJeff Skirvin 			 */
2159afd13a1fSJeff Skirvin 			writel(0x0001C500, &xcvr->afe_xcvr_control0);
2160afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2161cc9203bfSDan Williams 		}
2162cc9203bfSDan Williams 
2163afd13a1fSJeff Skirvin 		/* Power up TX and RX out from power down (PWRDNTX and
2164afd13a1fSJeff Skirvin 		 * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
21652e5da889SDan Williams 		 */
2166dc00c8b6SDan Williams 		if (is_a2(pdev))
21672e5da889SDan Williams 			writel(0x000003F0, &xcvr->afe_channel_control);
2168dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
21692e5da889SDan Williams 			writel(0x000003D7, &xcvr->afe_channel_control);
2170cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2171afd13a1fSJeff Skirvin 
21722e5da889SDan Williams 			writel(0x000003D4, &xcvr->afe_channel_control);
2173afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
21742e5da889SDan Williams 			writel(0x000001E7, &xcvr->afe_channel_control);
2175dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2176afd13a1fSJeff Skirvin 
21772e5da889SDan Williams 			writel(0x000001E4, &xcvr->afe_channel_control);
2178afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2179afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F7 : 0x000001F7,
2180afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2181afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2182afd13a1fSJeff Skirvin 
2183afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x000002F4 : 0x000001F4,
2184afd13a1fSJeff Skirvin 			       &xcvr->afe_channel_control);
2185cc9203bfSDan Williams 		}
2186cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2187cc9203bfSDan Williams 
2188dc00c8b6SDan Williams 		if (is_a2(pdev)) {
2189cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
21902e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2191cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2192cc9203bfSDan Williams 		}
2193cc9203bfSDan Williams 
2194afd13a1fSJeff Skirvin 		if (is_a2(pdev) || is_b0(pdev))
2195afd13a1fSJeff Skirvin 			/* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
2196afd13a1fSJeff Skirvin 			 * TPD=0x0(TX Power On), RDD=0x0(RX Detect
2197afd13a1fSJeff Skirvin 			 * Enabled) ....(0xe800)
2198afd13a1fSJeff Skirvin 			 */
21992e5da889SDan Williams 			writel(0x00004100, &xcvr->afe_xcvr_control0);
2200afd13a1fSJeff Skirvin 		else if (is_c0(pdev))
2201afd13a1fSJeff Skirvin 			writel(0x00014100, &xcvr->afe_xcvr_control0);
2202afd13a1fSJeff Skirvin 		else if (is_c1(pdev))
2203afd13a1fSJeff Skirvin 			writel(0x0001C100, &xcvr->afe_xcvr_control0);
2204cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2205cc9203bfSDan Williams 
2206cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2207dc00c8b6SDan Williams 		if (is_a2(pdev))
22082e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2209dc00c8b6SDan Williams 		else if (is_b0(pdev)) {
22102e5da889SDan Williams 			writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
2211cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2212cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
22132e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2214afd13a1fSJeff Skirvin 		} else if (is_c0(pdev)) {
2215afd13a1fSJeff Skirvin 			writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
2216dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2217dbb0743aSAdam Gruchala 
22182e5da889SDan Williams 			writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
2219dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2220dbb0743aSAdam Gruchala 
2221dbb0743aSAdam Gruchala 			/* Enable TX equalization (0xe824) */
22222e5da889SDan Williams 			writel(0x00040000, &xcvr->afe_tx_control);
2223afd13a1fSJeff Skirvin 		} else if (is_c1(pdev)) {
2224afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x01500C0C :
2225afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x01400C0D : 0x02400C0D,
2226afd13a1fSJeff Skirvin 			       &xcvr->afe_xcvr_control1);
2227afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2228afd13a1fSJeff Skirvin 
2229afd13a1fSJeff Skirvin 			writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
2230afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2231afd13a1fSJeff Skirvin 
2232afd13a1fSJeff Skirvin 			writel(cable_length_long ? 0x33091C1F :
2233afd13a1fSJeff Skirvin 			       cable_length_medium ? 0x3315181F : 0x2B17161F,
2234afd13a1fSJeff Skirvin 			       &xcvr->afe_rx_ssc_control0);
2235afd13a1fSJeff Skirvin 			udelay(AFE_REGISTER_WRITE_DELAY);
2236afd13a1fSJeff Skirvin 
2237afd13a1fSJeff Skirvin 			/* Enable TX equalization (0xe824) */
2238afd13a1fSJeff Skirvin 			writel(0x00040000, &xcvr->afe_tx_control);
2239cc9203bfSDan Williams 		}
2240dbb0743aSAdam Gruchala 
2241cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2242cc9203bfSDan Williams 
22432e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
2244cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2245cc9203bfSDan Williams 
22462e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
2247cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2248cc9203bfSDan Williams 
22492e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
2250cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2251cc9203bfSDan Williams 
22522e5da889SDan Williams 		writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
2253cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2254cc9203bfSDan Williams 	}
2255cc9203bfSDan Williams 
2256cc9203bfSDan Williams 	/* Transfer control to the PEs */
22572e5da889SDan Williams 	writel(0x00010f00, &afe->afe_dfx_master_control0);
2258cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2259cc9203bfSDan Williams }
2260cc9203bfSDan Williams 
226189a7301fSDan Williams static void sci_controller_initialize_power_control(struct isci_host *ihost)
2262cc9203bfSDan Williams {
2263d9dcb4baSDan Williams 	sci_init_timer(&ihost->power_control.timer, power_control_timeout);
2264cc9203bfSDan Williams 
2265d9dcb4baSDan Williams 	memset(ihost->power_control.requesters, 0,
2266d9dcb4baSDan Williams 	       sizeof(ihost->power_control.requesters));
2267cc9203bfSDan Williams 
2268d9dcb4baSDan Williams 	ihost->power_control.phys_waiting = 0;
2269d9dcb4baSDan Williams 	ihost->power_control.phys_granted_power = 0;
2270cc9203bfSDan Williams }
2271cc9203bfSDan Williams 
227289a7301fSDan Williams static enum sci_status sci_controller_initialize(struct isci_host *ihost)
2273cc9203bfSDan Williams {
2274d9dcb4baSDan Williams 	struct sci_base_state_machine *sm = &ihost->sm;
22757c78da31SDan Williams 	enum sci_status result = SCI_FAILURE;
22767c78da31SDan Williams 	unsigned long i, state, val;
2277cc9203bfSDan Williams 
2278d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_RESET) {
227914e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
228014e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2281cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2282cc9203bfSDan Williams 	}
2283cc9203bfSDan Williams 
2284e301370aSEdmund Nadolski 	sci_change_state(sm, SCIC_INITIALIZING);
2285cc9203bfSDan Williams 
2286d9dcb4baSDan Williams 	sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
2287bb3dbdf6SEdmund Nadolski 
2288d9dcb4baSDan Williams 	ihost->next_phy_to_start = 0;
2289d9dcb4baSDan Williams 	ihost->phy_startup_timer_pending = false;
2290cc9203bfSDan Williams 
229189a7301fSDan Williams 	sci_controller_initialize_power_control(ihost);
2292cc9203bfSDan Williams 
2293cc9203bfSDan Williams 	/*
2294cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2295cc9203bfSDan Williams 	 * program the AFE registers.
2296cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2297cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
229889a7301fSDan Williams 	sci_controller_afe_initialization(ihost);
2299cc9203bfSDan Williams 
2300cc9203bfSDan Williams 
2301cc9203bfSDan Williams 	/* Take the hardware out of reset */
2302d9dcb4baSDan Williams 	writel(0, &ihost->smu_registers->soft_reset_control);
2303cc9203bfSDan Williams 
2304cc9203bfSDan Williams 	/*
2305cc9203bfSDan Williams 	 * / @todo Provide meaningfull error code for hardware failure
2306cc9203bfSDan Williams 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
23077c78da31SDan Williams 	for (i = 100; i >= 1; i--) {
23087c78da31SDan Williams 		u32 status;
2309cc9203bfSDan Williams 
2310cc9203bfSDan Williams 		/* Loop until the hardware reports success */
2311cc9203bfSDan Williams 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2312d9dcb4baSDan Williams 		status = readl(&ihost->smu_registers->control_status);
2313cc9203bfSDan Williams 
23147c78da31SDan Williams 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
23157c78da31SDan Williams 			break;
2316cc9203bfSDan Williams 	}
23177c78da31SDan Williams 	if (i == 0)
23187c78da31SDan Williams 		goto out;
2319cc9203bfSDan Williams 
2320cc9203bfSDan Williams 	/*
2321cc9203bfSDan Williams 	 * Determine what are the actaul device capacities that the
2322cc9203bfSDan Williams 	 * hardware will support */
2323d9dcb4baSDan Williams 	val = readl(&ihost->smu_registers->device_context_capacity);
2324cc9203bfSDan Williams 
23257c78da31SDan Williams 	/* Record the smaller of the two capacity values */
2326d9dcb4baSDan Williams 	ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2327d9dcb4baSDan Williams 	ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2328d9dcb4baSDan Williams 	ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2329cc9203bfSDan Williams 
2330cc9203bfSDan Williams 	/*
2331cc9203bfSDan Williams 	 * Make all PEs that are unassigned match up with the
2332cc9203bfSDan Williams 	 * logical ports
2333cc9203bfSDan Williams 	 */
2334d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
2335cc9203bfSDan Williams 		struct scu_port_task_scheduler_group_registers __iomem
2336d9dcb4baSDan Williams 			*ptsg = &ihost->scu_registers->peg0.ptsg;
2337cc9203bfSDan Williams 
23387c78da31SDan Williams 		writel(i, &ptsg->protocol_engine[i]);
2339cc9203bfSDan Williams 	}
2340cc9203bfSDan Williams 
2341cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2342d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.pdma_configuration);
23437c78da31SDan Williams 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2344d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.pdma_configuration);
2345cc9203bfSDan Williams 
2346d9dcb4baSDan Williams 	val = readl(&ihost->scu_registers->sdma.cdma_configuration);
23477c78da31SDan Williams 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2348d9dcb4baSDan Williams 	writel(val, &ihost->scu_registers->sdma.cdma_configuration);
2349cc9203bfSDan Williams 
2350cc9203bfSDan Williams 	/*
2351cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2352cc9203bfSDan Williams 	 * are accessed during the port initialization.
2353cc9203bfSDan Williams 	 */
23547c78da31SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
235589a7301fSDan Williams 		result = sci_phy_initialize(&ihost->phys[i],
2356d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].tl,
2357d9dcb4baSDan Williams 					    &ihost->scu_registers->peg0.pe[i].ll);
23587c78da31SDan Williams 		if (result != SCI_SUCCESS)
23597c78da31SDan Williams 			goto out;
2360cc9203bfSDan Williams 	}
2361cc9203bfSDan Williams 
2362d9dcb4baSDan Williams 	for (i = 0; i < ihost->logical_port_entries; i++) {
236389a7301fSDan Williams 		struct isci_port *iport = &ihost->ports[i];
23647c78da31SDan Williams 
236589a7301fSDan Williams 		iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
236689a7301fSDan Williams 		iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
236789a7301fSDan Williams 		iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
2368cc9203bfSDan Williams 	}
2369cc9203bfSDan Williams 
237089a7301fSDan Williams 	result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
2371cc9203bfSDan Williams 
23727c78da31SDan Williams  out:
2373cc9203bfSDan Williams 	/* Advance the controller state machine */
2374cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2375e301370aSEdmund Nadolski 		state = SCIC_INITIALIZED;
2376cc9203bfSDan Williams 	else
2377e301370aSEdmund Nadolski 		state = SCIC_FAILED;
2378e301370aSEdmund Nadolski 	sci_change_state(sm, state);
2379cc9203bfSDan Williams 
2380cc9203bfSDan Williams 	return result;
2381cc9203bfSDan Williams }
2382cc9203bfSDan Williams 
238389a7301fSDan Williams static enum sci_status sci_user_parameters_set(struct isci_host *ihost,
238489a7301fSDan Williams 					       struct sci_user_parameters *sci_parms)
2385cc9203bfSDan Williams {
2386d9dcb4baSDan Williams 	u32 state = ihost->sm.current_state_id;
2387cc9203bfSDan Williams 
2388e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
2389e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
2390e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
2391cc9203bfSDan Williams 		u16 index;
2392cc9203bfSDan Williams 
2393cc9203bfSDan Williams 		/*
2394cc9203bfSDan Williams 		 * Validate the user parameters.  If they are not legal, then
2395cc9203bfSDan Williams 		 * return a failure.
2396cc9203bfSDan Williams 		 */
2397cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
2398cc9203bfSDan Williams 			struct sci_phy_user_params *user_phy;
2399cc9203bfSDan Williams 
240089a7301fSDan Williams 			user_phy = &sci_parms->phys[index];
2401cc9203bfSDan Williams 
2402cc9203bfSDan Williams 			if (!((user_phy->max_speed_generation <=
2403cc9203bfSDan Williams 						SCIC_SDS_PARM_MAX_SPEED) &&
2404cc9203bfSDan Williams 			      (user_phy->max_speed_generation >
2405cc9203bfSDan Williams 						SCIC_SDS_PARM_NO_SPEED)))
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 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2411cc9203bfSDan Williams 
2412cc9203bfSDan Williams 			if ((user_phy->in_connection_align_insertion_frequency <
2413cc9203bfSDan Williams 						3) ||
2414cc9203bfSDan Williams 			    (user_phy->align_insertion_frequency == 0) ||
2415cc9203bfSDan Williams 			    (user_phy->
2416cc9203bfSDan Williams 				notify_enable_spin_up_insertion_frequency ==
2417cc9203bfSDan Williams 						0))
2418cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2419cc9203bfSDan Williams 		}
2420cc9203bfSDan Williams 
242189a7301fSDan Williams 		if ((sci_parms->stp_inactivity_timeout == 0) ||
242289a7301fSDan Williams 		    (sci_parms->ssp_inactivity_timeout == 0) ||
242389a7301fSDan Williams 		    (sci_parms->stp_max_occupancy_timeout == 0) ||
242489a7301fSDan Williams 		    (sci_parms->ssp_max_occupancy_timeout == 0) ||
242589a7301fSDan Williams 		    (sci_parms->no_outbound_task_timeout == 0))
2426cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2427cc9203bfSDan Williams 
242889a7301fSDan Williams 		memcpy(&ihost->user_parameters, sci_parms, sizeof(*sci_parms));
2429cc9203bfSDan Williams 
2430cc9203bfSDan Williams 		return SCI_SUCCESS;
2431cc9203bfSDan Williams 	}
2432cc9203bfSDan Williams 
2433cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
2434cc9203bfSDan Williams }
2435cc9203bfSDan Williams 
243689a7301fSDan Williams static int sci_controller_mem_init(struct isci_host *ihost)
2437cc9203bfSDan Williams {
2438d9dcb4baSDan Williams 	struct device *dev = &ihost->pdev->dev;
24397c78da31SDan Williams 	dma_addr_t dma;
24407c78da31SDan Williams 	size_t size;
24417c78da31SDan Williams 	int err;
2442cc9203bfSDan Williams 
24437c78da31SDan Williams 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2444d9dcb4baSDan Williams 	ihost->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2445d9dcb4baSDan Williams 	if (!ihost->completion_queue)
2446cc9203bfSDan Williams 		return -ENOMEM;
2447cc9203bfSDan Williams 
2448d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->completion_queue_lower);
2449d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->completion_queue_upper);
2450cc9203bfSDan Williams 
2451d9dcb4baSDan Williams 	size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2452d9dcb4baSDan Williams 	ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
24537c78da31SDan Williams 							       GFP_KERNEL);
2454d9dcb4baSDan Williams 	if (!ihost->remote_node_context_table)
2455cc9203bfSDan Williams 		return -ENOMEM;
2456cc9203bfSDan Williams 
2457d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->remote_node_context_lower);
2458d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->remote_node_context_upper);
2459cc9203bfSDan Williams 
2460d9dcb4baSDan Williams 	size = ihost->task_context_entries * sizeof(struct scu_task_context),
2461d9dcb4baSDan Williams 	ihost->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2462d9dcb4baSDan Williams 	if (!ihost->task_context_table)
2463cc9203bfSDan Williams 		return -ENOMEM;
2464cc9203bfSDan Williams 
2465d9dcb4baSDan Williams 	ihost->task_context_dma = dma;
2466d9dcb4baSDan Williams 	writel(lower_32_bits(dma), &ihost->smu_registers->host_task_table_lower);
2467d9dcb4baSDan Williams 	writel(upper_32_bits(dma), &ihost->smu_registers->host_task_table_upper);
2468cc9203bfSDan Williams 
246989a7301fSDan Williams 	err = sci_unsolicited_frame_control_construct(ihost);
24707c78da31SDan Williams 	if (err)
24717c78da31SDan Williams 		return err;
2472cc9203bfSDan Williams 
2473cc9203bfSDan Williams 	/*
2474cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2475cc9203bfSDan Williams 	 * address table.
2476cc9203bfSDan Williams 	 */
2477d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2478d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_lower);
2479d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2480d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_header_base_address_upper);
2481cc9203bfSDan Williams 
2482d9dcb4baSDan Williams 	writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2483d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_lower);
2484d9dcb4baSDan Williams 	writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2485d9dcb4baSDan Williams 		&ihost->scu_registers->sdma.uf_address_table_upper);
2486cc9203bfSDan Williams 
2487cc9203bfSDan Williams 	return 0;
2488cc9203bfSDan Williams }
2489cc9203bfSDan Williams 
2490d9dcb4baSDan Williams int isci_host_init(struct isci_host *ihost)
24916f231ddaSDan Williams {
2492d9c37390SDan Williams 	int err = 0, i;
24936f231ddaSDan Williams 	enum sci_status status;
249489a7301fSDan Williams 	struct sci_user_parameters sci_user_params;
2495d9dcb4baSDan Williams 	struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
24966f231ddaSDan Williams 
2497d9dcb4baSDan Williams 	spin_lock_init(&ihost->state_lock);
2498d9dcb4baSDan Williams 	spin_lock_init(&ihost->scic_lock);
2499d9dcb4baSDan Williams 	init_waitqueue_head(&ihost->eventq);
25006f231ddaSDan Williams 
2501d9dcb4baSDan Williams 	isci_host_change_state(ihost, isci_starting);
25026f231ddaSDan Williams 
250389a7301fSDan Williams 	status = sci_controller_construct(ihost, scu_base(ihost),
2504d9dcb4baSDan Williams 					  smu_base(ihost));
25056f231ddaSDan Williams 
25066f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2507d9dcb4baSDan Williams 		dev_err(&ihost->pdev->dev,
250889a7301fSDan Williams 			"%s: sci_controller_construct failed - status = %x\n",
25096f231ddaSDan Williams 			__func__,
25106f231ddaSDan Williams 			status);
2511858d4aa7SDave Jiang 		return -ENODEV;
25126f231ddaSDan Williams 	}
25136f231ddaSDan Williams 
2514d9dcb4baSDan Williams 	ihost->sas_ha.dev = &ihost->pdev->dev;
2515d9dcb4baSDan Williams 	ihost->sas_ha.lldd_ha = ihost;
25166f231ddaSDan Williams 
2517d044af17SDan Williams 	/*
2518d044af17SDan Williams 	 * grab initial values stored in the controller object for OEM and USER
2519d044af17SDan Williams 	 * parameters
2520d044af17SDan Williams 	 */
252189a7301fSDan Williams 	isci_user_parameters_get(&sci_user_params);
252289a7301fSDan Williams 	status = sci_user_parameters_set(ihost, &sci_user_params);
2523d044af17SDan Williams 	if (status != SCI_SUCCESS) {
2524d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
252589a7301fSDan Williams 			 "%s: sci_user_parameters_set failed\n",
2526d044af17SDan Williams 			 __func__);
2527d044af17SDan Williams 		return -ENODEV;
2528d044af17SDan Williams 	}
25296f231ddaSDan Williams 
2530d044af17SDan Williams 	/* grab any OEM parameters specified in orom */
2531d044af17SDan Williams 	if (pci_info->orom) {
253289a7301fSDan Williams 		status = isci_parse_oem_parameters(&ihost->oem_parameters,
2533d044af17SDan Williams 						   pci_info->orom,
2534d9dcb4baSDan Williams 						   ihost->id);
25356f231ddaSDan Williams 		if (status != SCI_SUCCESS) {
2536d9dcb4baSDan Williams 			dev_warn(&ihost->pdev->dev,
25376f231ddaSDan Williams 				 "parsing firmware oem parameters failed\n");
2538858d4aa7SDave Jiang 			return -EINVAL;
25396f231ddaSDan Williams 		}
25404711ba10SDan Williams 	}
25414711ba10SDan Williams 
254289a7301fSDan Williams 	status = sci_oem_parameters_set(ihost);
25436f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
2544d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
254589a7301fSDan Williams 				"%s: sci_oem_parameters_set failed\n",
25466f231ddaSDan Williams 				__func__);
2547858d4aa7SDave Jiang 		return -ENODEV;
25486f231ddaSDan Williams 	}
25496f231ddaSDan Williams 
2550d9dcb4baSDan Williams 	tasklet_init(&ihost->completion_tasklet,
2551d9dcb4baSDan Williams 		     isci_host_completion_routine, (unsigned long)ihost);
25526f231ddaSDan Williams 
2553d9dcb4baSDan Williams 	INIT_LIST_HEAD(&ihost->requests_to_complete);
2554d9dcb4baSDan Williams 	INIT_LIST_HEAD(&ihost->requests_to_errorback);
25556f231ddaSDan Williams 
2556d9dcb4baSDan Williams 	spin_lock_irq(&ihost->scic_lock);
255789a7301fSDan Williams 	status = sci_controller_initialize(ihost);
2558d9dcb4baSDan Williams 	spin_unlock_irq(&ihost->scic_lock);
25597c40a803SDan Williams 	if (status != SCI_SUCCESS) {
2560d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
256189a7301fSDan Williams 			 "%s: sci_controller_initialize failed -"
25627c40a803SDan Williams 			 " status = 0x%x\n",
25637c40a803SDan Williams 			 __func__, status);
25647c40a803SDan Williams 		return -ENODEV;
25657c40a803SDan Williams 	}
25667c40a803SDan Williams 
256789a7301fSDan Williams 	err = sci_controller_mem_init(ihost);
25686f231ddaSDan Williams 	if (err)
2569858d4aa7SDave Jiang 		return err;
25706f231ddaSDan Williams 
2571d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
2572d9dcb4baSDan Williams 		isci_port_init(&ihost->ports[i], ihost, i);
25736f231ddaSDan Williams 
2574d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
2575d9dcb4baSDan Williams 		isci_phy_init(&ihost->phys[i], ihost, i);
2576d9c37390SDan Williams 
2577ad4f4c1dSDan Williams 	/* enable sgpio */
2578ad4f4c1dSDan Williams 	writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
2579ad4f4c1dSDan Williams 	for (i = 0; i < isci_gpio_count(ihost); i++)
2580ad4f4c1dSDan Williams 		writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
2581ad4f4c1dSDan Williams 	writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
2582ad4f4c1dSDan Williams 
2583d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
2584d9dcb4baSDan Williams 		struct isci_remote_device *idev = &ihost->devices[i];
2585d9c37390SDan Williams 
2586d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->reqs_in_process);
2587d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->node);
2588d9c37390SDan Williams 	}
25896f231ddaSDan Williams 
2590db056250SDan Williams 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2591db056250SDan Williams 		struct isci_request *ireq;
2592db056250SDan Williams 		dma_addr_t dma;
2593db056250SDan Williams 
2594d9dcb4baSDan Williams 		ireq = dmam_alloc_coherent(&ihost->pdev->dev,
2595db056250SDan Williams 					   sizeof(struct isci_request), &dma,
2596db056250SDan Williams 					   GFP_KERNEL);
2597db056250SDan Williams 		if (!ireq)
2598db056250SDan Williams 			return -ENOMEM;
2599db056250SDan Williams 
2600d9dcb4baSDan Williams 		ireq->tc = &ihost->task_context_table[i];
2601d9dcb4baSDan Williams 		ireq->owning_controller = ihost;
2602db056250SDan Williams 		spin_lock_init(&ireq->state_lock);
2603db056250SDan Williams 		ireq->request_daddr = dma;
2604d9dcb4baSDan Williams 		ireq->isci_host = ihost;
2605d9dcb4baSDan Williams 		ihost->reqs[i] = ireq;
2606db056250SDan Williams 	}
2607db056250SDan Williams 
2608858d4aa7SDave Jiang 	return 0;
26096f231ddaSDan Williams }
2610cc9203bfSDan Williams 
261189a7301fSDan Williams void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
261289a7301fSDan Williams 			    struct isci_phy *iphy)
2613cc9203bfSDan Williams {
2614d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2615e301370aSEdmund Nadolski 	case SCIC_STARTING:
2616d9dcb4baSDan Williams 		sci_del_timer(&ihost->phy_timer);
2617d9dcb4baSDan Williams 		ihost->phy_startup_timer_pending = false;
2618d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2619ffe191c9SDan Williams 						  iport, iphy);
262089a7301fSDan Williams 		sci_controller_start_next_phy(ihost);
2621cc9203bfSDan Williams 		break;
2622e301370aSEdmund Nadolski 	case SCIC_READY:
2623d9dcb4baSDan Williams 		ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
2624ffe191c9SDan Williams 						  iport, iphy);
2625cc9203bfSDan Williams 		break;
2626cc9203bfSDan Williams 	default:
2627d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2628cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
262985280955SDan Williams 			"unexpected state %d\n", __func__, iphy->phy_index,
2630d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2631cc9203bfSDan Williams 	}
2632cc9203bfSDan Williams }
2633cc9203bfSDan Williams 
263489a7301fSDan Williams void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
263589a7301fSDan Williams 			      struct isci_phy *iphy)
2636cc9203bfSDan Williams {
2637d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2638e301370aSEdmund Nadolski 	case SCIC_STARTING:
2639e301370aSEdmund Nadolski 	case SCIC_READY:
2640d9dcb4baSDan Williams 		ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
2641ffe191c9SDan Williams 						   iport, iphy);
2642cc9203bfSDan Williams 		break;
2643cc9203bfSDan Williams 	default:
2644d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2645cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2646cc9203bfSDan Williams 			"unexpected state %d\n",
2647cc9203bfSDan Williams 			__func__,
264885280955SDan Williams 			iphy->phy_index,
2649d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2650cc9203bfSDan Williams 	}
2651cc9203bfSDan Williams }
2652cc9203bfSDan Williams 
265389a7301fSDan Williams static bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
2654cc9203bfSDan Williams {
2655cc9203bfSDan Williams 	u32 index;
2656cc9203bfSDan Williams 
2657d9dcb4baSDan Williams 	for (index = 0; index < ihost->remote_node_entries; index++) {
2658d9dcb4baSDan Williams 		if ((ihost->device_table[index] != NULL) &&
2659d9dcb4baSDan Williams 		   (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2660cc9203bfSDan Williams 			return true;
2661cc9203bfSDan Williams 	}
2662cc9203bfSDan Williams 
2663cc9203bfSDan Williams 	return false;
2664cc9203bfSDan Williams }
2665cc9203bfSDan Williams 
266689a7301fSDan Williams void sci_controller_remote_device_stopped(struct isci_host *ihost,
266778a6f06eSDan Williams 					  struct isci_remote_device *idev)
2668cc9203bfSDan Williams {
2669d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_STOPPING) {
2670d9dcb4baSDan Williams 		dev_dbg(&ihost->pdev->dev,
2671cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2672cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2673d9dcb4baSDan Williams 			ihost, idev,
2674d9dcb4baSDan Williams 			ihost->sm.current_state_id);
2675cc9203bfSDan Williams 		return;
2676cc9203bfSDan Williams 	}
2677cc9203bfSDan Williams 
267889a7301fSDan Williams 	if (!sci_controller_has_remote_devices_stopping(ihost))
2679d9dcb4baSDan Williams 		sci_change_state(&ihost->sm, SCIC_STOPPED);
2680cc9203bfSDan Williams }
2681cc9203bfSDan Williams 
268289a7301fSDan Williams void sci_controller_post_request(struct isci_host *ihost, u32 request)
2683cc9203bfSDan Williams {
268489a7301fSDan Williams 	dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
268589a7301fSDan Williams 		__func__, ihost->id, request);
2686cc9203bfSDan Williams 
2687d9dcb4baSDan Williams 	writel(request, &ihost->smu_registers->post_context_port);
2688cc9203bfSDan Williams }
2689cc9203bfSDan Williams 
269089a7301fSDan Williams struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
2691cc9203bfSDan Williams {
2692cc9203bfSDan Williams 	u16 task_index;
2693cc9203bfSDan Williams 	u16 task_sequence;
2694cc9203bfSDan Williams 
2695dd047c8eSDan Williams 	task_index = ISCI_TAG_TCI(io_tag);
2696cc9203bfSDan Williams 
2697d9dcb4baSDan Williams 	if (task_index < ihost->task_context_entries) {
2698d9dcb4baSDan Williams 		struct isci_request *ireq = ihost->reqs[task_index];
2699db056250SDan Williams 
2700db056250SDan Williams 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2701dd047c8eSDan Williams 			task_sequence = ISCI_TAG_SEQ(io_tag);
2702cc9203bfSDan Williams 
2703d9dcb4baSDan Williams 			if (task_sequence == ihost->io_request_sequence[task_index])
27045076a1a9SDan Williams 				return ireq;
2705cc9203bfSDan Williams 		}
2706cc9203bfSDan Williams 	}
2707cc9203bfSDan Williams 
2708cc9203bfSDan Williams 	return NULL;
2709cc9203bfSDan Williams }
2710cc9203bfSDan Williams 
2711cc9203bfSDan Williams /**
2712cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2713cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2714cc9203bfSDan Williams  *    node index available.
2715cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2716cc9203bfSDan Williams  *    free remote node ids
2717cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2718cc9203bfSDan Williams  *    id
2719cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2720cc9203bfSDan Williams  *    is available
2721cc9203bfSDan Williams  *
2722cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2723cc9203bfSDan Williams  * node index available.
2724cc9203bfSDan Williams  */
272589a7301fSDan Williams enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
272678a6f06eSDan Williams 							    struct isci_remote_device *idev,
2727cc9203bfSDan Williams 							    u16 *node_id)
2728cc9203bfSDan Williams {
2729cc9203bfSDan Williams 	u16 node_index;
273089a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2731cc9203bfSDan Williams 
273289a7301fSDan Williams 	node_index = sci_remote_node_table_allocate_remote_node(
2733d9dcb4baSDan Williams 		&ihost->available_remote_nodes, remote_node_count
2734cc9203bfSDan Williams 		);
2735cc9203bfSDan Williams 
2736cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2737d9dcb4baSDan Williams 		ihost->device_table[node_index] = idev;
2738cc9203bfSDan Williams 
2739cc9203bfSDan Williams 		*node_id = node_index;
2740cc9203bfSDan Williams 
2741cc9203bfSDan Williams 		return SCI_SUCCESS;
2742cc9203bfSDan Williams 	}
2743cc9203bfSDan Williams 
2744cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2745cc9203bfSDan Williams }
2746cc9203bfSDan Williams 
274789a7301fSDan Williams void sci_controller_free_remote_node_context(struct isci_host *ihost,
274878a6f06eSDan Williams 					     struct isci_remote_device *idev,
2749cc9203bfSDan Williams 					     u16 node_id)
2750cc9203bfSDan Williams {
275189a7301fSDan Williams 	u32 remote_node_count = sci_remote_device_node_count(idev);
2752cc9203bfSDan Williams 
2753d9dcb4baSDan Williams 	if (ihost->device_table[node_id] == idev) {
2754d9dcb4baSDan Williams 		ihost->device_table[node_id] = NULL;
2755cc9203bfSDan Williams 
275689a7301fSDan Williams 		sci_remote_node_table_release_remote_node_index(
2757d9dcb4baSDan Williams 			&ihost->available_remote_nodes, remote_node_count, node_id
2758cc9203bfSDan Williams 			);
2759cc9203bfSDan Williams 	}
2760cc9203bfSDan Williams }
2761cc9203bfSDan Williams 
276289a7301fSDan Williams void sci_controller_copy_sata_response(void *response_buffer,
2763cc9203bfSDan Williams 				       void *frame_header,
2764cc9203bfSDan Williams 				       void *frame_buffer)
2765cc9203bfSDan Williams {
276689a7301fSDan Williams 	/* XXX type safety? */
2767cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2768cc9203bfSDan Williams 
2769cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2770cc9203bfSDan Williams 	       frame_buffer,
2771cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2772cc9203bfSDan Williams }
2773cc9203bfSDan Williams 
277489a7301fSDan Williams void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
2775cc9203bfSDan Williams {
277689a7301fSDan Williams 	if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
2777d9dcb4baSDan Williams 		writel(ihost->uf_control.get,
2778d9dcb4baSDan Williams 			&ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
2779cc9203bfSDan Williams }
2780cc9203bfSDan Williams 
2781312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci)
2782312e0c24SDan Williams {
2783312e0c24SDan Williams 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2784312e0c24SDan Williams 
2785312e0c24SDan Williams 	ihost->tci_pool[tail] = tci;
2786312e0c24SDan Williams 	ihost->tci_tail = tail + 1;
2787312e0c24SDan Williams }
2788312e0c24SDan Williams 
2789312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost)
2790312e0c24SDan Williams {
2791312e0c24SDan Williams 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2792312e0c24SDan Williams 	u16 tci = ihost->tci_pool[head];
2793312e0c24SDan Williams 
2794312e0c24SDan Williams 	ihost->tci_head = head + 1;
2795312e0c24SDan Williams 	return tci;
2796312e0c24SDan Williams }
2797312e0c24SDan Williams 
2798312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost)
2799312e0c24SDan Williams {
2800312e0c24SDan Williams 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2801312e0c24SDan Williams }
2802312e0c24SDan Williams 
2803312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost)
2804312e0c24SDan Williams {
2805312e0c24SDan Williams 	if (isci_tci_space(ihost)) {
2806312e0c24SDan Williams 		u16 tci = isci_tci_alloc(ihost);
2807d9dcb4baSDan Williams 		u8 seq = ihost->io_request_sequence[tci];
2808312e0c24SDan Williams 
2809312e0c24SDan Williams 		return ISCI_TAG(seq, tci);
2810312e0c24SDan Williams 	}
2811312e0c24SDan Williams 
2812312e0c24SDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
2813312e0c24SDan Williams }
2814312e0c24SDan Williams 
2815312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2816312e0c24SDan Williams {
2817312e0c24SDan Williams 	u16 tci = ISCI_TAG_TCI(io_tag);
2818312e0c24SDan Williams 	u16 seq = ISCI_TAG_SEQ(io_tag);
2819312e0c24SDan Williams 
2820312e0c24SDan Williams 	/* prevent tail from passing head */
2821312e0c24SDan Williams 	if (isci_tci_active(ihost) == 0)
2822312e0c24SDan Williams 		return SCI_FAILURE_INVALID_IO_TAG;
2823312e0c24SDan Williams 
2824d9dcb4baSDan Williams 	if (seq == ihost->io_request_sequence[tci]) {
2825d9dcb4baSDan Williams 		ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2826312e0c24SDan Williams 
2827312e0c24SDan Williams 		isci_tci_free(ihost, tci);
2828312e0c24SDan Williams 
2829312e0c24SDan Williams 		return SCI_SUCCESS;
2830312e0c24SDan Williams 	}
2831312e0c24SDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
2832312e0c24SDan Williams }
2833312e0c24SDan Williams 
283489a7301fSDan Williams enum sci_status sci_controller_start_io(struct isci_host *ihost,
283578a6f06eSDan Williams 					struct isci_remote_device *idev,
28365076a1a9SDan Williams 					struct isci_request *ireq)
2837cc9203bfSDan Williams {
2838cc9203bfSDan Williams 	enum sci_status status;
2839cc9203bfSDan Williams 
2840d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
284114e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
284214e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2843cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2844cc9203bfSDan Williams 	}
2845cc9203bfSDan Williams 
284689a7301fSDan Williams 	status = sci_remote_device_start_io(ihost, idev, ireq);
2847cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2848cc9203bfSDan Williams 		return status;
2849cc9203bfSDan Williams 
28505076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
285134a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2852cc9203bfSDan Williams 	return SCI_SUCCESS;
2853cc9203bfSDan Williams }
2854cc9203bfSDan Williams 
285589a7301fSDan Williams enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
285678a6f06eSDan Williams 						 struct isci_remote_device *idev,
28575076a1a9SDan Williams 						 struct isci_request *ireq)
2858cc9203bfSDan Williams {
285989a7301fSDan Williams 	/* terminate an ongoing (i.e. started) core IO request.  This does not
286089a7301fSDan Williams 	 * abort the IO request at the target, but rather removes the IO
286189a7301fSDan Williams 	 * request from the host controller.
286289a7301fSDan Williams 	 */
2863cc9203bfSDan Williams 	enum sci_status status;
2864cc9203bfSDan Williams 
2865d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
286614e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
286714e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2868cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2869cc9203bfSDan Williams 	}
2870cc9203bfSDan Williams 
287189a7301fSDan Williams 	status = sci_io_request_terminate(ireq);
2872cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2873cc9203bfSDan Williams 		return status;
2874cc9203bfSDan Williams 
2875cc9203bfSDan Williams 	/*
2876cc9203bfSDan Williams 	 * Utilize the original post context command and or in the POST_TC_ABORT
2877cc9203bfSDan Williams 	 * request sub-type.
2878cc9203bfSDan Williams 	 */
287989a7301fSDan Williams 	sci_controller_post_request(ihost,
288089a7301fSDan Williams 				    ireq->post_context | SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2881cc9203bfSDan Williams 	return SCI_SUCCESS;
2882cc9203bfSDan Williams }
2883cc9203bfSDan Williams 
2884cc9203bfSDan Williams /**
288589a7301fSDan Williams  * sci_controller_complete_io() - This method will perform core specific
2886cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
2887cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
2888cc9203bfSDan Williams  *    reused (i.e. re-constructed).
288989a7301fSDan Williams  * @ihost: The handle to the controller object for which to complete the
2890cc9203bfSDan Williams  *    IO request.
289189a7301fSDan Williams  * @idev: The handle to the remote device object for which to complete
2892cc9203bfSDan Williams  *    the IO request.
289389a7301fSDan Williams  * @ireq: the handle to the io request object to complete.
2894cc9203bfSDan Williams  */
289589a7301fSDan Williams enum sci_status sci_controller_complete_io(struct isci_host *ihost,
289678a6f06eSDan Williams 					   struct isci_remote_device *idev,
28975076a1a9SDan Williams 					   struct isci_request *ireq)
2898cc9203bfSDan Williams {
2899cc9203bfSDan Williams 	enum sci_status status;
2900cc9203bfSDan Williams 	u16 index;
2901cc9203bfSDan Williams 
2902d9dcb4baSDan Williams 	switch (ihost->sm.current_state_id) {
2903e301370aSEdmund Nadolski 	case SCIC_STOPPING:
2904cc9203bfSDan Williams 		/* XXX: Implement this function */
2905cc9203bfSDan Williams 		return SCI_FAILURE;
2906e301370aSEdmund Nadolski 	case SCIC_READY:
290789a7301fSDan Williams 		status = sci_remote_device_complete_io(ihost, idev, ireq);
2908cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
2909cc9203bfSDan Williams 			return status;
2910cc9203bfSDan Williams 
29115076a1a9SDan Williams 		index = ISCI_TAG_TCI(ireq->io_tag);
29125076a1a9SDan Williams 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2913cc9203bfSDan Williams 		return SCI_SUCCESS;
2914cc9203bfSDan Williams 	default:
291514e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
291614e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2917cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2918cc9203bfSDan Williams 	}
2919cc9203bfSDan Williams 
2920cc9203bfSDan Williams }
2921cc9203bfSDan Williams 
292289a7301fSDan Williams enum sci_status sci_controller_continue_io(struct isci_request *ireq)
2923cc9203bfSDan Williams {
2924d9dcb4baSDan Williams 	struct isci_host *ihost = ireq->owning_controller;
2925cc9203bfSDan Williams 
2926d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
292714e99b4aSDan Williams 		dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
292814e99b4aSDan Williams 			 __func__, ihost->sm.current_state_id);
2929cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2930cc9203bfSDan Williams 	}
2931cc9203bfSDan Williams 
29325076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
293334a99158SDan Williams 	sci_controller_post_request(ihost, ireq->post_context);
2934cc9203bfSDan Williams 	return SCI_SUCCESS;
2935cc9203bfSDan Williams }
2936cc9203bfSDan Williams 
2937cc9203bfSDan Williams /**
293889a7301fSDan Williams  * sci_controller_start_task() - This method is called by the SCIC user to
2939cc9203bfSDan Williams  *    send/start a framework task management request.
2940cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
2941cc9203bfSDan Williams  *    management request.
2942cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
2943cc9203bfSDan Williams  *    the task management request.
2944cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
2945cc9203bfSDan Williams  */
294689a7301fSDan Williams enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
294778a6f06eSDan Williams 					       struct isci_remote_device *idev,
29485076a1a9SDan Williams 					       struct isci_request *ireq)
2949cc9203bfSDan Williams {
2950cc9203bfSDan Williams 	enum sci_status status;
2951cc9203bfSDan Williams 
2952d9dcb4baSDan Williams 	if (ihost->sm.current_state_id != SCIC_READY) {
2953d9dcb4baSDan Williams 		dev_warn(&ihost->pdev->dev,
2954cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
2955cc9203bfSDan Williams 			 "state\n",
2956cc9203bfSDan Williams 			 __func__);
2957cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
2958cc9203bfSDan Williams 	}
2959cc9203bfSDan Williams 
296089a7301fSDan Williams 	status = sci_remote_device_start_task(ihost, idev, ireq);
2961cc9203bfSDan Williams 	switch (status) {
2962cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2963db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2964cc9203bfSDan Williams 
2965cc9203bfSDan Williams 		/*
2966cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
2967cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
2968cc9203bfSDan Williams 		 * RNC is resumed.)
2969cc9203bfSDan Williams 		 */
2970cc9203bfSDan Williams 		return SCI_SUCCESS;
2971cc9203bfSDan Williams 	case SCI_SUCCESS:
2972db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
297334a99158SDan Williams 		sci_controller_post_request(ihost, ireq->post_context);
2974cc9203bfSDan Williams 		break;
2975cc9203bfSDan Williams 	default:
2976cc9203bfSDan Williams 		break;
2977cc9203bfSDan Williams 	}
2978cc9203bfSDan Williams 
2979cc9203bfSDan Williams 	return status;
2980cc9203bfSDan Williams }
2981ad4f4c1dSDan Williams 
2982ad4f4c1dSDan Williams static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
2983ad4f4c1dSDan Williams {
2984ad4f4c1dSDan Williams 	int d;
2985ad4f4c1dSDan Williams 
2986ad4f4c1dSDan Williams 	/* no support for TX_GP_CFG */
2987ad4f4c1dSDan Williams 	if (reg_index == 0)
2988ad4f4c1dSDan Williams 		return -EINVAL;
2989ad4f4c1dSDan Williams 
2990ad4f4c1dSDan Williams 	for (d = 0; d < isci_gpio_count(ihost); d++) {
2991ad4f4c1dSDan Williams 		u32 val = 0x444; /* all ODx.n clear */
2992ad4f4c1dSDan Williams 		int i;
2993ad4f4c1dSDan Williams 
2994ad4f4c1dSDan Williams 		for (i = 0; i < 3; i++) {
2995ad4f4c1dSDan Williams 			int bit = (i << 2) + 2;
2996ad4f4c1dSDan Williams 
2997ad4f4c1dSDan Williams 			bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
2998ad4f4c1dSDan Williams 						       write_data, reg_index,
2999ad4f4c1dSDan Williams 						       reg_count);
3000ad4f4c1dSDan Williams 			if (bit < 0)
3001ad4f4c1dSDan Williams 				break;
3002ad4f4c1dSDan Williams 
3003ad4f4c1dSDan Williams 			/* if od is set, clear the 'invert' bit */
3004ad4f4c1dSDan Williams 			val &= ~(bit << ((i << 2) + 2));
3005ad4f4c1dSDan Williams 		}
3006ad4f4c1dSDan Williams 
3007ad4f4c1dSDan Williams 		if (i < 3)
3008ad4f4c1dSDan Williams 			break;
3009ad4f4c1dSDan Williams 		writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
3010ad4f4c1dSDan Williams 	}
3011ad4f4c1dSDan Williams 
3012ad4f4c1dSDan Williams 	/* unless reg_index is > 1, we should always be able to write at
3013ad4f4c1dSDan Williams 	 * least one register
3014ad4f4c1dSDan Williams 	 */
3015ad4f4c1dSDan Williams 	return d > 0;
3016ad4f4c1dSDan Williams }
3017ad4f4c1dSDan Williams 
3018ad4f4c1dSDan Williams int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
3019ad4f4c1dSDan Williams 		    u8 reg_count, u8 *write_data)
3020ad4f4c1dSDan Williams {
3021ad4f4c1dSDan Williams 	struct isci_host *ihost = sas_ha->lldd_ha;
3022ad4f4c1dSDan Williams 	int written;
3023ad4f4c1dSDan Williams 
3024ad4f4c1dSDan Williams 	switch (reg_type) {
3025ad4f4c1dSDan Williams 	case SAS_GPIO_REG_TX_GP:
3026ad4f4c1dSDan Williams 		written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
3027ad4f4c1dSDan Williams 		break;
3028ad4f4c1dSDan Williams 	default:
3029ad4f4c1dSDan Williams 		written = -EINVAL;
3030ad4f4c1dSDan Williams 	}
3031ad4f4c1dSDan Williams 
3032ad4f4c1dSDan Williams 	return written;
3033ad4f4c1dSDan Williams }
3034