xref: /openbmc/linux/drivers/scsi/isci/host.c (revision 852809559e4680ba4768262a6c3d21454fcd460e)
16f231ddaSDan Williams /*
26f231ddaSDan Williams  * This file is provided under a dual BSD/GPLv2 license.  When using or
36f231ddaSDan Williams  * redistributing this file, you may do so under either license.
46f231ddaSDan Williams  *
56f231ddaSDan Williams  * GPL LICENSE SUMMARY
66f231ddaSDan Williams  *
76f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
86f231ddaSDan Williams  *
96f231ddaSDan Williams  * This program is free software; you can redistribute it and/or modify
106f231ddaSDan Williams  * it under the terms of version 2 of the GNU General Public License as
116f231ddaSDan Williams  * published by the Free Software Foundation.
126f231ddaSDan Williams  *
136f231ddaSDan Williams  * This program is distributed in the hope that it will be useful, but
146f231ddaSDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
156f231ddaSDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
166f231ddaSDan Williams  * General Public License for more details.
176f231ddaSDan Williams  *
186f231ddaSDan Williams  * You should have received a copy of the GNU General Public License
196f231ddaSDan Williams  * along with this program; if not, write to the Free Software
206f231ddaSDan Williams  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
216f231ddaSDan Williams  * The full GNU General Public License is included in this distribution
226f231ddaSDan Williams  * in the file called LICENSE.GPL.
236f231ddaSDan Williams  *
246f231ddaSDan Williams  * BSD LICENSE
256f231ddaSDan Williams  *
266f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
276f231ddaSDan Williams  * All rights reserved.
286f231ddaSDan Williams  *
296f231ddaSDan Williams  * Redistribution and use in source and binary forms, with or without
306f231ddaSDan Williams  * modification, are permitted provided that the following conditions
316f231ddaSDan Williams  * are met:
326f231ddaSDan Williams  *
336f231ddaSDan Williams  *   * Redistributions of source code must retain the above copyright
346f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer.
356f231ddaSDan Williams  *   * Redistributions in binary form must reproduce the above copyright
366f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer in
376f231ddaSDan Williams  *     the documentation and/or other materials provided with the
386f231ddaSDan Williams  *     distribution.
396f231ddaSDan Williams  *   * Neither the name of Intel Corporation nor the names of its
406f231ddaSDan Williams  *     contributors may be used to endorse or promote products derived
416f231ddaSDan Williams  *     from this software without specific prior written permission.
426f231ddaSDan Williams  *
436f231ddaSDan Williams  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
446f231ddaSDan Williams  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
456f231ddaSDan Williams  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
466f231ddaSDan Williams  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
476f231ddaSDan Williams  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
486f231ddaSDan Williams  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
496f231ddaSDan Williams  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
506f231ddaSDan Williams  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
516f231ddaSDan Williams  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
526f231ddaSDan Williams  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
536f231ddaSDan Williams  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
546f231ddaSDan Williams  */
55ac668c69SDan Williams #include <linux/circ_buf.h>
56cc9203bfSDan Williams #include <linux/device.h>
57cc9203bfSDan Williams #include <scsi/sas.h>
58cc9203bfSDan Williams #include "host.h"
596f231ddaSDan Williams #include "isci.h"
606f231ddaSDan Williams #include "port.h"
616f231ddaSDan Williams #include "host.h"
62d044af17SDan Williams #include "probe_roms.h"
63cc9203bfSDan Williams #include "remote_device.h"
64cc9203bfSDan Williams #include "request.h"
65cc9203bfSDan Williams #include "scu_completion_codes.h"
66cc9203bfSDan Williams #include "scu_event_codes.h"
6763a3a15fSDan Williams #include "registers.h"
68cc9203bfSDan Williams #include "scu_remote_node_context.h"
69cc9203bfSDan Williams #include "scu_task_context.h"
70cc9203bfSDan Williams #include "scu_unsolicited_frame.h"
716f231ddaSDan Williams 
72cc9203bfSDan Williams #define SCU_CONTEXT_RAM_INIT_STALL_TIME      200
73cc9203bfSDan Williams 
747c78da31SDan Williams #define smu_max_ports(dcc_value) \
75cc9203bfSDan Williams 	(\
76cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
77cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
78cc9203bfSDan Williams 	)
79cc9203bfSDan Williams 
807c78da31SDan Williams #define smu_max_task_contexts(dcc_value)	\
81cc9203bfSDan Williams 	(\
82cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
83cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
84cc9203bfSDan Williams 	)
85cc9203bfSDan Williams 
867c78da31SDan Williams #define smu_max_rncs(dcc_value) \
87cc9203bfSDan Williams 	(\
88cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
89cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
90cc9203bfSDan Williams 	)
91cc9203bfSDan Williams 
92cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT      100
93cc9203bfSDan Williams 
94cc9203bfSDan Williams /**
95cc9203bfSDan Williams  *
96cc9203bfSDan Williams  *
97cc9203bfSDan Williams  * The number of milliseconds to wait while a given phy is consuming power
98cc9203bfSDan Williams  * before allowing another set of phys to consume power. Ultimately, this will
99cc9203bfSDan Williams  * be specified by OEM parameter.
100cc9203bfSDan Williams  */
101cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
102cc9203bfSDan Williams 
103cc9203bfSDan Williams /**
104cc9203bfSDan Williams  * NORMALIZE_PUT_POINTER() -
105cc9203bfSDan Williams  *
106cc9203bfSDan Williams  * This macro will normalize the completion queue put pointer so its value can
107cc9203bfSDan Williams  * be used as an array inde
108cc9203bfSDan Williams  */
109cc9203bfSDan Williams #define NORMALIZE_PUT_POINTER(x) \
110cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
111cc9203bfSDan Williams 
112cc9203bfSDan Williams 
113cc9203bfSDan Williams /**
114cc9203bfSDan Williams  * NORMALIZE_EVENT_POINTER() -
115cc9203bfSDan Williams  *
116cc9203bfSDan Williams  * This macro will normalize the completion queue event entry so its value can
117cc9203bfSDan Williams  * be used as an index.
118cc9203bfSDan Williams  */
119cc9203bfSDan Williams #define NORMALIZE_EVENT_POINTER(x) \
120cc9203bfSDan Williams 	(\
121cc9203bfSDan Williams 		((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
122cc9203bfSDan Williams 		>> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT	\
123cc9203bfSDan Williams 	)
124cc9203bfSDan Williams 
125cc9203bfSDan Williams /**
126cc9203bfSDan Williams  * NORMALIZE_GET_POINTER() -
127cc9203bfSDan Williams  *
128cc9203bfSDan Williams  * This macro will normalize the completion queue get pointer so its value can
129cc9203bfSDan Williams  * be used as an index into an array
130cc9203bfSDan Williams  */
131cc9203bfSDan Williams #define NORMALIZE_GET_POINTER(x) \
132cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
133cc9203bfSDan Williams 
134cc9203bfSDan Williams /**
135cc9203bfSDan Williams  * NORMALIZE_GET_POINTER_CYCLE_BIT() -
136cc9203bfSDan Williams  *
137cc9203bfSDan Williams  * This macro will normalize the completion queue cycle pointer so it matches
138cc9203bfSDan Williams  * the completion queue cycle bit
139cc9203bfSDan Williams  */
140cc9203bfSDan Williams #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
141cc9203bfSDan Williams 	((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
142cc9203bfSDan Williams 
143cc9203bfSDan Williams /**
144cc9203bfSDan Williams  * COMPLETION_QUEUE_CYCLE_BIT() -
145cc9203bfSDan Williams  *
146cc9203bfSDan Williams  * This macro will return the cycle bit of the completion queue entry
147cc9203bfSDan Williams  */
148cc9203bfSDan Williams #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
149cc9203bfSDan Williams 
15012ef6544SEdmund Nadolski /* Init the state machine and call the state entry function (if any) */
15112ef6544SEdmund Nadolski void sci_init_sm(struct sci_base_state_machine *sm,
15212ef6544SEdmund Nadolski 		 const struct sci_base_state *state_table, u32 initial_state)
15312ef6544SEdmund Nadolski {
15412ef6544SEdmund Nadolski 	sci_state_transition_t handler;
15512ef6544SEdmund Nadolski 
15612ef6544SEdmund Nadolski 	sm->initial_state_id    = initial_state;
15712ef6544SEdmund Nadolski 	sm->previous_state_id   = initial_state;
15812ef6544SEdmund Nadolski 	sm->current_state_id    = initial_state;
15912ef6544SEdmund Nadolski 	sm->state_table         = state_table;
16012ef6544SEdmund Nadolski 
16112ef6544SEdmund Nadolski 	handler = sm->state_table[initial_state].enter_state;
16212ef6544SEdmund Nadolski 	if (handler)
16312ef6544SEdmund Nadolski 		handler(sm);
16412ef6544SEdmund Nadolski }
16512ef6544SEdmund Nadolski 
16612ef6544SEdmund Nadolski /* Call the state exit fn, update the current state, call the state entry fn */
16712ef6544SEdmund Nadolski void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
16812ef6544SEdmund Nadolski {
16912ef6544SEdmund Nadolski 	sci_state_transition_t handler;
17012ef6544SEdmund Nadolski 
17112ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].exit_state;
17212ef6544SEdmund Nadolski 	if (handler)
17312ef6544SEdmund Nadolski 		handler(sm);
17412ef6544SEdmund Nadolski 
17512ef6544SEdmund Nadolski 	sm->previous_state_id = sm->current_state_id;
17612ef6544SEdmund Nadolski 	sm->current_state_id = next_state;
17712ef6544SEdmund Nadolski 
17812ef6544SEdmund Nadolski 	handler = sm->state_table[sm->current_state_id].enter_state;
17912ef6544SEdmund Nadolski 	if (handler)
18012ef6544SEdmund Nadolski 		handler(sm);
18112ef6544SEdmund Nadolski }
18212ef6544SEdmund Nadolski 
183cc9203bfSDan Williams static bool scic_sds_controller_completion_queue_has_entries(
184cc9203bfSDan Williams 	struct scic_sds_controller *scic)
185cc9203bfSDan Williams {
186cc9203bfSDan Williams 	u32 get_value = scic->completion_queue_get;
187cc9203bfSDan Williams 	u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
188cc9203bfSDan Williams 
189cc9203bfSDan Williams 	if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
190cc9203bfSDan Williams 	    COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
191cc9203bfSDan Williams 		return true;
192cc9203bfSDan Williams 
193cc9203bfSDan Williams 	return false;
194cc9203bfSDan Williams }
195cc9203bfSDan Williams 
196cc9203bfSDan Williams static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
197cc9203bfSDan Williams {
198cc9203bfSDan Williams 	if (scic_sds_controller_completion_queue_has_entries(scic)) {
199cc9203bfSDan Williams 		return true;
200cc9203bfSDan Williams 	} else {
201cc9203bfSDan Williams 		/*
202cc9203bfSDan Williams 		 * we have a spurious interrupt it could be that we have already
203cc9203bfSDan Williams 		 * emptied the completion queue from a previous interrupt */
204cc9203bfSDan Williams 		writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
205cc9203bfSDan Williams 
206cc9203bfSDan Williams 		/*
207cc9203bfSDan Williams 		 * There is a race in the hardware that could cause us not to be notified
208cc9203bfSDan Williams 		 * of an interrupt completion if we do not take this step.  We will mask
209cc9203bfSDan Williams 		 * then unmask the interrupts so if there is another interrupt pending
210cc9203bfSDan Williams 		 * the clearing of the interrupt source we get the next interrupt message. */
211cc9203bfSDan Williams 		writel(0xFF000000, &scic->smu_registers->interrupt_mask);
212cc9203bfSDan Williams 		writel(0, &scic->smu_registers->interrupt_mask);
213cc9203bfSDan Williams 	}
214cc9203bfSDan Williams 
215cc9203bfSDan Williams 	return false;
216cc9203bfSDan Williams }
217cc9203bfSDan Williams 
218c7ef4031SDan Williams irqreturn_t isci_msix_isr(int vec, void *data)
2196f231ddaSDan Williams {
220c7ef4031SDan Williams 	struct isci_host *ihost = data;
2216f231ddaSDan Williams 
222cc3dbd0aSArtur Wojcik 	if (scic_sds_controller_isr(&ihost->sci))
223c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
2246f231ddaSDan Williams 
225c7ef4031SDan Williams 	return IRQ_HANDLED;
226c7ef4031SDan Williams }
227c7ef4031SDan Williams 
228cc9203bfSDan Williams static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
229cc9203bfSDan Williams {
230cc9203bfSDan Williams 	u32 interrupt_status;
231cc9203bfSDan Williams 
232cc9203bfSDan Williams 	interrupt_status =
233cc9203bfSDan Williams 		readl(&scic->smu_registers->interrupt_status);
234cc9203bfSDan Williams 	interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
235cc9203bfSDan Williams 
236cc9203bfSDan Williams 	if (interrupt_status != 0) {
237cc9203bfSDan Williams 		/*
238cc9203bfSDan Williams 		 * There is an error interrupt pending so let it through and handle
239cc9203bfSDan Williams 		 * in the callback */
240cc9203bfSDan Williams 		return true;
241cc9203bfSDan Williams 	}
242cc9203bfSDan Williams 
243cc9203bfSDan Williams 	/*
244cc9203bfSDan Williams 	 * There is a race in the hardware that could cause us not to be notified
245cc9203bfSDan Williams 	 * of an interrupt completion if we do not take this step.  We will mask
246cc9203bfSDan Williams 	 * then unmask the error interrupts so if there was another interrupt
247cc9203bfSDan Williams 	 * pending we will be notified.
248cc9203bfSDan Williams 	 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
249cc9203bfSDan Williams 	writel(0xff, &scic->smu_registers->interrupt_mask);
250cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
251cc9203bfSDan Williams 
252cc9203bfSDan Williams 	return false;
253cc9203bfSDan Williams }
254cc9203bfSDan Williams 
255cc9203bfSDan Williams static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
256cc9203bfSDan Williams 						u32 completion_entry)
257cc9203bfSDan Williams {
258db056250SDan Williams 	u32 index = SCU_GET_COMPLETION_INDEX(completion_entry);
259db056250SDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
260db056250SDan Williams 	struct isci_request *ireq = ihost->reqs[index];
261cc9203bfSDan Williams 
262cc9203bfSDan Williams 	/* Make sure that we really want to process this IO request */
263db056250SDan Williams 	if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
2645076a1a9SDan Williams 	    ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
2655076a1a9SDan Williams 	    ISCI_TAG_SEQ(ireq->io_tag) == scic->io_request_sequence[index])
266cc9203bfSDan Williams 		/* Yep this is a valid io request pass it along to the io request handler */
2675076a1a9SDan Williams 		scic_sds_io_request_tc_completion(ireq, completion_entry);
268cc9203bfSDan Williams }
269cc9203bfSDan Williams 
270cc9203bfSDan Williams static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
271cc9203bfSDan Williams 						u32 completion_entry)
272cc9203bfSDan Williams {
273cc9203bfSDan Williams 	u32 index;
2745076a1a9SDan Williams 	struct isci_request *ireq;
275cc9203bfSDan Williams 	struct scic_sds_remote_device *device;
276cc9203bfSDan Williams 
277cc9203bfSDan Williams 	index = SCU_GET_COMPLETION_INDEX(completion_entry);
278cc9203bfSDan Williams 
279cc9203bfSDan Williams 	switch (scu_get_command_request_type(completion_entry)) {
280cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
281cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
2825076a1a9SDan Williams 		ireq = scic_to_ihost(scic)->reqs[index];
2835076a1a9SDan Williams 		dev_warn(scic_to_dev(scic), "%s: %x for io request %p\n",
2845076a1a9SDan Williams 			 __func__, completion_entry, ireq);
285cc9203bfSDan Williams 		/* @todo For a post TC operation we need to fail the IO
286cc9203bfSDan Williams 		 * request
287cc9203bfSDan Williams 		 */
288cc9203bfSDan Williams 		break;
289cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
290cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
291cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
292cc9203bfSDan Williams 		device = scic->device_table[index];
2935076a1a9SDan Williams 		dev_warn(scic_to_dev(scic), "%s: %x for device %p\n",
2945076a1a9SDan Williams 			 __func__, completion_entry, device);
295cc9203bfSDan Williams 		/* @todo For a port RNC operation we need to fail the
296cc9203bfSDan Williams 		 * device
297cc9203bfSDan Williams 		 */
298cc9203bfSDan Williams 		break;
299cc9203bfSDan Williams 	default:
3005076a1a9SDan Williams 		dev_warn(scic_to_dev(scic), "%s: unknown completion type %x\n",
3015076a1a9SDan Williams 			 __func__, completion_entry);
302cc9203bfSDan Williams 		break;
303cc9203bfSDan Williams 	}
304cc9203bfSDan Williams }
305cc9203bfSDan Williams 
306cc9203bfSDan Williams static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
307cc9203bfSDan Williams 						  u32 completion_entry)
308cc9203bfSDan Williams {
309cc9203bfSDan Williams 	u32 index;
310cc9203bfSDan Williams 	u32 frame_index;
311cc9203bfSDan Williams 
312cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
313cc9203bfSDan Williams 	struct scu_unsolicited_frame_header *frame_header;
314*85280955SDan Williams 	struct isci_phy *iphy;
315cc9203bfSDan Williams 	struct scic_sds_remote_device *device;
316cc9203bfSDan Williams 
317cc9203bfSDan Williams 	enum sci_status result = SCI_FAILURE;
318cc9203bfSDan Williams 
319cc9203bfSDan Williams 	frame_index = SCU_GET_FRAME_INDEX(completion_entry);
320cc9203bfSDan Williams 
321cc9203bfSDan Williams 	frame_header = scic->uf_control.buffers.array[frame_index].header;
322cc9203bfSDan Williams 	scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
323cc9203bfSDan Williams 
324cc9203bfSDan Williams 	if (SCU_GET_FRAME_ERROR(completion_entry)) {
325cc9203bfSDan Williams 		/*
326cc9203bfSDan Williams 		 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
327cc9203bfSDan Williams 		 * /       this cause a problem? We expect the phy initialization will
328cc9203bfSDan Williams 		 * /       fail if there is an error in the frame. */
329cc9203bfSDan Williams 		scic_sds_controller_release_frame(scic, frame_index);
330cc9203bfSDan Williams 		return;
331cc9203bfSDan Williams 	}
332cc9203bfSDan Williams 
333cc9203bfSDan Williams 	if (frame_header->is_address_frame) {
334cc9203bfSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
335*85280955SDan Williams 		iphy = &ihost->phys[index];
336*85280955SDan Williams 		result = scic_sds_phy_frame_handler(iphy, frame_index);
337cc9203bfSDan Williams 	} else {
338cc9203bfSDan Williams 
339cc9203bfSDan Williams 		index = SCU_GET_COMPLETION_INDEX(completion_entry);
340cc9203bfSDan Williams 
341cc9203bfSDan Williams 		if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
342cc9203bfSDan Williams 			/*
343cc9203bfSDan Williams 			 * This is a signature fis or a frame from a direct attached SATA
344cc9203bfSDan Williams 			 * device that has not yet been created.  In either case forwared
345cc9203bfSDan Williams 			 * the frame to the PE and let it take care of the frame data. */
346cc9203bfSDan Williams 			index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
347*85280955SDan Williams 			iphy = &ihost->phys[index];
348*85280955SDan Williams 			result = scic_sds_phy_frame_handler(iphy, frame_index);
349cc9203bfSDan Williams 		} else {
350cc9203bfSDan Williams 			if (index < scic->remote_node_entries)
351cc9203bfSDan Williams 				device = scic->device_table[index];
352cc9203bfSDan Williams 			else
353cc9203bfSDan Williams 				device = NULL;
354cc9203bfSDan Williams 
355cc9203bfSDan Williams 			if (device != NULL)
356cc9203bfSDan Williams 				result = scic_sds_remote_device_frame_handler(device, frame_index);
357cc9203bfSDan Williams 			else
358cc9203bfSDan Williams 				scic_sds_controller_release_frame(scic, frame_index);
359cc9203bfSDan Williams 		}
360cc9203bfSDan Williams 	}
361cc9203bfSDan Williams 
362cc9203bfSDan Williams 	if (result != SCI_SUCCESS) {
363cc9203bfSDan Williams 		/*
364cc9203bfSDan Williams 		 * / @todo Is there any reason to report some additional error message
365cc9203bfSDan Williams 		 * /       when we get this failure notifiction? */
366cc9203bfSDan Williams 	}
367cc9203bfSDan Williams }
368cc9203bfSDan Williams 
369cc9203bfSDan Williams static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
370cc9203bfSDan Williams 						 u32 completion_entry)
371cc9203bfSDan Williams {
372cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
373cc9203bfSDan Williams 	struct scic_sds_remote_device *device;
3745076a1a9SDan Williams 	struct isci_request *ireq;
375*85280955SDan Williams 	struct isci_phy *iphy;
376cc9203bfSDan Williams 	u32 index;
377cc9203bfSDan Williams 
378cc9203bfSDan Williams 	index = SCU_GET_COMPLETION_INDEX(completion_entry);
379cc9203bfSDan Williams 
380cc9203bfSDan Williams 	switch (scu_get_event_type(completion_entry)) {
381cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
382cc9203bfSDan Williams 		/* / @todo The driver did something wrong and we need to fix the condtion. */
383cc9203bfSDan Williams 		dev_err(scic_to_dev(scic),
384cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received SMU command error "
385cc9203bfSDan Williams 			"0x%x\n",
386cc9203bfSDan Williams 			__func__,
387cc9203bfSDan Williams 			scic,
388cc9203bfSDan Williams 			completion_entry);
389cc9203bfSDan Williams 		break;
390cc9203bfSDan Williams 
391cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
392cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_ERROR:
393cc9203bfSDan Williams 	case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
394cc9203bfSDan Williams 		/*
395cc9203bfSDan Williams 		 * / @todo This is a hardware failure and its likely that we want to
396cc9203bfSDan Williams 		 * /       reset the controller. */
397cc9203bfSDan Williams 		dev_err(scic_to_dev(scic),
398cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received fatal controller "
399cc9203bfSDan Williams 			"event  0x%x\n",
400cc9203bfSDan Williams 			__func__,
401cc9203bfSDan Williams 			scic,
402cc9203bfSDan Williams 			completion_entry);
403cc9203bfSDan Williams 		break;
404cc9203bfSDan Williams 
405cc9203bfSDan Williams 	case SCU_EVENT_TYPE_TRANSPORT_ERROR:
4065076a1a9SDan Williams 		ireq = ihost->reqs[index];
4075076a1a9SDan Williams 		scic_sds_io_request_event_handler(ireq, completion_entry);
408cc9203bfSDan Williams 		break;
409cc9203bfSDan Williams 
410cc9203bfSDan Williams 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
411cc9203bfSDan Williams 		switch (scu_get_event_specifier(completion_entry)) {
412cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
413cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
4145076a1a9SDan Williams 			ireq = ihost->reqs[index];
4155076a1a9SDan Williams 			if (ireq != NULL)
4165076a1a9SDan Williams 				scic_sds_io_request_event_handler(ireq, completion_entry);
417cc9203bfSDan Williams 			else
418cc9203bfSDan Williams 				dev_warn(scic_to_dev(scic),
419cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
420cc9203bfSDan Williams 					 "event 0x%x for io request object "
421cc9203bfSDan Williams 					 "that doesnt exist.\n",
422cc9203bfSDan Williams 					 __func__,
423cc9203bfSDan Williams 					 scic,
424cc9203bfSDan Williams 					 completion_entry);
425cc9203bfSDan Williams 
426cc9203bfSDan Williams 			break;
427cc9203bfSDan Williams 
428cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
429cc9203bfSDan Williams 			device = scic->device_table[index];
430cc9203bfSDan Williams 			if (device != NULL)
431cc9203bfSDan Williams 				scic_sds_remote_device_event_handler(device, completion_entry);
432cc9203bfSDan Williams 			else
433cc9203bfSDan Williams 				dev_warn(scic_to_dev(scic),
434cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
435cc9203bfSDan Williams 					 "event 0x%x for remote device object "
436cc9203bfSDan Williams 					 "that doesnt exist.\n",
437cc9203bfSDan Williams 					 __func__,
438cc9203bfSDan Williams 					 scic,
439cc9203bfSDan Williams 					 completion_entry);
440cc9203bfSDan Williams 
441cc9203bfSDan Williams 			break;
442cc9203bfSDan Williams 		}
443cc9203bfSDan Williams 		break;
444cc9203bfSDan Williams 
445cc9203bfSDan Williams 	case SCU_EVENT_TYPE_BROADCAST_CHANGE:
446cc9203bfSDan Williams 	/*
447cc9203bfSDan Williams 	 * direct the broadcast change event to the phy first and then let
448cc9203bfSDan Williams 	 * the phy redirect the broadcast change to the port object */
449cc9203bfSDan Williams 	case SCU_EVENT_TYPE_ERR_CNT_EVENT:
450cc9203bfSDan Williams 	/*
451cc9203bfSDan Williams 	 * direct error counter event to the phy object since that is where
452cc9203bfSDan Williams 	 * we get the event notification.  This is a type 4 event. */
453cc9203bfSDan Williams 	case SCU_EVENT_TYPE_OSSP_EVENT:
454cc9203bfSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
455*85280955SDan Williams 		iphy = &ihost->phys[index];
456*85280955SDan Williams 		scic_sds_phy_event_handler(iphy, completion_entry);
457cc9203bfSDan Williams 		break;
458cc9203bfSDan Williams 
459cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
460cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
461cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
462cc9203bfSDan Williams 		if (index < scic->remote_node_entries) {
463cc9203bfSDan Williams 			device = scic->device_table[index];
464cc9203bfSDan Williams 
465cc9203bfSDan Williams 			if (device != NULL)
466cc9203bfSDan Williams 				scic_sds_remote_device_event_handler(device, completion_entry);
467cc9203bfSDan Williams 		} else
468cc9203bfSDan Williams 			dev_err(scic_to_dev(scic),
469cc9203bfSDan Williams 				"%s: SCIC Controller 0x%p received event 0x%x "
470cc9203bfSDan Williams 				"for remote device object 0x%0x that doesnt "
471cc9203bfSDan Williams 				"exist.\n",
472cc9203bfSDan Williams 				__func__,
473cc9203bfSDan Williams 				scic,
474cc9203bfSDan Williams 				completion_entry,
475cc9203bfSDan Williams 				index);
476cc9203bfSDan Williams 
477cc9203bfSDan Williams 		break;
478cc9203bfSDan Williams 
479cc9203bfSDan Williams 	default:
480cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
481cc9203bfSDan Williams 			 "%s: SCIC Controller received unknown event code %x\n",
482cc9203bfSDan Williams 			 __func__,
483cc9203bfSDan Williams 			 completion_entry);
484cc9203bfSDan Williams 		break;
485cc9203bfSDan Williams 	}
486cc9203bfSDan Williams }
487cc9203bfSDan Williams 
488cc9203bfSDan Williams static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
489cc9203bfSDan Williams {
490cc9203bfSDan Williams 	u32 completion_count = 0;
491cc9203bfSDan Williams 	u32 completion_entry;
492cc9203bfSDan Williams 	u32 get_index;
493cc9203bfSDan Williams 	u32 get_cycle;
494994a9303SDan Williams 	u32 event_get;
495cc9203bfSDan Williams 	u32 event_cycle;
496cc9203bfSDan Williams 
497cc9203bfSDan Williams 	dev_dbg(scic_to_dev(scic),
498cc9203bfSDan Williams 		"%s: completion queue begining get:0x%08x\n",
499cc9203bfSDan Williams 		__func__,
500cc9203bfSDan Williams 		scic->completion_queue_get);
501cc9203bfSDan Williams 
502cc9203bfSDan Williams 	/* Get the component parts of the completion queue */
503cc9203bfSDan Williams 	get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
504cc9203bfSDan Williams 	get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
505cc9203bfSDan Williams 
506994a9303SDan Williams 	event_get = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
507cc9203bfSDan Williams 	event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
508cc9203bfSDan Williams 
509cc9203bfSDan Williams 	while (
510cc9203bfSDan Williams 		NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
511cc9203bfSDan Williams 		== COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
512cc9203bfSDan Williams 		) {
513cc9203bfSDan Williams 		completion_count++;
514cc9203bfSDan Williams 
515cc9203bfSDan Williams 		completion_entry = scic->completion_queue[get_index];
516994a9303SDan Williams 
517994a9303SDan Williams 		/* increment the get pointer and check for rollover to toggle the cycle bit */
518994a9303SDan Williams 		get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
519994a9303SDan Williams 			     (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
520994a9303SDan Williams 		get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
521cc9203bfSDan Williams 
522cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
523cc9203bfSDan Williams 			"%s: completion queue entry:0x%08x\n",
524cc9203bfSDan Williams 			__func__,
525cc9203bfSDan Williams 			completion_entry);
526cc9203bfSDan Williams 
527cc9203bfSDan Williams 		switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
528cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_TASK:
529cc9203bfSDan Williams 			scic_sds_controller_task_completion(scic, completion_entry);
530cc9203bfSDan Williams 			break;
531cc9203bfSDan Williams 
532cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_SDMA:
533cc9203bfSDan Williams 			scic_sds_controller_sdma_completion(scic, completion_entry);
534cc9203bfSDan Williams 			break;
535cc9203bfSDan Williams 
536cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_UFI:
537cc9203bfSDan Williams 			scic_sds_controller_unsolicited_frame(scic, completion_entry);
538cc9203bfSDan Williams 			break;
539cc9203bfSDan Williams 
540cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_EVENT:
541994a9303SDan Williams 		case SCU_COMPLETION_TYPE_NOTIFY: {
542994a9303SDan Williams 			event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
543994a9303SDan Williams 				       (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
544994a9303SDan Williams 			event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
545994a9303SDan Williams 
546cc9203bfSDan Williams 			scic_sds_controller_event_completion(scic, completion_entry);
547cc9203bfSDan Williams 			break;
548994a9303SDan Williams 		}
549cc9203bfSDan Williams 		default:
550cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
551cc9203bfSDan Williams 				 "%s: SCIC Controller received unknown "
552cc9203bfSDan Williams 				 "completion type %x\n",
553cc9203bfSDan Williams 				 __func__,
554cc9203bfSDan Williams 				 completion_entry);
555cc9203bfSDan Williams 			break;
556cc9203bfSDan Williams 		}
557cc9203bfSDan Williams 	}
558cc9203bfSDan Williams 
559cc9203bfSDan Williams 	/* Update the get register if we completed one or more entries */
560cc9203bfSDan Williams 	if (completion_count > 0) {
561cc9203bfSDan Williams 		scic->completion_queue_get =
562cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(ENABLE) |
563cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
564cc9203bfSDan Williams 			event_cycle |
565994a9303SDan Williams 			SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
566cc9203bfSDan Williams 			get_cycle |
567cc9203bfSDan Williams 			SMU_CQGR_GEN_VAL(POINTER, get_index);
568cc9203bfSDan Williams 
569cc9203bfSDan Williams 		writel(scic->completion_queue_get,
570cc9203bfSDan Williams 		       &scic->smu_registers->completion_queue_get);
571cc9203bfSDan Williams 
572cc9203bfSDan Williams 	}
573cc9203bfSDan Williams 
574cc9203bfSDan Williams 	dev_dbg(scic_to_dev(scic),
575cc9203bfSDan Williams 		"%s: completion queue ending get:0x%08x\n",
576cc9203bfSDan Williams 		__func__,
577cc9203bfSDan Williams 		scic->completion_queue_get);
578cc9203bfSDan Williams 
579cc9203bfSDan Williams }
580cc9203bfSDan Williams 
581cc9203bfSDan Williams static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
582cc9203bfSDan Williams {
583cc9203bfSDan Williams 	u32 interrupt_status;
584cc9203bfSDan Williams 
585cc9203bfSDan Williams 	interrupt_status =
586cc9203bfSDan Williams 		readl(&scic->smu_registers->interrupt_status);
587cc9203bfSDan Williams 
588cc9203bfSDan Williams 	if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
589cc9203bfSDan Williams 	    scic_sds_controller_completion_queue_has_entries(scic)) {
590cc9203bfSDan Williams 
591cc9203bfSDan Williams 		scic_sds_controller_process_completions(scic);
592cc9203bfSDan Williams 		writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
593cc9203bfSDan Williams 	} else {
594cc9203bfSDan Williams 		dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
595cc9203bfSDan Williams 			interrupt_status);
596cc9203bfSDan Williams 
597e301370aSEdmund Nadolski 		sci_change_state(&scic->sm, SCIC_FAILED);
598cc9203bfSDan Williams 
599cc9203bfSDan Williams 		return;
600cc9203bfSDan Williams 	}
601cc9203bfSDan Williams 
602cc9203bfSDan Williams 	/* If we dont process any completions I am not sure that we want to do this.
603cc9203bfSDan Williams 	 * We are in the middle of a hardware fault and should probably be reset.
604cc9203bfSDan Williams 	 */
605cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
606cc9203bfSDan Williams }
607cc9203bfSDan Williams 
608c7ef4031SDan Williams irqreturn_t isci_intx_isr(int vec, void *data)
6096f231ddaSDan Williams {
6106f231ddaSDan Williams 	irqreturn_t ret = IRQ_NONE;
61131e824edSDan Williams 	struct isci_host *ihost = data;
612cc3dbd0aSArtur Wojcik 	struct scic_sds_controller *scic = &ihost->sci;
6136f231ddaSDan Williams 
614c7ef4031SDan Williams 	if (scic_sds_controller_isr(scic)) {
61531e824edSDan Williams 		writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
616c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
6176f231ddaSDan Williams 		ret = IRQ_HANDLED;
61892f4f0f5SDan Williams 	} else if (scic_sds_controller_error_isr(scic)) {
61992f4f0f5SDan Williams 		spin_lock(&ihost->scic_lock);
62092f4f0f5SDan Williams 		scic_sds_controller_error_handler(scic);
62192f4f0f5SDan Williams 		spin_unlock(&ihost->scic_lock);
62292f4f0f5SDan Williams 		ret = IRQ_HANDLED;
6236f231ddaSDan Williams 	}
62492f4f0f5SDan Williams 
6256f231ddaSDan Williams 	return ret;
6266f231ddaSDan Williams }
6276f231ddaSDan Williams 
62892f4f0f5SDan Williams irqreturn_t isci_error_isr(int vec, void *data)
62992f4f0f5SDan Williams {
63092f4f0f5SDan Williams 	struct isci_host *ihost = data;
63192f4f0f5SDan Williams 
632cc3dbd0aSArtur Wojcik 	if (scic_sds_controller_error_isr(&ihost->sci))
633cc3dbd0aSArtur Wojcik 		scic_sds_controller_error_handler(&ihost->sci);
63492f4f0f5SDan Williams 
63592f4f0f5SDan Williams 	return IRQ_HANDLED;
63692f4f0f5SDan Williams }
6376f231ddaSDan Williams 
6386f231ddaSDan Williams /**
6396f231ddaSDan Williams  * isci_host_start_complete() - This function is called by the core library,
6406f231ddaSDan Williams  *    through the ISCI Module, to indicate controller start status.
6416f231ddaSDan Williams  * @isci_host: This parameter specifies the ISCI host object
6426f231ddaSDan Williams  * @completion_status: This parameter specifies the completion status from the
6436f231ddaSDan Williams  *    core library.
6446f231ddaSDan Williams  *
6456f231ddaSDan Williams  */
646cc9203bfSDan Williams static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
6476f231ddaSDan Williams {
6480cf89d1dSDan Williams 	if (completion_status != SCI_SUCCESS)
6490cf89d1dSDan Williams 		dev_info(&ihost->pdev->dev,
6500cf89d1dSDan Williams 			"controller start timed out, continuing...\n");
6510cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_ready);
6520cf89d1dSDan Williams 	clear_bit(IHOST_START_PENDING, &ihost->flags);
6530cf89d1dSDan Williams 	wake_up(&ihost->eventq);
6546f231ddaSDan Williams }
6556f231ddaSDan Williams 
656c7ef4031SDan Williams int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
6576f231ddaSDan Williams {
6584393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
6596f231ddaSDan Williams 
66077950f51SEdmund Nadolski 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
6616f231ddaSDan Williams 		return 0;
6626f231ddaSDan Williams 
66377950f51SEdmund Nadolski 	/* todo: use sas_flush_discovery once it is upstream */
66477950f51SEdmund Nadolski 	scsi_flush_work(shost);
66577950f51SEdmund Nadolski 
66677950f51SEdmund Nadolski 	scsi_flush_work(shost);
6676f231ddaSDan Williams 
6680cf89d1dSDan Williams 	dev_dbg(&ihost->pdev->dev,
6690cf89d1dSDan Williams 		"%s: ihost->status = %d, time = %ld\n",
6700cf89d1dSDan Williams 		 __func__, isci_host_get_state(ihost), time);
6716f231ddaSDan Williams 
6726f231ddaSDan Williams 	return 1;
6736f231ddaSDan Williams 
6746f231ddaSDan Williams }
6756f231ddaSDan Williams 
676cc9203bfSDan Williams /**
677cc9203bfSDan Williams  * scic_controller_get_suggested_start_timeout() - This method returns the
678cc9203bfSDan Williams  *    suggested scic_controller_start() timeout amount.  The user is free to
679cc9203bfSDan Williams  *    use any timeout value, but this method provides the suggested minimum
680cc9203bfSDan Williams  *    start timeout value.  The returned value is based upon empirical
681cc9203bfSDan Williams  *    information determined as a result of interoperability testing.
682cc9203bfSDan Williams  * @controller: the handle to the controller object for which to return the
683cc9203bfSDan Williams  *    suggested start timeout.
684cc9203bfSDan Williams  *
685cc9203bfSDan Williams  * This method returns the number of milliseconds for the suggested start
686cc9203bfSDan Williams  * operation timeout.
687cc9203bfSDan Williams  */
688cc9203bfSDan Williams static u32 scic_controller_get_suggested_start_timeout(
689cc9203bfSDan Williams 	struct scic_sds_controller *sc)
690cc9203bfSDan Williams {
691cc9203bfSDan Williams 	/* Validate the user supplied parameters. */
692cc9203bfSDan Williams 	if (sc == NULL)
693cc9203bfSDan Williams 		return 0;
694cc9203bfSDan Williams 
695cc9203bfSDan Williams 	/*
696cc9203bfSDan Williams 	 * The suggested minimum timeout value for a controller start operation:
697cc9203bfSDan Williams 	 *
698cc9203bfSDan Williams 	 *     Signature FIS Timeout
699cc9203bfSDan Williams 	 *   + Phy Start Timeout
700cc9203bfSDan Williams 	 *   + Number of Phy Spin Up Intervals
701cc9203bfSDan Williams 	 *   ---------------------------------
702cc9203bfSDan Williams 	 *   Number of milliseconds for the controller start operation.
703cc9203bfSDan Williams 	 *
704cc9203bfSDan Williams 	 * NOTE: The number of phy spin up intervals will be equivalent
705cc9203bfSDan Williams 	 *       to the number of phys divided by the number phys allowed
706cc9203bfSDan Williams 	 *       per interval - 1 (once OEM parameters are supported).
707cc9203bfSDan Williams 	 *       Currently we assume only 1 phy per interval. */
708cc9203bfSDan Williams 
709cc9203bfSDan Williams 	return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
710cc9203bfSDan Williams 		+ SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
711cc9203bfSDan Williams 		+ ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
712cc9203bfSDan Williams }
713cc9203bfSDan Williams 
714cc9203bfSDan Williams static void scic_controller_enable_interrupts(
715cc9203bfSDan Williams 	struct scic_sds_controller *scic)
716cc9203bfSDan Williams {
717cc9203bfSDan Williams 	BUG_ON(scic->smu_registers == NULL);
718cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
719cc9203bfSDan Williams }
720cc9203bfSDan Williams 
721cc9203bfSDan Williams void scic_controller_disable_interrupts(
722cc9203bfSDan Williams 	struct scic_sds_controller *scic)
723cc9203bfSDan Williams {
724cc9203bfSDan Williams 	BUG_ON(scic->smu_registers == NULL);
725cc9203bfSDan Williams 	writel(0xffffffff, &scic->smu_registers->interrupt_mask);
726cc9203bfSDan Williams }
727cc9203bfSDan Williams 
728cc9203bfSDan Williams static void scic_sds_controller_enable_port_task_scheduler(
729cc9203bfSDan Williams 	struct scic_sds_controller *scic)
730cc9203bfSDan Williams {
731cc9203bfSDan Williams 	u32 port_task_scheduler_value;
732cc9203bfSDan Williams 
733cc9203bfSDan Williams 	port_task_scheduler_value =
734cc9203bfSDan Williams 		readl(&scic->scu_registers->peg0.ptsg.control);
735cc9203bfSDan Williams 	port_task_scheduler_value |=
736cc9203bfSDan Williams 		(SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
737cc9203bfSDan Williams 		 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
738cc9203bfSDan Williams 	writel(port_task_scheduler_value,
739cc9203bfSDan Williams 	       &scic->scu_registers->peg0.ptsg.control);
740cc9203bfSDan Williams }
741cc9203bfSDan Williams 
742cc9203bfSDan Williams static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
743cc9203bfSDan Williams {
744cc9203bfSDan Williams 	u32 task_assignment;
745cc9203bfSDan Williams 
746cc9203bfSDan Williams 	/*
747cc9203bfSDan Williams 	 * Assign all the TCs to function 0
748cc9203bfSDan Williams 	 * TODO: Do we actually need to read this register to write it back?
749cc9203bfSDan Williams 	 */
750cc9203bfSDan Williams 
751cc9203bfSDan Williams 	task_assignment =
752cc9203bfSDan Williams 		readl(&scic->smu_registers->task_context_assignment[0]);
753cc9203bfSDan Williams 
754cc9203bfSDan Williams 	task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
755cc9203bfSDan Williams 		(SMU_TCA_GEN_VAL(ENDING,  scic->task_context_entries - 1)) |
756cc9203bfSDan Williams 		(SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
757cc9203bfSDan Williams 
758cc9203bfSDan Williams 	writel(task_assignment,
759cc9203bfSDan Williams 		&scic->smu_registers->task_context_assignment[0]);
760cc9203bfSDan Williams 
761cc9203bfSDan Williams }
762cc9203bfSDan Williams 
763cc9203bfSDan Williams static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
764cc9203bfSDan Williams {
765cc9203bfSDan Williams 	u32 index;
766cc9203bfSDan Williams 	u32 completion_queue_control_value;
767cc9203bfSDan Williams 	u32 completion_queue_get_value;
768cc9203bfSDan Williams 	u32 completion_queue_put_value;
769cc9203bfSDan Williams 
770cc9203bfSDan Williams 	scic->completion_queue_get = 0;
771cc9203bfSDan Williams 
7727c78da31SDan Williams 	completion_queue_control_value =
7737c78da31SDan Williams 		(SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
7747c78da31SDan Williams 		 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
775cc9203bfSDan Williams 
776cc9203bfSDan Williams 	writel(completion_queue_control_value,
777cc9203bfSDan Williams 	       &scic->smu_registers->completion_queue_control);
778cc9203bfSDan Williams 
779cc9203bfSDan Williams 
780cc9203bfSDan Williams 	/* Set the completion queue get pointer and enable the queue */
781cc9203bfSDan Williams 	completion_queue_get_value = (
782cc9203bfSDan Williams 		(SMU_CQGR_GEN_VAL(POINTER, 0))
783cc9203bfSDan Williams 		| (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
784cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(ENABLE))
785cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
786cc9203bfSDan Williams 		);
787cc9203bfSDan Williams 
788cc9203bfSDan Williams 	writel(completion_queue_get_value,
789cc9203bfSDan Williams 	       &scic->smu_registers->completion_queue_get);
790cc9203bfSDan Williams 
791cc9203bfSDan Williams 	/* Set the completion queue put pointer */
792cc9203bfSDan Williams 	completion_queue_put_value = (
793cc9203bfSDan Williams 		(SMU_CQPR_GEN_VAL(POINTER, 0))
794cc9203bfSDan Williams 		| (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
795cc9203bfSDan Williams 		);
796cc9203bfSDan Williams 
797cc9203bfSDan Williams 	writel(completion_queue_put_value,
798cc9203bfSDan Williams 	       &scic->smu_registers->completion_queue_put);
799cc9203bfSDan Williams 
800cc9203bfSDan Williams 	/* Initialize the cycle bit of the completion queue entries */
8017c78da31SDan Williams 	for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
802cc9203bfSDan Williams 		/*
803cc9203bfSDan Williams 		 * If get.cycle_bit != completion_queue.cycle_bit
804cc9203bfSDan Williams 		 * its not a valid completion queue entry
805cc9203bfSDan Williams 		 * so at system start all entries are invalid */
806cc9203bfSDan Williams 		scic->completion_queue[index] = 0x80000000;
807cc9203bfSDan Williams 	}
808cc9203bfSDan Williams }
809cc9203bfSDan Williams 
810cc9203bfSDan Williams static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
811cc9203bfSDan Williams {
812cc9203bfSDan Williams 	u32 frame_queue_control_value;
813cc9203bfSDan Williams 	u32 frame_queue_get_value;
814cc9203bfSDan Williams 	u32 frame_queue_put_value;
815cc9203bfSDan Williams 
816cc9203bfSDan Williams 	/* Write the queue size */
817cc9203bfSDan Williams 	frame_queue_control_value =
8187c78da31SDan Williams 		SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
819cc9203bfSDan Williams 
820cc9203bfSDan Williams 	writel(frame_queue_control_value,
821cc9203bfSDan Williams 	       &scic->scu_registers->sdma.unsolicited_frame_queue_control);
822cc9203bfSDan Williams 
823cc9203bfSDan Williams 	/* Setup the get pointer for the unsolicited frame queue */
824cc9203bfSDan Williams 	frame_queue_get_value = (
825cc9203bfSDan Williams 		SCU_UFQGP_GEN_VAL(POINTER, 0)
826cc9203bfSDan Williams 		|  SCU_UFQGP_GEN_BIT(ENABLE_BIT)
827cc9203bfSDan Williams 		);
828cc9203bfSDan Williams 
829cc9203bfSDan Williams 	writel(frame_queue_get_value,
830cc9203bfSDan Williams 	       &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
831cc9203bfSDan Williams 	/* Setup the put pointer for the unsolicited frame queue */
832cc9203bfSDan Williams 	frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
833cc9203bfSDan Williams 	writel(frame_queue_put_value,
834cc9203bfSDan Williams 	       &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
835cc9203bfSDan Williams }
836cc9203bfSDan Williams 
837cc9203bfSDan Williams /**
838cc9203bfSDan Williams  * This method will attempt to transition into the ready state for the
839cc9203bfSDan Williams  *    controller and indicate that the controller start operation has completed
840cc9203bfSDan Williams  *    if all criteria are met.
841cc9203bfSDan Williams  * @scic: This parameter indicates the controller object for which
842cc9203bfSDan Williams  *    to transition to ready.
843cc9203bfSDan Williams  * @status: This parameter indicates the status value to be pass into the call
844cc9203bfSDan Williams  *    to scic_cb_controller_start_complete().
845cc9203bfSDan Williams  *
846cc9203bfSDan Williams  * none.
847cc9203bfSDan Williams  */
848cc9203bfSDan Williams static void scic_sds_controller_transition_to_ready(
849cc9203bfSDan Williams 	struct scic_sds_controller *scic,
850cc9203bfSDan Williams 	enum sci_status status)
851cc9203bfSDan Williams {
852cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
853cc9203bfSDan Williams 
854e301370aSEdmund Nadolski 	if (scic->sm.current_state_id == SCIC_STARTING) {
855cc9203bfSDan Williams 		/*
856cc9203bfSDan Williams 		 * We move into the ready state, because some of the phys/ports
857cc9203bfSDan Williams 		 * may be up and operational.
858cc9203bfSDan Williams 		 */
859e301370aSEdmund Nadolski 		sci_change_state(&scic->sm, SCIC_READY);
860cc9203bfSDan Williams 
861cc9203bfSDan Williams 		isci_host_start_complete(ihost, status);
862cc9203bfSDan Williams 	}
863cc9203bfSDan Williams }
864cc9203bfSDan Williams 
865*85280955SDan Williams static bool is_phy_starting(struct isci_phy *iphy)
8664a33c525SAdam Gruchala {
8674a33c525SAdam Gruchala 	enum scic_sds_phy_states state;
8684a33c525SAdam Gruchala 
869*85280955SDan Williams 	state = iphy->sm.current_state_id;
8704a33c525SAdam Gruchala 	switch (state) {
871e301370aSEdmund Nadolski 	case SCI_PHY_STARTING:
872e301370aSEdmund Nadolski 	case SCI_PHY_SUB_INITIAL:
873e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
874e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_IAF_UF:
875e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SAS_POWER:
876e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_POWER:
877e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
878e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
879e301370aSEdmund Nadolski 	case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
880e301370aSEdmund Nadolski 	case SCI_PHY_SUB_FINAL:
8814a33c525SAdam Gruchala 		return true;
8824a33c525SAdam Gruchala 	default:
8834a33c525SAdam Gruchala 		return false;
8844a33c525SAdam Gruchala 	}
8854a33c525SAdam Gruchala }
8864a33c525SAdam Gruchala 
887cc9203bfSDan Williams /**
888cc9203bfSDan Williams  * scic_sds_controller_start_next_phy - start phy
889cc9203bfSDan Williams  * @scic: controller
890cc9203bfSDan Williams  *
891cc9203bfSDan Williams  * If all the phys have been started, then attempt to transition the
892cc9203bfSDan Williams  * controller to the READY state and inform the user
893cc9203bfSDan Williams  * (scic_cb_controller_start_complete()).
894cc9203bfSDan Williams  */
895cc9203bfSDan Williams static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
896cc9203bfSDan Williams {
897cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
898cc9203bfSDan Williams 	struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
899*85280955SDan Williams 	struct isci_phy *iphy;
900cc9203bfSDan Williams 	enum sci_status status;
901cc9203bfSDan Williams 
902cc9203bfSDan Williams 	status = SCI_SUCCESS;
903cc9203bfSDan Williams 
904cc9203bfSDan Williams 	if (scic->phy_startup_timer_pending)
905cc9203bfSDan Williams 		return status;
906cc9203bfSDan Williams 
907cc9203bfSDan Williams 	if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
908cc9203bfSDan Williams 		bool is_controller_start_complete = true;
909cc9203bfSDan Williams 		u32 state;
910cc9203bfSDan Williams 		u8 index;
911cc9203bfSDan Williams 
912cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
913*85280955SDan Williams 			iphy = &ihost->phys[index];
914*85280955SDan Williams 			state = iphy->sm.current_state_id;
915cc9203bfSDan Williams 
916*85280955SDan Williams 			if (!phy_get_non_dummy_port(iphy))
917cc9203bfSDan Williams 				continue;
918cc9203bfSDan Williams 
919cc9203bfSDan Williams 			/* The controller start operation is complete iff:
920cc9203bfSDan Williams 			 * - all links have been given an opportunity to start
921cc9203bfSDan Williams 			 * - have no indication of a connected device
922cc9203bfSDan Williams 			 * - have an indication of a connected device and it has
923cc9203bfSDan Williams 			 *   finished the link training process.
924cc9203bfSDan Williams 			 */
925*85280955SDan Williams 			if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
926*85280955SDan Williams 			    (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
927*85280955SDan Williams 			    (iphy->is_in_link_training == true && is_phy_starting(iphy))) {
928cc9203bfSDan Williams 				is_controller_start_complete = false;
929cc9203bfSDan Williams 				break;
930cc9203bfSDan Williams 			}
931cc9203bfSDan Williams 		}
932cc9203bfSDan Williams 
933cc9203bfSDan Williams 		/*
934cc9203bfSDan Williams 		 * The controller has successfully finished the start process.
935cc9203bfSDan Williams 		 * Inform the SCI Core user and transition to the READY state. */
936cc9203bfSDan Williams 		if (is_controller_start_complete == true) {
937cc9203bfSDan Williams 			scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
938bb3dbdf6SEdmund Nadolski 			sci_del_timer(&scic->phy_timer);
939bb3dbdf6SEdmund Nadolski 			scic->phy_startup_timer_pending = false;
940cc9203bfSDan Williams 		}
941cc9203bfSDan Williams 	} else {
942*85280955SDan Williams 		iphy = &ihost->phys[scic->next_phy_to_start];
943cc9203bfSDan Williams 
944cc9203bfSDan Williams 		if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
945*85280955SDan Williams 			if (phy_get_non_dummy_port(iphy) == NULL) {
946cc9203bfSDan Williams 				scic->next_phy_to_start++;
947cc9203bfSDan Williams 
948cc9203bfSDan Williams 				/* Caution recursion ahead be forwarned
949cc9203bfSDan Williams 				 *
950cc9203bfSDan Williams 				 * The PHY was never added to a PORT in MPC mode
951cc9203bfSDan Williams 				 * so start the next phy in sequence This phy
952cc9203bfSDan Williams 				 * will never go link up and will not draw power
953cc9203bfSDan Williams 				 * the OEM parameters either configured the phy
954cc9203bfSDan Williams 				 * incorrectly for the PORT or it was never
955cc9203bfSDan Williams 				 * assigned to a PORT
956cc9203bfSDan Williams 				 */
957cc9203bfSDan Williams 				return scic_sds_controller_start_next_phy(scic);
958cc9203bfSDan Williams 			}
959cc9203bfSDan Williams 		}
960cc9203bfSDan Williams 
961*85280955SDan Williams 		status = scic_sds_phy_start(iphy);
962cc9203bfSDan Williams 
963cc9203bfSDan Williams 		if (status == SCI_SUCCESS) {
964bb3dbdf6SEdmund Nadolski 			sci_mod_timer(&scic->phy_timer,
965bb3dbdf6SEdmund Nadolski 				      SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
966bb3dbdf6SEdmund Nadolski 			scic->phy_startup_timer_pending = true;
967cc9203bfSDan Williams 		} else {
968cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
969cc9203bfSDan Williams 				 "%s: Controller stop operation failed "
970cc9203bfSDan Williams 				 "to stop phy %d because of status "
971cc9203bfSDan Williams 				 "%d.\n",
972cc9203bfSDan Williams 				 __func__,
973*85280955SDan Williams 				 ihost->phys[scic->next_phy_to_start].phy_index,
974cc9203bfSDan Williams 				 status);
975cc9203bfSDan Williams 		}
976cc9203bfSDan Williams 
977cc9203bfSDan Williams 		scic->next_phy_to_start++;
978cc9203bfSDan Williams 	}
979cc9203bfSDan Williams 
980cc9203bfSDan Williams 	return status;
981cc9203bfSDan Williams }
982cc9203bfSDan Williams 
983bb3dbdf6SEdmund Nadolski static void phy_startup_timeout(unsigned long data)
984cc9203bfSDan Williams {
985bb3dbdf6SEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
986bb3dbdf6SEdmund Nadolski 	struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), phy_timer);
987bb3dbdf6SEdmund Nadolski 	struct isci_host *ihost = scic_to_ihost(scic);
988bb3dbdf6SEdmund Nadolski 	unsigned long flags;
989cc9203bfSDan Williams 	enum sci_status status;
990cc9203bfSDan Williams 
991bb3dbdf6SEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
992bb3dbdf6SEdmund Nadolski 
993bb3dbdf6SEdmund Nadolski 	if (tmr->cancel)
994bb3dbdf6SEdmund Nadolski 		goto done;
995bb3dbdf6SEdmund Nadolski 
996cc9203bfSDan Williams 	scic->phy_startup_timer_pending = false;
997bb3dbdf6SEdmund Nadolski 
998bb3dbdf6SEdmund Nadolski 	do {
999cc9203bfSDan Williams 		status = scic_sds_controller_start_next_phy(scic);
1000bb3dbdf6SEdmund Nadolski 	} while (status != SCI_SUCCESS);
1001bb3dbdf6SEdmund Nadolski 
1002bb3dbdf6SEdmund Nadolski done:
1003bb3dbdf6SEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1004cc9203bfSDan Williams }
1005cc9203bfSDan Williams 
1006ac668c69SDan Williams static u16 isci_tci_active(struct isci_host *ihost)
1007ac668c69SDan Williams {
1008ac668c69SDan Williams 	return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
1009ac668c69SDan Williams }
1010ac668c69SDan Williams 
1011cc9203bfSDan Williams static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
1012cc9203bfSDan Williams 					     u32 timeout)
1013cc9203bfSDan Williams {
1014cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1015cc9203bfSDan Williams 	enum sci_status result;
1016cc9203bfSDan Williams 	u16 index;
1017cc9203bfSDan Williams 
1018e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_INITIALIZED) {
1019cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
1020cc9203bfSDan Williams 			 "SCIC Controller start operation requested in "
1021cc9203bfSDan Williams 			 "invalid state\n");
1022cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1023cc9203bfSDan Williams 	}
1024cc9203bfSDan Williams 
1025cc9203bfSDan Williams 	/* Build the TCi free pool */
1026ac668c69SDan Williams 	BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1027ac668c69SDan Williams 	ihost->tci_head = 0;
1028ac668c69SDan Williams 	ihost->tci_tail = 0;
1029cc9203bfSDan Williams 	for (index = 0; index < scic->task_context_entries; index++)
1030ac668c69SDan Williams 		isci_tci_free(ihost, index);
1031cc9203bfSDan Williams 
1032cc9203bfSDan Williams 	/* Build the RNi free pool */
1033cc9203bfSDan Williams 	scic_sds_remote_node_table_initialize(
1034cc9203bfSDan Williams 			&scic->available_remote_nodes,
1035cc9203bfSDan Williams 			scic->remote_node_entries);
1036cc9203bfSDan Williams 
1037cc9203bfSDan Williams 	/*
1038cc9203bfSDan Williams 	 * Before anything else lets make sure we will not be
1039cc9203bfSDan Williams 	 * interrupted by the hardware.
1040cc9203bfSDan Williams 	 */
1041cc9203bfSDan Williams 	scic_controller_disable_interrupts(scic);
1042cc9203bfSDan Williams 
1043cc9203bfSDan Williams 	/* Enable the port task scheduler */
1044cc9203bfSDan Williams 	scic_sds_controller_enable_port_task_scheduler(scic);
1045cc9203bfSDan Williams 
1046cc9203bfSDan Williams 	/* Assign all the task entries to scic physical function */
1047cc9203bfSDan Williams 	scic_sds_controller_assign_task_entries(scic);
1048cc9203bfSDan Williams 
1049cc9203bfSDan Williams 	/* Now initialize the completion queue */
1050cc9203bfSDan Williams 	scic_sds_controller_initialize_completion_queue(scic);
1051cc9203bfSDan Williams 
1052cc9203bfSDan Williams 	/* Initialize the unsolicited frame queue for use */
1053cc9203bfSDan Williams 	scic_sds_controller_initialize_unsolicited_frame_queue(scic);
1054cc9203bfSDan Williams 
1055cc9203bfSDan Williams 	/* Start all of the ports on this controller */
1056cc9203bfSDan Williams 	for (index = 0; index < scic->logical_port_entries; index++) {
1057cc9203bfSDan Williams 		struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1058cc9203bfSDan Williams 
1059d76f71d9SPiotr Sawicki 		result = scic_sds_port_start(sci_port);
1060cc9203bfSDan Williams 		if (result)
1061cc9203bfSDan Williams 			return result;
1062cc9203bfSDan Williams 	}
1063cc9203bfSDan Williams 
1064cc9203bfSDan Williams 	scic_sds_controller_start_next_phy(scic);
1065cc9203bfSDan Williams 
10666cb5853dSEdmund Nadolski 	sci_mod_timer(&scic->timer, timeout);
1067cc9203bfSDan Williams 
1068e301370aSEdmund Nadolski 	sci_change_state(&scic->sm, SCIC_STARTING);
1069cc9203bfSDan Williams 
1070cc9203bfSDan Williams 	return SCI_SUCCESS;
1071cc9203bfSDan Williams }
1072cc9203bfSDan Williams 
10736f231ddaSDan Williams void isci_host_scan_start(struct Scsi_Host *shost)
10746f231ddaSDan Williams {
10754393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
1076cc3dbd0aSArtur Wojcik 	unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
10776f231ddaSDan Williams 
10780cf89d1dSDan Williams 	set_bit(IHOST_START_PENDING, &ihost->flags);
107977950f51SEdmund Nadolski 
108077950f51SEdmund Nadolski 	spin_lock_irq(&ihost->scic_lock);
1081cc3dbd0aSArtur Wojcik 	scic_controller_start(&ihost->sci, tmo);
1082cc3dbd0aSArtur Wojcik 	scic_controller_enable_interrupts(&ihost->sci);
108377950f51SEdmund Nadolski 	spin_unlock_irq(&ihost->scic_lock);
10846f231ddaSDan Williams }
10856f231ddaSDan Williams 
1086cc9203bfSDan Williams static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
10876f231ddaSDan Williams {
10880cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopped);
1089cc3dbd0aSArtur Wojcik 	scic_controller_disable_interrupts(&ihost->sci);
10900cf89d1dSDan Williams 	clear_bit(IHOST_STOP_PENDING, &ihost->flags);
10910cf89d1dSDan Williams 	wake_up(&ihost->eventq);
10926f231ddaSDan Williams }
10936f231ddaSDan Williams 
1094cc9203bfSDan Williams static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
1095cc9203bfSDan Williams {
1096cc9203bfSDan Williams 	/* Empty out the completion queue */
1097cc9203bfSDan Williams 	if (scic_sds_controller_completion_queue_has_entries(scic))
1098cc9203bfSDan Williams 		scic_sds_controller_process_completions(scic);
1099cc9203bfSDan Williams 
1100cc9203bfSDan Williams 	/* Clear the interrupt and enable all interrupts again */
1101cc9203bfSDan Williams 	writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
1102cc9203bfSDan Williams 	/* Could we write the value of SMU_ISR_COMPLETION? */
1103cc9203bfSDan Williams 	writel(0xFF000000, &scic->smu_registers->interrupt_mask);
1104cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
1105cc9203bfSDan Williams }
1106cc9203bfSDan Williams 
11076f231ddaSDan Williams /**
11086f231ddaSDan Williams  * isci_host_completion_routine() - This function is the delayed service
11096f231ddaSDan Williams  *    routine that calls the sci core library's completion handler. It's
11106f231ddaSDan Williams  *    scheduled as a tasklet from the interrupt service routine when interrupts
11116f231ddaSDan Williams  *    in use, or set as the timeout function in polled mode.
11126f231ddaSDan Williams  * @data: This parameter specifies the ISCI host object
11136f231ddaSDan Williams  *
11146f231ddaSDan Williams  */
11156f231ddaSDan Williams static void isci_host_completion_routine(unsigned long data)
11166f231ddaSDan Williams {
11176f231ddaSDan Williams 	struct isci_host *isci_host = (struct isci_host *)data;
11186f231ddaSDan Williams 	struct list_head    completed_request_list;
111911b00c19SJeff Skirvin 	struct list_head    errored_request_list;
11206f231ddaSDan Williams 	struct list_head    *current_position;
11216f231ddaSDan Williams 	struct list_head    *next_position;
11226f231ddaSDan Williams 	struct isci_request *request;
11236f231ddaSDan Williams 	struct isci_request *next_request;
11246f231ddaSDan Williams 	struct sas_task     *task;
11256f231ddaSDan Williams 
11266f231ddaSDan Williams 	INIT_LIST_HEAD(&completed_request_list);
112711b00c19SJeff Skirvin 	INIT_LIST_HEAD(&errored_request_list);
11286f231ddaSDan Williams 
11296f231ddaSDan Williams 	spin_lock_irq(&isci_host->scic_lock);
11306f231ddaSDan Williams 
1131cc3dbd0aSArtur Wojcik 	scic_sds_controller_completion_handler(&isci_host->sci);
1132c7ef4031SDan Williams 
11336f231ddaSDan Williams 	/* Take the lists of completed I/Os from the host. */
113411b00c19SJeff Skirvin 
11356f231ddaSDan Williams 	list_splice_init(&isci_host->requests_to_complete,
11366f231ddaSDan Williams 			 &completed_request_list);
11376f231ddaSDan Williams 
113811b00c19SJeff Skirvin 	/* Take the list of errored I/Os from the host. */
113911b00c19SJeff Skirvin 	list_splice_init(&isci_host->requests_to_errorback,
114011b00c19SJeff Skirvin 			 &errored_request_list);
11416f231ddaSDan Williams 
11426f231ddaSDan Williams 	spin_unlock_irq(&isci_host->scic_lock);
11436f231ddaSDan Williams 
11446f231ddaSDan Williams 	/* Process any completions in the lists. */
11456f231ddaSDan Williams 	list_for_each_safe(current_position, next_position,
11466f231ddaSDan Williams 			   &completed_request_list) {
11476f231ddaSDan Williams 
11486f231ddaSDan Williams 		request = list_entry(current_position, struct isci_request,
11496f231ddaSDan Williams 				     completed_node);
11506f231ddaSDan Williams 		task = isci_request_access_task(request);
11516f231ddaSDan Williams 
11526f231ddaSDan Williams 		/* Normal notification (task_done) */
11536f231ddaSDan Williams 		dev_dbg(&isci_host->pdev->dev,
11546f231ddaSDan Williams 			"%s: Normal - request/task = %p/%p\n",
11556f231ddaSDan Williams 			__func__,
11566f231ddaSDan Williams 			request,
11576f231ddaSDan Williams 			task);
11586f231ddaSDan Williams 
115911b00c19SJeff Skirvin 		/* Return the task to libsas */
116011b00c19SJeff Skirvin 		if (task != NULL) {
11616f231ddaSDan Williams 
116211b00c19SJeff Skirvin 			task->lldd_task = NULL;
116311b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
116411b00c19SJeff Skirvin 
116511b00c19SJeff Skirvin 				/* If the task is already in the abort path,
116611b00c19SJeff Skirvin 				* the task_done callback cannot be called.
116711b00c19SJeff Skirvin 				*/
116811b00c19SJeff Skirvin 				task->task_done(task);
116911b00c19SJeff Skirvin 			}
117011b00c19SJeff Skirvin 		}
1171312e0c24SDan Williams 
1172312e0c24SDan Williams 		spin_lock_irq(&isci_host->scic_lock);
11735076a1a9SDan Williams 		isci_free_tag(isci_host, request->io_tag);
1174312e0c24SDan Williams 		spin_unlock_irq(&isci_host->scic_lock);
11756f231ddaSDan Williams 	}
117611b00c19SJeff Skirvin 	list_for_each_entry_safe(request, next_request, &errored_request_list,
11776f231ddaSDan Williams 				 completed_node) {
11786f231ddaSDan Williams 
11796f231ddaSDan Williams 		task = isci_request_access_task(request);
11806f231ddaSDan Williams 
11816f231ddaSDan Williams 		/* Use sas_task_abort */
11826f231ddaSDan Williams 		dev_warn(&isci_host->pdev->dev,
11836f231ddaSDan Williams 			 "%s: Error - request/task = %p/%p\n",
11846f231ddaSDan Williams 			 __func__,
11856f231ddaSDan Williams 			 request,
11866f231ddaSDan Williams 			 task);
11876f231ddaSDan Williams 
118811b00c19SJeff Skirvin 		if (task != NULL) {
118911b00c19SJeff Skirvin 
119011b00c19SJeff Skirvin 			/* Put the task into the abort path if it's not there
119111b00c19SJeff Skirvin 			 * already.
119211b00c19SJeff Skirvin 			 */
119311b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
11946f231ddaSDan Williams 				sas_task_abort(task);
119511b00c19SJeff Skirvin 
119611b00c19SJeff Skirvin 		} else {
119711b00c19SJeff Skirvin 			/* This is a case where the request has completed with a
119811b00c19SJeff Skirvin 			 * status such that it needed further target servicing,
119911b00c19SJeff Skirvin 			 * but the sas_task reference has already been removed
120011b00c19SJeff Skirvin 			 * from the request.  Since it was errored, it was not
120111b00c19SJeff Skirvin 			 * being aborted, so there is nothing to do except free
120211b00c19SJeff Skirvin 			 * it.
120311b00c19SJeff Skirvin 			 */
120411b00c19SJeff Skirvin 
120511b00c19SJeff Skirvin 			spin_lock_irq(&isci_host->scic_lock);
120611b00c19SJeff Skirvin 			/* Remove the request from the remote device's list
120711b00c19SJeff Skirvin 			* of pending requests.
120811b00c19SJeff Skirvin 			*/
120911b00c19SJeff Skirvin 			list_del_init(&request->dev_node);
12105076a1a9SDan Williams 			isci_free_tag(isci_host, request->io_tag);
121111b00c19SJeff Skirvin 			spin_unlock_irq(&isci_host->scic_lock);
121211b00c19SJeff Skirvin 		}
12136f231ddaSDan Williams 	}
12146f231ddaSDan Williams 
12156f231ddaSDan Williams }
12166f231ddaSDan Williams 
1217cc9203bfSDan Williams /**
1218cc9203bfSDan Williams  * scic_controller_stop() - This method will stop an individual controller
1219cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1220cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1221cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1222cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1223cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1224cc9203bfSDan Williams  *    the hardware is halted.
1225cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1226cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1227cc9203bfSDan Williams  *    stop operation should complete.
1228cc9203bfSDan Williams  *
1229cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1230cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1231cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1232cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1233cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1234cc9203bfSDan Williams  */
1235cc9203bfSDan Williams static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
1236cc9203bfSDan Williams 					    u32 timeout)
1237cc9203bfSDan Williams {
1238e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_READY) {
1239cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
1240cc9203bfSDan Williams 			 "SCIC Controller stop operation requested in "
1241cc9203bfSDan Williams 			 "invalid state\n");
1242cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1243cc9203bfSDan Williams 	}
1244cc9203bfSDan Williams 
12456cb5853dSEdmund Nadolski 	sci_mod_timer(&scic->timer, timeout);
1246e301370aSEdmund Nadolski 	sci_change_state(&scic->sm, SCIC_STOPPING);
1247cc9203bfSDan Williams 	return SCI_SUCCESS;
1248cc9203bfSDan Williams }
1249cc9203bfSDan Williams 
1250cc9203bfSDan Williams /**
1251cc9203bfSDan Williams  * scic_controller_reset() - This method will reset the supplied core
1252cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1253cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1254cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1255cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1256cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1257cc9203bfSDan Williams  *
1258cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1259cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1260cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1261cc9203bfSDan Williams  */
1262cc9203bfSDan Williams static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
1263cc9203bfSDan Williams {
1264e301370aSEdmund Nadolski 	switch (scic->sm.current_state_id) {
1265e301370aSEdmund Nadolski 	case SCIC_RESET:
1266e301370aSEdmund Nadolski 	case SCIC_READY:
1267e301370aSEdmund Nadolski 	case SCIC_STOPPED:
1268e301370aSEdmund Nadolski 	case SCIC_FAILED:
1269cc9203bfSDan Williams 		/*
1270cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1271cc9203bfSDan Williams 		 * perform the state transition.
1272cc9203bfSDan Williams 		 */
1273e301370aSEdmund Nadolski 		sci_change_state(&scic->sm, SCIC_RESETTING);
1274cc9203bfSDan Williams 		return SCI_SUCCESS;
1275cc9203bfSDan Williams 	default:
1276cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
1277cc9203bfSDan Williams 			 "SCIC Controller reset operation requested in "
1278cc9203bfSDan Williams 			 "invalid state\n");
1279cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1280cc9203bfSDan Williams 	}
1281cc9203bfSDan Williams }
1282cc9203bfSDan Williams 
12830cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
12846f231ddaSDan Williams {
12856f231ddaSDan Williams 	int i;
12866f231ddaSDan Williams 
12870cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopping);
12886f231ddaSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++) {
1289e531381eSDan Williams 		struct isci_port *iport = &ihost->ports[i];
12900cf89d1dSDan Williams 		struct isci_remote_device *idev, *d;
12910cf89d1dSDan Williams 
1292e531381eSDan Williams 		list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
1293209fae14SDan Williams 			if (test_bit(IDEV_ALLOCATED, &idev->flags))
12946ad31fecSDan Williams 				isci_remote_device_stop(ihost, idev);
12956f231ddaSDan Williams 		}
12966f231ddaSDan Williams 	}
12976f231ddaSDan Williams 
12980cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
12997c40a803SDan Williams 
13007c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
1301cc3dbd0aSArtur Wojcik 	scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
13027c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
13037c40a803SDan Williams 
13040cf89d1dSDan Williams 	wait_for_stop(ihost);
1305cc3dbd0aSArtur Wojcik 	scic_controller_reset(&ihost->sci);
13065553ba2bSEdmund Nadolski 
13075553ba2bSEdmund Nadolski 	/* Cancel any/all outstanding port timers */
13085553ba2bSEdmund Nadolski 	for (i = 0; i < ihost->sci.logical_port_entries; i++) {
13095553ba2bSEdmund Nadolski 		struct scic_sds_port *sci_port = &ihost->ports[i].sci;
13105553ba2bSEdmund Nadolski 		del_timer_sync(&sci_port->timer.timer);
13115553ba2bSEdmund Nadolski 	}
13125553ba2bSEdmund Nadolski 
1313a628d478SEdmund Nadolski 	/* Cancel any/all outstanding phy timers */
1314a628d478SEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1315*85280955SDan Williams 		struct isci_phy *iphy = &ihost->phys[i];
1316*85280955SDan Williams 		del_timer_sync(&iphy->sata_timer.timer);
1317a628d478SEdmund Nadolski 	}
1318a628d478SEdmund Nadolski 
1319ac0eeb4fSEdmund Nadolski 	del_timer_sync(&ihost->sci.port_agent.timer.timer);
1320ac0eeb4fSEdmund Nadolski 
13210473661aSEdmund Nadolski 	del_timer_sync(&ihost->sci.power_control.timer.timer);
13220473661aSEdmund Nadolski 
13236cb5853dSEdmund Nadolski 	del_timer_sync(&ihost->sci.timer.timer);
13246cb5853dSEdmund Nadolski 
1325bb3dbdf6SEdmund Nadolski 	del_timer_sync(&ihost->sci.phy_timer.timer);
13266f231ddaSDan Williams }
13276f231ddaSDan Williams 
13286f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
13296f231ddaSDan Williams {
13306f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13316f231ddaSDan Williams 	int id = isci_host->id;
13326f231ddaSDan Williams 
13336f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
13346f231ddaSDan Williams }
13356f231ddaSDan Williams 
13366f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
13376f231ddaSDan Williams {
13386f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13396f231ddaSDan Williams 	int id = isci_host->id;
13406f231ddaSDan Williams 
13416f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13426f231ddaSDan Williams }
13436f231ddaSDan Williams 
1344b5f18a20SDave Jiang static void isci_user_parameters_get(
1345b5f18a20SDave Jiang 		struct isci_host *isci_host,
1346b5f18a20SDave Jiang 		union scic_user_parameters *scic_user_params)
1347b5f18a20SDave Jiang {
1348b5f18a20SDave Jiang 	struct scic_sds_user_parameters *u = &scic_user_params->sds1;
1349b5f18a20SDave Jiang 	int i;
1350b5f18a20SDave Jiang 
1351b5f18a20SDave Jiang 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1352b5f18a20SDave Jiang 		struct sci_phy_user_params *u_phy = &u->phys[i];
1353b5f18a20SDave Jiang 
1354b5f18a20SDave Jiang 		u_phy->max_speed_generation = phy_gen;
1355b5f18a20SDave Jiang 
1356b5f18a20SDave Jiang 		/* we are not exporting these for now */
1357b5f18a20SDave Jiang 		u_phy->align_insertion_frequency = 0x7f;
1358b5f18a20SDave Jiang 		u_phy->in_connection_align_insertion_frequency = 0xff;
1359b5f18a20SDave Jiang 		u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1360b5f18a20SDave Jiang 	}
1361b5f18a20SDave Jiang 
1362b5f18a20SDave Jiang 	u->stp_inactivity_timeout = stp_inactive_to;
1363b5f18a20SDave Jiang 	u->ssp_inactivity_timeout = ssp_inactive_to;
1364b5f18a20SDave Jiang 	u->stp_max_occupancy_timeout = stp_max_occ_to;
1365b5f18a20SDave Jiang 	u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1366b5f18a20SDave Jiang 	u->no_outbound_task_timeout = no_outbound_task_to;
1367b5f18a20SDave Jiang 	u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1368b5f18a20SDave Jiang }
1369b5f18a20SDave Jiang 
13709269e0e8SDan Williams static void scic_sds_controller_initial_state_enter(struct sci_base_state_machine *sm)
1371cc9203bfSDan Williams {
1372e301370aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
1373cc9203bfSDan Williams 
1374e301370aSEdmund Nadolski 	sci_change_state(&scic->sm, SCIC_RESET);
1375cc9203bfSDan Williams }
1376cc9203bfSDan Williams 
13779269e0e8SDan Williams static inline void scic_sds_controller_starting_state_exit(struct sci_base_state_machine *sm)
1378cc9203bfSDan Williams {
1379e301370aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
1380cc9203bfSDan Williams 
13816cb5853dSEdmund Nadolski 	sci_del_timer(&scic->timer);
1382cc9203bfSDan Williams }
1383cc9203bfSDan Williams 
1384cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1385cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1386cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1387cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1388cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1389cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1390cc9203bfSDan Williams 
1391cc9203bfSDan Williams /**
1392cc9203bfSDan Williams  * scic_controller_set_interrupt_coalescence() - This method allows the user to
1393cc9203bfSDan Williams  *    configure the interrupt coalescence.
1394cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1395cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1396cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1397cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1398cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1399cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1400cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1401cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1402cc9203bfSDan Williams  *    interrupt coalescing timeout.
1403cc9203bfSDan Williams  *
1404cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1405cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1406cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1407cc9203bfSDan Williams  */
1408cc9203bfSDan Williams static enum sci_status scic_controller_set_interrupt_coalescence(
1409cc9203bfSDan Williams 	struct scic_sds_controller *scic_controller,
1410cc9203bfSDan Williams 	u32 coalesce_number,
1411cc9203bfSDan Williams 	u32 coalesce_timeout)
1412cc9203bfSDan Williams {
1413cc9203bfSDan Williams 	u8 timeout_encode = 0;
1414cc9203bfSDan Williams 	u32 min = 0;
1415cc9203bfSDan Williams 	u32 max = 0;
1416cc9203bfSDan Williams 
1417cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1418cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1419cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1420cc9203bfSDan Williams 
1421cc9203bfSDan Williams 	/*
1422cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1423cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1424cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1425cc9203bfSDan Williams 	 *              0       -        -       Disabled
1426cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1427cc9203bfSDan Williams 	 *              2       26.7     40.0
1428cc9203bfSDan Williams 	 *              3       53.3     80.0
1429cc9203bfSDan Williams 	 *              4       106.7    160.0
1430cc9203bfSDan Williams 	 *              5       213.3    320.0
1431cc9203bfSDan Williams 	 *              6       426.7    640.0
1432cc9203bfSDan Williams 	 *              7       853.3    1280.0
1433cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1434cc9203bfSDan Williams 	 *              9       3.4      5.1
1435cc9203bfSDan Williams 	 *              10      6.8      10.2
1436cc9203bfSDan Williams 	 *              11      13.7     20.5
1437cc9203bfSDan Williams 	 *              12      27.3     41.0
1438cc9203bfSDan Williams 	 *              13      54.6     81.9
1439cc9203bfSDan Williams 	 *              14      109.2    163.8
1440cc9203bfSDan Williams 	 *              15      218.5    327.7
1441cc9203bfSDan Williams 	 *              16      436.9    655.4
1442cc9203bfSDan Williams 	 *              17      873.8    1310.7
1443cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1444cc9203bfSDan Williams 	 *              19      3.5      5.2
1445cc9203bfSDan Williams 	 *              20      7.0      10.5
1446cc9203bfSDan Williams 	 *              21      14.0     21.0
1447cc9203bfSDan Williams 	 *              22      28.0     41.9
1448cc9203bfSDan Williams 	 *              23      55.9     83.9
1449cc9203bfSDan Williams 	 *              24      111.8    167.8
1450cc9203bfSDan Williams 	 *              25      223.7    335.5
1451cc9203bfSDan Williams 	 *              26      447.4    671.1
1452cc9203bfSDan Williams 	 *              27      894.8    1342.2
1453cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1454cc9203bfSDan Williams 	 *              Others Undefined */
1455cc9203bfSDan Williams 
1456cc9203bfSDan Williams 	/*
1457cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1458cc9203bfSDan Williams 	 * value for register writing. */
1459cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1460cc9203bfSDan Williams 		timeout_encode = 0;
1461cc9203bfSDan Williams 	else{
1462cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1463cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1464cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1465cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1466cc9203bfSDan Williams 
1467cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1468cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1469cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1470cc9203bfSDan Williams 		      timeout_encode++) {
1471cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1472cc9203bfSDan Williams 				break;
1473cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1474cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1475cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1476cc9203bfSDan Williams 					break;
1477cc9203bfSDan Williams 				else{
1478cc9203bfSDan Williams 					timeout_encode++;
1479cc9203bfSDan Williams 					break;
1480cc9203bfSDan Williams 				}
1481cc9203bfSDan Williams 			} else {
1482cc9203bfSDan Williams 				max = max * 2;
1483cc9203bfSDan Williams 				min = min * 2;
1484cc9203bfSDan Williams 			}
1485cc9203bfSDan Williams 		}
1486cc9203bfSDan Williams 
1487cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1488cc9203bfSDan Williams 			/* the value is out of range. */
1489cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1490cc9203bfSDan Williams 	}
1491cc9203bfSDan Williams 
1492cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1493cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1494cc9203bfSDan Williams 	       &scic_controller->smu_registers->interrupt_coalesce_control);
1495cc9203bfSDan Williams 
1496cc9203bfSDan Williams 
1497cc9203bfSDan Williams 	scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
1498cc9203bfSDan Williams 	scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
1499cc9203bfSDan Williams 
1500cc9203bfSDan Williams 	return SCI_SUCCESS;
1501cc9203bfSDan Williams }
1502cc9203bfSDan Williams 
1503cc9203bfSDan Williams 
15049269e0e8SDan Williams static void scic_sds_controller_ready_state_enter(struct sci_base_state_machine *sm)
1505cc9203bfSDan Williams {
1506e301370aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
1507cc9203bfSDan Williams 
1508cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
1509cc9203bfSDan Williams 	scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
1510cc9203bfSDan Williams }
1511cc9203bfSDan Williams 
15129269e0e8SDan Williams static void scic_sds_controller_ready_state_exit(struct sci_base_state_machine *sm)
1513cc9203bfSDan Williams {
1514e301370aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
1515cc9203bfSDan Williams 
1516cc9203bfSDan Williams 	/* disable interrupt coalescence. */
1517cc9203bfSDan Williams 	scic_controller_set_interrupt_coalescence(scic, 0, 0);
1518cc9203bfSDan Williams }
1519cc9203bfSDan Williams 
1520cc9203bfSDan Williams static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
1521cc9203bfSDan Williams {
1522cc9203bfSDan Williams 	u32 index;
1523cc9203bfSDan Williams 	enum sci_status status;
1524cc9203bfSDan Williams 	enum sci_status phy_status;
1525cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1526cc9203bfSDan Williams 
1527cc9203bfSDan Williams 	status = SCI_SUCCESS;
1528cc9203bfSDan Williams 
1529cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1530*85280955SDan Williams 		phy_status = scic_sds_phy_stop(&ihost->phys[index]);
1531cc9203bfSDan Williams 
1532cc9203bfSDan Williams 		if (phy_status != SCI_SUCCESS &&
1533cc9203bfSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1534cc9203bfSDan Williams 			status = SCI_FAILURE;
1535cc9203bfSDan Williams 
1536cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
1537cc9203bfSDan Williams 				 "%s: Controller stop operation failed to stop "
1538cc9203bfSDan Williams 				 "phy %d because of status %d.\n",
1539cc9203bfSDan Williams 				 __func__,
1540*85280955SDan Williams 				 ihost->phys[index].phy_index, phy_status);
1541cc9203bfSDan Williams 		}
1542cc9203bfSDan Williams 	}
1543cc9203bfSDan Williams 
1544cc9203bfSDan Williams 	return status;
1545cc9203bfSDan Williams }
1546cc9203bfSDan Williams 
1547cc9203bfSDan Williams static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
1548cc9203bfSDan Williams {
1549cc9203bfSDan Williams 	u32 index;
1550cc9203bfSDan Williams 	enum sci_status port_status;
1551cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1552cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1553cc9203bfSDan Williams 
1554cc9203bfSDan Williams 	for (index = 0; index < scic->logical_port_entries; index++) {
1555cc9203bfSDan Williams 		struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1556cc9203bfSDan Williams 
15578bc80d30SPiotr Sawicki 		port_status = scic_sds_port_stop(sci_port);
1558cc9203bfSDan Williams 
1559cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1560cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1561cc9203bfSDan Williams 			status = SCI_FAILURE;
1562cc9203bfSDan Williams 
1563cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
1564cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1565cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1566cc9203bfSDan Williams 				 __func__,
1567cc9203bfSDan Williams 				 sci_port->logical_port_index,
1568cc9203bfSDan Williams 				 port_status);
1569cc9203bfSDan Williams 		}
1570cc9203bfSDan Williams 	}
1571cc9203bfSDan Williams 
1572cc9203bfSDan Williams 	return status;
1573cc9203bfSDan Williams }
1574cc9203bfSDan Williams 
1575cc9203bfSDan Williams static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
1576cc9203bfSDan Williams {
1577cc9203bfSDan Williams 	u32 index;
1578cc9203bfSDan Williams 	enum sci_status status;
1579cc9203bfSDan Williams 	enum sci_status device_status;
1580cc9203bfSDan Williams 
1581cc9203bfSDan Williams 	status = SCI_SUCCESS;
1582cc9203bfSDan Williams 
1583cc9203bfSDan Williams 	for (index = 0; index < scic->remote_node_entries; index++) {
1584cc9203bfSDan Williams 		if (scic->device_table[index] != NULL) {
1585cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
1586cc9203bfSDan Williams 			device_status = scic_remote_device_stop(scic->device_table[index], 0);
1587cc9203bfSDan Williams 
1588cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1589cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1590cc9203bfSDan Williams 				dev_warn(scic_to_dev(scic),
1591cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1592cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1593cc9203bfSDan Williams 					 "status %d.\n",
1594cc9203bfSDan Williams 					 __func__,
1595cc9203bfSDan Williams 					 scic->device_table[index], device_status);
1596cc9203bfSDan Williams 			}
1597cc9203bfSDan Williams 		}
1598cc9203bfSDan Williams 	}
1599cc9203bfSDan Williams 
1600cc9203bfSDan Williams 	return status;
1601cc9203bfSDan Williams }
1602cc9203bfSDan Williams 
16039269e0e8SDan Williams static void scic_sds_controller_stopping_state_enter(struct sci_base_state_machine *sm)
1604cc9203bfSDan Williams {
1605e301370aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
1606cc9203bfSDan Williams 
1607cc9203bfSDan Williams 	/* Stop all of the components for this controller */
1608cc9203bfSDan Williams 	scic_sds_controller_stop_phys(scic);
1609cc9203bfSDan Williams 	scic_sds_controller_stop_ports(scic);
1610cc9203bfSDan Williams 	scic_sds_controller_stop_devices(scic);
1611cc9203bfSDan Williams }
1612cc9203bfSDan Williams 
16139269e0e8SDan Williams static void scic_sds_controller_stopping_state_exit(struct sci_base_state_machine *sm)
1614cc9203bfSDan Williams {
1615e301370aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
1616cc9203bfSDan Williams 
16176cb5853dSEdmund Nadolski 	sci_del_timer(&scic->timer);
1618cc9203bfSDan Williams }
1619cc9203bfSDan Williams 
1620cc9203bfSDan Williams 
1621cc9203bfSDan Williams /**
1622cc9203bfSDan Williams  * scic_sds_controller_reset_hardware() -
1623cc9203bfSDan Williams  *
1624cc9203bfSDan Williams  * This method will reset the controller hardware.
1625cc9203bfSDan Williams  */
1626cc9203bfSDan Williams static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
1627cc9203bfSDan Williams {
1628cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
1629cc9203bfSDan Williams 	scic_controller_disable_interrupts(scic);
1630cc9203bfSDan Williams 
1631cc9203bfSDan Williams 	/* Reset the SCU */
1632cc9203bfSDan Williams 	writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
1633cc9203bfSDan Williams 
1634cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1635cc9203bfSDan Williams 	udelay(1000);
1636cc9203bfSDan Williams 
1637cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1638cc9203bfSDan Williams 	writel(0x00000000, &scic->smu_registers->completion_queue_get);
1639cc9203bfSDan Williams 
1640cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1641cc9203bfSDan Williams 	writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
1642cc9203bfSDan Williams }
1643cc9203bfSDan Williams 
16449269e0e8SDan Williams static void scic_sds_controller_resetting_state_enter(struct sci_base_state_machine *sm)
1645cc9203bfSDan Williams {
1646e301370aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
1647cc9203bfSDan Williams 
1648cc9203bfSDan Williams 	scic_sds_controller_reset_hardware(scic);
1649e301370aSEdmund Nadolski 	sci_change_state(&scic->sm, SCIC_RESET);
1650cc9203bfSDan Williams }
1651cc9203bfSDan Williams 
1652cc9203bfSDan Williams static const struct sci_base_state scic_sds_controller_state_table[] = {
1653e301370aSEdmund Nadolski 	[SCIC_INITIAL] = {
1654cc9203bfSDan Williams 		.enter_state = scic_sds_controller_initial_state_enter,
1655cc9203bfSDan Williams 	},
1656e301370aSEdmund Nadolski 	[SCIC_RESET] = {},
1657e301370aSEdmund Nadolski 	[SCIC_INITIALIZING] = {},
1658e301370aSEdmund Nadolski 	[SCIC_INITIALIZED] = {},
1659e301370aSEdmund Nadolski 	[SCIC_STARTING] = {
1660cc9203bfSDan Williams 		.exit_state  = scic_sds_controller_starting_state_exit,
1661cc9203bfSDan Williams 	},
1662e301370aSEdmund Nadolski 	[SCIC_READY] = {
1663cc9203bfSDan Williams 		.enter_state = scic_sds_controller_ready_state_enter,
1664cc9203bfSDan Williams 		.exit_state  = scic_sds_controller_ready_state_exit,
1665cc9203bfSDan Williams 	},
1666e301370aSEdmund Nadolski 	[SCIC_RESETTING] = {
1667cc9203bfSDan Williams 		.enter_state = scic_sds_controller_resetting_state_enter,
1668cc9203bfSDan Williams 	},
1669e301370aSEdmund Nadolski 	[SCIC_STOPPING] = {
1670cc9203bfSDan Williams 		.enter_state = scic_sds_controller_stopping_state_enter,
1671cc9203bfSDan Williams 		.exit_state = scic_sds_controller_stopping_state_exit,
1672cc9203bfSDan Williams 	},
1673e301370aSEdmund Nadolski 	[SCIC_STOPPED] = {},
1674e301370aSEdmund Nadolski 	[SCIC_FAILED] = {}
1675cc9203bfSDan Williams };
1676cc9203bfSDan Williams 
1677cc9203bfSDan Williams static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
1678cc9203bfSDan Williams {
1679cc9203bfSDan Williams 	/* these defaults are overridden by the platform / firmware */
1680cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1681cc9203bfSDan Williams 	u16 index;
1682cc9203bfSDan Williams 
1683cc9203bfSDan Williams 	/* Default to APC mode. */
1684cc9203bfSDan Williams 	scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1685cc9203bfSDan Williams 
1686cc9203bfSDan Williams 	/* Default to APC mode. */
1687cc9203bfSDan Williams 	scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
1688cc9203bfSDan Williams 
1689cc9203bfSDan Williams 	/* Default to no SSC operation. */
1690cc9203bfSDan Williams 	scic->oem_parameters.sds1.controller.do_enable_ssc = false;
1691cc9203bfSDan Williams 
1692cc9203bfSDan Williams 	/* Initialize all of the port parameter information to narrow ports. */
1693cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PORTS; index++) {
1694cc9203bfSDan Williams 		scic->oem_parameters.sds1.ports[index].phy_mask = 0;
1695cc9203bfSDan Williams 	}
1696cc9203bfSDan Williams 
1697cc9203bfSDan Williams 	/* Initialize all of the phy parameter information. */
1698cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1699cc9203bfSDan Williams 		/* Default to 6G (i.e. Gen 3) for now. */
1700cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
1701cc9203bfSDan Williams 
1702cc9203bfSDan Williams 		/* the frequencies cannot be 0 */
1703cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
1704cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
1705cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1706cc9203bfSDan Williams 
1707cc9203bfSDan Williams 		/*
1708cc9203bfSDan Williams 		 * Previous Vitesse based expanders had a arbitration issue that
1709cc9203bfSDan Williams 		 * is worked around by having the upper 32-bits of SAS address
1710cc9203bfSDan Williams 		 * with a value greater then the Vitesse company identifier.
1711cc9203bfSDan Williams 		 * Hence, usage of 0x5FCFFFFF. */
1712cc9203bfSDan Williams 		scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
1713cc9203bfSDan Williams 		scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
1714cc9203bfSDan Williams 	}
1715cc9203bfSDan Williams 
1716cc9203bfSDan Williams 	scic->user_parameters.sds1.stp_inactivity_timeout = 5;
1717cc9203bfSDan Williams 	scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
1718cc9203bfSDan Williams 	scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
1719cc9203bfSDan Williams 	scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
1720cc9203bfSDan Williams 	scic->user_parameters.sds1.no_outbound_task_timeout = 20;
1721cc9203bfSDan Williams }
1722cc9203bfSDan Williams 
17236cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data)
17246cb5853dSEdmund Nadolski {
17256cb5853dSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
17266cb5853dSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), timer);
17276cb5853dSEdmund Nadolski 	struct isci_host *ihost = scic_to_ihost(scic);
1728e301370aSEdmund Nadolski 	struct sci_base_state_machine *sm = &scic->sm;
17296cb5853dSEdmund Nadolski 	unsigned long flags;
1730cc9203bfSDan Williams 
17316cb5853dSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
17326cb5853dSEdmund Nadolski 
17336cb5853dSEdmund Nadolski 	if (tmr->cancel)
17346cb5853dSEdmund Nadolski 		goto done;
17356cb5853dSEdmund Nadolski 
1736e301370aSEdmund Nadolski 	if (sm->current_state_id == SCIC_STARTING)
17376cb5853dSEdmund Nadolski 		scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
1738e301370aSEdmund Nadolski 	else if (sm->current_state_id == SCIC_STOPPING) {
1739e301370aSEdmund Nadolski 		sci_change_state(sm, SCIC_FAILED);
17406cb5853dSEdmund Nadolski 		isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
17416cb5853dSEdmund Nadolski 	} else	/* / @todo Now what do we want to do in this case? */
17426cb5853dSEdmund Nadolski 		dev_err(scic_to_dev(scic),
17436cb5853dSEdmund Nadolski 			"%s: Controller timer fired when controller was not "
17446cb5853dSEdmund Nadolski 			"in a state being timed.\n",
17456cb5853dSEdmund Nadolski 			__func__);
17466cb5853dSEdmund Nadolski 
17476cb5853dSEdmund Nadolski done:
17486cb5853dSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
17496cb5853dSEdmund Nadolski }
1750cc9203bfSDan Williams 
1751cc9203bfSDan Williams /**
1752cc9203bfSDan Williams  * scic_controller_construct() - This method will attempt to construct a
1753cc9203bfSDan Williams  *    controller object utilizing the supplied parameter information.
1754cc9203bfSDan Williams  * @c: This parameter specifies the controller to be constructed.
1755cc9203bfSDan Williams  * @scu_base: mapped base address of the scu registers
1756cc9203bfSDan Williams  * @smu_base: mapped base address of the smu registers
1757cc9203bfSDan Williams  *
1758cc9203bfSDan Williams  * Indicate if the controller was successfully constructed or if it failed in
1759cc9203bfSDan Williams  * some way. SCI_SUCCESS This value is returned if the controller was
1760cc9203bfSDan Williams  * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
1761cc9203bfSDan Williams  * if the interrupt coalescence timer may cause SAS compliance issues for SMP
1762cc9203bfSDan Williams  * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
1763cc9203bfSDan Williams  * This value is returned if the controller does not support the supplied type.
1764cc9203bfSDan Williams  * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
1765cc9203bfSDan Williams  * controller does not support the supplied initialization data version.
1766cc9203bfSDan Williams  */
1767cc9203bfSDan Williams static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
1768cc9203bfSDan Williams 					  void __iomem *scu_base,
1769cc9203bfSDan Williams 					  void __iomem *smu_base)
1770cc9203bfSDan Williams {
1771cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1772cc9203bfSDan Williams 	u8 i;
1773cc9203bfSDan Williams 
177412ef6544SEdmund Nadolski 	sci_init_sm(&scic->sm, scic_sds_controller_state_table, SCIC_INITIAL);
1775cc9203bfSDan Williams 
1776cc9203bfSDan Williams 	scic->scu_registers = scu_base;
1777cc9203bfSDan Williams 	scic->smu_registers = smu_base;
1778cc9203bfSDan Williams 
1779cc9203bfSDan Williams 	scic_sds_port_configuration_agent_construct(&scic->port_agent);
1780cc9203bfSDan Williams 
1781cc9203bfSDan Williams 	/* Construct the ports for this controller */
1782cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1783cc9203bfSDan Williams 		scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
1784cc9203bfSDan Williams 	scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
1785cc9203bfSDan Williams 
1786cc9203bfSDan Williams 	/* Construct the phys for this controller */
1787cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1788cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
1789*85280955SDan Williams 		scic_sds_phy_construct(&ihost->phys[i],
1790cc9203bfSDan Williams 				       &ihost->ports[SCI_MAX_PORTS].sci, i);
1791cc9203bfSDan Williams 	}
1792cc9203bfSDan Williams 
1793cc9203bfSDan Williams 	scic->invalid_phy_mask = 0;
1794cc9203bfSDan Williams 
17956cb5853dSEdmund Nadolski 	sci_init_timer(&scic->timer, controller_timeout);
17966cb5853dSEdmund Nadolski 
1797cc9203bfSDan Williams 	/* Initialize the User and OEM parameters to default values. */
1798cc9203bfSDan Williams 	scic_sds_controller_set_default_config_parameters(scic);
1799cc9203bfSDan Williams 
1800cc9203bfSDan Williams 	return scic_controller_reset(scic);
1801cc9203bfSDan Williams }
1802cc9203bfSDan Williams 
1803cc9203bfSDan Williams int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
1804cc9203bfSDan Williams {
1805cc9203bfSDan Williams 	int i;
1806cc9203bfSDan Williams 
1807cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1808cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1809cc9203bfSDan Williams 			return -EINVAL;
1810cc9203bfSDan Williams 
1811cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1812cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1813cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1814cc9203bfSDan Williams 			return -EINVAL;
1815cc9203bfSDan Williams 
1816cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1817cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1818cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1819cc9203bfSDan Williams 				return -EINVAL;
1820cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1821cc9203bfSDan Williams 		u8 phy_mask = 0;
1822cc9203bfSDan Williams 
1823cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1824cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1825cc9203bfSDan Williams 
1826cc9203bfSDan Williams 		if (phy_mask == 0)
1827cc9203bfSDan Williams 			return -EINVAL;
1828cc9203bfSDan Williams 	} else
1829cc9203bfSDan Williams 		return -EINVAL;
1830cc9203bfSDan Williams 
1831cc9203bfSDan Williams 	if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1832cc9203bfSDan Williams 		return -EINVAL;
1833cc9203bfSDan Williams 
1834cc9203bfSDan Williams 	return 0;
1835cc9203bfSDan Williams }
1836cc9203bfSDan Williams 
1837cc9203bfSDan Williams static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
1838cc9203bfSDan Williams 					union scic_oem_parameters *scic_parms)
1839cc9203bfSDan Williams {
1840e301370aSEdmund Nadolski 	u32 state = scic->sm.current_state_id;
1841cc9203bfSDan Williams 
1842e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
1843e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
1844e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
1845cc9203bfSDan Williams 
1846cc9203bfSDan Williams 		if (scic_oem_parameters_validate(&scic_parms->sds1))
1847cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1848cc9203bfSDan Williams 		scic->oem_parameters.sds1 = scic_parms->sds1;
1849cc9203bfSDan Williams 
1850cc9203bfSDan Williams 		return SCI_SUCCESS;
1851cc9203bfSDan Williams 	}
1852cc9203bfSDan Williams 
1853cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
1854cc9203bfSDan Williams }
1855cc9203bfSDan Williams 
1856cc9203bfSDan Williams void scic_oem_parameters_get(
1857cc9203bfSDan Williams 	struct scic_sds_controller *scic,
1858cc9203bfSDan Williams 	union scic_oem_parameters *scic_parms)
1859cc9203bfSDan Williams {
1860cc9203bfSDan Williams 	memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
1861cc9203bfSDan Williams }
1862cc9203bfSDan Williams 
18630473661aSEdmund Nadolski static void power_control_timeout(unsigned long data)
1864cc9203bfSDan Williams {
18650473661aSEdmund Nadolski 	struct sci_timer *tmr = (struct sci_timer *)data;
18660473661aSEdmund Nadolski 	struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), power_control.timer);
18670473661aSEdmund Nadolski 	struct isci_host *ihost = scic_to_ihost(scic);
1868*85280955SDan Williams 	struct isci_phy *iphy;
18690473661aSEdmund Nadolski 	unsigned long flags;
18700473661aSEdmund Nadolski 	u8 i;
1871cc9203bfSDan Williams 
18720473661aSEdmund Nadolski 	spin_lock_irqsave(&ihost->scic_lock, flags);
1873cc9203bfSDan Williams 
18740473661aSEdmund Nadolski 	if (tmr->cancel)
18750473661aSEdmund Nadolski 		goto done;
1876cc9203bfSDan Williams 
1877cc9203bfSDan Williams 	scic->power_control.phys_granted_power = 0;
1878cc9203bfSDan Williams 
1879cc9203bfSDan Williams 	if (scic->power_control.phys_waiting == 0) {
1880cc9203bfSDan Williams 		scic->power_control.timer_started = false;
18810473661aSEdmund Nadolski 		goto done;
18820473661aSEdmund Nadolski 	}
1883cc9203bfSDan Williams 
18840473661aSEdmund Nadolski 	for (i = 0; i < SCI_MAX_PHYS; i++) {
18850473661aSEdmund Nadolski 
18860473661aSEdmund Nadolski 		if (scic->power_control.phys_waiting == 0)
18870473661aSEdmund Nadolski 			break;
18880473661aSEdmund Nadolski 
1889*85280955SDan Williams 		iphy = scic->power_control.requesters[i];
1890*85280955SDan Williams 		if (iphy == NULL)
18910473661aSEdmund Nadolski 			continue;
18920473661aSEdmund Nadolski 
18930473661aSEdmund Nadolski 		if (scic->power_control.phys_granted_power >=
18940473661aSEdmund Nadolski 		    scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up)
18950473661aSEdmund Nadolski 			break;
18960473661aSEdmund Nadolski 
1897cc9203bfSDan Williams 		scic->power_control.requesters[i] = NULL;
1898cc9203bfSDan Williams 		scic->power_control.phys_waiting--;
1899cc9203bfSDan Williams 		scic->power_control.phys_granted_power++;
1900*85280955SDan Williams 		scic_sds_phy_consume_power_handler(iphy);
1901cc9203bfSDan Williams 	}
1902cc9203bfSDan Williams 
1903cc9203bfSDan Williams 	/*
1904cc9203bfSDan Williams 	 * It doesn't matter if the power list is empty, we need to start the
1905cc9203bfSDan Williams 	 * timer in case another phy becomes ready.
1906cc9203bfSDan Williams 	 */
19070473661aSEdmund Nadolski 	sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
19080473661aSEdmund Nadolski 	scic->power_control.timer_started = true;
19090473661aSEdmund Nadolski 
19100473661aSEdmund Nadolski done:
19110473661aSEdmund Nadolski 	spin_unlock_irqrestore(&ihost->scic_lock, flags);
1912cc9203bfSDan Williams }
1913cc9203bfSDan Williams 
1914cc9203bfSDan Williams /**
1915cc9203bfSDan Williams  * This method inserts the phy in the stagger spinup control queue.
1916cc9203bfSDan Williams  * @scic:
1917cc9203bfSDan Williams  *
1918cc9203bfSDan Williams  *
1919cc9203bfSDan Williams  */
1920cc9203bfSDan Williams void scic_sds_controller_power_control_queue_insert(
1921cc9203bfSDan Williams 	struct scic_sds_controller *scic,
1922*85280955SDan Williams 	struct isci_phy *iphy)
1923cc9203bfSDan Williams {
1924*85280955SDan Williams 	BUG_ON(iphy == NULL);
1925cc9203bfSDan Williams 
1926cc9203bfSDan Williams 	if (scic->power_control.phys_granted_power <
1927cc9203bfSDan Williams 	    scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
1928cc9203bfSDan Williams 		scic->power_control.phys_granted_power++;
1929*85280955SDan Williams 		scic_sds_phy_consume_power_handler(iphy);
1930cc9203bfSDan Williams 
1931cc9203bfSDan Williams 		/*
1932cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1933cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1934cc9203bfSDan Williams 		 */
19350473661aSEdmund Nadolski 		if (scic->power_control.timer_started)
19360473661aSEdmund Nadolski 			sci_del_timer(&scic->power_control.timer);
19370473661aSEdmund Nadolski 
19380473661aSEdmund Nadolski 		sci_mod_timer(&scic->power_control.timer,
19390473661aSEdmund Nadolski 				 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
19400473661aSEdmund Nadolski 		scic->power_control.timer_started = true;
19410473661aSEdmund Nadolski 
1942cc9203bfSDan Williams 	} else {
1943cc9203bfSDan Williams 		/* Add the phy in the waiting list */
1944*85280955SDan Williams 		scic->power_control.requesters[iphy->phy_index] = iphy;
1945cc9203bfSDan Williams 		scic->power_control.phys_waiting++;
1946cc9203bfSDan Williams 	}
1947cc9203bfSDan Williams }
1948cc9203bfSDan Williams 
1949cc9203bfSDan Williams /**
1950cc9203bfSDan Williams  * This method removes the phy from the stagger spinup control queue.
1951cc9203bfSDan Williams  * @scic:
1952cc9203bfSDan Williams  *
1953cc9203bfSDan Williams  *
1954cc9203bfSDan Williams  */
1955cc9203bfSDan Williams void scic_sds_controller_power_control_queue_remove(
1956cc9203bfSDan Williams 	struct scic_sds_controller *scic,
1957*85280955SDan Williams 	struct isci_phy *iphy)
1958cc9203bfSDan Williams {
1959*85280955SDan Williams 	BUG_ON(iphy == NULL);
1960cc9203bfSDan Williams 
1961*85280955SDan Williams 	if (scic->power_control.requesters[iphy->phy_index] != NULL) {
1962cc9203bfSDan Williams 		scic->power_control.phys_waiting--;
1963cc9203bfSDan Williams 	}
1964cc9203bfSDan Williams 
1965*85280955SDan Williams 	scic->power_control.requesters[iphy->phy_index] = NULL;
1966cc9203bfSDan Williams }
1967cc9203bfSDan Williams 
1968cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
1969cc9203bfSDan Williams 
1970cc9203bfSDan Williams /* Initialize the AFE for this phy index. We need to read the AFE setup from
1971cc9203bfSDan Williams  * the OEM parameters
1972cc9203bfSDan Williams  */
1973cc9203bfSDan Williams static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
1974cc9203bfSDan Williams {
1975cc9203bfSDan Williams 	const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
1976cc9203bfSDan Williams 	u32 afe_status;
1977cc9203bfSDan Williams 	u32 phy_id;
1978cc9203bfSDan Williams 
1979cc9203bfSDan Williams 	/* Clear DFX Status registers */
1980cc9203bfSDan Williams 	writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
1981cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1982cc9203bfSDan Williams 
1983cc9203bfSDan Williams 	if (is_b0()) {
1984cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
1985cc9203bfSDan Williams 		 * Timer, PM Stagger Timer */
1986cc9203bfSDan Williams 		writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
1987cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
1988cc9203bfSDan Williams 	}
1989cc9203bfSDan Williams 
1990cc9203bfSDan Williams 	/* Configure bias currents to normal */
1991cc9203bfSDan Williams 	if (is_a0())
1992cc9203bfSDan Williams 		writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
1993cc9203bfSDan Williams 	else if (is_a2())
1994cc9203bfSDan Williams 		writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
1995dbb0743aSAdam Gruchala 	else if (is_b0() || is_c0())
1996cc9203bfSDan Williams 		writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
1997cc9203bfSDan Williams 
1998cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
1999cc9203bfSDan Williams 
2000cc9203bfSDan Williams 	/* Enable PLL */
2001dbb0743aSAdam Gruchala 	if (is_b0() || is_c0())
2002cc9203bfSDan Williams 		writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
2003cc9203bfSDan Williams 	else
2004cc9203bfSDan Williams 		writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
2005cc9203bfSDan Williams 
2006cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2007cc9203bfSDan Williams 
2008cc9203bfSDan Williams 	/* Wait for the PLL to lock */
2009cc9203bfSDan Williams 	do {
2010cc9203bfSDan Williams 		afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
2011cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2012cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
2013cc9203bfSDan Williams 
2014cc9203bfSDan Williams 	if (is_a0() || is_a2()) {
2015cc9203bfSDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
2016cc9203bfSDan Williams 		writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
2017cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2018cc9203bfSDan Williams 	}
2019cc9203bfSDan Williams 
2020cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
2021cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2022cc9203bfSDan Williams 
2023cc9203bfSDan Williams 		if (is_b0()) {
2024cc9203bfSDan Williams 			 /* Configure transmitter SSC parameters */
2025cc9203bfSDan Williams 			writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2026cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2027dbb0743aSAdam Gruchala 		} else if (is_c0()) {
2028dbb0743aSAdam Gruchala 			 /* Configure transmitter SSC parameters */
2029dbb0743aSAdam Gruchala 			writel(0x0003000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2030dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2031dbb0743aSAdam Gruchala 
2032dbb0743aSAdam Gruchala 			/*
2033dbb0743aSAdam Gruchala 			 * All defaults, except the Receive Word Alignament/Comma Detect
2034dbb0743aSAdam Gruchala 			 * Enable....(0xe800) */
2035dbb0743aSAdam Gruchala 			writel(0x00004500, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2036dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2037cc9203bfSDan Williams 		} else {
2038cc9203bfSDan Williams 			/*
2039cc9203bfSDan Williams 			 * All defaults, except the Receive Word Alignament/Comma Detect
2040cc9203bfSDan Williams 			 * Enable....(0xe800) */
2041cc9203bfSDan Williams 			writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2042cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2043cc9203bfSDan Williams 
2044cc9203bfSDan Williams 			writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
2045cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2046cc9203bfSDan Williams 		}
2047cc9203bfSDan Williams 
2048cc9203bfSDan Williams 		/*
2049cc9203bfSDan Williams 		 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2050cc9203bfSDan Williams 		 * & increase TX int & ext bias 20%....(0xe85c) */
2051cc9203bfSDan Williams 		if (is_a0())
2052cc9203bfSDan Williams 			writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2053cc9203bfSDan Williams 		else if (is_a2())
2054cc9203bfSDan Williams 			writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2055dbb0743aSAdam Gruchala 		else if (is_b0()) {
2056cc9203bfSDan Williams 			 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
2057dbb0743aSAdam Gruchala 			writel(0x000003D7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2058cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2059cc9203bfSDan Williams 
2060cc9203bfSDan Williams 			/*
2061cc9203bfSDan Williams 			 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2062cc9203bfSDan Williams 			 * & increase TX int & ext bias 20%....(0xe85c) */
2063dbb0743aSAdam Gruchala 			writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2064dbb0743aSAdam Gruchala 		} else {
2065dbb0743aSAdam Gruchala 			writel(0x000001E7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2066dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2067dbb0743aSAdam Gruchala 
2068dbb0743aSAdam Gruchala 			/*
2069dbb0743aSAdam Gruchala 			 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2070dbb0743aSAdam Gruchala 			 * & increase TX int & ext bias 20%....(0xe85c) */
2071dbb0743aSAdam Gruchala 			writel(0x000001E4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2072cc9203bfSDan Williams 		}
2073cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2074cc9203bfSDan Williams 
2075cc9203bfSDan Williams 		if (is_a0() || is_a2()) {
2076cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
2077cc9203bfSDan Williams 			writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2078cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2079cc9203bfSDan Williams 		}
2080cc9203bfSDan Williams 
2081cc9203bfSDan Williams 		/*
2082cc9203bfSDan Williams 		 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
2083cc9203bfSDan Williams 		 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
2084cc9203bfSDan Williams 		writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2085cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2086cc9203bfSDan Williams 
2087cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2088cc9203bfSDan Williams 		if (is_a0())
2089cc9203bfSDan Williams 			writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2090cc9203bfSDan Williams 		else if (is_a2())
2091cc9203bfSDan Williams 			writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2092dbb0743aSAdam Gruchala 		else if (is_b0()) {
2093cc9203bfSDan Williams 			writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2094cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2095cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
2096cc9203bfSDan Williams 			writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2097dbb0743aSAdam Gruchala 		} else {
2098dbb0743aSAdam Gruchala 			writel(0x0140DF0F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
2099dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2100dbb0743aSAdam Gruchala 
2101dbb0743aSAdam Gruchala 			writel(0x3F6F103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2102dbb0743aSAdam Gruchala 			udelay(AFE_REGISTER_WRITE_DELAY);
2103dbb0743aSAdam Gruchala 
2104dbb0743aSAdam Gruchala 			/* Enable TX equalization (0xe824) */
2105dbb0743aSAdam Gruchala 			writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2106cc9203bfSDan Williams 		}
2107dbb0743aSAdam Gruchala 
2108cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2109cc9203bfSDan Williams 
2110cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control0,
2111cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
2112cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2113cc9203bfSDan Williams 
2114cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control1,
2115cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
2116cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2117cc9203bfSDan Williams 
2118cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control2,
2119cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
2120cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2121cc9203bfSDan Williams 
2122cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control3,
2123cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
2124cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2125cc9203bfSDan Williams 	}
2126cc9203bfSDan Williams 
2127cc9203bfSDan Williams 	/* Transfer control to the PEs */
2128cc9203bfSDan Williams 	writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
2129cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2130cc9203bfSDan Williams }
2131cc9203bfSDan Williams 
2132cc9203bfSDan Williams static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
2133cc9203bfSDan Williams {
21340473661aSEdmund Nadolski 	sci_init_timer(&scic->power_control.timer, power_control_timeout);
2135cc9203bfSDan Williams 
2136cc9203bfSDan Williams 	memset(scic->power_control.requesters, 0,
2137cc9203bfSDan Williams 	       sizeof(scic->power_control.requesters));
2138cc9203bfSDan Williams 
2139cc9203bfSDan Williams 	scic->power_control.phys_waiting = 0;
2140cc9203bfSDan Williams 	scic->power_control.phys_granted_power = 0;
2141cc9203bfSDan Williams }
2142cc9203bfSDan Williams 
2143cc9203bfSDan Williams static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
2144cc9203bfSDan Williams {
2145e301370aSEdmund Nadolski 	struct sci_base_state_machine *sm = &scic->sm;
2146cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
21477c78da31SDan Williams 	enum sci_status result = SCI_FAILURE;
21487c78da31SDan Williams 	unsigned long i, state, val;
2149cc9203bfSDan Williams 
2150e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_RESET) {
2151cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
2152cc9203bfSDan Williams 			 "SCIC Controller initialize operation requested "
2153cc9203bfSDan Williams 			 "in invalid state\n");
2154cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2155cc9203bfSDan Williams 	}
2156cc9203bfSDan Williams 
2157e301370aSEdmund Nadolski 	sci_change_state(sm, SCIC_INITIALIZING);
2158cc9203bfSDan Williams 
2159bb3dbdf6SEdmund Nadolski 	sci_init_timer(&scic->phy_timer, phy_startup_timeout);
2160bb3dbdf6SEdmund Nadolski 
2161bb3dbdf6SEdmund Nadolski 	scic->next_phy_to_start = 0;
2162bb3dbdf6SEdmund Nadolski 	scic->phy_startup_timer_pending = false;
2163cc9203bfSDan Williams 
2164cc9203bfSDan Williams 	scic_sds_controller_initialize_power_control(scic);
2165cc9203bfSDan Williams 
2166cc9203bfSDan Williams 	/*
2167cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2168cc9203bfSDan Williams 	 * program the AFE registers.
2169cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2170cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
2171cc9203bfSDan Williams 	scic_sds_controller_afe_initialization(scic);
2172cc9203bfSDan Williams 
2173cc9203bfSDan Williams 
2174cc9203bfSDan Williams 	/* Take the hardware out of reset */
2175cc9203bfSDan Williams 	writel(0, &scic->smu_registers->soft_reset_control);
2176cc9203bfSDan Williams 
2177cc9203bfSDan Williams 	/*
2178cc9203bfSDan Williams 	 * / @todo Provide meaningfull error code for hardware failure
2179cc9203bfSDan Williams 	 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
21807c78da31SDan Williams 	for (i = 100; i >= 1; i--) {
21817c78da31SDan Williams 		u32 status;
2182cc9203bfSDan Williams 
2183cc9203bfSDan Williams 		/* Loop until the hardware reports success */
2184cc9203bfSDan Williams 		udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2185cc9203bfSDan Williams 		status = readl(&scic->smu_registers->control_status);
2186cc9203bfSDan Williams 
21877c78da31SDan Williams 		if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
21887c78da31SDan Williams 			break;
2189cc9203bfSDan Williams 	}
21907c78da31SDan Williams 	if (i == 0)
21917c78da31SDan Williams 		goto out;
2192cc9203bfSDan Williams 
2193cc9203bfSDan Williams 	/*
2194cc9203bfSDan Williams 	 * Determine what are the actaul device capacities that the
2195cc9203bfSDan Williams 	 * hardware will support */
21967c78da31SDan Williams 	val = readl(&scic->smu_registers->device_context_capacity);
2197cc9203bfSDan Williams 
21987c78da31SDan Williams 	/* Record the smaller of the two capacity values */
21997c78da31SDan Williams 	scic->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
22007c78da31SDan Williams 	scic->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
22017c78da31SDan Williams 	scic->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
2202cc9203bfSDan Williams 
2203cc9203bfSDan Williams 	/*
2204cc9203bfSDan Williams 	 * Make all PEs that are unassigned match up with the
2205cc9203bfSDan Williams 	 * logical ports
2206cc9203bfSDan Williams 	 */
22077c78da31SDan Williams 	for (i = 0; i < scic->logical_port_entries; i++) {
2208cc9203bfSDan Williams 		struct scu_port_task_scheduler_group_registers __iomem
2209cc9203bfSDan Williams 			*ptsg = &scic->scu_registers->peg0.ptsg;
2210cc9203bfSDan Williams 
22117c78da31SDan Williams 		writel(i, &ptsg->protocol_engine[i]);
2212cc9203bfSDan Williams 	}
2213cc9203bfSDan Williams 
2214cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
22157c78da31SDan Williams 	val = readl(&scic->scu_registers->sdma.pdma_configuration);
22167c78da31SDan Williams 	val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
22177c78da31SDan Williams 	writel(val, &scic->scu_registers->sdma.pdma_configuration);
2218cc9203bfSDan Williams 
22197c78da31SDan Williams 	val = readl(&scic->scu_registers->sdma.cdma_configuration);
22207c78da31SDan Williams 	val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
22217c78da31SDan Williams 	writel(val, &scic->scu_registers->sdma.cdma_configuration);
2222cc9203bfSDan Williams 
2223cc9203bfSDan Williams 	/*
2224cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2225cc9203bfSDan Williams 	 * are accessed during the port initialization.
2226cc9203bfSDan Williams 	 */
22277c78da31SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
2228*85280955SDan Williams 		result = scic_sds_phy_initialize(&ihost->phys[i],
22297c78da31SDan Williams 						 &scic->scu_registers->peg0.pe[i].tl,
22307c78da31SDan Williams 						 &scic->scu_registers->peg0.pe[i].ll);
22317c78da31SDan Williams 		if (result != SCI_SUCCESS)
22327c78da31SDan Williams 			goto out;
2233cc9203bfSDan Williams 	}
2234cc9203bfSDan Williams 
22357c78da31SDan Williams 	for (i = 0; i < scic->logical_port_entries; i++) {
22367c78da31SDan Williams 		result = scic_sds_port_initialize(&ihost->ports[i].sci,
22377c78da31SDan Williams 						  &scic->scu_registers->peg0.ptsg.port[i],
2238cc9203bfSDan Williams 						  &scic->scu_registers->peg0.ptsg.protocol_engine,
22397c78da31SDan Williams 						  &scic->scu_registers->peg0.viit[i]);
22407c78da31SDan Williams 
22417c78da31SDan Williams 		if (result != SCI_SUCCESS)
22427c78da31SDan Williams 			goto out;
2243cc9203bfSDan Williams 	}
2244cc9203bfSDan Williams 
22457c78da31SDan Williams 	result = scic_sds_port_configuration_agent_initialize(scic, &scic->port_agent);
2246cc9203bfSDan Williams 
22477c78da31SDan Williams  out:
2248cc9203bfSDan Williams 	/* Advance the controller state machine */
2249cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2250e301370aSEdmund Nadolski 		state = SCIC_INITIALIZED;
2251cc9203bfSDan Williams 	else
2252e301370aSEdmund Nadolski 		state = SCIC_FAILED;
2253e301370aSEdmund Nadolski 	sci_change_state(sm, state);
2254cc9203bfSDan Williams 
2255cc9203bfSDan Williams 	return result;
2256cc9203bfSDan Williams }
2257cc9203bfSDan Williams 
2258cc9203bfSDan Williams static enum sci_status scic_user_parameters_set(
2259cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2260cc9203bfSDan Williams 	union scic_user_parameters *scic_parms)
2261cc9203bfSDan Williams {
2262e301370aSEdmund Nadolski 	u32 state = scic->sm.current_state_id;
2263cc9203bfSDan Williams 
2264e301370aSEdmund Nadolski 	if (state == SCIC_RESET ||
2265e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZING ||
2266e301370aSEdmund Nadolski 	    state == SCIC_INITIALIZED) {
2267cc9203bfSDan Williams 		u16 index;
2268cc9203bfSDan Williams 
2269cc9203bfSDan Williams 		/*
2270cc9203bfSDan Williams 		 * Validate the user parameters.  If they are not legal, then
2271cc9203bfSDan Williams 		 * return a failure.
2272cc9203bfSDan Williams 		 */
2273cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
2274cc9203bfSDan Williams 			struct sci_phy_user_params *user_phy;
2275cc9203bfSDan Williams 
2276cc9203bfSDan Williams 			user_phy = &scic_parms->sds1.phys[index];
2277cc9203bfSDan Williams 
2278cc9203bfSDan Williams 			if (!((user_phy->max_speed_generation <=
2279cc9203bfSDan Williams 						SCIC_SDS_PARM_MAX_SPEED) &&
2280cc9203bfSDan Williams 			      (user_phy->max_speed_generation >
2281cc9203bfSDan Williams 						SCIC_SDS_PARM_NO_SPEED)))
2282cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2283cc9203bfSDan Williams 
2284cc9203bfSDan Williams 			if (user_phy->in_connection_align_insertion_frequency <
2285cc9203bfSDan Williams 					3)
2286cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2287cc9203bfSDan Williams 
2288cc9203bfSDan Williams 			if ((user_phy->in_connection_align_insertion_frequency <
2289cc9203bfSDan Williams 						3) ||
2290cc9203bfSDan Williams 			    (user_phy->align_insertion_frequency == 0) ||
2291cc9203bfSDan Williams 			    (user_phy->
2292cc9203bfSDan Williams 				notify_enable_spin_up_insertion_frequency ==
2293cc9203bfSDan Williams 						0))
2294cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2295cc9203bfSDan Williams 		}
2296cc9203bfSDan Williams 
2297cc9203bfSDan Williams 		if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
2298cc9203bfSDan Williams 		    (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
2299cc9203bfSDan Williams 		    (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
2300cc9203bfSDan Williams 		    (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
2301cc9203bfSDan Williams 		    (scic_parms->sds1.no_outbound_task_timeout == 0))
2302cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2303cc9203bfSDan Williams 
2304cc9203bfSDan Williams 		memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
2305cc9203bfSDan Williams 
2306cc9203bfSDan Williams 		return SCI_SUCCESS;
2307cc9203bfSDan Williams 	}
2308cc9203bfSDan Williams 
2309cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
2310cc9203bfSDan Williams }
2311cc9203bfSDan Williams 
2312cc9203bfSDan Williams static int scic_controller_mem_init(struct scic_sds_controller *scic)
2313cc9203bfSDan Williams {
2314cc9203bfSDan Williams 	struct device *dev = scic_to_dev(scic);
23157c78da31SDan Williams 	dma_addr_t dma;
23167c78da31SDan Williams 	size_t size;
23177c78da31SDan Williams 	int err;
2318cc9203bfSDan Williams 
23197c78da31SDan Williams 	size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
23207c78da31SDan Williams 	scic->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2321cc9203bfSDan Williams 	if (!scic->completion_queue)
2322cc9203bfSDan Williams 		return -ENOMEM;
2323cc9203bfSDan Williams 
23247c78da31SDan Williams 	writel(lower_32_bits(dma), &scic->smu_registers->completion_queue_lower);
23257c78da31SDan Williams 	writel(upper_32_bits(dma), &scic->smu_registers->completion_queue_upper);
2326cc9203bfSDan Williams 
23277c78da31SDan Williams 	size = scic->remote_node_entries * sizeof(union scu_remote_node_context);
23287c78da31SDan Williams 	scic->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
23297c78da31SDan Williams 							      GFP_KERNEL);
2330cc9203bfSDan Williams 	if (!scic->remote_node_context_table)
2331cc9203bfSDan Williams 		return -ENOMEM;
2332cc9203bfSDan Williams 
23337c78da31SDan Williams 	writel(lower_32_bits(dma), &scic->smu_registers->remote_node_context_lower);
23347c78da31SDan Williams 	writel(upper_32_bits(dma), &scic->smu_registers->remote_node_context_upper);
2335cc9203bfSDan Williams 
23367c78da31SDan Williams 	size = scic->task_context_entries * sizeof(struct scu_task_context),
23377c78da31SDan Williams 	scic->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2338cc9203bfSDan Williams 	if (!scic->task_context_table)
2339cc9203bfSDan Williams 		return -ENOMEM;
2340cc9203bfSDan Williams 
2341312e0c24SDan Williams 	scic->task_context_dma = dma;
23427c78da31SDan Williams 	writel(lower_32_bits(dma), &scic->smu_registers->host_task_table_lower);
23437c78da31SDan Williams 	writel(upper_32_bits(dma), &scic->smu_registers->host_task_table_upper);
2344cc9203bfSDan Williams 
23457c78da31SDan Williams 	err = scic_sds_unsolicited_frame_control_construct(scic);
23467c78da31SDan Williams 	if (err)
23477c78da31SDan Williams 		return err;
2348cc9203bfSDan Williams 
2349cc9203bfSDan Williams 	/*
2350cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2351cc9203bfSDan Williams 	 * address table.
2352cc9203bfSDan Williams 	 */
2353cc9203bfSDan Williams 	writel(lower_32_bits(scic->uf_control.headers.physical_address),
2354cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_header_base_address_lower);
2355cc9203bfSDan Williams 	writel(upper_32_bits(scic->uf_control.headers.physical_address),
2356cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_header_base_address_upper);
2357cc9203bfSDan Williams 
2358cc9203bfSDan Williams 	writel(lower_32_bits(scic->uf_control.address_table.physical_address),
2359cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_address_table_lower);
2360cc9203bfSDan Williams 	writel(upper_32_bits(scic->uf_control.address_table.physical_address),
2361cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_address_table_upper);
2362cc9203bfSDan Williams 
2363cc9203bfSDan Williams 	return 0;
2364cc9203bfSDan Williams }
2365cc9203bfSDan Williams 
23666f231ddaSDan Williams int isci_host_init(struct isci_host *isci_host)
23676f231ddaSDan Williams {
2368d9c37390SDan Williams 	int err = 0, i;
23696f231ddaSDan Williams 	enum sci_status status;
23704711ba10SDan Williams 	union scic_oem_parameters oem;
23716f231ddaSDan Williams 	union scic_user_parameters scic_user_params;
2372d044af17SDan Williams 	struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
23736f231ddaSDan Williams 
23746f231ddaSDan Williams 	spin_lock_init(&isci_host->state_lock);
23756f231ddaSDan Williams 	spin_lock_init(&isci_host->scic_lock);
23760cf89d1dSDan Williams 	init_waitqueue_head(&isci_host->eventq);
23776f231ddaSDan Williams 
23786f231ddaSDan Williams 	isci_host_change_state(isci_host, isci_starting);
23796f231ddaSDan Williams 
2380cc3dbd0aSArtur Wojcik 	status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
23816f231ddaSDan Williams 					   smu_base(isci_host));
23826f231ddaSDan Williams 
23836f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
23846f231ddaSDan Williams 		dev_err(&isci_host->pdev->dev,
23856f231ddaSDan Williams 			"%s: scic_controller_construct failed - status = %x\n",
23866f231ddaSDan Williams 			__func__,
23876f231ddaSDan Williams 			status);
2388858d4aa7SDave Jiang 		return -ENODEV;
23896f231ddaSDan Williams 	}
23906f231ddaSDan Williams 
23916f231ddaSDan Williams 	isci_host->sas_ha.dev = &isci_host->pdev->dev;
23926f231ddaSDan Williams 	isci_host->sas_ha.lldd_ha = isci_host;
23936f231ddaSDan Williams 
2394d044af17SDan Williams 	/*
2395d044af17SDan Williams 	 * grab initial values stored in the controller object for OEM and USER
2396d044af17SDan Williams 	 * parameters
2397d044af17SDan Williams 	 */
2398b5f18a20SDave Jiang 	isci_user_parameters_get(isci_host, &scic_user_params);
2399cc3dbd0aSArtur Wojcik 	status = scic_user_parameters_set(&isci_host->sci,
2400d044af17SDan Williams 					  &scic_user_params);
2401d044af17SDan Williams 	if (status != SCI_SUCCESS) {
2402d044af17SDan Williams 		dev_warn(&isci_host->pdev->dev,
2403d044af17SDan Williams 			 "%s: scic_user_parameters_set failed\n",
2404d044af17SDan Williams 			 __func__);
2405d044af17SDan Williams 		return -ENODEV;
2406d044af17SDan Williams 	}
24076f231ddaSDan Williams 
2408cc3dbd0aSArtur Wojcik 	scic_oem_parameters_get(&isci_host->sci, &oem);
2409d044af17SDan Williams 
2410d044af17SDan Williams 	/* grab any OEM parameters specified in orom */
2411d044af17SDan Williams 	if (pci_info->orom) {
24124711ba10SDan Williams 		status = isci_parse_oem_parameters(&oem,
2413d044af17SDan Williams 						   pci_info->orom,
2414d044af17SDan Williams 						   isci_host->id);
24156f231ddaSDan Williams 		if (status != SCI_SUCCESS) {
24166f231ddaSDan Williams 			dev_warn(&isci_host->pdev->dev,
24176f231ddaSDan Williams 				 "parsing firmware oem parameters failed\n");
2418858d4aa7SDave Jiang 			return -EINVAL;
24196f231ddaSDan Williams 		}
24204711ba10SDan Williams 	}
24214711ba10SDan Williams 
2422cc3dbd0aSArtur Wojcik 	status = scic_oem_parameters_set(&isci_host->sci, &oem);
24236f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
24246f231ddaSDan Williams 		dev_warn(&isci_host->pdev->dev,
24256f231ddaSDan Williams 				"%s: scic_oem_parameters_set failed\n",
24266f231ddaSDan Williams 				__func__);
2427858d4aa7SDave Jiang 		return -ENODEV;
24286f231ddaSDan Williams 	}
24296f231ddaSDan Williams 
24306f231ddaSDan Williams 	tasklet_init(&isci_host->completion_tasklet,
2431c7ef4031SDan Williams 		     isci_host_completion_routine, (unsigned long)isci_host);
24326f231ddaSDan Williams 
24336f231ddaSDan Williams 	INIT_LIST_HEAD(&isci_host->requests_to_complete);
243411b00c19SJeff Skirvin 	INIT_LIST_HEAD(&isci_host->requests_to_errorback);
24356f231ddaSDan Williams 
24367c40a803SDan Williams 	spin_lock_irq(&isci_host->scic_lock);
2437cc3dbd0aSArtur Wojcik 	status = scic_controller_initialize(&isci_host->sci);
24387c40a803SDan Williams 	spin_unlock_irq(&isci_host->scic_lock);
24397c40a803SDan Williams 	if (status != SCI_SUCCESS) {
24407c40a803SDan Williams 		dev_warn(&isci_host->pdev->dev,
24417c40a803SDan Williams 			 "%s: scic_controller_initialize failed -"
24427c40a803SDan Williams 			 " status = 0x%x\n",
24437c40a803SDan Williams 			 __func__, status);
24447c40a803SDan Williams 		return -ENODEV;
24457c40a803SDan Williams 	}
24467c40a803SDan Williams 
2447cc3dbd0aSArtur Wojcik 	err = scic_controller_mem_init(&isci_host->sci);
24486f231ddaSDan Williams 	if (err)
2449858d4aa7SDave Jiang 		return err;
24506f231ddaSDan Williams 
2451d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
2452e531381eSDan Williams 		isci_port_init(&isci_host->ports[i], isci_host, i);
24536f231ddaSDan Williams 
2454d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
2455d9c37390SDan Williams 		isci_phy_init(&isci_host->phys[i], isci_host, i);
2456d9c37390SDan Williams 
2457d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
245857f20f4eSDan Williams 		struct isci_remote_device *idev = &isci_host->devices[i];
2459d9c37390SDan Williams 
2460d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->reqs_in_process);
2461d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->node);
2462d9c37390SDan Williams 	}
24636f231ddaSDan Williams 
2464db056250SDan Williams 	for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2465db056250SDan Williams 		struct isci_request *ireq;
2466db056250SDan Williams 		dma_addr_t dma;
2467db056250SDan Williams 
2468db056250SDan Williams 		ireq = dmam_alloc_coherent(&isci_host->pdev->dev,
2469db056250SDan Williams 					   sizeof(struct isci_request), &dma,
2470db056250SDan Williams 					   GFP_KERNEL);
2471db056250SDan Williams 		if (!ireq)
2472db056250SDan Williams 			return -ENOMEM;
2473db056250SDan Williams 
24745076a1a9SDan Williams 		ireq->tc = &isci_host->sci.task_context_table[i];
24755076a1a9SDan Williams 		ireq->owning_controller = &isci_host->sci;
2476db056250SDan Williams 		spin_lock_init(&ireq->state_lock);
2477db056250SDan Williams 		ireq->request_daddr = dma;
2478db056250SDan Williams 		ireq->isci_host = isci_host;
2479db056250SDan Williams 
2480db056250SDan Williams 		isci_host->reqs[i] = ireq;
2481db056250SDan Williams 	}
2482db056250SDan Williams 
2483858d4aa7SDave Jiang 	return 0;
24846f231ddaSDan Williams }
2485cc9203bfSDan Williams 
2486cc9203bfSDan Williams void scic_sds_controller_link_up(struct scic_sds_controller *scic,
2487*85280955SDan Williams 		struct scic_sds_port *port, struct isci_phy *iphy)
2488cc9203bfSDan Williams {
2489e301370aSEdmund Nadolski 	switch (scic->sm.current_state_id) {
2490e301370aSEdmund Nadolski 	case SCIC_STARTING:
2491bb3dbdf6SEdmund Nadolski 		sci_del_timer(&scic->phy_timer);
2492bb3dbdf6SEdmund Nadolski 		scic->phy_startup_timer_pending = false;
2493cc9203bfSDan Williams 		scic->port_agent.link_up_handler(scic, &scic->port_agent,
2494*85280955SDan Williams 						 port, iphy);
2495cc9203bfSDan Williams 		scic_sds_controller_start_next_phy(scic);
2496cc9203bfSDan Williams 		break;
2497e301370aSEdmund Nadolski 	case SCIC_READY:
2498cc9203bfSDan Williams 		scic->port_agent.link_up_handler(scic, &scic->port_agent,
2499*85280955SDan Williams 						 port, iphy);
2500cc9203bfSDan Williams 		break;
2501cc9203bfSDan Williams 	default:
2502cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
2503cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
2504*85280955SDan Williams 			"unexpected state %d\n", __func__, iphy->phy_index,
2505e301370aSEdmund Nadolski 			scic->sm.current_state_id);
2506cc9203bfSDan Williams 	}
2507cc9203bfSDan Williams }
2508cc9203bfSDan Williams 
2509cc9203bfSDan Williams void scic_sds_controller_link_down(struct scic_sds_controller *scic,
2510*85280955SDan Williams 		struct scic_sds_port *port, struct isci_phy *iphy)
2511cc9203bfSDan Williams {
2512e301370aSEdmund Nadolski 	switch (scic->sm.current_state_id) {
2513e301370aSEdmund Nadolski 	case SCIC_STARTING:
2514e301370aSEdmund Nadolski 	case SCIC_READY:
2515cc9203bfSDan Williams 		scic->port_agent.link_down_handler(scic, &scic->port_agent,
2516*85280955SDan Williams 						   port, iphy);
2517cc9203bfSDan Williams 		break;
2518cc9203bfSDan Williams 	default:
2519cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
2520cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2521cc9203bfSDan Williams 			"unexpected state %d\n",
2522cc9203bfSDan Williams 			__func__,
2523*85280955SDan Williams 			iphy->phy_index,
2524e301370aSEdmund Nadolski 			scic->sm.current_state_id);
2525cc9203bfSDan Williams 	}
2526cc9203bfSDan Williams }
2527cc9203bfSDan Williams 
2528cc9203bfSDan Williams /**
2529cc9203bfSDan Williams  * This is a helper method to determine if any remote devices on this
2530cc9203bfSDan Williams  * controller are still in the stopping state.
2531cc9203bfSDan Williams  *
2532cc9203bfSDan Williams  */
2533cc9203bfSDan Williams static bool scic_sds_controller_has_remote_devices_stopping(
2534cc9203bfSDan Williams 	struct scic_sds_controller *controller)
2535cc9203bfSDan Williams {
2536cc9203bfSDan Williams 	u32 index;
2537cc9203bfSDan Williams 
2538cc9203bfSDan Williams 	for (index = 0; index < controller->remote_node_entries; index++) {
2539cc9203bfSDan Williams 		if ((controller->device_table[index] != NULL) &&
2540e301370aSEdmund Nadolski 		   (controller->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
2541cc9203bfSDan Williams 			return true;
2542cc9203bfSDan Williams 	}
2543cc9203bfSDan Williams 
2544cc9203bfSDan Williams 	return false;
2545cc9203bfSDan Williams }
2546cc9203bfSDan Williams 
2547cc9203bfSDan Williams /**
2548cc9203bfSDan Williams  * This method is called by the remote device to inform the controller
2549cc9203bfSDan Williams  * object that the remote device has stopped.
2550cc9203bfSDan Williams  */
2551cc9203bfSDan Williams void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
2552cc9203bfSDan Williams 					       struct scic_sds_remote_device *sci_dev)
2553cc9203bfSDan Williams {
2554e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_STOPPING) {
2555cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
2556cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2557cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2558cc9203bfSDan Williams 			scic, sci_dev,
2559e301370aSEdmund Nadolski 			scic->sm.current_state_id);
2560cc9203bfSDan Williams 		return;
2561cc9203bfSDan Williams 	}
2562cc9203bfSDan Williams 
2563cc9203bfSDan Williams 	if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
2564e301370aSEdmund Nadolski 		sci_change_state(&scic->sm, SCIC_STOPPED);
2565cc9203bfSDan Williams 	}
2566cc9203bfSDan Williams }
2567cc9203bfSDan Williams 
2568cc9203bfSDan Williams /**
2569cc9203bfSDan Williams  * This method will write to the SCU PCP register the request value. The method
2570cc9203bfSDan Williams  *    is used to suspend/resume ports, devices, and phys.
2571cc9203bfSDan Williams  * @scic:
2572cc9203bfSDan Williams  *
2573cc9203bfSDan Williams  *
2574cc9203bfSDan Williams  */
2575cc9203bfSDan Williams void scic_sds_controller_post_request(
2576cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2577cc9203bfSDan Williams 	u32 request)
2578cc9203bfSDan Williams {
2579cc9203bfSDan Williams 	dev_dbg(scic_to_dev(scic),
2580cc9203bfSDan Williams 		"%s: SCIC Controller 0x%p post request 0x%08x\n",
2581cc9203bfSDan Williams 		__func__,
2582cc9203bfSDan Williams 		scic,
2583cc9203bfSDan Williams 		request);
2584cc9203bfSDan Williams 
2585cc9203bfSDan Williams 	writel(request, &scic->smu_registers->post_context_port);
2586cc9203bfSDan Williams }
2587cc9203bfSDan Williams 
25885076a1a9SDan Williams struct isci_request *scic_request_by_tag(struct scic_sds_controller *scic, u16 io_tag)
2589cc9203bfSDan Williams {
2590cc9203bfSDan Williams 	u16 task_index;
2591cc9203bfSDan Williams 	u16 task_sequence;
2592cc9203bfSDan Williams 
2593dd047c8eSDan Williams 	task_index = ISCI_TAG_TCI(io_tag);
2594cc9203bfSDan Williams 
2595cc9203bfSDan Williams 	if (task_index < scic->task_context_entries) {
2596db056250SDan Williams 		struct isci_request *ireq = scic_to_ihost(scic)->reqs[task_index];
2597db056250SDan Williams 
2598db056250SDan Williams 		if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
2599dd047c8eSDan Williams 			task_sequence = ISCI_TAG_SEQ(io_tag);
2600cc9203bfSDan Williams 
2601db056250SDan Williams 			if (task_sequence == scic->io_request_sequence[task_index])
26025076a1a9SDan Williams 				return ireq;
2603cc9203bfSDan Williams 		}
2604cc9203bfSDan Williams 	}
2605cc9203bfSDan Williams 
2606cc9203bfSDan Williams 	return NULL;
2607cc9203bfSDan Williams }
2608cc9203bfSDan Williams 
2609cc9203bfSDan Williams /**
2610cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2611cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2612cc9203bfSDan Williams  *    node index available.
2613cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2614cc9203bfSDan Williams  *    free remote node ids
2615cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2616cc9203bfSDan Williams  *    id
2617cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2618cc9203bfSDan Williams  *    is available
2619cc9203bfSDan Williams  *
2620cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2621cc9203bfSDan Williams  * node index available.
2622cc9203bfSDan Williams  */
2623cc9203bfSDan Williams enum sci_status scic_sds_controller_allocate_remote_node_context(
2624cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2625cc9203bfSDan Williams 	struct scic_sds_remote_device *sci_dev,
2626cc9203bfSDan Williams 	u16 *node_id)
2627cc9203bfSDan Williams {
2628cc9203bfSDan Williams 	u16 node_index;
2629cc9203bfSDan Williams 	u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2630cc9203bfSDan Williams 
2631cc9203bfSDan Williams 	node_index = scic_sds_remote_node_table_allocate_remote_node(
2632cc9203bfSDan Williams 		&scic->available_remote_nodes, remote_node_count
2633cc9203bfSDan Williams 		);
2634cc9203bfSDan Williams 
2635cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2636cc9203bfSDan Williams 		scic->device_table[node_index] = sci_dev;
2637cc9203bfSDan Williams 
2638cc9203bfSDan Williams 		*node_id = node_index;
2639cc9203bfSDan Williams 
2640cc9203bfSDan Williams 		return SCI_SUCCESS;
2641cc9203bfSDan Williams 	}
2642cc9203bfSDan Williams 
2643cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2644cc9203bfSDan Williams }
2645cc9203bfSDan Williams 
2646cc9203bfSDan Williams /**
2647cc9203bfSDan Williams  * This method frees the remote node index back to the available pool.  Once
2648cc9203bfSDan Williams  *    this is done the remote node context buffer is no longer valid and can
2649cc9203bfSDan Williams  *    not be used.
2650cc9203bfSDan Williams  * @scic:
2651cc9203bfSDan Williams  * @sci_dev:
2652cc9203bfSDan Williams  * @node_id:
2653cc9203bfSDan Williams  *
2654cc9203bfSDan Williams  */
2655cc9203bfSDan Williams void scic_sds_controller_free_remote_node_context(
2656cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2657cc9203bfSDan Williams 	struct scic_sds_remote_device *sci_dev,
2658cc9203bfSDan Williams 	u16 node_id)
2659cc9203bfSDan Williams {
2660cc9203bfSDan Williams 	u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2661cc9203bfSDan Williams 
2662cc9203bfSDan Williams 	if (scic->device_table[node_id] == sci_dev) {
2663cc9203bfSDan Williams 		scic->device_table[node_id] = NULL;
2664cc9203bfSDan Williams 
2665cc9203bfSDan Williams 		scic_sds_remote_node_table_release_remote_node_index(
2666cc9203bfSDan Williams 			&scic->available_remote_nodes, remote_node_count, node_id
2667cc9203bfSDan Williams 			);
2668cc9203bfSDan Williams 	}
2669cc9203bfSDan Williams }
2670cc9203bfSDan Williams 
2671cc9203bfSDan Williams /**
2672cc9203bfSDan Williams  * This method returns the union scu_remote_node_context for the specified remote
2673cc9203bfSDan Williams  *    node id.
2674cc9203bfSDan Williams  * @scic:
2675cc9203bfSDan Williams  * @node_id:
2676cc9203bfSDan Williams  *
2677cc9203bfSDan Williams  * union scu_remote_node_context*
2678cc9203bfSDan Williams  */
2679cc9203bfSDan Williams union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
2680cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2681cc9203bfSDan Williams 	u16 node_id
2682cc9203bfSDan Williams 	) {
2683cc9203bfSDan Williams 	if (
2684cc9203bfSDan Williams 		(node_id < scic->remote_node_entries)
2685cc9203bfSDan Williams 		&& (scic->device_table[node_id] != NULL)
2686cc9203bfSDan Williams 		) {
2687cc9203bfSDan Williams 		return &scic->remote_node_context_table[node_id];
2688cc9203bfSDan Williams 	}
2689cc9203bfSDan Williams 
2690cc9203bfSDan Williams 	return NULL;
2691cc9203bfSDan Williams }
2692cc9203bfSDan Williams 
2693cc9203bfSDan Williams /**
2694cc9203bfSDan Williams  *
2695cc9203bfSDan Williams  * @resposne_buffer: This is the buffer into which the D2H register FIS will be
2696cc9203bfSDan Williams  *    constructed.
2697cc9203bfSDan Williams  * @frame_header: This is the frame header returned by the hardware.
2698cc9203bfSDan Williams  * @frame_buffer: This is the frame buffer returned by the hardware.
2699cc9203bfSDan Williams  *
2700cc9203bfSDan Williams  * This method will combind the frame header and frame buffer to create a SATA
2701cc9203bfSDan Williams  * D2H register FIS none
2702cc9203bfSDan Williams  */
2703cc9203bfSDan Williams void scic_sds_controller_copy_sata_response(
2704cc9203bfSDan Williams 	void *response_buffer,
2705cc9203bfSDan Williams 	void *frame_header,
2706cc9203bfSDan Williams 	void *frame_buffer)
2707cc9203bfSDan Williams {
2708cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2709cc9203bfSDan Williams 
2710cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2711cc9203bfSDan Williams 	       frame_buffer,
2712cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2713cc9203bfSDan Williams }
2714cc9203bfSDan Williams 
2715cc9203bfSDan Williams /**
2716cc9203bfSDan Williams  * This method releases the frame once this is done the frame is available for
2717cc9203bfSDan Williams  *    re-use by the hardware.  The data contained in the frame header and frame
2718cc9203bfSDan Williams  *    buffer is no longer valid. The UF queue get pointer is only updated if UF
2719cc9203bfSDan Williams  *    control indicates this is appropriate.
2720cc9203bfSDan Williams  * @scic:
2721cc9203bfSDan Williams  * @frame_index:
2722cc9203bfSDan Williams  *
2723cc9203bfSDan Williams  */
2724cc9203bfSDan Williams void scic_sds_controller_release_frame(
2725cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2726cc9203bfSDan Williams 	u32 frame_index)
2727cc9203bfSDan Williams {
2728cc9203bfSDan Williams 	if (scic_sds_unsolicited_frame_control_release_frame(
2729cc9203bfSDan Williams 		    &scic->uf_control, frame_index) == true)
2730cc9203bfSDan Williams 		writel(scic->uf_control.get,
2731cc9203bfSDan Williams 			&scic->scu_registers->sdma.unsolicited_frame_get_pointer);
2732cc9203bfSDan Williams }
2733cc9203bfSDan Williams 
2734312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci)
2735312e0c24SDan Williams {
2736312e0c24SDan Williams 	u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2737312e0c24SDan Williams 
2738312e0c24SDan Williams 	ihost->tci_pool[tail] = tci;
2739312e0c24SDan Williams 	ihost->tci_tail = tail + 1;
2740312e0c24SDan Williams }
2741312e0c24SDan Williams 
2742312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost)
2743312e0c24SDan Williams {
2744312e0c24SDan Williams 	u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2745312e0c24SDan Williams 	u16 tci = ihost->tci_pool[head];
2746312e0c24SDan Williams 
2747312e0c24SDan Williams 	ihost->tci_head = head + 1;
2748312e0c24SDan Williams 	return tci;
2749312e0c24SDan Williams }
2750312e0c24SDan Williams 
2751312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost)
2752312e0c24SDan Williams {
2753312e0c24SDan Williams 	return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2754312e0c24SDan Williams }
2755312e0c24SDan Williams 
2756312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost)
2757312e0c24SDan Williams {
2758312e0c24SDan Williams 	if (isci_tci_space(ihost)) {
2759312e0c24SDan Williams 		u16 tci = isci_tci_alloc(ihost);
2760312e0c24SDan Williams 		u8 seq = ihost->sci.io_request_sequence[tci];
2761312e0c24SDan Williams 
2762312e0c24SDan Williams 		return ISCI_TAG(seq, tci);
2763312e0c24SDan Williams 	}
2764312e0c24SDan Williams 
2765312e0c24SDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
2766312e0c24SDan Williams }
2767312e0c24SDan Williams 
2768312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2769312e0c24SDan Williams {
2770312e0c24SDan Williams 	struct scic_sds_controller *scic = &ihost->sci;
2771312e0c24SDan Williams 	u16 tci = ISCI_TAG_TCI(io_tag);
2772312e0c24SDan Williams 	u16 seq = ISCI_TAG_SEQ(io_tag);
2773312e0c24SDan Williams 
2774312e0c24SDan Williams 	/* prevent tail from passing head */
2775312e0c24SDan Williams 	if (isci_tci_active(ihost) == 0)
2776312e0c24SDan Williams 		return SCI_FAILURE_INVALID_IO_TAG;
2777312e0c24SDan Williams 
2778312e0c24SDan Williams 	if (seq == scic->io_request_sequence[tci]) {
2779312e0c24SDan Williams 		scic->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
2780312e0c24SDan Williams 
2781312e0c24SDan Williams 		isci_tci_free(ihost, tci);
2782312e0c24SDan Williams 
2783312e0c24SDan Williams 		return SCI_SUCCESS;
2784312e0c24SDan Williams 	}
2785312e0c24SDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
2786312e0c24SDan Williams }
2787312e0c24SDan Williams 
2788cc9203bfSDan Williams /**
2789cc9203bfSDan Williams  * scic_controller_start_io() - This method is called by the SCI user to
2790cc9203bfSDan Williams  *    send/start an IO request. If the method invocation is successful, then
2791cc9203bfSDan Williams  *    the IO request has been queued to the hardware for processing.
2792cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start an IO
2793cc9203bfSDan Williams  *    request.
2794cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start an
2795cc9203bfSDan Williams  *    IO request.
2796cc9203bfSDan Williams  * @io_request: the handle to the io request object to start.
2797cc9203bfSDan Williams  * @io_tag: This parameter specifies a previously allocated IO tag that the
2798312e0c24SDan Williams  *    user desires to be utilized for this request.
2799cc9203bfSDan Williams  */
2800dd047c8eSDan Williams enum sci_status scic_controller_start_io(struct scic_sds_controller *scic,
2801cc9203bfSDan Williams 					 struct scic_sds_remote_device *rdev,
28025076a1a9SDan Williams 					 struct isci_request *ireq)
2803cc9203bfSDan Williams {
2804cc9203bfSDan Williams 	enum sci_status status;
2805cc9203bfSDan Williams 
2806e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_READY) {
2807cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic), "invalid state to start I/O");
2808cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2809cc9203bfSDan Williams 	}
2810cc9203bfSDan Williams 
28115076a1a9SDan Williams 	status = scic_sds_remote_device_start_io(scic, rdev, ireq);
2812cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2813cc9203bfSDan Williams 		return status;
2814cc9203bfSDan Williams 
28155076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
28165076a1a9SDan Williams 	scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(ireq));
2817cc9203bfSDan Williams 	return SCI_SUCCESS;
2818cc9203bfSDan Williams }
2819cc9203bfSDan Williams 
2820cc9203bfSDan Williams /**
2821cc9203bfSDan Williams  * scic_controller_terminate_request() - This method is called by the SCI Core
2822cc9203bfSDan Williams  *    user to terminate an ongoing (i.e. started) core IO request.  This does
2823cc9203bfSDan Williams  *    not abort the IO request at the target, but rather removes the IO request
2824cc9203bfSDan Williams  *    from the host controller.
2825cc9203bfSDan Williams  * @controller: the handle to the controller object for which to terminate a
2826cc9203bfSDan Williams  *    request.
2827cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to
2828cc9203bfSDan Williams  *    terminate a request.
2829cc9203bfSDan Williams  * @request: the handle to the io or task management request object to
2830cc9203bfSDan Williams  *    terminate.
2831cc9203bfSDan Williams  *
2832cc9203bfSDan Williams  * Indicate if the controller successfully began the terminate process for the
2833cc9203bfSDan Williams  * IO request. SCI_SUCCESS if the terminate process was successfully started
2834cc9203bfSDan Williams  * for the request. Determine the failure situations and return values.
2835cc9203bfSDan Williams  */
2836cc9203bfSDan Williams enum sci_status scic_controller_terminate_request(
2837cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2838cc9203bfSDan Williams 	struct scic_sds_remote_device *rdev,
28395076a1a9SDan Williams 	struct isci_request *ireq)
2840cc9203bfSDan Williams {
2841cc9203bfSDan Williams 	enum sci_status status;
2842cc9203bfSDan Williams 
2843e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_READY) {
2844cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
2845cc9203bfSDan Williams 			 "invalid state to terminate request\n");
2846cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2847cc9203bfSDan Williams 	}
2848cc9203bfSDan Williams 
28495076a1a9SDan Williams 	status = scic_sds_io_request_terminate(ireq);
2850cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2851cc9203bfSDan Williams 		return status;
2852cc9203bfSDan Williams 
2853cc9203bfSDan Williams 	/*
2854cc9203bfSDan Williams 	 * Utilize the original post context command and or in the POST_TC_ABORT
2855cc9203bfSDan Williams 	 * request sub-type.
2856cc9203bfSDan Williams 	 */
2857cc9203bfSDan Williams 	scic_sds_controller_post_request(scic,
28585076a1a9SDan Williams 		scic_sds_request_get_post_context(ireq) |
2859cc9203bfSDan Williams 		SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2860cc9203bfSDan Williams 	return SCI_SUCCESS;
2861cc9203bfSDan Williams }
2862cc9203bfSDan Williams 
2863cc9203bfSDan Williams /**
2864cc9203bfSDan Williams  * scic_controller_complete_io() - This method will perform core specific
2865cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
2866cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
2867cc9203bfSDan Williams  *    reused (i.e. re-constructed).
2868cc9203bfSDan Williams  * @controller: The handle to the controller object for which to complete the
2869cc9203bfSDan Williams  *    IO request.
2870cc9203bfSDan Williams  * @remote_device: The handle to the remote device object for which to complete
2871cc9203bfSDan Williams  *    the IO request.
2872cc9203bfSDan Williams  * @io_request: the handle to the io request object to complete.
2873cc9203bfSDan Williams  */
2874cc9203bfSDan Williams enum sci_status scic_controller_complete_io(
2875cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2876cc9203bfSDan Williams 	struct scic_sds_remote_device *rdev,
28775076a1a9SDan Williams 	struct isci_request *ireq)
2878cc9203bfSDan Williams {
2879cc9203bfSDan Williams 	enum sci_status status;
2880cc9203bfSDan Williams 	u16 index;
2881cc9203bfSDan Williams 
2882e301370aSEdmund Nadolski 	switch (scic->sm.current_state_id) {
2883e301370aSEdmund Nadolski 	case SCIC_STOPPING:
2884cc9203bfSDan Williams 		/* XXX: Implement this function */
2885cc9203bfSDan Williams 		return SCI_FAILURE;
2886e301370aSEdmund Nadolski 	case SCIC_READY:
28875076a1a9SDan Williams 		status = scic_sds_remote_device_complete_io(scic, rdev, ireq);
2888cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
2889cc9203bfSDan Williams 			return status;
2890cc9203bfSDan Williams 
28915076a1a9SDan Williams 		index = ISCI_TAG_TCI(ireq->io_tag);
28925076a1a9SDan Williams 		clear_bit(IREQ_ACTIVE, &ireq->flags);
2893cc9203bfSDan Williams 		return SCI_SUCCESS;
2894cc9203bfSDan Williams 	default:
2895cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
2896cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2897cc9203bfSDan Williams 	}
2898cc9203bfSDan Williams 
2899cc9203bfSDan Williams }
2900cc9203bfSDan Williams 
29015076a1a9SDan Williams enum sci_status scic_controller_continue_io(struct isci_request *ireq)
2902cc9203bfSDan Williams {
29035076a1a9SDan Williams 	struct scic_sds_controller *scic = ireq->owning_controller;
2904cc9203bfSDan Williams 
2905e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_READY) {
2906cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
2907cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2908cc9203bfSDan Williams 	}
2909cc9203bfSDan Williams 
29105076a1a9SDan Williams 	set_bit(IREQ_ACTIVE, &ireq->flags);
29115076a1a9SDan Williams 	scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(ireq));
2912cc9203bfSDan Williams 	return SCI_SUCCESS;
2913cc9203bfSDan Williams }
2914cc9203bfSDan Williams 
2915cc9203bfSDan Williams /**
2916cc9203bfSDan Williams  * scic_controller_start_task() - This method is called by the SCIC user to
2917cc9203bfSDan Williams  *    send/start a framework task management request.
2918cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
2919cc9203bfSDan Williams  *    management request.
2920cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
2921cc9203bfSDan Williams  *    the task management request.
2922cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
2923cc9203bfSDan Williams  */
2924cc9203bfSDan Williams enum sci_task_status scic_controller_start_task(
2925cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2926cc9203bfSDan Williams 	struct scic_sds_remote_device *rdev,
29275076a1a9SDan Williams 	struct isci_request *ireq)
2928cc9203bfSDan Williams {
2929cc9203bfSDan Williams 	enum sci_status status;
2930cc9203bfSDan Williams 
2931e301370aSEdmund Nadolski 	if (scic->sm.current_state_id != SCIC_READY) {
2932cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
2933cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
2934cc9203bfSDan Williams 			 "state\n",
2935cc9203bfSDan Williams 			 __func__);
2936cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
2937cc9203bfSDan Williams 	}
2938cc9203bfSDan Williams 
29395076a1a9SDan Williams 	status = scic_sds_remote_device_start_task(scic, rdev, ireq);
2940cc9203bfSDan Williams 	switch (status) {
2941cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
2942db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2943cc9203bfSDan Williams 
2944cc9203bfSDan Williams 		/*
2945cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
2946cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
2947cc9203bfSDan Williams 		 * RNC is resumed.)
2948cc9203bfSDan Williams 		 */
2949cc9203bfSDan Williams 		return SCI_SUCCESS;
2950cc9203bfSDan Williams 	case SCI_SUCCESS:
2951db056250SDan Williams 		set_bit(IREQ_ACTIVE, &ireq->flags);
2952cc9203bfSDan Williams 
2953cc9203bfSDan Williams 		scic_sds_controller_post_request(scic,
29545076a1a9SDan Williams 			scic_sds_request_get_post_context(ireq));
2955cc9203bfSDan Williams 		break;
2956cc9203bfSDan Williams 	default:
2957cc9203bfSDan Williams 		break;
2958cc9203bfSDan Williams 	}
2959cc9203bfSDan Williams 
2960cc9203bfSDan Williams 	return status;
2961cc9203bfSDan Williams }
2962