xref: /openbmc/linux/drivers/scsi/isci/host.c (revision cc9203bf381a465cd115762b9cf7c9a313c874bc)
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  */
55*cc9203bfSDan Williams #include <linux/device.h>
56*cc9203bfSDan Williams #include <scsi/sas.h>
57*cc9203bfSDan Williams #include "host.h"
586f231ddaSDan Williams #include "isci.h"
596f231ddaSDan Williams #include "port.h"
606f231ddaSDan Williams #include "host.h"
61d044af17SDan Williams #include "probe_roms.h"
62*cc9203bfSDan Williams #include "remote_device.h"
63*cc9203bfSDan Williams #include "request.h"
64*cc9203bfSDan Williams #include "scic_io_request.h"
65*cc9203bfSDan Williams #include "scic_sds_port_configuration_agent.h"
66*cc9203bfSDan Williams #include "sci_util.h"
67*cc9203bfSDan Williams #include "scu_completion_codes.h"
68*cc9203bfSDan Williams #include "scu_event_codes.h"
69*cc9203bfSDan Williams #include "scu_registers.h"
70*cc9203bfSDan Williams #include "scu_remote_node_context.h"
71*cc9203bfSDan Williams #include "scu_task_context.h"
72*cc9203bfSDan Williams #include "scu_unsolicited_frame.h"
73ce2b3261SDan Williams #include "timers.h"
746f231ddaSDan Williams 
75*cc9203bfSDan Williams #define SCU_CONTEXT_RAM_INIT_STALL_TIME      200
76*cc9203bfSDan Williams 
77*cc9203bfSDan Williams /**
78*cc9203bfSDan Williams  * smu_dcc_get_max_ports() -
79*cc9203bfSDan Williams  *
80*cc9203bfSDan Williams  * This macro returns the maximum number of logical ports supported by the
81*cc9203bfSDan Williams  * hardware. The caller passes in the value read from the device context
82*cc9203bfSDan Williams  * capacity register and this macro will mash and shift the value appropriately.
83*cc9203bfSDan Williams  */
84*cc9203bfSDan Williams #define smu_dcc_get_max_ports(dcc_value) \
85*cc9203bfSDan Williams 	(\
86*cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
87*cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
88*cc9203bfSDan Williams 	)
89*cc9203bfSDan Williams 
90*cc9203bfSDan Williams /**
91*cc9203bfSDan Williams  * smu_dcc_get_max_task_context() -
92*cc9203bfSDan Williams  *
93*cc9203bfSDan Williams  * This macro returns the maximum number of task contexts supported by the
94*cc9203bfSDan Williams  * hardware. The caller passes in the value read from the device context
95*cc9203bfSDan Williams  * capacity register and this macro will mash and shift the value appropriately.
96*cc9203bfSDan Williams  */
97*cc9203bfSDan Williams #define smu_dcc_get_max_task_context(dcc_value)	\
98*cc9203bfSDan Williams 	(\
99*cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
100*cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
101*cc9203bfSDan Williams 	)
102*cc9203bfSDan Williams 
103*cc9203bfSDan Williams /**
104*cc9203bfSDan Williams  * smu_dcc_get_max_remote_node_context() -
105*cc9203bfSDan Williams  *
106*cc9203bfSDan Williams  * This macro returns the maximum number of remote node contexts supported by
107*cc9203bfSDan Williams  * the hardware. The caller passes in the value read from the device context
108*cc9203bfSDan Williams  * capacity register and this macro will mash and shift the value appropriately.
109*cc9203bfSDan Williams  */
110*cc9203bfSDan Williams #define smu_dcc_get_max_remote_node_context(dcc_value) \
111*cc9203bfSDan Williams 	(\
112*cc9203bfSDan Williams 		(((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
113*cc9203bfSDan Williams 		 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
114*cc9203bfSDan Williams 	)
115*cc9203bfSDan Williams 
116*cc9203bfSDan Williams 
117*cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_MIN_TIMER_COUNT  3
118*cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_MAX_TIMER_COUNT  3
119*cc9203bfSDan Williams 
120*cc9203bfSDan Williams /**
121*cc9203bfSDan Williams  *
122*cc9203bfSDan Williams  *
123*cc9203bfSDan Williams  * The number of milliseconds to wait for a phy to start.
124*cc9203bfSDan Williams  */
125*cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT      100
126*cc9203bfSDan Williams 
127*cc9203bfSDan Williams /**
128*cc9203bfSDan Williams  *
129*cc9203bfSDan Williams  *
130*cc9203bfSDan Williams  * The number of milliseconds to wait while a given phy is consuming power
131*cc9203bfSDan Williams  * before allowing another set of phys to consume power. Ultimately, this will
132*cc9203bfSDan Williams  * be specified by OEM parameter.
133*cc9203bfSDan Williams  */
134*cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
135*cc9203bfSDan Williams 
136*cc9203bfSDan Williams /**
137*cc9203bfSDan Williams  * NORMALIZE_PUT_POINTER() -
138*cc9203bfSDan Williams  *
139*cc9203bfSDan Williams  * This macro will normalize the completion queue put pointer so its value can
140*cc9203bfSDan Williams  * be used as an array inde
141*cc9203bfSDan Williams  */
142*cc9203bfSDan Williams #define NORMALIZE_PUT_POINTER(x) \
143*cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
144*cc9203bfSDan Williams 
145*cc9203bfSDan Williams 
146*cc9203bfSDan Williams /**
147*cc9203bfSDan Williams  * NORMALIZE_EVENT_POINTER() -
148*cc9203bfSDan Williams  *
149*cc9203bfSDan Williams  * This macro will normalize the completion queue event entry so its value can
150*cc9203bfSDan Williams  * be used as an index.
151*cc9203bfSDan Williams  */
152*cc9203bfSDan Williams #define NORMALIZE_EVENT_POINTER(x) \
153*cc9203bfSDan Williams 	(\
154*cc9203bfSDan Williams 		((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
155*cc9203bfSDan Williams 		>> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT	\
156*cc9203bfSDan Williams 	)
157*cc9203bfSDan Williams 
158*cc9203bfSDan Williams /**
159*cc9203bfSDan Williams  * INCREMENT_COMPLETION_QUEUE_GET() -
160*cc9203bfSDan Williams  *
161*cc9203bfSDan Williams  * This macro will increment the controllers completion queue index value and
162*cc9203bfSDan Williams  * possibly toggle the cycle bit if the completion queue index wraps back to 0.
163*cc9203bfSDan Williams  */
164*cc9203bfSDan Williams #define INCREMENT_COMPLETION_QUEUE_GET(controller, index, cycle) \
165*cc9203bfSDan Williams 	INCREMENT_QUEUE_GET(\
166*cc9203bfSDan Williams 		(index), \
167*cc9203bfSDan Williams 		(cycle), \
168*cc9203bfSDan Williams 		(controller)->completion_queue_entries,	\
169*cc9203bfSDan Williams 		SMU_CQGR_CYCLE_BIT \
170*cc9203bfSDan Williams 		)
171*cc9203bfSDan Williams 
172*cc9203bfSDan Williams /**
173*cc9203bfSDan Williams  * INCREMENT_EVENT_QUEUE_GET() -
174*cc9203bfSDan Williams  *
175*cc9203bfSDan Williams  * This macro will increment the controllers event queue index value and
176*cc9203bfSDan Williams  * possibly toggle the event cycle bit if the event queue index wraps back to 0.
177*cc9203bfSDan Williams  */
178*cc9203bfSDan Williams #define INCREMENT_EVENT_QUEUE_GET(controller, index, cycle) \
179*cc9203bfSDan Williams 	INCREMENT_QUEUE_GET(\
180*cc9203bfSDan Williams 		(index), \
181*cc9203bfSDan Williams 		(cycle), \
182*cc9203bfSDan Williams 		(controller)->completion_event_entries,	\
183*cc9203bfSDan Williams 		SMU_CQGR_EVENT_CYCLE_BIT \
184*cc9203bfSDan Williams 		)
185*cc9203bfSDan Williams 
186*cc9203bfSDan Williams 
187*cc9203bfSDan Williams /**
188*cc9203bfSDan Williams  * NORMALIZE_GET_POINTER() -
189*cc9203bfSDan Williams  *
190*cc9203bfSDan Williams  * This macro will normalize the completion queue get pointer so its value can
191*cc9203bfSDan Williams  * be used as an index into an array
192*cc9203bfSDan Williams  */
193*cc9203bfSDan Williams #define NORMALIZE_GET_POINTER(x) \
194*cc9203bfSDan Williams 	((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
195*cc9203bfSDan Williams 
196*cc9203bfSDan Williams /**
197*cc9203bfSDan Williams  * NORMALIZE_GET_POINTER_CYCLE_BIT() -
198*cc9203bfSDan Williams  *
199*cc9203bfSDan Williams  * This macro will normalize the completion queue cycle pointer so it matches
200*cc9203bfSDan Williams  * the completion queue cycle bit
201*cc9203bfSDan Williams  */
202*cc9203bfSDan Williams #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
203*cc9203bfSDan Williams 	((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
204*cc9203bfSDan Williams 
205*cc9203bfSDan Williams /**
206*cc9203bfSDan Williams  * COMPLETION_QUEUE_CYCLE_BIT() -
207*cc9203bfSDan Williams  *
208*cc9203bfSDan Williams  * This macro will return the cycle bit of the completion queue entry
209*cc9203bfSDan Williams  */
210*cc9203bfSDan Williams #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
211*cc9203bfSDan Williams 
212*cc9203bfSDan Williams static bool scic_sds_controller_completion_queue_has_entries(
213*cc9203bfSDan Williams 	struct scic_sds_controller *scic)
214*cc9203bfSDan Williams {
215*cc9203bfSDan Williams 	u32 get_value = scic->completion_queue_get;
216*cc9203bfSDan Williams 	u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
217*cc9203bfSDan Williams 
218*cc9203bfSDan Williams 	if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
219*cc9203bfSDan Williams 	    COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
220*cc9203bfSDan Williams 		return true;
221*cc9203bfSDan Williams 
222*cc9203bfSDan Williams 	return false;
223*cc9203bfSDan Williams }
224*cc9203bfSDan Williams 
225*cc9203bfSDan Williams static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
226*cc9203bfSDan Williams {
227*cc9203bfSDan Williams 	if (scic_sds_controller_completion_queue_has_entries(scic)) {
228*cc9203bfSDan Williams 		return true;
229*cc9203bfSDan Williams 	} else {
230*cc9203bfSDan Williams 		/*
231*cc9203bfSDan Williams 		 * we have a spurious interrupt it could be that we have already
232*cc9203bfSDan Williams 		 * emptied the completion queue from a previous interrupt */
233*cc9203bfSDan Williams 		writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
234*cc9203bfSDan Williams 
235*cc9203bfSDan Williams 		/*
236*cc9203bfSDan Williams 		 * There is a race in the hardware that could cause us not to be notified
237*cc9203bfSDan Williams 		 * of an interrupt completion if we do not take this step.  We will mask
238*cc9203bfSDan Williams 		 * then unmask the interrupts so if there is another interrupt pending
239*cc9203bfSDan Williams 		 * the clearing of the interrupt source we get the next interrupt message. */
240*cc9203bfSDan Williams 		writel(0xFF000000, &scic->smu_registers->interrupt_mask);
241*cc9203bfSDan Williams 		writel(0, &scic->smu_registers->interrupt_mask);
242*cc9203bfSDan Williams 	}
243*cc9203bfSDan Williams 
244*cc9203bfSDan Williams 	return false;
245*cc9203bfSDan Williams }
246*cc9203bfSDan Williams 
247c7ef4031SDan Williams irqreturn_t isci_msix_isr(int vec, void *data)
2486f231ddaSDan Williams {
249c7ef4031SDan Williams 	struct isci_host *ihost = data;
2506f231ddaSDan Williams 
251cc3dbd0aSArtur Wojcik 	if (scic_sds_controller_isr(&ihost->sci))
252c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
2536f231ddaSDan Williams 
254c7ef4031SDan Williams 	return IRQ_HANDLED;
255c7ef4031SDan Williams }
256c7ef4031SDan Williams 
257*cc9203bfSDan Williams static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
258*cc9203bfSDan Williams {
259*cc9203bfSDan Williams 	u32 interrupt_status;
260*cc9203bfSDan Williams 
261*cc9203bfSDan Williams 	interrupt_status =
262*cc9203bfSDan Williams 		readl(&scic->smu_registers->interrupt_status);
263*cc9203bfSDan Williams 	interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
264*cc9203bfSDan Williams 
265*cc9203bfSDan Williams 	if (interrupt_status != 0) {
266*cc9203bfSDan Williams 		/*
267*cc9203bfSDan Williams 		 * There is an error interrupt pending so let it through and handle
268*cc9203bfSDan Williams 		 * in the callback */
269*cc9203bfSDan Williams 		return true;
270*cc9203bfSDan Williams 	}
271*cc9203bfSDan Williams 
272*cc9203bfSDan Williams 	/*
273*cc9203bfSDan Williams 	 * There is a race in the hardware that could cause us not to be notified
274*cc9203bfSDan Williams 	 * of an interrupt completion if we do not take this step.  We will mask
275*cc9203bfSDan Williams 	 * then unmask the error interrupts so if there was another interrupt
276*cc9203bfSDan Williams 	 * pending we will be notified.
277*cc9203bfSDan Williams 	 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
278*cc9203bfSDan Williams 	writel(0xff, &scic->smu_registers->interrupt_mask);
279*cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
280*cc9203bfSDan Williams 
281*cc9203bfSDan Williams 	return false;
282*cc9203bfSDan Williams }
283*cc9203bfSDan Williams 
284*cc9203bfSDan Williams static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
285*cc9203bfSDan Williams 						u32 completion_entry)
286*cc9203bfSDan Williams {
287*cc9203bfSDan Williams 	u32 index;
288*cc9203bfSDan Williams 	struct scic_sds_request *io_request;
289*cc9203bfSDan Williams 
290*cc9203bfSDan Williams 	index = SCU_GET_COMPLETION_INDEX(completion_entry);
291*cc9203bfSDan Williams 	io_request = scic->io_request_table[index];
292*cc9203bfSDan Williams 
293*cc9203bfSDan Williams 	/* Make sure that we really want to process this IO request */
294*cc9203bfSDan Williams 	if (
295*cc9203bfSDan Williams 		(io_request != NULL)
296*cc9203bfSDan Williams 		&& (io_request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG)
297*cc9203bfSDan Williams 		&& (
298*cc9203bfSDan Williams 			scic_sds_io_tag_get_sequence(io_request->io_tag)
299*cc9203bfSDan Williams 			== scic->io_request_sequence[index]
300*cc9203bfSDan Williams 			)
301*cc9203bfSDan Williams 		) {
302*cc9203bfSDan Williams 		/* Yep this is a valid io request pass it along to the io request handler */
303*cc9203bfSDan Williams 		scic_sds_io_request_tc_completion(io_request, completion_entry);
304*cc9203bfSDan Williams 	}
305*cc9203bfSDan Williams }
306*cc9203bfSDan Williams 
307*cc9203bfSDan Williams static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
308*cc9203bfSDan Williams 						u32 completion_entry)
309*cc9203bfSDan Williams {
310*cc9203bfSDan Williams 	u32 index;
311*cc9203bfSDan Williams 	struct scic_sds_request *io_request;
312*cc9203bfSDan Williams 	struct scic_sds_remote_device *device;
313*cc9203bfSDan Williams 
314*cc9203bfSDan Williams 	index = SCU_GET_COMPLETION_INDEX(completion_entry);
315*cc9203bfSDan Williams 
316*cc9203bfSDan Williams 	switch (scu_get_command_request_type(completion_entry)) {
317*cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
318*cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
319*cc9203bfSDan Williams 		io_request = scic->io_request_table[index];
320*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
321*cc9203bfSDan Williams 			 "%s: SCIC SDS Completion type SDMA %x for io request "
322*cc9203bfSDan Williams 			 "%p\n",
323*cc9203bfSDan Williams 			 __func__,
324*cc9203bfSDan Williams 			 completion_entry,
325*cc9203bfSDan Williams 			 io_request);
326*cc9203bfSDan Williams 		/* @todo For a post TC operation we need to fail the IO
327*cc9203bfSDan Williams 		 * request
328*cc9203bfSDan Williams 		 */
329*cc9203bfSDan Williams 		break;
330*cc9203bfSDan Williams 
331*cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
332*cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
333*cc9203bfSDan Williams 	case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
334*cc9203bfSDan Williams 		device = scic->device_table[index];
335*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
336*cc9203bfSDan Williams 			 "%s: SCIC SDS Completion type SDMA %x for remote "
337*cc9203bfSDan Williams 			 "device %p\n",
338*cc9203bfSDan Williams 			 __func__,
339*cc9203bfSDan Williams 			 completion_entry,
340*cc9203bfSDan Williams 			 device);
341*cc9203bfSDan Williams 		/* @todo For a port RNC operation we need to fail the
342*cc9203bfSDan Williams 		 * device
343*cc9203bfSDan Williams 		 */
344*cc9203bfSDan Williams 		break;
345*cc9203bfSDan Williams 
346*cc9203bfSDan Williams 	default:
347*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
348*cc9203bfSDan Williams 			 "%s: SCIC SDS Completion unknown SDMA completion "
349*cc9203bfSDan Williams 			 "type %x\n",
350*cc9203bfSDan Williams 			 __func__,
351*cc9203bfSDan Williams 			 completion_entry);
352*cc9203bfSDan Williams 		break;
353*cc9203bfSDan Williams 
354*cc9203bfSDan Williams 	}
355*cc9203bfSDan Williams }
356*cc9203bfSDan Williams 
357*cc9203bfSDan Williams static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
358*cc9203bfSDan Williams 						  u32 completion_entry)
359*cc9203bfSDan Williams {
360*cc9203bfSDan Williams 	u32 index;
361*cc9203bfSDan Williams 	u32 frame_index;
362*cc9203bfSDan Williams 
363*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
364*cc9203bfSDan Williams 	struct scu_unsolicited_frame_header *frame_header;
365*cc9203bfSDan Williams 	struct scic_sds_phy *phy;
366*cc9203bfSDan Williams 	struct scic_sds_remote_device *device;
367*cc9203bfSDan Williams 
368*cc9203bfSDan Williams 	enum sci_status result = SCI_FAILURE;
369*cc9203bfSDan Williams 
370*cc9203bfSDan Williams 	frame_index = SCU_GET_FRAME_INDEX(completion_entry);
371*cc9203bfSDan Williams 
372*cc9203bfSDan Williams 	frame_header = scic->uf_control.buffers.array[frame_index].header;
373*cc9203bfSDan Williams 	scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
374*cc9203bfSDan Williams 
375*cc9203bfSDan Williams 	if (SCU_GET_FRAME_ERROR(completion_entry)) {
376*cc9203bfSDan Williams 		/*
377*cc9203bfSDan Williams 		 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
378*cc9203bfSDan Williams 		 * /       this cause a problem? We expect the phy initialization will
379*cc9203bfSDan Williams 		 * /       fail if there is an error in the frame. */
380*cc9203bfSDan Williams 		scic_sds_controller_release_frame(scic, frame_index);
381*cc9203bfSDan Williams 		return;
382*cc9203bfSDan Williams 	}
383*cc9203bfSDan Williams 
384*cc9203bfSDan Williams 	if (frame_header->is_address_frame) {
385*cc9203bfSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
386*cc9203bfSDan Williams 		phy = &ihost->phys[index].sci;
387*cc9203bfSDan Williams 		result = scic_sds_phy_frame_handler(phy, frame_index);
388*cc9203bfSDan Williams 	} else {
389*cc9203bfSDan Williams 
390*cc9203bfSDan Williams 		index = SCU_GET_COMPLETION_INDEX(completion_entry);
391*cc9203bfSDan Williams 
392*cc9203bfSDan Williams 		if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
393*cc9203bfSDan Williams 			/*
394*cc9203bfSDan Williams 			 * This is a signature fis or a frame from a direct attached SATA
395*cc9203bfSDan Williams 			 * device that has not yet been created.  In either case forwared
396*cc9203bfSDan Williams 			 * the frame to the PE and let it take care of the frame data. */
397*cc9203bfSDan Williams 			index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
398*cc9203bfSDan Williams 			phy = &ihost->phys[index].sci;
399*cc9203bfSDan Williams 			result = scic_sds_phy_frame_handler(phy, frame_index);
400*cc9203bfSDan Williams 		} else {
401*cc9203bfSDan Williams 			if (index < scic->remote_node_entries)
402*cc9203bfSDan Williams 				device = scic->device_table[index];
403*cc9203bfSDan Williams 			else
404*cc9203bfSDan Williams 				device = NULL;
405*cc9203bfSDan Williams 
406*cc9203bfSDan Williams 			if (device != NULL)
407*cc9203bfSDan Williams 				result = scic_sds_remote_device_frame_handler(device, frame_index);
408*cc9203bfSDan Williams 			else
409*cc9203bfSDan Williams 				scic_sds_controller_release_frame(scic, frame_index);
410*cc9203bfSDan Williams 		}
411*cc9203bfSDan Williams 	}
412*cc9203bfSDan Williams 
413*cc9203bfSDan Williams 	if (result != SCI_SUCCESS) {
414*cc9203bfSDan Williams 		/*
415*cc9203bfSDan Williams 		 * / @todo Is there any reason to report some additional error message
416*cc9203bfSDan Williams 		 * /       when we get this failure notifiction? */
417*cc9203bfSDan Williams 	}
418*cc9203bfSDan Williams }
419*cc9203bfSDan Williams 
420*cc9203bfSDan Williams static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
421*cc9203bfSDan Williams 						 u32 completion_entry)
422*cc9203bfSDan Williams {
423*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
424*cc9203bfSDan Williams 	struct scic_sds_request *io_request;
425*cc9203bfSDan Williams 	struct scic_sds_remote_device *device;
426*cc9203bfSDan Williams 	struct scic_sds_phy *phy;
427*cc9203bfSDan Williams 	u32 index;
428*cc9203bfSDan Williams 
429*cc9203bfSDan Williams 	index = SCU_GET_COMPLETION_INDEX(completion_entry);
430*cc9203bfSDan Williams 
431*cc9203bfSDan Williams 	switch (scu_get_event_type(completion_entry)) {
432*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
433*cc9203bfSDan Williams 		/* / @todo The driver did something wrong and we need to fix the condtion. */
434*cc9203bfSDan Williams 		dev_err(scic_to_dev(scic),
435*cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received SMU command error "
436*cc9203bfSDan Williams 			"0x%x\n",
437*cc9203bfSDan Williams 			__func__,
438*cc9203bfSDan Williams 			scic,
439*cc9203bfSDan Williams 			completion_entry);
440*cc9203bfSDan Williams 		break;
441*cc9203bfSDan Williams 
442*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
443*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_SMU_ERROR:
444*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
445*cc9203bfSDan Williams 		/*
446*cc9203bfSDan Williams 		 * / @todo This is a hardware failure and its likely that we want to
447*cc9203bfSDan Williams 		 * /       reset the controller. */
448*cc9203bfSDan Williams 		dev_err(scic_to_dev(scic),
449*cc9203bfSDan Williams 			"%s: SCIC Controller 0x%p received fatal controller "
450*cc9203bfSDan Williams 			"event  0x%x\n",
451*cc9203bfSDan Williams 			__func__,
452*cc9203bfSDan Williams 			scic,
453*cc9203bfSDan Williams 			completion_entry);
454*cc9203bfSDan Williams 		break;
455*cc9203bfSDan Williams 
456*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_TRANSPORT_ERROR:
457*cc9203bfSDan Williams 		io_request = scic->io_request_table[index];
458*cc9203bfSDan Williams 		scic_sds_io_request_event_handler(io_request, completion_entry);
459*cc9203bfSDan Williams 		break;
460*cc9203bfSDan Williams 
461*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
462*cc9203bfSDan Williams 		switch (scu_get_event_specifier(completion_entry)) {
463*cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
464*cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
465*cc9203bfSDan Williams 			io_request = scic->io_request_table[index];
466*cc9203bfSDan Williams 			if (io_request != NULL)
467*cc9203bfSDan Williams 				scic_sds_io_request_event_handler(io_request, completion_entry);
468*cc9203bfSDan Williams 			else
469*cc9203bfSDan Williams 				dev_warn(scic_to_dev(scic),
470*cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
471*cc9203bfSDan Williams 					 "event 0x%x for io request object "
472*cc9203bfSDan Williams 					 "that doesnt exist.\n",
473*cc9203bfSDan Williams 					 __func__,
474*cc9203bfSDan Williams 					 scic,
475*cc9203bfSDan Williams 					 completion_entry);
476*cc9203bfSDan Williams 
477*cc9203bfSDan Williams 			break;
478*cc9203bfSDan Williams 
479*cc9203bfSDan Williams 		case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
480*cc9203bfSDan Williams 			device = scic->device_table[index];
481*cc9203bfSDan Williams 			if (device != NULL)
482*cc9203bfSDan Williams 				scic_sds_remote_device_event_handler(device, completion_entry);
483*cc9203bfSDan Williams 			else
484*cc9203bfSDan Williams 				dev_warn(scic_to_dev(scic),
485*cc9203bfSDan Williams 					 "%s: SCIC Controller 0x%p received "
486*cc9203bfSDan Williams 					 "event 0x%x for remote device object "
487*cc9203bfSDan Williams 					 "that doesnt exist.\n",
488*cc9203bfSDan Williams 					 __func__,
489*cc9203bfSDan Williams 					 scic,
490*cc9203bfSDan Williams 					 completion_entry);
491*cc9203bfSDan Williams 
492*cc9203bfSDan Williams 			break;
493*cc9203bfSDan Williams 		}
494*cc9203bfSDan Williams 		break;
495*cc9203bfSDan Williams 
496*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_BROADCAST_CHANGE:
497*cc9203bfSDan Williams 	/*
498*cc9203bfSDan Williams 	 * direct the broadcast change event to the phy first and then let
499*cc9203bfSDan Williams 	 * the phy redirect the broadcast change to the port object */
500*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_ERR_CNT_EVENT:
501*cc9203bfSDan Williams 	/*
502*cc9203bfSDan Williams 	 * direct error counter event to the phy object since that is where
503*cc9203bfSDan Williams 	 * we get the event notification.  This is a type 4 event. */
504*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_OSSP_EVENT:
505*cc9203bfSDan Williams 		index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
506*cc9203bfSDan Williams 		phy = &ihost->phys[index].sci;
507*cc9203bfSDan Williams 		scic_sds_phy_event_handler(phy, completion_entry);
508*cc9203bfSDan Williams 		break;
509*cc9203bfSDan Williams 
510*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
511*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
512*cc9203bfSDan Williams 	case SCU_EVENT_TYPE_RNC_OPS_MISC:
513*cc9203bfSDan Williams 		if (index < scic->remote_node_entries) {
514*cc9203bfSDan Williams 			device = scic->device_table[index];
515*cc9203bfSDan Williams 
516*cc9203bfSDan Williams 			if (device != NULL)
517*cc9203bfSDan Williams 				scic_sds_remote_device_event_handler(device, completion_entry);
518*cc9203bfSDan Williams 		} else
519*cc9203bfSDan Williams 			dev_err(scic_to_dev(scic),
520*cc9203bfSDan Williams 				"%s: SCIC Controller 0x%p received event 0x%x "
521*cc9203bfSDan Williams 				"for remote device object 0x%0x that doesnt "
522*cc9203bfSDan Williams 				"exist.\n",
523*cc9203bfSDan Williams 				__func__,
524*cc9203bfSDan Williams 				scic,
525*cc9203bfSDan Williams 				completion_entry,
526*cc9203bfSDan Williams 				index);
527*cc9203bfSDan Williams 
528*cc9203bfSDan Williams 		break;
529*cc9203bfSDan Williams 
530*cc9203bfSDan Williams 	default:
531*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
532*cc9203bfSDan Williams 			 "%s: SCIC Controller received unknown event code %x\n",
533*cc9203bfSDan Williams 			 __func__,
534*cc9203bfSDan Williams 			 completion_entry);
535*cc9203bfSDan Williams 		break;
536*cc9203bfSDan Williams 	}
537*cc9203bfSDan Williams }
538*cc9203bfSDan Williams 
539*cc9203bfSDan Williams 
540*cc9203bfSDan Williams 
541*cc9203bfSDan Williams static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
542*cc9203bfSDan Williams {
543*cc9203bfSDan Williams 	u32 completion_count = 0;
544*cc9203bfSDan Williams 	u32 completion_entry;
545*cc9203bfSDan Williams 	u32 get_index;
546*cc9203bfSDan Williams 	u32 get_cycle;
547*cc9203bfSDan Williams 	u32 event_index;
548*cc9203bfSDan Williams 	u32 event_cycle;
549*cc9203bfSDan Williams 
550*cc9203bfSDan Williams 	dev_dbg(scic_to_dev(scic),
551*cc9203bfSDan Williams 		"%s: completion queue begining get:0x%08x\n",
552*cc9203bfSDan Williams 		__func__,
553*cc9203bfSDan Williams 		scic->completion_queue_get);
554*cc9203bfSDan Williams 
555*cc9203bfSDan Williams 	/* Get the component parts of the completion queue */
556*cc9203bfSDan Williams 	get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
557*cc9203bfSDan Williams 	get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
558*cc9203bfSDan Williams 
559*cc9203bfSDan Williams 	event_index = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
560*cc9203bfSDan Williams 	event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
561*cc9203bfSDan Williams 
562*cc9203bfSDan Williams 	while (
563*cc9203bfSDan Williams 		NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
564*cc9203bfSDan Williams 		== COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
565*cc9203bfSDan Williams 		) {
566*cc9203bfSDan Williams 		completion_count++;
567*cc9203bfSDan Williams 
568*cc9203bfSDan Williams 		completion_entry = scic->completion_queue[get_index];
569*cc9203bfSDan Williams 		INCREMENT_COMPLETION_QUEUE_GET(scic, get_index, get_cycle);
570*cc9203bfSDan Williams 
571*cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
572*cc9203bfSDan Williams 			"%s: completion queue entry:0x%08x\n",
573*cc9203bfSDan Williams 			__func__,
574*cc9203bfSDan Williams 			completion_entry);
575*cc9203bfSDan Williams 
576*cc9203bfSDan Williams 		switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
577*cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_TASK:
578*cc9203bfSDan Williams 			scic_sds_controller_task_completion(scic, completion_entry);
579*cc9203bfSDan Williams 			break;
580*cc9203bfSDan Williams 
581*cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_SDMA:
582*cc9203bfSDan Williams 			scic_sds_controller_sdma_completion(scic, completion_entry);
583*cc9203bfSDan Williams 			break;
584*cc9203bfSDan Williams 
585*cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_UFI:
586*cc9203bfSDan Williams 			scic_sds_controller_unsolicited_frame(scic, completion_entry);
587*cc9203bfSDan Williams 			break;
588*cc9203bfSDan Williams 
589*cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_EVENT:
590*cc9203bfSDan Williams 			INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
591*cc9203bfSDan Williams 			scic_sds_controller_event_completion(scic, completion_entry);
592*cc9203bfSDan Williams 			break;
593*cc9203bfSDan Williams 
594*cc9203bfSDan Williams 		case SCU_COMPLETION_TYPE_NOTIFY:
595*cc9203bfSDan Williams 			/*
596*cc9203bfSDan Williams 			 * Presently we do the same thing with a notify event that we do with the
597*cc9203bfSDan Williams 			 * other event codes. */
598*cc9203bfSDan Williams 			INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
599*cc9203bfSDan Williams 			scic_sds_controller_event_completion(scic, completion_entry);
600*cc9203bfSDan Williams 			break;
601*cc9203bfSDan Williams 
602*cc9203bfSDan Williams 		default:
603*cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
604*cc9203bfSDan Williams 				 "%s: SCIC Controller received unknown "
605*cc9203bfSDan Williams 				 "completion type %x\n",
606*cc9203bfSDan Williams 				 __func__,
607*cc9203bfSDan Williams 				 completion_entry);
608*cc9203bfSDan Williams 			break;
609*cc9203bfSDan Williams 		}
610*cc9203bfSDan Williams 	}
611*cc9203bfSDan Williams 
612*cc9203bfSDan Williams 	/* Update the get register if we completed one or more entries */
613*cc9203bfSDan Williams 	if (completion_count > 0) {
614*cc9203bfSDan Williams 		scic->completion_queue_get =
615*cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(ENABLE) |
616*cc9203bfSDan Williams 			SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
617*cc9203bfSDan Williams 			event_cycle |
618*cc9203bfSDan Williams 			SMU_CQGR_GEN_VAL(EVENT_POINTER, event_index) |
619*cc9203bfSDan Williams 			get_cycle |
620*cc9203bfSDan Williams 			SMU_CQGR_GEN_VAL(POINTER, get_index);
621*cc9203bfSDan Williams 
622*cc9203bfSDan Williams 		writel(scic->completion_queue_get,
623*cc9203bfSDan Williams 		       &scic->smu_registers->completion_queue_get);
624*cc9203bfSDan Williams 
625*cc9203bfSDan Williams 	}
626*cc9203bfSDan Williams 
627*cc9203bfSDan Williams 	dev_dbg(scic_to_dev(scic),
628*cc9203bfSDan Williams 		"%s: completion queue ending get:0x%08x\n",
629*cc9203bfSDan Williams 		__func__,
630*cc9203bfSDan Williams 		scic->completion_queue_get);
631*cc9203bfSDan Williams 
632*cc9203bfSDan Williams }
633*cc9203bfSDan Williams 
634*cc9203bfSDan Williams static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
635*cc9203bfSDan Williams {
636*cc9203bfSDan Williams 	u32 interrupt_status;
637*cc9203bfSDan Williams 
638*cc9203bfSDan Williams 	interrupt_status =
639*cc9203bfSDan Williams 		readl(&scic->smu_registers->interrupt_status);
640*cc9203bfSDan Williams 
641*cc9203bfSDan Williams 	if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
642*cc9203bfSDan Williams 	    scic_sds_controller_completion_queue_has_entries(scic)) {
643*cc9203bfSDan Williams 
644*cc9203bfSDan Williams 		scic_sds_controller_process_completions(scic);
645*cc9203bfSDan Williams 		writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
646*cc9203bfSDan Williams 	} else {
647*cc9203bfSDan Williams 		dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
648*cc9203bfSDan Williams 			interrupt_status);
649*cc9203bfSDan Williams 
650*cc9203bfSDan Williams 		sci_base_state_machine_change_state(&scic->state_machine,
651*cc9203bfSDan Williams 						    SCI_BASE_CONTROLLER_STATE_FAILED);
652*cc9203bfSDan Williams 
653*cc9203bfSDan Williams 		return;
654*cc9203bfSDan Williams 	}
655*cc9203bfSDan Williams 
656*cc9203bfSDan Williams 	/* If we dont process any completions I am not sure that we want to do this.
657*cc9203bfSDan Williams 	 * We are in the middle of a hardware fault and should probably be reset.
658*cc9203bfSDan Williams 	 */
659*cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
660*cc9203bfSDan Williams }
661*cc9203bfSDan Williams 
662c7ef4031SDan Williams irqreturn_t isci_intx_isr(int vec, void *data)
6636f231ddaSDan Williams {
6646f231ddaSDan Williams 	irqreturn_t ret = IRQ_NONE;
66531e824edSDan Williams 	struct isci_host *ihost = data;
666cc3dbd0aSArtur Wojcik 	struct scic_sds_controller *scic = &ihost->sci;
6676f231ddaSDan Williams 
668c7ef4031SDan Williams 	if (scic_sds_controller_isr(scic)) {
66931e824edSDan Williams 		writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
670c7ef4031SDan Williams 		tasklet_schedule(&ihost->completion_tasklet);
6716f231ddaSDan Williams 		ret = IRQ_HANDLED;
67292f4f0f5SDan Williams 	} else if (scic_sds_controller_error_isr(scic)) {
67392f4f0f5SDan Williams 		spin_lock(&ihost->scic_lock);
67492f4f0f5SDan Williams 		scic_sds_controller_error_handler(scic);
67592f4f0f5SDan Williams 		spin_unlock(&ihost->scic_lock);
67692f4f0f5SDan Williams 		ret = IRQ_HANDLED;
6776f231ddaSDan Williams 	}
67892f4f0f5SDan Williams 
6796f231ddaSDan Williams 	return ret;
6806f231ddaSDan Williams }
6816f231ddaSDan Williams 
68292f4f0f5SDan Williams irqreturn_t isci_error_isr(int vec, void *data)
68392f4f0f5SDan Williams {
68492f4f0f5SDan Williams 	struct isci_host *ihost = data;
68592f4f0f5SDan Williams 
686cc3dbd0aSArtur Wojcik 	if (scic_sds_controller_error_isr(&ihost->sci))
687cc3dbd0aSArtur Wojcik 		scic_sds_controller_error_handler(&ihost->sci);
68892f4f0f5SDan Williams 
68992f4f0f5SDan Williams 	return IRQ_HANDLED;
69092f4f0f5SDan Williams }
6916f231ddaSDan Williams 
6926f231ddaSDan Williams /**
6936f231ddaSDan Williams  * isci_host_start_complete() - This function is called by the core library,
6946f231ddaSDan Williams  *    through the ISCI Module, to indicate controller start status.
6956f231ddaSDan Williams  * @isci_host: This parameter specifies the ISCI host object
6966f231ddaSDan Williams  * @completion_status: This parameter specifies the completion status from the
6976f231ddaSDan Williams  *    core library.
6986f231ddaSDan Williams  *
6996f231ddaSDan Williams  */
700*cc9203bfSDan Williams static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
7016f231ddaSDan Williams {
7020cf89d1dSDan Williams 	if (completion_status != SCI_SUCCESS)
7030cf89d1dSDan Williams 		dev_info(&ihost->pdev->dev,
7040cf89d1dSDan Williams 			"controller start timed out, continuing...\n");
7050cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_ready);
7060cf89d1dSDan Williams 	clear_bit(IHOST_START_PENDING, &ihost->flags);
7070cf89d1dSDan Williams 	wake_up(&ihost->eventq);
7086f231ddaSDan Williams }
7096f231ddaSDan Williams 
710c7ef4031SDan Williams int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
7116f231ddaSDan Williams {
7124393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
7136f231ddaSDan Williams 
71477950f51SEdmund Nadolski 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
7156f231ddaSDan Williams 		return 0;
7166f231ddaSDan Williams 
71777950f51SEdmund Nadolski 	/* todo: use sas_flush_discovery once it is upstream */
71877950f51SEdmund Nadolski 	scsi_flush_work(shost);
71977950f51SEdmund Nadolski 
72077950f51SEdmund Nadolski 	scsi_flush_work(shost);
7216f231ddaSDan Williams 
7220cf89d1dSDan Williams 	dev_dbg(&ihost->pdev->dev,
7230cf89d1dSDan Williams 		"%s: ihost->status = %d, time = %ld\n",
7240cf89d1dSDan Williams 		 __func__, isci_host_get_state(ihost), time);
7256f231ddaSDan Williams 
7266f231ddaSDan Williams 	return 1;
7276f231ddaSDan Williams 
7286f231ddaSDan Williams }
7296f231ddaSDan Williams 
730*cc9203bfSDan Williams /**
731*cc9203bfSDan Williams  * scic_controller_get_suggested_start_timeout() - This method returns the
732*cc9203bfSDan Williams  *    suggested scic_controller_start() timeout amount.  The user is free to
733*cc9203bfSDan Williams  *    use any timeout value, but this method provides the suggested minimum
734*cc9203bfSDan Williams  *    start timeout value.  The returned value is based upon empirical
735*cc9203bfSDan Williams  *    information determined as a result of interoperability testing.
736*cc9203bfSDan Williams  * @controller: the handle to the controller object for which to return the
737*cc9203bfSDan Williams  *    suggested start timeout.
738*cc9203bfSDan Williams  *
739*cc9203bfSDan Williams  * This method returns the number of milliseconds for the suggested start
740*cc9203bfSDan Williams  * operation timeout.
741*cc9203bfSDan Williams  */
742*cc9203bfSDan Williams static u32 scic_controller_get_suggested_start_timeout(
743*cc9203bfSDan Williams 	struct scic_sds_controller *sc)
744*cc9203bfSDan Williams {
745*cc9203bfSDan Williams 	/* Validate the user supplied parameters. */
746*cc9203bfSDan Williams 	if (sc == NULL)
747*cc9203bfSDan Williams 		return 0;
748*cc9203bfSDan Williams 
749*cc9203bfSDan Williams 	/*
750*cc9203bfSDan Williams 	 * The suggested minimum timeout value for a controller start operation:
751*cc9203bfSDan Williams 	 *
752*cc9203bfSDan Williams 	 *     Signature FIS Timeout
753*cc9203bfSDan Williams 	 *   + Phy Start Timeout
754*cc9203bfSDan Williams 	 *   + Number of Phy Spin Up Intervals
755*cc9203bfSDan Williams 	 *   ---------------------------------
756*cc9203bfSDan Williams 	 *   Number of milliseconds for the controller start operation.
757*cc9203bfSDan Williams 	 *
758*cc9203bfSDan Williams 	 * NOTE: The number of phy spin up intervals will be equivalent
759*cc9203bfSDan Williams 	 *       to the number of phys divided by the number phys allowed
760*cc9203bfSDan Williams 	 *       per interval - 1 (once OEM parameters are supported).
761*cc9203bfSDan Williams 	 *       Currently we assume only 1 phy per interval. */
762*cc9203bfSDan Williams 
763*cc9203bfSDan Williams 	return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
764*cc9203bfSDan Williams 		+ SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
765*cc9203bfSDan Williams 		+ ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
766*cc9203bfSDan Williams }
767*cc9203bfSDan Williams 
768*cc9203bfSDan Williams static void scic_controller_enable_interrupts(
769*cc9203bfSDan Williams 	struct scic_sds_controller *scic)
770*cc9203bfSDan Williams {
771*cc9203bfSDan Williams 	BUG_ON(scic->smu_registers == NULL);
772*cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
773*cc9203bfSDan Williams }
774*cc9203bfSDan Williams 
775*cc9203bfSDan Williams void scic_controller_disable_interrupts(
776*cc9203bfSDan Williams 	struct scic_sds_controller *scic)
777*cc9203bfSDan Williams {
778*cc9203bfSDan Williams 	BUG_ON(scic->smu_registers == NULL);
779*cc9203bfSDan Williams 	writel(0xffffffff, &scic->smu_registers->interrupt_mask);
780*cc9203bfSDan Williams }
781*cc9203bfSDan Williams 
782*cc9203bfSDan Williams static void scic_sds_controller_enable_port_task_scheduler(
783*cc9203bfSDan Williams 	struct scic_sds_controller *scic)
784*cc9203bfSDan Williams {
785*cc9203bfSDan Williams 	u32 port_task_scheduler_value;
786*cc9203bfSDan Williams 
787*cc9203bfSDan Williams 	port_task_scheduler_value =
788*cc9203bfSDan Williams 		readl(&scic->scu_registers->peg0.ptsg.control);
789*cc9203bfSDan Williams 	port_task_scheduler_value |=
790*cc9203bfSDan Williams 		(SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
791*cc9203bfSDan Williams 		 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
792*cc9203bfSDan Williams 	writel(port_task_scheduler_value,
793*cc9203bfSDan Williams 	       &scic->scu_registers->peg0.ptsg.control);
794*cc9203bfSDan Williams }
795*cc9203bfSDan Williams 
796*cc9203bfSDan Williams static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
797*cc9203bfSDan Williams {
798*cc9203bfSDan Williams 	u32 task_assignment;
799*cc9203bfSDan Williams 
800*cc9203bfSDan Williams 	/*
801*cc9203bfSDan Williams 	 * Assign all the TCs to function 0
802*cc9203bfSDan Williams 	 * TODO: Do we actually need to read this register to write it back?
803*cc9203bfSDan Williams 	 */
804*cc9203bfSDan Williams 
805*cc9203bfSDan Williams 	task_assignment =
806*cc9203bfSDan Williams 		readl(&scic->smu_registers->task_context_assignment[0]);
807*cc9203bfSDan Williams 
808*cc9203bfSDan Williams 	task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
809*cc9203bfSDan Williams 		(SMU_TCA_GEN_VAL(ENDING,  scic->task_context_entries - 1)) |
810*cc9203bfSDan Williams 		(SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
811*cc9203bfSDan Williams 
812*cc9203bfSDan Williams 	writel(task_assignment,
813*cc9203bfSDan Williams 		&scic->smu_registers->task_context_assignment[0]);
814*cc9203bfSDan Williams 
815*cc9203bfSDan Williams }
816*cc9203bfSDan Williams 
817*cc9203bfSDan Williams static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
818*cc9203bfSDan Williams {
819*cc9203bfSDan Williams 	u32 index;
820*cc9203bfSDan Williams 	u32 completion_queue_control_value;
821*cc9203bfSDan Williams 	u32 completion_queue_get_value;
822*cc9203bfSDan Williams 	u32 completion_queue_put_value;
823*cc9203bfSDan Williams 
824*cc9203bfSDan Williams 	scic->completion_queue_get = 0;
825*cc9203bfSDan Williams 
826*cc9203bfSDan Williams 	completion_queue_control_value = (
827*cc9203bfSDan Williams 		SMU_CQC_QUEUE_LIMIT_SET(scic->completion_queue_entries - 1)
828*cc9203bfSDan Williams 		| SMU_CQC_EVENT_LIMIT_SET(scic->completion_event_entries - 1)
829*cc9203bfSDan Williams 		);
830*cc9203bfSDan Williams 
831*cc9203bfSDan Williams 	writel(completion_queue_control_value,
832*cc9203bfSDan Williams 	       &scic->smu_registers->completion_queue_control);
833*cc9203bfSDan Williams 
834*cc9203bfSDan Williams 
835*cc9203bfSDan Williams 	/* Set the completion queue get pointer and enable the queue */
836*cc9203bfSDan Williams 	completion_queue_get_value = (
837*cc9203bfSDan Williams 		(SMU_CQGR_GEN_VAL(POINTER, 0))
838*cc9203bfSDan Williams 		| (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
839*cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(ENABLE))
840*cc9203bfSDan Williams 		| (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
841*cc9203bfSDan Williams 		);
842*cc9203bfSDan Williams 
843*cc9203bfSDan Williams 	writel(completion_queue_get_value,
844*cc9203bfSDan Williams 	       &scic->smu_registers->completion_queue_get);
845*cc9203bfSDan Williams 
846*cc9203bfSDan Williams 	/* Set the completion queue put pointer */
847*cc9203bfSDan Williams 	completion_queue_put_value = (
848*cc9203bfSDan Williams 		(SMU_CQPR_GEN_VAL(POINTER, 0))
849*cc9203bfSDan Williams 		| (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
850*cc9203bfSDan Williams 		);
851*cc9203bfSDan Williams 
852*cc9203bfSDan Williams 	writel(completion_queue_put_value,
853*cc9203bfSDan Williams 	       &scic->smu_registers->completion_queue_put);
854*cc9203bfSDan Williams 
855*cc9203bfSDan Williams 	/* Initialize the cycle bit of the completion queue entries */
856*cc9203bfSDan Williams 	for (index = 0; index < scic->completion_queue_entries; index++) {
857*cc9203bfSDan Williams 		/*
858*cc9203bfSDan Williams 		 * If get.cycle_bit != completion_queue.cycle_bit
859*cc9203bfSDan Williams 		 * its not a valid completion queue entry
860*cc9203bfSDan Williams 		 * so at system start all entries are invalid */
861*cc9203bfSDan Williams 		scic->completion_queue[index] = 0x80000000;
862*cc9203bfSDan Williams 	}
863*cc9203bfSDan Williams }
864*cc9203bfSDan Williams 
865*cc9203bfSDan Williams static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
866*cc9203bfSDan Williams {
867*cc9203bfSDan Williams 	u32 frame_queue_control_value;
868*cc9203bfSDan Williams 	u32 frame_queue_get_value;
869*cc9203bfSDan Williams 	u32 frame_queue_put_value;
870*cc9203bfSDan Williams 
871*cc9203bfSDan Williams 	/* Write the queue size */
872*cc9203bfSDan Williams 	frame_queue_control_value =
873*cc9203bfSDan Williams 		SCU_UFQC_GEN_VAL(QUEUE_SIZE,
874*cc9203bfSDan Williams 				 scic->uf_control.address_table.count);
875*cc9203bfSDan Williams 
876*cc9203bfSDan Williams 	writel(frame_queue_control_value,
877*cc9203bfSDan Williams 	       &scic->scu_registers->sdma.unsolicited_frame_queue_control);
878*cc9203bfSDan Williams 
879*cc9203bfSDan Williams 	/* Setup the get pointer for the unsolicited frame queue */
880*cc9203bfSDan Williams 	frame_queue_get_value = (
881*cc9203bfSDan Williams 		SCU_UFQGP_GEN_VAL(POINTER, 0)
882*cc9203bfSDan Williams 		|  SCU_UFQGP_GEN_BIT(ENABLE_BIT)
883*cc9203bfSDan Williams 		);
884*cc9203bfSDan Williams 
885*cc9203bfSDan Williams 	writel(frame_queue_get_value,
886*cc9203bfSDan Williams 	       &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
887*cc9203bfSDan Williams 	/* Setup the put pointer for the unsolicited frame queue */
888*cc9203bfSDan Williams 	frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
889*cc9203bfSDan Williams 	writel(frame_queue_put_value,
890*cc9203bfSDan Williams 	       &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
891*cc9203bfSDan Williams }
892*cc9203bfSDan Williams 
893*cc9203bfSDan Williams /**
894*cc9203bfSDan Williams  * This method will attempt to transition into the ready state for the
895*cc9203bfSDan Williams  *    controller and indicate that the controller start operation has completed
896*cc9203bfSDan Williams  *    if all criteria are met.
897*cc9203bfSDan Williams  * @scic: This parameter indicates the controller object for which
898*cc9203bfSDan Williams  *    to transition to ready.
899*cc9203bfSDan Williams  * @status: This parameter indicates the status value to be pass into the call
900*cc9203bfSDan Williams  *    to scic_cb_controller_start_complete().
901*cc9203bfSDan Williams  *
902*cc9203bfSDan Williams  * none.
903*cc9203bfSDan Williams  */
904*cc9203bfSDan Williams static void scic_sds_controller_transition_to_ready(
905*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
906*cc9203bfSDan Williams 	enum sci_status status)
907*cc9203bfSDan Williams {
908*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
909*cc9203bfSDan Williams 
910*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id ==
911*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_STARTING) {
912*cc9203bfSDan Williams 		/*
913*cc9203bfSDan Williams 		 * We move into the ready state, because some of the phys/ports
914*cc9203bfSDan Williams 		 * may be up and operational.
915*cc9203bfSDan Williams 		 */
916*cc9203bfSDan Williams 		sci_base_state_machine_change_state(&scic->state_machine,
917*cc9203bfSDan Williams 						    SCI_BASE_CONTROLLER_STATE_READY);
918*cc9203bfSDan Williams 
919*cc9203bfSDan Williams 		isci_host_start_complete(ihost, status);
920*cc9203bfSDan Williams 	}
921*cc9203bfSDan Williams }
922*cc9203bfSDan Williams 
923*cc9203bfSDan Williams static void scic_sds_controller_phy_timer_stop(struct scic_sds_controller *scic)
924*cc9203bfSDan Williams {
925*cc9203bfSDan Williams 	isci_timer_stop(scic->phy_startup_timer);
926*cc9203bfSDan Williams 
927*cc9203bfSDan Williams 	scic->phy_startup_timer_pending = false;
928*cc9203bfSDan Williams }
929*cc9203bfSDan Williams 
930*cc9203bfSDan Williams static void scic_sds_controller_phy_timer_start(struct scic_sds_controller *scic)
931*cc9203bfSDan Williams {
932*cc9203bfSDan Williams 	isci_timer_start(scic->phy_startup_timer,
933*cc9203bfSDan Williams 			 SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
934*cc9203bfSDan Williams 
935*cc9203bfSDan Williams 	scic->phy_startup_timer_pending = true;
936*cc9203bfSDan Williams }
937*cc9203bfSDan Williams 
938*cc9203bfSDan Williams /**
939*cc9203bfSDan Williams  * scic_sds_controller_start_next_phy - start phy
940*cc9203bfSDan Williams  * @scic: controller
941*cc9203bfSDan Williams  *
942*cc9203bfSDan Williams  * If all the phys have been started, then attempt to transition the
943*cc9203bfSDan Williams  * controller to the READY state and inform the user
944*cc9203bfSDan Williams  * (scic_cb_controller_start_complete()).
945*cc9203bfSDan Williams  */
946*cc9203bfSDan Williams static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
947*cc9203bfSDan Williams {
948*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
949*cc9203bfSDan Williams 	struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
950*cc9203bfSDan Williams 	struct scic_sds_phy *sci_phy;
951*cc9203bfSDan Williams 	enum sci_status status;
952*cc9203bfSDan Williams 
953*cc9203bfSDan Williams 	status = SCI_SUCCESS;
954*cc9203bfSDan Williams 
955*cc9203bfSDan Williams 	if (scic->phy_startup_timer_pending)
956*cc9203bfSDan Williams 		return status;
957*cc9203bfSDan Williams 
958*cc9203bfSDan Williams 	if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
959*cc9203bfSDan Williams 		bool is_controller_start_complete = true;
960*cc9203bfSDan Williams 		u32 state;
961*cc9203bfSDan Williams 		u8 index;
962*cc9203bfSDan Williams 
963*cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
964*cc9203bfSDan Williams 			sci_phy = &ihost->phys[index].sci;
965*cc9203bfSDan Williams 			state = sci_phy->state_machine.current_state_id;
966*cc9203bfSDan Williams 
967*cc9203bfSDan Williams 			if (!scic_sds_phy_get_port(sci_phy))
968*cc9203bfSDan Williams 				continue;
969*cc9203bfSDan Williams 
970*cc9203bfSDan Williams 			/* The controller start operation is complete iff:
971*cc9203bfSDan Williams 			 * - all links have been given an opportunity to start
972*cc9203bfSDan Williams 			 * - have no indication of a connected device
973*cc9203bfSDan Williams 			 * - have an indication of a connected device and it has
974*cc9203bfSDan Williams 			 *   finished the link training process.
975*cc9203bfSDan Williams 			 */
976*cc9203bfSDan Williams 			if ((sci_phy->is_in_link_training == false &&
977*cc9203bfSDan Williams 			     state == SCI_BASE_PHY_STATE_INITIAL) ||
978*cc9203bfSDan Williams 			    (sci_phy->is_in_link_training == false &&
979*cc9203bfSDan Williams 			     state == SCI_BASE_PHY_STATE_STOPPED) ||
980*cc9203bfSDan Williams 			    (sci_phy->is_in_link_training == true &&
981*cc9203bfSDan Williams 			     state == SCI_BASE_PHY_STATE_STARTING)) {
982*cc9203bfSDan Williams 				is_controller_start_complete = false;
983*cc9203bfSDan Williams 				break;
984*cc9203bfSDan Williams 			}
985*cc9203bfSDan Williams 		}
986*cc9203bfSDan Williams 
987*cc9203bfSDan Williams 		/*
988*cc9203bfSDan Williams 		 * The controller has successfully finished the start process.
989*cc9203bfSDan Williams 		 * Inform the SCI Core user and transition to the READY state. */
990*cc9203bfSDan Williams 		if (is_controller_start_complete == true) {
991*cc9203bfSDan Williams 			scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
992*cc9203bfSDan Williams 			scic_sds_controller_phy_timer_stop(scic);
993*cc9203bfSDan Williams 		}
994*cc9203bfSDan Williams 	} else {
995*cc9203bfSDan Williams 		sci_phy = &ihost->phys[scic->next_phy_to_start].sci;
996*cc9203bfSDan Williams 
997*cc9203bfSDan Williams 		if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
998*cc9203bfSDan Williams 			if (scic_sds_phy_get_port(sci_phy) == NULL) {
999*cc9203bfSDan Williams 				scic->next_phy_to_start++;
1000*cc9203bfSDan Williams 
1001*cc9203bfSDan Williams 				/* Caution recursion ahead be forwarned
1002*cc9203bfSDan Williams 				 *
1003*cc9203bfSDan Williams 				 * The PHY was never added to a PORT in MPC mode
1004*cc9203bfSDan Williams 				 * so start the next phy in sequence This phy
1005*cc9203bfSDan Williams 				 * will never go link up and will not draw power
1006*cc9203bfSDan Williams 				 * the OEM parameters either configured the phy
1007*cc9203bfSDan Williams 				 * incorrectly for the PORT or it was never
1008*cc9203bfSDan Williams 				 * assigned to a PORT
1009*cc9203bfSDan Williams 				 */
1010*cc9203bfSDan Williams 				return scic_sds_controller_start_next_phy(scic);
1011*cc9203bfSDan Williams 			}
1012*cc9203bfSDan Williams 		}
1013*cc9203bfSDan Williams 
1014*cc9203bfSDan Williams 		status = scic_sds_phy_start(sci_phy);
1015*cc9203bfSDan Williams 
1016*cc9203bfSDan Williams 		if (status == SCI_SUCCESS) {
1017*cc9203bfSDan Williams 			scic_sds_controller_phy_timer_start(scic);
1018*cc9203bfSDan Williams 		} else {
1019*cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
1020*cc9203bfSDan Williams 				 "%s: Controller stop operation failed "
1021*cc9203bfSDan Williams 				 "to stop phy %d because of status "
1022*cc9203bfSDan Williams 				 "%d.\n",
1023*cc9203bfSDan Williams 				 __func__,
1024*cc9203bfSDan Williams 				 ihost->phys[scic->next_phy_to_start].sci.phy_index,
1025*cc9203bfSDan Williams 				 status);
1026*cc9203bfSDan Williams 		}
1027*cc9203bfSDan Williams 
1028*cc9203bfSDan Williams 		scic->next_phy_to_start++;
1029*cc9203bfSDan Williams 	}
1030*cc9203bfSDan Williams 
1031*cc9203bfSDan Williams 	return status;
1032*cc9203bfSDan Williams }
1033*cc9203bfSDan Williams 
1034*cc9203bfSDan Williams static void scic_sds_controller_phy_startup_timeout_handler(void *_scic)
1035*cc9203bfSDan Williams {
1036*cc9203bfSDan Williams 	struct scic_sds_controller *scic = _scic;
1037*cc9203bfSDan Williams 	enum sci_status status;
1038*cc9203bfSDan Williams 
1039*cc9203bfSDan Williams 	scic->phy_startup_timer_pending = false;
1040*cc9203bfSDan Williams 	status = SCI_FAILURE;
1041*cc9203bfSDan Williams 	while (status != SCI_SUCCESS)
1042*cc9203bfSDan Williams 		status = scic_sds_controller_start_next_phy(scic);
1043*cc9203bfSDan Williams }
1044*cc9203bfSDan Williams 
1045*cc9203bfSDan Williams static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
1046*cc9203bfSDan Williams 					     u32 timeout)
1047*cc9203bfSDan Williams {
1048*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1049*cc9203bfSDan Williams 	enum sci_status result;
1050*cc9203bfSDan Williams 	u16 index;
1051*cc9203bfSDan Williams 
1052*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
1053*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_INITIALIZED) {
1054*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
1055*cc9203bfSDan Williams 			 "SCIC Controller start operation requested in "
1056*cc9203bfSDan Williams 			 "invalid state\n");
1057*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1058*cc9203bfSDan Williams 	}
1059*cc9203bfSDan Williams 
1060*cc9203bfSDan Williams 	/* Build the TCi free pool */
1061*cc9203bfSDan Williams 	sci_pool_initialize(scic->tci_pool);
1062*cc9203bfSDan Williams 	for (index = 0; index < scic->task_context_entries; index++)
1063*cc9203bfSDan Williams 		sci_pool_put(scic->tci_pool, index);
1064*cc9203bfSDan Williams 
1065*cc9203bfSDan Williams 	/* Build the RNi free pool */
1066*cc9203bfSDan Williams 	scic_sds_remote_node_table_initialize(
1067*cc9203bfSDan Williams 			&scic->available_remote_nodes,
1068*cc9203bfSDan Williams 			scic->remote_node_entries);
1069*cc9203bfSDan Williams 
1070*cc9203bfSDan Williams 	/*
1071*cc9203bfSDan Williams 	 * Before anything else lets make sure we will not be
1072*cc9203bfSDan Williams 	 * interrupted by the hardware.
1073*cc9203bfSDan Williams 	 */
1074*cc9203bfSDan Williams 	scic_controller_disable_interrupts(scic);
1075*cc9203bfSDan Williams 
1076*cc9203bfSDan Williams 	/* Enable the port task scheduler */
1077*cc9203bfSDan Williams 	scic_sds_controller_enable_port_task_scheduler(scic);
1078*cc9203bfSDan Williams 
1079*cc9203bfSDan Williams 	/* Assign all the task entries to scic physical function */
1080*cc9203bfSDan Williams 	scic_sds_controller_assign_task_entries(scic);
1081*cc9203bfSDan Williams 
1082*cc9203bfSDan Williams 	/* Now initialize the completion queue */
1083*cc9203bfSDan Williams 	scic_sds_controller_initialize_completion_queue(scic);
1084*cc9203bfSDan Williams 
1085*cc9203bfSDan Williams 	/* Initialize the unsolicited frame queue for use */
1086*cc9203bfSDan Williams 	scic_sds_controller_initialize_unsolicited_frame_queue(scic);
1087*cc9203bfSDan Williams 
1088*cc9203bfSDan Williams 	/* Start all of the ports on this controller */
1089*cc9203bfSDan Williams 	for (index = 0; index < scic->logical_port_entries; index++) {
1090*cc9203bfSDan Williams 		struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1091*cc9203bfSDan Williams 
1092*cc9203bfSDan Williams 		result = sci_port->state_handlers->start_handler(sci_port);
1093*cc9203bfSDan Williams 		if (result)
1094*cc9203bfSDan Williams 			return result;
1095*cc9203bfSDan Williams 	}
1096*cc9203bfSDan Williams 
1097*cc9203bfSDan Williams 	scic_sds_controller_start_next_phy(scic);
1098*cc9203bfSDan Williams 
1099*cc9203bfSDan Williams 	isci_timer_start(scic->timeout_timer, timeout);
1100*cc9203bfSDan Williams 
1101*cc9203bfSDan Williams 	sci_base_state_machine_change_state(&scic->state_machine,
1102*cc9203bfSDan Williams 					    SCI_BASE_CONTROLLER_STATE_STARTING);
1103*cc9203bfSDan Williams 
1104*cc9203bfSDan Williams 	return SCI_SUCCESS;
1105*cc9203bfSDan Williams }
1106*cc9203bfSDan Williams 
11076f231ddaSDan Williams void isci_host_scan_start(struct Scsi_Host *shost)
11086f231ddaSDan Williams {
11094393aa4eSDan Williams 	struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
1110cc3dbd0aSArtur Wojcik 	unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
11116f231ddaSDan Williams 
11120cf89d1dSDan Williams 	set_bit(IHOST_START_PENDING, &ihost->flags);
111377950f51SEdmund Nadolski 
111477950f51SEdmund Nadolski 	spin_lock_irq(&ihost->scic_lock);
1115cc3dbd0aSArtur Wojcik 	scic_controller_start(&ihost->sci, tmo);
1116cc3dbd0aSArtur Wojcik 	scic_controller_enable_interrupts(&ihost->sci);
111777950f51SEdmund Nadolski 	spin_unlock_irq(&ihost->scic_lock);
11186f231ddaSDan Williams }
11196f231ddaSDan Williams 
1120*cc9203bfSDan Williams static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
11216f231ddaSDan Williams {
11220cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopped);
1123cc3dbd0aSArtur Wojcik 	scic_controller_disable_interrupts(&ihost->sci);
11240cf89d1dSDan Williams 	clear_bit(IHOST_STOP_PENDING, &ihost->flags);
11250cf89d1dSDan Williams 	wake_up(&ihost->eventq);
11266f231ddaSDan Williams }
11276f231ddaSDan Williams 
1128*cc9203bfSDan Williams static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
1129*cc9203bfSDan Williams {
1130*cc9203bfSDan Williams 	/* Empty out the completion queue */
1131*cc9203bfSDan Williams 	if (scic_sds_controller_completion_queue_has_entries(scic))
1132*cc9203bfSDan Williams 		scic_sds_controller_process_completions(scic);
1133*cc9203bfSDan Williams 
1134*cc9203bfSDan Williams 	/* Clear the interrupt and enable all interrupts again */
1135*cc9203bfSDan Williams 	writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
1136*cc9203bfSDan Williams 	/* Could we write the value of SMU_ISR_COMPLETION? */
1137*cc9203bfSDan Williams 	writel(0xFF000000, &scic->smu_registers->interrupt_mask);
1138*cc9203bfSDan Williams 	writel(0, &scic->smu_registers->interrupt_mask);
1139*cc9203bfSDan Williams }
1140*cc9203bfSDan Williams 
11416f231ddaSDan Williams /**
11426f231ddaSDan Williams  * isci_host_completion_routine() - This function is the delayed service
11436f231ddaSDan Williams  *    routine that calls the sci core library's completion handler. It's
11446f231ddaSDan Williams  *    scheduled as a tasklet from the interrupt service routine when interrupts
11456f231ddaSDan Williams  *    in use, or set as the timeout function in polled mode.
11466f231ddaSDan Williams  * @data: This parameter specifies the ISCI host object
11476f231ddaSDan Williams  *
11486f231ddaSDan Williams  */
11496f231ddaSDan Williams static void isci_host_completion_routine(unsigned long data)
11506f231ddaSDan Williams {
11516f231ddaSDan Williams 	struct isci_host *isci_host = (struct isci_host *)data;
11526f231ddaSDan Williams 	struct list_head    completed_request_list;
115311b00c19SJeff Skirvin 	struct list_head    errored_request_list;
11546f231ddaSDan Williams 	struct list_head    *current_position;
11556f231ddaSDan Williams 	struct list_head    *next_position;
11566f231ddaSDan Williams 	struct isci_request *request;
11576f231ddaSDan Williams 	struct isci_request *next_request;
11586f231ddaSDan Williams 	struct sas_task     *task;
11596f231ddaSDan Williams 
11606f231ddaSDan Williams 	INIT_LIST_HEAD(&completed_request_list);
116111b00c19SJeff Skirvin 	INIT_LIST_HEAD(&errored_request_list);
11626f231ddaSDan Williams 
11636f231ddaSDan Williams 	spin_lock_irq(&isci_host->scic_lock);
11646f231ddaSDan Williams 
1165cc3dbd0aSArtur Wojcik 	scic_sds_controller_completion_handler(&isci_host->sci);
1166c7ef4031SDan Williams 
11676f231ddaSDan Williams 	/* Take the lists of completed I/Os from the host. */
116811b00c19SJeff Skirvin 
11696f231ddaSDan Williams 	list_splice_init(&isci_host->requests_to_complete,
11706f231ddaSDan Williams 			 &completed_request_list);
11716f231ddaSDan Williams 
117211b00c19SJeff Skirvin 	/* Take the list of errored I/Os from the host. */
117311b00c19SJeff Skirvin 	list_splice_init(&isci_host->requests_to_errorback,
117411b00c19SJeff Skirvin 			 &errored_request_list);
11756f231ddaSDan Williams 
11766f231ddaSDan Williams 	spin_unlock_irq(&isci_host->scic_lock);
11776f231ddaSDan Williams 
11786f231ddaSDan Williams 	/* Process any completions in the lists. */
11796f231ddaSDan Williams 	list_for_each_safe(current_position, next_position,
11806f231ddaSDan Williams 			   &completed_request_list) {
11816f231ddaSDan Williams 
11826f231ddaSDan Williams 		request = list_entry(current_position, struct isci_request,
11836f231ddaSDan Williams 				     completed_node);
11846f231ddaSDan Williams 		task = isci_request_access_task(request);
11856f231ddaSDan Williams 
11866f231ddaSDan Williams 		/* Normal notification (task_done) */
11876f231ddaSDan Williams 		dev_dbg(&isci_host->pdev->dev,
11886f231ddaSDan Williams 			"%s: Normal - request/task = %p/%p\n",
11896f231ddaSDan Williams 			__func__,
11906f231ddaSDan Williams 			request,
11916f231ddaSDan Williams 			task);
11926f231ddaSDan Williams 
119311b00c19SJeff Skirvin 		/* Return the task to libsas */
119411b00c19SJeff Skirvin 		if (task != NULL) {
11956f231ddaSDan Williams 
119611b00c19SJeff Skirvin 			task->lldd_task = NULL;
119711b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
119811b00c19SJeff Skirvin 
119911b00c19SJeff Skirvin 				/* If the task is already in the abort path,
120011b00c19SJeff Skirvin 				* the task_done callback cannot be called.
120111b00c19SJeff Skirvin 				*/
120211b00c19SJeff Skirvin 				task->task_done(task);
120311b00c19SJeff Skirvin 			}
120411b00c19SJeff Skirvin 		}
12056f231ddaSDan Williams 		/* Free the request object. */
12066f231ddaSDan Williams 		isci_request_free(isci_host, request);
12076f231ddaSDan Williams 	}
120811b00c19SJeff Skirvin 	list_for_each_entry_safe(request, next_request, &errored_request_list,
12096f231ddaSDan Williams 				 completed_node) {
12106f231ddaSDan Williams 
12116f231ddaSDan Williams 		task = isci_request_access_task(request);
12126f231ddaSDan Williams 
12136f231ddaSDan Williams 		/* Use sas_task_abort */
12146f231ddaSDan Williams 		dev_warn(&isci_host->pdev->dev,
12156f231ddaSDan Williams 			 "%s: Error - request/task = %p/%p\n",
12166f231ddaSDan Williams 			 __func__,
12176f231ddaSDan Williams 			 request,
12186f231ddaSDan Williams 			 task);
12196f231ddaSDan Williams 
122011b00c19SJeff Skirvin 		if (task != NULL) {
122111b00c19SJeff Skirvin 
122211b00c19SJeff Skirvin 			/* Put the task into the abort path if it's not there
122311b00c19SJeff Skirvin 			 * already.
122411b00c19SJeff Skirvin 			 */
122511b00c19SJeff Skirvin 			if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
12266f231ddaSDan Williams 				sas_task_abort(task);
122711b00c19SJeff Skirvin 
122811b00c19SJeff Skirvin 		} else {
122911b00c19SJeff Skirvin 			/* This is a case where the request has completed with a
123011b00c19SJeff Skirvin 			 * status such that it needed further target servicing,
123111b00c19SJeff Skirvin 			 * but the sas_task reference has already been removed
123211b00c19SJeff Skirvin 			 * from the request.  Since it was errored, it was not
123311b00c19SJeff Skirvin 			 * being aborted, so there is nothing to do except free
123411b00c19SJeff Skirvin 			 * it.
123511b00c19SJeff Skirvin 			 */
123611b00c19SJeff Skirvin 
123711b00c19SJeff Skirvin 			spin_lock_irq(&isci_host->scic_lock);
123811b00c19SJeff Skirvin 			/* Remove the request from the remote device's list
123911b00c19SJeff Skirvin 			* of pending requests.
124011b00c19SJeff Skirvin 			*/
124111b00c19SJeff Skirvin 			list_del_init(&request->dev_node);
124211b00c19SJeff Skirvin 			spin_unlock_irq(&isci_host->scic_lock);
124311b00c19SJeff Skirvin 
124411b00c19SJeff Skirvin 			/* Free the request object. */
124511b00c19SJeff Skirvin 			isci_request_free(isci_host, request);
124611b00c19SJeff Skirvin 		}
12476f231ddaSDan Williams 	}
12486f231ddaSDan Williams 
12496f231ddaSDan Williams }
12506f231ddaSDan Williams 
1251*cc9203bfSDan Williams /**
1252*cc9203bfSDan Williams  * scic_controller_stop() - This method will stop an individual controller
1253*cc9203bfSDan Williams  *    object.This method will invoke the associated user callback upon
1254*cc9203bfSDan Williams  *    completion.  The completion callback is called when the following
1255*cc9203bfSDan Williams  *    conditions are met: -# the method return status is SCI_SUCCESS. -# the
1256*cc9203bfSDan Williams  *    controller has been quiesced. This method will ensure that all IO
1257*cc9203bfSDan Williams  *    requests are quiesced, phys are stopped, and all additional operation by
1258*cc9203bfSDan Williams  *    the hardware is halted.
1259*cc9203bfSDan Williams  * @controller: the handle to the controller object to stop.
1260*cc9203bfSDan Williams  * @timeout: This parameter specifies the number of milliseconds in which the
1261*cc9203bfSDan Williams  *    stop operation should complete.
1262*cc9203bfSDan Williams  *
1263*cc9203bfSDan Williams  * The controller must be in the STARTED or STOPPED state. Indicate if the
1264*cc9203bfSDan Williams  * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1265*cc9203bfSDan Williams  * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1266*cc9203bfSDan Williams  * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1267*cc9203bfSDan Williams  * controller is not either in the STARTED or STOPPED states.
1268*cc9203bfSDan Williams  */
1269*cc9203bfSDan Williams static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
1270*cc9203bfSDan Williams 					    u32 timeout)
1271*cc9203bfSDan Williams {
1272*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
1273*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_READY) {
1274*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
1275*cc9203bfSDan Williams 			 "SCIC Controller stop operation requested in "
1276*cc9203bfSDan Williams 			 "invalid state\n");
1277*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1278*cc9203bfSDan Williams 	}
1279*cc9203bfSDan Williams 
1280*cc9203bfSDan Williams 	isci_timer_start(scic->timeout_timer, timeout);
1281*cc9203bfSDan Williams 	sci_base_state_machine_change_state(&scic->state_machine,
1282*cc9203bfSDan Williams 					    SCI_BASE_CONTROLLER_STATE_STOPPING);
1283*cc9203bfSDan Williams 	return SCI_SUCCESS;
1284*cc9203bfSDan Williams }
1285*cc9203bfSDan Williams 
1286*cc9203bfSDan Williams /**
1287*cc9203bfSDan Williams  * scic_controller_reset() - This method will reset the supplied core
1288*cc9203bfSDan Williams  *    controller regardless of the state of said controller.  This operation is
1289*cc9203bfSDan Williams  *    considered destructive.  In other words, all current operations are wiped
1290*cc9203bfSDan Williams  *    out.  No IO completions for outstanding devices occur.  Outstanding IO
1291*cc9203bfSDan Williams  *    requests are not aborted or completed at the actual remote device.
1292*cc9203bfSDan Williams  * @controller: the handle to the controller object to reset.
1293*cc9203bfSDan Williams  *
1294*cc9203bfSDan Williams  * Indicate if the controller reset method succeeded or failed in some way.
1295*cc9203bfSDan Williams  * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1296*cc9203bfSDan Williams  * the controller reset operation is unable to complete.
1297*cc9203bfSDan Williams  */
1298*cc9203bfSDan Williams static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
1299*cc9203bfSDan Williams {
1300*cc9203bfSDan Williams 	switch (scic->state_machine.current_state_id) {
1301*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_RESET:
1302*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_READY:
1303*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_STOPPED:
1304*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_FAILED:
1305*cc9203bfSDan Williams 		/*
1306*cc9203bfSDan Williams 		 * The reset operation is not a graceful cleanup, just
1307*cc9203bfSDan Williams 		 * perform the state transition.
1308*cc9203bfSDan Williams 		 */
1309*cc9203bfSDan Williams 		sci_base_state_machine_change_state(&scic->state_machine,
1310*cc9203bfSDan Williams 				SCI_BASE_CONTROLLER_STATE_RESETTING);
1311*cc9203bfSDan Williams 		return SCI_SUCCESS;
1312*cc9203bfSDan Williams 	default:
1313*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
1314*cc9203bfSDan Williams 			 "SCIC Controller reset operation requested in "
1315*cc9203bfSDan Williams 			 "invalid state\n");
1316*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
1317*cc9203bfSDan Williams 	}
1318*cc9203bfSDan Williams }
1319*cc9203bfSDan Williams 
13200cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost)
13216f231ddaSDan Williams {
13226f231ddaSDan Williams 	int i;
13236f231ddaSDan Williams 
13240cf89d1dSDan Williams 	isci_host_change_state(ihost, isci_stopping);
13256f231ddaSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++) {
1326e531381eSDan Williams 		struct isci_port *iport = &ihost->ports[i];
13270cf89d1dSDan Williams 		struct isci_remote_device *idev, *d;
13280cf89d1dSDan Williams 
1329e531381eSDan Williams 		list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
13300cf89d1dSDan Williams 			isci_remote_device_change_state(idev, isci_stopping);
13316ad31fecSDan Williams 			isci_remote_device_stop(ihost, idev);
13326f231ddaSDan Williams 		}
13336f231ddaSDan Williams 	}
13346f231ddaSDan Williams 
13350cf89d1dSDan Williams 	set_bit(IHOST_STOP_PENDING, &ihost->flags);
13367c40a803SDan Williams 
13377c40a803SDan Williams 	spin_lock_irq(&ihost->scic_lock);
1338cc3dbd0aSArtur Wojcik 	scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
13397c40a803SDan Williams 	spin_unlock_irq(&ihost->scic_lock);
13407c40a803SDan Williams 
13410cf89d1dSDan Williams 	wait_for_stop(ihost);
1342cc3dbd0aSArtur Wojcik 	scic_controller_reset(&ihost->sci);
13437c40a803SDan Williams 	isci_timer_list_destroy(ihost);
13446f231ddaSDan Williams }
13456f231ddaSDan Williams 
13466f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host)
13476f231ddaSDan Williams {
13486f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13496f231ddaSDan Williams 	int id = isci_host->id;
13506f231ddaSDan Williams 
13516f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
13526f231ddaSDan Williams }
13536f231ddaSDan Williams 
13546f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host)
13556f231ddaSDan Williams {
13566f231ddaSDan Williams 	struct pci_dev *pdev = isci_host->pdev;
13576f231ddaSDan Williams 	int id = isci_host->id;
13586f231ddaSDan Williams 
13596f231ddaSDan Williams 	return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
13606f231ddaSDan Williams }
13616f231ddaSDan Williams 
1362b5f18a20SDave Jiang static void isci_user_parameters_get(
1363b5f18a20SDave Jiang 		struct isci_host *isci_host,
1364b5f18a20SDave Jiang 		union scic_user_parameters *scic_user_params)
1365b5f18a20SDave Jiang {
1366b5f18a20SDave Jiang 	struct scic_sds_user_parameters *u = &scic_user_params->sds1;
1367b5f18a20SDave Jiang 	int i;
1368b5f18a20SDave Jiang 
1369b5f18a20SDave Jiang 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1370b5f18a20SDave Jiang 		struct sci_phy_user_params *u_phy = &u->phys[i];
1371b5f18a20SDave Jiang 
1372b5f18a20SDave Jiang 		u_phy->max_speed_generation = phy_gen;
1373b5f18a20SDave Jiang 
1374b5f18a20SDave Jiang 		/* we are not exporting these for now */
1375b5f18a20SDave Jiang 		u_phy->align_insertion_frequency = 0x7f;
1376b5f18a20SDave Jiang 		u_phy->in_connection_align_insertion_frequency = 0xff;
1377b5f18a20SDave Jiang 		u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1378b5f18a20SDave Jiang 	}
1379b5f18a20SDave Jiang 
1380b5f18a20SDave Jiang 	u->stp_inactivity_timeout = stp_inactive_to;
1381b5f18a20SDave Jiang 	u->ssp_inactivity_timeout = ssp_inactive_to;
1382b5f18a20SDave Jiang 	u->stp_max_occupancy_timeout = stp_max_occ_to;
1383b5f18a20SDave Jiang 	u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1384b5f18a20SDave Jiang 	u->no_outbound_task_timeout = no_outbound_task_to;
1385b5f18a20SDave Jiang 	u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1386b5f18a20SDave Jiang }
1387b5f18a20SDave Jiang 
1388*cc9203bfSDan Williams static void scic_sds_controller_initial_state_enter(void *object)
1389*cc9203bfSDan Williams {
1390*cc9203bfSDan Williams 	struct scic_sds_controller *scic = object;
1391*cc9203bfSDan Williams 
1392*cc9203bfSDan Williams 	sci_base_state_machine_change_state(&scic->state_machine,
1393*cc9203bfSDan Williams 			SCI_BASE_CONTROLLER_STATE_RESET);
1394*cc9203bfSDan Williams }
1395*cc9203bfSDan Williams 
1396*cc9203bfSDan Williams static inline void scic_sds_controller_starting_state_exit(void *object)
1397*cc9203bfSDan Williams {
1398*cc9203bfSDan Williams 	struct scic_sds_controller *scic = object;
1399*cc9203bfSDan Williams 
1400*cc9203bfSDan Williams 	isci_timer_stop(scic->timeout_timer);
1401*cc9203bfSDan Williams }
1402*cc9203bfSDan Williams 
1403*cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1404*cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1405*cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US                    2700000
1406*cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX                        256
1407*cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN                7
1408*cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX                28
1409*cc9203bfSDan Williams 
1410*cc9203bfSDan Williams /**
1411*cc9203bfSDan Williams  * scic_controller_set_interrupt_coalescence() - This method allows the user to
1412*cc9203bfSDan Williams  *    configure the interrupt coalescence.
1413*cc9203bfSDan Williams  * @controller: This parameter represents the handle to the controller object
1414*cc9203bfSDan Williams  *    for which its interrupt coalesce register is overridden.
1415*cc9203bfSDan Williams  * @coalesce_number: Used to control the number of entries in the Completion
1416*cc9203bfSDan Williams  *    Queue before an interrupt is generated. If the number of entries exceed
1417*cc9203bfSDan Williams  *    this number, an interrupt will be generated. The valid range of the input
1418*cc9203bfSDan Williams  *    is [0, 256]. A setting of 0 results in coalescing being disabled.
1419*cc9203bfSDan Williams  * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1420*cc9203bfSDan Williams  *    input is [0, 2700000] . A setting of 0 is allowed and results in no
1421*cc9203bfSDan Williams  *    interrupt coalescing timeout.
1422*cc9203bfSDan Williams  *
1423*cc9203bfSDan Williams  * Indicate if the user successfully set the interrupt coalesce parameters.
1424*cc9203bfSDan Williams  * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1425*cc9203bfSDan Williams  * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1426*cc9203bfSDan Williams  */
1427*cc9203bfSDan Williams static enum sci_status scic_controller_set_interrupt_coalescence(
1428*cc9203bfSDan Williams 	struct scic_sds_controller *scic_controller,
1429*cc9203bfSDan Williams 	u32 coalesce_number,
1430*cc9203bfSDan Williams 	u32 coalesce_timeout)
1431*cc9203bfSDan Williams {
1432*cc9203bfSDan Williams 	u8 timeout_encode = 0;
1433*cc9203bfSDan Williams 	u32 min = 0;
1434*cc9203bfSDan Williams 	u32 max = 0;
1435*cc9203bfSDan Williams 
1436*cc9203bfSDan Williams 	/* Check if the input parameters fall in the range. */
1437*cc9203bfSDan Williams 	if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1438*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1439*cc9203bfSDan Williams 
1440*cc9203bfSDan Williams 	/*
1441*cc9203bfSDan Williams 	 *  Defined encoding for interrupt coalescing timeout:
1442*cc9203bfSDan Williams 	 *              Value   Min      Max     Units
1443*cc9203bfSDan Williams 	 *              -----   ---      ---     -----
1444*cc9203bfSDan Williams 	 *              0       -        -       Disabled
1445*cc9203bfSDan Williams 	 *              1       13.3     20.0    ns
1446*cc9203bfSDan Williams 	 *              2       26.7     40.0
1447*cc9203bfSDan Williams 	 *              3       53.3     80.0
1448*cc9203bfSDan Williams 	 *              4       106.7    160.0
1449*cc9203bfSDan Williams 	 *              5       213.3    320.0
1450*cc9203bfSDan Williams 	 *              6       426.7    640.0
1451*cc9203bfSDan Williams 	 *              7       853.3    1280.0
1452*cc9203bfSDan Williams 	 *              8       1.7      2.6     us
1453*cc9203bfSDan Williams 	 *              9       3.4      5.1
1454*cc9203bfSDan Williams 	 *              10      6.8      10.2
1455*cc9203bfSDan Williams 	 *              11      13.7     20.5
1456*cc9203bfSDan Williams 	 *              12      27.3     41.0
1457*cc9203bfSDan Williams 	 *              13      54.6     81.9
1458*cc9203bfSDan Williams 	 *              14      109.2    163.8
1459*cc9203bfSDan Williams 	 *              15      218.5    327.7
1460*cc9203bfSDan Williams 	 *              16      436.9    655.4
1461*cc9203bfSDan Williams 	 *              17      873.8    1310.7
1462*cc9203bfSDan Williams 	 *              18      1.7      2.6     ms
1463*cc9203bfSDan Williams 	 *              19      3.5      5.2
1464*cc9203bfSDan Williams 	 *              20      7.0      10.5
1465*cc9203bfSDan Williams 	 *              21      14.0     21.0
1466*cc9203bfSDan Williams 	 *              22      28.0     41.9
1467*cc9203bfSDan Williams 	 *              23      55.9     83.9
1468*cc9203bfSDan Williams 	 *              24      111.8    167.8
1469*cc9203bfSDan Williams 	 *              25      223.7    335.5
1470*cc9203bfSDan Williams 	 *              26      447.4    671.1
1471*cc9203bfSDan Williams 	 *              27      894.8    1342.2
1472*cc9203bfSDan Williams 	 *              28      1.8      2.7     s
1473*cc9203bfSDan Williams 	 *              Others Undefined */
1474*cc9203bfSDan Williams 
1475*cc9203bfSDan Williams 	/*
1476*cc9203bfSDan Williams 	 * Use the table above to decide the encode of interrupt coalescing timeout
1477*cc9203bfSDan Williams 	 * value for register writing. */
1478*cc9203bfSDan Williams 	if (coalesce_timeout == 0)
1479*cc9203bfSDan Williams 		timeout_encode = 0;
1480*cc9203bfSDan Williams 	else{
1481*cc9203bfSDan Williams 		/* make the timeout value in unit of (10 ns). */
1482*cc9203bfSDan Williams 		coalesce_timeout = coalesce_timeout * 100;
1483*cc9203bfSDan Williams 		min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1484*cc9203bfSDan Williams 		max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1485*cc9203bfSDan Williams 
1486*cc9203bfSDan Williams 		/* get the encode of timeout for register writing. */
1487*cc9203bfSDan Williams 		for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1488*cc9203bfSDan Williams 		      timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1489*cc9203bfSDan Williams 		      timeout_encode++) {
1490*cc9203bfSDan Williams 			if (min <= coalesce_timeout &&  max > coalesce_timeout)
1491*cc9203bfSDan Williams 				break;
1492*cc9203bfSDan Williams 			else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1493*cc9203bfSDan Williams 				 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1494*cc9203bfSDan Williams 				if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1495*cc9203bfSDan Williams 					break;
1496*cc9203bfSDan Williams 				else{
1497*cc9203bfSDan Williams 					timeout_encode++;
1498*cc9203bfSDan Williams 					break;
1499*cc9203bfSDan Williams 				}
1500*cc9203bfSDan Williams 			} else {
1501*cc9203bfSDan Williams 				max = max * 2;
1502*cc9203bfSDan Williams 				min = min * 2;
1503*cc9203bfSDan Williams 			}
1504*cc9203bfSDan Williams 		}
1505*cc9203bfSDan Williams 
1506*cc9203bfSDan Williams 		if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1507*cc9203bfSDan Williams 			/* the value is out of range. */
1508*cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1509*cc9203bfSDan Williams 	}
1510*cc9203bfSDan Williams 
1511*cc9203bfSDan Williams 	writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1512*cc9203bfSDan Williams 	       SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1513*cc9203bfSDan Williams 	       &scic_controller->smu_registers->interrupt_coalesce_control);
1514*cc9203bfSDan Williams 
1515*cc9203bfSDan Williams 
1516*cc9203bfSDan Williams 	scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
1517*cc9203bfSDan Williams 	scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
1518*cc9203bfSDan Williams 
1519*cc9203bfSDan Williams 	return SCI_SUCCESS;
1520*cc9203bfSDan Williams }
1521*cc9203bfSDan Williams 
1522*cc9203bfSDan Williams 
1523*cc9203bfSDan Williams static void scic_sds_controller_ready_state_enter(void *object)
1524*cc9203bfSDan Williams {
1525*cc9203bfSDan Williams 	struct scic_sds_controller *scic = object;
1526*cc9203bfSDan Williams 
1527*cc9203bfSDan Williams 	/* set the default interrupt coalescence number and timeout value. */
1528*cc9203bfSDan Williams 	scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
1529*cc9203bfSDan Williams }
1530*cc9203bfSDan Williams 
1531*cc9203bfSDan Williams static void scic_sds_controller_ready_state_exit(void *object)
1532*cc9203bfSDan Williams {
1533*cc9203bfSDan Williams 	struct scic_sds_controller *scic = object;
1534*cc9203bfSDan Williams 
1535*cc9203bfSDan Williams 	/* disable interrupt coalescence. */
1536*cc9203bfSDan Williams 	scic_controller_set_interrupt_coalescence(scic, 0, 0);
1537*cc9203bfSDan Williams }
1538*cc9203bfSDan Williams 
1539*cc9203bfSDan Williams static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
1540*cc9203bfSDan Williams {
1541*cc9203bfSDan Williams 	u32 index;
1542*cc9203bfSDan Williams 	enum sci_status status;
1543*cc9203bfSDan Williams 	enum sci_status phy_status;
1544*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1545*cc9203bfSDan Williams 
1546*cc9203bfSDan Williams 	status = SCI_SUCCESS;
1547*cc9203bfSDan Williams 
1548*cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1549*cc9203bfSDan Williams 		phy_status = scic_sds_phy_stop(&ihost->phys[index].sci);
1550*cc9203bfSDan Williams 
1551*cc9203bfSDan Williams 		if (phy_status != SCI_SUCCESS &&
1552*cc9203bfSDan Williams 		    phy_status != SCI_FAILURE_INVALID_STATE) {
1553*cc9203bfSDan Williams 			status = SCI_FAILURE;
1554*cc9203bfSDan Williams 
1555*cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
1556*cc9203bfSDan Williams 				 "%s: Controller stop operation failed to stop "
1557*cc9203bfSDan Williams 				 "phy %d because of status %d.\n",
1558*cc9203bfSDan Williams 				 __func__,
1559*cc9203bfSDan Williams 				 ihost->phys[index].sci.phy_index, phy_status);
1560*cc9203bfSDan Williams 		}
1561*cc9203bfSDan Williams 	}
1562*cc9203bfSDan Williams 
1563*cc9203bfSDan Williams 	return status;
1564*cc9203bfSDan Williams }
1565*cc9203bfSDan Williams 
1566*cc9203bfSDan Williams static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
1567*cc9203bfSDan Williams {
1568*cc9203bfSDan Williams 	u32 index;
1569*cc9203bfSDan Williams 	enum sci_status port_status;
1570*cc9203bfSDan Williams 	enum sci_status status = SCI_SUCCESS;
1571*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1572*cc9203bfSDan Williams 
1573*cc9203bfSDan Williams 	for (index = 0; index < scic->logical_port_entries; index++) {
1574*cc9203bfSDan Williams 		struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1575*cc9203bfSDan Williams 		scic_sds_port_handler_t stop;
1576*cc9203bfSDan Williams 
1577*cc9203bfSDan Williams 		stop = sci_port->state_handlers->stop_handler;
1578*cc9203bfSDan Williams 		port_status = stop(sci_port);
1579*cc9203bfSDan Williams 
1580*cc9203bfSDan Williams 		if ((port_status != SCI_SUCCESS) &&
1581*cc9203bfSDan Williams 		    (port_status != SCI_FAILURE_INVALID_STATE)) {
1582*cc9203bfSDan Williams 			status = SCI_FAILURE;
1583*cc9203bfSDan Williams 
1584*cc9203bfSDan Williams 			dev_warn(scic_to_dev(scic),
1585*cc9203bfSDan Williams 				 "%s: Controller stop operation failed to "
1586*cc9203bfSDan Williams 				 "stop port %d because of status %d.\n",
1587*cc9203bfSDan Williams 				 __func__,
1588*cc9203bfSDan Williams 				 sci_port->logical_port_index,
1589*cc9203bfSDan Williams 				 port_status);
1590*cc9203bfSDan Williams 		}
1591*cc9203bfSDan Williams 	}
1592*cc9203bfSDan Williams 
1593*cc9203bfSDan Williams 	return status;
1594*cc9203bfSDan Williams }
1595*cc9203bfSDan Williams 
1596*cc9203bfSDan Williams static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
1597*cc9203bfSDan Williams {
1598*cc9203bfSDan Williams 	u32 index;
1599*cc9203bfSDan Williams 	enum sci_status status;
1600*cc9203bfSDan Williams 	enum sci_status device_status;
1601*cc9203bfSDan Williams 
1602*cc9203bfSDan Williams 	status = SCI_SUCCESS;
1603*cc9203bfSDan Williams 
1604*cc9203bfSDan Williams 	for (index = 0; index < scic->remote_node_entries; index++) {
1605*cc9203bfSDan Williams 		if (scic->device_table[index] != NULL) {
1606*cc9203bfSDan Williams 			/* / @todo What timeout value do we want to provide to this request? */
1607*cc9203bfSDan Williams 			device_status = scic_remote_device_stop(scic->device_table[index], 0);
1608*cc9203bfSDan Williams 
1609*cc9203bfSDan Williams 			if ((device_status != SCI_SUCCESS) &&
1610*cc9203bfSDan Williams 			    (device_status != SCI_FAILURE_INVALID_STATE)) {
1611*cc9203bfSDan Williams 				dev_warn(scic_to_dev(scic),
1612*cc9203bfSDan Williams 					 "%s: Controller stop operation failed "
1613*cc9203bfSDan Williams 					 "to stop device 0x%p because of "
1614*cc9203bfSDan Williams 					 "status %d.\n",
1615*cc9203bfSDan Williams 					 __func__,
1616*cc9203bfSDan Williams 					 scic->device_table[index], device_status);
1617*cc9203bfSDan Williams 			}
1618*cc9203bfSDan Williams 		}
1619*cc9203bfSDan Williams 	}
1620*cc9203bfSDan Williams 
1621*cc9203bfSDan Williams 	return status;
1622*cc9203bfSDan Williams }
1623*cc9203bfSDan Williams 
1624*cc9203bfSDan Williams static void scic_sds_controller_stopping_state_enter(void *object)
1625*cc9203bfSDan Williams {
1626*cc9203bfSDan Williams 	struct scic_sds_controller *scic = object;
1627*cc9203bfSDan Williams 
1628*cc9203bfSDan Williams 	/* Stop all of the components for this controller */
1629*cc9203bfSDan Williams 	scic_sds_controller_stop_phys(scic);
1630*cc9203bfSDan Williams 	scic_sds_controller_stop_ports(scic);
1631*cc9203bfSDan Williams 	scic_sds_controller_stop_devices(scic);
1632*cc9203bfSDan Williams }
1633*cc9203bfSDan Williams 
1634*cc9203bfSDan Williams static void scic_sds_controller_stopping_state_exit(void *object)
1635*cc9203bfSDan Williams {
1636*cc9203bfSDan Williams 	struct scic_sds_controller *scic = object;
1637*cc9203bfSDan Williams 
1638*cc9203bfSDan Williams 	isci_timer_stop(scic->timeout_timer);
1639*cc9203bfSDan Williams }
1640*cc9203bfSDan Williams 
1641*cc9203bfSDan Williams 
1642*cc9203bfSDan Williams /**
1643*cc9203bfSDan Williams  * scic_sds_controller_reset_hardware() -
1644*cc9203bfSDan Williams  *
1645*cc9203bfSDan Williams  * This method will reset the controller hardware.
1646*cc9203bfSDan Williams  */
1647*cc9203bfSDan Williams static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
1648*cc9203bfSDan Williams {
1649*cc9203bfSDan Williams 	/* Disable interrupts so we dont take any spurious interrupts */
1650*cc9203bfSDan Williams 	scic_controller_disable_interrupts(scic);
1651*cc9203bfSDan Williams 
1652*cc9203bfSDan Williams 	/* Reset the SCU */
1653*cc9203bfSDan Williams 	writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
1654*cc9203bfSDan Williams 
1655*cc9203bfSDan Williams 	/* Delay for 1ms to before clearing the CQP and UFQPR. */
1656*cc9203bfSDan Williams 	udelay(1000);
1657*cc9203bfSDan Williams 
1658*cc9203bfSDan Williams 	/* The write to the CQGR clears the CQP */
1659*cc9203bfSDan Williams 	writel(0x00000000, &scic->smu_registers->completion_queue_get);
1660*cc9203bfSDan Williams 
1661*cc9203bfSDan Williams 	/* The write to the UFQGP clears the UFQPR */
1662*cc9203bfSDan Williams 	writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
1663*cc9203bfSDan Williams }
1664*cc9203bfSDan Williams 
1665*cc9203bfSDan Williams static void scic_sds_controller_resetting_state_enter(void *object)
1666*cc9203bfSDan Williams {
1667*cc9203bfSDan Williams 	struct scic_sds_controller *scic = object;
1668*cc9203bfSDan Williams 
1669*cc9203bfSDan Williams 	scic_sds_controller_reset_hardware(scic);
1670*cc9203bfSDan Williams 	sci_base_state_machine_change_state(&scic->state_machine,
1671*cc9203bfSDan Williams 					    SCI_BASE_CONTROLLER_STATE_RESET);
1672*cc9203bfSDan Williams }
1673*cc9203bfSDan Williams 
1674*cc9203bfSDan Williams static const struct sci_base_state scic_sds_controller_state_table[] = {
1675*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_INITIAL] = {
1676*cc9203bfSDan Williams 		.enter_state = scic_sds_controller_initial_state_enter,
1677*cc9203bfSDan Williams 	},
1678*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_RESET] = {},
1679*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_INITIALIZING] = {},
1680*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_INITIALIZED] = {},
1681*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_STARTING] = {
1682*cc9203bfSDan Williams 		.exit_state  = scic_sds_controller_starting_state_exit,
1683*cc9203bfSDan Williams 	},
1684*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_READY] = {
1685*cc9203bfSDan Williams 		.enter_state = scic_sds_controller_ready_state_enter,
1686*cc9203bfSDan Williams 		.exit_state  = scic_sds_controller_ready_state_exit,
1687*cc9203bfSDan Williams 	},
1688*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_RESETTING] = {
1689*cc9203bfSDan Williams 		.enter_state = scic_sds_controller_resetting_state_enter,
1690*cc9203bfSDan Williams 	},
1691*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_STOPPING] = {
1692*cc9203bfSDan Williams 		.enter_state = scic_sds_controller_stopping_state_enter,
1693*cc9203bfSDan Williams 		.exit_state = scic_sds_controller_stopping_state_exit,
1694*cc9203bfSDan Williams 	},
1695*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_STOPPED] = {},
1696*cc9203bfSDan Williams 	[SCI_BASE_CONTROLLER_STATE_FAILED] = {}
1697*cc9203bfSDan Williams };
1698*cc9203bfSDan Williams 
1699*cc9203bfSDan Williams static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
1700*cc9203bfSDan Williams {
1701*cc9203bfSDan Williams 	/* these defaults are overridden by the platform / firmware */
1702*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1703*cc9203bfSDan Williams 	u16 index;
1704*cc9203bfSDan Williams 
1705*cc9203bfSDan Williams 	/* Default to APC mode. */
1706*cc9203bfSDan Williams 	scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1707*cc9203bfSDan Williams 
1708*cc9203bfSDan Williams 	/* Default to APC mode. */
1709*cc9203bfSDan Williams 	scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
1710*cc9203bfSDan Williams 
1711*cc9203bfSDan Williams 	/* Default to no SSC operation. */
1712*cc9203bfSDan Williams 	scic->oem_parameters.sds1.controller.do_enable_ssc = false;
1713*cc9203bfSDan Williams 
1714*cc9203bfSDan Williams 	/* Initialize all of the port parameter information to narrow ports. */
1715*cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PORTS; index++) {
1716*cc9203bfSDan Williams 		scic->oem_parameters.sds1.ports[index].phy_mask = 0;
1717*cc9203bfSDan Williams 	}
1718*cc9203bfSDan Williams 
1719*cc9203bfSDan Williams 	/* Initialize all of the phy parameter information. */
1720*cc9203bfSDan Williams 	for (index = 0; index < SCI_MAX_PHYS; index++) {
1721*cc9203bfSDan Williams 		/* Default to 6G (i.e. Gen 3) for now. */
1722*cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
1723*cc9203bfSDan Williams 
1724*cc9203bfSDan Williams 		/* the frequencies cannot be 0 */
1725*cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
1726*cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
1727*cc9203bfSDan Williams 		scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1728*cc9203bfSDan Williams 
1729*cc9203bfSDan Williams 		/*
1730*cc9203bfSDan Williams 		 * Previous Vitesse based expanders had a arbitration issue that
1731*cc9203bfSDan Williams 		 * is worked around by having the upper 32-bits of SAS address
1732*cc9203bfSDan Williams 		 * with a value greater then the Vitesse company identifier.
1733*cc9203bfSDan Williams 		 * Hence, usage of 0x5FCFFFFF. */
1734*cc9203bfSDan Williams 		scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
1735*cc9203bfSDan Williams 		scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
1736*cc9203bfSDan Williams 	}
1737*cc9203bfSDan Williams 
1738*cc9203bfSDan Williams 	scic->user_parameters.sds1.stp_inactivity_timeout = 5;
1739*cc9203bfSDan Williams 	scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
1740*cc9203bfSDan Williams 	scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
1741*cc9203bfSDan Williams 	scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
1742*cc9203bfSDan Williams 	scic->user_parameters.sds1.no_outbound_task_timeout = 20;
1743*cc9203bfSDan Williams }
1744*cc9203bfSDan Williams 
1745*cc9203bfSDan Williams 
1746*cc9203bfSDan Williams 
1747*cc9203bfSDan Williams /**
1748*cc9203bfSDan Williams  * scic_controller_construct() - This method will attempt to construct a
1749*cc9203bfSDan Williams  *    controller object utilizing the supplied parameter information.
1750*cc9203bfSDan Williams  * @c: This parameter specifies the controller to be constructed.
1751*cc9203bfSDan Williams  * @scu_base: mapped base address of the scu registers
1752*cc9203bfSDan Williams  * @smu_base: mapped base address of the smu registers
1753*cc9203bfSDan Williams  *
1754*cc9203bfSDan Williams  * Indicate if the controller was successfully constructed or if it failed in
1755*cc9203bfSDan Williams  * some way. SCI_SUCCESS This value is returned if the controller was
1756*cc9203bfSDan Williams  * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
1757*cc9203bfSDan Williams  * if the interrupt coalescence timer may cause SAS compliance issues for SMP
1758*cc9203bfSDan Williams  * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
1759*cc9203bfSDan Williams  * This value is returned if the controller does not support the supplied type.
1760*cc9203bfSDan Williams  * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
1761*cc9203bfSDan Williams  * controller does not support the supplied initialization data version.
1762*cc9203bfSDan Williams  */
1763*cc9203bfSDan Williams static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
1764*cc9203bfSDan Williams 					  void __iomem *scu_base,
1765*cc9203bfSDan Williams 					  void __iomem *smu_base)
1766*cc9203bfSDan Williams {
1767*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1768*cc9203bfSDan Williams 	u8 i;
1769*cc9203bfSDan Williams 
1770*cc9203bfSDan Williams 	sci_base_state_machine_construct(&scic->state_machine,
1771*cc9203bfSDan Williams 		scic, scic_sds_controller_state_table,
1772*cc9203bfSDan Williams 		SCI_BASE_CONTROLLER_STATE_INITIAL);
1773*cc9203bfSDan Williams 
1774*cc9203bfSDan Williams 	sci_base_state_machine_start(&scic->state_machine);
1775*cc9203bfSDan Williams 
1776*cc9203bfSDan Williams 	scic->scu_registers = scu_base;
1777*cc9203bfSDan Williams 	scic->smu_registers = smu_base;
1778*cc9203bfSDan Williams 
1779*cc9203bfSDan Williams 	scic_sds_port_configuration_agent_construct(&scic->port_agent);
1780*cc9203bfSDan Williams 
1781*cc9203bfSDan Williams 	/* Construct the ports for this controller */
1782*cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1783*cc9203bfSDan Williams 		scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
1784*cc9203bfSDan Williams 	scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
1785*cc9203bfSDan Williams 
1786*cc9203bfSDan Williams 	/* Construct the phys for this controller */
1787*cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++) {
1788*cc9203bfSDan Williams 		/* Add all the PHYs to the dummy port */
1789*cc9203bfSDan Williams 		scic_sds_phy_construct(&ihost->phys[i].sci,
1790*cc9203bfSDan Williams 				       &ihost->ports[SCI_MAX_PORTS].sci, i);
1791*cc9203bfSDan Williams 	}
1792*cc9203bfSDan Williams 
1793*cc9203bfSDan Williams 	scic->invalid_phy_mask = 0;
1794*cc9203bfSDan Williams 
1795*cc9203bfSDan Williams 	/* Set the default maximum values */
1796*cc9203bfSDan Williams 	scic->completion_event_entries      = SCU_EVENT_COUNT;
1797*cc9203bfSDan Williams 	scic->completion_queue_entries      = SCU_COMPLETION_QUEUE_COUNT;
1798*cc9203bfSDan Williams 	scic->remote_node_entries           = SCI_MAX_REMOTE_DEVICES;
1799*cc9203bfSDan Williams 	scic->logical_port_entries          = SCI_MAX_PORTS;
1800*cc9203bfSDan Williams 	scic->task_context_entries          = SCU_IO_REQUEST_COUNT;
1801*cc9203bfSDan Williams 	scic->uf_control.buffers.count      = SCU_UNSOLICITED_FRAME_COUNT;
1802*cc9203bfSDan Williams 	scic->uf_control.address_table.count = SCU_UNSOLICITED_FRAME_COUNT;
1803*cc9203bfSDan Williams 
1804*cc9203bfSDan Williams 	/* Initialize the User and OEM parameters to default values. */
1805*cc9203bfSDan Williams 	scic_sds_controller_set_default_config_parameters(scic);
1806*cc9203bfSDan Williams 
1807*cc9203bfSDan Williams 	return scic_controller_reset(scic);
1808*cc9203bfSDan Williams }
1809*cc9203bfSDan Williams 
1810*cc9203bfSDan Williams int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
1811*cc9203bfSDan Williams {
1812*cc9203bfSDan Williams 	int i;
1813*cc9203bfSDan Williams 
1814*cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
1815*cc9203bfSDan Williams 		if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1816*cc9203bfSDan Williams 			return -EINVAL;
1817*cc9203bfSDan Williams 
1818*cc9203bfSDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
1819*cc9203bfSDan Williams 		if (oem->phys[i].sas_address.high == 0 &&
1820*cc9203bfSDan Williams 		    oem->phys[i].sas_address.low == 0)
1821*cc9203bfSDan Williams 			return -EINVAL;
1822*cc9203bfSDan Williams 
1823*cc9203bfSDan Williams 	if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1824*cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1825*cc9203bfSDan Williams 			if (oem->ports[i].phy_mask != 0)
1826*cc9203bfSDan Williams 				return -EINVAL;
1827*cc9203bfSDan Williams 	} else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1828*cc9203bfSDan Williams 		u8 phy_mask = 0;
1829*cc9203bfSDan Williams 
1830*cc9203bfSDan Williams 		for (i = 0; i < SCI_MAX_PHYS; i++)
1831*cc9203bfSDan Williams 			phy_mask |= oem->ports[i].phy_mask;
1832*cc9203bfSDan Williams 
1833*cc9203bfSDan Williams 		if (phy_mask == 0)
1834*cc9203bfSDan Williams 			return -EINVAL;
1835*cc9203bfSDan Williams 	} else
1836*cc9203bfSDan Williams 		return -EINVAL;
1837*cc9203bfSDan Williams 
1838*cc9203bfSDan Williams 	if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1839*cc9203bfSDan Williams 		return -EINVAL;
1840*cc9203bfSDan Williams 
1841*cc9203bfSDan Williams 	return 0;
1842*cc9203bfSDan Williams }
1843*cc9203bfSDan Williams 
1844*cc9203bfSDan Williams static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
1845*cc9203bfSDan Williams 					union scic_oem_parameters *scic_parms)
1846*cc9203bfSDan Williams {
1847*cc9203bfSDan Williams 	u32 state = scic->state_machine.current_state_id;
1848*cc9203bfSDan Williams 
1849*cc9203bfSDan Williams 	if (state == SCI_BASE_CONTROLLER_STATE_RESET ||
1850*cc9203bfSDan Williams 	    state == SCI_BASE_CONTROLLER_STATE_INITIALIZING ||
1851*cc9203bfSDan Williams 	    state == SCI_BASE_CONTROLLER_STATE_INITIALIZED) {
1852*cc9203bfSDan Williams 
1853*cc9203bfSDan Williams 		if (scic_oem_parameters_validate(&scic_parms->sds1))
1854*cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1855*cc9203bfSDan Williams 		scic->oem_parameters.sds1 = scic_parms->sds1;
1856*cc9203bfSDan Williams 
1857*cc9203bfSDan Williams 		return SCI_SUCCESS;
1858*cc9203bfSDan Williams 	}
1859*cc9203bfSDan Williams 
1860*cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
1861*cc9203bfSDan Williams }
1862*cc9203bfSDan Williams 
1863*cc9203bfSDan Williams void scic_oem_parameters_get(
1864*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
1865*cc9203bfSDan Williams 	union scic_oem_parameters *scic_parms)
1866*cc9203bfSDan Williams {
1867*cc9203bfSDan Williams 	memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
1868*cc9203bfSDan Williams }
1869*cc9203bfSDan Williams 
1870*cc9203bfSDan Williams static void scic_sds_controller_timeout_handler(void *_scic)
1871*cc9203bfSDan Williams {
1872*cc9203bfSDan Williams 	struct scic_sds_controller *scic = _scic;
1873*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1874*cc9203bfSDan Williams 	struct sci_base_state_machine *sm = &scic->state_machine;
1875*cc9203bfSDan Williams 
1876*cc9203bfSDan Williams 	if (sm->current_state_id == SCI_BASE_CONTROLLER_STATE_STARTING)
1877*cc9203bfSDan Williams 		scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
1878*cc9203bfSDan Williams 	else if (sm->current_state_id == SCI_BASE_CONTROLLER_STATE_STOPPING) {
1879*cc9203bfSDan Williams 		sci_base_state_machine_change_state(sm, SCI_BASE_CONTROLLER_STATE_FAILED);
1880*cc9203bfSDan Williams 		isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
1881*cc9203bfSDan Williams 	} else	/* / @todo Now what do we want to do in this case? */
1882*cc9203bfSDan Williams 		dev_err(scic_to_dev(scic),
1883*cc9203bfSDan Williams 			"%s: Controller timer fired when controller was not "
1884*cc9203bfSDan Williams 			"in a state being timed.\n",
1885*cc9203bfSDan Williams 			__func__);
1886*cc9203bfSDan Williams }
1887*cc9203bfSDan Williams 
1888*cc9203bfSDan Williams static enum sci_status scic_sds_controller_initialize_phy_startup(struct scic_sds_controller *scic)
1889*cc9203bfSDan Williams {
1890*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
1891*cc9203bfSDan Williams 
1892*cc9203bfSDan Williams 	scic->phy_startup_timer = isci_timer_create(ihost,
1893*cc9203bfSDan Williams 						    scic,
1894*cc9203bfSDan Williams 						    scic_sds_controller_phy_startup_timeout_handler);
1895*cc9203bfSDan Williams 
1896*cc9203bfSDan Williams 	if (scic->phy_startup_timer == NULL)
1897*cc9203bfSDan Williams 		return SCI_FAILURE_INSUFFICIENT_RESOURCES;
1898*cc9203bfSDan Williams 	else {
1899*cc9203bfSDan Williams 		scic->next_phy_to_start = 0;
1900*cc9203bfSDan Williams 		scic->phy_startup_timer_pending = false;
1901*cc9203bfSDan Williams 	}
1902*cc9203bfSDan Williams 
1903*cc9203bfSDan Williams 	return SCI_SUCCESS;
1904*cc9203bfSDan Williams }
1905*cc9203bfSDan Williams 
1906*cc9203bfSDan Williams static void scic_sds_controller_power_control_timer_start(struct scic_sds_controller *scic)
1907*cc9203bfSDan Williams {
1908*cc9203bfSDan Williams 	isci_timer_start(scic->power_control.timer,
1909*cc9203bfSDan Williams 			 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1910*cc9203bfSDan Williams 
1911*cc9203bfSDan Williams 	scic->power_control.timer_started = true;
1912*cc9203bfSDan Williams }
1913*cc9203bfSDan Williams 
1914*cc9203bfSDan Williams static void scic_sds_controller_power_control_timer_stop(struct scic_sds_controller *scic)
1915*cc9203bfSDan Williams {
1916*cc9203bfSDan Williams 	if (scic->power_control.timer_started) {
1917*cc9203bfSDan Williams 		isci_timer_stop(scic->power_control.timer);
1918*cc9203bfSDan Williams 		scic->power_control.timer_started = false;
1919*cc9203bfSDan Williams 	}
1920*cc9203bfSDan Williams }
1921*cc9203bfSDan Williams 
1922*cc9203bfSDan Williams static void scic_sds_controller_power_control_timer_restart(struct scic_sds_controller *scic)
1923*cc9203bfSDan Williams {
1924*cc9203bfSDan Williams 	scic_sds_controller_power_control_timer_stop(scic);
1925*cc9203bfSDan Williams 	scic_sds_controller_power_control_timer_start(scic);
1926*cc9203bfSDan Williams }
1927*cc9203bfSDan Williams 
1928*cc9203bfSDan Williams static void scic_sds_controller_power_control_timer_handler(
1929*cc9203bfSDan Williams 	void *controller)
1930*cc9203bfSDan Williams {
1931*cc9203bfSDan Williams 	struct scic_sds_controller *scic;
1932*cc9203bfSDan Williams 
1933*cc9203bfSDan Williams 	scic = (struct scic_sds_controller *)controller;
1934*cc9203bfSDan Williams 
1935*cc9203bfSDan Williams 	scic->power_control.phys_granted_power = 0;
1936*cc9203bfSDan Williams 
1937*cc9203bfSDan Williams 	if (scic->power_control.phys_waiting == 0) {
1938*cc9203bfSDan Williams 		scic->power_control.timer_started = false;
1939*cc9203bfSDan Williams 	} else {
1940*cc9203bfSDan Williams 		struct scic_sds_phy *sci_phy = NULL;
1941*cc9203bfSDan Williams 		u8 i;
1942*cc9203bfSDan Williams 
1943*cc9203bfSDan Williams 		for (i = 0;
1944*cc9203bfSDan Williams 		     (i < SCI_MAX_PHYS)
1945*cc9203bfSDan Williams 		     && (scic->power_control.phys_waiting != 0);
1946*cc9203bfSDan Williams 		     i++) {
1947*cc9203bfSDan Williams 			if (scic->power_control.requesters[i] != NULL) {
1948*cc9203bfSDan Williams 				if (scic->power_control.phys_granted_power <
1949*cc9203bfSDan Williams 				    scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
1950*cc9203bfSDan Williams 					sci_phy = scic->power_control.requesters[i];
1951*cc9203bfSDan Williams 					scic->power_control.requesters[i] = NULL;
1952*cc9203bfSDan Williams 					scic->power_control.phys_waiting--;
1953*cc9203bfSDan Williams 					scic->power_control.phys_granted_power++;
1954*cc9203bfSDan Williams 					scic_sds_phy_consume_power_handler(sci_phy);
1955*cc9203bfSDan Williams 				} else {
1956*cc9203bfSDan Williams 					break;
1957*cc9203bfSDan Williams 				}
1958*cc9203bfSDan Williams 			}
1959*cc9203bfSDan Williams 		}
1960*cc9203bfSDan Williams 
1961*cc9203bfSDan Williams 		/*
1962*cc9203bfSDan Williams 		 * It doesn't matter if the power list is empty, we need to start the
1963*cc9203bfSDan Williams 		 * timer in case another phy becomes ready.
1964*cc9203bfSDan Williams 		 */
1965*cc9203bfSDan Williams 		scic_sds_controller_power_control_timer_start(scic);
1966*cc9203bfSDan Williams 	}
1967*cc9203bfSDan Williams }
1968*cc9203bfSDan Williams 
1969*cc9203bfSDan Williams /**
1970*cc9203bfSDan Williams  * This method inserts the phy in the stagger spinup control queue.
1971*cc9203bfSDan Williams  * @scic:
1972*cc9203bfSDan Williams  *
1973*cc9203bfSDan Williams  *
1974*cc9203bfSDan Williams  */
1975*cc9203bfSDan Williams void scic_sds_controller_power_control_queue_insert(
1976*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
1977*cc9203bfSDan Williams 	struct scic_sds_phy *sci_phy)
1978*cc9203bfSDan Williams {
1979*cc9203bfSDan Williams 	BUG_ON(sci_phy == NULL);
1980*cc9203bfSDan Williams 
1981*cc9203bfSDan Williams 	if (scic->power_control.phys_granted_power <
1982*cc9203bfSDan Williams 	    scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
1983*cc9203bfSDan Williams 		scic->power_control.phys_granted_power++;
1984*cc9203bfSDan Williams 		scic_sds_phy_consume_power_handler(sci_phy);
1985*cc9203bfSDan Williams 
1986*cc9203bfSDan Williams 		/*
1987*cc9203bfSDan Williams 		 * stop and start the power_control timer. When the timer fires, the
1988*cc9203bfSDan Williams 		 * no_of_phys_granted_power will be set to 0
1989*cc9203bfSDan Williams 		 */
1990*cc9203bfSDan Williams 		scic_sds_controller_power_control_timer_restart(scic);
1991*cc9203bfSDan Williams 	} else {
1992*cc9203bfSDan Williams 		/* Add the phy in the waiting list */
1993*cc9203bfSDan Williams 		scic->power_control.requesters[sci_phy->phy_index] = sci_phy;
1994*cc9203bfSDan Williams 		scic->power_control.phys_waiting++;
1995*cc9203bfSDan Williams 	}
1996*cc9203bfSDan Williams }
1997*cc9203bfSDan Williams 
1998*cc9203bfSDan Williams /**
1999*cc9203bfSDan Williams  * This method removes the phy from the stagger spinup control queue.
2000*cc9203bfSDan Williams  * @scic:
2001*cc9203bfSDan Williams  *
2002*cc9203bfSDan Williams  *
2003*cc9203bfSDan Williams  */
2004*cc9203bfSDan Williams void scic_sds_controller_power_control_queue_remove(
2005*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2006*cc9203bfSDan Williams 	struct scic_sds_phy *sci_phy)
2007*cc9203bfSDan Williams {
2008*cc9203bfSDan Williams 	BUG_ON(sci_phy == NULL);
2009*cc9203bfSDan Williams 
2010*cc9203bfSDan Williams 	if (scic->power_control.requesters[sci_phy->phy_index] != NULL) {
2011*cc9203bfSDan Williams 		scic->power_control.phys_waiting--;
2012*cc9203bfSDan Williams 	}
2013*cc9203bfSDan Williams 
2014*cc9203bfSDan Williams 	scic->power_control.requesters[sci_phy->phy_index] = NULL;
2015*cc9203bfSDan Williams }
2016*cc9203bfSDan Williams 
2017*cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10
2018*cc9203bfSDan Williams 
2019*cc9203bfSDan Williams /* Initialize the AFE for this phy index. We need to read the AFE setup from
2020*cc9203bfSDan Williams  * the OEM parameters
2021*cc9203bfSDan Williams  */
2022*cc9203bfSDan Williams static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
2023*cc9203bfSDan Williams {
2024*cc9203bfSDan Williams 	const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
2025*cc9203bfSDan Williams 	u32 afe_status;
2026*cc9203bfSDan Williams 	u32 phy_id;
2027*cc9203bfSDan Williams 
2028*cc9203bfSDan Williams 	/* Clear DFX Status registers */
2029*cc9203bfSDan Williams 	writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
2030*cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2031*cc9203bfSDan Williams 
2032*cc9203bfSDan Williams 	if (is_b0()) {
2033*cc9203bfSDan Williams 		/* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
2034*cc9203bfSDan Williams 		 * Timer, PM Stagger Timer */
2035*cc9203bfSDan Williams 		writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
2036*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2037*cc9203bfSDan Williams 	}
2038*cc9203bfSDan Williams 
2039*cc9203bfSDan Williams 	/* Configure bias currents to normal */
2040*cc9203bfSDan Williams 	if (is_a0())
2041*cc9203bfSDan Williams 		writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
2042*cc9203bfSDan Williams 	else if (is_a2())
2043*cc9203bfSDan Williams 		writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
2044*cc9203bfSDan Williams 	else if (is_b0())
2045*cc9203bfSDan Williams 		writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
2046*cc9203bfSDan Williams 
2047*cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2048*cc9203bfSDan Williams 
2049*cc9203bfSDan Williams 	/* Enable PLL */
2050*cc9203bfSDan Williams 	if (is_b0())
2051*cc9203bfSDan Williams 		writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
2052*cc9203bfSDan Williams 	else
2053*cc9203bfSDan Williams 		writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
2054*cc9203bfSDan Williams 
2055*cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2056*cc9203bfSDan Williams 
2057*cc9203bfSDan Williams 	/* Wait for the PLL to lock */
2058*cc9203bfSDan Williams 	do {
2059*cc9203bfSDan Williams 		afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
2060*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2061*cc9203bfSDan Williams 	} while ((afe_status & 0x00001000) == 0);
2062*cc9203bfSDan Williams 
2063*cc9203bfSDan Williams 	if (is_a0() || is_a2()) {
2064*cc9203bfSDan Williams 		/* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
2065*cc9203bfSDan Williams 		writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
2066*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2067*cc9203bfSDan Williams 	}
2068*cc9203bfSDan Williams 
2069*cc9203bfSDan Williams 	for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
2070*cc9203bfSDan Williams 		const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2071*cc9203bfSDan Williams 
2072*cc9203bfSDan Williams 		if (is_b0()) {
2073*cc9203bfSDan Williams 			 /* Configure transmitter SSC parameters */
2074*cc9203bfSDan Williams 			writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2075*cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2076*cc9203bfSDan Williams 		} else {
2077*cc9203bfSDan Williams 			/*
2078*cc9203bfSDan Williams 			 * All defaults, except the Receive Word Alignament/Comma Detect
2079*cc9203bfSDan Williams 			 * Enable....(0xe800) */
2080*cc9203bfSDan Williams 			writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2081*cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2082*cc9203bfSDan Williams 
2083*cc9203bfSDan Williams 			writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
2084*cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2085*cc9203bfSDan Williams 		}
2086*cc9203bfSDan Williams 
2087*cc9203bfSDan Williams 		/*
2088*cc9203bfSDan Williams 		 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2089*cc9203bfSDan Williams 		 * & increase TX int & ext bias 20%....(0xe85c) */
2090*cc9203bfSDan Williams 		if (is_a0())
2091*cc9203bfSDan Williams 			writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2092*cc9203bfSDan Williams 		else if (is_a2())
2093*cc9203bfSDan Williams 			writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2094*cc9203bfSDan Williams 		else {
2095*cc9203bfSDan Williams 			 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
2096*cc9203bfSDan Williams 			writel(0x000003d7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2097*cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2098*cc9203bfSDan Williams 
2099*cc9203bfSDan Williams 			/*
2100*cc9203bfSDan Williams 			 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2101*cc9203bfSDan Williams 			 * & increase TX int & ext bias 20%....(0xe85c) */
2102*cc9203bfSDan Williams 			writel(0x000003d4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2103*cc9203bfSDan Williams 		}
2104*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2105*cc9203bfSDan Williams 
2106*cc9203bfSDan Williams 		if (is_a0() || is_a2()) {
2107*cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
2108*cc9203bfSDan Williams 			writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2109*cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2110*cc9203bfSDan Williams 		}
2111*cc9203bfSDan Williams 
2112*cc9203bfSDan Williams 		/*
2113*cc9203bfSDan Williams 		 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
2114*cc9203bfSDan Williams 		 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
2115*cc9203bfSDan Williams 		writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2116*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2117*cc9203bfSDan Williams 
2118*cc9203bfSDan Williams 		/* Leave DFE/FFE on */
2119*cc9203bfSDan Williams 		if (is_a0())
2120*cc9203bfSDan Williams 			writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2121*cc9203bfSDan Williams 		else if (is_a2())
2122*cc9203bfSDan Williams 			writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2123*cc9203bfSDan Williams 		else {
2124*cc9203bfSDan Williams 			writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2125*cc9203bfSDan Williams 			udelay(AFE_REGISTER_WRITE_DELAY);
2126*cc9203bfSDan Williams 			/* Enable TX equalization (0xe824) */
2127*cc9203bfSDan Williams 			writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2128*cc9203bfSDan Williams 		}
2129*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2130*cc9203bfSDan Williams 
2131*cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control0,
2132*cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
2133*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2134*cc9203bfSDan Williams 
2135*cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control1,
2136*cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
2137*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2138*cc9203bfSDan Williams 
2139*cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control2,
2140*cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
2141*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2142*cc9203bfSDan Williams 
2143*cc9203bfSDan Williams 		writel(oem_phy->afe_tx_amp_control3,
2144*cc9203bfSDan Williams 			&scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
2145*cc9203bfSDan Williams 		udelay(AFE_REGISTER_WRITE_DELAY);
2146*cc9203bfSDan Williams 	}
2147*cc9203bfSDan Williams 
2148*cc9203bfSDan Williams 	/* Transfer control to the PEs */
2149*cc9203bfSDan Williams 	writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
2150*cc9203bfSDan Williams 	udelay(AFE_REGISTER_WRITE_DELAY);
2151*cc9203bfSDan Williams }
2152*cc9203bfSDan Williams 
2153*cc9203bfSDan Williams static enum sci_status scic_controller_set_mode(struct scic_sds_controller *scic,
2154*cc9203bfSDan Williams 						enum sci_controller_mode operating_mode)
2155*cc9203bfSDan Williams {
2156*cc9203bfSDan Williams 	enum sci_status status          = SCI_SUCCESS;
2157*cc9203bfSDan Williams 
2158*cc9203bfSDan Williams 	if ((scic->state_machine.current_state_id ==
2159*cc9203bfSDan Williams 				SCI_BASE_CONTROLLER_STATE_INITIALIZING) ||
2160*cc9203bfSDan Williams 	    (scic->state_machine.current_state_id ==
2161*cc9203bfSDan Williams 				SCI_BASE_CONTROLLER_STATE_INITIALIZED)) {
2162*cc9203bfSDan Williams 		switch (operating_mode) {
2163*cc9203bfSDan Williams 		case SCI_MODE_SPEED:
2164*cc9203bfSDan Williams 			scic->remote_node_entries      = SCI_MAX_REMOTE_DEVICES;
2165*cc9203bfSDan Williams 			scic->task_context_entries     = SCU_IO_REQUEST_COUNT;
2166*cc9203bfSDan Williams 			scic->uf_control.buffers.count =
2167*cc9203bfSDan Williams 				SCU_UNSOLICITED_FRAME_COUNT;
2168*cc9203bfSDan Williams 			scic->completion_event_entries = SCU_EVENT_COUNT;
2169*cc9203bfSDan Williams 			scic->completion_queue_entries =
2170*cc9203bfSDan Williams 				SCU_COMPLETION_QUEUE_COUNT;
2171*cc9203bfSDan Williams 			break;
2172*cc9203bfSDan Williams 
2173*cc9203bfSDan Williams 		case SCI_MODE_SIZE:
2174*cc9203bfSDan Williams 			scic->remote_node_entries      = SCI_MIN_REMOTE_DEVICES;
2175*cc9203bfSDan Williams 			scic->task_context_entries     = SCI_MIN_IO_REQUESTS;
2176*cc9203bfSDan Williams 			scic->uf_control.buffers.count =
2177*cc9203bfSDan Williams 				SCU_MIN_UNSOLICITED_FRAMES;
2178*cc9203bfSDan Williams 			scic->completion_event_entries = SCU_MIN_EVENTS;
2179*cc9203bfSDan Williams 			scic->completion_queue_entries =
2180*cc9203bfSDan Williams 				SCU_MIN_COMPLETION_QUEUE_ENTRIES;
2181*cc9203bfSDan Williams 			break;
2182*cc9203bfSDan Williams 
2183*cc9203bfSDan Williams 		default:
2184*cc9203bfSDan Williams 			status = SCI_FAILURE_INVALID_PARAMETER_VALUE;
2185*cc9203bfSDan Williams 			break;
2186*cc9203bfSDan Williams 		}
2187*cc9203bfSDan Williams 	} else
2188*cc9203bfSDan Williams 		status = SCI_FAILURE_INVALID_STATE;
2189*cc9203bfSDan Williams 
2190*cc9203bfSDan Williams 	return status;
2191*cc9203bfSDan Williams }
2192*cc9203bfSDan Williams 
2193*cc9203bfSDan Williams static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
2194*cc9203bfSDan Williams {
2195*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
2196*cc9203bfSDan Williams 	scic->power_control.timer = isci_timer_create(ihost,
2197*cc9203bfSDan Williams 						      scic,
2198*cc9203bfSDan Williams 					scic_sds_controller_power_control_timer_handler);
2199*cc9203bfSDan Williams 
2200*cc9203bfSDan Williams 	memset(scic->power_control.requesters, 0,
2201*cc9203bfSDan Williams 	       sizeof(scic->power_control.requesters));
2202*cc9203bfSDan Williams 
2203*cc9203bfSDan Williams 	scic->power_control.phys_waiting = 0;
2204*cc9203bfSDan Williams 	scic->power_control.phys_granted_power = 0;
2205*cc9203bfSDan Williams }
2206*cc9203bfSDan Williams 
2207*cc9203bfSDan Williams static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
2208*cc9203bfSDan Williams {
2209*cc9203bfSDan Williams 	struct sci_base_state_machine *sm = &scic->state_machine;
2210*cc9203bfSDan Williams 	enum sci_status result = SCI_SUCCESS;
2211*cc9203bfSDan Williams 	struct isci_host *ihost = scic_to_ihost(scic);
2212*cc9203bfSDan Williams 	u32 index, state;
2213*cc9203bfSDan Williams 
2214*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
2215*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_RESET) {
2216*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
2217*cc9203bfSDan Williams 			 "SCIC Controller initialize operation requested "
2218*cc9203bfSDan Williams 			 "in invalid state\n");
2219*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2220*cc9203bfSDan Williams 	}
2221*cc9203bfSDan Williams 
2222*cc9203bfSDan Williams 	sci_base_state_machine_change_state(sm, SCI_BASE_CONTROLLER_STATE_INITIALIZING);
2223*cc9203bfSDan Williams 
2224*cc9203bfSDan Williams 	scic->timeout_timer = isci_timer_create(ihost, scic,
2225*cc9203bfSDan Williams 						scic_sds_controller_timeout_handler);
2226*cc9203bfSDan Williams 
2227*cc9203bfSDan Williams 	scic_sds_controller_initialize_phy_startup(scic);
2228*cc9203bfSDan Williams 
2229*cc9203bfSDan Williams 	scic_sds_controller_initialize_power_control(scic);
2230*cc9203bfSDan Williams 
2231*cc9203bfSDan Williams 	/*
2232*cc9203bfSDan Williams 	 * There is nothing to do here for B0 since we do not have to
2233*cc9203bfSDan Williams 	 * program the AFE registers.
2234*cc9203bfSDan Williams 	 * / @todo The AFE settings are supposed to be correct for the B0 but
2235*cc9203bfSDan Williams 	 * /       presently they seem to be wrong. */
2236*cc9203bfSDan Williams 	scic_sds_controller_afe_initialization(scic);
2237*cc9203bfSDan Williams 
2238*cc9203bfSDan Williams 	if (result == SCI_SUCCESS) {
2239*cc9203bfSDan Williams 		u32 status;
2240*cc9203bfSDan Williams 		u32 terminate_loop;
2241*cc9203bfSDan Williams 
2242*cc9203bfSDan Williams 		/* Take the hardware out of reset */
2243*cc9203bfSDan Williams 		writel(0, &scic->smu_registers->soft_reset_control);
2244*cc9203bfSDan Williams 
2245*cc9203bfSDan Williams 		/*
2246*cc9203bfSDan Williams 		 * / @todo Provide meaningfull error code for hardware failure
2247*cc9203bfSDan Williams 		 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2248*cc9203bfSDan Williams 		result = SCI_FAILURE;
2249*cc9203bfSDan Williams 		terminate_loop = 100;
2250*cc9203bfSDan Williams 
2251*cc9203bfSDan Williams 		while (terminate_loop-- && (result != SCI_SUCCESS)) {
2252*cc9203bfSDan Williams 			/* Loop until the hardware reports success */
2253*cc9203bfSDan Williams 			udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2254*cc9203bfSDan Williams 			status = readl(&scic->smu_registers->control_status);
2255*cc9203bfSDan Williams 
2256*cc9203bfSDan Williams 			if ((status & SCU_RAM_INIT_COMPLETED) ==
2257*cc9203bfSDan Williams 					SCU_RAM_INIT_COMPLETED)
2258*cc9203bfSDan Williams 				result = SCI_SUCCESS;
2259*cc9203bfSDan Williams 		}
2260*cc9203bfSDan Williams 	}
2261*cc9203bfSDan Williams 
2262*cc9203bfSDan Williams 	if (result == SCI_SUCCESS) {
2263*cc9203bfSDan Williams 		u32 max_supported_ports;
2264*cc9203bfSDan Williams 		u32 max_supported_devices;
2265*cc9203bfSDan Williams 		u32 max_supported_io_requests;
2266*cc9203bfSDan Williams 		u32 device_context_capacity;
2267*cc9203bfSDan Williams 
2268*cc9203bfSDan Williams 		/*
2269*cc9203bfSDan Williams 		 * Determine what are the actaul device capacities that the
2270*cc9203bfSDan Williams 		 * hardware will support */
2271*cc9203bfSDan Williams 		device_context_capacity =
2272*cc9203bfSDan Williams 			readl(&scic->smu_registers->device_context_capacity);
2273*cc9203bfSDan Williams 
2274*cc9203bfSDan Williams 
2275*cc9203bfSDan Williams 		max_supported_ports = smu_dcc_get_max_ports(device_context_capacity);
2276*cc9203bfSDan Williams 		max_supported_devices = smu_dcc_get_max_remote_node_context(device_context_capacity);
2277*cc9203bfSDan Williams 		max_supported_io_requests = smu_dcc_get_max_task_context(device_context_capacity);
2278*cc9203bfSDan Williams 
2279*cc9203bfSDan Williams 		/*
2280*cc9203bfSDan Williams 		 * Make all PEs that are unassigned match up with the
2281*cc9203bfSDan Williams 		 * logical ports
2282*cc9203bfSDan Williams 		 */
2283*cc9203bfSDan Williams 		for (index = 0; index < max_supported_ports; index++) {
2284*cc9203bfSDan Williams 			struct scu_port_task_scheduler_group_registers __iomem
2285*cc9203bfSDan Williams 				*ptsg = &scic->scu_registers->peg0.ptsg;
2286*cc9203bfSDan Williams 
2287*cc9203bfSDan Williams 			writel(index, &ptsg->protocol_engine[index]);
2288*cc9203bfSDan Williams 		}
2289*cc9203bfSDan Williams 
2290*cc9203bfSDan Williams 		/* Record the smaller of the two capacity values */
2291*cc9203bfSDan Williams 		scic->logical_port_entries =
2292*cc9203bfSDan Williams 			min(max_supported_ports, scic->logical_port_entries);
2293*cc9203bfSDan Williams 
2294*cc9203bfSDan Williams 		scic->task_context_entries =
2295*cc9203bfSDan Williams 			min(max_supported_io_requests,
2296*cc9203bfSDan Williams 			    scic->task_context_entries);
2297*cc9203bfSDan Williams 
2298*cc9203bfSDan Williams 		scic->remote_node_entries =
2299*cc9203bfSDan Williams 			min(max_supported_devices, scic->remote_node_entries);
2300*cc9203bfSDan Williams 
2301*cc9203bfSDan Williams 		/*
2302*cc9203bfSDan Williams 		 * Now that we have the correct hardware reported minimum values
2303*cc9203bfSDan Williams 		 * build the MDL for the controller.  Default to a performance
2304*cc9203bfSDan Williams 		 * configuration.
2305*cc9203bfSDan Williams 		 */
2306*cc9203bfSDan Williams 		scic_controller_set_mode(scic, SCI_MODE_SPEED);
2307*cc9203bfSDan Williams 	}
2308*cc9203bfSDan Williams 
2309*cc9203bfSDan Williams 	/* Initialize hardware PCI Relaxed ordering in DMA engines */
2310*cc9203bfSDan Williams 	if (result == SCI_SUCCESS) {
2311*cc9203bfSDan Williams 		u32 dma_configuration;
2312*cc9203bfSDan Williams 
2313*cc9203bfSDan Williams 		/* Configure the payload DMA */
2314*cc9203bfSDan Williams 		dma_configuration =
2315*cc9203bfSDan Williams 			readl(&scic->scu_registers->sdma.pdma_configuration);
2316*cc9203bfSDan Williams 		dma_configuration |=
2317*cc9203bfSDan Williams 			SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2318*cc9203bfSDan Williams 		writel(dma_configuration,
2319*cc9203bfSDan Williams 			&scic->scu_registers->sdma.pdma_configuration);
2320*cc9203bfSDan Williams 
2321*cc9203bfSDan Williams 		/* Configure the control DMA */
2322*cc9203bfSDan Williams 		dma_configuration =
2323*cc9203bfSDan Williams 			readl(&scic->scu_registers->sdma.cdma_configuration);
2324*cc9203bfSDan Williams 		dma_configuration |=
2325*cc9203bfSDan Williams 			SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2326*cc9203bfSDan Williams 		writel(dma_configuration,
2327*cc9203bfSDan Williams 			&scic->scu_registers->sdma.cdma_configuration);
2328*cc9203bfSDan Williams 	}
2329*cc9203bfSDan Williams 
2330*cc9203bfSDan Williams 	/*
2331*cc9203bfSDan Williams 	 * Initialize the PHYs before the PORTs because the PHY registers
2332*cc9203bfSDan Williams 	 * are accessed during the port initialization.
2333*cc9203bfSDan Williams 	 */
2334*cc9203bfSDan Williams 	if (result == SCI_SUCCESS) {
2335*cc9203bfSDan Williams 		/* Initialize the phys */
2336*cc9203bfSDan Williams 		for (index = 0;
2337*cc9203bfSDan Williams 		     (result == SCI_SUCCESS) && (index < SCI_MAX_PHYS);
2338*cc9203bfSDan Williams 		     index++) {
2339*cc9203bfSDan Williams 			result = scic_sds_phy_initialize(
2340*cc9203bfSDan Williams 				&ihost->phys[index].sci,
2341*cc9203bfSDan Williams 				&scic->scu_registers->peg0.pe[index].tl,
2342*cc9203bfSDan Williams 				&scic->scu_registers->peg0.pe[index].ll);
2343*cc9203bfSDan Williams 		}
2344*cc9203bfSDan Williams 	}
2345*cc9203bfSDan Williams 
2346*cc9203bfSDan Williams 	if (result == SCI_SUCCESS) {
2347*cc9203bfSDan Williams 		/* Initialize the logical ports */
2348*cc9203bfSDan Williams 		for (index = 0;
2349*cc9203bfSDan Williams 		     (index < scic->logical_port_entries) &&
2350*cc9203bfSDan Williams 		     (result == SCI_SUCCESS);
2351*cc9203bfSDan Williams 		     index++) {
2352*cc9203bfSDan Williams 			result = scic_sds_port_initialize(
2353*cc9203bfSDan Williams 				&ihost->ports[index].sci,
2354*cc9203bfSDan Williams 				&scic->scu_registers->peg0.ptsg.port[index],
2355*cc9203bfSDan Williams 				&scic->scu_registers->peg0.ptsg.protocol_engine,
2356*cc9203bfSDan Williams 				&scic->scu_registers->peg0.viit[index]);
2357*cc9203bfSDan Williams 		}
2358*cc9203bfSDan Williams 	}
2359*cc9203bfSDan Williams 
2360*cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2361*cc9203bfSDan Williams 		result = scic_sds_port_configuration_agent_initialize(
2362*cc9203bfSDan Williams 				scic,
2363*cc9203bfSDan Williams 				&scic->port_agent);
2364*cc9203bfSDan Williams 
2365*cc9203bfSDan Williams 	/* Advance the controller state machine */
2366*cc9203bfSDan Williams 	if (result == SCI_SUCCESS)
2367*cc9203bfSDan Williams 		state = SCI_BASE_CONTROLLER_STATE_INITIALIZED;
2368*cc9203bfSDan Williams 	else
2369*cc9203bfSDan Williams 		state = SCI_BASE_CONTROLLER_STATE_FAILED;
2370*cc9203bfSDan Williams 	sci_base_state_machine_change_state(sm, state);
2371*cc9203bfSDan Williams 
2372*cc9203bfSDan Williams 	return result;
2373*cc9203bfSDan Williams }
2374*cc9203bfSDan Williams 
2375*cc9203bfSDan Williams static enum sci_status scic_user_parameters_set(
2376*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2377*cc9203bfSDan Williams 	union scic_user_parameters *scic_parms)
2378*cc9203bfSDan Williams {
2379*cc9203bfSDan Williams 	u32 state = scic->state_machine.current_state_id;
2380*cc9203bfSDan Williams 
2381*cc9203bfSDan Williams 	if (state == SCI_BASE_CONTROLLER_STATE_RESET ||
2382*cc9203bfSDan Williams 	    state == SCI_BASE_CONTROLLER_STATE_INITIALIZING ||
2383*cc9203bfSDan Williams 	    state == SCI_BASE_CONTROLLER_STATE_INITIALIZED) {
2384*cc9203bfSDan Williams 		u16 index;
2385*cc9203bfSDan Williams 
2386*cc9203bfSDan Williams 		/*
2387*cc9203bfSDan Williams 		 * Validate the user parameters.  If they are not legal, then
2388*cc9203bfSDan Williams 		 * return a failure.
2389*cc9203bfSDan Williams 		 */
2390*cc9203bfSDan Williams 		for (index = 0; index < SCI_MAX_PHYS; index++) {
2391*cc9203bfSDan Williams 			struct sci_phy_user_params *user_phy;
2392*cc9203bfSDan Williams 
2393*cc9203bfSDan Williams 			user_phy = &scic_parms->sds1.phys[index];
2394*cc9203bfSDan Williams 
2395*cc9203bfSDan Williams 			if (!((user_phy->max_speed_generation <=
2396*cc9203bfSDan Williams 						SCIC_SDS_PARM_MAX_SPEED) &&
2397*cc9203bfSDan Williams 			      (user_phy->max_speed_generation >
2398*cc9203bfSDan Williams 						SCIC_SDS_PARM_NO_SPEED)))
2399*cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2400*cc9203bfSDan Williams 
2401*cc9203bfSDan Williams 			if (user_phy->in_connection_align_insertion_frequency <
2402*cc9203bfSDan Williams 					3)
2403*cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2404*cc9203bfSDan Williams 
2405*cc9203bfSDan Williams 			if ((user_phy->in_connection_align_insertion_frequency <
2406*cc9203bfSDan Williams 						3) ||
2407*cc9203bfSDan Williams 			    (user_phy->align_insertion_frequency == 0) ||
2408*cc9203bfSDan Williams 			    (user_phy->
2409*cc9203bfSDan Williams 				notify_enable_spin_up_insertion_frequency ==
2410*cc9203bfSDan Williams 						0))
2411*cc9203bfSDan Williams 				return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2412*cc9203bfSDan Williams 		}
2413*cc9203bfSDan Williams 
2414*cc9203bfSDan Williams 		if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
2415*cc9203bfSDan Williams 		    (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
2416*cc9203bfSDan Williams 		    (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
2417*cc9203bfSDan Williams 		    (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
2418*cc9203bfSDan Williams 		    (scic_parms->sds1.no_outbound_task_timeout == 0))
2419*cc9203bfSDan Williams 			return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2420*cc9203bfSDan Williams 
2421*cc9203bfSDan Williams 		memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
2422*cc9203bfSDan Williams 
2423*cc9203bfSDan Williams 		return SCI_SUCCESS;
2424*cc9203bfSDan Williams 	}
2425*cc9203bfSDan Williams 
2426*cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_STATE;
2427*cc9203bfSDan Williams }
2428*cc9203bfSDan Williams 
2429*cc9203bfSDan Williams static int scic_controller_mem_init(struct scic_sds_controller *scic)
2430*cc9203bfSDan Williams {
2431*cc9203bfSDan Williams 	struct device *dev = scic_to_dev(scic);
2432*cc9203bfSDan Williams 	dma_addr_t dma_handle;
2433*cc9203bfSDan Williams 	enum sci_status result;
2434*cc9203bfSDan Williams 
2435*cc9203bfSDan Williams 	scic->completion_queue = dmam_alloc_coherent(dev,
2436*cc9203bfSDan Williams 			scic->completion_queue_entries * sizeof(u32),
2437*cc9203bfSDan Williams 			&dma_handle, GFP_KERNEL);
2438*cc9203bfSDan Williams 	if (!scic->completion_queue)
2439*cc9203bfSDan Williams 		return -ENOMEM;
2440*cc9203bfSDan Williams 
2441*cc9203bfSDan Williams 	writel(lower_32_bits(dma_handle),
2442*cc9203bfSDan Williams 		&scic->smu_registers->completion_queue_lower);
2443*cc9203bfSDan Williams 	writel(upper_32_bits(dma_handle),
2444*cc9203bfSDan Williams 		&scic->smu_registers->completion_queue_upper);
2445*cc9203bfSDan Williams 
2446*cc9203bfSDan Williams 	scic->remote_node_context_table = dmam_alloc_coherent(dev,
2447*cc9203bfSDan Williams 			scic->remote_node_entries *
2448*cc9203bfSDan Williams 				sizeof(union scu_remote_node_context),
2449*cc9203bfSDan Williams 			&dma_handle, GFP_KERNEL);
2450*cc9203bfSDan Williams 	if (!scic->remote_node_context_table)
2451*cc9203bfSDan Williams 		return -ENOMEM;
2452*cc9203bfSDan Williams 
2453*cc9203bfSDan Williams 	writel(lower_32_bits(dma_handle),
2454*cc9203bfSDan Williams 		&scic->smu_registers->remote_node_context_lower);
2455*cc9203bfSDan Williams 	writel(upper_32_bits(dma_handle),
2456*cc9203bfSDan Williams 		&scic->smu_registers->remote_node_context_upper);
2457*cc9203bfSDan Williams 
2458*cc9203bfSDan Williams 	scic->task_context_table = dmam_alloc_coherent(dev,
2459*cc9203bfSDan Williams 			scic->task_context_entries *
2460*cc9203bfSDan Williams 				sizeof(struct scu_task_context),
2461*cc9203bfSDan Williams 			&dma_handle, GFP_KERNEL);
2462*cc9203bfSDan Williams 	if (!scic->task_context_table)
2463*cc9203bfSDan Williams 		return -ENOMEM;
2464*cc9203bfSDan Williams 
2465*cc9203bfSDan Williams 	writel(lower_32_bits(dma_handle),
2466*cc9203bfSDan Williams 		&scic->smu_registers->host_task_table_lower);
2467*cc9203bfSDan Williams 	writel(upper_32_bits(dma_handle),
2468*cc9203bfSDan Williams 		&scic->smu_registers->host_task_table_upper);
2469*cc9203bfSDan Williams 
2470*cc9203bfSDan Williams 	result = scic_sds_unsolicited_frame_control_construct(scic);
2471*cc9203bfSDan Williams 	if (result)
2472*cc9203bfSDan Williams 		return result;
2473*cc9203bfSDan Williams 
2474*cc9203bfSDan Williams 	/*
2475*cc9203bfSDan Williams 	 * Inform the silicon as to the location of the UF headers and
2476*cc9203bfSDan Williams 	 * address table.
2477*cc9203bfSDan Williams 	 */
2478*cc9203bfSDan Williams 	writel(lower_32_bits(scic->uf_control.headers.physical_address),
2479*cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_header_base_address_lower);
2480*cc9203bfSDan Williams 	writel(upper_32_bits(scic->uf_control.headers.physical_address),
2481*cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_header_base_address_upper);
2482*cc9203bfSDan Williams 
2483*cc9203bfSDan Williams 	writel(lower_32_bits(scic->uf_control.address_table.physical_address),
2484*cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_address_table_lower);
2485*cc9203bfSDan Williams 	writel(upper_32_bits(scic->uf_control.address_table.physical_address),
2486*cc9203bfSDan Williams 		&scic->scu_registers->sdma.uf_address_table_upper);
2487*cc9203bfSDan Williams 
2488*cc9203bfSDan Williams 	return 0;
2489*cc9203bfSDan Williams }
2490*cc9203bfSDan Williams 
24916f231ddaSDan Williams int isci_host_init(struct isci_host *isci_host)
24926f231ddaSDan Williams {
2493d9c37390SDan Williams 	int err = 0, i;
24946f231ddaSDan Williams 	enum sci_status status;
24954711ba10SDan Williams 	union scic_oem_parameters oem;
24966f231ddaSDan Williams 	union scic_user_parameters scic_user_params;
2497d044af17SDan Williams 	struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
24986f231ddaSDan Williams 
24997c40a803SDan Williams 	isci_timer_list_construct(isci_host);
25006f231ddaSDan Williams 
25016f231ddaSDan Williams 	spin_lock_init(&isci_host->state_lock);
25026f231ddaSDan Williams 	spin_lock_init(&isci_host->scic_lock);
25036f231ddaSDan Williams 	spin_lock_init(&isci_host->queue_lock);
25040cf89d1dSDan Williams 	init_waitqueue_head(&isci_host->eventq);
25056f231ddaSDan Williams 
25066f231ddaSDan Williams 	isci_host_change_state(isci_host, isci_starting);
25076f231ddaSDan Williams 	isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
25086f231ddaSDan Williams 
2509cc3dbd0aSArtur Wojcik 	status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
25106f231ddaSDan Williams 					   smu_base(isci_host));
25116f231ddaSDan Williams 
25126f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
25136f231ddaSDan Williams 		dev_err(&isci_host->pdev->dev,
25146f231ddaSDan Williams 			"%s: scic_controller_construct failed - status = %x\n",
25156f231ddaSDan Williams 			__func__,
25166f231ddaSDan Williams 			status);
2517858d4aa7SDave Jiang 		return -ENODEV;
25186f231ddaSDan Williams 	}
25196f231ddaSDan Williams 
25206f231ddaSDan Williams 	isci_host->sas_ha.dev = &isci_host->pdev->dev;
25216f231ddaSDan Williams 	isci_host->sas_ha.lldd_ha = isci_host;
25226f231ddaSDan Williams 
2523d044af17SDan Williams 	/*
2524d044af17SDan Williams 	 * grab initial values stored in the controller object for OEM and USER
2525d044af17SDan Williams 	 * parameters
2526d044af17SDan Williams 	 */
2527b5f18a20SDave Jiang 	isci_user_parameters_get(isci_host, &scic_user_params);
2528cc3dbd0aSArtur Wojcik 	status = scic_user_parameters_set(&isci_host->sci,
2529d044af17SDan Williams 					  &scic_user_params);
2530d044af17SDan Williams 	if (status != SCI_SUCCESS) {
2531d044af17SDan Williams 		dev_warn(&isci_host->pdev->dev,
2532d044af17SDan Williams 			 "%s: scic_user_parameters_set failed\n",
2533d044af17SDan Williams 			 __func__);
2534d044af17SDan Williams 		return -ENODEV;
2535d044af17SDan Williams 	}
25366f231ddaSDan Williams 
2537cc3dbd0aSArtur Wojcik 	scic_oem_parameters_get(&isci_host->sci, &oem);
2538d044af17SDan Williams 
2539d044af17SDan Williams 	/* grab any OEM parameters specified in orom */
2540d044af17SDan Williams 	if (pci_info->orom) {
25414711ba10SDan Williams 		status = isci_parse_oem_parameters(&oem,
2542d044af17SDan Williams 						   pci_info->orom,
2543d044af17SDan Williams 						   isci_host->id);
25446f231ddaSDan Williams 		if (status != SCI_SUCCESS) {
25456f231ddaSDan Williams 			dev_warn(&isci_host->pdev->dev,
25466f231ddaSDan Williams 				 "parsing firmware oem parameters failed\n");
2547858d4aa7SDave Jiang 			return -EINVAL;
25486f231ddaSDan Williams 		}
25494711ba10SDan Williams 	}
25504711ba10SDan Williams 
2551cc3dbd0aSArtur Wojcik 	status = scic_oem_parameters_set(&isci_host->sci, &oem);
25526f231ddaSDan Williams 	if (status != SCI_SUCCESS) {
25536f231ddaSDan Williams 		dev_warn(&isci_host->pdev->dev,
25546f231ddaSDan Williams 				"%s: scic_oem_parameters_set failed\n",
25556f231ddaSDan Williams 				__func__);
2556858d4aa7SDave Jiang 		return -ENODEV;
25576f231ddaSDan Williams 	}
25586f231ddaSDan Williams 
25596f231ddaSDan Williams 	tasklet_init(&isci_host->completion_tasklet,
2560c7ef4031SDan Williams 		     isci_host_completion_routine, (unsigned long)isci_host);
25616f231ddaSDan Williams 
25626f231ddaSDan Williams 	INIT_LIST_HEAD(&isci_host->requests_to_complete);
256311b00c19SJeff Skirvin 	INIT_LIST_HEAD(&isci_host->requests_to_errorback);
25646f231ddaSDan Williams 
25657c40a803SDan Williams 	spin_lock_irq(&isci_host->scic_lock);
2566cc3dbd0aSArtur Wojcik 	status = scic_controller_initialize(&isci_host->sci);
25677c40a803SDan Williams 	spin_unlock_irq(&isci_host->scic_lock);
25687c40a803SDan Williams 	if (status != SCI_SUCCESS) {
25697c40a803SDan Williams 		dev_warn(&isci_host->pdev->dev,
25707c40a803SDan Williams 			 "%s: scic_controller_initialize failed -"
25717c40a803SDan Williams 			 " status = 0x%x\n",
25727c40a803SDan Williams 			 __func__, status);
25737c40a803SDan Williams 		return -ENODEV;
25747c40a803SDan Williams 	}
25757c40a803SDan Williams 
2576cc3dbd0aSArtur Wojcik 	err = scic_controller_mem_init(&isci_host->sci);
25776f231ddaSDan Williams 	if (err)
2578858d4aa7SDave Jiang 		return err;
25796f231ddaSDan Williams 
25806f231ddaSDan Williams 	isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
258167ea838dSDan Williams 					       sizeof(struct isci_request),
25826f231ddaSDan Williams 					       SLAB_HWCACHE_ALIGN, 0);
25836f231ddaSDan Williams 
2584858d4aa7SDave Jiang 	if (!isci_host->dma_pool)
2585858d4aa7SDave Jiang 		return -ENOMEM;
25866f231ddaSDan Williams 
2587d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PORTS; i++)
2588e531381eSDan Williams 		isci_port_init(&isci_host->ports[i], isci_host, i);
25896f231ddaSDan Williams 
2590d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_PHYS; i++)
2591d9c37390SDan Williams 		isci_phy_init(&isci_host->phys[i], isci_host, i);
2592d9c37390SDan Williams 
2593d9c37390SDan Williams 	for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
259457f20f4eSDan Williams 		struct isci_remote_device *idev = &isci_host->devices[i];
2595d9c37390SDan Williams 
2596d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->reqs_in_process);
2597d9c37390SDan Williams 		INIT_LIST_HEAD(&idev->node);
2598d9c37390SDan Williams 		spin_lock_init(&idev->state_lock);
2599d9c37390SDan Williams 	}
26006f231ddaSDan Williams 
2601858d4aa7SDave Jiang 	return 0;
26026f231ddaSDan Williams }
2603*cc9203bfSDan Williams 
2604*cc9203bfSDan Williams void scic_sds_controller_link_up(struct scic_sds_controller *scic,
2605*cc9203bfSDan Williams 		struct scic_sds_port *port, struct scic_sds_phy *phy)
2606*cc9203bfSDan Williams {
2607*cc9203bfSDan Williams 	switch (scic->state_machine.current_state_id) {
2608*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_STARTING:
2609*cc9203bfSDan Williams 		scic_sds_controller_phy_timer_stop(scic);
2610*cc9203bfSDan Williams 		scic->port_agent.link_up_handler(scic, &scic->port_agent,
2611*cc9203bfSDan Williams 						 port, phy);
2612*cc9203bfSDan Williams 		scic_sds_controller_start_next_phy(scic);
2613*cc9203bfSDan Williams 		break;
2614*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_READY:
2615*cc9203bfSDan Williams 		scic->port_agent.link_up_handler(scic, &scic->port_agent,
2616*cc9203bfSDan Williams 						 port, phy);
2617*cc9203bfSDan Williams 		break;
2618*cc9203bfSDan Williams 	default:
2619*cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
2620*cc9203bfSDan Williams 			"%s: SCIC Controller linkup event from phy %d in "
2621*cc9203bfSDan Williams 			"unexpected state %d\n", __func__, phy->phy_index,
2622*cc9203bfSDan Williams 			scic->state_machine.current_state_id);
2623*cc9203bfSDan Williams 	}
2624*cc9203bfSDan Williams }
2625*cc9203bfSDan Williams 
2626*cc9203bfSDan Williams void scic_sds_controller_link_down(struct scic_sds_controller *scic,
2627*cc9203bfSDan Williams 		struct scic_sds_port *port, struct scic_sds_phy *phy)
2628*cc9203bfSDan Williams {
2629*cc9203bfSDan Williams 	switch (scic->state_machine.current_state_id) {
2630*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_STARTING:
2631*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_READY:
2632*cc9203bfSDan Williams 		scic->port_agent.link_down_handler(scic, &scic->port_agent,
2633*cc9203bfSDan Williams 						   port, phy);
2634*cc9203bfSDan Williams 		break;
2635*cc9203bfSDan Williams 	default:
2636*cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
2637*cc9203bfSDan Williams 			"%s: SCIC Controller linkdown event from phy %d in "
2638*cc9203bfSDan Williams 			"unexpected state %d\n",
2639*cc9203bfSDan Williams 			__func__,
2640*cc9203bfSDan Williams 			phy->phy_index,
2641*cc9203bfSDan Williams 			scic->state_machine.current_state_id);
2642*cc9203bfSDan Williams 	}
2643*cc9203bfSDan Williams }
2644*cc9203bfSDan Williams 
2645*cc9203bfSDan Williams /**
2646*cc9203bfSDan Williams  * This is a helper method to determine if any remote devices on this
2647*cc9203bfSDan Williams  * controller are still in the stopping state.
2648*cc9203bfSDan Williams  *
2649*cc9203bfSDan Williams  */
2650*cc9203bfSDan Williams static bool scic_sds_controller_has_remote_devices_stopping(
2651*cc9203bfSDan Williams 	struct scic_sds_controller *controller)
2652*cc9203bfSDan Williams {
2653*cc9203bfSDan Williams 	u32 index;
2654*cc9203bfSDan Williams 
2655*cc9203bfSDan Williams 	for (index = 0; index < controller->remote_node_entries; index++) {
2656*cc9203bfSDan Williams 		if ((controller->device_table[index] != NULL) &&
2657*cc9203bfSDan Williams 		   (controller->device_table[index]->state_machine.current_state_id
2658*cc9203bfSDan Williams 		    == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING))
2659*cc9203bfSDan Williams 			return true;
2660*cc9203bfSDan Williams 	}
2661*cc9203bfSDan Williams 
2662*cc9203bfSDan Williams 	return false;
2663*cc9203bfSDan Williams }
2664*cc9203bfSDan Williams 
2665*cc9203bfSDan Williams /**
2666*cc9203bfSDan Williams  * This method is called by the remote device to inform the controller
2667*cc9203bfSDan Williams  * object that the remote device has stopped.
2668*cc9203bfSDan Williams  */
2669*cc9203bfSDan Williams void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
2670*cc9203bfSDan Williams 					       struct scic_sds_remote_device *sci_dev)
2671*cc9203bfSDan Williams {
2672*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
2673*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_STOPPING) {
2674*cc9203bfSDan Williams 		dev_dbg(scic_to_dev(scic),
2675*cc9203bfSDan Williams 			"SCIC Controller 0x%p remote device stopped event "
2676*cc9203bfSDan Williams 			"from device 0x%p in unexpected state %d\n",
2677*cc9203bfSDan Williams 			scic, sci_dev,
2678*cc9203bfSDan Williams 			scic->state_machine.current_state_id);
2679*cc9203bfSDan Williams 		return;
2680*cc9203bfSDan Williams 	}
2681*cc9203bfSDan Williams 
2682*cc9203bfSDan Williams 	if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
2683*cc9203bfSDan Williams 		sci_base_state_machine_change_state(&scic->state_machine,
2684*cc9203bfSDan Williams 				SCI_BASE_CONTROLLER_STATE_STOPPED);
2685*cc9203bfSDan Williams 	}
2686*cc9203bfSDan Williams }
2687*cc9203bfSDan Williams 
2688*cc9203bfSDan Williams /**
2689*cc9203bfSDan Williams  * This method will write to the SCU PCP register the request value. The method
2690*cc9203bfSDan Williams  *    is used to suspend/resume ports, devices, and phys.
2691*cc9203bfSDan Williams  * @scic:
2692*cc9203bfSDan Williams  *
2693*cc9203bfSDan Williams  *
2694*cc9203bfSDan Williams  */
2695*cc9203bfSDan Williams void scic_sds_controller_post_request(
2696*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2697*cc9203bfSDan Williams 	u32 request)
2698*cc9203bfSDan Williams {
2699*cc9203bfSDan Williams 	dev_dbg(scic_to_dev(scic),
2700*cc9203bfSDan Williams 		"%s: SCIC Controller 0x%p post request 0x%08x\n",
2701*cc9203bfSDan Williams 		__func__,
2702*cc9203bfSDan Williams 		scic,
2703*cc9203bfSDan Williams 		request);
2704*cc9203bfSDan Williams 
2705*cc9203bfSDan Williams 	writel(request, &scic->smu_registers->post_context_port);
2706*cc9203bfSDan Williams }
2707*cc9203bfSDan Williams 
2708*cc9203bfSDan Williams /**
2709*cc9203bfSDan Williams  * This method will copy the soft copy of the task context into the physical
2710*cc9203bfSDan Williams  *    memory accessible by the controller.
2711*cc9203bfSDan Williams  * @scic: This parameter specifies the controller for which to copy
2712*cc9203bfSDan Williams  *    the task context.
2713*cc9203bfSDan Williams  * @sci_req: This parameter specifies the request for which the task
2714*cc9203bfSDan Williams  *    context is being copied.
2715*cc9203bfSDan Williams  *
2716*cc9203bfSDan Williams  * After this call is made the SCIC_SDS_IO_REQUEST object will always point to
2717*cc9203bfSDan Williams  * the physical memory version of the task context. Thus, all subsequent
2718*cc9203bfSDan Williams  * updates to the task context are performed in the TC table (i.e. DMAable
2719*cc9203bfSDan Williams  * memory). none
2720*cc9203bfSDan Williams  */
2721*cc9203bfSDan Williams void scic_sds_controller_copy_task_context(
2722*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2723*cc9203bfSDan Williams 	struct scic_sds_request *sci_req)
2724*cc9203bfSDan Williams {
2725*cc9203bfSDan Williams 	struct scu_task_context *task_context_buffer;
2726*cc9203bfSDan Williams 
2727*cc9203bfSDan Williams 	task_context_buffer = scic_sds_controller_get_task_context_buffer(
2728*cc9203bfSDan Williams 		scic, sci_req->io_tag);
2729*cc9203bfSDan Williams 
2730*cc9203bfSDan Williams 	memcpy(task_context_buffer,
2731*cc9203bfSDan Williams 	       sci_req->task_context_buffer,
2732*cc9203bfSDan Williams 	       offsetof(struct scu_task_context, sgl_snapshot_ac));
2733*cc9203bfSDan Williams 
2734*cc9203bfSDan Williams 	/*
2735*cc9203bfSDan Williams 	 * Now that the soft copy of the TC has been copied into the TC
2736*cc9203bfSDan Williams 	 * table accessible by the silicon.  Thus, any further changes to
2737*cc9203bfSDan Williams 	 * the TC (e.g. TC termination) occur in the appropriate location. */
2738*cc9203bfSDan Williams 	sci_req->task_context_buffer = task_context_buffer;
2739*cc9203bfSDan Williams }
2740*cc9203bfSDan Williams 
2741*cc9203bfSDan Williams /**
2742*cc9203bfSDan Williams  * This method returns the task context buffer for the given io tag.
2743*cc9203bfSDan Williams  * @scic:
2744*cc9203bfSDan Williams  * @io_tag:
2745*cc9203bfSDan Williams  *
2746*cc9203bfSDan Williams  * struct scu_task_context*
2747*cc9203bfSDan Williams  */
2748*cc9203bfSDan Williams struct scu_task_context *scic_sds_controller_get_task_context_buffer(
2749*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2750*cc9203bfSDan Williams 	u16 io_tag
2751*cc9203bfSDan Williams 	) {
2752*cc9203bfSDan Williams 	u16 task_index = scic_sds_io_tag_get_index(io_tag);
2753*cc9203bfSDan Williams 
2754*cc9203bfSDan Williams 	if (task_index < scic->task_context_entries) {
2755*cc9203bfSDan Williams 		return &scic->task_context_table[task_index];
2756*cc9203bfSDan Williams 	}
2757*cc9203bfSDan Williams 
2758*cc9203bfSDan Williams 	return NULL;
2759*cc9203bfSDan Williams }
2760*cc9203bfSDan Williams 
2761*cc9203bfSDan Williams struct scic_sds_request *scic_request_by_tag(struct scic_sds_controller *scic,
2762*cc9203bfSDan Williams 					     u16 io_tag)
2763*cc9203bfSDan Williams {
2764*cc9203bfSDan Williams 	u16 task_index;
2765*cc9203bfSDan Williams 	u16 task_sequence;
2766*cc9203bfSDan Williams 
2767*cc9203bfSDan Williams 	task_index = scic_sds_io_tag_get_index(io_tag);
2768*cc9203bfSDan Williams 
2769*cc9203bfSDan Williams 	if (task_index  < scic->task_context_entries) {
2770*cc9203bfSDan Williams 		if (scic->io_request_table[task_index] != NULL) {
2771*cc9203bfSDan Williams 			task_sequence = scic_sds_io_tag_get_sequence(io_tag);
2772*cc9203bfSDan Williams 
2773*cc9203bfSDan Williams 			if (task_sequence == scic->io_request_sequence[task_index]) {
2774*cc9203bfSDan Williams 				return scic->io_request_table[task_index];
2775*cc9203bfSDan Williams 			}
2776*cc9203bfSDan Williams 		}
2777*cc9203bfSDan Williams 	}
2778*cc9203bfSDan Williams 
2779*cc9203bfSDan Williams 	return NULL;
2780*cc9203bfSDan Williams }
2781*cc9203bfSDan Williams 
2782*cc9203bfSDan Williams /**
2783*cc9203bfSDan Williams  * This method allocates remote node index and the reserves the remote node
2784*cc9203bfSDan Williams  *    context space for use. This method can fail if there are no more remote
2785*cc9203bfSDan Williams  *    node index available.
2786*cc9203bfSDan Williams  * @scic: This is the controller object which contains the set of
2787*cc9203bfSDan Williams  *    free remote node ids
2788*cc9203bfSDan Williams  * @sci_dev: This is the device object which is requesting the a remote node
2789*cc9203bfSDan Williams  *    id
2790*cc9203bfSDan Williams  * @node_id: This is the remote node id that is assinged to the device if one
2791*cc9203bfSDan Williams  *    is available
2792*cc9203bfSDan Williams  *
2793*cc9203bfSDan Williams  * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2794*cc9203bfSDan Williams  * node index available.
2795*cc9203bfSDan Williams  */
2796*cc9203bfSDan Williams enum sci_status scic_sds_controller_allocate_remote_node_context(
2797*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2798*cc9203bfSDan Williams 	struct scic_sds_remote_device *sci_dev,
2799*cc9203bfSDan Williams 	u16 *node_id)
2800*cc9203bfSDan Williams {
2801*cc9203bfSDan Williams 	u16 node_index;
2802*cc9203bfSDan Williams 	u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2803*cc9203bfSDan Williams 
2804*cc9203bfSDan Williams 	node_index = scic_sds_remote_node_table_allocate_remote_node(
2805*cc9203bfSDan Williams 		&scic->available_remote_nodes, remote_node_count
2806*cc9203bfSDan Williams 		);
2807*cc9203bfSDan Williams 
2808*cc9203bfSDan Williams 	if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2809*cc9203bfSDan Williams 		scic->device_table[node_index] = sci_dev;
2810*cc9203bfSDan Williams 
2811*cc9203bfSDan Williams 		*node_id = node_index;
2812*cc9203bfSDan Williams 
2813*cc9203bfSDan Williams 		return SCI_SUCCESS;
2814*cc9203bfSDan Williams 	}
2815*cc9203bfSDan Williams 
2816*cc9203bfSDan Williams 	return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2817*cc9203bfSDan Williams }
2818*cc9203bfSDan Williams 
2819*cc9203bfSDan Williams /**
2820*cc9203bfSDan Williams  * This method frees the remote node index back to the available pool.  Once
2821*cc9203bfSDan Williams  *    this is done the remote node context buffer is no longer valid and can
2822*cc9203bfSDan Williams  *    not be used.
2823*cc9203bfSDan Williams  * @scic:
2824*cc9203bfSDan Williams  * @sci_dev:
2825*cc9203bfSDan Williams  * @node_id:
2826*cc9203bfSDan Williams  *
2827*cc9203bfSDan Williams  */
2828*cc9203bfSDan Williams void scic_sds_controller_free_remote_node_context(
2829*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2830*cc9203bfSDan Williams 	struct scic_sds_remote_device *sci_dev,
2831*cc9203bfSDan Williams 	u16 node_id)
2832*cc9203bfSDan Williams {
2833*cc9203bfSDan Williams 	u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2834*cc9203bfSDan Williams 
2835*cc9203bfSDan Williams 	if (scic->device_table[node_id] == sci_dev) {
2836*cc9203bfSDan Williams 		scic->device_table[node_id] = NULL;
2837*cc9203bfSDan Williams 
2838*cc9203bfSDan Williams 		scic_sds_remote_node_table_release_remote_node_index(
2839*cc9203bfSDan Williams 			&scic->available_remote_nodes, remote_node_count, node_id
2840*cc9203bfSDan Williams 			);
2841*cc9203bfSDan Williams 	}
2842*cc9203bfSDan Williams }
2843*cc9203bfSDan Williams 
2844*cc9203bfSDan Williams /**
2845*cc9203bfSDan Williams  * This method returns the union scu_remote_node_context for the specified remote
2846*cc9203bfSDan Williams  *    node id.
2847*cc9203bfSDan Williams  * @scic:
2848*cc9203bfSDan Williams  * @node_id:
2849*cc9203bfSDan Williams  *
2850*cc9203bfSDan Williams  * union scu_remote_node_context*
2851*cc9203bfSDan Williams  */
2852*cc9203bfSDan Williams union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
2853*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2854*cc9203bfSDan Williams 	u16 node_id
2855*cc9203bfSDan Williams 	) {
2856*cc9203bfSDan Williams 	if (
2857*cc9203bfSDan Williams 		(node_id < scic->remote_node_entries)
2858*cc9203bfSDan Williams 		&& (scic->device_table[node_id] != NULL)
2859*cc9203bfSDan Williams 		) {
2860*cc9203bfSDan Williams 		return &scic->remote_node_context_table[node_id];
2861*cc9203bfSDan Williams 	}
2862*cc9203bfSDan Williams 
2863*cc9203bfSDan Williams 	return NULL;
2864*cc9203bfSDan Williams }
2865*cc9203bfSDan Williams 
2866*cc9203bfSDan Williams /**
2867*cc9203bfSDan Williams  *
2868*cc9203bfSDan Williams  * @resposne_buffer: This is the buffer into which the D2H register FIS will be
2869*cc9203bfSDan Williams  *    constructed.
2870*cc9203bfSDan Williams  * @frame_header: This is the frame header returned by the hardware.
2871*cc9203bfSDan Williams  * @frame_buffer: This is the frame buffer returned by the hardware.
2872*cc9203bfSDan Williams  *
2873*cc9203bfSDan Williams  * This method will combind the frame header and frame buffer to create a SATA
2874*cc9203bfSDan Williams  * D2H register FIS none
2875*cc9203bfSDan Williams  */
2876*cc9203bfSDan Williams void scic_sds_controller_copy_sata_response(
2877*cc9203bfSDan Williams 	void *response_buffer,
2878*cc9203bfSDan Williams 	void *frame_header,
2879*cc9203bfSDan Williams 	void *frame_buffer)
2880*cc9203bfSDan Williams {
2881*cc9203bfSDan Williams 	memcpy(response_buffer, frame_header, sizeof(u32));
2882*cc9203bfSDan Williams 
2883*cc9203bfSDan Williams 	memcpy(response_buffer + sizeof(u32),
2884*cc9203bfSDan Williams 	       frame_buffer,
2885*cc9203bfSDan Williams 	       sizeof(struct dev_to_host_fis) - sizeof(u32));
2886*cc9203bfSDan Williams }
2887*cc9203bfSDan Williams 
2888*cc9203bfSDan Williams /**
2889*cc9203bfSDan Williams  * This method releases the frame once this is done the frame is available for
2890*cc9203bfSDan Williams  *    re-use by the hardware.  The data contained in the frame header and frame
2891*cc9203bfSDan Williams  *    buffer is no longer valid. The UF queue get pointer is only updated if UF
2892*cc9203bfSDan Williams  *    control indicates this is appropriate.
2893*cc9203bfSDan Williams  * @scic:
2894*cc9203bfSDan Williams  * @frame_index:
2895*cc9203bfSDan Williams  *
2896*cc9203bfSDan Williams  */
2897*cc9203bfSDan Williams void scic_sds_controller_release_frame(
2898*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2899*cc9203bfSDan Williams 	u32 frame_index)
2900*cc9203bfSDan Williams {
2901*cc9203bfSDan Williams 	if (scic_sds_unsolicited_frame_control_release_frame(
2902*cc9203bfSDan Williams 		    &scic->uf_control, frame_index) == true)
2903*cc9203bfSDan Williams 		writel(scic->uf_control.get,
2904*cc9203bfSDan Williams 			&scic->scu_registers->sdma.unsolicited_frame_get_pointer);
2905*cc9203bfSDan Williams }
2906*cc9203bfSDan Williams 
2907*cc9203bfSDan Williams /**
2908*cc9203bfSDan Williams  * scic_controller_start_io() - This method is called by the SCI user to
2909*cc9203bfSDan Williams  *    send/start an IO request. If the method invocation is successful, then
2910*cc9203bfSDan Williams  *    the IO request has been queued to the hardware for processing.
2911*cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start an IO
2912*cc9203bfSDan Williams  *    request.
2913*cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start an
2914*cc9203bfSDan Williams  *    IO request.
2915*cc9203bfSDan Williams  * @io_request: the handle to the io request object to start.
2916*cc9203bfSDan Williams  * @io_tag: This parameter specifies a previously allocated IO tag that the
2917*cc9203bfSDan Williams  *    user desires to be utilized for this request. This parameter is optional.
2918*cc9203bfSDan Williams  *     The user is allowed to supply SCI_CONTROLLER_INVALID_IO_TAG as the value
2919*cc9203bfSDan Williams  *    for this parameter.
2920*cc9203bfSDan Williams  *
2921*cc9203bfSDan Williams  * - IO tags are a protected resource.  It is incumbent upon the SCI Core user
2922*cc9203bfSDan Williams  * to ensure that each of the methods that may allocate or free available IO
2923*cc9203bfSDan Williams  * tags are handled in a mutually exclusive manner.  This method is one of said
2924*cc9203bfSDan Williams  * methods requiring proper critical code section protection (e.g. semaphore,
2925*cc9203bfSDan Williams  * spin-lock, etc.). - For SATA, the user is required to manage NCQ tags.  As a
2926*cc9203bfSDan Williams  * result, it is expected the user will have set the NCQ tag field in the host
2927*cc9203bfSDan Williams  * to device register FIS prior to calling this method.  There is also a
2928*cc9203bfSDan Williams  * requirement for the user to call scic_stp_io_set_ncq_tag() prior to invoking
2929*cc9203bfSDan Williams  * the scic_controller_start_io() method. scic_controller_allocate_tag() for
2930*cc9203bfSDan Williams  * more information on allocating a tag. Indicate if the controller
2931*cc9203bfSDan Williams  * successfully started the IO request. SCI_SUCCESS if the IO request was
2932*cc9203bfSDan Williams  * successfully started. Determine the failure situations and return values.
2933*cc9203bfSDan Williams  */
2934*cc9203bfSDan Williams enum sci_status scic_controller_start_io(
2935*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2936*cc9203bfSDan Williams 	struct scic_sds_remote_device *rdev,
2937*cc9203bfSDan Williams 	struct scic_sds_request *req,
2938*cc9203bfSDan Williams 	u16 io_tag)
2939*cc9203bfSDan Williams {
2940*cc9203bfSDan Williams 	enum sci_status status;
2941*cc9203bfSDan Williams 
2942*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
2943*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_READY) {
2944*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic), "invalid state to start I/O");
2945*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2946*cc9203bfSDan Williams 	}
2947*cc9203bfSDan Williams 
2948*cc9203bfSDan Williams 	status = scic_sds_remote_device_start_io(scic, rdev, req);
2949*cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2950*cc9203bfSDan Williams 		return status;
2951*cc9203bfSDan Williams 
2952*cc9203bfSDan Williams 	scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
2953*cc9203bfSDan Williams 	scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(req));
2954*cc9203bfSDan Williams 	return SCI_SUCCESS;
2955*cc9203bfSDan Williams }
2956*cc9203bfSDan Williams 
2957*cc9203bfSDan Williams /**
2958*cc9203bfSDan Williams  * scic_controller_terminate_request() - This method is called by the SCI Core
2959*cc9203bfSDan Williams  *    user to terminate an ongoing (i.e. started) core IO request.  This does
2960*cc9203bfSDan Williams  *    not abort the IO request at the target, but rather removes the IO request
2961*cc9203bfSDan Williams  *    from the host controller.
2962*cc9203bfSDan Williams  * @controller: the handle to the controller object for which to terminate a
2963*cc9203bfSDan Williams  *    request.
2964*cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to
2965*cc9203bfSDan Williams  *    terminate a request.
2966*cc9203bfSDan Williams  * @request: the handle to the io or task management request object to
2967*cc9203bfSDan Williams  *    terminate.
2968*cc9203bfSDan Williams  *
2969*cc9203bfSDan Williams  * Indicate if the controller successfully began the terminate process for the
2970*cc9203bfSDan Williams  * IO request. SCI_SUCCESS if the terminate process was successfully started
2971*cc9203bfSDan Williams  * for the request. Determine the failure situations and return values.
2972*cc9203bfSDan Williams  */
2973*cc9203bfSDan Williams enum sci_status scic_controller_terminate_request(
2974*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
2975*cc9203bfSDan Williams 	struct scic_sds_remote_device *rdev,
2976*cc9203bfSDan Williams 	struct scic_sds_request *req)
2977*cc9203bfSDan Williams {
2978*cc9203bfSDan Williams 	enum sci_status status;
2979*cc9203bfSDan Williams 
2980*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
2981*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_READY) {
2982*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
2983*cc9203bfSDan Williams 			 "invalid state to terminate request\n");
2984*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
2985*cc9203bfSDan Williams 	}
2986*cc9203bfSDan Williams 
2987*cc9203bfSDan Williams 	status = scic_sds_io_request_terminate(req);
2988*cc9203bfSDan Williams 	if (status != SCI_SUCCESS)
2989*cc9203bfSDan Williams 		return status;
2990*cc9203bfSDan Williams 
2991*cc9203bfSDan Williams 	/*
2992*cc9203bfSDan Williams 	 * Utilize the original post context command and or in the POST_TC_ABORT
2993*cc9203bfSDan Williams 	 * request sub-type.
2994*cc9203bfSDan Williams 	 */
2995*cc9203bfSDan Williams 	scic_sds_controller_post_request(scic,
2996*cc9203bfSDan Williams 		scic_sds_request_get_post_context(req) |
2997*cc9203bfSDan Williams 		SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2998*cc9203bfSDan Williams 	return SCI_SUCCESS;
2999*cc9203bfSDan Williams }
3000*cc9203bfSDan Williams 
3001*cc9203bfSDan Williams /**
3002*cc9203bfSDan Williams  * scic_controller_complete_io() - This method will perform core specific
3003*cc9203bfSDan Williams  *    completion operations for an IO request.  After this method is invoked,
3004*cc9203bfSDan Williams  *    the user should consider the IO request as invalid until it is properly
3005*cc9203bfSDan Williams  *    reused (i.e. re-constructed).
3006*cc9203bfSDan Williams  * @controller: The handle to the controller object for which to complete the
3007*cc9203bfSDan Williams  *    IO request.
3008*cc9203bfSDan Williams  * @remote_device: The handle to the remote device object for which to complete
3009*cc9203bfSDan Williams  *    the IO request.
3010*cc9203bfSDan Williams  * @io_request: the handle to the io request object to complete.
3011*cc9203bfSDan Williams  *
3012*cc9203bfSDan Williams  * - IO tags are a protected resource.  It is incumbent upon the SCI Core user
3013*cc9203bfSDan Williams  * to ensure that each of the methods that may allocate or free available IO
3014*cc9203bfSDan Williams  * tags are handled in a mutually exclusive manner.  This method is one of said
3015*cc9203bfSDan Williams  * methods requiring proper critical code section protection (e.g. semaphore,
3016*cc9203bfSDan Williams  * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
3017*cc9203bfSDan Williams  * Core user, using the scic_controller_allocate_io_tag() method, then it is
3018*cc9203bfSDan Williams  * the responsibility of the caller to invoke the scic_controller_free_io_tag()
3019*cc9203bfSDan Williams  * method to free the tag (i.e. this method will not free the IO tag). Indicate
3020*cc9203bfSDan Williams  * if the controller successfully completed the IO request. SCI_SUCCESS if the
3021*cc9203bfSDan Williams  * completion process was successful.
3022*cc9203bfSDan Williams  */
3023*cc9203bfSDan Williams enum sci_status scic_controller_complete_io(
3024*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
3025*cc9203bfSDan Williams 	struct scic_sds_remote_device *rdev,
3026*cc9203bfSDan Williams 	struct scic_sds_request *request)
3027*cc9203bfSDan Williams {
3028*cc9203bfSDan Williams 	enum sci_status status;
3029*cc9203bfSDan Williams 	u16 index;
3030*cc9203bfSDan Williams 
3031*cc9203bfSDan Williams 	switch (scic->state_machine.current_state_id) {
3032*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_STOPPING:
3033*cc9203bfSDan Williams 		/* XXX: Implement this function */
3034*cc9203bfSDan Williams 		return SCI_FAILURE;
3035*cc9203bfSDan Williams 	case SCI_BASE_CONTROLLER_STATE_READY:
3036*cc9203bfSDan Williams 		status = scic_sds_remote_device_complete_io(scic, rdev, request);
3037*cc9203bfSDan Williams 		if (status != SCI_SUCCESS)
3038*cc9203bfSDan Williams 			return status;
3039*cc9203bfSDan Williams 
3040*cc9203bfSDan Williams 		index = scic_sds_io_tag_get_index(request->io_tag);
3041*cc9203bfSDan Williams 		scic->io_request_table[index] = NULL;
3042*cc9203bfSDan Williams 		return SCI_SUCCESS;
3043*cc9203bfSDan Williams 	default:
3044*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
3045*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
3046*cc9203bfSDan Williams 	}
3047*cc9203bfSDan Williams 
3048*cc9203bfSDan Williams }
3049*cc9203bfSDan Williams 
3050*cc9203bfSDan Williams enum sci_status scic_controller_continue_io(struct scic_sds_request *sci_req)
3051*cc9203bfSDan Williams {
3052*cc9203bfSDan Williams 	struct scic_sds_controller *scic = sci_req->owning_controller;
3053*cc9203bfSDan Williams 
3054*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
3055*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_READY) {
3056*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
3057*cc9203bfSDan Williams 		return SCI_FAILURE_INVALID_STATE;
3058*cc9203bfSDan Williams 	}
3059*cc9203bfSDan Williams 
3060*cc9203bfSDan Williams 	scic->io_request_table[scic_sds_io_tag_get_index(sci_req->io_tag)] = sci_req;
3061*cc9203bfSDan Williams 	scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(sci_req));
3062*cc9203bfSDan Williams 	return SCI_SUCCESS;
3063*cc9203bfSDan Williams }
3064*cc9203bfSDan Williams 
3065*cc9203bfSDan Williams /**
3066*cc9203bfSDan Williams  * scic_controller_start_task() - This method is called by the SCIC user to
3067*cc9203bfSDan Williams  *    send/start a framework task management request.
3068*cc9203bfSDan Williams  * @controller: the handle to the controller object for which to start the task
3069*cc9203bfSDan Williams  *    management request.
3070*cc9203bfSDan Williams  * @remote_device: the handle to the remote device object for which to start
3071*cc9203bfSDan Williams  *    the task management request.
3072*cc9203bfSDan Williams  * @task_request: the handle to the task request object to start.
3073*cc9203bfSDan Williams  * @io_tag: This parameter specifies a previously allocated IO tag that the
3074*cc9203bfSDan Williams  *    user desires to be utilized for this request.  Note this not the io_tag
3075*cc9203bfSDan Williams  *    of the request being managed.  It is to be utilized for the task request
3076*cc9203bfSDan Williams  *    itself. This parameter is optional.  The user is allowed to supply
3077*cc9203bfSDan Williams  *    SCI_CONTROLLER_INVALID_IO_TAG as the value for this parameter.
3078*cc9203bfSDan Williams  *
3079*cc9203bfSDan Williams  * - IO tags are a protected resource.  It is incumbent upon the SCI Core user
3080*cc9203bfSDan Williams  * to ensure that each of the methods that may allocate or free available IO
3081*cc9203bfSDan Williams  * tags are handled in a mutually exclusive manner.  This method is one of said
3082*cc9203bfSDan Williams  * methods requiring proper critical code section protection (e.g. semaphore,
3083*cc9203bfSDan Williams  * spin-lock, etc.). - The user must synchronize this task with completion
3084*cc9203bfSDan Williams  * queue processing.  If they are not synchronized then it is possible for the
3085*cc9203bfSDan Williams  * io requests that are being managed by the task request can complete before
3086*cc9203bfSDan Williams  * starting the task request. scic_controller_allocate_tag() for more
3087*cc9203bfSDan Williams  * information on allocating a tag. Indicate if the controller successfully
3088*cc9203bfSDan Williams  * started the IO request. SCI_TASK_SUCCESS if the task request was
3089*cc9203bfSDan Williams  * successfully started. SCI_TASK_FAILURE_REQUIRES_SCSI_ABORT This value is
3090*cc9203bfSDan Williams  * returned if there is/are task(s) outstanding that require termination or
3091*cc9203bfSDan Williams  * completion before this request can succeed.
3092*cc9203bfSDan Williams  */
3093*cc9203bfSDan Williams enum sci_task_status scic_controller_start_task(
3094*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
3095*cc9203bfSDan Williams 	struct scic_sds_remote_device *rdev,
3096*cc9203bfSDan Williams 	struct scic_sds_request *req,
3097*cc9203bfSDan Williams 	u16 task_tag)
3098*cc9203bfSDan Williams {
3099*cc9203bfSDan Williams 	enum sci_status status;
3100*cc9203bfSDan Williams 
3101*cc9203bfSDan Williams 	if (scic->state_machine.current_state_id !=
3102*cc9203bfSDan Williams 	    SCI_BASE_CONTROLLER_STATE_READY) {
3103*cc9203bfSDan Williams 		dev_warn(scic_to_dev(scic),
3104*cc9203bfSDan Williams 			 "%s: SCIC Controller starting task from invalid "
3105*cc9203bfSDan Williams 			 "state\n",
3106*cc9203bfSDan Williams 			 __func__);
3107*cc9203bfSDan Williams 		return SCI_TASK_FAILURE_INVALID_STATE;
3108*cc9203bfSDan Williams 	}
3109*cc9203bfSDan Williams 
3110*cc9203bfSDan Williams 	status = scic_sds_remote_device_start_task(scic, rdev, req);
3111*cc9203bfSDan Williams 	switch (status) {
3112*cc9203bfSDan Williams 	case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
3113*cc9203bfSDan Williams 		scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
3114*cc9203bfSDan Williams 
3115*cc9203bfSDan Williams 		/*
3116*cc9203bfSDan Williams 		 * We will let framework know this task request started successfully,
3117*cc9203bfSDan Williams 		 * although core is still woring on starting the request (to post tc when
3118*cc9203bfSDan Williams 		 * RNC is resumed.)
3119*cc9203bfSDan Williams 		 */
3120*cc9203bfSDan Williams 		return SCI_SUCCESS;
3121*cc9203bfSDan Williams 	case SCI_SUCCESS:
3122*cc9203bfSDan Williams 		scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
3123*cc9203bfSDan Williams 
3124*cc9203bfSDan Williams 		scic_sds_controller_post_request(scic,
3125*cc9203bfSDan Williams 			scic_sds_request_get_post_context(req));
3126*cc9203bfSDan Williams 		break;
3127*cc9203bfSDan Williams 	default:
3128*cc9203bfSDan Williams 		break;
3129*cc9203bfSDan Williams 	}
3130*cc9203bfSDan Williams 
3131*cc9203bfSDan Williams 	return status;
3132*cc9203bfSDan Williams }
3133*cc9203bfSDan Williams 
3134*cc9203bfSDan Williams /**
3135*cc9203bfSDan Williams  * scic_controller_allocate_io_tag() - This method will allocate a tag from the
3136*cc9203bfSDan Williams  *    pool of free IO tags. Direct allocation of IO tags by the SCI Core user
3137*cc9203bfSDan Williams  *    is optional. The scic_controller_start_io() method will allocate an IO
3138*cc9203bfSDan Williams  *    tag if this method is not utilized and the tag is not supplied to the IO
3139*cc9203bfSDan Williams  *    construct routine.  Direct allocation of IO tags may provide additional
3140*cc9203bfSDan Williams  *    performance improvements in environments capable of supporting this usage
3141*cc9203bfSDan Williams  *    model.  Additionally, direct allocation of IO tags also provides
3142*cc9203bfSDan Williams  *    additional flexibility to the SCI Core user.  Specifically, the user may
3143*cc9203bfSDan Williams  *    retain IO tags across the lives of multiple IO requests.
3144*cc9203bfSDan Williams  * @controller: the handle to the controller object for which to allocate the
3145*cc9203bfSDan Williams  *    tag.
3146*cc9203bfSDan Williams  *
3147*cc9203bfSDan Williams  * IO tags are a protected resource.  It is incumbent upon the SCI Core user to
3148*cc9203bfSDan Williams  * ensure that each of the methods that may allocate or free available IO tags
3149*cc9203bfSDan Williams  * are handled in a mutually exclusive manner.  This method is one of said
3150*cc9203bfSDan Williams  * methods requiring proper critical code section protection (e.g. semaphore,
3151*cc9203bfSDan Williams  * spin-lock, etc.). An unsigned integer representing an available IO tag.
3152*cc9203bfSDan Williams  * SCI_CONTROLLER_INVALID_IO_TAG This value is returned if there are no
3153*cc9203bfSDan Williams  * currently available tags to be allocated. All return other values indicate a
3154*cc9203bfSDan Williams  * legitimate tag.
3155*cc9203bfSDan Williams  */
3156*cc9203bfSDan Williams u16 scic_controller_allocate_io_tag(
3157*cc9203bfSDan Williams 	struct scic_sds_controller *scic)
3158*cc9203bfSDan Williams {
3159*cc9203bfSDan Williams 	u16 task_context;
3160*cc9203bfSDan Williams 	u16 sequence_count;
3161*cc9203bfSDan Williams 
3162*cc9203bfSDan Williams 	if (!sci_pool_empty(scic->tci_pool)) {
3163*cc9203bfSDan Williams 		sci_pool_get(scic->tci_pool, task_context);
3164*cc9203bfSDan Williams 
3165*cc9203bfSDan Williams 		sequence_count = scic->io_request_sequence[task_context];
3166*cc9203bfSDan Williams 
3167*cc9203bfSDan Williams 		return scic_sds_io_tag_construct(sequence_count, task_context);
3168*cc9203bfSDan Williams 	}
3169*cc9203bfSDan Williams 
3170*cc9203bfSDan Williams 	return SCI_CONTROLLER_INVALID_IO_TAG;
3171*cc9203bfSDan Williams }
3172*cc9203bfSDan Williams 
3173*cc9203bfSDan Williams /**
3174*cc9203bfSDan Williams  * scic_controller_free_io_tag() - This method will free an IO tag to the pool
3175*cc9203bfSDan Williams  *    of free IO tags. This method provides the SCI Core user more flexibility
3176*cc9203bfSDan Williams  *    with regards to IO tags.  The user may desire to keep an IO tag after an
3177*cc9203bfSDan Williams  *    IO request has completed, because they plan on re-using the tag for a
3178*cc9203bfSDan Williams  *    subsequent IO request.  This method is only legal if the tag was
3179*cc9203bfSDan Williams  *    allocated via scic_controller_allocate_io_tag().
3180*cc9203bfSDan Williams  * @controller: This parameter specifies the handle to the controller object
3181*cc9203bfSDan Williams  *    for which to free/return the tag.
3182*cc9203bfSDan Williams  * @io_tag: This parameter represents the tag to be freed to the pool of
3183*cc9203bfSDan Williams  *    available tags.
3184*cc9203bfSDan Williams  *
3185*cc9203bfSDan Williams  * - IO tags are a protected resource.  It is incumbent upon the SCI Core user
3186*cc9203bfSDan Williams  * to ensure that each of the methods that may allocate or free available IO
3187*cc9203bfSDan Williams  * tags are handled in a mutually exclusive manner.  This method is one of said
3188*cc9203bfSDan Williams  * methods requiring proper critical code section protection (e.g. semaphore,
3189*cc9203bfSDan Williams  * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
3190*cc9203bfSDan Williams  * Core user, using the scic_controller_allocate_io_tag() method, then it is
3191*cc9203bfSDan Williams  * the responsibility of the caller to invoke this method to free the tag. This
3192*cc9203bfSDan Williams  * method returns an indication of whether the tag was successfully put back
3193*cc9203bfSDan Williams  * (freed) to the pool of available tags. SCI_SUCCESS This return value
3194*cc9203bfSDan Williams  * indicates the tag was successfully placed into the pool of available IO
3195*cc9203bfSDan Williams  * tags. SCI_FAILURE_INVALID_IO_TAG This value is returned if the supplied tag
3196*cc9203bfSDan Williams  * is not a valid IO tag value.
3197*cc9203bfSDan Williams  */
3198*cc9203bfSDan Williams enum sci_status scic_controller_free_io_tag(
3199*cc9203bfSDan Williams 	struct scic_sds_controller *scic,
3200*cc9203bfSDan Williams 	u16 io_tag)
3201*cc9203bfSDan Williams {
3202*cc9203bfSDan Williams 	u16 sequence;
3203*cc9203bfSDan Williams 	u16 index;
3204*cc9203bfSDan Williams 
3205*cc9203bfSDan Williams 	BUG_ON(io_tag == SCI_CONTROLLER_INVALID_IO_TAG);
3206*cc9203bfSDan Williams 
3207*cc9203bfSDan Williams 	sequence = scic_sds_io_tag_get_sequence(io_tag);
3208*cc9203bfSDan Williams 	index    = scic_sds_io_tag_get_index(io_tag);
3209*cc9203bfSDan Williams 
3210*cc9203bfSDan Williams 	if (!sci_pool_full(scic->tci_pool)) {
3211*cc9203bfSDan Williams 		if (sequence == scic->io_request_sequence[index]) {
3212*cc9203bfSDan Williams 			scic_sds_io_sequence_increment(
3213*cc9203bfSDan Williams 				scic->io_request_sequence[index]);
3214*cc9203bfSDan Williams 
3215*cc9203bfSDan Williams 			sci_pool_put(scic->tci_pool, index);
3216*cc9203bfSDan Williams 
3217*cc9203bfSDan Williams 			return SCI_SUCCESS;
3218*cc9203bfSDan Williams 		}
3219*cc9203bfSDan Williams 	}
3220*cc9203bfSDan Williams 
3221*cc9203bfSDan Williams 	return SCI_FAILURE_INVALID_IO_TAG;
3222*cc9203bfSDan Williams }
3223*cc9203bfSDan Williams 
3224*cc9203bfSDan Williams 
3225