blob: bb298f8f609ad53411db17f724a4ee378cef552a [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Dan Williamsac668c62011-06-07 18:50:55 -070055#include <linux/circ_buf.h>
Dan Williamscc9203b2011-05-08 17:34:44 -070056#include <linux/device.h>
57#include <scsi/sas.h>
58#include "host.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070059#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070060#include "port.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070061#include "host.h"
Dan Williamsd044af12011-03-08 09:52:49 -080062#include "probe_roms.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070063#include "remote_device.h"
64#include "request.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070065#include "scu_completion_codes.h"
66#include "scu_event_codes.h"
Dan Williams63a3a152011-05-08 21:36:46 -070067#include "registers.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070068#include "scu_remote_node_context.h"
69#include "scu_task_context.h"
70#include "scu_unsolicited_frame.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070071
Dan Williamscc9203b2011-05-08 17:34:44 -070072#define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
73
Dan Williams7c78da32011-06-01 16:00:01 -070074#define smu_max_ports(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070075 (\
76 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
77 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
78 )
79
Dan Williams7c78da32011-06-01 16:00:01 -070080#define smu_max_task_contexts(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070081 (\
82 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
83 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
84 )
85
Dan Williams7c78da32011-06-01 16:00:01 -070086#define smu_max_rncs(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070087 (\
88 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
89 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
90 )
91
Dan Williamscc9203b2011-05-08 17:34:44 -070092#define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
93
94/**
95 *
96 *
97 * The number of milliseconds to wait while a given phy is consuming power
98 * before allowing another set of phys to consume power. Ultimately, this will
99 * be specified by OEM parameter.
100 */
101#define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
102
103/**
104 * NORMALIZE_PUT_POINTER() -
105 *
106 * This macro will normalize the completion queue put pointer so its value can
107 * be used as an array inde
108 */
109#define NORMALIZE_PUT_POINTER(x) \
110 ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
111
112
113/**
114 * NORMALIZE_EVENT_POINTER() -
115 *
116 * This macro will normalize the completion queue event entry so its value can
117 * be used as an index.
118 */
119#define NORMALIZE_EVENT_POINTER(x) \
120 (\
121 ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
122 >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
123 )
124
125/**
Dan Williamscc9203b2011-05-08 17:34:44 -0700126 * NORMALIZE_GET_POINTER() -
127 *
128 * This macro will normalize the completion queue get pointer so its value can
129 * be used as an index into an array
130 */
131#define NORMALIZE_GET_POINTER(x) \
132 ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
133
134/**
135 * NORMALIZE_GET_POINTER_CYCLE_BIT() -
136 *
137 * This macro will normalize the completion queue cycle pointer so it matches
138 * the completion queue cycle bit
139 */
140#define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
141 ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
142
143/**
144 * COMPLETION_QUEUE_CYCLE_BIT() -
145 *
146 * This macro will return the cycle bit of the completion queue entry
147 */
148#define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
149
Edmund Nadolski12ef6542011-06-02 00:10:50 +0000150/* Init the state machine and call the state entry function (if any) */
151void sci_init_sm(struct sci_base_state_machine *sm,
152 const struct sci_base_state *state_table, u32 initial_state)
153{
154 sci_state_transition_t handler;
155
156 sm->initial_state_id = initial_state;
157 sm->previous_state_id = initial_state;
158 sm->current_state_id = initial_state;
159 sm->state_table = state_table;
160
161 handler = sm->state_table[initial_state].enter_state;
162 if (handler)
163 handler(sm);
164}
165
166/* Call the state exit fn, update the current state, call the state entry fn */
167void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
168{
169 sci_state_transition_t handler;
170
171 handler = sm->state_table[sm->current_state_id].exit_state;
172 if (handler)
173 handler(sm);
174
175 sm->previous_state_id = sm->current_state_id;
176 sm->current_state_id = next_state;
177
178 handler = sm->state_table[sm->current_state_id].enter_state;
179 if (handler)
180 handler(sm);
181}
182
Dan Williamscc9203b2011-05-08 17:34:44 -0700183static bool scic_sds_controller_completion_queue_has_entries(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700184 struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700185{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700186 u32 get_value = ihost->completion_queue_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700187 u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
188
189 if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700190 COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
Dan Williamscc9203b2011-05-08 17:34:44 -0700191 return true;
192
193 return false;
194}
195
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700196static bool scic_sds_controller_isr(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700197{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700198 if (scic_sds_controller_completion_queue_has_entries(ihost)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700199 return true;
200 } else {
201 /*
202 * we have a spurious interrupt it could be that we have already
203 * emptied the completion queue from a previous interrupt */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700204 writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700205
206 /*
207 * There is a race in the hardware that could cause us not to be notified
208 * of an interrupt completion if we do not take this step. We will mask
209 * then unmask the interrupts so if there is another interrupt pending
210 * the clearing of the interrupt source we get the next interrupt message. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700211 writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
212 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700213 }
214
215 return false;
216}
217
Dan Williamsc7ef4032011-02-18 09:25:05 -0800218irqreturn_t isci_msix_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700219{
Dan Williamsc7ef4032011-02-18 09:25:05 -0800220 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700221
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700222 if (scic_sds_controller_isr(ihost))
Dan Williams0cf89d12011-02-18 09:25:07 -0800223 tasklet_schedule(&ihost->completion_tasklet);
Dan Williams6f231dd2011-07-02 22:56:22 -0700224
Dan Williamsc7ef4032011-02-18 09:25:05 -0800225 return IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700226}
227
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700228static bool scic_sds_controller_error_isr(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700229{
230 u32 interrupt_status;
231
232 interrupt_status =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700233 readl(&ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700234 interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
235
236 if (interrupt_status != 0) {
237 /*
238 * There is an error interrupt pending so let it through and handle
239 * in the callback */
240 return true;
241 }
242
243 /*
244 * There is a race in the hardware that could cause us not to be notified
245 * of an interrupt completion if we do not take this step. We will mask
246 * then unmask the error interrupts so if there was another interrupt
247 * pending we will be notified.
248 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700249 writel(0xff, &ihost->smu_registers->interrupt_mask);
250 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700251
252 return false;
253}
254
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700255static void scic_sds_controller_task_completion(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700256 u32 completion_entry)
257{
Dan Williamsdb056252011-06-17 14:18:39 -0700258 u32 index = SCU_GET_COMPLETION_INDEX(completion_entry);
Dan Williamsdb056252011-06-17 14:18:39 -0700259 struct isci_request *ireq = ihost->reqs[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700260
261 /* Make sure that we really want to process this IO request */
Dan Williamsdb056252011-06-17 14:18:39 -0700262 if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
Dan Williams5076a1a2011-06-27 14:57:03 -0700263 ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700264 ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
Dan Williamscc9203b2011-05-08 17:34:44 -0700265 /* Yep this is a valid io request pass it along to the io request handler */
Dan Williams5076a1a2011-06-27 14:57:03 -0700266 scic_sds_io_request_tc_completion(ireq, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700267}
268
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700269static void scic_sds_controller_sdma_completion(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700270 u32 completion_entry)
271{
272 u32 index;
Dan Williams5076a1a2011-06-27 14:57:03 -0700273 struct isci_request *ireq;
Dan Williams78a6f062011-06-30 16:31:37 -0700274 struct isci_remote_device *idev;
Dan Williamscc9203b2011-05-08 17:34:44 -0700275
276 index = SCU_GET_COMPLETION_INDEX(completion_entry);
277
278 switch (scu_get_command_request_type(completion_entry)) {
279 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
280 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700281 ireq = ihost->reqs[index];
282 dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700283 __func__, completion_entry, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -0700284 /* @todo For a post TC operation we need to fail the IO
285 * request
286 */
287 break;
Dan Williamscc9203b2011-05-08 17:34:44 -0700288 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
289 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
290 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700291 idev = ihost->device_table[index];
292 dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
Dan Williams78a6f062011-06-30 16:31:37 -0700293 __func__, completion_entry, idev);
Dan Williamscc9203b2011-05-08 17:34:44 -0700294 /* @todo For a port RNC operation we need to fail the
295 * device
296 */
297 break;
Dan Williamscc9203b2011-05-08 17:34:44 -0700298 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700299 dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
Dan Williams5076a1a2011-06-27 14:57:03 -0700300 __func__, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700301 break;
Dan Williamscc9203b2011-05-08 17:34:44 -0700302 }
303}
304
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700305static void scic_sds_controller_unsolicited_frame(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700306 u32 completion_entry)
307{
308 u32 index;
309 u32 frame_index;
310
Dan Williamscc9203b2011-05-08 17:34:44 -0700311 struct scu_unsolicited_frame_header *frame_header;
Dan Williams85280952011-06-28 15:05:53 -0700312 struct isci_phy *iphy;
Dan Williams78a6f062011-06-30 16:31:37 -0700313 struct isci_remote_device *idev;
Dan Williamscc9203b2011-05-08 17:34:44 -0700314
315 enum sci_status result = SCI_FAILURE;
316
317 frame_index = SCU_GET_FRAME_INDEX(completion_entry);
318
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700319 frame_header = ihost->uf_control.buffers.array[frame_index].header;
320 ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
Dan Williamscc9203b2011-05-08 17:34:44 -0700321
322 if (SCU_GET_FRAME_ERROR(completion_entry)) {
323 /*
324 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
325 * / this cause a problem? We expect the phy initialization will
326 * / fail if there is an error in the frame. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700327 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700328 return;
329 }
330
331 if (frame_header->is_address_frame) {
332 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
Dan Williams85280952011-06-28 15:05:53 -0700333 iphy = &ihost->phys[index];
334 result = scic_sds_phy_frame_handler(iphy, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700335 } else {
336
337 index = SCU_GET_COMPLETION_INDEX(completion_entry);
338
339 if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
340 /*
341 * This is a signature fis or a frame from a direct attached SATA
342 * device that has not yet been created. In either case forwared
343 * the frame to the PE and let it take care of the frame data. */
344 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
Dan Williams85280952011-06-28 15:05:53 -0700345 iphy = &ihost->phys[index];
346 result = scic_sds_phy_frame_handler(iphy, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700347 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700348 if (index < ihost->remote_node_entries)
349 idev = ihost->device_table[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700350 else
Dan Williams78a6f062011-06-30 16:31:37 -0700351 idev = NULL;
Dan Williamscc9203b2011-05-08 17:34:44 -0700352
Dan Williams78a6f062011-06-30 16:31:37 -0700353 if (idev != NULL)
354 result = scic_sds_remote_device_frame_handler(idev, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700355 else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700356 scic_sds_controller_release_frame(ihost, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700357 }
358 }
359
360 if (result != SCI_SUCCESS) {
361 /*
362 * / @todo Is there any reason to report some additional error message
363 * / when we get this failure notifiction? */
364 }
365}
366
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700367static void scic_sds_controller_event_completion(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700368 u32 completion_entry)
369{
Dan Williams78a6f062011-06-30 16:31:37 -0700370 struct isci_remote_device *idev;
Dan Williams5076a1a2011-06-27 14:57:03 -0700371 struct isci_request *ireq;
Dan Williams85280952011-06-28 15:05:53 -0700372 struct isci_phy *iphy;
Dan Williamscc9203b2011-05-08 17:34:44 -0700373 u32 index;
374
375 index = SCU_GET_COMPLETION_INDEX(completion_entry);
376
377 switch (scu_get_event_type(completion_entry)) {
378 case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
379 /* / @todo The driver did something wrong and we need to fix the condtion. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700380 dev_err(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700381 "%s: SCIC Controller 0x%p received SMU command error "
382 "0x%x\n",
383 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700384 ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700385 completion_entry);
386 break;
387
388 case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
389 case SCU_EVENT_TYPE_SMU_ERROR:
390 case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
391 /*
392 * / @todo This is a hardware failure and its likely that we want to
393 * / reset the controller. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700394 dev_err(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700395 "%s: SCIC Controller 0x%p received fatal controller "
396 "event 0x%x\n",
397 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700398 ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700399 completion_entry);
400 break;
401
402 case SCU_EVENT_TYPE_TRANSPORT_ERROR:
Dan Williams5076a1a2011-06-27 14:57:03 -0700403 ireq = ihost->reqs[index];
404 scic_sds_io_request_event_handler(ireq, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700405 break;
406
407 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
408 switch (scu_get_event_specifier(completion_entry)) {
409 case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
410 case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
Dan Williams5076a1a2011-06-27 14:57:03 -0700411 ireq = ihost->reqs[index];
412 if (ireq != NULL)
413 scic_sds_io_request_event_handler(ireq, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700414 else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700415 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700416 "%s: SCIC Controller 0x%p received "
417 "event 0x%x for io request object "
418 "that doesnt exist.\n",
419 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700420 ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700421 completion_entry);
422
423 break;
424
425 case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700426 idev = ihost->device_table[index];
Dan Williams78a6f062011-06-30 16:31:37 -0700427 if (idev != NULL)
428 scic_sds_remote_device_event_handler(idev, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700429 else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700430 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700431 "%s: SCIC Controller 0x%p received "
432 "event 0x%x for remote device object "
433 "that doesnt exist.\n",
434 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700435 ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700436 completion_entry);
437
438 break;
439 }
440 break;
441
442 case SCU_EVENT_TYPE_BROADCAST_CHANGE:
443 /*
444 * direct the broadcast change event to the phy first and then let
445 * the phy redirect the broadcast change to the port object */
446 case SCU_EVENT_TYPE_ERR_CNT_EVENT:
447 /*
448 * direct error counter event to the phy object since that is where
449 * we get the event notification. This is a type 4 event. */
450 case SCU_EVENT_TYPE_OSSP_EVENT:
451 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
Dan Williams85280952011-06-28 15:05:53 -0700452 iphy = &ihost->phys[index];
453 scic_sds_phy_event_handler(iphy, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700454 break;
455
456 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
457 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
458 case SCU_EVENT_TYPE_RNC_OPS_MISC:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700459 if (index < ihost->remote_node_entries) {
460 idev = ihost->device_table[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700461
Dan Williams78a6f062011-06-30 16:31:37 -0700462 if (idev != NULL)
463 scic_sds_remote_device_event_handler(idev, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700464 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700465 dev_err(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700466 "%s: SCIC Controller 0x%p received event 0x%x "
467 "for remote device object 0x%0x that doesnt "
468 "exist.\n",
469 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700470 ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700471 completion_entry,
472 index);
473
474 break;
475
476 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700477 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700478 "%s: SCIC Controller received unknown event code %x\n",
479 __func__,
480 completion_entry);
481 break;
482 }
483}
484
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700485static void scic_sds_controller_process_completions(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700486{
487 u32 completion_count = 0;
488 u32 completion_entry;
489 u32 get_index;
490 u32 get_cycle;
Dan Williams994a9302011-06-09 16:04:28 -0700491 u32 event_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700492 u32 event_cycle;
493
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700494 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700495 "%s: completion queue begining get:0x%08x\n",
496 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700497 ihost->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700498
499 /* Get the component parts of the completion queue */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700500 get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
501 get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700502
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700503 event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
504 event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700505
506 while (
507 NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700508 == COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
Dan Williamscc9203b2011-05-08 17:34:44 -0700509 ) {
510 completion_count++;
511
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700512 completion_entry = ihost->completion_queue[get_index];
Dan Williams994a9302011-06-09 16:04:28 -0700513
514 /* increment the get pointer and check for rollover to toggle the cycle bit */
515 get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
516 (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
517 get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
Dan Williamscc9203b2011-05-08 17:34:44 -0700518
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700519 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700520 "%s: completion queue entry:0x%08x\n",
521 __func__,
522 completion_entry);
523
524 switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
525 case SCU_COMPLETION_TYPE_TASK:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700526 scic_sds_controller_task_completion(ihost, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700527 break;
528
529 case SCU_COMPLETION_TYPE_SDMA:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700530 scic_sds_controller_sdma_completion(ihost, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700531 break;
532
533 case SCU_COMPLETION_TYPE_UFI:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700534 scic_sds_controller_unsolicited_frame(ihost, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700535 break;
536
537 case SCU_COMPLETION_TYPE_EVENT:
Dan Williams994a9302011-06-09 16:04:28 -0700538 case SCU_COMPLETION_TYPE_NOTIFY: {
539 event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
540 (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
541 event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
542
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700543 scic_sds_controller_event_completion(ihost, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700544 break;
Dan Williams994a9302011-06-09 16:04:28 -0700545 }
Dan Williamscc9203b2011-05-08 17:34:44 -0700546 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700547 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700548 "%s: SCIC Controller received unknown "
549 "completion type %x\n",
550 __func__,
551 completion_entry);
552 break;
553 }
554 }
555
556 /* Update the get register if we completed one or more entries */
557 if (completion_count > 0) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700558 ihost->completion_queue_get =
Dan Williamscc9203b2011-05-08 17:34:44 -0700559 SMU_CQGR_GEN_BIT(ENABLE) |
560 SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
561 event_cycle |
Dan Williams994a9302011-06-09 16:04:28 -0700562 SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
Dan Williamscc9203b2011-05-08 17:34:44 -0700563 get_cycle |
564 SMU_CQGR_GEN_VAL(POINTER, get_index);
565
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700566 writel(ihost->completion_queue_get,
567 &ihost->smu_registers->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700568
569 }
570
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700571 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700572 "%s: completion queue ending get:0x%08x\n",
573 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700574 ihost->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700575
576}
577
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700578static void scic_sds_controller_error_handler(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700579{
580 u32 interrupt_status;
581
582 interrupt_status =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700583 readl(&ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700584
585 if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700586 scic_sds_controller_completion_queue_has_entries(ihost)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700587
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700588 scic_sds_controller_process_completions(ihost);
589 writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700590 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700591 dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
Dan Williamscc9203b2011-05-08 17:34:44 -0700592 interrupt_status);
593
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700594 sci_change_state(&ihost->sm, SCIC_FAILED);
Dan Williamscc9203b2011-05-08 17:34:44 -0700595
596 return;
597 }
598
599 /* If we dont process any completions I am not sure that we want to do this.
600 * We are in the middle of a hardware fault and should probably be reset.
601 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700602 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700603}
604
Dan Williamsc7ef4032011-02-18 09:25:05 -0800605irqreturn_t isci_intx_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700606{
Dan Williams6f231dd2011-07-02 22:56:22 -0700607 irqreturn_t ret = IRQ_NONE;
Dan Williams31e824e2011-04-19 12:32:51 -0700608 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700609
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700610 if (scic_sds_controller_isr(ihost)) {
611 writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
Dan Williams31e824e2011-04-19 12:32:51 -0700612 tasklet_schedule(&ihost->completion_tasklet);
613 ret = IRQ_HANDLED;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700614 } else if (scic_sds_controller_error_isr(ihost)) {
Dan Williams31e824e2011-04-19 12:32:51 -0700615 spin_lock(&ihost->scic_lock);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700616 scic_sds_controller_error_handler(ihost);
Dan Williams31e824e2011-04-19 12:32:51 -0700617 spin_unlock(&ihost->scic_lock);
618 ret = IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700619 }
Dan Williams92f4f0f2011-02-18 09:25:11 -0800620
Dan Williams6f231dd2011-07-02 22:56:22 -0700621 return ret;
622}
623
Dan Williams92f4f0f2011-02-18 09:25:11 -0800624irqreturn_t isci_error_isr(int vec, void *data)
625{
626 struct isci_host *ihost = data;
Dan Williams92f4f0f2011-02-18 09:25:11 -0800627
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700628 if (scic_sds_controller_error_isr(ihost))
629 scic_sds_controller_error_handler(ihost);
Dan Williams92f4f0f2011-02-18 09:25:11 -0800630
631 return IRQ_HANDLED;
632}
Dan Williams6f231dd2011-07-02 22:56:22 -0700633
634/**
635 * isci_host_start_complete() - This function is called by the core library,
636 * through the ISCI Module, to indicate controller start status.
637 * @isci_host: This parameter specifies the ISCI host object
638 * @completion_status: This parameter specifies the completion status from the
639 * core library.
640 *
641 */
Dan Williamscc9203b2011-05-08 17:34:44 -0700642static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700643{
Dan Williams0cf89d12011-02-18 09:25:07 -0800644 if (completion_status != SCI_SUCCESS)
645 dev_info(&ihost->pdev->dev,
646 "controller start timed out, continuing...\n");
647 isci_host_change_state(ihost, isci_ready);
648 clear_bit(IHOST_START_PENDING, &ihost->flags);
649 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700650}
651
Dan Williamsc7ef4032011-02-18 09:25:05 -0800652int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
Dan Williams6f231dd2011-07-02 22:56:22 -0700653{
Dan Williams4393aa42011-03-31 13:10:44 -0700654 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williams6f231dd2011-07-02 22:56:22 -0700655
Edmund Nadolski77950f52011-02-18 09:25:09 -0800656 if (test_bit(IHOST_START_PENDING, &ihost->flags))
Dan Williams6f231dd2011-07-02 22:56:22 -0700657 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700658
Edmund Nadolski77950f52011-02-18 09:25:09 -0800659 /* todo: use sas_flush_discovery once it is upstream */
660 scsi_flush_work(shost);
661
662 scsi_flush_work(shost);
Dan Williams6f231dd2011-07-02 22:56:22 -0700663
Dan Williams0cf89d12011-02-18 09:25:07 -0800664 dev_dbg(&ihost->pdev->dev,
665 "%s: ihost->status = %d, time = %ld\n",
666 __func__, isci_host_get_state(ihost), time);
Dan Williams6f231dd2011-07-02 22:56:22 -0700667
Dan Williams6f231dd2011-07-02 22:56:22 -0700668 return 1;
669
670}
671
Dan Williamscc9203b2011-05-08 17:34:44 -0700672/**
673 * scic_controller_get_suggested_start_timeout() - This method returns the
674 * suggested scic_controller_start() timeout amount. The user is free to
675 * use any timeout value, but this method provides the suggested minimum
676 * start timeout value. The returned value is based upon empirical
677 * information determined as a result of interoperability testing.
678 * @controller: the handle to the controller object for which to return the
679 * suggested start timeout.
680 *
681 * This method returns the number of milliseconds for the suggested start
682 * operation timeout.
683 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700684static u32 scic_controller_get_suggested_start_timeout(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700685{
686 /* Validate the user supplied parameters. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700687 if (!ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700688 return 0;
689
690 /*
691 * The suggested minimum timeout value for a controller start operation:
692 *
693 * Signature FIS Timeout
694 * + Phy Start Timeout
695 * + Number of Phy Spin Up Intervals
696 * ---------------------------------
697 * Number of milliseconds for the controller start operation.
698 *
699 * NOTE: The number of phy spin up intervals will be equivalent
700 * to the number of phys divided by the number phys allowed
701 * per interval - 1 (once OEM parameters are supported).
702 * Currently we assume only 1 phy per interval. */
703
704 return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
705 + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
706 + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
707}
708
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700709static void scic_controller_enable_interrupts(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700710{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700711 BUG_ON(ihost->smu_registers == NULL);
712 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700713}
714
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700715void scic_controller_disable_interrupts(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700716{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700717 BUG_ON(ihost->smu_registers == NULL);
718 writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700719}
720
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700721static void scic_sds_controller_enable_port_task_scheduler(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700722{
723 u32 port_task_scheduler_value;
724
725 port_task_scheduler_value =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700726 readl(&ihost->scu_registers->peg0.ptsg.control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700727 port_task_scheduler_value |=
728 (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
729 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
730 writel(port_task_scheduler_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700731 &ihost->scu_registers->peg0.ptsg.control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700732}
733
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700734static void scic_sds_controller_assign_task_entries(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700735{
736 u32 task_assignment;
737
738 /*
739 * Assign all the TCs to function 0
740 * TODO: Do we actually need to read this register to write it back?
741 */
742
743 task_assignment =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700744 readl(&ihost->smu_registers->task_context_assignment[0]);
Dan Williamscc9203b2011-05-08 17:34:44 -0700745
746 task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700747 (SMU_TCA_GEN_VAL(ENDING, ihost->task_context_entries - 1)) |
Dan Williamscc9203b2011-05-08 17:34:44 -0700748 (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
749
750 writel(task_assignment,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700751 &ihost->smu_registers->task_context_assignment[0]);
Dan Williamscc9203b2011-05-08 17:34:44 -0700752
753}
754
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700755static void scic_sds_controller_initialize_completion_queue(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700756{
757 u32 index;
758 u32 completion_queue_control_value;
759 u32 completion_queue_get_value;
760 u32 completion_queue_put_value;
761
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700762 ihost->completion_queue_get = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -0700763
Dan Williams7c78da32011-06-01 16:00:01 -0700764 completion_queue_control_value =
765 (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
766 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
Dan Williamscc9203b2011-05-08 17:34:44 -0700767
768 writel(completion_queue_control_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700769 &ihost->smu_registers->completion_queue_control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700770
771
772 /* Set the completion queue get pointer and enable the queue */
773 completion_queue_get_value = (
774 (SMU_CQGR_GEN_VAL(POINTER, 0))
775 | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
776 | (SMU_CQGR_GEN_BIT(ENABLE))
777 | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
778 );
779
780 writel(completion_queue_get_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700781 &ihost->smu_registers->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700782
783 /* Set the completion queue put pointer */
784 completion_queue_put_value = (
785 (SMU_CQPR_GEN_VAL(POINTER, 0))
786 | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
787 );
788
789 writel(completion_queue_put_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700790 &ihost->smu_registers->completion_queue_put);
Dan Williamscc9203b2011-05-08 17:34:44 -0700791
792 /* Initialize the cycle bit of the completion queue entries */
Dan Williams7c78da32011-06-01 16:00:01 -0700793 for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700794 /*
795 * If get.cycle_bit != completion_queue.cycle_bit
796 * its not a valid completion queue entry
797 * so at system start all entries are invalid */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700798 ihost->completion_queue[index] = 0x80000000;
Dan Williamscc9203b2011-05-08 17:34:44 -0700799 }
800}
801
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700802static void scic_sds_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700803{
804 u32 frame_queue_control_value;
805 u32 frame_queue_get_value;
806 u32 frame_queue_put_value;
807
808 /* Write the queue size */
809 frame_queue_control_value =
Dan Williams7c78da32011-06-01 16:00:01 -0700810 SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
Dan Williamscc9203b2011-05-08 17:34:44 -0700811
812 writel(frame_queue_control_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700813 &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700814
815 /* Setup the get pointer for the unsolicited frame queue */
816 frame_queue_get_value = (
817 SCU_UFQGP_GEN_VAL(POINTER, 0)
818 | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
819 );
820
821 writel(frame_queue_get_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700822 &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -0700823 /* Setup the put pointer for the unsolicited frame queue */
824 frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
825 writel(frame_queue_put_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700826 &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -0700827}
828
829/**
830 * This method will attempt to transition into the ready state for the
831 * controller and indicate that the controller start operation has completed
832 * if all criteria are met.
833 * @scic: This parameter indicates the controller object for which
834 * to transition to ready.
835 * @status: This parameter indicates the status value to be pass into the call
836 * to scic_cb_controller_start_complete().
837 *
838 * none.
839 */
840static void scic_sds_controller_transition_to_ready(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700841 struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700842 enum sci_status status)
843{
Dan Williamscc9203b2011-05-08 17:34:44 -0700844
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700845 if (ihost->sm.current_state_id == SCIC_STARTING) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700846 /*
847 * We move into the ready state, because some of the phys/ports
848 * may be up and operational.
849 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700850 sci_change_state(&ihost->sm, SCIC_READY);
Dan Williamscc9203b2011-05-08 17:34:44 -0700851
852 isci_host_start_complete(ihost, status);
853 }
854}
855
Dan Williams85280952011-06-28 15:05:53 -0700856static bool is_phy_starting(struct isci_phy *iphy)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000857{
858 enum scic_sds_phy_states state;
859
Dan Williams85280952011-06-28 15:05:53 -0700860 state = iphy->sm.current_state_id;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000861 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000862 case SCI_PHY_STARTING:
863 case SCI_PHY_SUB_INITIAL:
864 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
865 case SCI_PHY_SUB_AWAIT_IAF_UF:
866 case SCI_PHY_SUB_AWAIT_SAS_POWER:
867 case SCI_PHY_SUB_AWAIT_SATA_POWER:
868 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
869 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
870 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
871 case SCI_PHY_SUB_FINAL:
Adam Gruchala4a33c522011-05-10 23:54:23 +0000872 return true;
873 default:
874 return false;
875 }
876}
877
Dan Williamscc9203b2011-05-08 17:34:44 -0700878/**
879 * scic_sds_controller_start_next_phy - start phy
880 * @scic: controller
881 *
882 * If all the phys have been started, then attempt to transition the
883 * controller to the READY state and inform the user
884 * (scic_cb_controller_start_complete()).
885 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700886static enum sci_status scic_sds_controller_start_next_phy(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700887{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700888 struct scic_sds_oem_params *oem = &ihost->oem_parameters.sds1;
Dan Williams85280952011-06-28 15:05:53 -0700889 struct isci_phy *iphy;
Dan Williamscc9203b2011-05-08 17:34:44 -0700890 enum sci_status status;
891
892 status = SCI_SUCCESS;
893
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700894 if (ihost->phy_startup_timer_pending)
Dan Williamscc9203b2011-05-08 17:34:44 -0700895 return status;
896
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700897 if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700898 bool is_controller_start_complete = true;
899 u32 state;
900 u8 index;
901
902 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700903 iphy = &ihost->phys[index];
904 state = iphy->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -0700905
Dan Williams85280952011-06-28 15:05:53 -0700906 if (!phy_get_non_dummy_port(iphy))
Dan Williamscc9203b2011-05-08 17:34:44 -0700907 continue;
908
909 /* The controller start operation is complete iff:
910 * - all links have been given an opportunity to start
911 * - have no indication of a connected device
912 * - have an indication of a connected device and it has
913 * finished the link training process.
914 */
Dan Williams85280952011-06-28 15:05:53 -0700915 if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
916 (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
917 (iphy->is_in_link_training == true && is_phy_starting(iphy))) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700918 is_controller_start_complete = false;
919 break;
920 }
921 }
922
923 /*
924 * The controller has successfully finished the start process.
925 * Inform the SCI Core user and transition to the READY state. */
926 if (is_controller_start_complete == true) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700927 scic_sds_controller_transition_to_ready(ihost, SCI_SUCCESS);
928 sci_del_timer(&ihost->phy_timer);
929 ihost->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -0700930 }
931 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700932 iphy = &ihost->phys[ihost->next_phy_to_start];
Dan Williamscc9203b2011-05-08 17:34:44 -0700933
934 if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dan Williams85280952011-06-28 15:05:53 -0700935 if (phy_get_non_dummy_port(iphy) == NULL) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700936 ihost->next_phy_to_start++;
Dan Williamscc9203b2011-05-08 17:34:44 -0700937
938 /* Caution recursion ahead be forwarned
939 *
940 * The PHY was never added to a PORT in MPC mode
941 * so start the next phy in sequence This phy
942 * will never go link up and will not draw power
943 * the OEM parameters either configured the phy
944 * incorrectly for the PORT or it was never
945 * assigned to a PORT
946 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700947 return scic_sds_controller_start_next_phy(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -0700948 }
949 }
950
Dan Williams85280952011-06-28 15:05:53 -0700951 status = scic_sds_phy_start(iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -0700952
953 if (status == SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700954 sci_mod_timer(&ihost->phy_timer,
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700955 SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700956 ihost->phy_startup_timer_pending = true;
Dan Williamscc9203b2011-05-08 17:34:44 -0700957 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700958 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700959 "%s: Controller stop operation failed "
960 "to stop phy %d because of status "
961 "%d.\n",
962 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700963 ihost->phys[ihost->next_phy_to_start].phy_index,
Dan Williamscc9203b2011-05-08 17:34:44 -0700964 status);
965 }
966
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700967 ihost->next_phy_to_start++;
Dan Williamscc9203b2011-05-08 17:34:44 -0700968 }
969
970 return status;
971}
972
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700973static void phy_startup_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -0700974{
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700975 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700976 struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700977 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -0700978 enum sci_status status;
979
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700980 spin_lock_irqsave(&ihost->scic_lock, flags);
981
982 if (tmr->cancel)
983 goto done;
984
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700985 ihost->phy_startup_timer_pending = false;
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700986
987 do {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700988 status = scic_sds_controller_start_next_phy(ihost);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700989 } while (status != SCI_SUCCESS);
990
991done:
992 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -0700993}
994
Dan Williamsac668c62011-06-07 18:50:55 -0700995static u16 isci_tci_active(struct isci_host *ihost)
996{
997 return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
998}
999
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001000static enum sci_status scic_controller_start(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07001001 u32 timeout)
1002{
Dan Williamscc9203b2011-05-08 17:34:44 -07001003 enum sci_status result;
1004 u16 index;
1005
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001006 if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
1007 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001008 "SCIC Controller start operation requested in "
1009 "invalid state\n");
1010 return SCI_FAILURE_INVALID_STATE;
1011 }
1012
1013 /* Build the TCi free pool */
Dan Williamsac668c62011-06-07 18:50:55 -07001014 BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1015 ihost->tci_head = 0;
1016 ihost->tci_tail = 0;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001017 for (index = 0; index < ihost->task_context_entries; index++)
Dan Williamsac668c62011-06-07 18:50:55 -07001018 isci_tci_free(ihost, index);
Dan Williamscc9203b2011-05-08 17:34:44 -07001019
1020 /* Build the RNi free pool */
1021 scic_sds_remote_node_table_initialize(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001022 &ihost->available_remote_nodes,
1023 ihost->remote_node_entries);
Dan Williamscc9203b2011-05-08 17:34:44 -07001024
1025 /*
1026 * Before anything else lets make sure we will not be
1027 * interrupted by the hardware.
1028 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001029 scic_controller_disable_interrupts(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001030
1031 /* Enable the port task scheduler */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001032 scic_sds_controller_enable_port_task_scheduler(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001033
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001034 /* Assign all the task entries to ihost physical function */
1035 scic_sds_controller_assign_task_entries(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001036
1037 /* Now initialize the completion queue */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001038 scic_sds_controller_initialize_completion_queue(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001039
1040 /* Initialize the unsolicited frame queue for use */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001041 scic_sds_controller_initialize_unsolicited_frame_queue(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001042
1043 /* Start all of the ports on this controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001044 for (index = 0; index < ihost->logical_port_entries; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001045 struct isci_port *iport = &ihost->ports[index];
Dan Williamscc9203b2011-05-08 17:34:44 -07001046
Dan Williamsffe191c2011-06-29 13:09:25 -07001047 result = scic_sds_port_start(iport);
Dan Williamscc9203b2011-05-08 17:34:44 -07001048 if (result)
1049 return result;
1050 }
1051
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001052 scic_sds_controller_start_next_phy(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001053
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001054 sci_mod_timer(&ihost->timer, timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07001055
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001056 sci_change_state(&ihost->sm, SCIC_STARTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001057
1058 return SCI_SUCCESS;
1059}
1060
Dan Williams6f231dd2011-07-02 22:56:22 -07001061void isci_host_scan_start(struct Scsi_Host *shost)
1062{
Dan Williams4393aa42011-03-31 13:10:44 -07001063 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001064 unsigned long tmo = scic_controller_get_suggested_start_timeout(ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001065
Dan Williams0cf89d12011-02-18 09:25:07 -08001066 set_bit(IHOST_START_PENDING, &ihost->flags);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001067
1068 spin_lock_irq(&ihost->scic_lock);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001069 scic_controller_start(ihost, tmo);
1070 scic_controller_enable_interrupts(ihost);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001071 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001072}
1073
Dan Williamscc9203b2011-05-08 17:34:44 -07001074static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001075{
Dan Williams0cf89d12011-02-18 09:25:07 -08001076 isci_host_change_state(ihost, isci_stopped);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001077 scic_controller_disable_interrupts(ihost);
Dan Williams0cf89d12011-02-18 09:25:07 -08001078 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
1079 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001080}
1081
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001082static void scic_sds_controller_completion_handler(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001083{
1084 /* Empty out the completion queue */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001085 if (scic_sds_controller_completion_queue_has_entries(ihost))
1086 scic_sds_controller_process_completions(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001087
1088 /* Clear the interrupt and enable all interrupts again */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001089 writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001090 /* Could we write the value of SMU_ISR_COMPLETION? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001091 writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
1092 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -07001093}
1094
Dan Williams6f231dd2011-07-02 22:56:22 -07001095/**
1096 * isci_host_completion_routine() - This function is the delayed service
1097 * routine that calls the sci core library's completion handler. It's
1098 * scheduled as a tasklet from the interrupt service routine when interrupts
1099 * in use, or set as the timeout function in polled mode.
1100 * @data: This parameter specifies the ISCI host object
1101 *
1102 */
1103static void isci_host_completion_routine(unsigned long data)
1104{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001105 struct isci_host *ihost = (struct isci_host *)data;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001106 struct list_head completed_request_list;
1107 struct list_head errored_request_list;
1108 struct list_head *current_position;
1109 struct list_head *next_position;
Dan Williams6f231dd2011-07-02 22:56:22 -07001110 struct isci_request *request;
1111 struct isci_request *next_request;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001112 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -07001113
1114 INIT_LIST_HEAD(&completed_request_list);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001115 INIT_LIST_HEAD(&errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001116
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001117 spin_lock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001118
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001119 scic_sds_controller_completion_handler(ihost);
Dan Williamsc7ef4032011-02-18 09:25:05 -08001120
Dan Williams6f231dd2011-07-02 22:56:22 -07001121 /* Take the lists of completed I/Os from the host. */
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001122
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001123 list_splice_init(&ihost->requests_to_complete,
Dan Williams6f231dd2011-07-02 22:56:22 -07001124 &completed_request_list);
1125
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001126 /* Take the list of errored I/Os from the host. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001127 list_splice_init(&ihost->requests_to_errorback,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001128 &errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001129
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001130 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001131
1132 /* Process any completions in the lists. */
1133 list_for_each_safe(current_position, next_position,
1134 &completed_request_list) {
1135
1136 request = list_entry(current_position, struct isci_request,
1137 completed_node);
1138 task = isci_request_access_task(request);
1139
1140 /* Normal notification (task_done) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001141 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001142 "%s: Normal - request/task = %p/%p\n",
1143 __func__,
1144 request,
1145 task);
1146
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001147 /* Return the task to libsas */
1148 if (task != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001149
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001150 task->lldd_task = NULL;
1151 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1152
1153 /* If the task is already in the abort path,
1154 * the task_done callback cannot be called.
1155 */
1156 task->task_done(task);
1157 }
1158 }
Dan Williams312e0c22011-06-28 13:47:09 -07001159
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001160 spin_lock_irq(&ihost->scic_lock);
1161 isci_free_tag(ihost, request->io_tag);
1162 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001163 }
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001164 list_for_each_entry_safe(request, next_request, &errored_request_list,
Dan Williams6f231dd2011-07-02 22:56:22 -07001165 completed_node) {
1166
1167 task = isci_request_access_task(request);
1168
1169 /* Use sas_task_abort */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001170 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001171 "%s: Error - request/task = %p/%p\n",
1172 __func__,
1173 request,
1174 task);
1175
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001176 if (task != NULL) {
1177
1178 /* Put the task into the abort path if it's not there
1179 * already.
1180 */
1181 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
1182 sas_task_abort(task);
1183
1184 } else {
1185 /* This is a case where the request has completed with a
1186 * status such that it needed further target servicing,
1187 * but the sas_task reference has already been removed
1188 * from the request. Since it was errored, it was not
1189 * being aborted, so there is nothing to do except free
1190 * it.
1191 */
1192
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001193 spin_lock_irq(&ihost->scic_lock);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001194 /* Remove the request from the remote device's list
1195 * of pending requests.
1196 */
1197 list_del_init(&request->dev_node);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001198 isci_free_tag(ihost, request->io_tag);
1199 spin_unlock_irq(&ihost->scic_lock);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001200 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001201 }
1202
1203}
1204
Dan Williamscc9203b2011-05-08 17:34:44 -07001205/**
1206 * scic_controller_stop() - This method will stop an individual controller
1207 * object.This method will invoke the associated user callback upon
1208 * completion. The completion callback is called when the following
1209 * conditions are met: -# the method return status is SCI_SUCCESS. -# the
1210 * controller has been quiesced. This method will ensure that all IO
1211 * requests are quiesced, phys are stopped, and all additional operation by
1212 * the hardware is halted.
1213 * @controller: the handle to the controller object to stop.
1214 * @timeout: This parameter specifies the number of milliseconds in which the
1215 * stop operation should complete.
1216 *
1217 * The controller must be in the STARTED or STOPPED state. Indicate if the
1218 * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1219 * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1220 * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1221 * controller is not either in the STARTED or STOPPED states.
1222 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001223static enum sci_status scic_controller_stop(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07001224 u32 timeout)
1225{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001226 if (ihost->sm.current_state_id != SCIC_READY) {
1227 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001228 "SCIC Controller stop operation requested in "
1229 "invalid state\n");
1230 return SCI_FAILURE_INVALID_STATE;
1231 }
1232
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001233 sci_mod_timer(&ihost->timer, timeout);
1234 sci_change_state(&ihost->sm, SCIC_STOPPING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001235 return SCI_SUCCESS;
1236}
1237
1238/**
1239 * scic_controller_reset() - This method will reset the supplied core
1240 * controller regardless of the state of said controller. This operation is
1241 * considered destructive. In other words, all current operations are wiped
1242 * out. No IO completions for outstanding devices occur. Outstanding IO
1243 * requests are not aborted or completed at the actual remote device.
1244 * @controller: the handle to the controller object to reset.
1245 *
1246 * Indicate if the controller reset method succeeded or failed in some way.
1247 * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1248 * the controller reset operation is unable to complete.
1249 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001250static enum sci_status scic_controller_reset(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001251{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001252 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001253 case SCIC_RESET:
1254 case SCIC_READY:
1255 case SCIC_STOPPED:
1256 case SCIC_FAILED:
Dan Williamscc9203b2011-05-08 17:34:44 -07001257 /*
1258 * The reset operation is not a graceful cleanup, just
1259 * perform the state transition.
1260 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001261 sci_change_state(&ihost->sm, SCIC_RESETTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001262 return SCI_SUCCESS;
1263 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001264 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001265 "SCIC Controller reset operation requested in "
1266 "invalid state\n");
1267 return SCI_FAILURE_INVALID_STATE;
1268 }
1269}
1270
Dan Williams0cf89d12011-02-18 09:25:07 -08001271void isci_host_deinit(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07001272{
1273 int i;
1274
Dan Williams0cf89d12011-02-18 09:25:07 -08001275 isci_host_change_state(ihost, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001276 for (i = 0; i < SCI_MAX_PORTS; i++) {
Dan Williamse5313812011-05-07 10:11:43 -07001277 struct isci_port *iport = &ihost->ports[i];
Dan Williams0cf89d12011-02-18 09:25:07 -08001278 struct isci_remote_device *idev, *d;
1279
Dan Williamse5313812011-05-07 10:11:43 -07001280 list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
Dan Williams209fae12011-06-13 17:39:44 -07001281 if (test_bit(IDEV_ALLOCATED, &idev->flags))
1282 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001283 }
1284 }
1285
Dan Williams0cf89d12011-02-18 09:25:07 -08001286 set_bit(IHOST_STOP_PENDING, &ihost->flags);
Dan Williams7c40a802011-03-02 11:49:26 -08001287
1288 spin_lock_irq(&ihost->scic_lock);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001289 scic_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
Dan Williams7c40a802011-03-02 11:49:26 -08001290 spin_unlock_irq(&ihost->scic_lock);
1291
Dan Williams0cf89d12011-02-18 09:25:07 -08001292 wait_for_stop(ihost);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001293 scic_controller_reset(ihost);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001294
1295 /* Cancel any/all outstanding port timers */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001296 for (i = 0; i < ihost->logical_port_entries; i++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001297 struct isci_port *iport = &ihost->ports[i];
1298 del_timer_sync(&iport->timer.timer);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001299 }
1300
Edmund Nadolskia628d472011-05-19 11:59:36 +00001301 /* Cancel any/all outstanding phy timers */
1302 for (i = 0; i < SCI_MAX_PHYS; i++) {
Dan Williams85280952011-06-28 15:05:53 -07001303 struct isci_phy *iphy = &ihost->phys[i];
1304 del_timer_sync(&iphy->sata_timer.timer);
Edmund Nadolskia628d472011-05-19 11:59:36 +00001305 }
1306
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001307 del_timer_sync(&ihost->port_agent.timer.timer);
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -07001308
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001309 del_timer_sync(&ihost->power_control.timer.timer);
Edmund Nadolski04736612011-05-19 20:17:47 -07001310
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001311 del_timer_sync(&ihost->timer.timer);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001312
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001313 del_timer_sync(&ihost->phy_timer.timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001314}
1315
Dan Williams6f231dd2011-07-02 22:56:22 -07001316static void __iomem *scu_base(struct isci_host *isci_host)
1317{
1318 struct pci_dev *pdev = isci_host->pdev;
1319 int id = isci_host->id;
1320
1321 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
1322}
1323
1324static void __iomem *smu_base(struct isci_host *isci_host)
1325{
1326 struct pci_dev *pdev = isci_host->pdev;
1327 int id = isci_host->id;
1328
1329 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
1330}
1331
Dave Jiangb5f18a22011-03-16 14:57:23 -07001332static void isci_user_parameters_get(
1333 struct isci_host *isci_host,
1334 union scic_user_parameters *scic_user_params)
1335{
1336 struct scic_sds_user_parameters *u = &scic_user_params->sds1;
1337 int i;
1338
1339 for (i = 0; i < SCI_MAX_PHYS; i++) {
1340 struct sci_phy_user_params *u_phy = &u->phys[i];
1341
1342 u_phy->max_speed_generation = phy_gen;
1343
1344 /* we are not exporting these for now */
1345 u_phy->align_insertion_frequency = 0x7f;
1346 u_phy->in_connection_align_insertion_frequency = 0xff;
1347 u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1348 }
1349
1350 u->stp_inactivity_timeout = stp_inactive_to;
1351 u->ssp_inactivity_timeout = ssp_inactive_to;
1352 u->stp_max_occupancy_timeout = stp_max_occ_to;
1353 u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1354 u->no_outbound_task_timeout = no_outbound_task_to;
1355 u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1356}
1357
Dan Williams9269e0e2011-05-12 07:42:17 -07001358static void scic_sds_controller_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001359{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001360 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001361
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001362 sci_change_state(&ihost->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001363}
1364
Dan Williams9269e0e2011-05-12 07:42:17 -07001365static inline void scic_sds_controller_starting_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001366{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001367 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001368
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001369 sci_del_timer(&ihost->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001370}
1371
1372#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1373#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1374#define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
1375#define INTERRUPT_COALESCE_NUMBER_MAX 256
1376#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
1377#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
1378
1379/**
1380 * scic_controller_set_interrupt_coalescence() - This method allows the user to
1381 * configure the interrupt coalescence.
1382 * @controller: This parameter represents the handle to the controller object
1383 * for which its interrupt coalesce register is overridden.
1384 * @coalesce_number: Used to control the number of entries in the Completion
1385 * Queue before an interrupt is generated. If the number of entries exceed
1386 * this number, an interrupt will be generated. The valid range of the input
1387 * is [0, 256]. A setting of 0 results in coalescing being disabled.
1388 * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1389 * input is [0, 2700000] . A setting of 0 is allowed and results in no
1390 * interrupt coalescing timeout.
1391 *
1392 * Indicate if the user successfully set the interrupt coalesce parameters.
1393 * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1394 * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1395 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001396static enum sci_status
1397scic_controller_set_interrupt_coalescence(struct isci_host *ihost,
1398 u32 coalesce_number,
1399 u32 coalesce_timeout)
Dan Williamscc9203b2011-05-08 17:34:44 -07001400{
1401 u8 timeout_encode = 0;
1402 u32 min = 0;
1403 u32 max = 0;
1404
1405 /* Check if the input parameters fall in the range. */
1406 if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1407 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1408
1409 /*
1410 * Defined encoding for interrupt coalescing timeout:
1411 * Value Min Max Units
1412 * ----- --- --- -----
1413 * 0 - - Disabled
1414 * 1 13.3 20.0 ns
1415 * 2 26.7 40.0
1416 * 3 53.3 80.0
1417 * 4 106.7 160.0
1418 * 5 213.3 320.0
1419 * 6 426.7 640.0
1420 * 7 853.3 1280.0
1421 * 8 1.7 2.6 us
1422 * 9 3.4 5.1
1423 * 10 6.8 10.2
1424 * 11 13.7 20.5
1425 * 12 27.3 41.0
1426 * 13 54.6 81.9
1427 * 14 109.2 163.8
1428 * 15 218.5 327.7
1429 * 16 436.9 655.4
1430 * 17 873.8 1310.7
1431 * 18 1.7 2.6 ms
1432 * 19 3.5 5.2
1433 * 20 7.0 10.5
1434 * 21 14.0 21.0
1435 * 22 28.0 41.9
1436 * 23 55.9 83.9
1437 * 24 111.8 167.8
1438 * 25 223.7 335.5
1439 * 26 447.4 671.1
1440 * 27 894.8 1342.2
1441 * 28 1.8 2.7 s
1442 * Others Undefined */
1443
1444 /*
1445 * Use the table above to decide the encode of interrupt coalescing timeout
1446 * value for register writing. */
1447 if (coalesce_timeout == 0)
1448 timeout_encode = 0;
1449 else{
1450 /* make the timeout value in unit of (10 ns). */
1451 coalesce_timeout = coalesce_timeout * 100;
1452 min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1453 max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1454
1455 /* get the encode of timeout for register writing. */
1456 for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1457 timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1458 timeout_encode++) {
1459 if (min <= coalesce_timeout && max > coalesce_timeout)
1460 break;
1461 else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1462 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1463 if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1464 break;
1465 else{
1466 timeout_encode++;
1467 break;
1468 }
1469 } else {
1470 max = max * 2;
1471 min = min * 2;
1472 }
1473 }
1474
1475 if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1476 /* the value is out of range. */
1477 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1478 }
1479
1480 writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1481 SMU_ICC_GEN_VAL(TIMER, timeout_encode),
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001482 &ihost->smu_registers->interrupt_coalesce_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001483
1484
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001485 ihost->interrupt_coalesce_number = (u16)coalesce_number;
1486 ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
Dan Williamscc9203b2011-05-08 17:34:44 -07001487
1488 return SCI_SUCCESS;
1489}
1490
1491
Dan Williams9269e0e2011-05-12 07:42:17 -07001492static void scic_sds_controller_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001493{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001494 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001495
1496 /* set the default interrupt coalescence number and timeout value. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001497 scic_controller_set_interrupt_coalescence(ihost, 0x10, 250);
Dan Williamscc9203b2011-05-08 17:34:44 -07001498}
1499
Dan Williams9269e0e2011-05-12 07:42:17 -07001500static void scic_sds_controller_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001501{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001502 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001503
1504 /* disable interrupt coalescence. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001505 scic_controller_set_interrupt_coalescence(ihost, 0, 0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001506}
1507
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001508static enum sci_status scic_sds_controller_stop_phys(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001509{
1510 u32 index;
1511 enum sci_status status;
1512 enum sci_status phy_status;
Dan Williamscc9203b2011-05-08 17:34:44 -07001513
1514 status = SCI_SUCCESS;
1515
1516 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -07001517 phy_status = scic_sds_phy_stop(&ihost->phys[index]);
Dan Williamscc9203b2011-05-08 17:34:44 -07001518
1519 if (phy_status != SCI_SUCCESS &&
1520 phy_status != SCI_FAILURE_INVALID_STATE) {
1521 status = SCI_FAILURE;
1522
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001523 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001524 "%s: Controller stop operation failed to stop "
1525 "phy %d because of status %d.\n",
1526 __func__,
Dan Williams85280952011-06-28 15:05:53 -07001527 ihost->phys[index].phy_index, phy_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001528 }
1529 }
1530
1531 return status;
1532}
1533
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001534static enum sci_status scic_sds_controller_stop_ports(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001535{
1536 u32 index;
1537 enum sci_status port_status;
1538 enum sci_status status = SCI_SUCCESS;
Dan Williamscc9203b2011-05-08 17:34:44 -07001539
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001540 for (index = 0; index < ihost->logical_port_entries; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001541 struct isci_port *iport = &ihost->ports[index];
Dan Williamscc9203b2011-05-08 17:34:44 -07001542
Dan Williamsffe191c2011-06-29 13:09:25 -07001543 port_status = scic_sds_port_stop(iport);
Dan Williamscc9203b2011-05-08 17:34:44 -07001544
1545 if ((port_status != SCI_SUCCESS) &&
1546 (port_status != SCI_FAILURE_INVALID_STATE)) {
1547 status = SCI_FAILURE;
1548
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001549 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001550 "%s: Controller stop operation failed to "
1551 "stop port %d because of status %d.\n",
1552 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -07001553 iport->logical_port_index,
Dan Williamscc9203b2011-05-08 17:34:44 -07001554 port_status);
1555 }
1556 }
1557
1558 return status;
1559}
1560
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001561static enum sci_status scic_sds_controller_stop_devices(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001562{
1563 u32 index;
1564 enum sci_status status;
1565 enum sci_status device_status;
1566
1567 status = SCI_SUCCESS;
1568
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001569 for (index = 0; index < ihost->remote_node_entries; index++) {
1570 if (ihost->device_table[index] != NULL) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001571 /* / @todo What timeout value do we want to provide to this request? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001572 device_status = scic_remote_device_stop(ihost->device_table[index], 0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001573
1574 if ((device_status != SCI_SUCCESS) &&
1575 (device_status != SCI_FAILURE_INVALID_STATE)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001576 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001577 "%s: Controller stop operation failed "
1578 "to stop device 0x%p because of "
1579 "status %d.\n",
1580 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001581 ihost->device_table[index], device_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001582 }
1583 }
1584 }
1585
1586 return status;
1587}
1588
Dan Williams9269e0e2011-05-12 07:42:17 -07001589static void scic_sds_controller_stopping_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001590{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001591 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001592
1593 /* Stop all of the components for this controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001594 scic_sds_controller_stop_phys(ihost);
1595 scic_sds_controller_stop_ports(ihost);
1596 scic_sds_controller_stop_devices(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001597}
1598
Dan Williams9269e0e2011-05-12 07:42:17 -07001599static void scic_sds_controller_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001600{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001601 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001602
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001603 sci_del_timer(&ihost->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001604}
1605
1606
1607/**
1608 * scic_sds_controller_reset_hardware() -
1609 *
1610 * This method will reset the controller hardware.
1611 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001612static void scic_sds_controller_reset_hardware(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001613{
1614 /* Disable interrupts so we dont take any spurious interrupts */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001615 scic_controller_disable_interrupts(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001616
1617 /* Reset the SCU */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001618 writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001619
1620 /* Delay for 1ms to before clearing the CQP and UFQPR. */
1621 udelay(1000);
1622
1623 /* The write to the CQGR clears the CQP */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001624 writel(0x00000000, &ihost->smu_registers->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -07001625
1626 /* The write to the UFQGP clears the UFQPR */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001627 writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001628}
1629
Dan Williams9269e0e2011-05-12 07:42:17 -07001630static void scic_sds_controller_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001631{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001632 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001633
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001634 scic_sds_controller_reset_hardware(ihost);
1635 sci_change_state(&ihost->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001636}
1637
1638static const struct sci_base_state scic_sds_controller_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001639 [SCIC_INITIAL] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001640 .enter_state = scic_sds_controller_initial_state_enter,
1641 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001642 [SCIC_RESET] = {},
1643 [SCIC_INITIALIZING] = {},
1644 [SCIC_INITIALIZED] = {},
1645 [SCIC_STARTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001646 .exit_state = scic_sds_controller_starting_state_exit,
1647 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001648 [SCIC_READY] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001649 .enter_state = scic_sds_controller_ready_state_enter,
1650 .exit_state = scic_sds_controller_ready_state_exit,
1651 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001652 [SCIC_RESETTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001653 .enter_state = scic_sds_controller_resetting_state_enter,
1654 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001655 [SCIC_STOPPING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001656 .enter_state = scic_sds_controller_stopping_state_enter,
1657 .exit_state = scic_sds_controller_stopping_state_exit,
1658 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001659 [SCIC_STOPPED] = {},
1660 [SCIC_FAILED] = {}
Dan Williamscc9203b2011-05-08 17:34:44 -07001661};
1662
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001663static void scic_sds_controller_set_default_config_parameters(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001664{
1665 /* these defaults are overridden by the platform / firmware */
Dan Williamscc9203b2011-05-08 17:34:44 -07001666 u16 index;
1667
1668 /* Default to APC mode. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001669 ihost->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
Dan Williamscc9203b2011-05-08 17:34:44 -07001670
1671 /* Default to APC mode. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001672 ihost->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
Dan Williamscc9203b2011-05-08 17:34:44 -07001673
1674 /* Default to no SSC operation. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001675 ihost->oem_parameters.sds1.controller.do_enable_ssc = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07001676
1677 /* Initialize all of the port parameter information to narrow ports. */
1678 for (index = 0; index < SCI_MAX_PORTS; index++) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001679 ihost->oem_parameters.sds1.ports[index].phy_mask = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001680 }
1681
1682 /* Initialize all of the phy parameter information. */
1683 for (index = 0; index < SCI_MAX_PHYS; index++) {
1684 /* Default to 6G (i.e. Gen 3) for now. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001685 ihost->user_parameters.sds1.phys[index].max_speed_generation = 3;
Dan Williamscc9203b2011-05-08 17:34:44 -07001686
1687 /* the frequencies cannot be 0 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001688 ihost->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
1689 ihost->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
1690 ihost->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
Dan Williamscc9203b2011-05-08 17:34:44 -07001691
1692 /*
1693 * Previous Vitesse based expanders had a arbitration issue that
1694 * is worked around by having the upper 32-bits of SAS address
1695 * with a value greater then the Vitesse company identifier.
1696 * Hence, usage of 0x5FCFFFFF. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001697 ihost->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
1698 ihost->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
Dan Williamscc9203b2011-05-08 17:34:44 -07001699 }
1700
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001701 ihost->user_parameters.sds1.stp_inactivity_timeout = 5;
1702 ihost->user_parameters.sds1.ssp_inactivity_timeout = 5;
1703 ihost->user_parameters.sds1.stp_max_occupancy_timeout = 5;
1704 ihost->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
1705 ihost->user_parameters.sds1.no_outbound_task_timeout = 20;
Dan Williamscc9203b2011-05-08 17:34:44 -07001706}
1707
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001708static void controller_timeout(unsigned long data)
1709{
1710 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001711 struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1712 struct sci_base_state_machine *sm = &ihost->sm;
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001713 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001714
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001715 spin_lock_irqsave(&ihost->scic_lock, flags);
1716
1717 if (tmr->cancel)
1718 goto done;
1719
Edmund Nadolskie3013702011-06-02 00:10:43 +00001720 if (sm->current_state_id == SCIC_STARTING)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001721 scic_sds_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001722 else if (sm->current_state_id == SCIC_STOPPING) {
1723 sci_change_state(sm, SCIC_FAILED);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001724 isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
1725 } else /* / @todo Now what do we want to do in this case? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001726 dev_err(&ihost->pdev->dev,
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001727 "%s: Controller timer fired when controller was not "
1728 "in a state being timed.\n",
1729 __func__);
1730
1731done:
1732 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1733}
Dan Williamscc9203b2011-05-08 17:34:44 -07001734
1735/**
1736 * scic_controller_construct() - This method will attempt to construct a
1737 * controller object utilizing the supplied parameter information.
1738 * @c: This parameter specifies the controller to be constructed.
1739 * @scu_base: mapped base address of the scu registers
1740 * @smu_base: mapped base address of the smu registers
1741 *
1742 * Indicate if the controller was successfully constructed or if it failed in
1743 * some way. SCI_SUCCESS This value is returned if the controller was
1744 * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
1745 * if the interrupt coalescence timer may cause SAS compliance issues for SMP
1746 * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
1747 * This value is returned if the controller does not support the supplied type.
1748 * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
1749 * controller does not support the supplied initialization data version.
1750 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001751static enum sci_status scic_controller_construct(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07001752 void __iomem *scu_base,
1753 void __iomem *smu_base)
1754{
Dan Williamscc9203b2011-05-08 17:34:44 -07001755 u8 i;
1756
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001757 sci_init_sm(&ihost->sm, scic_sds_controller_state_table, SCIC_INITIAL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001758
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001759 ihost->scu_registers = scu_base;
1760 ihost->smu_registers = smu_base;
Dan Williamscc9203b2011-05-08 17:34:44 -07001761
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001762 scic_sds_port_configuration_agent_construct(&ihost->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07001763
1764 /* Construct the ports for this controller */
1765 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001766 scic_sds_port_construct(&ihost->ports[i], i, ihost);
1767 scic_sds_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001768
1769 /* Construct the phys for this controller */
1770 for (i = 0; i < SCI_MAX_PHYS; i++) {
1771 /* Add all the PHYs to the dummy port */
Dan Williams85280952011-06-28 15:05:53 -07001772 scic_sds_phy_construct(&ihost->phys[i],
Dan Williamsffe191c2011-06-29 13:09:25 -07001773 &ihost->ports[SCI_MAX_PORTS], i);
Dan Williamscc9203b2011-05-08 17:34:44 -07001774 }
1775
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001776 ihost->invalid_phy_mask = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001777
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001778 sci_init_timer(&ihost->timer, controller_timeout);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001779
Dan Williamscc9203b2011-05-08 17:34:44 -07001780 /* Initialize the User and OEM parameters to default values. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001781 scic_sds_controller_set_default_config_parameters(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001782
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001783 return scic_controller_reset(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001784}
1785
1786int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
1787{
1788 int i;
1789
1790 for (i = 0; i < SCI_MAX_PORTS; i++)
1791 if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1792 return -EINVAL;
1793
1794 for (i = 0; i < SCI_MAX_PHYS; i++)
1795 if (oem->phys[i].sas_address.high == 0 &&
1796 oem->phys[i].sas_address.low == 0)
1797 return -EINVAL;
1798
1799 if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1800 for (i = 0; i < SCI_MAX_PHYS; i++)
1801 if (oem->ports[i].phy_mask != 0)
1802 return -EINVAL;
1803 } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1804 u8 phy_mask = 0;
1805
1806 for (i = 0; i < SCI_MAX_PHYS; i++)
1807 phy_mask |= oem->ports[i].phy_mask;
1808
1809 if (phy_mask == 0)
1810 return -EINVAL;
1811 } else
1812 return -EINVAL;
1813
1814 if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1815 return -EINVAL;
1816
1817 return 0;
1818}
1819
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001820static enum sci_status scic_oem_parameters_set(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07001821 union scic_oem_parameters *scic_parms)
1822{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001823 u32 state = ihost->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07001824
Edmund Nadolskie3013702011-06-02 00:10:43 +00001825 if (state == SCIC_RESET ||
1826 state == SCIC_INITIALIZING ||
1827 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001828
1829 if (scic_oem_parameters_validate(&scic_parms->sds1))
1830 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001831 ihost->oem_parameters.sds1 = scic_parms->sds1;
Dan Williamscc9203b2011-05-08 17:34:44 -07001832
1833 return SCI_SUCCESS;
1834 }
1835
1836 return SCI_FAILURE_INVALID_STATE;
1837}
1838
1839void scic_oem_parameters_get(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001840 struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07001841 union scic_oem_parameters *scic_parms)
1842{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001843 memcpy(scic_parms, (&ihost->oem_parameters), sizeof(*scic_parms));
Dan Williamscc9203b2011-05-08 17:34:44 -07001844}
1845
Edmund Nadolski04736612011-05-19 20:17:47 -07001846static void power_control_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001847{
Edmund Nadolski04736612011-05-19 20:17:47 -07001848 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001849 struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
Dan Williams85280952011-06-28 15:05:53 -07001850 struct isci_phy *iphy;
Edmund Nadolski04736612011-05-19 20:17:47 -07001851 unsigned long flags;
1852 u8 i;
Dan Williamscc9203b2011-05-08 17:34:44 -07001853
Edmund Nadolski04736612011-05-19 20:17:47 -07001854 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001855
Edmund Nadolski04736612011-05-19 20:17:47 -07001856 if (tmr->cancel)
1857 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001858
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001859 ihost->power_control.phys_granted_power = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001860
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001861 if (ihost->power_control.phys_waiting == 0) {
1862 ihost->power_control.timer_started = false;
Edmund Nadolski04736612011-05-19 20:17:47 -07001863 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001864 }
Edmund Nadolski04736612011-05-19 20:17:47 -07001865
1866 for (i = 0; i < SCI_MAX_PHYS; i++) {
1867
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001868 if (ihost->power_control.phys_waiting == 0)
Edmund Nadolski04736612011-05-19 20:17:47 -07001869 break;
1870
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001871 iphy = ihost->power_control.requesters[i];
Dan Williams85280952011-06-28 15:05:53 -07001872 if (iphy == NULL)
Edmund Nadolski04736612011-05-19 20:17:47 -07001873 continue;
1874
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001875 if (ihost->power_control.phys_granted_power >=
1876 ihost->oem_parameters.sds1.controller.max_concurrent_dev_spin_up)
Edmund Nadolski04736612011-05-19 20:17:47 -07001877 break;
1878
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001879 ihost->power_control.requesters[i] = NULL;
1880 ihost->power_control.phys_waiting--;
1881 ihost->power_control.phys_granted_power++;
Dan Williams85280952011-06-28 15:05:53 -07001882 scic_sds_phy_consume_power_handler(iphy);
Edmund Nadolski04736612011-05-19 20:17:47 -07001883 }
1884
1885 /*
1886 * It doesn't matter if the power list is empty, we need to start the
1887 * timer in case another phy becomes ready.
1888 */
1889 sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001890 ihost->power_control.timer_started = true;
Edmund Nadolski04736612011-05-19 20:17:47 -07001891
1892done:
1893 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001894}
1895
1896/**
1897 * This method inserts the phy in the stagger spinup control queue.
1898 * @scic:
1899 *
1900 *
1901 */
1902void scic_sds_controller_power_control_queue_insert(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001903 struct isci_host *ihost,
Dan Williams85280952011-06-28 15:05:53 -07001904 struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07001905{
Dan Williams85280952011-06-28 15:05:53 -07001906 BUG_ON(iphy == NULL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001907
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001908 if (ihost->power_control.phys_granted_power <
1909 ihost->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
1910 ihost->power_control.phys_granted_power++;
Dan Williams85280952011-06-28 15:05:53 -07001911 scic_sds_phy_consume_power_handler(iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -07001912
1913 /*
1914 * stop and start the power_control timer. When the timer fires, the
1915 * no_of_phys_granted_power will be set to 0
1916 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001917 if (ihost->power_control.timer_started)
1918 sci_del_timer(&ihost->power_control.timer);
Edmund Nadolski04736612011-05-19 20:17:47 -07001919
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001920 sci_mod_timer(&ihost->power_control.timer,
Edmund Nadolski04736612011-05-19 20:17:47 -07001921 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001922 ihost->power_control.timer_started = true;
Edmund Nadolski04736612011-05-19 20:17:47 -07001923
Dan Williamscc9203b2011-05-08 17:34:44 -07001924 } else {
1925 /* Add the phy in the waiting list */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001926 ihost->power_control.requesters[iphy->phy_index] = iphy;
1927 ihost->power_control.phys_waiting++;
Dan Williamscc9203b2011-05-08 17:34:44 -07001928 }
1929}
1930
1931/**
1932 * This method removes the phy from the stagger spinup control queue.
1933 * @scic:
1934 *
1935 *
1936 */
1937void scic_sds_controller_power_control_queue_remove(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001938 struct isci_host *ihost,
Dan Williams85280952011-06-28 15:05:53 -07001939 struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07001940{
Dan Williams85280952011-06-28 15:05:53 -07001941 BUG_ON(iphy == NULL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001942
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001943 if (ihost->power_control.requesters[iphy->phy_index] != NULL) {
1944 ihost->power_control.phys_waiting--;
Dan Williamscc9203b2011-05-08 17:34:44 -07001945 }
1946
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001947 ihost->power_control.requesters[iphy->phy_index] = NULL;
Dan Williamscc9203b2011-05-08 17:34:44 -07001948}
1949
1950#define AFE_REGISTER_WRITE_DELAY 10
1951
1952/* Initialize the AFE for this phy index. We need to read the AFE setup from
1953 * the OEM parameters
1954 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001955static void scic_sds_controller_afe_initialization(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001956{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001957 const struct scic_sds_oem_params *oem = &ihost->oem_parameters.sds1;
Dan Williamscc9203b2011-05-08 17:34:44 -07001958 u32 afe_status;
1959 u32 phy_id;
1960
1961 /* Clear DFX Status registers */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001962 writel(0x0081000f, &ihost->scu_registers->afe.afe_dfx_master_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001963 udelay(AFE_REGISTER_WRITE_DELAY);
1964
1965 if (is_b0()) {
1966 /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
1967 * Timer, PM Stagger Timer */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001968 writel(0x0007BFFF, &ihost->scu_registers->afe.afe_pmsn_master_control2);
Dan Williamscc9203b2011-05-08 17:34:44 -07001969 udelay(AFE_REGISTER_WRITE_DELAY);
1970 }
1971
1972 /* Configure bias currents to normal */
1973 if (is_a0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001974 writel(0x00005500, &ihost->scu_registers->afe.afe_bias_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001975 else if (is_a2())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001976 writel(0x00005A00, &ihost->scu_registers->afe.afe_bias_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001977 else if (is_b0() || is_c0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001978 writel(0x00005F00, &ihost->scu_registers->afe.afe_bias_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001979
1980 udelay(AFE_REGISTER_WRITE_DELAY);
1981
1982 /* Enable PLL */
Adam Gruchaladbb07432011-06-01 22:31:03 +00001983 if (is_b0() || is_c0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001984 writel(0x80040A08, &ihost->scu_registers->afe.afe_pll_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001985 else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001986 writel(0x80040908, &ihost->scu_registers->afe.afe_pll_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001987
1988 udelay(AFE_REGISTER_WRITE_DELAY);
1989
1990 /* Wait for the PLL to lock */
1991 do {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001992 afe_status = readl(&ihost->scu_registers->afe.afe_common_block_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001993 udelay(AFE_REGISTER_WRITE_DELAY);
1994 } while ((afe_status & 0x00001000) == 0);
1995
1996 if (is_a0() || is_a2()) {
1997 /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001998 writel(0x7bcc96ad, &ihost->scu_registers->afe.afe_pmsn_master_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001999 udelay(AFE_REGISTER_WRITE_DELAY);
2000 }
2001
2002 for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
2003 const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2004
2005 if (is_b0()) {
2006 /* Configure transmitter SSC parameters */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002007 writel(0x00030000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002008 udelay(AFE_REGISTER_WRITE_DELAY);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002009 } else if (is_c0()) {
2010 /* Configure transmitter SSC parameters */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002011 writel(0x0003000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002012 udelay(AFE_REGISTER_WRITE_DELAY);
2013
2014 /*
2015 * All defaults, except the Receive Word Alignament/Comma Detect
2016 * Enable....(0xe800) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002017 writel(0x00004500, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002018 udelay(AFE_REGISTER_WRITE_DELAY);
Dan Williamscc9203b2011-05-08 17:34:44 -07002019 } else {
2020 /*
2021 * All defaults, except the Receive Word Alignament/Comma Detect
2022 * Enable....(0xe800) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002023 writel(0x00004512, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002024 udelay(AFE_REGISTER_WRITE_DELAY);
2025
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002026 writel(0x0050100F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
Dan Williamscc9203b2011-05-08 17:34:44 -07002027 udelay(AFE_REGISTER_WRITE_DELAY);
2028 }
2029
2030 /*
2031 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2032 * & increase TX int & ext bias 20%....(0xe85c) */
2033 if (is_a0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002034 writel(0x000003D4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002035 else if (is_a2())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002036 writel(0x000003F0, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002037 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002038 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002039 writel(0x000003D7, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002040 udelay(AFE_REGISTER_WRITE_DELAY);
2041
2042 /*
2043 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2044 * & increase TX int & ext bias 20%....(0xe85c) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002045 writel(0x000003D4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002046 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002047 writel(0x000001E7, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002048 udelay(AFE_REGISTER_WRITE_DELAY);
2049
2050 /*
2051 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2052 * & increase TX int & ext bias 20%....(0xe85c) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002053 writel(0x000001E4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002054 }
2055 udelay(AFE_REGISTER_WRITE_DELAY);
2056
2057 if (is_a0() || is_a2()) {
2058 /* Enable TX equalization (0xe824) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002059 writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002060 udelay(AFE_REGISTER_WRITE_DELAY);
2061 }
2062
2063 /*
2064 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
2065 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002066 writel(0x00004100, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002067 udelay(AFE_REGISTER_WRITE_DELAY);
2068
2069 /* Leave DFE/FFE on */
2070 if (is_a0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002071 writel(0x3F09983F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002072 else if (is_a2())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002073 writel(0x3F11103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002074 else if (is_b0()) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002075 writel(0x3F11103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002076 udelay(AFE_REGISTER_WRITE_DELAY);
2077 /* Enable TX equalization (0xe824) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002078 writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002079 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002080 writel(0x0140DF0F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002081 udelay(AFE_REGISTER_WRITE_DELAY);
2082
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002083 writel(0x3F6F103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002084 udelay(AFE_REGISTER_WRITE_DELAY);
2085
2086 /* Enable TX equalization (0xe824) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002087 writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002088 }
Adam Gruchaladbb07432011-06-01 22:31:03 +00002089
Dan Williamscc9203b2011-05-08 17:34:44 -07002090 udelay(AFE_REGISTER_WRITE_DELAY);
2091
2092 writel(oem_phy->afe_tx_amp_control0,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002093 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002094 udelay(AFE_REGISTER_WRITE_DELAY);
2095
2096 writel(oem_phy->afe_tx_amp_control1,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002097 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
Dan Williamscc9203b2011-05-08 17:34:44 -07002098 udelay(AFE_REGISTER_WRITE_DELAY);
2099
2100 writel(oem_phy->afe_tx_amp_control2,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002101 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
Dan Williamscc9203b2011-05-08 17:34:44 -07002102 udelay(AFE_REGISTER_WRITE_DELAY);
2103
2104 writel(oem_phy->afe_tx_amp_control3,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002105 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
Dan Williamscc9203b2011-05-08 17:34:44 -07002106 udelay(AFE_REGISTER_WRITE_DELAY);
2107 }
2108
2109 /* Transfer control to the PEs */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002110 writel(0x00010f00, &ihost->scu_registers->afe.afe_dfx_master_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002111 udelay(AFE_REGISTER_WRITE_DELAY);
2112}
2113
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002114static void scic_sds_controller_initialize_power_control(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002115{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002116 sci_init_timer(&ihost->power_control.timer, power_control_timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07002117
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002118 memset(ihost->power_control.requesters, 0,
2119 sizeof(ihost->power_control.requesters));
Dan Williamscc9203b2011-05-08 17:34:44 -07002120
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002121 ihost->power_control.phys_waiting = 0;
2122 ihost->power_control.phys_granted_power = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07002123}
2124
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002125static enum sci_status scic_controller_initialize(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002126{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002127 struct sci_base_state_machine *sm = &ihost->sm;
Dan Williams7c78da32011-06-01 16:00:01 -07002128 enum sci_status result = SCI_FAILURE;
2129 unsigned long i, state, val;
Dan Williamscc9203b2011-05-08 17:34:44 -07002130
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002131 if (ihost->sm.current_state_id != SCIC_RESET) {
2132 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002133 "SCIC Controller initialize operation requested "
2134 "in invalid state\n");
2135 return SCI_FAILURE_INVALID_STATE;
2136 }
2137
Edmund Nadolskie3013702011-06-02 00:10:43 +00002138 sci_change_state(sm, SCIC_INITIALIZING);
Dan Williamscc9203b2011-05-08 17:34:44 -07002139
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002140 sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002141
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002142 ihost->next_phy_to_start = 0;
2143 ihost->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002144
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002145 scic_sds_controller_initialize_power_control(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07002146
2147 /*
2148 * There is nothing to do here for B0 since we do not have to
2149 * program the AFE registers.
2150 * / @todo The AFE settings are supposed to be correct for the B0 but
2151 * / presently they seem to be wrong. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002152 scic_sds_controller_afe_initialization(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07002153
Dan Williams7c78da32011-06-01 16:00:01 -07002154
2155 /* Take the hardware out of reset */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002156 writel(0, &ihost->smu_registers->soft_reset_control);
Dan Williams7c78da32011-06-01 16:00:01 -07002157
2158 /*
2159 * / @todo Provide meaningfull error code for hardware failure
2160 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2161 for (i = 100; i >= 1; i--) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002162 u32 status;
Dan Williamscc9203b2011-05-08 17:34:44 -07002163
Dan Williams7c78da32011-06-01 16:00:01 -07002164 /* Loop until the hardware reports success */
2165 udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002166 status = readl(&ihost->smu_registers->control_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07002167
Dan Williams7c78da32011-06-01 16:00:01 -07002168 if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
2169 break;
Dan Williamscc9203b2011-05-08 17:34:44 -07002170 }
Dan Williams7c78da32011-06-01 16:00:01 -07002171 if (i == 0)
2172 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002173
Dan Williams7c78da32011-06-01 16:00:01 -07002174 /*
2175 * Determine what are the actaul device capacities that the
2176 * hardware will support */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002177 val = readl(&ihost->smu_registers->device_context_capacity);
Dan Williamscc9203b2011-05-08 17:34:44 -07002178
Dan Williams7c78da32011-06-01 16:00:01 -07002179 /* Record the smaller of the two capacity values */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002180 ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2181 ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2182 ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
Dan Williamscc9203b2011-05-08 17:34:44 -07002183
Dan Williams7c78da32011-06-01 16:00:01 -07002184 /*
2185 * Make all PEs that are unassigned match up with the
2186 * logical ports
2187 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002188 for (i = 0; i < ihost->logical_port_entries; i++) {
Dan Williams7c78da32011-06-01 16:00:01 -07002189 struct scu_port_task_scheduler_group_registers __iomem
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002190 *ptsg = &ihost->scu_registers->peg0.ptsg;
Dan Williamscc9203b2011-05-08 17:34:44 -07002191
Dan Williams7c78da32011-06-01 16:00:01 -07002192 writel(i, &ptsg->protocol_engine[i]);
Dan Williamscc9203b2011-05-08 17:34:44 -07002193 }
2194
2195 /* Initialize hardware PCI Relaxed ordering in DMA engines */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002196 val = readl(&ihost->scu_registers->sdma.pdma_configuration);
Dan Williams7c78da32011-06-01 16:00:01 -07002197 val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002198 writel(val, &ihost->scu_registers->sdma.pdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002199
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002200 val = readl(&ihost->scu_registers->sdma.cdma_configuration);
Dan Williams7c78da32011-06-01 16:00:01 -07002201 val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002202 writel(val, &ihost->scu_registers->sdma.cdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002203
2204 /*
2205 * Initialize the PHYs before the PORTs because the PHY registers
2206 * are accessed during the port initialization.
2207 */
Dan Williams7c78da32011-06-01 16:00:01 -07002208 for (i = 0; i < SCI_MAX_PHYS; i++) {
Dan Williams85280952011-06-28 15:05:53 -07002209 result = scic_sds_phy_initialize(&ihost->phys[i],
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002210 &ihost->scu_registers->peg0.pe[i].tl,
2211 &ihost->scu_registers->peg0.pe[i].ll);
Dan Williams7c78da32011-06-01 16:00:01 -07002212 if (result != SCI_SUCCESS)
2213 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002214 }
2215
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002216 for (i = 0; i < ihost->logical_port_entries; i++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07002217 result = scic_sds_port_initialize(&ihost->ports[i],
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002218 &ihost->scu_registers->peg0.ptsg.port[i],
2219 &ihost->scu_registers->peg0.ptsg.protocol_engine,
2220 &ihost->scu_registers->peg0.viit[i]);
Dan Williams7c78da32011-06-01 16:00:01 -07002221
2222 if (result != SCI_SUCCESS)
2223 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002224 }
2225
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002226 result = scic_sds_port_configuration_agent_initialize(ihost, &ihost->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07002227
Dan Williams7c78da32011-06-01 16:00:01 -07002228 out:
Dan Williamscc9203b2011-05-08 17:34:44 -07002229 /* Advance the controller state machine */
2230 if (result == SCI_SUCCESS)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002231 state = SCIC_INITIALIZED;
Dan Williamscc9203b2011-05-08 17:34:44 -07002232 else
Edmund Nadolskie3013702011-06-02 00:10:43 +00002233 state = SCIC_FAILED;
2234 sci_change_state(sm, state);
Dan Williamscc9203b2011-05-08 17:34:44 -07002235
2236 return result;
2237}
2238
2239static enum sci_status scic_user_parameters_set(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002240 struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07002241 union scic_user_parameters *scic_parms)
2242{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002243 u32 state = ihost->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07002244
Edmund Nadolskie3013702011-06-02 00:10:43 +00002245 if (state == SCIC_RESET ||
2246 state == SCIC_INITIALIZING ||
2247 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002248 u16 index;
2249
2250 /*
2251 * Validate the user parameters. If they are not legal, then
2252 * return a failure.
2253 */
2254 for (index = 0; index < SCI_MAX_PHYS; index++) {
2255 struct sci_phy_user_params *user_phy;
2256
2257 user_phy = &scic_parms->sds1.phys[index];
2258
2259 if (!((user_phy->max_speed_generation <=
2260 SCIC_SDS_PARM_MAX_SPEED) &&
2261 (user_phy->max_speed_generation >
2262 SCIC_SDS_PARM_NO_SPEED)))
2263 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2264
2265 if (user_phy->in_connection_align_insertion_frequency <
2266 3)
2267 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2268
2269 if ((user_phy->in_connection_align_insertion_frequency <
2270 3) ||
2271 (user_phy->align_insertion_frequency == 0) ||
2272 (user_phy->
2273 notify_enable_spin_up_insertion_frequency ==
2274 0))
2275 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2276 }
2277
2278 if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
2279 (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
2280 (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
2281 (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
2282 (scic_parms->sds1.no_outbound_task_timeout == 0))
2283 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2284
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002285 memcpy(&ihost->user_parameters, scic_parms, sizeof(*scic_parms));
Dan Williamscc9203b2011-05-08 17:34:44 -07002286
2287 return SCI_SUCCESS;
2288 }
2289
2290 return SCI_FAILURE_INVALID_STATE;
2291}
2292
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002293static int scic_controller_mem_init(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002294{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002295 struct device *dev = &ihost->pdev->dev;
Dan Williams7c78da32011-06-01 16:00:01 -07002296 dma_addr_t dma;
2297 size_t size;
2298 int err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002299
Dan Williams7c78da32011-06-01 16:00:01 -07002300 size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002301 ihost->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2302 if (!ihost->completion_queue)
Dan Williamscc9203b2011-05-08 17:34:44 -07002303 return -ENOMEM;
2304
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002305 writel(lower_32_bits(dma), &ihost->smu_registers->completion_queue_lower);
2306 writel(upper_32_bits(dma), &ihost->smu_registers->completion_queue_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002307
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002308 size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2309 ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
Dan Williams7c78da32011-06-01 16:00:01 -07002310 GFP_KERNEL);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002311 if (!ihost->remote_node_context_table)
Dan Williamscc9203b2011-05-08 17:34:44 -07002312 return -ENOMEM;
2313
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002314 writel(lower_32_bits(dma), &ihost->smu_registers->remote_node_context_lower);
2315 writel(upper_32_bits(dma), &ihost->smu_registers->remote_node_context_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002316
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002317 size = ihost->task_context_entries * sizeof(struct scu_task_context),
2318 ihost->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2319 if (!ihost->task_context_table)
Dan Williamscc9203b2011-05-08 17:34:44 -07002320 return -ENOMEM;
2321
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002322 ihost->task_context_dma = dma;
2323 writel(lower_32_bits(dma), &ihost->smu_registers->host_task_table_lower);
2324 writel(upper_32_bits(dma), &ihost->smu_registers->host_task_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002325
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002326 err = scic_sds_unsolicited_frame_control_construct(ihost);
Dan Williams7c78da32011-06-01 16:00:01 -07002327 if (err)
2328 return err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002329
2330 /*
2331 * Inform the silicon as to the location of the UF headers and
2332 * address table.
2333 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002334 writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2335 &ihost->scu_registers->sdma.uf_header_base_address_lower);
2336 writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2337 &ihost->scu_registers->sdma.uf_header_base_address_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002338
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002339 writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2340 &ihost->scu_registers->sdma.uf_address_table_lower);
2341 writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2342 &ihost->scu_registers->sdma.uf_address_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002343
2344 return 0;
2345}
2346
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002347int isci_host_init(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07002348{
Dan Williamsd9c37392011-03-03 17:59:32 -08002349 int err = 0, i;
Dan Williams6f231dd2011-07-02 22:56:22 -07002350 enum sci_status status;
Dan Williams4711ba12011-03-11 10:43:57 -08002351 union scic_oem_parameters oem;
Dan Williams6f231dd2011-07-02 22:56:22 -07002352 union scic_user_parameters scic_user_params;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002353 struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002354
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002355 spin_lock_init(&ihost->state_lock);
2356 spin_lock_init(&ihost->scic_lock);
2357 init_waitqueue_head(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07002358
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002359 isci_host_change_state(ihost, isci_starting);
Dan Williams6f231dd2011-07-02 22:56:22 -07002360
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002361 status = scic_controller_construct(ihost, scu_base(ihost),
2362 smu_base(ihost));
Dan Williams6f231dd2011-07-02 22:56:22 -07002363
2364 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002365 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002366 "%s: scic_controller_construct failed - status = %x\n",
2367 __func__,
2368 status);
Dave Jiang858d4aa2011-02-22 01:27:03 -08002369 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002370 }
2371
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002372 ihost->sas_ha.dev = &ihost->pdev->dev;
2373 ihost->sas_ha.lldd_ha = ihost;
Dan Williams6f231dd2011-07-02 22:56:22 -07002374
Dan Williamsd044af12011-03-08 09:52:49 -08002375 /*
2376 * grab initial values stored in the controller object for OEM and USER
2377 * parameters
2378 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002379 isci_user_parameters_get(ihost, &scic_user_params);
2380 status = scic_user_parameters_set(ihost,
Dan Williamsd044af12011-03-08 09:52:49 -08002381 &scic_user_params);
2382 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002383 dev_warn(&ihost->pdev->dev,
Dan Williamsd044af12011-03-08 09:52:49 -08002384 "%s: scic_user_parameters_set failed\n",
2385 __func__);
2386 return -ENODEV;
2387 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002388
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002389 scic_oem_parameters_get(ihost, &oem);
Dan Williamsd044af12011-03-08 09:52:49 -08002390
2391 /* grab any OEM parameters specified in orom */
2392 if (pci_info->orom) {
Dan Williams4711ba12011-03-11 10:43:57 -08002393 status = isci_parse_oem_parameters(&oem,
Dan Williamsd044af12011-03-08 09:52:49 -08002394 pci_info->orom,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002395 ihost->id);
Dan Williams6f231dd2011-07-02 22:56:22 -07002396 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002397 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002398 "parsing firmware oem parameters failed\n");
Dave Jiang858d4aa2011-02-22 01:27:03 -08002399 return -EINVAL;
Dan Williams6f231dd2011-07-02 22:56:22 -07002400 }
Dan Williams4711ba12011-03-11 10:43:57 -08002401 }
2402
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002403 status = scic_oem_parameters_set(ihost, &oem);
Dan Williams4711ba12011-03-11 10:43:57 -08002404 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002405 dev_warn(&ihost->pdev->dev,
Dan Williams4711ba12011-03-11 10:43:57 -08002406 "%s: scic_oem_parameters_set failed\n",
2407 __func__);
2408 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002409 }
2410
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002411 tasklet_init(&ihost->completion_tasklet,
2412 isci_host_completion_routine, (unsigned long)ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002413
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002414 INIT_LIST_HEAD(&ihost->requests_to_complete);
2415 INIT_LIST_HEAD(&ihost->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002416
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002417 spin_lock_irq(&ihost->scic_lock);
2418 status = scic_controller_initialize(ihost);
2419 spin_unlock_irq(&ihost->scic_lock);
Dan Williams7c40a802011-03-02 11:49:26 -08002420 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002421 dev_warn(&ihost->pdev->dev,
Dan Williams7c40a802011-03-02 11:49:26 -08002422 "%s: scic_controller_initialize failed -"
2423 " status = 0x%x\n",
2424 __func__, status);
2425 return -ENODEV;
2426 }
2427
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002428 err = scic_controller_mem_init(ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002429 if (err)
Dave Jiang858d4aa2011-02-22 01:27:03 -08002430 return err;
Dan Williams6f231dd2011-07-02 22:56:22 -07002431
Dan Williamsd9c37392011-03-03 17:59:32 -08002432 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002433 isci_port_init(&ihost->ports[i], ihost, i);
Dan Williams6f231dd2011-07-02 22:56:22 -07002434
Dan Williamsd9c37392011-03-03 17:59:32 -08002435 for (i = 0; i < SCI_MAX_PHYS; i++)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002436 isci_phy_init(&ihost->phys[i], ihost, i);
Dan Williamsd9c37392011-03-03 17:59:32 -08002437
2438 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002439 struct isci_remote_device *idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08002440
2441 INIT_LIST_HEAD(&idev->reqs_in_process);
2442 INIT_LIST_HEAD(&idev->node);
Dan Williamsd9c37392011-03-03 17:59:32 -08002443 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002444
Dan Williamsdb056252011-06-17 14:18:39 -07002445 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2446 struct isci_request *ireq;
2447 dma_addr_t dma;
2448
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002449 ireq = dmam_alloc_coherent(&ihost->pdev->dev,
Dan Williamsdb056252011-06-17 14:18:39 -07002450 sizeof(struct isci_request), &dma,
2451 GFP_KERNEL);
2452 if (!ireq)
2453 return -ENOMEM;
2454
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002455 ireq->tc = &ihost->task_context_table[i];
2456 ireq->owning_controller = ihost;
Dan Williamsdb056252011-06-17 14:18:39 -07002457 spin_lock_init(&ireq->state_lock);
2458 ireq->request_daddr = dma;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002459 ireq->isci_host = ihost;
2460 ihost->reqs[i] = ireq;
Dan Williamsdb056252011-06-17 14:18:39 -07002461 }
2462
Dave Jiang858d4aa2011-02-22 01:27:03 -08002463 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -07002464}
Dan Williamscc9203b2011-05-08 17:34:44 -07002465
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002466void scic_sds_controller_link_up(struct isci_host *ihost,
Dan Williamsffe191c2011-06-29 13:09:25 -07002467 struct isci_port *iport, struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07002468{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002469 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002470 case SCIC_STARTING:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002471 sci_del_timer(&ihost->phy_timer);
2472 ihost->phy_startup_timer_pending = false;
2473 ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -07002474 iport, iphy);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002475 scic_sds_controller_start_next_phy(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07002476 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002477 case SCIC_READY:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002478 ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -07002479 iport, iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -07002480 break;
2481 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002482 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002483 "%s: SCIC Controller linkup event from phy %d in "
Dan Williams85280952011-06-28 15:05:53 -07002484 "unexpected state %d\n", __func__, iphy->phy_index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002485 ihost->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002486 }
2487}
2488
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002489void scic_sds_controller_link_down(struct isci_host *ihost,
Dan Williamsffe191c2011-06-29 13:09:25 -07002490 struct isci_port *iport, struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07002491{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002492 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002493 case SCIC_STARTING:
2494 case SCIC_READY:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002495 ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -07002496 iport, iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -07002497 break;
2498 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002499 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002500 "%s: SCIC Controller linkdown event from phy %d in "
2501 "unexpected state %d\n",
2502 __func__,
Dan Williams85280952011-06-28 15:05:53 -07002503 iphy->phy_index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002504 ihost->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002505 }
2506}
2507
2508/**
2509 * This is a helper method to determine if any remote devices on this
2510 * controller are still in the stopping state.
2511 *
2512 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002513static bool scic_sds_controller_has_remote_devices_stopping(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002514{
2515 u32 index;
2516
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002517 for (index = 0; index < ihost->remote_node_entries; index++) {
2518 if ((ihost->device_table[index] != NULL) &&
2519 (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
Dan Williamscc9203b2011-05-08 17:34:44 -07002520 return true;
2521 }
2522
2523 return false;
2524}
2525
2526/**
2527 * This method is called by the remote device to inform the controller
2528 * object that the remote device has stopped.
2529 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002530void scic_sds_controller_remote_device_stopped(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002531 struct isci_remote_device *idev)
Dan Williamscc9203b2011-05-08 17:34:44 -07002532{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002533 if (ihost->sm.current_state_id != SCIC_STOPPING) {
2534 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002535 "SCIC Controller 0x%p remote device stopped event "
2536 "from device 0x%p in unexpected state %d\n",
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002537 ihost, idev,
2538 ihost->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002539 return;
2540 }
2541
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002542 if (!scic_sds_controller_has_remote_devices_stopping(ihost)) {
2543 sci_change_state(&ihost->sm, SCIC_STOPPED);
Dan Williamscc9203b2011-05-08 17:34:44 -07002544 }
2545}
2546
2547/**
2548 * This method will write to the SCU PCP register the request value. The method
2549 * is used to suspend/resume ports, devices, and phys.
2550 * @scic:
2551 *
2552 *
2553 */
2554void scic_sds_controller_post_request(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002555 struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07002556 u32 request)
2557{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002558 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002559 "%s: SCIC Controller 0x%p post request 0x%08x\n",
2560 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002561 ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07002562 request);
2563
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002564 writel(request, &ihost->smu_registers->post_context_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07002565}
2566
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002567struct isci_request *scic_request_by_tag(struct isci_host *ihost, u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07002568{
2569 u16 task_index;
2570 u16 task_sequence;
2571
Dan Williamsdd047c82011-06-09 11:06:58 -07002572 task_index = ISCI_TAG_TCI(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002573
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002574 if (task_index < ihost->task_context_entries) {
2575 struct isci_request *ireq = ihost->reqs[task_index];
Dan Williamsdb056252011-06-17 14:18:39 -07002576
2577 if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
Dan Williamsdd047c82011-06-09 11:06:58 -07002578 task_sequence = ISCI_TAG_SEQ(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002579
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002580 if (task_sequence == ihost->io_request_sequence[task_index])
Dan Williams5076a1a2011-06-27 14:57:03 -07002581 return ireq;
Dan Williamscc9203b2011-05-08 17:34:44 -07002582 }
2583 }
2584
2585 return NULL;
2586}
2587
2588/**
2589 * This method allocates remote node index and the reserves the remote node
2590 * context space for use. This method can fail if there are no more remote
2591 * node index available.
2592 * @scic: This is the controller object which contains the set of
2593 * free remote node ids
2594 * @sci_dev: This is the device object which is requesting the a remote node
2595 * id
2596 * @node_id: This is the remote node id that is assinged to the device if one
2597 * is available
2598 *
2599 * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2600 * node index available.
2601 */
2602enum sci_status scic_sds_controller_allocate_remote_node_context(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002603 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002604 struct isci_remote_device *idev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002605 u16 *node_id)
2606{
2607 u16 node_index;
Dan Williams78a6f062011-06-30 16:31:37 -07002608 u32 remote_node_count = scic_sds_remote_device_node_count(idev);
Dan Williamscc9203b2011-05-08 17:34:44 -07002609
2610 node_index = scic_sds_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002611 &ihost->available_remote_nodes, remote_node_count
Dan Williamscc9203b2011-05-08 17:34:44 -07002612 );
2613
2614 if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002615 ihost->device_table[node_index] = idev;
Dan Williamscc9203b2011-05-08 17:34:44 -07002616
2617 *node_id = node_index;
2618
2619 return SCI_SUCCESS;
2620 }
2621
2622 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2623}
2624
2625/**
2626 * This method frees the remote node index back to the available pool. Once
2627 * this is done the remote node context buffer is no longer valid and can
2628 * not be used.
2629 * @scic:
2630 * @sci_dev:
2631 * @node_id:
2632 *
2633 */
2634void scic_sds_controller_free_remote_node_context(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002635 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002636 struct isci_remote_device *idev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002637 u16 node_id)
2638{
Dan Williams78a6f062011-06-30 16:31:37 -07002639 u32 remote_node_count = scic_sds_remote_device_node_count(idev);
Dan Williamscc9203b2011-05-08 17:34:44 -07002640
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002641 if (ihost->device_table[node_id] == idev) {
2642 ihost->device_table[node_id] = NULL;
Dan Williamscc9203b2011-05-08 17:34:44 -07002643
2644 scic_sds_remote_node_table_release_remote_node_index(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002645 &ihost->available_remote_nodes, remote_node_count, node_id
Dan Williamscc9203b2011-05-08 17:34:44 -07002646 );
2647 }
2648}
2649
2650/**
2651 * This method returns the union scu_remote_node_context for the specified remote
2652 * node id.
2653 * @scic:
2654 * @node_id:
2655 *
2656 * union scu_remote_node_context*
2657 */
2658union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002659 struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07002660 u16 node_id
2661 ) {
2662 if (
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002663 (node_id < ihost->remote_node_entries)
2664 && (ihost->device_table[node_id] != NULL)
Dan Williamscc9203b2011-05-08 17:34:44 -07002665 ) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002666 return &ihost->remote_node_context_table[node_id];
Dan Williamscc9203b2011-05-08 17:34:44 -07002667 }
2668
2669 return NULL;
2670}
2671
2672/**
2673 *
2674 * @resposne_buffer: This is the buffer into which the D2H register FIS will be
2675 * constructed.
2676 * @frame_header: This is the frame header returned by the hardware.
2677 * @frame_buffer: This is the frame buffer returned by the hardware.
2678 *
2679 * This method will combind the frame header and frame buffer to create a SATA
2680 * D2H register FIS none
2681 */
2682void scic_sds_controller_copy_sata_response(
2683 void *response_buffer,
2684 void *frame_header,
2685 void *frame_buffer)
2686{
2687 memcpy(response_buffer, frame_header, sizeof(u32));
2688
2689 memcpy(response_buffer + sizeof(u32),
2690 frame_buffer,
2691 sizeof(struct dev_to_host_fis) - sizeof(u32));
2692}
2693
2694/**
2695 * This method releases the frame once this is done the frame is available for
2696 * re-use by the hardware. The data contained in the frame header and frame
2697 * buffer is no longer valid. The UF queue get pointer is only updated if UF
2698 * control indicates this is appropriate.
2699 * @scic:
2700 * @frame_index:
2701 *
2702 */
2703void scic_sds_controller_release_frame(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002704 struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -07002705 u32 frame_index)
2706{
2707 if (scic_sds_unsolicited_frame_control_release_frame(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002708 &ihost->uf_control, frame_index) == true)
2709 writel(ihost->uf_control.get,
2710 &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -07002711}
2712
Dan Williams312e0c22011-06-28 13:47:09 -07002713void isci_tci_free(struct isci_host *ihost, u16 tci)
2714{
2715 u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2716
2717 ihost->tci_pool[tail] = tci;
2718 ihost->tci_tail = tail + 1;
2719}
2720
2721static u16 isci_tci_alloc(struct isci_host *ihost)
2722{
2723 u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2724 u16 tci = ihost->tci_pool[head];
2725
2726 ihost->tci_head = head + 1;
2727 return tci;
2728}
2729
2730static u16 isci_tci_space(struct isci_host *ihost)
2731{
2732 return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2733}
2734
2735u16 isci_alloc_tag(struct isci_host *ihost)
2736{
2737 if (isci_tci_space(ihost)) {
2738 u16 tci = isci_tci_alloc(ihost);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002739 u8 seq = ihost->io_request_sequence[tci];
Dan Williams312e0c22011-06-28 13:47:09 -07002740
2741 return ISCI_TAG(seq, tci);
2742 }
2743
2744 return SCI_CONTROLLER_INVALID_IO_TAG;
2745}
2746
2747enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2748{
Dan Williams312e0c22011-06-28 13:47:09 -07002749 u16 tci = ISCI_TAG_TCI(io_tag);
2750 u16 seq = ISCI_TAG_SEQ(io_tag);
2751
2752 /* prevent tail from passing head */
2753 if (isci_tci_active(ihost) == 0)
2754 return SCI_FAILURE_INVALID_IO_TAG;
2755
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002756 if (seq == ihost->io_request_sequence[tci]) {
2757 ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
Dan Williams312e0c22011-06-28 13:47:09 -07002758
2759 isci_tci_free(ihost, tci);
2760
2761 return SCI_SUCCESS;
2762 }
2763 return SCI_FAILURE_INVALID_IO_TAG;
2764}
2765
Dan Williamscc9203b2011-05-08 17:34:44 -07002766/**
2767 * scic_controller_start_io() - This method is called by the SCI user to
2768 * send/start an IO request. If the method invocation is successful, then
2769 * the IO request has been queued to the hardware for processing.
2770 * @controller: the handle to the controller object for which to start an IO
2771 * request.
2772 * @remote_device: the handle to the remote device object for which to start an
2773 * IO request.
2774 * @io_request: the handle to the io request object to start.
2775 * @io_tag: This parameter specifies a previously allocated IO tag that the
Dan Williams312e0c22011-06-28 13:47:09 -07002776 * user desires to be utilized for this request.
Dan Williamscc9203b2011-05-08 17:34:44 -07002777 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002778enum sci_status scic_controller_start_io(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002779 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002780 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002781{
2782 enum sci_status status;
2783
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002784 if (ihost->sm.current_state_id != SCIC_READY) {
2785 dev_warn(&ihost->pdev->dev, "invalid state to start I/O");
Dan Williamscc9203b2011-05-08 17:34:44 -07002786 return SCI_FAILURE_INVALID_STATE;
2787 }
2788
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002789 status = scic_sds_remote_device_start_io(ihost, idev, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002790 if (status != SCI_SUCCESS)
2791 return status;
2792
Dan Williams5076a1a2011-06-27 14:57:03 -07002793 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002794 scic_sds_controller_post_request(ihost, scic_sds_request_get_post_context(ireq));
Dan Williamscc9203b2011-05-08 17:34:44 -07002795 return SCI_SUCCESS;
2796}
2797
2798/**
2799 * scic_controller_terminate_request() - This method is called by the SCI Core
2800 * user to terminate an ongoing (i.e. started) core IO request. This does
2801 * not abort the IO request at the target, but rather removes the IO request
2802 * from the host controller.
2803 * @controller: the handle to the controller object for which to terminate a
2804 * request.
2805 * @remote_device: the handle to the remote device object for which to
2806 * terminate a request.
2807 * @request: the handle to the io or task management request object to
2808 * terminate.
2809 *
2810 * Indicate if the controller successfully began the terminate process for the
2811 * IO request. SCI_SUCCESS if the terminate process was successfully started
2812 * for the request. Determine the failure situations and return values.
2813 */
2814enum sci_status scic_controller_terminate_request(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002815 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002816 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002817 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002818{
2819 enum sci_status status;
2820
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002821 if (ihost->sm.current_state_id != SCIC_READY) {
2822 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002823 "invalid state to terminate request\n");
2824 return SCI_FAILURE_INVALID_STATE;
2825 }
2826
Dan Williams5076a1a2011-06-27 14:57:03 -07002827 status = scic_sds_io_request_terminate(ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002828 if (status != SCI_SUCCESS)
2829 return status;
2830
2831 /*
2832 * Utilize the original post context command and or in the POST_TC_ABORT
2833 * request sub-type.
2834 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002835 scic_sds_controller_post_request(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -07002836 scic_sds_request_get_post_context(ireq) |
Dan Williamscc9203b2011-05-08 17:34:44 -07002837 SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2838 return SCI_SUCCESS;
2839}
2840
2841/**
2842 * scic_controller_complete_io() - This method will perform core specific
2843 * completion operations for an IO request. After this method is invoked,
2844 * the user should consider the IO request as invalid until it is properly
2845 * reused (i.e. re-constructed).
2846 * @controller: The handle to the controller object for which to complete the
2847 * IO request.
2848 * @remote_device: The handle to the remote device object for which to complete
2849 * the IO request.
2850 * @io_request: the handle to the io request object to complete.
Dan Williamscc9203b2011-05-08 17:34:44 -07002851 */
2852enum sci_status scic_controller_complete_io(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002853 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002854 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002855 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002856{
2857 enum sci_status status;
2858 u16 index;
2859
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002860 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002861 case SCIC_STOPPING:
Dan Williamscc9203b2011-05-08 17:34:44 -07002862 /* XXX: Implement this function */
2863 return SCI_FAILURE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002864 case SCIC_READY:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002865 status = scic_sds_remote_device_complete_io(ihost, idev, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002866 if (status != SCI_SUCCESS)
2867 return status;
2868
Dan Williams5076a1a2011-06-27 14:57:03 -07002869 index = ISCI_TAG_TCI(ireq->io_tag);
2870 clear_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07002871 return SCI_SUCCESS;
2872 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002873 dev_warn(&ihost->pdev->dev, "invalid state to complete I/O");
Dan Williamscc9203b2011-05-08 17:34:44 -07002874 return SCI_FAILURE_INVALID_STATE;
2875 }
2876
2877}
2878
Dan Williams5076a1a2011-06-27 14:57:03 -07002879enum sci_status scic_controller_continue_io(struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002880{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002881 struct isci_host *ihost = ireq->owning_controller;
Dan Williamscc9203b2011-05-08 17:34:44 -07002882
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002883 if (ihost->sm.current_state_id != SCIC_READY) {
2884 dev_warn(&ihost->pdev->dev, "invalid state to continue I/O");
Dan Williamscc9203b2011-05-08 17:34:44 -07002885 return SCI_FAILURE_INVALID_STATE;
2886 }
2887
Dan Williams5076a1a2011-06-27 14:57:03 -07002888 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002889 scic_sds_controller_post_request(ihost, scic_sds_request_get_post_context(ireq));
Dan Williamscc9203b2011-05-08 17:34:44 -07002890 return SCI_SUCCESS;
2891}
2892
2893/**
2894 * scic_controller_start_task() - This method is called by the SCIC user to
2895 * send/start a framework task management request.
2896 * @controller: the handle to the controller object for which to start the task
2897 * management request.
2898 * @remote_device: the handle to the remote device object for which to start
2899 * the task management request.
2900 * @task_request: the handle to the task request object to start.
Dan Williamscc9203b2011-05-08 17:34:44 -07002901 */
2902enum sci_task_status scic_controller_start_task(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002903 struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07002904 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07002905 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002906{
2907 enum sci_status status;
2908
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002909 if (ihost->sm.current_state_id != SCIC_READY) {
2910 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002911 "%s: SCIC Controller starting task from invalid "
2912 "state\n",
2913 __func__);
2914 return SCI_TASK_FAILURE_INVALID_STATE;
2915 }
2916
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002917 status = scic_sds_remote_device_start_task(ihost, idev, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002918 switch (status) {
2919 case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
Dan Williamsdb056252011-06-17 14:18:39 -07002920 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07002921
2922 /*
2923 * We will let framework know this task request started successfully,
2924 * although core is still woring on starting the request (to post tc when
2925 * RNC is resumed.)
2926 */
2927 return SCI_SUCCESS;
2928 case SCI_SUCCESS:
Dan Williamsdb056252011-06-17 14:18:39 -07002929 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07002930
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002931 scic_sds_controller_post_request(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -07002932 scic_sds_request_get_post_context(ireq));
Dan Williamscc9203b2011-05-08 17:34:44 -07002933 break;
2934 default:
2935 break;
2936 }
2937
2938 return status;
2939}