blob: 88e731333532d3b2866f096bf179839f664aa09f [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 Williams89a73012011-06-30 19:14:33 -0700183static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700184{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700185 u32 get_value = ihost->completion_queue_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700186 u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
187
188 if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700189 COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
Dan Williamscc9203b2011-05-08 17:34:44 -0700190 return true;
191
192 return false;
193}
194
Dan Williams89a73012011-06-30 19:14:33 -0700195static bool sci_controller_isr(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700196{
Dan Williams89a73012011-06-30 19:14:33 -0700197 if (sci_controller_completion_queue_has_entries(ihost)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700198 return true;
199 } else {
200 /*
201 * we have a spurious interrupt it could be that we have already
202 * emptied the completion queue from a previous interrupt */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700203 writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700204
205 /*
206 * There is a race in the hardware that could cause us not to be notified
207 * of an interrupt completion if we do not take this step. We will mask
208 * then unmask the interrupts so if there is another interrupt pending
209 * the clearing of the interrupt source we get the next interrupt message. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700210 writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
211 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700212 }
213
214 return false;
215}
216
Dan Williamsc7ef4032011-02-18 09:25:05 -0800217irqreturn_t isci_msix_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700218{
Dan Williamsc7ef4032011-02-18 09:25:05 -0800219 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700220
Dan Williams89a73012011-06-30 19:14:33 -0700221 if (sci_controller_isr(ihost))
Dan Williams0cf89d12011-02-18 09:25:07 -0800222 tasklet_schedule(&ihost->completion_tasklet);
Dan Williams6f231dd2011-07-02 22:56:22 -0700223
Dan Williamsc7ef4032011-02-18 09:25:05 -0800224 return IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700225}
226
Dan Williams89a73012011-06-30 19:14:33 -0700227static bool sci_controller_error_isr(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700228{
229 u32 interrupt_status;
230
231 interrupt_status =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700232 readl(&ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700233 interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
234
235 if (interrupt_status != 0) {
236 /*
237 * There is an error interrupt pending so let it through and handle
238 * in the callback */
239 return true;
240 }
241
242 /*
243 * There is a race in the hardware that could cause us not to be notified
244 * of an interrupt completion if we do not take this step. We will mask
245 * then unmask the error interrupts so if there was another interrupt
246 * pending we will be notified.
247 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700248 writel(0xff, &ihost->smu_registers->interrupt_mask);
249 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700250
251 return false;
252}
253
Dan Williams89a73012011-06-30 19:14:33 -0700254static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
Dan Williamscc9203b2011-05-08 17:34:44 -0700255{
Dan Williams89a73012011-06-30 19:14:33 -0700256 u32 index = SCU_GET_COMPLETION_INDEX(ent);
Dan Williamsdb056252011-06-17 14:18:39 -0700257 struct isci_request *ireq = ihost->reqs[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700258
259 /* Make sure that we really want to process this IO request */
Dan Williamsdb056252011-06-17 14:18:39 -0700260 if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
Dan Williams5076a1a2011-06-27 14:57:03 -0700261 ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700262 ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
Dan Williams89a73012011-06-30 19:14:33 -0700263 /* Yep this is a valid io request pass it along to the
264 * io request handler
265 */
266 sci_io_request_tc_completion(ireq, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700267}
268
Dan Williams89a73012011-06-30 19:14:33 -0700269static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
Dan Williamscc9203b2011-05-08 17:34:44 -0700270{
271 u32 index;
Dan Williams5076a1a2011-06-27 14:57:03 -0700272 struct isci_request *ireq;
Dan Williams78a6f062011-06-30 16:31:37 -0700273 struct isci_remote_device *idev;
Dan Williamscc9203b2011-05-08 17:34:44 -0700274
Dan Williams89a73012011-06-30 19:14:33 -0700275 index = SCU_GET_COMPLETION_INDEX(ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700276
Dan Williams89a73012011-06-30 19:14:33 -0700277 switch (scu_get_command_request_type(ent)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700278 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
279 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700280 ireq = ihost->reqs[index];
281 dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
Dan Williams89a73012011-06-30 19:14:33 -0700282 __func__, ent, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -0700283 /* @todo For a post TC operation we need to fail the IO
284 * request
285 */
286 break;
Dan Williamscc9203b2011-05-08 17:34:44 -0700287 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
288 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
289 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700290 idev = ihost->device_table[index];
291 dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
Dan Williams89a73012011-06-30 19:14:33 -0700292 __func__, ent, idev);
Dan Williamscc9203b2011-05-08 17:34:44 -0700293 /* @todo For a port RNC operation we need to fail the
294 * device
295 */
296 break;
Dan Williamscc9203b2011-05-08 17:34:44 -0700297 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700298 dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
Dan Williams89a73012011-06-30 19:14:33 -0700299 __func__, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700300 break;
Dan Williamscc9203b2011-05-08 17:34:44 -0700301 }
302}
303
Dan Williams89a73012011-06-30 19:14:33 -0700304static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
Dan Williamscc9203b2011-05-08 17:34:44 -0700305{
306 u32 index;
307 u32 frame_index;
308
Dan Williamscc9203b2011-05-08 17:34:44 -0700309 struct scu_unsolicited_frame_header *frame_header;
Dan Williams85280952011-06-28 15:05:53 -0700310 struct isci_phy *iphy;
Dan Williams78a6f062011-06-30 16:31:37 -0700311 struct isci_remote_device *idev;
Dan Williamscc9203b2011-05-08 17:34:44 -0700312
313 enum sci_status result = SCI_FAILURE;
314
Dan Williams89a73012011-06-30 19:14:33 -0700315 frame_index = SCU_GET_FRAME_INDEX(ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700316
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700317 frame_header = ihost->uf_control.buffers.array[frame_index].header;
318 ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
Dan Williamscc9203b2011-05-08 17:34:44 -0700319
Dan Williams89a73012011-06-30 19:14:33 -0700320 if (SCU_GET_FRAME_ERROR(ent)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700321 /*
322 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
323 * / this cause a problem? We expect the phy initialization will
324 * / fail if there is an error in the frame. */
Dan Williams89a73012011-06-30 19:14:33 -0700325 sci_controller_release_frame(ihost, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700326 return;
327 }
328
329 if (frame_header->is_address_frame) {
Dan Williams89a73012011-06-30 19:14:33 -0700330 index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
Dan Williams85280952011-06-28 15:05:53 -0700331 iphy = &ihost->phys[index];
Dan Williams89a73012011-06-30 19:14:33 -0700332 result = sci_phy_frame_handler(iphy, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700333 } else {
334
Dan Williams89a73012011-06-30 19:14:33 -0700335 index = SCU_GET_COMPLETION_INDEX(ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700336
337 if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
338 /*
339 * This is a signature fis or a frame from a direct attached SATA
340 * device that has not yet been created. In either case forwared
341 * the frame to the PE and let it take care of the frame data. */
Dan Williams89a73012011-06-30 19:14:33 -0700342 index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
Dan Williams85280952011-06-28 15:05:53 -0700343 iphy = &ihost->phys[index];
Dan Williams89a73012011-06-30 19:14:33 -0700344 result = sci_phy_frame_handler(iphy, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700345 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700346 if (index < ihost->remote_node_entries)
347 idev = ihost->device_table[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700348 else
Dan Williams78a6f062011-06-30 16:31:37 -0700349 idev = NULL;
Dan Williamscc9203b2011-05-08 17:34:44 -0700350
Dan Williams78a6f062011-06-30 16:31:37 -0700351 if (idev != NULL)
Dan Williams89a73012011-06-30 19:14:33 -0700352 result = sci_remote_device_frame_handler(idev, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700353 else
Dan Williams89a73012011-06-30 19:14:33 -0700354 sci_controller_release_frame(ihost, frame_index);
Dan Williamscc9203b2011-05-08 17:34:44 -0700355 }
356 }
357
358 if (result != SCI_SUCCESS) {
359 /*
360 * / @todo Is there any reason to report some additional error message
361 * / when we get this failure notifiction? */
362 }
363}
364
Dan Williams89a73012011-06-30 19:14:33 -0700365static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
Dan Williamscc9203b2011-05-08 17:34:44 -0700366{
Dan Williams78a6f062011-06-30 16:31:37 -0700367 struct isci_remote_device *idev;
Dan Williams5076a1a2011-06-27 14:57:03 -0700368 struct isci_request *ireq;
Dan Williams85280952011-06-28 15:05:53 -0700369 struct isci_phy *iphy;
Dan Williamscc9203b2011-05-08 17:34:44 -0700370 u32 index;
371
Dan Williams89a73012011-06-30 19:14:33 -0700372 index = SCU_GET_COMPLETION_INDEX(ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700373
Dan Williams89a73012011-06-30 19:14:33 -0700374 switch (scu_get_event_type(ent)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700375 case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
376 /* / @todo The driver did something wrong and we need to fix the condtion. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700377 dev_err(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700378 "%s: SCIC Controller 0x%p received SMU command error "
379 "0x%x\n",
380 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700381 ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700382 ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700383 break;
384
385 case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
386 case SCU_EVENT_TYPE_SMU_ERROR:
387 case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
388 /*
389 * / @todo This is a hardware failure and its likely that we want to
390 * / reset the controller. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700391 dev_err(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700392 "%s: SCIC Controller 0x%p received fatal controller "
393 "event 0x%x\n",
394 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700395 ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700396 ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700397 break;
398
399 case SCU_EVENT_TYPE_TRANSPORT_ERROR:
Dan Williams5076a1a2011-06-27 14:57:03 -0700400 ireq = ihost->reqs[index];
Dan Williams89a73012011-06-30 19:14:33 -0700401 sci_io_request_event_handler(ireq, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700402 break;
403
404 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
Dan Williams89a73012011-06-30 19:14:33 -0700405 switch (scu_get_event_specifier(ent)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700406 case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
407 case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
Dan Williams5076a1a2011-06-27 14:57:03 -0700408 ireq = ihost->reqs[index];
409 if (ireq != NULL)
Dan Williams89a73012011-06-30 19:14:33 -0700410 sci_io_request_event_handler(ireq, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700411 else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700412 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700413 "%s: SCIC Controller 0x%p received "
414 "event 0x%x for io request object "
415 "that doesnt exist.\n",
416 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700417 ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700418 ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700419
420 break;
421
422 case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700423 idev = ihost->device_table[index];
Dan Williams78a6f062011-06-30 16:31:37 -0700424 if (idev != NULL)
Dan Williams89a73012011-06-30 19:14:33 -0700425 sci_remote_device_event_handler(idev, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700426 else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700427 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700428 "%s: SCIC Controller 0x%p received "
429 "event 0x%x for remote device object "
430 "that doesnt exist.\n",
431 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700432 ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700433 ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700434
435 break;
436 }
437 break;
438
439 case SCU_EVENT_TYPE_BROADCAST_CHANGE:
440 /*
441 * direct the broadcast change event to the phy first and then let
442 * the phy redirect the broadcast change to the port object */
443 case SCU_EVENT_TYPE_ERR_CNT_EVENT:
444 /*
445 * direct error counter event to the phy object since that is where
446 * we get the event notification. This is a type 4 event. */
447 case SCU_EVENT_TYPE_OSSP_EVENT:
Dan Williams89a73012011-06-30 19:14:33 -0700448 index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
Dan Williams85280952011-06-28 15:05:53 -0700449 iphy = &ihost->phys[index];
Dan Williams89a73012011-06-30 19:14:33 -0700450 sci_phy_event_handler(iphy, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700451 break;
452
453 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
454 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
455 case SCU_EVENT_TYPE_RNC_OPS_MISC:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700456 if (index < ihost->remote_node_entries) {
457 idev = ihost->device_table[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700458
Dan Williams78a6f062011-06-30 16:31:37 -0700459 if (idev != NULL)
Dan Williams89a73012011-06-30 19:14:33 -0700460 sci_remote_device_event_handler(idev, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700461 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700462 dev_err(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700463 "%s: SCIC Controller 0x%p received event 0x%x "
464 "for remote device object 0x%0x that doesnt "
465 "exist.\n",
466 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700467 ihost,
Dan Williams89a73012011-06-30 19:14:33 -0700468 ent,
Dan Williamscc9203b2011-05-08 17:34:44 -0700469 index);
470
471 break;
472
473 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700474 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700475 "%s: SCIC Controller received unknown event code %x\n",
476 __func__,
Dan Williams89a73012011-06-30 19:14:33 -0700477 ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700478 break;
479 }
480}
481
Dan Williams89a73012011-06-30 19:14:33 -0700482static void sci_controller_process_completions(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700483{
484 u32 completion_count = 0;
Dan Williams89a73012011-06-30 19:14:33 -0700485 u32 ent;
Dan Williamscc9203b2011-05-08 17:34:44 -0700486 u32 get_index;
487 u32 get_cycle;
Dan Williams994a9302011-06-09 16:04:28 -0700488 u32 event_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700489 u32 event_cycle;
490
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700491 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700492 "%s: completion queue begining get:0x%08x\n",
493 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700494 ihost->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700495
496 /* Get the component parts of the completion queue */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700497 get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
498 get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700499
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700500 event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
501 event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700502
503 while (
504 NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700505 == COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
Dan Williamscc9203b2011-05-08 17:34:44 -0700506 ) {
507 completion_count++;
508
Dan Williams89a73012011-06-30 19:14:33 -0700509 ent = ihost->completion_queue[get_index];
Dan Williams994a9302011-06-09 16:04:28 -0700510
511 /* increment the get pointer and check for rollover to toggle the cycle bit */
512 get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
513 (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
514 get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
Dan Williamscc9203b2011-05-08 17:34:44 -0700515
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700516 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700517 "%s: completion queue entry:0x%08x\n",
518 __func__,
Dan Williams89a73012011-06-30 19:14:33 -0700519 ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700520
Dan Williams89a73012011-06-30 19:14:33 -0700521 switch (SCU_GET_COMPLETION_TYPE(ent)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700522 case SCU_COMPLETION_TYPE_TASK:
Dan Williams89a73012011-06-30 19:14:33 -0700523 sci_controller_task_completion(ihost, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700524 break;
525
526 case SCU_COMPLETION_TYPE_SDMA:
Dan Williams89a73012011-06-30 19:14:33 -0700527 sci_controller_sdma_completion(ihost, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700528 break;
529
530 case SCU_COMPLETION_TYPE_UFI:
Dan Williams89a73012011-06-30 19:14:33 -0700531 sci_controller_unsolicited_frame(ihost, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700532 break;
533
534 case SCU_COMPLETION_TYPE_EVENT:
Dan Williams994a9302011-06-09 16:04:28 -0700535 case SCU_COMPLETION_TYPE_NOTIFY: {
536 event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
537 (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
538 event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
539
Dan Williams89a73012011-06-30 19:14:33 -0700540 sci_controller_event_completion(ihost, ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700541 break;
Dan Williams994a9302011-06-09 16:04:28 -0700542 }
Dan Williamscc9203b2011-05-08 17:34:44 -0700543 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700544 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700545 "%s: SCIC Controller received unknown "
546 "completion type %x\n",
547 __func__,
Dan Williams89a73012011-06-30 19:14:33 -0700548 ent);
Dan Williamscc9203b2011-05-08 17:34:44 -0700549 break;
550 }
551 }
552
553 /* Update the get register if we completed one or more entries */
554 if (completion_count > 0) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700555 ihost->completion_queue_get =
Dan Williamscc9203b2011-05-08 17:34:44 -0700556 SMU_CQGR_GEN_BIT(ENABLE) |
557 SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
558 event_cycle |
Dan Williams994a9302011-06-09 16:04:28 -0700559 SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
Dan Williamscc9203b2011-05-08 17:34:44 -0700560 get_cycle |
561 SMU_CQGR_GEN_VAL(POINTER, get_index);
562
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700563 writel(ihost->completion_queue_get,
564 &ihost->smu_registers->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700565
566 }
567
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700568 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700569 "%s: completion queue ending get:0x%08x\n",
570 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700571 ihost->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700572
573}
574
Dan Williams89a73012011-06-30 19:14:33 -0700575static void sci_controller_error_handler(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700576{
577 u32 interrupt_status;
578
579 interrupt_status =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700580 readl(&ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700581
582 if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
Dan Williams89a73012011-06-30 19:14:33 -0700583 sci_controller_completion_queue_has_entries(ihost)) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700584
Dan Williams89a73012011-06-30 19:14:33 -0700585 sci_controller_process_completions(ihost);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700586 writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -0700587 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700588 dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
Dan Williamscc9203b2011-05-08 17:34:44 -0700589 interrupt_status);
590
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700591 sci_change_state(&ihost->sm, SCIC_FAILED);
Dan Williamscc9203b2011-05-08 17:34:44 -0700592
593 return;
594 }
595
596 /* If we dont process any completions I am not sure that we want to do this.
597 * We are in the middle of a hardware fault and should probably be reset.
598 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700599 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700600}
601
Dan Williamsc7ef4032011-02-18 09:25:05 -0800602irqreturn_t isci_intx_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700603{
Dan Williams6f231dd2011-07-02 22:56:22 -0700604 irqreturn_t ret = IRQ_NONE;
Dan Williams31e824e2011-04-19 12:32:51 -0700605 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700606
Dan Williams89a73012011-06-30 19:14:33 -0700607 if (sci_controller_isr(ihost)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700608 writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
Dan Williams31e824e2011-04-19 12:32:51 -0700609 tasklet_schedule(&ihost->completion_tasklet);
610 ret = IRQ_HANDLED;
Dan Williams89a73012011-06-30 19:14:33 -0700611 } else if (sci_controller_error_isr(ihost)) {
Dan Williams31e824e2011-04-19 12:32:51 -0700612 spin_lock(&ihost->scic_lock);
Dan Williams89a73012011-06-30 19:14:33 -0700613 sci_controller_error_handler(ihost);
Dan Williams31e824e2011-04-19 12:32:51 -0700614 spin_unlock(&ihost->scic_lock);
615 ret = IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700616 }
Dan Williams92f4f0f2011-02-18 09:25:11 -0800617
Dan Williams6f231dd2011-07-02 22:56:22 -0700618 return ret;
619}
620
Dan Williams92f4f0f2011-02-18 09:25:11 -0800621irqreturn_t isci_error_isr(int vec, void *data)
622{
623 struct isci_host *ihost = data;
Dan Williams92f4f0f2011-02-18 09:25:11 -0800624
Dan Williams89a73012011-06-30 19:14:33 -0700625 if (sci_controller_error_isr(ihost))
626 sci_controller_error_handler(ihost);
Dan Williams92f4f0f2011-02-18 09:25:11 -0800627
628 return IRQ_HANDLED;
629}
Dan Williams6f231dd2011-07-02 22:56:22 -0700630
631/**
632 * isci_host_start_complete() - This function is called by the core library,
633 * through the ISCI Module, to indicate controller start status.
634 * @isci_host: This parameter specifies the ISCI host object
635 * @completion_status: This parameter specifies the completion status from the
636 * core library.
637 *
638 */
Dan Williamscc9203b2011-05-08 17:34:44 -0700639static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700640{
Dan Williams0cf89d12011-02-18 09:25:07 -0800641 if (completion_status != SCI_SUCCESS)
642 dev_info(&ihost->pdev->dev,
643 "controller start timed out, continuing...\n");
644 isci_host_change_state(ihost, isci_ready);
645 clear_bit(IHOST_START_PENDING, &ihost->flags);
646 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700647}
648
Dan Williamsc7ef4032011-02-18 09:25:05 -0800649int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
Dan Williams6f231dd2011-07-02 22:56:22 -0700650{
Dan Williams4393aa42011-03-31 13:10:44 -0700651 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williams6f231dd2011-07-02 22:56:22 -0700652
Edmund Nadolski77950f52011-02-18 09:25:09 -0800653 if (test_bit(IHOST_START_PENDING, &ihost->flags))
Dan Williams6f231dd2011-07-02 22:56:22 -0700654 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700655
Edmund Nadolski77950f52011-02-18 09:25:09 -0800656 /* todo: use sas_flush_discovery once it is upstream */
657 scsi_flush_work(shost);
658
659 scsi_flush_work(shost);
Dan Williams6f231dd2011-07-02 22:56:22 -0700660
Dan Williams0cf89d12011-02-18 09:25:07 -0800661 dev_dbg(&ihost->pdev->dev,
662 "%s: ihost->status = %d, time = %ld\n",
663 __func__, isci_host_get_state(ihost), time);
Dan Williams6f231dd2011-07-02 22:56:22 -0700664
Dan Williams6f231dd2011-07-02 22:56:22 -0700665 return 1;
666
667}
668
Dan Williamscc9203b2011-05-08 17:34:44 -0700669/**
Dan Williams89a73012011-06-30 19:14:33 -0700670 * sci_controller_get_suggested_start_timeout() - This method returns the
671 * suggested sci_controller_start() timeout amount. The user is free to
Dan Williamscc9203b2011-05-08 17:34:44 -0700672 * use any timeout value, but this method provides the suggested minimum
673 * start timeout value. The returned value is based upon empirical
674 * information determined as a result of interoperability testing.
675 * @controller: the handle to the controller object for which to return the
676 * suggested start timeout.
677 *
678 * This method returns the number of milliseconds for the suggested start
679 * operation timeout.
680 */
Dan Williams89a73012011-06-30 19:14:33 -0700681static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700682{
683 /* Validate the user supplied parameters. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700684 if (!ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700685 return 0;
686
687 /*
688 * The suggested minimum timeout value for a controller start operation:
689 *
690 * Signature FIS Timeout
691 * + Phy Start Timeout
692 * + Number of Phy Spin Up Intervals
693 * ---------------------------------
694 * Number of milliseconds for the controller start operation.
695 *
696 * NOTE: The number of phy spin up intervals will be equivalent
697 * to the number of phys divided by the number phys allowed
698 * per interval - 1 (once OEM parameters are supported).
699 * Currently we assume only 1 phy per interval. */
700
701 return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
702 + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
703 + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
704}
705
Dan Williams89a73012011-06-30 19:14:33 -0700706static void sci_controller_enable_interrupts(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700707{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700708 BUG_ON(ihost->smu_registers == NULL);
709 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700710}
711
Dan Williams89a73012011-06-30 19:14:33 -0700712void sci_controller_disable_interrupts(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700713{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700714 BUG_ON(ihost->smu_registers == NULL);
715 writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -0700716}
717
Dan Williams89a73012011-06-30 19:14:33 -0700718static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700719{
720 u32 port_task_scheduler_value;
721
722 port_task_scheduler_value =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700723 readl(&ihost->scu_registers->peg0.ptsg.control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700724 port_task_scheduler_value |=
725 (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
726 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
727 writel(port_task_scheduler_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700728 &ihost->scu_registers->peg0.ptsg.control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700729}
730
Dan Williams89a73012011-06-30 19:14:33 -0700731static void sci_controller_assign_task_entries(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700732{
733 u32 task_assignment;
734
735 /*
736 * Assign all the TCs to function 0
737 * TODO: Do we actually need to read this register to write it back?
738 */
739
740 task_assignment =
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700741 readl(&ihost->smu_registers->task_context_assignment[0]);
Dan Williamscc9203b2011-05-08 17:34:44 -0700742
743 task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700744 (SMU_TCA_GEN_VAL(ENDING, ihost->task_context_entries - 1)) |
Dan Williamscc9203b2011-05-08 17:34:44 -0700745 (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
746
747 writel(task_assignment,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700748 &ihost->smu_registers->task_context_assignment[0]);
Dan Williamscc9203b2011-05-08 17:34:44 -0700749
750}
751
Dan Williams89a73012011-06-30 19:14:33 -0700752static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700753{
754 u32 index;
755 u32 completion_queue_control_value;
756 u32 completion_queue_get_value;
757 u32 completion_queue_put_value;
758
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700759 ihost->completion_queue_get = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -0700760
Dan Williams7c78da32011-06-01 16:00:01 -0700761 completion_queue_control_value =
762 (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
763 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
Dan Williamscc9203b2011-05-08 17:34:44 -0700764
765 writel(completion_queue_control_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700766 &ihost->smu_registers->completion_queue_control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700767
768
769 /* Set the completion queue get pointer and enable the queue */
770 completion_queue_get_value = (
771 (SMU_CQGR_GEN_VAL(POINTER, 0))
772 | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
773 | (SMU_CQGR_GEN_BIT(ENABLE))
774 | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
775 );
776
777 writel(completion_queue_get_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700778 &ihost->smu_registers->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700779
780 /* Set the completion queue put pointer */
781 completion_queue_put_value = (
782 (SMU_CQPR_GEN_VAL(POINTER, 0))
783 | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
784 );
785
786 writel(completion_queue_put_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700787 &ihost->smu_registers->completion_queue_put);
Dan Williamscc9203b2011-05-08 17:34:44 -0700788
789 /* Initialize the cycle bit of the completion queue entries */
Dan Williams7c78da32011-06-01 16:00:01 -0700790 for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700791 /*
792 * If get.cycle_bit != completion_queue.cycle_bit
793 * its not a valid completion queue entry
794 * so at system start all entries are invalid */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700795 ihost->completion_queue[index] = 0x80000000;
Dan Williamscc9203b2011-05-08 17:34:44 -0700796 }
797}
798
Dan Williams89a73012011-06-30 19:14:33 -0700799static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700800{
801 u32 frame_queue_control_value;
802 u32 frame_queue_get_value;
803 u32 frame_queue_put_value;
804
805 /* Write the queue size */
806 frame_queue_control_value =
Dan Williams7c78da32011-06-01 16:00:01 -0700807 SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
Dan Williamscc9203b2011-05-08 17:34:44 -0700808
809 writel(frame_queue_control_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700810 &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
Dan Williamscc9203b2011-05-08 17:34:44 -0700811
812 /* Setup the get pointer for the unsolicited frame queue */
813 frame_queue_get_value = (
814 SCU_UFQGP_GEN_VAL(POINTER, 0)
815 | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
816 );
817
818 writel(frame_queue_get_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700819 &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -0700820 /* Setup the put pointer for the unsolicited frame queue */
821 frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
822 writel(frame_queue_put_value,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700823 &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -0700824}
825
Dan Williams89a73012011-06-30 19:14:33 -0700826static void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
Dan Williamscc9203b2011-05-08 17:34:44 -0700827{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700828 if (ihost->sm.current_state_id == SCIC_STARTING) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700829 /*
830 * We move into the ready state, because some of the phys/ports
831 * may be up and operational.
832 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700833 sci_change_state(&ihost->sm, SCIC_READY);
Dan Williamscc9203b2011-05-08 17:34:44 -0700834
835 isci_host_start_complete(ihost, status);
836 }
837}
838
Dan Williams85280952011-06-28 15:05:53 -0700839static bool is_phy_starting(struct isci_phy *iphy)
Adam Gruchala4a33c522011-05-10 23:54:23 +0000840{
Dan Williams89a73012011-06-30 19:14:33 -0700841 enum sci_phy_states state;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000842
Dan Williams85280952011-06-28 15:05:53 -0700843 state = iphy->sm.current_state_id;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000844 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000845 case SCI_PHY_STARTING:
846 case SCI_PHY_SUB_INITIAL:
847 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
848 case SCI_PHY_SUB_AWAIT_IAF_UF:
849 case SCI_PHY_SUB_AWAIT_SAS_POWER:
850 case SCI_PHY_SUB_AWAIT_SATA_POWER:
851 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
852 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
853 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
854 case SCI_PHY_SUB_FINAL:
Adam Gruchala4a33c522011-05-10 23:54:23 +0000855 return true;
856 default:
857 return false;
858 }
859}
860
Dan Williamscc9203b2011-05-08 17:34:44 -0700861/**
Dan Williams89a73012011-06-30 19:14:33 -0700862 * sci_controller_start_next_phy - start phy
Dan Williamscc9203b2011-05-08 17:34:44 -0700863 * @scic: controller
864 *
865 * If all the phys have been started, then attempt to transition the
866 * controller to the READY state and inform the user
Dan Williams89a73012011-06-30 19:14:33 -0700867 * (sci_cb_controller_start_complete()).
Dan Williamscc9203b2011-05-08 17:34:44 -0700868 */
Dan Williams89a73012011-06-30 19:14:33 -0700869static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -0700870{
Dan Williams89a73012011-06-30 19:14:33 -0700871 struct sci_oem_params *oem = &ihost->oem_parameters;
Dan Williams85280952011-06-28 15:05:53 -0700872 struct isci_phy *iphy;
Dan Williamscc9203b2011-05-08 17:34:44 -0700873 enum sci_status status;
874
875 status = SCI_SUCCESS;
876
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700877 if (ihost->phy_startup_timer_pending)
Dan Williamscc9203b2011-05-08 17:34:44 -0700878 return status;
879
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700880 if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700881 bool is_controller_start_complete = true;
882 u32 state;
883 u8 index;
884
885 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700886 iphy = &ihost->phys[index];
887 state = iphy->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -0700888
Dan Williams85280952011-06-28 15:05:53 -0700889 if (!phy_get_non_dummy_port(iphy))
Dan Williamscc9203b2011-05-08 17:34:44 -0700890 continue;
891
892 /* The controller start operation is complete iff:
893 * - all links have been given an opportunity to start
894 * - have no indication of a connected device
895 * - have an indication of a connected device and it has
896 * finished the link training process.
897 */
Dan Williams85280952011-06-28 15:05:53 -0700898 if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
899 (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
900 (iphy->is_in_link_training == true && is_phy_starting(iphy))) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700901 is_controller_start_complete = false;
902 break;
903 }
904 }
905
906 /*
907 * The controller has successfully finished the start process.
908 * Inform the SCI Core user and transition to the READY state. */
909 if (is_controller_start_complete == true) {
Dan Williams89a73012011-06-30 19:14:33 -0700910 sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700911 sci_del_timer(&ihost->phy_timer);
912 ihost->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -0700913 }
914 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700915 iphy = &ihost->phys[ihost->next_phy_to_start];
Dan Williamscc9203b2011-05-08 17:34:44 -0700916
917 if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dan Williams85280952011-06-28 15:05:53 -0700918 if (phy_get_non_dummy_port(iphy) == NULL) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700919 ihost->next_phy_to_start++;
Dan Williamscc9203b2011-05-08 17:34:44 -0700920
921 /* Caution recursion ahead be forwarned
922 *
923 * The PHY was never added to a PORT in MPC mode
924 * so start the next phy in sequence This phy
925 * will never go link up and will not draw power
926 * the OEM parameters either configured the phy
927 * incorrectly for the PORT or it was never
928 * assigned to a PORT
929 */
Dan Williams89a73012011-06-30 19:14:33 -0700930 return sci_controller_start_next_phy(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -0700931 }
932 }
933
Dan Williams89a73012011-06-30 19:14:33 -0700934 status = sci_phy_start(iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -0700935
936 if (status == SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700937 sci_mod_timer(&ihost->phy_timer,
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700938 SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700939 ihost->phy_startup_timer_pending = true;
Dan Williamscc9203b2011-05-08 17:34:44 -0700940 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700941 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700942 "%s: Controller stop operation failed "
943 "to stop phy %d because of status "
944 "%d.\n",
945 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700946 ihost->phys[ihost->next_phy_to_start].phy_index,
Dan Williamscc9203b2011-05-08 17:34:44 -0700947 status);
948 }
949
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700950 ihost->next_phy_to_start++;
Dan Williamscc9203b2011-05-08 17:34:44 -0700951 }
952
953 return status;
954}
955
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700956static void phy_startup_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -0700957{
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700958 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700959 struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700960 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -0700961 enum sci_status status;
962
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700963 spin_lock_irqsave(&ihost->scic_lock, flags);
964
965 if (tmr->cancel)
966 goto done;
967
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700968 ihost->phy_startup_timer_pending = false;
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700969
970 do {
Dan Williams89a73012011-06-30 19:14:33 -0700971 status = sci_controller_start_next_phy(ihost);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700972 } while (status != SCI_SUCCESS);
973
974done:
975 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -0700976}
977
Dan Williamsac668c62011-06-07 18:50:55 -0700978static u16 isci_tci_active(struct isci_host *ihost)
979{
980 return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
981}
982
Dan Williams89a73012011-06-30 19:14:33 -0700983static enum sci_status sci_controller_start(struct isci_host *ihost,
Dan Williamscc9203b2011-05-08 17:34:44 -0700984 u32 timeout)
985{
Dan Williamscc9203b2011-05-08 17:34:44 -0700986 enum sci_status result;
987 u16 index;
988
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700989 if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
990 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -0700991 "SCIC Controller start operation requested in "
992 "invalid state\n");
993 return SCI_FAILURE_INVALID_STATE;
994 }
995
996 /* Build the TCi free pool */
Dan Williamsac668c62011-06-07 18:50:55 -0700997 BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
998 ihost->tci_head = 0;
999 ihost->tci_tail = 0;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001000 for (index = 0; index < ihost->task_context_entries; index++)
Dan Williamsac668c62011-06-07 18:50:55 -07001001 isci_tci_free(ihost, index);
Dan Williamscc9203b2011-05-08 17:34:44 -07001002
1003 /* Build the RNi free pool */
Dan Williams89a73012011-06-30 19:14:33 -07001004 sci_remote_node_table_initialize(&ihost->available_remote_nodes,
1005 ihost->remote_node_entries);
Dan Williamscc9203b2011-05-08 17:34:44 -07001006
1007 /*
1008 * Before anything else lets make sure we will not be
1009 * interrupted by the hardware.
1010 */
Dan Williams89a73012011-06-30 19:14:33 -07001011 sci_controller_disable_interrupts(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001012
1013 /* Enable the port task scheduler */
Dan Williams89a73012011-06-30 19:14:33 -07001014 sci_controller_enable_port_task_scheduler(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001015
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001016 /* Assign all the task entries to ihost physical function */
Dan Williams89a73012011-06-30 19:14:33 -07001017 sci_controller_assign_task_entries(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001018
1019 /* Now initialize the completion queue */
Dan Williams89a73012011-06-30 19:14:33 -07001020 sci_controller_initialize_completion_queue(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001021
1022 /* Initialize the unsolicited frame queue for use */
Dan Williams89a73012011-06-30 19:14:33 -07001023 sci_controller_initialize_unsolicited_frame_queue(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001024
1025 /* Start all of the ports on this controller */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001026 for (index = 0; index < ihost->logical_port_entries; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001027 struct isci_port *iport = &ihost->ports[index];
Dan Williamscc9203b2011-05-08 17:34:44 -07001028
Dan Williams89a73012011-06-30 19:14:33 -07001029 result = sci_port_start(iport);
Dan Williamscc9203b2011-05-08 17:34:44 -07001030 if (result)
1031 return result;
1032 }
1033
Dan Williams89a73012011-06-30 19:14:33 -07001034 sci_controller_start_next_phy(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001035
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001036 sci_mod_timer(&ihost->timer, timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07001037
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001038 sci_change_state(&ihost->sm, SCIC_STARTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001039
1040 return SCI_SUCCESS;
1041}
1042
Dan Williams6f231dd2011-07-02 22:56:22 -07001043void isci_host_scan_start(struct Scsi_Host *shost)
1044{
Dan Williams4393aa42011-03-31 13:10:44 -07001045 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williams89a73012011-06-30 19:14:33 -07001046 unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07001047
Dan Williams0cf89d12011-02-18 09:25:07 -08001048 set_bit(IHOST_START_PENDING, &ihost->flags);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001049
1050 spin_lock_irq(&ihost->scic_lock);
Dan Williams89a73012011-06-30 19:14:33 -07001051 sci_controller_start(ihost, tmo);
1052 sci_controller_enable_interrupts(ihost);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001053 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001054}
1055
Dan Williamscc9203b2011-05-08 17:34:44 -07001056static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001057{
Dan Williams0cf89d12011-02-18 09:25:07 -08001058 isci_host_change_state(ihost, isci_stopped);
Dan Williams89a73012011-06-30 19:14:33 -07001059 sci_controller_disable_interrupts(ihost);
Dan Williams0cf89d12011-02-18 09:25:07 -08001060 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
1061 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001062}
1063
Dan Williams89a73012011-06-30 19:14:33 -07001064static void sci_controller_completion_handler(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001065{
1066 /* Empty out the completion queue */
Dan Williams89a73012011-06-30 19:14:33 -07001067 if (sci_controller_completion_queue_has_entries(ihost))
1068 sci_controller_process_completions(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001069
1070 /* Clear the interrupt and enable all interrupts again */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001071 writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001072 /* Could we write the value of SMU_ISR_COMPLETION? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001073 writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
1074 writel(0, &ihost->smu_registers->interrupt_mask);
Dan Williamscc9203b2011-05-08 17:34:44 -07001075}
1076
Dan Williams6f231dd2011-07-02 22:56:22 -07001077/**
1078 * isci_host_completion_routine() - This function is the delayed service
1079 * routine that calls the sci core library's completion handler. It's
1080 * scheduled as a tasklet from the interrupt service routine when interrupts
1081 * in use, or set as the timeout function in polled mode.
1082 * @data: This parameter specifies the ISCI host object
1083 *
1084 */
1085static void isci_host_completion_routine(unsigned long data)
1086{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001087 struct isci_host *ihost = (struct isci_host *)data;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001088 struct list_head completed_request_list;
1089 struct list_head errored_request_list;
1090 struct list_head *current_position;
1091 struct list_head *next_position;
Dan Williams6f231dd2011-07-02 22:56:22 -07001092 struct isci_request *request;
1093 struct isci_request *next_request;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001094 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -07001095
1096 INIT_LIST_HEAD(&completed_request_list);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001097 INIT_LIST_HEAD(&errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001098
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001099 spin_lock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001100
Dan Williams89a73012011-06-30 19:14:33 -07001101 sci_controller_completion_handler(ihost);
Dan Williamsc7ef4032011-02-18 09:25:05 -08001102
Dan Williams6f231dd2011-07-02 22:56:22 -07001103 /* Take the lists of completed I/Os from the host. */
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001104
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001105 list_splice_init(&ihost->requests_to_complete,
Dan Williams6f231dd2011-07-02 22:56:22 -07001106 &completed_request_list);
1107
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001108 /* Take the list of errored I/Os from the host. */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001109 list_splice_init(&ihost->requests_to_errorback,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001110 &errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001111
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001112 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001113
1114 /* Process any completions in the lists. */
1115 list_for_each_safe(current_position, next_position,
1116 &completed_request_list) {
1117
1118 request = list_entry(current_position, struct isci_request,
1119 completed_node);
1120 task = isci_request_access_task(request);
1121
1122 /* Normal notification (task_done) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001123 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001124 "%s: Normal - request/task = %p/%p\n",
1125 __func__,
1126 request,
1127 task);
1128
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001129 /* Return the task to libsas */
1130 if (task != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001131
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001132 task->lldd_task = NULL;
1133 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1134
1135 /* If the task is already in the abort path,
1136 * the task_done callback cannot be called.
1137 */
1138 task->task_done(task);
1139 }
1140 }
Dan Williams312e0c22011-06-28 13:47:09 -07001141
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001142 spin_lock_irq(&ihost->scic_lock);
1143 isci_free_tag(ihost, request->io_tag);
1144 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001145 }
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001146 list_for_each_entry_safe(request, next_request, &errored_request_list,
Dan Williams6f231dd2011-07-02 22:56:22 -07001147 completed_node) {
1148
1149 task = isci_request_access_task(request);
1150
1151 /* Use sas_task_abort */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001152 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001153 "%s: Error - request/task = %p/%p\n",
1154 __func__,
1155 request,
1156 task);
1157
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001158 if (task != NULL) {
1159
1160 /* Put the task into the abort path if it's not there
1161 * already.
1162 */
1163 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
1164 sas_task_abort(task);
1165
1166 } else {
1167 /* This is a case where the request has completed with a
1168 * status such that it needed further target servicing,
1169 * but the sas_task reference has already been removed
1170 * from the request. Since it was errored, it was not
1171 * being aborted, so there is nothing to do except free
1172 * it.
1173 */
1174
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001175 spin_lock_irq(&ihost->scic_lock);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001176 /* Remove the request from the remote device's list
1177 * of pending requests.
1178 */
1179 list_del_init(&request->dev_node);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001180 isci_free_tag(ihost, request->io_tag);
1181 spin_unlock_irq(&ihost->scic_lock);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001182 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001183 }
1184
1185}
1186
Dan Williamscc9203b2011-05-08 17:34:44 -07001187/**
Dan Williams89a73012011-06-30 19:14:33 -07001188 * sci_controller_stop() - This method will stop an individual controller
Dan Williamscc9203b2011-05-08 17:34:44 -07001189 * object.This method will invoke the associated user callback upon
1190 * completion. The completion callback is called when the following
1191 * conditions are met: -# the method return status is SCI_SUCCESS. -# the
1192 * controller has been quiesced. This method will ensure that all IO
1193 * requests are quiesced, phys are stopped, and all additional operation by
1194 * the hardware is halted.
1195 * @controller: the handle to the controller object to stop.
1196 * @timeout: This parameter specifies the number of milliseconds in which the
1197 * stop operation should complete.
1198 *
1199 * The controller must be in the STARTED or STOPPED state. Indicate if the
1200 * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1201 * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1202 * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1203 * controller is not either in the STARTED or STOPPED states.
1204 */
Dan Williams89a73012011-06-30 19:14:33 -07001205static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
Dan Williamscc9203b2011-05-08 17:34:44 -07001206{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001207 if (ihost->sm.current_state_id != SCIC_READY) {
1208 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001209 "SCIC Controller stop operation requested in "
1210 "invalid state\n");
1211 return SCI_FAILURE_INVALID_STATE;
1212 }
1213
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001214 sci_mod_timer(&ihost->timer, timeout);
1215 sci_change_state(&ihost->sm, SCIC_STOPPING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001216 return SCI_SUCCESS;
1217}
1218
1219/**
Dan Williams89a73012011-06-30 19:14:33 -07001220 * sci_controller_reset() - This method will reset the supplied core
Dan Williamscc9203b2011-05-08 17:34:44 -07001221 * controller regardless of the state of said controller. This operation is
1222 * considered destructive. In other words, all current operations are wiped
1223 * out. No IO completions for outstanding devices occur. Outstanding IO
1224 * requests are not aborted or completed at the actual remote device.
1225 * @controller: the handle to the controller object to reset.
1226 *
1227 * Indicate if the controller reset method succeeded or failed in some way.
1228 * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1229 * the controller reset operation is unable to complete.
1230 */
Dan Williams89a73012011-06-30 19:14:33 -07001231static enum sci_status sci_controller_reset(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001232{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001233 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001234 case SCIC_RESET:
1235 case SCIC_READY:
1236 case SCIC_STOPPED:
1237 case SCIC_FAILED:
Dan Williamscc9203b2011-05-08 17:34:44 -07001238 /*
1239 * The reset operation is not a graceful cleanup, just
1240 * perform the state transition.
1241 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001242 sci_change_state(&ihost->sm, SCIC_RESETTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001243 return SCI_SUCCESS;
1244 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001245 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001246 "SCIC Controller reset operation requested in "
1247 "invalid state\n");
1248 return SCI_FAILURE_INVALID_STATE;
1249 }
1250}
1251
Dan Williams0cf89d12011-02-18 09:25:07 -08001252void isci_host_deinit(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07001253{
1254 int i;
1255
Dan Williams0cf89d12011-02-18 09:25:07 -08001256 isci_host_change_state(ihost, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001257 for (i = 0; i < SCI_MAX_PORTS; i++) {
Dan Williamse5313812011-05-07 10:11:43 -07001258 struct isci_port *iport = &ihost->ports[i];
Dan Williams0cf89d12011-02-18 09:25:07 -08001259 struct isci_remote_device *idev, *d;
1260
Dan Williamse5313812011-05-07 10:11:43 -07001261 list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
Dan Williams209fae12011-06-13 17:39:44 -07001262 if (test_bit(IDEV_ALLOCATED, &idev->flags))
1263 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001264 }
1265 }
1266
Dan Williams0cf89d12011-02-18 09:25:07 -08001267 set_bit(IHOST_STOP_PENDING, &ihost->flags);
Dan Williams7c40a802011-03-02 11:49:26 -08001268
1269 spin_lock_irq(&ihost->scic_lock);
Dan Williams89a73012011-06-30 19:14:33 -07001270 sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
Dan Williams7c40a802011-03-02 11:49:26 -08001271 spin_unlock_irq(&ihost->scic_lock);
1272
Dan Williams0cf89d12011-02-18 09:25:07 -08001273 wait_for_stop(ihost);
Dan Williams89a73012011-06-30 19:14:33 -07001274 sci_controller_reset(ihost);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001275
1276 /* Cancel any/all outstanding port timers */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001277 for (i = 0; i < ihost->logical_port_entries; i++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001278 struct isci_port *iport = &ihost->ports[i];
1279 del_timer_sync(&iport->timer.timer);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001280 }
1281
Edmund Nadolskia628d472011-05-19 11:59:36 +00001282 /* Cancel any/all outstanding phy timers */
1283 for (i = 0; i < SCI_MAX_PHYS; i++) {
Dan Williams85280952011-06-28 15:05:53 -07001284 struct isci_phy *iphy = &ihost->phys[i];
1285 del_timer_sync(&iphy->sata_timer.timer);
Edmund Nadolskia628d472011-05-19 11:59:36 +00001286 }
1287
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001288 del_timer_sync(&ihost->port_agent.timer.timer);
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -07001289
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001290 del_timer_sync(&ihost->power_control.timer.timer);
Edmund Nadolski04736612011-05-19 20:17:47 -07001291
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001292 del_timer_sync(&ihost->timer.timer);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001293
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001294 del_timer_sync(&ihost->phy_timer.timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001295}
1296
Dan Williams6f231dd2011-07-02 22:56:22 -07001297static void __iomem *scu_base(struct isci_host *isci_host)
1298{
1299 struct pci_dev *pdev = isci_host->pdev;
1300 int id = isci_host->id;
1301
1302 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
1303}
1304
1305static void __iomem *smu_base(struct isci_host *isci_host)
1306{
1307 struct pci_dev *pdev = isci_host->pdev;
1308 int id = isci_host->id;
1309
1310 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
1311}
1312
Dan Williams89a73012011-06-30 19:14:33 -07001313static void isci_user_parameters_get(struct sci_user_parameters *u)
Dave Jiangb5f18a22011-03-16 14:57:23 -07001314{
Dave Jiangb5f18a22011-03-16 14:57:23 -07001315 int i;
1316
1317 for (i = 0; i < SCI_MAX_PHYS; i++) {
1318 struct sci_phy_user_params *u_phy = &u->phys[i];
1319
1320 u_phy->max_speed_generation = phy_gen;
1321
1322 /* we are not exporting these for now */
1323 u_phy->align_insertion_frequency = 0x7f;
1324 u_phy->in_connection_align_insertion_frequency = 0xff;
1325 u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1326 }
1327
1328 u->stp_inactivity_timeout = stp_inactive_to;
1329 u->ssp_inactivity_timeout = ssp_inactive_to;
1330 u->stp_max_occupancy_timeout = stp_max_occ_to;
1331 u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1332 u->no_outbound_task_timeout = no_outbound_task_to;
1333 u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1334}
1335
Dan Williams89a73012011-06-30 19:14:33 -07001336static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001337{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001338 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001339
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001340 sci_change_state(&ihost->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001341}
1342
Dan Williams89a73012011-06-30 19:14:33 -07001343static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001344{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001345 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001346
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001347 sci_del_timer(&ihost->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001348}
1349
1350#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1351#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1352#define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
1353#define INTERRUPT_COALESCE_NUMBER_MAX 256
1354#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
1355#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
1356
1357/**
Dan Williams89a73012011-06-30 19:14:33 -07001358 * sci_controller_set_interrupt_coalescence() - This method allows the user to
Dan Williamscc9203b2011-05-08 17:34:44 -07001359 * configure the interrupt coalescence.
1360 * @controller: This parameter represents the handle to the controller object
1361 * for which its interrupt coalesce register is overridden.
1362 * @coalesce_number: Used to control the number of entries in the Completion
1363 * Queue before an interrupt is generated. If the number of entries exceed
1364 * this number, an interrupt will be generated. The valid range of the input
1365 * is [0, 256]. A setting of 0 results in coalescing being disabled.
1366 * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1367 * input is [0, 2700000] . A setting of 0 is allowed and results in no
1368 * interrupt coalescing timeout.
1369 *
1370 * Indicate if the user successfully set the interrupt coalesce parameters.
1371 * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1372 * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1373 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001374static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001375sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
1376 u32 coalesce_number,
1377 u32 coalesce_timeout)
Dan Williamscc9203b2011-05-08 17:34:44 -07001378{
1379 u8 timeout_encode = 0;
1380 u32 min = 0;
1381 u32 max = 0;
1382
1383 /* Check if the input parameters fall in the range. */
1384 if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1385 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1386
1387 /*
1388 * Defined encoding for interrupt coalescing timeout:
1389 * Value Min Max Units
1390 * ----- --- --- -----
1391 * 0 - - Disabled
1392 * 1 13.3 20.0 ns
1393 * 2 26.7 40.0
1394 * 3 53.3 80.0
1395 * 4 106.7 160.0
1396 * 5 213.3 320.0
1397 * 6 426.7 640.0
1398 * 7 853.3 1280.0
1399 * 8 1.7 2.6 us
1400 * 9 3.4 5.1
1401 * 10 6.8 10.2
1402 * 11 13.7 20.5
1403 * 12 27.3 41.0
1404 * 13 54.6 81.9
1405 * 14 109.2 163.8
1406 * 15 218.5 327.7
1407 * 16 436.9 655.4
1408 * 17 873.8 1310.7
1409 * 18 1.7 2.6 ms
1410 * 19 3.5 5.2
1411 * 20 7.0 10.5
1412 * 21 14.0 21.0
1413 * 22 28.0 41.9
1414 * 23 55.9 83.9
1415 * 24 111.8 167.8
1416 * 25 223.7 335.5
1417 * 26 447.4 671.1
1418 * 27 894.8 1342.2
1419 * 28 1.8 2.7 s
1420 * Others Undefined */
1421
1422 /*
1423 * Use the table above to decide the encode of interrupt coalescing timeout
1424 * value for register writing. */
1425 if (coalesce_timeout == 0)
1426 timeout_encode = 0;
1427 else{
1428 /* make the timeout value in unit of (10 ns). */
1429 coalesce_timeout = coalesce_timeout * 100;
1430 min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1431 max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1432
1433 /* get the encode of timeout for register writing. */
1434 for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1435 timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1436 timeout_encode++) {
1437 if (min <= coalesce_timeout && max > coalesce_timeout)
1438 break;
1439 else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1440 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1441 if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1442 break;
1443 else{
1444 timeout_encode++;
1445 break;
1446 }
1447 } else {
1448 max = max * 2;
1449 min = min * 2;
1450 }
1451 }
1452
1453 if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1454 /* the value is out of range. */
1455 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1456 }
1457
1458 writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1459 SMU_ICC_GEN_VAL(TIMER, timeout_encode),
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001460 &ihost->smu_registers->interrupt_coalesce_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001461
1462
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001463 ihost->interrupt_coalesce_number = (u16)coalesce_number;
1464 ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
Dan Williamscc9203b2011-05-08 17:34:44 -07001465
1466 return SCI_SUCCESS;
1467}
1468
1469
Dan Williams89a73012011-06-30 19:14:33 -07001470static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001471{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001472 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001473
1474 /* set the default interrupt coalescence number and timeout value. */
Dan Williams89a73012011-06-30 19:14:33 -07001475 sci_controller_set_interrupt_coalescence(ihost, 0x10, 250);
Dan Williamscc9203b2011-05-08 17:34:44 -07001476}
1477
Dan Williams89a73012011-06-30 19:14:33 -07001478static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001479{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001480 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001481
1482 /* disable interrupt coalescence. */
Dan Williams89a73012011-06-30 19:14:33 -07001483 sci_controller_set_interrupt_coalescence(ihost, 0, 0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001484}
1485
Dan Williams89a73012011-06-30 19:14:33 -07001486static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001487{
1488 u32 index;
1489 enum sci_status status;
1490 enum sci_status phy_status;
Dan Williamscc9203b2011-05-08 17:34:44 -07001491
1492 status = SCI_SUCCESS;
1493
1494 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -07001495 phy_status = sci_phy_stop(&ihost->phys[index]);
Dan Williamscc9203b2011-05-08 17:34:44 -07001496
1497 if (phy_status != SCI_SUCCESS &&
1498 phy_status != SCI_FAILURE_INVALID_STATE) {
1499 status = SCI_FAILURE;
1500
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001501 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001502 "%s: Controller stop operation failed to stop "
1503 "phy %d because of status %d.\n",
1504 __func__,
Dan Williams85280952011-06-28 15:05:53 -07001505 ihost->phys[index].phy_index, phy_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001506 }
1507 }
1508
1509 return status;
1510}
1511
Dan Williams89a73012011-06-30 19:14:33 -07001512static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001513{
1514 u32 index;
1515 enum sci_status port_status;
1516 enum sci_status status = SCI_SUCCESS;
Dan Williamscc9203b2011-05-08 17:34:44 -07001517
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001518 for (index = 0; index < ihost->logical_port_entries; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001519 struct isci_port *iport = &ihost->ports[index];
Dan Williamscc9203b2011-05-08 17:34:44 -07001520
Dan Williams89a73012011-06-30 19:14:33 -07001521 port_status = sci_port_stop(iport);
Dan Williamscc9203b2011-05-08 17:34:44 -07001522
1523 if ((port_status != SCI_SUCCESS) &&
1524 (port_status != SCI_FAILURE_INVALID_STATE)) {
1525 status = SCI_FAILURE;
1526
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001527 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001528 "%s: Controller stop operation failed to "
1529 "stop port %d because of status %d.\n",
1530 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -07001531 iport->logical_port_index,
Dan Williamscc9203b2011-05-08 17:34:44 -07001532 port_status);
1533 }
1534 }
1535
1536 return status;
1537}
1538
Dan Williams89a73012011-06-30 19:14:33 -07001539static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001540{
1541 u32 index;
1542 enum sci_status status;
1543 enum sci_status device_status;
1544
1545 status = SCI_SUCCESS;
1546
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001547 for (index = 0; index < ihost->remote_node_entries; index++) {
1548 if (ihost->device_table[index] != NULL) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001549 /* / @todo What timeout value do we want to provide to this request? */
Dan Williams89a73012011-06-30 19:14:33 -07001550 device_status = sci_remote_device_stop(ihost->device_table[index], 0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001551
1552 if ((device_status != SCI_SUCCESS) &&
1553 (device_status != SCI_FAILURE_INVALID_STATE)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001554 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07001555 "%s: Controller stop operation failed "
1556 "to stop device 0x%p because of "
1557 "status %d.\n",
1558 __func__,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001559 ihost->device_table[index], device_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001560 }
1561 }
1562 }
1563
1564 return status;
1565}
1566
Dan Williams89a73012011-06-30 19:14:33 -07001567static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001568{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001569 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001570
1571 /* Stop all of the components for this controller */
Dan Williams89a73012011-06-30 19:14:33 -07001572 sci_controller_stop_phys(ihost);
1573 sci_controller_stop_ports(ihost);
1574 sci_controller_stop_devices(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001575}
1576
Dan Williams89a73012011-06-30 19:14:33 -07001577static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001578{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001579 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001580
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001581 sci_del_timer(&ihost->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001582}
1583
Dan Williams89a73012011-06-30 19:14:33 -07001584static void sci_controller_reset_hardware(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001585{
1586 /* Disable interrupts so we dont take any spurious interrupts */
Dan Williams89a73012011-06-30 19:14:33 -07001587 sci_controller_disable_interrupts(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001588
1589 /* Reset the SCU */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001590 writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001591
1592 /* Delay for 1ms to before clearing the CQP and UFQPR. */
1593 udelay(1000);
1594
1595 /* The write to the CQGR clears the CQP */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001596 writel(0x00000000, &ihost->smu_registers->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -07001597
1598 /* The write to the UFQGP clears the UFQPR */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001599 writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001600}
1601
Dan Williams89a73012011-06-30 19:14:33 -07001602static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001603{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001604 struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001605
Dan Williams89a73012011-06-30 19:14:33 -07001606 sci_controller_reset_hardware(ihost);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001607 sci_change_state(&ihost->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001608}
1609
Dan Williams89a73012011-06-30 19:14:33 -07001610static const struct sci_base_state sci_controller_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001611 [SCIC_INITIAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001612 .enter_state = sci_controller_initial_state_enter,
Dan Williamscc9203b2011-05-08 17:34:44 -07001613 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001614 [SCIC_RESET] = {},
1615 [SCIC_INITIALIZING] = {},
1616 [SCIC_INITIALIZED] = {},
1617 [SCIC_STARTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001618 .exit_state = sci_controller_starting_state_exit,
Dan Williamscc9203b2011-05-08 17:34:44 -07001619 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001620 [SCIC_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001621 .enter_state = sci_controller_ready_state_enter,
1622 .exit_state = sci_controller_ready_state_exit,
Dan Williamscc9203b2011-05-08 17:34:44 -07001623 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001624 [SCIC_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001625 .enter_state = sci_controller_resetting_state_enter,
Dan Williamscc9203b2011-05-08 17:34:44 -07001626 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001627 [SCIC_STOPPING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001628 .enter_state = sci_controller_stopping_state_enter,
1629 .exit_state = sci_controller_stopping_state_exit,
Dan Williamscc9203b2011-05-08 17:34:44 -07001630 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001631 [SCIC_STOPPED] = {},
1632 [SCIC_FAILED] = {}
Dan Williamscc9203b2011-05-08 17:34:44 -07001633};
1634
Dan Williams89a73012011-06-30 19:14:33 -07001635static void sci_controller_set_default_config_parameters(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001636{
1637 /* these defaults are overridden by the platform / firmware */
Dan Williamscc9203b2011-05-08 17:34:44 -07001638 u16 index;
1639
1640 /* Default to APC mode. */
Dan Williams89a73012011-06-30 19:14:33 -07001641 ihost->oem_parameters.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
Dan Williamscc9203b2011-05-08 17:34:44 -07001642
1643 /* Default to APC mode. */
Dan Williams89a73012011-06-30 19:14:33 -07001644 ihost->oem_parameters.controller.max_concurrent_dev_spin_up = 1;
Dan Williamscc9203b2011-05-08 17:34:44 -07001645
1646 /* Default to no SSC operation. */
Dan Williams89a73012011-06-30 19:14:33 -07001647 ihost->oem_parameters.controller.do_enable_ssc = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07001648
1649 /* Initialize all of the port parameter information to narrow ports. */
1650 for (index = 0; index < SCI_MAX_PORTS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -07001651 ihost->oem_parameters.ports[index].phy_mask = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001652 }
1653
1654 /* Initialize all of the phy parameter information. */
1655 for (index = 0; index < SCI_MAX_PHYS; index++) {
1656 /* Default to 6G (i.e. Gen 3) for now. */
Dan Williams89a73012011-06-30 19:14:33 -07001657 ihost->user_parameters.phys[index].max_speed_generation = 3;
Dan Williamscc9203b2011-05-08 17:34:44 -07001658
1659 /* the frequencies cannot be 0 */
Dan Williams89a73012011-06-30 19:14:33 -07001660 ihost->user_parameters.phys[index].align_insertion_frequency = 0x7f;
1661 ihost->user_parameters.phys[index].in_connection_align_insertion_frequency = 0xff;
1662 ihost->user_parameters.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
Dan Williamscc9203b2011-05-08 17:34:44 -07001663
1664 /*
1665 * Previous Vitesse based expanders had a arbitration issue that
1666 * is worked around by having the upper 32-bits of SAS address
1667 * with a value greater then the Vitesse company identifier.
1668 * Hence, usage of 0x5FCFFFFF. */
Dan Williams89a73012011-06-30 19:14:33 -07001669 ihost->oem_parameters.phys[index].sas_address.low = 0x1 + ihost->id;
1670 ihost->oem_parameters.phys[index].sas_address.high = 0x5FCFFFFF;
Dan Williamscc9203b2011-05-08 17:34:44 -07001671 }
1672
Dan Williams89a73012011-06-30 19:14:33 -07001673 ihost->user_parameters.stp_inactivity_timeout = 5;
1674 ihost->user_parameters.ssp_inactivity_timeout = 5;
1675 ihost->user_parameters.stp_max_occupancy_timeout = 5;
1676 ihost->user_parameters.ssp_max_occupancy_timeout = 20;
1677 ihost->user_parameters.no_outbound_task_timeout = 20;
Dan Williamscc9203b2011-05-08 17:34:44 -07001678}
1679
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001680static void controller_timeout(unsigned long data)
1681{
1682 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001683 struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
1684 struct sci_base_state_machine *sm = &ihost->sm;
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001685 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001686
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001687 spin_lock_irqsave(&ihost->scic_lock, flags);
1688
1689 if (tmr->cancel)
1690 goto done;
1691
Edmund Nadolskie3013702011-06-02 00:10:43 +00001692 if (sm->current_state_id == SCIC_STARTING)
Dan Williams89a73012011-06-30 19:14:33 -07001693 sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001694 else if (sm->current_state_id == SCIC_STOPPING) {
1695 sci_change_state(sm, SCIC_FAILED);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001696 isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
1697 } else /* / @todo Now what do we want to do in this case? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001698 dev_err(&ihost->pdev->dev,
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001699 "%s: Controller timer fired when controller was not "
1700 "in a state being timed.\n",
1701 __func__);
1702
1703done:
1704 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1705}
Dan Williamscc9203b2011-05-08 17:34:44 -07001706
Dan Williams89a73012011-06-30 19:14:33 -07001707static enum sci_status sci_controller_construct(struct isci_host *ihost,
1708 void __iomem *scu_base,
1709 void __iomem *smu_base)
Dan Williamscc9203b2011-05-08 17:34:44 -07001710{
Dan Williamscc9203b2011-05-08 17:34:44 -07001711 u8 i;
1712
Dan Williams89a73012011-06-30 19:14:33 -07001713 sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001714
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001715 ihost->scu_registers = scu_base;
1716 ihost->smu_registers = smu_base;
Dan Williamscc9203b2011-05-08 17:34:44 -07001717
Dan Williams89a73012011-06-30 19:14:33 -07001718 sci_port_configuration_agent_construct(&ihost->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07001719
1720 /* Construct the ports for this controller */
1721 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williams89a73012011-06-30 19:14:33 -07001722 sci_port_construct(&ihost->ports[i], i, ihost);
1723 sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001724
1725 /* Construct the phys for this controller */
1726 for (i = 0; i < SCI_MAX_PHYS; i++) {
1727 /* Add all the PHYs to the dummy port */
Dan Williams89a73012011-06-30 19:14:33 -07001728 sci_phy_construct(&ihost->phys[i],
1729 &ihost->ports[SCI_MAX_PORTS], i);
Dan Williamscc9203b2011-05-08 17:34:44 -07001730 }
1731
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001732 ihost->invalid_phy_mask = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001733
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001734 sci_init_timer(&ihost->timer, controller_timeout);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001735
Dan Williamscc9203b2011-05-08 17:34:44 -07001736 /* Initialize the User and OEM parameters to default values. */
Dan Williams89a73012011-06-30 19:14:33 -07001737 sci_controller_set_default_config_parameters(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001738
Dan Williams89a73012011-06-30 19:14:33 -07001739 return sci_controller_reset(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07001740}
1741
Dan Williams89a73012011-06-30 19:14:33 -07001742int sci_oem_parameters_validate(struct sci_oem_params *oem)
Dan Williamscc9203b2011-05-08 17:34:44 -07001743{
1744 int i;
1745
1746 for (i = 0; i < SCI_MAX_PORTS; i++)
1747 if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1748 return -EINVAL;
1749
1750 for (i = 0; i < SCI_MAX_PHYS; i++)
1751 if (oem->phys[i].sas_address.high == 0 &&
1752 oem->phys[i].sas_address.low == 0)
1753 return -EINVAL;
1754
1755 if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1756 for (i = 0; i < SCI_MAX_PHYS; i++)
1757 if (oem->ports[i].phy_mask != 0)
1758 return -EINVAL;
1759 } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1760 u8 phy_mask = 0;
1761
1762 for (i = 0; i < SCI_MAX_PHYS; i++)
1763 phy_mask |= oem->ports[i].phy_mask;
1764
1765 if (phy_mask == 0)
1766 return -EINVAL;
1767 } else
1768 return -EINVAL;
1769
1770 if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1771 return -EINVAL;
1772
1773 return 0;
1774}
1775
Dan Williams89a73012011-06-30 19:14:33 -07001776static enum sci_status sci_oem_parameters_set(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001777{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001778 u32 state = ihost->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07001779
Edmund Nadolskie3013702011-06-02 00:10:43 +00001780 if (state == SCIC_RESET ||
1781 state == SCIC_INITIALIZING ||
1782 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001783
Dan Williams89a73012011-06-30 19:14:33 -07001784 if (sci_oem_parameters_validate(&ihost->oem_parameters))
Dan Williamscc9203b2011-05-08 17:34:44 -07001785 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
Dan Williamscc9203b2011-05-08 17:34:44 -07001786
1787 return SCI_SUCCESS;
1788 }
1789
1790 return SCI_FAILURE_INVALID_STATE;
1791}
1792
Edmund Nadolski04736612011-05-19 20:17:47 -07001793static void power_control_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001794{
Edmund Nadolski04736612011-05-19 20:17:47 -07001795 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001796 struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
Dan Williams85280952011-06-28 15:05:53 -07001797 struct isci_phy *iphy;
Edmund Nadolski04736612011-05-19 20:17:47 -07001798 unsigned long flags;
1799 u8 i;
Dan Williamscc9203b2011-05-08 17:34:44 -07001800
Edmund Nadolski04736612011-05-19 20:17:47 -07001801 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001802
Edmund Nadolski04736612011-05-19 20:17:47 -07001803 if (tmr->cancel)
1804 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001805
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001806 ihost->power_control.phys_granted_power = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001807
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001808 if (ihost->power_control.phys_waiting == 0) {
1809 ihost->power_control.timer_started = false;
Edmund Nadolski04736612011-05-19 20:17:47 -07001810 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001811 }
Edmund Nadolski04736612011-05-19 20:17:47 -07001812
1813 for (i = 0; i < SCI_MAX_PHYS; i++) {
1814
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001815 if (ihost->power_control.phys_waiting == 0)
Edmund Nadolski04736612011-05-19 20:17:47 -07001816 break;
1817
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001818 iphy = ihost->power_control.requesters[i];
Dan Williams85280952011-06-28 15:05:53 -07001819 if (iphy == NULL)
Edmund Nadolski04736612011-05-19 20:17:47 -07001820 continue;
1821
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001822 if (ihost->power_control.phys_granted_power >=
Dan Williams89a73012011-06-30 19:14:33 -07001823 ihost->oem_parameters.controller.max_concurrent_dev_spin_up)
Edmund Nadolski04736612011-05-19 20:17:47 -07001824 break;
1825
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001826 ihost->power_control.requesters[i] = NULL;
1827 ihost->power_control.phys_waiting--;
1828 ihost->power_control.phys_granted_power++;
Dan Williams89a73012011-06-30 19:14:33 -07001829 sci_phy_consume_power_handler(iphy);
Edmund Nadolski04736612011-05-19 20:17:47 -07001830 }
1831
1832 /*
1833 * It doesn't matter if the power list is empty, we need to start the
1834 * timer in case another phy becomes ready.
1835 */
1836 sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001837 ihost->power_control.timer_started = true;
Edmund Nadolski04736612011-05-19 20:17:47 -07001838
1839done:
1840 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001841}
1842
Dan Williams89a73012011-06-30 19:14:33 -07001843void sci_controller_power_control_queue_insert(struct isci_host *ihost,
1844 struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07001845{
Dan Williams85280952011-06-28 15:05:53 -07001846 BUG_ON(iphy == NULL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001847
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001848 if (ihost->power_control.phys_granted_power <
Dan Williams89a73012011-06-30 19:14:33 -07001849 ihost->oem_parameters.controller.max_concurrent_dev_spin_up) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001850 ihost->power_control.phys_granted_power++;
Dan Williams89a73012011-06-30 19:14:33 -07001851 sci_phy_consume_power_handler(iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -07001852
1853 /*
1854 * stop and start the power_control timer. When the timer fires, the
1855 * no_of_phys_granted_power will be set to 0
1856 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001857 if (ihost->power_control.timer_started)
1858 sci_del_timer(&ihost->power_control.timer);
Edmund Nadolski04736612011-05-19 20:17:47 -07001859
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001860 sci_mod_timer(&ihost->power_control.timer,
Edmund Nadolski04736612011-05-19 20:17:47 -07001861 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001862 ihost->power_control.timer_started = true;
Edmund Nadolski04736612011-05-19 20:17:47 -07001863
Dan Williamscc9203b2011-05-08 17:34:44 -07001864 } else {
1865 /* Add the phy in the waiting list */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001866 ihost->power_control.requesters[iphy->phy_index] = iphy;
1867 ihost->power_control.phys_waiting++;
Dan Williamscc9203b2011-05-08 17:34:44 -07001868 }
1869}
1870
Dan Williams89a73012011-06-30 19:14:33 -07001871void sci_controller_power_control_queue_remove(struct isci_host *ihost,
1872 struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07001873{
Dan Williams85280952011-06-28 15:05:53 -07001874 BUG_ON(iphy == NULL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001875
Dan Williams89a73012011-06-30 19:14:33 -07001876 if (ihost->power_control.requesters[iphy->phy_index])
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001877 ihost->power_control.phys_waiting--;
Dan Williamscc9203b2011-05-08 17:34:44 -07001878
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001879 ihost->power_control.requesters[iphy->phy_index] = NULL;
Dan Williamscc9203b2011-05-08 17:34:44 -07001880}
1881
1882#define AFE_REGISTER_WRITE_DELAY 10
1883
1884/* Initialize the AFE for this phy index. We need to read the AFE setup from
1885 * the OEM parameters
1886 */
Dan Williams89a73012011-06-30 19:14:33 -07001887static void sci_controller_afe_initialization(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07001888{
Dan Williams89a73012011-06-30 19:14:33 -07001889 const struct sci_oem_params *oem = &ihost->oem_parameters;
Dan Williamscc9203b2011-05-08 17:34:44 -07001890 u32 afe_status;
1891 u32 phy_id;
1892
1893 /* Clear DFX Status registers */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001894 writel(0x0081000f, &ihost->scu_registers->afe.afe_dfx_master_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001895 udelay(AFE_REGISTER_WRITE_DELAY);
1896
1897 if (is_b0()) {
1898 /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
1899 * Timer, PM Stagger Timer */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001900 writel(0x0007BFFF, &ihost->scu_registers->afe.afe_pmsn_master_control2);
Dan Williamscc9203b2011-05-08 17:34:44 -07001901 udelay(AFE_REGISTER_WRITE_DELAY);
1902 }
1903
1904 /* Configure bias currents to normal */
1905 if (is_a0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001906 writel(0x00005500, &ihost->scu_registers->afe.afe_bias_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001907 else if (is_a2())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001908 writel(0x00005A00, &ihost->scu_registers->afe.afe_bias_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001909 else if (is_b0() || is_c0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001910 writel(0x00005F00, &ihost->scu_registers->afe.afe_bias_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001911
1912 udelay(AFE_REGISTER_WRITE_DELAY);
1913
1914 /* Enable PLL */
Adam Gruchaladbb07432011-06-01 22:31:03 +00001915 if (is_b0() || is_c0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001916 writel(0x80040A08, &ihost->scu_registers->afe.afe_pll_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001917 else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001918 writel(0x80040908, &ihost->scu_registers->afe.afe_pll_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001919
1920 udelay(AFE_REGISTER_WRITE_DELAY);
1921
1922 /* Wait for the PLL to lock */
1923 do {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001924 afe_status = readl(&ihost->scu_registers->afe.afe_common_block_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07001925 udelay(AFE_REGISTER_WRITE_DELAY);
1926 } while ((afe_status & 0x00001000) == 0);
1927
1928 if (is_a0() || is_a2()) {
1929 /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001930 writel(0x7bcc96ad, &ihost->scu_registers->afe.afe_pmsn_master_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001931 udelay(AFE_REGISTER_WRITE_DELAY);
1932 }
1933
1934 for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
1935 const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
1936
1937 if (is_b0()) {
1938 /* Configure transmitter SSC parameters */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001939 writel(0x00030000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001940 udelay(AFE_REGISTER_WRITE_DELAY);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001941 } else if (is_c0()) {
1942 /* Configure transmitter SSC parameters */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001943 writel(0x0003000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001944 udelay(AFE_REGISTER_WRITE_DELAY);
1945
1946 /*
1947 * All defaults, except the Receive Word Alignament/Comma Detect
1948 * Enable....(0xe800) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001949 writel(0x00004500, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001950 udelay(AFE_REGISTER_WRITE_DELAY);
Dan Williamscc9203b2011-05-08 17:34:44 -07001951 } else {
1952 /*
1953 * All defaults, except the Receive Word Alignament/Comma Detect
1954 * Enable....(0xe800) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001955 writel(0x00004512, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001956 udelay(AFE_REGISTER_WRITE_DELAY);
1957
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001958 writel(0x0050100F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
Dan Williamscc9203b2011-05-08 17:34:44 -07001959 udelay(AFE_REGISTER_WRITE_DELAY);
1960 }
1961
1962 /*
1963 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
1964 * & increase TX int & ext bias 20%....(0xe85c) */
1965 if (is_a0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001966 writel(0x000003D4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001967 else if (is_a2())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001968 writel(0x000003F0, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001969 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001970 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001971 writel(0x000003D7, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001972 udelay(AFE_REGISTER_WRITE_DELAY);
1973
1974 /*
1975 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
1976 * & increase TX int & ext bias 20%....(0xe85c) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001977 writel(0x000003D4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001978 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001979 writel(0x000001E7, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00001980 udelay(AFE_REGISTER_WRITE_DELAY);
1981
1982 /*
1983 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
1984 * & increase TX int & ext bias 20%....(0xe85c) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001985 writel(0x000001E4, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001986 }
1987 udelay(AFE_REGISTER_WRITE_DELAY);
1988
1989 if (is_a0() || is_a2()) {
1990 /* Enable TX equalization (0xe824) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001991 writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07001992 udelay(AFE_REGISTER_WRITE_DELAY);
1993 }
1994
1995 /*
1996 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
1997 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001998 writel(0x00004100, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07001999 udelay(AFE_REGISTER_WRITE_DELAY);
2000
2001 /* Leave DFE/FFE on */
2002 if (is_a0())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002003 writel(0x3F09983F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002004 else if (is_a2())
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002005 writel(0x3F11103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002006 else if (is_b0()) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002007 writel(0x3F11103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002008 udelay(AFE_REGISTER_WRITE_DELAY);
2009 /* Enable TX equalization (0xe824) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002010 writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002011 } else {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002012 writel(0x0140DF0F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002013 udelay(AFE_REGISTER_WRITE_DELAY);
2014
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002015 writel(0x3F6F103F, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002016 udelay(AFE_REGISTER_WRITE_DELAY);
2017
2018 /* Enable TX equalization (0xe824) */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002019 writel(0x00040000, &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002020 }
Adam Gruchaladbb07432011-06-01 22:31:03 +00002021
Dan Williamscc9203b2011-05-08 17:34:44 -07002022 udelay(AFE_REGISTER_WRITE_DELAY);
2023
2024 writel(oem_phy->afe_tx_amp_control0,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002025 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002026 udelay(AFE_REGISTER_WRITE_DELAY);
2027
2028 writel(oem_phy->afe_tx_amp_control1,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002029 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
Dan Williamscc9203b2011-05-08 17:34:44 -07002030 udelay(AFE_REGISTER_WRITE_DELAY);
2031
2032 writel(oem_phy->afe_tx_amp_control2,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002033 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
Dan Williamscc9203b2011-05-08 17:34:44 -07002034 udelay(AFE_REGISTER_WRITE_DELAY);
2035
2036 writel(oem_phy->afe_tx_amp_control3,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002037 &ihost->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
Dan Williamscc9203b2011-05-08 17:34:44 -07002038 udelay(AFE_REGISTER_WRITE_DELAY);
2039 }
2040
2041 /* Transfer control to the PEs */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002042 writel(0x00010f00, &ihost->scu_registers->afe.afe_dfx_master_control0);
Dan Williamscc9203b2011-05-08 17:34:44 -07002043 udelay(AFE_REGISTER_WRITE_DELAY);
2044}
2045
Dan Williams89a73012011-06-30 19:14:33 -07002046static void sci_controller_initialize_power_control(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002047{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002048 sci_init_timer(&ihost->power_control.timer, power_control_timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07002049
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002050 memset(ihost->power_control.requesters, 0,
2051 sizeof(ihost->power_control.requesters));
Dan Williamscc9203b2011-05-08 17:34:44 -07002052
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002053 ihost->power_control.phys_waiting = 0;
2054 ihost->power_control.phys_granted_power = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07002055}
2056
Dan Williams89a73012011-06-30 19:14:33 -07002057static enum sci_status sci_controller_initialize(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002058{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002059 struct sci_base_state_machine *sm = &ihost->sm;
Dan Williams7c78da32011-06-01 16:00:01 -07002060 enum sci_status result = SCI_FAILURE;
2061 unsigned long i, state, val;
Dan Williamscc9203b2011-05-08 17:34:44 -07002062
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002063 if (ihost->sm.current_state_id != SCIC_RESET) {
2064 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002065 "SCIC Controller initialize operation requested "
2066 "in invalid state\n");
2067 return SCI_FAILURE_INVALID_STATE;
2068 }
2069
Edmund Nadolskie3013702011-06-02 00:10:43 +00002070 sci_change_state(sm, SCIC_INITIALIZING);
Dan Williamscc9203b2011-05-08 17:34:44 -07002071
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002072 sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002073
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002074 ihost->next_phy_to_start = 0;
2075 ihost->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002076
Dan Williams89a73012011-06-30 19:14:33 -07002077 sci_controller_initialize_power_control(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07002078
2079 /*
2080 * There is nothing to do here for B0 since we do not have to
2081 * program the AFE registers.
2082 * / @todo The AFE settings are supposed to be correct for the B0 but
2083 * / presently they seem to be wrong. */
Dan Williams89a73012011-06-30 19:14:33 -07002084 sci_controller_afe_initialization(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07002085
Dan Williams7c78da32011-06-01 16:00:01 -07002086
2087 /* Take the hardware out of reset */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002088 writel(0, &ihost->smu_registers->soft_reset_control);
Dan Williams7c78da32011-06-01 16:00:01 -07002089
2090 /*
2091 * / @todo Provide meaningfull error code for hardware failure
2092 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2093 for (i = 100; i >= 1; i--) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002094 u32 status;
Dan Williamscc9203b2011-05-08 17:34:44 -07002095
Dan Williams7c78da32011-06-01 16:00:01 -07002096 /* Loop until the hardware reports success */
2097 udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002098 status = readl(&ihost->smu_registers->control_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07002099
Dan Williams7c78da32011-06-01 16:00:01 -07002100 if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
2101 break;
Dan Williamscc9203b2011-05-08 17:34:44 -07002102 }
Dan Williams7c78da32011-06-01 16:00:01 -07002103 if (i == 0)
2104 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002105
Dan Williams7c78da32011-06-01 16:00:01 -07002106 /*
2107 * Determine what are the actaul device capacities that the
2108 * hardware will support */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002109 val = readl(&ihost->smu_registers->device_context_capacity);
Dan Williamscc9203b2011-05-08 17:34:44 -07002110
Dan Williams7c78da32011-06-01 16:00:01 -07002111 /* Record the smaller of the two capacity values */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002112 ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2113 ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2114 ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
Dan Williamscc9203b2011-05-08 17:34:44 -07002115
Dan Williams7c78da32011-06-01 16:00:01 -07002116 /*
2117 * Make all PEs that are unassigned match up with the
2118 * logical ports
2119 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002120 for (i = 0; i < ihost->logical_port_entries; i++) {
Dan Williams7c78da32011-06-01 16:00:01 -07002121 struct scu_port_task_scheduler_group_registers __iomem
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002122 *ptsg = &ihost->scu_registers->peg0.ptsg;
Dan Williamscc9203b2011-05-08 17:34:44 -07002123
Dan Williams7c78da32011-06-01 16:00:01 -07002124 writel(i, &ptsg->protocol_engine[i]);
Dan Williamscc9203b2011-05-08 17:34:44 -07002125 }
2126
2127 /* Initialize hardware PCI Relaxed ordering in DMA engines */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002128 val = readl(&ihost->scu_registers->sdma.pdma_configuration);
Dan Williams7c78da32011-06-01 16:00:01 -07002129 val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002130 writel(val, &ihost->scu_registers->sdma.pdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002131
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002132 val = readl(&ihost->scu_registers->sdma.cdma_configuration);
Dan Williams7c78da32011-06-01 16:00:01 -07002133 val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002134 writel(val, &ihost->scu_registers->sdma.cdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002135
2136 /*
2137 * Initialize the PHYs before the PORTs because the PHY registers
2138 * are accessed during the port initialization.
2139 */
Dan Williams7c78da32011-06-01 16:00:01 -07002140 for (i = 0; i < SCI_MAX_PHYS; i++) {
Dan Williams89a73012011-06-30 19:14:33 -07002141 result = sci_phy_initialize(&ihost->phys[i],
2142 &ihost->scu_registers->peg0.pe[i].tl,
2143 &ihost->scu_registers->peg0.pe[i].ll);
Dan Williams7c78da32011-06-01 16:00:01 -07002144 if (result != SCI_SUCCESS)
2145 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002146 }
2147
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002148 for (i = 0; i < ihost->logical_port_entries; i++) {
Dan Williams89a73012011-06-30 19:14:33 -07002149 struct isci_port *iport = &ihost->ports[i];
Dan Williams7c78da32011-06-01 16:00:01 -07002150
Dan Williams89a73012011-06-30 19:14:33 -07002151 iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
2152 iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
2153 iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
Dan Williamscc9203b2011-05-08 17:34:44 -07002154 }
2155
Dan Williams89a73012011-06-30 19:14:33 -07002156 result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07002157
Dan Williams7c78da32011-06-01 16:00:01 -07002158 out:
Dan Williamscc9203b2011-05-08 17:34:44 -07002159 /* Advance the controller state machine */
2160 if (result == SCI_SUCCESS)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002161 state = SCIC_INITIALIZED;
Dan Williamscc9203b2011-05-08 17:34:44 -07002162 else
Edmund Nadolskie3013702011-06-02 00:10:43 +00002163 state = SCIC_FAILED;
2164 sci_change_state(sm, state);
Dan Williamscc9203b2011-05-08 17:34:44 -07002165
2166 return result;
2167}
2168
Dan Williams89a73012011-06-30 19:14:33 -07002169static enum sci_status sci_user_parameters_set(struct isci_host *ihost,
2170 struct sci_user_parameters *sci_parms)
Dan Williamscc9203b2011-05-08 17:34:44 -07002171{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002172 u32 state = ihost->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07002173
Edmund Nadolskie3013702011-06-02 00:10:43 +00002174 if (state == SCIC_RESET ||
2175 state == SCIC_INITIALIZING ||
2176 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002177 u16 index;
2178
2179 /*
2180 * Validate the user parameters. If they are not legal, then
2181 * return a failure.
2182 */
2183 for (index = 0; index < SCI_MAX_PHYS; index++) {
2184 struct sci_phy_user_params *user_phy;
2185
Dan Williams89a73012011-06-30 19:14:33 -07002186 user_phy = &sci_parms->phys[index];
Dan Williamscc9203b2011-05-08 17:34:44 -07002187
2188 if (!((user_phy->max_speed_generation <=
2189 SCIC_SDS_PARM_MAX_SPEED) &&
2190 (user_phy->max_speed_generation >
2191 SCIC_SDS_PARM_NO_SPEED)))
2192 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2193
2194 if (user_phy->in_connection_align_insertion_frequency <
2195 3)
2196 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2197
2198 if ((user_phy->in_connection_align_insertion_frequency <
2199 3) ||
2200 (user_phy->align_insertion_frequency == 0) ||
2201 (user_phy->
2202 notify_enable_spin_up_insertion_frequency ==
2203 0))
2204 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2205 }
2206
Dan Williams89a73012011-06-30 19:14:33 -07002207 if ((sci_parms->stp_inactivity_timeout == 0) ||
2208 (sci_parms->ssp_inactivity_timeout == 0) ||
2209 (sci_parms->stp_max_occupancy_timeout == 0) ||
2210 (sci_parms->ssp_max_occupancy_timeout == 0) ||
2211 (sci_parms->no_outbound_task_timeout == 0))
Dan Williamscc9203b2011-05-08 17:34:44 -07002212 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2213
Dan Williams89a73012011-06-30 19:14:33 -07002214 memcpy(&ihost->user_parameters, sci_parms, sizeof(*sci_parms));
Dan Williamscc9203b2011-05-08 17:34:44 -07002215
2216 return SCI_SUCCESS;
2217 }
2218
2219 return SCI_FAILURE_INVALID_STATE;
2220}
2221
Dan Williams89a73012011-06-30 19:14:33 -07002222static int sci_controller_mem_init(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002223{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002224 struct device *dev = &ihost->pdev->dev;
Dan Williams7c78da32011-06-01 16:00:01 -07002225 dma_addr_t dma;
2226 size_t size;
2227 int err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002228
Dan Williams7c78da32011-06-01 16:00:01 -07002229 size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002230 ihost->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2231 if (!ihost->completion_queue)
Dan Williamscc9203b2011-05-08 17:34:44 -07002232 return -ENOMEM;
2233
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002234 writel(lower_32_bits(dma), &ihost->smu_registers->completion_queue_lower);
2235 writel(upper_32_bits(dma), &ihost->smu_registers->completion_queue_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002236
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002237 size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
2238 ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
Dan Williams89a73012011-06-30 19:14:33 -07002239 GFP_KERNEL);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002240 if (!ihost->remote_node_context_table)
Dan Williamscc9203b2011-05-08 17:34:44 -07002241 return -ENOMEM;
2242
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002243 writel(lower_32_bits(dma), &ihost->smu_registers->remote_node_context_lower);
2244 writel(upper_32_bits(dma), &ihost->smu_registers->remote_node_context_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002245
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002246 size = ihost->task_context_entries * sizeof(struct scu_task_context),
2247 ihost->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
2248 if (!ihost->task_context_table)
Dan Williamscc9203b2011-05-08 17:34:44 -07002249 return -ENOMEM;
2250
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002251 ihost->task_context_dma = dma;
2252 writel(lower_32_bits(dma), &ihost->smu_registers->host_task_table_lower);
2253 writel(upper_32_bits(dma), &ihost->smu_registers->host_task_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002254
Dan Williams89a73012011-06-30 19:14:33 -07002255 err = sci_unsolicited_frame_control_construct(ihost);
Dan Williams7c78da32011-06-01 16:00:01 -07002256 if (err)
2257 return err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002258
2259 /*
2260 * Inform the silicon as to the location of the UF headers and
2261 * address table.
2262 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002263 writel(lower_32_bits(ihost->uf_control.headers.physical_address),
2264 &ihost->scu_registers->sdma.uf_header_base_address_lower);
2265 writel(upper_32_bits(ihost->uf_control.headers.physical_address),
2266 &ihost->scu_registers->sdma.uf_header_base_address_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002267
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002268 writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
2269 &ihost->scu_registers->sdma.uf_address_table_lower);
2270 writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
2271 &ihost->scu_registers->sdma.uf_address_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002272
2273 return 0;
2274}
2275
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002276int isci_host_init(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07002277{
Dan Williamsd9c37392011-03-03 17:59:32 -08002278 int err = 0, i;
Dan Williams6f231dd2011-07-02 22:56:22 -07002279 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07002280 struct sci_user_parameters sci_user_params;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002281 struct isci_pci_info *pci_info = to_pci_info(ihost->pdev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002282
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002283 spin_lock_init(&ihost->state_lock);
2284 spin_lock_init(&ihost->scic_lock);
2285 init_waitqueue_head(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07002286
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002287 isci_host_change_state(ihost, isci_starting);
Dan Williams6f231dd2011-07-02 22:56:22 -07002288
Dan Williams89a73012011-06-30 19:14:33 -07002289 status = sci_controller_construct(ihost, scu_base(ihost),
2290 smu_base(ihost));
Dan Williams6f231dd2011-07-02 22:56:22 -07002291
2292 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002293 dev_err(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07002294 "%s: sci_controller_construct failed - status = %x\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07002295 __func__,
2296 status);
Dave Jiang858d4aa2011-02-22 01:27:03 -08002297 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002298 }
2299
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002300 ihost->sas_ha.dev = &ihost->pdev->dev;
2301 ihost->sas_ha.lldd_ha = ihost;
Dan Williams6f231dd2011-07-02 22:56:22 -07002302
Dan Williamsd044af12011-03-08 09:52:49 -08002303 /*
2304 * grab initial values stored in the controller object for OEM and USER
2305 * parameters
2306 */
Dan Williams89a73012011-06-30 19:14:33 -07002307 isci_user_parameters_get(&sci_user_params);
2308 status = sci_user_parameters_set(ihost, &sci_user_params);
Dan Williamsd044af12011-03-08 09:52:49 -08002309 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002310 dev_warn(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07002311 "%s: sci_user_parameters_set failed\n",
Dan Williamsd044af12011-03-08 09:52:49 -08002312 __func__);
2313 return -ENODEV;
2314 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002315
Dan Williamsd044af12011-03-08 09:52:49 -08002316 /* grab any OEM parameters specified in orom */
2317 if (pci_info->orom) {
Dan Williams89a73012011-06-30 19:14:33 -07002318 status = isci_parse_oem_parameters(&ihost->oem_parameters,
Dan Williamsd044af12011-03-08 09:52:49 -08002319 pci_info->orom,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002320 ihost->id);
Dan Williams6f231dd2011-07-02 22:56:22 -07002321 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002322 dev_warn(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002323 "parsing firmware oem parameters failed\n");
Dave Jiang858d4aa2011-02-22 01:27:03 -08002324 return -EINVAL;
Dan Williams6f231dd2011-07-02 22:56:22 -07002325 }
Dan Williams4711ba12011-03-11 10:43:57 -08002326 }
2327
Dan Williams89a73012011-06-30 19:14:33 -07002328 status = sci_oem_parameters_set(ihost);
Dan Williams4711ba12011-03-11 10:43:57 -08002329 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002330 dev_warn(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07002331 "%s: sci_oem_parameters_set failed\n",
Dan Williams4711ba12011-03-11 10:43:57 -08002332 __func__);
2333 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002334 }
2335
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002336 tasklet_init(&ihost->completion_tasklet,
2337 isci_host_completion_routine, (unsigned long)ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002338
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002339 INIT_LIST_HEAD(&ihost->requests_to_complete);
2340 INIT_LIST_HEAD(&ihost->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002341
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002342 spin_lock_irq(&ihost->scic_lock);
Dan Williams89a73012011-06-30 19:14:33 -07002343 status = sci_controller_initialize(ihost);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002344 spin_unlock_irq(&ihost->scic_lock);
Dan Williams7c40a802011-03-02 11:49:26 -08002345 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002346 dev_warn(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07002347 "%s: sci_controller_initialize failed -"
Dan Williams7c40a802011-03-02 11:49:26 -08002348 " status = 0x%x\n",
2349 __func__, status);
2350 return -ENODEV;
2351 }
2352
Dan Williams89a73012011-06-30 19:14:33 -07002353 err = sci_controller_mem_init(ihost);
Dan Williams6f231dd2011-07-02 22:56:22 -07002354 if (err)
Dave Jiang858d4aa2011-02-22 01:27:03 -08002355 return err;
Dan Williams6f231dd2011-07-02 22:56:22 -07002356
Dan Williamsd9c37392011-03-03 17:59:32 -08002357 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002358 isci_port_init(&ihost->ports[i], ihost, i);
Dan Williams6f231dd2011-07-02 22:56:22 -07002359
Dan Williamsd9c37392011-03-03 17:59:32 -08002360 for (i = 0; i < SCI_MAX_PHYS; i++)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002361 isci_phy_init(&ihost->phys[i], ihost, i);
Dan Williamsd9c37392011-03-03 17:59:32 -08002362
2363 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002364 struct isci_remote_device *idev = &ihost->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08002365
2366 INIT_LIST_HEAD(&idev->reqs_in_process);
2367 INIT_LIST_HEAD(&idev->node);
Dan Williamsd9c37392011-03-03 17:59:32 -08002368 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002369
Dan Williamsdb056252011-06-17 14:18:39 -07002370 for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
2371 struct isci_request *ireq;
2372 dma_addr_t dma;
2373
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002374 ireq = dmam_alloc_coherent(&ihost->pdev->dev,
Dan Williamsdb056252011-06-17 14:18:39 -07002375 sizeof(struct isci_request), &dma,
2376 GFP_KERNEL);
2377 if (!ireq)
2378 return -ENOMEM;
2379
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002380 ireq->tc = &ihost->task_context_table[i];
2381 ireq->owning_controller = ihost;
Dan Williamsdb056252011-06-17 14:18:39 -07002382 spin_lock_init(&ireq->state_lock);
2383 ireq->request_daddr = dma;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002384 ireq->isci_host = ihost;
2385 ihost->reqs[i] = ireq;
Dan Williamsdb056252011-06-17 14:18:39 -07002386 }
2387
Dave Jiang858d4aa2011-02-22 01:27:03 -08002388 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -07002389}
Dan Williamscc9203b2011-05-08 17:34:44 -07002390
Dan Williams89a73012011-06-30 19:14:33 -07002391void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
2392 struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07002393{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002394 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002395 case SCIC_STARTING:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002396 sci_del_timer(&ihost->phy_timer);
2397 ihost->phy_startup_timer_pending = false;
2398 ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
Dan Williams89a73012011-06-30 19:14:33 -07002399 iport, iphy);
2400 sci_controller_start_next_phy(ihost);
Dan Williamscc9203b2011-05-08 17:34:44 -07002401 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002402 case SCIC_READY:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002403 ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
Dan Williams89a73012011-06-30 19:14:33 -07002404 iport, iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -07002405 break;
2406 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002407 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002408 "%s: SCIC Controller linkup event from phy %d in "
Dan Williams85280952011-06-28 15:05:53 -07002409 "unexpected state %d\n", __func__, iphy->phy_index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002410 ihost->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002411 }
2412}
2413
Dan Williams89a73012011-06-30 19:14:33 -07002414void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
2415 struct isci_phy *iphy)
Dan Williamscc9203b2011-05-08 17:34:44 -07002416{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002417 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002418 case SCIC_STARTING:
2419 case SCIC_READY:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002420 ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
Dan Williamsffe191c2011-06-29 13:09:25 -07002421 iport, iphy);
Dan Williamscc9203b2011-05-08 17:34:44 -07002422 break;
2423 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002424 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002425 "%s: SCIC Controller linkdown event from phy %d in "
2426 "unexpected state %d\n",
2427 __func__,
Dan Williams85280952011-06-28 15:05:53 -07002428 iphy->phy_index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002429 ihost->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002430 }
2431}
2432
Dan Williams89a73012011-06-30 19:14:33 -07002433static bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
Dan Williamscc9203b2011-05-08 17:34:44 -07002434{
2435 u32 index;
2436
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002437 for (index = 0; index < ihost->remote_node_entries; index++) {
2438 if ((ihost->device_table[index] != NULL) &&
2439 (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
Dan Williamscc9203b2011-05-08 17:34:44 -07002440 return true;
2441 }
2442
2443 return false;
2444}
2445
Dan Williams89a73012011-06-30 19:14:33 -07002446void sci_controller_remote_device_stopped(struct isci_host *ihost,
2447 struct isci_remote_device *idev)
Dan Williamscc9203b2011-05-08 17:34:44 -07002448{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002449 if (ihost->sm.current_state_id != SCIC_STOPPING) {
2450 dev_dbg(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002451 "SCIC Controller 0x%p remote device stopped event "
2452 "from device 0x%p in unexpected state %d\n",
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002453 ihost, idev,
2454 ihost->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002455 return;
2456 }
2457
Dan Williams89a73012011-06-30 19:14:33 -07002458 if (!sci_controller_has_remote_devices_stopping(ihost))
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002459 sci_change_state(&ihost->sm, SCIC_STOPPED);
Dan Williamscc9203b2011-05-08 17:34:44 -07002460}
2461
Dan Williams89a73012011-06-30 19:14:33 -07002462void sci_controller_post_request(struct isci_host *ihost, u32 request)
Dan Williamscc9203b2011-05-08 17:34:44 -07002463{
Dan Williams89a73012011-06-30 19:14:33 -07002464 dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
2465 __func__, ihost->id, request);
Dan Williamscc9203b2011-05-08 17:34:44 -07002466
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002467 writel(request, &ihost->smu_registers->post_context_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07002468}
2469
Dan Williams89a73012011-06-30 19:14:33 -07002470struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07002471{
2472 u16 task_index;
2473 u16 task_sequence;
2474
Dan Williamsdd047c82011-06-09 11:06:58 -07002475 task_index = ISCI_TAG_TCI(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002476
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002477 if (task_index < ihost->task_context_entries) {
2478 struct isci_request *ireq = ihost->reqs[task_index];
Dan Williamsdb056252011-06-17 14:18:39 -07002479
2480 if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
Dan Williamsdd047c82011-06-09 11:06:58 -07002481 task_sequence = ISCI_TAG_SEQ(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002482
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002483 if (task_sequence == ihost->io_request_sequence[task_index])
Dan Williams5076a1a2011-06-27 14:57:03 -07002484 return ireq;
Dan Williamscc9203b2011-05-08 17:34:44 -07002485 }
2486 }
2487
2488 return NULL;
2489}
2490
2491/**
2492 * This method allocates remote node index and the reserves the remote node
2493 * context space for use. This method can fail if there are no more remote
2494 * node index available.
2495 * @scic: This is the controller object which contains the set of
2496 * free remote node ids
2497 * @sci_dev: This is the device object which is requesting the a remote node
2498 * id
2499 * @node_id: This is the remote node id that is assinged to the device if one
2500 * is available
2501 *
2502 * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2503 * node index available.
2504 */
Dan Williams89a73012011-06-30 19:14:33 -07002505enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
2506 struct isci_remote_device *idev,
2507 u16 *node_id)
Dan Williamscc9203b2011-05-08 17:34:44 -07002508{
2509 u16 node_index;
Dan Williams89a73012011-06-30 19:14:33 -07002510 u32 remote_node_count = sci_remote_device_node_count(idev);
Dan Williamscc9203b2011-05-08 17:34:44 -07002511
Dan Williams89a73012011-06-30 19:14:33 -07002512 node_index = sci_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002513 &ihost->available_remote_nodes, remote_node_count
Dan Williamscc9203b2011-05-08 17:34:44 -07002514 );
2515
2516 if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002517 ihost->device_table[node_index] = idev;
Dan Williamscc9203b2011-05-08 17:34:44 -07002518
2519 *node_id = node_index;
2520
2521 return SCI_SUCCESS;
2522 }
2523
2524 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2525}
2526
Dan Williams89a73012011-06-30 19:14:33 -07002527void sci_controller_free_remote_node_context(struct isci_host *ihost,
2528 struct isci_remote_device *idev,
2529 u16 node_id)
Dan Williamscc9203b2011-05-08 17:34:44 -07002530{
Dan Williams89a73012011-06-30 19:14:33 -07002531 u32 remote_node_count = sci_remote_device_node_count(idev);
Dan Williamscc9203b2011-05-08 17:34:44 -07002532
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002533 if (ihost->device_table[node_id] == idev) {
2534 ihost->device_table[node_id] = NULL;
Dan Williamscc9203b2011-05-08 17:34:44 -07002535
Dan Williams89a73012011-06-30 19:14:33 -07002536 sci_remote_node_table_release_remote_node_index(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002537 &ihost->available_remote_nodes, remote_node_count, node_id
Dan Williamscc9203b2011-05-08 17:34:44 -07002538 );
2539 }
2540}
2541
Dan Williams89a73012011-06-30 19:14:33 -07002542void sci_controller_copy_sata_response(void *response_buffer,
2543 void *frame_header,
2544 void *frame_buffer)
Dan Williamscc9203b2011-05-08 17:34:44 -07002545{
Dan Williams89a73012011-06-30 19:14:33 -07002546 /* XXX type safety? */
Dan Williamscc9203b2011-05-08 17:34:44 -07002547 memcpy(response_buffer, frame_header, sizeof(u32));
2548
2549 memcpy(response_buffer + sizeof(u32),
2550 frame_buffer,
2551 sizeof(struct dev_to_host_fis) - sizeof(u32));
2552}
2553
Dan Williams89a73012011-06-30 19:14:33 -07002554void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
Dan Williamscc9203b2011-05-08 17:34:44 -07002555{
Dan Williams89a73012011-06-30 19:14:33 -07002556 if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002557 writel(ihost->uf_control.get,
2558 &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
Dan Williamscc9203b2011-05-08 17:34:44 -07002559}
2560
Dan Williams312e0c22011-06-28 13:47:09 -07002561void isci_tci_free(struct isci_host *ihost, u16 tci)
2562{
2563 u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
2564
2565 ihost->tci_pool[tail] = tci;
2566 ihost->tci_tail = tail + 1;
2567}
2568
2569static u16 isci_tci_alloc(struct isci_host *ihost)
2570{
2571 u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
2572 u16 tci = ihost->tci_pool[head];
2573
2574 ihost->tci_head = head + 1;
2575 return tci;
2576}
2577
2578static u16 isci_tci_space(struct isci_host *ihost)
2579{
2580 return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
2581}
2582
2583u16 isci_alloc_tag(struct isci_host *ihost)
2584{
2585 if (isci_tci_space(ihost)) {
2586 u16 tci = isci_tci_alloc(ihost);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002587 u8 seq = ihost->io_request_sequence[tci];
Dan Williams312e0c22011-06-28 13:47:09 -07002588
2589 return ISCI_TAG(seq, tci);
2590 }
2591
2592 return SCI_CONTROLLER_INVALID_IO_TAG;
2593}
2594
2595enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
2596{
Dan Williams312e0c22011-06-28 13:47:09 -07002597 u16 tci = ISCI_TAG_TCI(io_tag);
2598 u16 seq = ISCI_TAG_SEQ(io_tag);
2599
2600 /* prevent tail from passing head */
2601 if (isci_tci_active(ihost) == 0)
2602 return SCI_FAILURE_INVALID_IO_TAG;
2603
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002604 if (seq == ihost->io_request_sequence[tci]) {
2605 ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
Dan Williams312e0c22011-06-28 13:47:09 -07002606
2607 isci_tci_free(ihost, tci);
2608
2609 return SCI_SUCCESS;
2610 }
2611 return SCI_FAILURE_INVALID_IO_TAG;
2612}
2613
Dan Williams89a73012011-06-30 19:14:33 -07002614enum sci_status sci_controller_start_io(struct isci_host *ihost,
2615 struct isci_remote_device *idev,
2616 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002617{
2618 enum sci_status status;
2619
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002620 if (ihost->sm.current_state_id != SCIC_READY) {
2621 dev_warn(&ihost->pdev->dev, "invalid state to start I/O");
Dan Williamscc9203b2011-05-08 17:34:44 -07002622 return SCI_FAILURE_INVALID_STATE;
2623 }
2624
Dan Williams89a73012011-06-30 19:14:33 -07002625 status = sci_remote_device_start_io(ihost, idev, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002626 if (status != SCI_SUCCESS)
2627 return status;
2628
Dan Williams5076a1a2011-06-27 14:57:03 -07002629 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williams34a99152011-07-01 02:25:15 -07002630 sci_controller_post_request(ihost, ireq->post_context);
Dan Williamscc9203b2011-05-08 17:34:44 -07002631 return SCI_SUCCESS;
2632}
2633
Dan Williams89a73012011-06-30 19:14:33 -07002634enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
2635 struct isci_remote_device *idev,
2636 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002637{
Dan Williams89a73012011-06-30 19:14:33 -07002638 /* terminate an ongoing (i.e. started) core IO request. This does not
2639 * abort the IO request at the target, but rather removes the IO
2640 * request from the host controller.
2641 */
Dan Williamscc9203b2011-05-08 17:34:44 -07002642 enum sci_status status;
2643
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002644 if (ihost->sm.current_state_id != SCIC_READY) {
2645 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002646 "invalid state to terminate request\n");
2647 return SCI_FAILURE_INVALID_STATE;
2648 }
2649
Dan Williams89a73012011-06-30 19:14:33 -07002650 status = sci_io_request_terminate(ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002651 if (status != SCI_SUCCESS)
2652 return status;
2653
2654 /*
2655 * Utilize the original post context command and or in the POST_TC_ABORT
2656 * request sub-type.
2657 */
Dan Williams89a73012011-06-30 19:14:33 -07002658 sci_controller_post_request(ihost,
2659 ireq->post_context | SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
Dan Williamscc9203b2011-05-08 17:34:44 -07002660 return SCI_SUCCESS;
2661}
2662
2663/**
Dan Williams89a73012011-06-30 19:14:33 -07002664 * sci_controller_complete_io() - This method will perform core specific
Dan Williamscc9203b2011-05-08 17:34:44 -07002665 * completion operations for an IO request. After this method is invoked,
2666 * the user should consider the IO request as invalid until it is properly
2667 * reused (i.e. re-constructed).
Dan Williams89a73012011-06-30 19:14:33 -07002668 * @ihost: The handle to the controller object for which to complete the
Dan Williamscc9203b2011-05-08 17:34:44 -07002669 * IO request.
Dan Williams89a73012011-06-30 19:14:33 -07002670 * @idev: The handle to the remote device object for which to complete
Dan Williamscc9203b2011-05-08 17:34:44 -07002671 * the IO request.
Dan Williams89a73012011-06-30 19:14:33 -07002672 * @ireq: the handle to the io request object to complete.
Dan Williamscc9203b2011-05-08 17:34:44 -07002673 */
Dan Williams89a73012011-06-30 19:14:33 -07002674enum sci_status sci_controller_complete_io(struct isci_host *ihost,
2675 struct isci_remote_device *idev,
2676 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002677{
2678 enum sci_status status;
2679 u16 index;
2680
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002681 switch (ihost->sm.current_state_id) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002682 case SCIC_STOPPING:
Dan Williamscc9203b2011-05-08 17:34:44 -07002683 /* XXX: Implement this function */
2684 return SCI_FAILURE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002685 case SCIC_READY:
Dan Williams89a73012011-06-30 19:14:33 -07002686 status = sci_remote_device_complete_io(ihost, idev, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002687 if (status != SCI_SUCCESS)
2688 return status;
2689
Dan Williams5076a1a2011-06-27 14:57:03 -07002690 index = ISCI_TAG_TCI(ireq->io_tag);
2691 clear_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07002692 return SCI_SUCCESS;
2693 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002694 dev_warn(&ihost->pdev->dev, "invalid state to complete I/O");
Dan Williamscc9203b2011-05-08 17:34:44 -07002695 return SCI_FAILURE_INVALID_STATE;
2696 }
2697
2698}
2699
Dan Williams89a73012011-06-30 19:14:33 -07002700enum sci_status sci_controller_continue_io(struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002701{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002702 struct isci_host *ihost = ireq->owning_controller;
Dan Williamscc9203b2011-05-08 17:34:44 -07002703
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002704 if (ihost->sm.current_state_id != SCIC_READY) {
2705 dev_warn(&ihost->pdev->dev, "invalid state to continue I/O");
Dan Williamscc9203b2011-05-08 17:34:44 -07002706 return SCI_FAILURE_INVALID_STATE;
2707 }
2708
Dan Williams5076a1a2011-06-27 14:57:03 -07002709 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williams34a99152011-07-01 02:25:15 -07002710 sci_controller_post_request(ihost, ireq->post_context);
Dan Williamscc9203b2011-05-08 17:34:44 -07002711 return SCI_SUCCESS;
2712}
2713
2714/**
Dan Williams89a73012011-06-30 19:14:33 -07002715 * sci_controller_start_task() - This method is called by the SCIC user to
Dan Williamscc9203b2011-05-08 17:34:44 -07002716 * send/start a framework task management request.
2717 * @controller: the handle to the controller object for which to start the task
2718 * management request.
2719 * @remote_device: the handle to the remote device object for which to start
2720 * the task management request.
2721 * @task_request: the handle to the task request object to start.
Dan Williamscc9203b2011-05-08 17:34:44 -07002722 */
Dan Williams89a73012011-06-30 19:14:33 -07002723enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
2724 struct isci_remote_device *idev,
2725 struct isci_request *ireq)
Dan Williamscc9203b2011-05-08 17:34:44 -07002726{
2727 enum sci_status status;
2728
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002729 if (ihost->sm.current_state_id != SCIC_READY) {
2730 dev_warn(&ihost->pdev->dev,
Dan Williamscc9203b2011-05-08 17:34:44 -07002731 "%s: SCIC Controller starting task from invalid "
2732 "state\n",
2733 __func__);
2734 return SCI_TASK_FAILURE_INVALID_STATE;
2735 }
2736
Dan Williams89a73012011-06-30 19:14:33 -07002737 status = sci_remote_device_start_task(ihost, idev, ireq);
Dan Williamscc9203b2011-05-08 17:34:44 -07002738 switch (status) {
2739 case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
Dan Williamsdb056252011-06-17 14:18:39 -07002740 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07002741
2742 /*
2743 * We will let framework know this task request started successfully,
2744 * although core is still woring on starting the request (to post tc when
2745 * RNC is resumed.)
2746 */
2747 return SCI_SUCCESS;
2748 case SCI_SUCCESS:
Dan Williamsdb056252011-06-17 14:18:39 -07002749 set_bit(IREQ_ACTIVE, &ireq->flags);
Dan Williams34a99152011-07-01 02:25:15 -07002750 sci_controller_post_request(ihost, ireq->post_context);
Dan Williamscc9203b2011-05-08 17:34:44 -07002751 break;
2752 default:
2753 break;
2754 }
2755
2756 return status;
2757}