blob: ae9edae1d2452b203f1234c32e03a52ce982fff7 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Dan Williamsac668c62011-06-07 18:50:55 -070055#include <linux/circ_buf.h>
Dan Williamscc9203b2011-05-08 17:34:44 -070056#include <linux/device.h>
57#include <scsi/sas.h>
58#include "host.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070059#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070060#include "port.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070061#include "host.h"
Dan Williamsd044af12011-03-08 09:52:49 -080062#include "probe_roms.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070063#include "remote_device.h"
64#include "request.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070065#include "scu_completion_codes.h"
66#include "scu_event_codes.h"
Dan Williams63a3a152011-05-08 21:36:46 -070067#include "registers.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070068#include "scu_remote_node_context.h"
69#include "scu_task_context.h"
70#include "scu_unsolicited_frame.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070071
Dan Williamscc9203b2011-05-08 17:34:44 -070072#define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
73
Dan Williams7c78da32011-06-01 16:00:01 -070074#define smu_max_ports(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070075 (\
76 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
77 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
78 )
79
Dan Williams7c78da32011-06-01 16:00:01 -070080#define smu_max_task_contexts(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070081 (\
82 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
83 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
84 )
85
Dan Williams7c78da32011-06-01 16:00:01 -070086#define smu_max_rncs(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070087 (\
88 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
89 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
90 )
91
Dan Williamscc9203b2011-05-08 17:34:44 -070092#define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
93
94/**
95 *
96 *
97 * The number of milliseconds to wait while a given phy is consuming power
98 * before allowing another set of phys to consume power. Ultimately, this will
99 * be specified by OEM parameter.
100 */
101#define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
102
103/**
104 * NORMALIZE_PUT_POINTER() -
105 *
106 * This macro will normalize the completion queue put pointer so its value can
107 * be used as an array inde
108 */
109#define NORMALIZE_PUT_POINTER(x) \
110 ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
111
112
113/**
114 * NORMALIZE_EVENT_POINTER() -
115 *
116 * This macro will normalize the completion queue event entry so its value can
117 * be used as an index.
118 */
119#define NORMALIZE_EVENT_POINTER(x) \
120 (\
121 ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
122 >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
123 )
124
125/**
Dan Williamscc9203b2011-05-08 17:34:44 -0700126 * NORMALIZE_GET_POINTER() -
127 *
128 * This macro will normalize the completion queue get pointer so its value can
129 * be used as an index into an array
130 */
131#define NORMALIZE_GET_POINTER(x) \
132 ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
133
134/**
135 * NORMALIZE_GET_POINTER_CYCLE_BIT() -
136 *
137 * This macro will normalize the completion queue cycle pointer so it matches
138 * the completion queue cycle bit
139 */
140#define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
141 ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
142
143/**
144 * COMPLETION_QUEUE_CYCLE_BIT() -
145 *
146 * This macro will return the cycle bit of the completion queue entry
147 */
148#define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
149
Edmund Nadolski12ef6542011-06-02 00:10:50 +0000150/* Init the state machine and call the state entry function (if any) */
151void sci_init_sm(struct sci_base_state_machine *sm,
152 const struct sci_base_state *state_table, u32 initial_state)
153{
154 sci_state_transition_t handler;
155
156 sm->initial_state_id = initial_state;
157 sm->previous_state_id = initial_state;
158 sm->current_state_id = initial_state;
159 sm->state_table = state_table;
160
161 handler = sm->state_table[initial_state].enter_state;
162 if (handler)
163 handler(sm);
164}
165
166/* Call the state exit fn, update the current state, call the state entry fn */
167void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
168{
169 sci_state_transition_t handler;
170
171 handler = sm->state_table[sm->current_state_id].exit_state;
172 if (handler)
173 handler(sm);
174
175 sm->previous_state_id = sm->current_state_id;
176 sm->current_state_id = next_state;
177
178 handler = sm->state_table[sm->current_state_id].enter_state;
179 if (handler)
180 handler(sm);
181}
182
Dan Williamscc9203b2011-05-08 17:34:44 -0700183static bool scic_sds_controller_completion_queue_has_entries(
184 struct scic_sds_controller *scic)
185{
186 u32 get_value = scic->completion_queue_get;
187 u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
188
189 if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
190 COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
191 return true;
192
193 return false;
194}
195
196static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
197{
198 if (scic_sds_controller_completion_queue_has_entries(scic)) {
199 return true;
200 } else {
201 /*
202 * we have a spurious interrupt it could be that we have already
203 * emptied the completion queue from a previous interrupt */
204 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
205
206 /*
207 * There is a race in the hardware that could cause us not to be notified
208 * of an interrupt completion if we do not take this step. We will mask
209 * then unmask the interrupts so if there is another interrupt pending
210 * the clearing of the interrupt source we get the next interrupt message. */
211 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
212 writel(0, &scic->smu_registers->interrupt_mask);
213 }
214
215 return false;
216}
217
Dan Williamsc7ef4032011-02-18 09:25:05 -0800218irqreturn_t isci_msix_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700219{
Dan Williamsc7ef4032011-02-18 09:25:05 -0800220 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700221
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000222 if (scic_sds_controller_isr(&ihost->sci))
Dan Williams0cf89d12011-02-18 09:25:07 -0800223 tasklet_schedule(&ihost->completion_tasklet);
Dan Williams6f231dd2011-07-02 22:56:22 -0700224
Dan Williamsc7ef4032011-02-18 09:25:05 -0800225 return IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700226}
227
Dan Williamscc9203b2011-05-08 17:34:44 -0700228static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
229{
230 u32 interrupt_status;
231
232 interrupt_status =
233 readl(&scic->smu_registers->interrupt_status);
234 interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
235
236 if (interrupt_status != 0) {
237 /*
238 * There is an error interrupt pending so let it through and handle
239 * in the callback */
240 return true;
241 }
242
243 /*
244 * There is a race in the hardware that could cause us not to be notified
245 * of an interrupt completion if we do not take this step. We will mask
246 * then unmask the error interrupts so if there was another interrupt
247 * pending we will be notified.
248 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
249 writel(0xff, &scic->smu_registers->interrupt_mask);
250 writel(0, &scic->smu_registers->interrupt_mask);
251
252 return false;
253}
254
255static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
256 u32 completion_entry)
257{
258 u32 index;
Dan Williamsdd047c82011-06-09 11:06:58 -0700259 struct scic_sds_request *sci_req;
Dan Williamscc9203b2011-05-08 17:34:44 -0700260
261 index = SCU_GET_COMPLETION_INDEX(completion_entry);
Dan Williamsdd047c82011-06-09 11:06:58 -0700262 sci_req = scic->io_request_table[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700263
264 /* Make sure that we really want to process this IO request */
Dan Williamsdd047c82011-06-09 11:06:58 -0700265 if (sci_req && sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
266 ISCI_TAG_SEQ(sci_req->io_tag) == scic->io_request_sequence[index])
Dan Williamscc9203b2011-05-08 17:34:44 -0700267 /* Yep this is a valid io request pass it along to the io request handler */
Dan Williamsdd047c82011-06-09 11:06:58 -0700268 scic_sds_io_request_tc_completion(sci_req, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700269}
270
271static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
272 u32 completion_entry)
273{
274 u32 index;
275 struct scic_sds_request *io_request;
276 struct scic_sds_remote_device *device;
277
278 index = SCU_GET_COMPLETION_INDEX(completion_entry);
279
280 switch (scu_get_command_request_type(completion_entry)) {
281 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
282 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
283 io_request = scic->io_request_table[index];
284 dev_warn(scic_to_dev(scic),
285 "%s: SCIC SDS Completion type SDMA %x for io request "
286 "%p\n",
287 __func__,
288 completion_entry,
289 io_request);
290 /* @todo For a post TC operation we need to fail the IO
291 * request
292 */
293 break;
294
295 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
296 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
297 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
298 device = scic->device_table[index];
299 dev_warn(scic_to_dev(scic),
300 "%s: SCIC SDS Completion type SDMA %x for remote "
301 "device %p\n",
302 __func__,
303 completion_entry,
304 device);
305 /* @todo For a port RNC operation we need to fail the
306 * device
307 */
308 break;
309
310 default:
311 dev_warn(scic_to_dev(scic),
312 "%s: SCIC SDS Completion unknown SDMA completion "
313 "type %x\n",
314 __func__,
315 completion_entry);
316 break;
317
318 }
319}
320
321static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
322 u32 completion_entry)
323{
324 u32 index;
325 u32 frame_index;
326
327 struct isci_host *ihost = scic_to_ihost(scic);
328 struct scu_unsolicited_frame_header *frame_header;
329 struct scic_sds_phy *phy;
330 struct scic_sds_remote_device *device;
331
332 enum sci_status result = SCI_FAILURE;
333
334 frame_index = SCU_GET_FRAME_INDEX(completion_entry);
335
336 frame_header = scic->uf_control.buffers.array[frame_index].header;
337 scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
338
339 if (SCU_GET_FRAME_ERROR(completion_entry)) {
340 /*
341 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
342 * / this cause a problem? We expect the phy initialization will
343 * / fail if there is an error in the frame. */
344 scic_sds_controller_release_frame(scic, frame_index);
345 return;
346 }
347
348 if (frame_header->is_address_frame) {
349 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
350 phy = &ihost->phys[index].sci;
351 result = scic_sds_phy_frame_handler(phy, frame_index);
352 } else {
353
354 index = SCU_GET_COMPLETION_INDEX(completion_entry);
355
356 if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
357 /*
358 * This is a signature fis or a frame from a direct attached SATA
359 * device that has not yet been created. In either case forwared
360 * the frame to the PE and let it take care of the frame data. */
361 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
362 phy = &ihost->phys[index].sci;
363 result = scic_sds_phy_frame_handler(phy, frame_index);
364 } else {
365 if (index < scic->remote_node_entries)
366 device = scic->device_table[index];
367 else
368 device = NULL;
369
370 if (device != NULL)
371 result = scic_sds_remote_device_frame_handler(device, frame_index);
372 else
373 scic_sds_controller_release_frame(scic, frame_index);
374 }
375 }
376
377 if (result != SCI_SUCCESS) {
378 /*
379 * / @todo Is there any reason to report some additional error message
380 * / when we get this failure notifiction? */
381 }
382}
383
384static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
385 u32 completion_entry)
386{
387 struct isci_host *ihost = scic_to_ihost(scic);
388 struct scic_sds_request *io_request;
389 struct scic_sds_remote_device *device;
390 struct scic_sds_phy *phy;
391 u32 index;
392
393 index = SCU_GET_COMPLETION_INDEX(completion_entry);
394
395 switch (scu_get_event_type(completion_entry)) {
396 case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
397 /* / @todo The driver did something wrong and we need to fix the condtion. */
398 dev_err(scic_to_dev(scic),
399 "%s: SCIC Controller 0x%p received SMU command error "
400 "0x%x\n",
401 __func__,
402 scic,
403 completion_entry);
404 break;
405
406 case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
407 case SCU_EVENT_TYPE_SMU_ERROR:
408 case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
409 /*
410 * / @todo This is a hardware failure and its likely that we want to
411 * / reset the controller. */
412 dev_err(scic_to_dev(scic),
413 "%s: SCIC Controller 0x%p received fatal controller "
414 "event 0x%x\n",
415 __func__,
416 scic,
417 completion_entry);
418 break;
419
420 case SCU_EVENT_TYPE_TRANSPORT_ERROR:
421 io_request = scic->io_request_table[index];
422 scic_sds_io_request_event_handler(io_request, completion_entry);
423 break;
424
425 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
426 switch (scu_get_event_specifier(completion_entry)) {
427 case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
428 case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
429 io_request = scic->io_request_table[index];
430 if (io_request != NULL)
431 scic_sds_io_request_event_handler(io_request, completion_entry);
432 else
433 dev_warn(scic_to_dev(scic),
434 "%s: SCIC Controller 0x%p received "
435 "event 0x%x for io request object "
436 "that doesnt exist.\n",
437 __func__,
438 scic,
439 completion_entry);
440
441 break;
442
443 case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
444 device = scic->device_table[index];
445 if (device != NULL)
446 scic_sds_remote_device_event_handler(device, completion_entry);
447 else
448 dev_warn(scic_to_dev(scic),
449 "%s: SCIC Controller 0x%p received "
450 "event 0x%x for remote device object "
451 "that doesnt exist.\n",
452 __func__,
453 scic,
454 completion_entry);
455
456 break;
457 }
458 break;
459
460 case SCU_EVENT_TYPE_BROADCAST_CHANGE:
461 /*
462 * direct the broadcast change event to the phy first and then let
463 * the phy redirect the broadcast change to the port object */
464 case SCU_EVENT_TYPE_ERR_CNT_EVENT:
465 /*
466 * direct error counter event to the phy object since that is where
467 * we get the event notification. This is a type 4 event. */
468 case SCU_EVENT_TYPE_OSSP_EVENT:
469 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
470 phy = &ihost->phys[index].sci;
471 scic_sds_phy_event_handler(phy, completion_entry);
472 break;
473
474 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
475 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
476 case SCU_EVENT_TYPE_RNC_OPS_MISC:
477 if (index < scic->remote_node_entries) {
478 device = scic->device_table[index];
479
480 if (device != NULL)
481 scic_sds_remote_device_event_handler(device, completion_entry);
482 } else
483 dev_err(scic_to_dev(scic),
484 "%s: SCIC Controller 0x%p received event 0x%x "
485 "for remote device object 0x%0x that doesnt "
486 "exist.\n",
487 __func__,
488 scic,
489 completion_entry,
490 index);
491
492 break;
493
494 default:
495 dev_warn(scic_to_dev(scic),
496 "%s: SCIC Controller received unknown event code %x\n",
497 __func__,
498 completion_entry);
499 break;
500 }
501}
502
Dan Williamscc9203b2011-05-08 17:34:44 -0700503static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
504{
505 u32 completion_count = 0;
506 u32 completion_entry;
507 u32 get_index;
508 u32 get_cycle;
Dan Williams994a9302011-06-09 16:04:28 -0700509 u32 event_get;
Dan Williamscc9203b2011-05-08 17:34:44 -0700510 u32 event_cycle;
511
512 dev_dbg(scic_to_dev(scic),
513 "%s: completion queue begining get:0x%08x\n",
514 __func__,
515 scic->completion_queue_get);
516
517 /* Get the component parts of the completion queue */
518 get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
519 get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
520
Dan Williams994a9302011-06-09 16:04:28 -0700521 event_get = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
Dan Williamscc9203b2011-05-08 17:34:44 -0700522 event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
523
524 while (
525 NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
526 == COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
527 ) {
528 completion_count++;
529
530 completion_entry = scic->completion_queue[get_index];
Dan Williams994a9302011-06-09 16:04:28 -0700531
532 /* increment the get pointer and check for rollover to toggle the cycle bit */
533 get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
534 (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
535 get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
Dan Williamscc9203b2011-05-08 17:34:44 -0700536
537 dev_dbg(scic_to_dev(scic),
538 "%s: completion queue entry:0x%08x\n",
539 __func__,
540 completion_entry);
541
542 switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
543 case SCU_COMPLETION_TYPE_TASK:
544 scic_sds_controller_task_completion(scic, completion_entry);
545 break;
546
547 case SCU_COMPLETION_TYPE_SDMA:
548 scic_sds_controller_sdma_completion(scic, completion_entry);
549 break;
550
551 case SCU_COMPLETION_TYPE_UFI:
552 scic_sds_controller_unsolicited_frame(scic, completion_entry);
553 break;
554
555 case SCU_COMPLETION_TYPE_EVENT:
Dan Williams994a9302011-06-09 16:04:28 -0700556 case SCU_COMPLETION_TYPE_NOTIFY: {
557 event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
558 (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
559 event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
560
Dan Williamscc9203b2011-05-08 17:34:44 -0700561 scic_sds_controller_event_completion(scic, completion_entry);
562 break;
Dan Williams994a9302011-06-09 16:04:28 -0700563 }
Dan Williamscc9203b2011-05-08 17:34:44 -0700564 default:
565 dev_warn(scic_to_dev(scic),
566 "%s: SCIC Controller received unknown "
567 "completion type %x\n",
568 __func__,
569 completion_entry);
570 break;
571 }
572 }
573
574 /* Update the get register if we completed one or more entries */
575 if (completion_count > 0) {
576 scic->completion_queue_get =
577 SMU_CQGR_GEN_BIT(ENABLE) |
578 SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
579 event_cycle |
Dan Williams994a9302011-06-09 16:04:28 -0700580 SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
Dan Williamscc9203b2011-05-08 17:34:44 -0700581 get_cycle |
582 SMU_CQGR_GEN_VAL(POINTER, get_index);
583
584 writel(scic->completion_queue_get,
585 &scic->smu_registers->completion_queue_get);
586
587 }
588
589 dev_dbg(scic_to_dev(scic),
590 "%s: completion queue ending get:0x%08x\n",
591 __func__,
592 scic->completion_queue_get);
593
594}
595
596static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
597{
598 u32 interrupt_status;
599
600 interrupt_status =
601 readl(&scic->smu_registers->interrupt_status);
602
603 if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
604 scic_sds_controller_completion_queue_has_entries(scic)) {
605
606 scic_sds_controller_process_completions(scic);
607 writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
608 } else {
609 dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
610 interrupt_status);
611
Edmund Nadolskie3013702011-06-02 00:10:43 +0000612 sci_change_state(&scic->sm, SCIC_FAILED);
Dan Williamscc9203b2011-05-08 17:34:44 -0700613
614 return;
615 }
616
617 /* If we dont process any completions I am not sure that we want to do this.
618 * We are in the middle of a hardware fault and should probably be reset.
619 */
620 writel(0, &scic->smu_registers->interrupt_mask);
621}
622
Dan Williamsc7ef4032011-02-18 09:25:05 -0800623irqreturn_t isci_intx_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700624{
Dan Williams6f231dd2011-07-02 22:56:22 -0700625 irqreturn_t ret = IRQ_NONE;
Dan Williams31e824e2011-04-19 12:32:51 -0700626 struct isci_host *ihost = data;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000627 struct scic_sds_controller *scic = &ihost->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700628
Dan Williams31e824e2011-04-19 12:32:51 -0700629 if (scic_sds_controller_isr(scic)) {
630 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
631 tasklet_schedule(&ihost->completion_tasklet);
632 ret = IRQ_HANDLED;
633 } else if (scic_sds_controller_error_isr(scic)) {
634 spin_lock(&ihost->scic_lock);
635 scic_sds_controller_error_handler(scic);
636 spin_unlock(&ihost->scic_lock);
637 ret = IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700638 }
Dan Williams92f4f0f2011-02-18 09:25:11 -0800639
Dan Williams6f231dd2011-07-02 22:56:22 -0700640 return ret;
641}
642
Dan Williams92f4f0f2011-02-18 09:25:11 -0800643irqreturn_t isci_error_isr(int vec, void *data)
644{
645 struct isci_host *ihost = data;
Dan Williams92f4f0f2011-02-18 09:25:11 -0800646
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000647 if (scic_sds_controller_error_isr(&ihost->sci))
648 scic_sds_controller_error_handler(&ihost->sci);
Dan Williams92f4f0f2011-02-18 09:25:11 -0800649
650 return IRQ_HANDLED;
651}
Dan Williams6f231dd2011-07-02 22:56:22 -0700652
653/**
654 * isci_host_start_complete() - This function is called by the core library,
655 * through the ISCI Module, to indicate controller start status.
656 * @isci_host: This parameter specifies the ISCI host object
657 * @completion_status: This parameter specifies the completion status from the
658 * core library.
659 *
660 */
Dan Williamscc9203b2011-05-08 17:34:44 -0700661static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700662{
Dan Williams0cf89d12011-02-18 09:25:07 -0800663 if (completion_status != SCI_SUCCESS)
664 dev_info(&ihost->pdev->dev,
665 "controller start timed out, continuing...\n");
666 isci_host_change_state(ihost, isci_ready);
667 clear_bit(IHOST_START_PENDING, &ihost->flags);
668 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700669}
670
Dan Williamsc7ef4032011-02-18 09:25:05 -0800671int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
Dan Williams6f231dd2011-07-02 22:56:22 -0700672{
Dan Williams4393aa42011-03-31 13:10:44 -0700673 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williams6f231dd2011-07-02 22:56:22 -0700674
Edmund Nadolski77950f52011-02-18 09:25:09 -0800675 if (test_bit(IHOST_START_PENDING, &ihost->flags))
Dan Williams6f231dd2011-07-02 22:56:22 -0700676 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700677
Edmund Nadolski77950f52011-02-18 09:25:09 -0800678 /* todo: use sas_flush_discovery once it is upstream */
679 scsi_flush_work(shost);
680
681 scsi_flush_work(shost);
Dan Williams6f231dd2011-07-02 22:56:22 -0700682
Dan Williams0cf89d12011-02-18 09:25:07 -0800683 dev_dbg(&ihost->pdev->dev,
684 "%s: ihost->status = %d, time = %ld\n",
685 __func__, isci_host_get_state(ihost), time);
Dan Williams6f231dd2011-07-02 22:56:22 -0700686
Dan Williams6f231dd2011-07-02 22:56:22 -0700687 return 1;
688
689}
690
Dan Williamscc9203b2011-05-08 17:34:44 -0700691/**
692 * scic_controller_get_suggested_start_timeout() - This method returns the
693 * suggested scic_controller_start() timeout amount. The user is free to
694 * use any timeout value, but this method provides the suggested minimum
695 * start timeout value. The returned value is based upon empirical
696 * information determined as a result of interoperability testing.
697 * @controller: the handle to the controller object for which to return the
698 * suggested start timeout.
699 *
700 * This method returns the number of milliseconds for the suggested start
701 * operation timeout.
702 */
703static u32 scic_controller_get_suggested_start_timeout(
704 struct scic_sds_controller *sc)
705{
706 /* Validate the user supplied parameters. */
707 if (sc == NULL)
708 return 0;
709
710 /*
711 * The suggested minimum timeout value for a controller start operation:
712 *
713 * Signature FIS Timeout
714 * + Phy Start Timeout
715 * + Number of Phy Spin Up Intervals
716 * ---------------------------------
717 * Number of milliseconds for the controller start operation.
718 *
719 * NOTE: The number of phy spin up intervals will be equivalent
720 * to the number of phys divided by the number phys allowed
721 * per interval - 1 (once OEM parameters are supported).
722 * Currently we assume only 1 phy per interval. */
723
724 return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
725 + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
726 + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
727}
728
729static void scic_controller_enable_interrupts(
730 struct scic_sds_controller *scic)
731{
732 BUG_ON(scic->smu_registers == NULL);
733 writel(0, &scic->smu_registers->interrupt_mask);
734}
735
736void scic_controller_disable_interrupts(
737 struct scic_sds_controller *scic)
738{
739 BUG_ON(scic->smu_registers == NULL);
740 writel(0xffffffff, &scic->smu_registers->interrupt_mask);
741}
742
743static void scic_sds_controller_enable_port_task_scheduler(
744 struct scic_sds_controller *scic)
745{
746 u32 port_task_scheduler_value;
747
748 port_task_scheduler_value =
749 readl(&scic->scu_registers->peg0.ptsg.control);
750 port_task_scheduler_value |=
751 (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
752 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
753 writel(port_task_scheduler_value,
754 &scic->scu_registers->peg0.ptsg.control);
755}
756
757static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
758{
759 u32 task_assignment;
760
761 /*
762 * Assign all the TCs to function 0
763 * TODO: Do we actually need to read this register to write it back?
764 */
765
766 task_assignment =
767 readl(&scic->smu_registers->task_context_assignment[0]);
768
769 task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
770 (SMU_TCA_GEN_VAL(ENDING, scic->task_context_entries - 1)) |
771 (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
772
773 writel(task_assignment,
774 &scic->smu_registers->task_context_assignment[0]);
775
776}
777
778static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
779{
780 u32 index;
781 u32 completion_queue_control_value;
782 u32 completion_queue_get_value;
783 u32 completion_queue_put_value;
784
785 scic->completion_queue_get = 0;
786
Dan Williams7c78da32011-06-01 16:00:01 -0700787 completion_queue_control_value =
788 (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
789 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
Dan Williamscc9203b2011-05-08 17:34:44 -0700790
791 writel(completion_queue_control_value,
792 &scic->smu_registers->completion_queue_control);
793
794
795 /* Set the completion queue get pointer and enable the queue */
796 completion_queue_get_value = (
797 (SMU_CQGR_GEN_VAL(POINTER, 0))
798 | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
799 | (SMU_CQGR_GEN_BIT(ENABLE))
800 | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
801 );
802
803 writel(completion_queue_get_value,
804 &scic->smu_registers->completion_queue_get);
805
806 /* Set the completion queue put pointer */
807 completion_queue_put_value = (
808 (SMU_CQPR_GEN_VAL(POINTER, 0))
809 | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
810 );
811
812 writel(completion_queue_put_value,
813 &scic->smu_registers->completion_queue_put);
814
815 /* Initialize the cycle bit of the completion queue entries */
Dan Williams7c78da32011-06-01 16:00:01 -0700816 for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700817 /*
818 * If get.cycle_bit != completion_queue.cycle_bit
819 * its not a valid completion queue entry
820 * so at system start all entries are invalid */
821 scic->completion_queue[index] = 0x80000000;
822 }
823}
824
825static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
826{
827 u32 frame_queue_control_value;
828 u32 frame_queue_get_value;
829 u32 frame_queue_put_value;
830
831 /* Write the queue size */
832 frame_queue_control_value =
Dan Williams7c78da32011-06-01 16:00:01 -0700833 SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
Dan Williamscc9203b2011-05-08 17:34:44 -0700834
835 writel(frame_queue_control_value,
836 &scic->scu_registers->sdma.unsolicited_frame_queue_control);
837
838 /* Setup the get pointer for the unsolicited frame queue */
839 frame_queue_get_value = (
840 SCU_UFQGP_GEN_VAL(POINTER, 0)
841 | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
842 );
843
844 writel(frame_queue_get_value,
845 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
846 /* Setup the put pointer for the unsolicited frame queue */
847 frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
848 writel(frame_queue_put_value,
849 &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
850}
851
852/**
853 * This method will attempt to transition into the ready state for the
854 * controller and indicate that the controller start operation has completed
855 * if all criteria are met.
856 * @scic: This parameter indicates the controller object for which
857 * to transition to ready.
858 * @status: This parameter indicates the status value to be pass into the call
859 * to scic_cb_controller_start_complete().
860 *
861 * none.
862 */
863static void scic_sds_controller_transition_to_ready(
864 struct scic_sds_controller *scic,
865 enum sci_status status)
866{
867 struct isci_host *ihost = scic_to_ihost(scic);
868
Edmund Nadolskie3013702011-06-02 00:10:43 +0000869 if (scic->sm.current_state_id == SCIC_STARTING) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700870 /*
871 * We move into the ready state, because some of the phys/ports
872 * may be up and operational.
873 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000874 sci_change_state(&scic->sm, SCIC_READY);
Dan Williamscc9203b2011-05-08 17:34:44 -0700875
876 isci_host_start_complete(ihost, status);
877 }
878}
879
Adam Gruchala4a33c522011-05-10 23:54:23 +0000880static bool is_phy_starting(struct scic_sds_phy *sci_phy)
881{
882 enum scic_sds_phy_states state;
883
Edmund Nadolskie3013702011-06-02 00:10:43 +0000884 state = sci_phy->sm.current_state_id;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000885 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000886 case SCI_PHY_STARTING:
887 case SCI_PHY_SUB_INITIAL:
888 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
889 case SCI_PHY_SUB_AWAIT_IAF_UF:
890 case SCI_PHY_SUB_AWAIT_SAS_POWER:
891 case SCI_PHY_SUB_AWAIT_SATA_POWER:
892 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
893 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
894 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
895 case SCI_PHY_SUB_FINAL:
Adam Gruchala4a33c522011-05-10 23:54:23 +0000896 return true;
897 default:
898 return false;
899 }
900}
901
Dan Williamscc9203b2011-05-08 17:34:44 -0700902/**
903 * scic_sds_controller_start_next_phy - start phy
904 * @scic: controller
905 *
906 * If all the phys have been started, then attempt to transition the
907 * controller to the READY state and inform the user
908 * (scic_cb_controller_start_complete()).
909 */
910static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
911{
912 struct isci_host *ihost = scic_to_ihost(scic);
913 struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
914 struct scic_sds_phy *sci_phy;
915 enum sci_status status;
916
917 status = SCI_SUCCESS;
918
919 if (scic->phy_startup_timer_pending)
920 return status;
921
922 if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
923 bool is_controller_start_complete = true;
924 u32 state;
925 u8 index;
926
927 for (index = 0; index < SCI_MAX_PHYS; index++) {
928 sci_phy = &ihost->phys[index].sci;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000929 state = sci_phy->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -0700930
Dan Williams4f20ef42011-05-12 06:00:31 -0700931 if (!phy_get_non_dummy_port(sci_phy))
Dan Williamscc9203b2011-05-08 17:34:44 -0700932 continue;
933
934 /* The controller start operation is complete iff:
935 * - all links have been given an opportunity to start
936 * - have no indication of a connected device
937 * - have an indication of a connected device and it has
938 * finished the link training process.
939 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000940 if ((sci_phy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
941 (sci_phy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
942 (sci_phy->is_in_link_training == true && is_phy_starting(sci_phy))) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700943 is_controller_start_complete = false;
944 break;
945 }
946 }
947
948 /*
949 * The controller has successfully finished the start process.
950 * Inform the SCI Core user and transition to the READY state. */
951 if (is_controller_start_complete == true) {
952 scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700953 sci_del_timer(&scic->phy_timer);
954 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -0700955 }
956 } else {
957 sci_phy = &ihost->phys[scic->next_phy_to_start].sci;
958
959 if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dan Williams4f20ef42011-05-12 06:00:31 -0700960 if (phy_get_non_dummy_port(sci_phy) == NULL) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700961 scic->next_phy_to_start++;
962
963 /* Caution recursion ahead be forwarned
964 *
965 * The PHY was never added to a PORT in MPC mode
966 * so start the next phy in sequence This phy
967 * will never go link up and will not draw power
968 * the OEM parameters either configured the phy
969 * incorrectly for the PORT or it was never
970 * assigned to a PORT
971 */
972 return scic_sds_controller_start_next_phy(scic);
973 }
974 }
975
976 status = scic_sds_phy_start(sci_phy);
977
978 if (status == SCI_SUCCESS) {
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700979 sci_mod_timer(&scic->phy_timer,
980 SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
981 scic->phy_startup_timer_pending = true;
Dan Williamscc9203b2011-05-08 17:34:44 -0700982 } else {
983 dev_warn(scic_to_dev(scic),
984 "%s: Controller stop operation failed "
985 "to stop phy %d because of status "
986 "%d.\n",
987 __func__,
988 ihost->phys[scic->next_phy_to_start].sci.phy_index,
989 status);
990 }
991
992 scic->next_phy_to_start++;
993 }
994
995 return status;
996}
997
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700998static void phy_startup_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -0700999{
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001000 struct sci_timer *tmr = (struct sci_timer *)data;
1001 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), phy_timer);
1002 struct isci_host *ihost = scic_to_ihost(scic);
1003 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001004 enum sci_status status;
1005
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001006 spin_lock_irqsave(&ihost->scic_lock, flags);
1007
1008 if (tmr->cancel)
1009 goto done;
1010
Dan Williamscc9203b2011-05-08 17:34:44 -07001011 scic->phy_startup_timer_pending = false;
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001012
1013 do {
Dan Williamscc9203b2011-05-08 17:34:44 -07001014 status = scic_sds_controller_start_next_phy(scic);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001015 } while (status != SCI_SUCCESS);
1016
1017done:
1018 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001019}
1020
Dan Williamsac668c62011-06-07 18:50:55 -07001021static void isci_tci_free(struct isci_host *ihost, u16 tci)
1022{
1023 u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
1024
1025 ihost->tci_pool[tail] = tci;
1026 ihost->tci_tail = tail + 1;
1027}
1028
1029static u16 isci_tci_alloc(struct isci_host *ihost)
1030{
1031 u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
1032 u16 tci = ihost->tci_pool[head];
1033
1034 ihost->tci_head = head + 1;
1035 return tci;
1036}
1037
1038static u16 isci_tci_active(struct isci_host *ihost)
1039{
1040 return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
1041}
1042
1043static u16 isci_tci_space(struct isci_host *ihost)
1044{
1045 return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
1046}
1047
Dan Williamscc9203b2011-05-08 17:34:44 -07001048static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
1049 u32 timeout)
1050{
1051 struct isci_host *ihost = scic_to_ihost(scic);
1052 enum sci_status result;
1053 u16 index;
1054
Edmund Nadolskie3013702011-06-02 00:10:43 +00001055 if (scic->sm.current_state_id != SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001056 dev_warn(scic_to_dev(scic),
1057 "SCIC Controller start operation requested in "
1058 "invalid state\n");
1059 return SCI_FAILURE_INVALID_STATE;
1060 }
1061
1062 /* Build the TCi free pool */
Dan Williamsac668c62011-06-07 18:50:55 -07001063 BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1064 ihost->tci_head = 0;
1065 ihost->tci_tail = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001066 for (index = 0; index < scic->task_context_entries; index++)
Dan Williamsac668c62011-06-07 18:50:55 -07001067 isci_tci_free(ihost, index);
Dan Williamscc9203b2011-05-08 17:34:44 -07001068
1069 /* Build the RNi free pool */
1070 scic_sds_remote_node_table_initialize(
1071 &scic->available_remote_nodes,
1072 scic->remote_node_entries);
1073
1074 /*
1075 * Before anything else lets make sure we will not be
1076 * interrupted by the hardware.
1077 */
1078 scic_controller_disable_interrupts(scic);
1079
1080 /* Enable the port task scheduler */
1081 scic_sds_controller_enable_port_task_scheduler(scic);
1082
1083 /* Assign all the task entries to scic physical function */
1084 scic_sds_controller_assign_task_entries(scic);
1085
1086 /* Now initialize the completion queue */
1087 scic_sds_controller_initialize_completion_queue(scic);
1088
1089 /* Initialize the unsolicited frame queue for use */
1090 scic_sds_controller_initialize_unsolicited_frame_queue(scic);
1091
1092 /* Start all of the ports on this controller */
1093 for (index = 0; index < scic->logical_port_entries; index++) {
1094 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1095
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001096 result = scic_sds_port_start(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001097 if (result)
1098 return result;
1099 }
1100
1101 scic_sds_controller_start_next_phy(scic);
1102
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001103 sci_mod_timer(&scic->timer, timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07001104
Edmund Nadolskie3013702011-06-02 00:10:43 +00001105 sci_change_state(&scic->sm, SCIC_STARTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001106
1107 return SCI_SUCCESS;
1108}
1109
Dan Williams6f231dd2011-07-02 22:56:22 -07001110void isci_host_scan_start(struct Scsi_Host *shost)
1111{
Dan Williams4393aa42011-03-31 13:10:44 -07001112 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001113 unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001114
Dan Williams0cf89d12011-02-18 09:25:07 -08001115 set_bit(IHOST_START_PENDING, &ihost->flags);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001116
1117 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001118 scic_controller_start(&ihost->sci, tmo);
1119 scic_controller_enable_interrupts(&ihost->sci);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001120 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001121}
1122
Dan Williamscc9203b2011-05-08 17:34:44 -07001123static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001124{
Dan Williams0cf89d12011-02-18 09:25:07 -08001125 isci_host_change_state(ihost, isci_stopped);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001126 scic_controller_disable_interrupts(&ihost->sci);
Dan Williams0cf89d12011-02-18 09:25:07 -08001127 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
1128 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001129}
1130
Dan Williamscc9203b2011-05-08 17:34:44 -07001131static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
1132{
1133 /* Empty out the completion queue */
1134 if (scic_sds_controller_completion_queue_has_entries(scic))
1135 scic_sds_controller_process_completions(scic);
1136
1137 /* Clear the interrupt and enable all interrupts again */
1138 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
1139 /* Could we write the value of SMU_ISR_COMPLETION? */
1140 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
1141 writel(0, &scic->smu_registers->interrupt_mask);
1142}
1143
Dan Williams6f231dd2011-07-02 22:56:22 -07001144/**
1145 * isci_host_completion_routine() - This function is the delayed service
1146 * routine that calls the sci core library's completion handler. It's
1147 * scheduled as a tasklet from the interrupt service routine when interrupts
1148 * in use, or set as the timeout function in polled mode.
1149 * @data: This parameter specifies the ISCI host object
1150 *
1151 */
1152static void isci_host_completion_routine(unsigned long data)
1153{
1154 struct isci_host *isci_host = (struct isci_host *)data;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001155 struct list_head completed_request_list;
1156 struct list_head errored_request_list;
1157 struct list_head *current_position;
1158 struct list_head *next_position;
Dan Williams6f231dd2011-07-02 22:56:22 -07001159 struct isci_request *request;
1160 struct isci_request *next_request;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001161 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -07001162
1163 INIT_LIST_HEAD(&completed_request_list);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001164 INIT_LIST_HEAD(&errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001165
1166 spin_lock_irq(&isci_host->scic_lock);
1167
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001168 scic_sds_controller_completion_handler(&isci_host->sci);
Dan Williamsc7ef4032011-02-18 09:25:05 -08001169
Dan Williams6f231dd2011-07-02 22:56:22 -07001170 /* Take the lists of completed I/Os from the host. */
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001171
Dan Williams6f231dd2011-07-02 22:56:22 -07001172 list_splice_init(&isci_host->requests_to_complete,
1173 &completed_request_list);
1174
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001175 /* Take the list of errored I/Os from the host. */
1176 list_splice_init(&isci_host->requests_to_errorback,
1177 &errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001178
1179 spin_unlock_irq(&isci_host->scic_lock);
1180
1181 /* Process any completions in the lists. */
1182 list_for_each_safe(current_position, next_position,
1183 &completed_request_list) {
1184
1185 request = list_entry(current_position, struct isci_request,
1186 completed_node);
1187 task = isci_request_access_task(request);
1188
1189 /* Normal notification (task_done) */
1190 dev_dbg(&isci_host->pdev->dev,
1191 "%s: Normal - request/task = %p/%p\n",
1192 __func__,
1193 request,
1194 task);
1195
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001196 /* Return the task to libsas */
1197 if (task != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001198
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001199 task->lldd_task = NULL;
1200 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1201
1202 /* If the task is already in the abort path,
1203 * the task_done callback cannot be called.
1204 */
1205 task->task_done(task);
1206 }
1207 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001208 /* Free the request object. */
1209 isci_request_free(isci_host, request);
1210 }
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001211 list_for_each_entry_safe(request, next_request, &errored_request_list,
Dan Williams6f231dd2011-07-02 22:56:22 -07001212 completed_node) {
1213
1214 task = isci_request_access_task(request);
1215
1216 /* Use sas_task_abort */
1217 dev_warn(&isci_host->pdev->dev,
1218 "%s: Error - request/task = %p/%p\n",
1219 __func__,
1220 request,
1221 task);
1222
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001223 if (task != NULL) {
1224
1225 /* Put the task into the abort path if it's not there
1226 * already.
1227 */
1228 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
1229 sas_task_abort(task);
1230
1231 } else {
1232 /* This is a case where the request has completed with a
1233 * status such that it needed further target servicing,
1234 * but the sas_task reference has already been removed
1235 * from the request. Since it was errored, it was not
1236 * being aborted, so there is nothing to do except free
1237 * it.
1238 */
1239
1240 spin_lock_irq(&isci_host->scic_lock);
1241 /* Remove the request from the remote device's list
1242 * of pending requests.
1243 */
1244 list_del_init(&request->dev_node);
1245 spin_unlock_irq(&isci_host->scic_lock);
1246
1247 /* Free the request object. */
1248 isci_request_free(isci_host, request);
1249 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001250 }
1251
1252}
1253
Dan Williamscc9203b2011-05-08 17:34:44 -07001254/**
1255 * scic_controller_stop() - This method will stop an individual controller
1256 * object.This method will invoke the associated user callback upon
1257 * completion. The completion callback is called when the following
1258 * conditions are met: -# the method return status is SCI_SUCCESS. -# the
1259 * controller has been quiesced. This method will ensure that all IO
1260 * requests are quiesced, phys are stopped, and all additional operation by
1261 * the hardware is halted.
1262 * @controller: the handle to the controller object to stop.
1263 * @timeout: This parameter specifies the number of milliseconds in which the
1264 * stop operation should complete.
1265 *
1266 * The controller must be in the STARTED or STOPPED state. Indicate if the
1267 * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1268 * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1269 * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1270 * controller is not either in the STARTED or STOPPED states.
1271 */
1272static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
1273 u32 timeout)
1274{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001275 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001276 dev_warn(scic_to_dev(scic),
1277 "SCIC Controller stop operation requested in "
1278 "invalid state\n");
1279 return SCI_FAILURE_INVALID_STATE;
1280 }
1281
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001282 sci_mod_timer(&scic->timer, timeout);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001283 sci_change_state(&scic->sm, SCIC_STOPPING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001284 return SCI_SUCCESS;
1285}
1286
1287/**
1288 * scic_controller_reset() - This method will reset the supplied core
1289 * controller regardless of the state of said controller. This operation is
1290 * considered destructive. In other words, all current operations are wiped
1291 * out. No IO completions for outstanding devices occur. Outstanding IO
1292 * requests are not aborted or completed at the actual remote device.
1293 * @controller: the handle to the controller object to reset.
1294 *
1295 * Indicate if the controller reset method succeeded or failed in some way.
1296 * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1297 * the controller reset operation is unable to complete.
1298 */
1299static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
1300{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001301 switch (scic->sm.current_state_id) {
1302 case SCIC_RESET:
1303 case SCIC_READY:
1304 case SCIC_STOPPED:
1305 case SCIC_FAILED:
Dan Williamscc9203b2011-05-08 17:34:44 -07001306 /*
1307 * The reset operation is not a graceful cleanup, just
1308 * perform the state transition.
1309 */
Edmund Nadolskie3013702011-06-02 00:10:43 +00001310 sci_change_state(&scic->sm, SCIC_RESETTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001311 return SCI_SUCCESS;
1312 default:
1313 dev_warn(scic_to_dev(scic),
1314 "SCIC Controller reset operation requested in "
1315 "invalid state\n");
1316 return SCI_FAILURE_INVALID_STATE;
1317 }
1318}
1319
Dan Williams0cf89d12011-02-18 09:25:07 -08001320void isci_host_deinit(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07001321{
1322 int i;
1323
Dan Williams0cf89d12011-02-18 09:25:07 -08001324 isci_host_change_state(ihost, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001325 for (i = 0; i < SCI_MAX_PORTS; i++) {
Dan Williamse5313812011-05-07 10:11:43 -07001326 struct isci_port *iport = &ihost->ports[i];
Dan Williams0cf89d12011-02-18 09:25:07 -08001327 struct isci_remote_device *idev, *d;
1328
Dan Williamse5313812011-05-07 10:11:43 -07001329 list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
Dan Williams0cf89d12011-02-18 09:25:07 -08001330 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001331 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001332 }
1333 }
1334
Dan Williams0cf89d12011-02-18 09:25:07 -08001335 set_bit(IHOST_STOP_PENDING, &ihost->flags);
Dan Williams7c40a802011-03-02 11:49:26 -08001336
1337 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001338 scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
Dan Williams7c40a802011-03-02 11:49:26 -08001339 spin_unlock_irq(&ihost->scic_lock);
1340
Dan Williams0cf89d12011-02-18 09:25:07 -08001341 wait_for_stop(ihost);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001342 scic_controller_reset(&ihost->sci);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001343
1344 /* Cancel any/all outstanding port timers */
1345 for (i = 0; i < ihost->sci.logical_port_entries; i++) {
1346 struct scic_sds_port *sci_port = &ihost->ports[i].sci;
1347 del_timer_sync(&sci_port->timer.timer);
1348 }
1349
Edmund Nadolskia628d472011-05-19 11:59:36 +00001350 /* Cancel any/all outstanding phy timers */
1351 for (i = 0; i < SCI_MAX_PHYS; i++) {
1352 struct scic_sds_phy *sci_phy = &ihost->phys[i].sci;
1353 del_timer_sync(&sci_phy->sata_timer.timer);
1354 }
1355
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -07001356 del_timer_sync(&ihost->sci.port_agent.timer.timer);
1357
Edmund Nadolski04736612011-05-19 20:17:47 -07001358 del_timer_sync(&ihost->sci.power_control.timer.timer);
1359
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001360 del_timer_sync(&ihost->sci.timer.timer);
1361
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001362 del_timer_sync(&ihost->sci.phy_timer.timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001363}
1364
Dan Williams6f231dd2011-07-02 22:56:22 -07001365static void __iomem *scu_base(struct isci_host *isci_host)
1366{
1367 struct pci_dev *pdev = isci_host->pdev;
1368 int id = isci_host->id;
1369
1370 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
1371}
1372
1373static void __iomem *smu_base(struct isci_host *isci_host)
1374{
1375 struct pci_dev *pdev = isci_host->pdev;
1376 int id = isci_host->id;
1377
1378 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
1379}
1380
Dave Jiangb5f18a22011-03-16 14:57:23 -07001381static void isci_user_parameters_get(
1382 struct isci_host *isci_host,
1383 union scic_user_parameters *scic_user_params)
1384{
1385 struct scic_sds_user_parameters *u = &scic_user_params->sds1;
1386 int i;
1387
1388 for (i = 0; i < SCI_MAX_PHYS; i++) {
1389 struct sci_phy_user_params *u_phy = &u->phys[i];
1390
1391 u_phy->max_speed_generation = phy_gen;
1392
1393 /* we are not exporting these for now */
1394 u_phy->align_insertion_frequency = 0x7f;
1395 u_phy->in_connection_align_insertion_frequency = 0xff;
1396 u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1397 }
1398
1399 u->stp_inactivity_timeout = stp_inactive_to;
1400 u->ssp_inactivity_timeout = ssp_inactive_to;
1401 u->stp_max_occupancy_timeout = stp_max_occ_to;
1402 u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1403 u->no_outbound_task_timeout = no_outbound_task_to;
1404 u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1405}
1406
Dan Williams9269e0e2011-05-12 07:42:17 -07001407static void scic_sds_controller_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001408{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001409 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001410
Edmund Nadolskie3013702011-06-02 00:10:43 +00001411 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001412}
1413
Dan Williams9269e0e2011-05-12 07:42:17 -07001414static inline void scic_sds_controller_starting_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001415{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001416 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001417
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001418 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001419}
1420
1421#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1422#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1423#define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
1424#define INTERRUPT_COALESCE_NUMBER_MAX 256
1425#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
1426#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
1427
1428/**
1429 * scic_controller_set_interrupt_coalescence() - This method allows the user to
1430 * configure the interrupt coalescence.
1431 * @controller: This parameter represents the handle to the controller object
1432 * for which its interrupt coalesce register is overridden.
1433 * @coalesce_number: Used to control the number of entries in the Completion
1434 * Queue before an interrupt is generated. If the number of entries exceed
1435 * this number, an interrupt will be generated. The valid range of the input
1436 * is [0, 256]. A setting of 0 results in coalescing being disabled.
1437 * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1438 * input is [0, 2700000] . A setting of 0 is allowed and results in no
1439 * interrupt coalescing timeout.
1440 *
1441 * Indicate if the user successfully set the interrupt coalesce parameters.
1442 * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1443 * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1444 */
1445static enum sci_status scic_controller_set_interrupt_coalescence(
1446 struct scic_sds_controller *scic_controller,
1447 u32 coalesce_number,
1448 u32 coalesce_timeout)
1449{
1450 u8 timeout_encode = 0;
1451 u32 min = 0;
1452 u32 max = 0;
1453
1454 /* Check if the input parameters fall in the range. */
1455 if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1456 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1457
1458 /*
1459 * Defined encoding for interrupt coalescing timeout:
1460 * Value Min Max Units
1461 * ----- --- --- -----
1462 * 0 - - Disabled
1463 * 1 13.3 20.0 ns
1464 * 2 26.7 40.0
1465 * 3 53.3 80.0
1466 * 4 106.7 160.0
1467 * 5 213.3 320.0
1468 * 6 426.7 640.0
1469 * 7 853.3 1280.0
1470 * 8 1.7 2.6 us
1471 * 9 3.4 5.1
1472 * 10 6.8 10.2
1473 * 11 13.7 20.5
1474 * 12 27.3 41.0
1475 * 13 54.6 81.9
1476 * 14 109.2 163.8
1477 * 15 218.5 327.7
1478 * 16 436.9 655.4
1479 * 17 873.8 1310.7
1480 * 18 1.7 2.6 ms
1481 * 19 3.5 5.2
1482 * 20 7.0 10.5
1483 * 21 14.0 21.0
1484 * 22 28.0 41.9
1485 * 23 55.9 83.9
1486 * 24 111.8 167.8
1487 * 25 223.7 335.5
1488 * 26 447.4 671.1
1489 * 27 894.8 1342.2
1490 * 28 1.8 2.7 s
1491 * Others Undefined */
1492
1493 /*
1494 * Use the table above to decide the encode of interrupt coalescing timeout
1495 * value for register writing. */
1496 if (coalesce_timeout == 0)
1497 timeout_encode = 0;
1498 else{
1499 /* make the timeout value in unit of (10 ns). */
1500 coalesce_timeout = coalesce_timeout * 100;
1501 min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1502 max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1503
1504 /* get the encode of timeout for register writing. */
1505 for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1506 timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1507 timeout_encode++) {
1508 if (min <= coalesce_timeout && max > coalesce_timeout)
1509 break;
1510 else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1511 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1512 if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1513 break;
1514 else{
1515 timeout_encode++;
1516 break;
1517 }
1518 } else {
1519 max = max * 2;
1520 min = min * 2;
1521 }
1522 }
1523
1524 if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1525 /* the value is out of range. */
1526 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1527 }
1528
1529 writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1530 SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1531 &scic_controller->smu_registers->interrupt_coalesce_control);
1532
1533
1534 scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
1535 scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
1536
1537 return SCI_SUCCESS;
1538}
1539
1540
Dan Williams9269e0e2011-05-12 07:42:17 -07001541static void scic_sds_controller_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001542{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001543 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001544
1545 /* set the default interrupt coalescence number and timeout value. */
1546 scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
1547}
1548
Dan Williams9269e0e2011-05-12 07:42:17 -07001549static void scic_sds_controller_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001550{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001551 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001552
1553 /* disable interrupt coalescence. */
1554 scic_controller_set_interrupt_coalescence(scic, 0, 0);
1555}
1556
1557static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
1558{
1559 u32 index;
1560 enum sci_status status;
1561 enum sci_status phy_status;
1562 struct isci_host *ihost = scic_to_ihost(scic);
1563
1564 status = SCI_SUCCESS;
1565
1566 for (index = 0; index < SCI_MAX_PHYS; index++) {
1567 phy_status = scic_sds_phy_stop(&ihost->phys[index].sci);
1568
1569 if (phy_status != SCI_SUCCESS &&
1570 phy_status != SCI_FAILURE_INVALID_STATE) {
1571 status = SCI_FAILURE;
1572
1573 dev_warn(scic_to_dev(scic),
1574 "%s: Controller stop operation failed to stop "
1575 "phy %d because of status %d.\n",
1576 __func__,
1577 ihost->phys[index].sci.phy_index, phy_status);
1578 }
1579 }
1580
1581 return status;
1582}
1583
1584static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
1585{
1586 u32 index;
1587 enum sci_status port_status;
1588 enum sci_status status = SCI_SUCCESS;
1589 struct isci_host *ihost = scic_to_ihost(scic);
1590
1591 for (index = 0; index < scic->logical_port_entries; index++) {
1592 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
Dan Williamscc9203b2011-05-08 17:34:44 -07001593
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001594 port_status = scic_sds_port_stop(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001595
1596 if ((port_status != SCI_SUCCESS) &&
1597 (port_status != SCI_FAILURE_INVALID_STATE)) {
1598 status = SCI_FAILURE;
1599
1600 dev_warn(scic_to_dev(scic),
1601 "%s: Controller stop operation failed to "
1602 "stop port %d because of status %d.\n",
1603 __func__,
1604 sci_port->logical_port_index,
1605 port_status);
1606 }
1607 }
1608
1609 return status;
1610}
1611
1612static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
1613{
1614 u32 index;
1615 enum sci_status status;
1616 enum sci_status device_status;
1617
1618 status = SCI_SUCCESS;
1619
1620 for (index = 0; index < scic->remote_node_entries; index++) {
1621 if (scic->device_table[index] != NULL) {
1622 /* / @todo What timeout value do we want to provide to this request? */
1623 device_status = scic_remote_device_stop(scic->device_table[index], 0);
1624
1625 if ((device_status != SCI_SUCCESS) &&
1626 (device_status != SCI_FAILURE_INVALID_STATE)) {
1627 dev_warn(scic_to_dev(scic),
1628 "%s: Controller stop operation failed "
1629 "to stop device 0x%p because of "
1630 "status %d.\n",
1631 __func__,
1632 scic->device_table[index], device_status);
1633 }
1634 }
1635 }
1636
1637 return status;
1638}
1639
Dan Williams9269e0e2011-05-12 07:42:17 -07001640static void scic_sds_controller_stopping_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001641{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001642 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001643
1644 /* Stop all of the components for this controller */
1645 scic_sds_controller_stop_phys(scic);
1646 scic_sds_controller_stop_ports(scic);
1647 scic_sds_controller_stop_devices(scic);
1648}
1649
Dan Williams9269e0e2011-05-12 07:42:17 -07001650static void scic_sds_controller_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001651{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001652 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001653
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001654 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001655}
1656
1657
1658/**
1659 * scic_sds_controller_reset_hardware() -
1660 *
1661 * This method will reset the controller hardware.
1662 */
1663static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
1664{
1665 /* Disable interrupts so we dont take any spurious interrupts */
1666 scic_controller_disable_interrupts(scic);
1667
1668 /* Reset the SCU */
1669 writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
1670
1671 /* Delay for 1ms to before clearing the CQP and UFQPR. */
1672 udelay(1000);
1673
1674 /* The write to the CQGR clears the CQP */
1675 writel(0x00000000, &scic->smu_registers->completion_queue_get);
1676
1677 /* The write to the UFQGP clears the UFQPR */
1678 writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
1679}
1680
Dan Williams9269e0e2011-05-12 07:42:17 -07001681static void scic_sds_controller_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001682{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001683 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001684
1685 scic_sds_controller_reset_hardware(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001686 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001687}
1688
1689static const struct sci_base_state scic_sds_controller_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001690 [SCIC_INITIAL] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001691 .enter_state = scic_sds_controller_initial_state_enter,
1692 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001693 [SCIC_RESET] = {},
1694 [SCIC_INITIALIZING] = {},
1695 [SCIC_INITIALIZED] = {},
1696 [SCIC_STARTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001697 .exit_state = scic_sds_controller_starting_state_exit,
1698 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001699 [SCIC_READY] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001700 .enter_state = scic_sds_controller_ready_state_enter,
1701 .exit_state = scic_sds_controller_ready_state_exit,
1702 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001703 [SCIC_RESETTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001704 .enter_state = scic_sds_controller_resetting_state_enter,
1705 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001706 [SCIC_STOPPING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001707 .enter_state = scic_sds_controller_stopping_state_enter,
1708 .exit_state = scic_sds_controller_stopping_state_exit,
1709 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001710 [SCIC_STOPPED] = {},
1711 [SCIC_FAILED] = {}
Dan Williamscc9203b2011-05-08 17:34:44 -07001712};
1713
1714static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
1715{
1716 /* these defaults are overridden by the platform / firmware */
1717 struct isci_host *ihost = scic_to_ihost(scic);
1718 u16 index;
1719
1720 /* Default to APC mode. */
1721 scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1722
1723 /* Default to APC mode. */
1724 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
1725
1726 /* Default to no SSC operation. */
1727 scic->oem_parameters.sds1.controller.do_enable_ssc = false;
1728
1729 /* Initialize all of the port parameter information to narrow ports. */
1730 for (index = 0; index < SCI_MAX_PORTS; index++) {
1731 scic->oem_parameters.sds1.ports[index].phy_mask = 0;
1732 }
1733
1734 /* Initialize all of the phy parameter information. */
1735 for (index = 0; index < SCI_MAX_PHYS; index++) {
1736 /* Default to 6G (i.e. Gen 3) for now. */
1737 scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
1738
1739 /* the frequencies cannot be 0 */
1740 scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
1741 scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
1742 scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1743
1744 /*
1745 * Previous Vitesse based expanders had a arbitration issue that
1746 * is worked around by having the upper 32-bits of SAS address
1747 * with a value greater then the Vitesse company identifier.
1748 * Hence, usage of 0x5FCFFFFF. */
1749 scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
1750 scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
1751 }
1752
1753 scic->user_parameters.sds1.stp_inactivity_timeout = 5;
1754 scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
1755 scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
1756 scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
1757 scic->user_parameters.sds1.no_outbound_task_timeout = 20;
1758}
1759
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001760static void controller_timeout(unsigned long data)
1761{
1762 struct sci_timer *tmr = (struct sci_timer *)data;
1763 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), timer);
1764 struct isci_host *ihost = scic_to_ihost(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001765 struct sci_base_state_machine *sm = &scic->sm;
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001766 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001767
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001768 spin_lock_irqsave(&ihost->scic_lock, flags);
1769
1770 if (tmr->cancel)
1771 goto done;
1772
Edmund Nadolskie3013702011-06-02 00:10:43 +00001773 if (sm->current_state_id == SCIC_STARTING)
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001774 scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001775 else if (sm->current_state_id == SCIC_STOPPING) {
1776 sci_change_state(sm, SCIC_FAILED);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001777 isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
1778 } else /* / @todo Now what do we want to do in this case? */
1779 dev_err(scic_to_dev(scic),
1780 "%s: Controller timer fired when controller was not "
1781 "in a state being timed.\n",
1782 __func__);
1783
1784done:
1785 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1786}
Dan Williamscc9203b2011-05-08 17:34:44 -07001787
1788/**
1789 * scic_controller_construct() - This method will attempt to construct a
1790 * controller object utilizing the supplied parameter information.
1791 * @c: This parameter specifies the controller to be constructed.
1792 * @scu_base: mapped base address of the scu registers
1793 * @smu_base: mapped base address of the smu registers
1794 *
1795 * Indicate if the controller was successfully constructed or if it failed in
1796 * some way. SCI_SUCCESS This value is returned if the controller was
1797 * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
1798 * if the interrupt coalescence timer may cause SAS compliance issues for SMP
1799 * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
1800 * This value is returned if the controller does not support the supplied type.
1801 * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
1802 * controller does not support the supplied initialization data version.
1803 */
1804static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
1805 void __iomem *scu_base,
1806 void __iomem *smu_base)
1807{
1808 struct isci_host *ihost = scic_to_ihost(scic);
1809 u8 i;
1810
Edmund Nadolski12ef6542011-06-02 00:10:50 +00001811 sci_init_sm(&scic->sm, scic_sds_controller_state_table, SCIC_INITIAL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001812
1813 scic->scu_registers = scu_base;
1814 scic->smu_registers = smu_base;
1815
1816 scic_sds_port_configuration_agent_construct(&scic->port_agent);
1817
1818 /* Construct the ports for this controller */
1819 for (i = 0; i < SCI_MAX_PORTS; i++)
1820 scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
1821 scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
1822
1823 /* Construct the phys for this controller */
1824 for (i = 0; i < SCI_MAX_PHYS; i++) {
1825 /* Add all the PHYs to the dummy port */
1826 scic_sds_phy_construct(&ihost->phys[i].sci,
1827 &ihost->ports[SCI_MAX_PORTS].sci, i);
1828 }
1829
1830 scic->invalid_phy_mask = 0;
1831
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001832 sci_init_timer(&scic->timer, controller_timeout);
1833
Dan Williamscc9203b2011-05-08 17:34:44 -07001834 /* Initialize the User and OEM parameters to default values. */
1835 scic_sds_controller_set_default_config_parameters(scic);
1836
1837 return scic_controller_reset(scic);
1838}
1839
1840int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
1841{
1842 int i;
1843
1844 for (i = 0; i < SCI_MAX_PORTS; i++)
1845 if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1846 return -EINVAL;
1847
1848 for (i = 0; i < SCI_MAX_PHYS; i++)
1849 if (oem->phys[i].sas_address.high == 0 &&
1850 oem->phys[i].sas_address.low == 0)
1851 return -EINVAL;
1852
1853 if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1854 for (i = 0; i < SCI_MAX_PHYS; i++)
1855 if (oem->ports[i].phy_mask != 0)
1856 return -EINVAL;
1857 } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1858 u8 phy_mask = 0;
1859
1860 for (i = 0; i < SCI_MAX_PHYS; i++)
1861 phy_mask |= oem->ports[i].phy_mask;
1862
1863 if (phy_mask == 0)
1864 return -EINVAL;
1865 } else
1866 return -EINVAL;
1867
1868 if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1869 return -EINVAL;
1870
1871 return 0;
1872}
1873
1874static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
1875 union scic_oem_parameters *scic_parms)
1876{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001877 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07001878
Edmund Nadolskie3013702011-06-02 00:10:43 +00001879 if (state == SCIC_RESET ||
1880 state == SCIC_INITIALIZING ||
1881 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001882
1883 if (scic_oem_parameters_validate(&scic_parms->sds1))
1884 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1885 scic->oem_parameters.sds1 = scic_parms->sds1;
1886
1887 return SCI_SUCCESS;
1888 }
1889
1890 return SCI_FAILURE_INVALID_STATE;
1891}
1892
1893void scic_oem_parameters_get(
1894 struct scic_sds_controller *scic,
1895 union scic_oem_parameters *scic_parms)
1896{
1897 memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
1898}
1899
Edmund Nadolski04736612011-05-19 20:17:47 -07001900static void power_control_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001901{
Edmund Nadolski04736612011-05-19 20:17:47 -07001902 struct sci_timer *tmr = (struct sci_timer *)data;
1903 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), power_control.timer);
1904 struct isci_host *ihost = scic_to_ihost(scic);
1905 struct scic_sds_phy *sci_phy;
1906 unsigned long flags;
1907 u8 i;
Dan Williamscc9203b2011-05-08 17:34:44 -07001908
Edmund Nadolski04736612011-05-19 20:17:47 -07001909 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001910
Edmund Nadolski04736612011-05-19 20:17:47 -07001911 if (tmr->cancel)
1912 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001913
1914 scic->power_control.phys_granted_power = 0;
1915
1916 if (scic->power_control.phys_waiting == 0) {
1917 scic->power_control.timer_started = false;
Edmund Nadolski04736612011-05-19 20:17:47 -07001918 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001919 }
Edmund Nadolski04736612011-05-19 20:17:47 -07001920
1921 for (i = 0; i < SCI_MAX_PHYS; i++) {
1922
1923 if (scic->power_control.phys_waiting == 0)
1924 break;
1925
1926 sci_phy = scic->power_control.requesters[i];
1927 if (sci_phy == NULL)
1928 continue;
1929
1930 if (scic->power_control.phys_granted_power >=
1931 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up)
1932 break;
1933
1934 scic->power_control.requesters[i] = NULL;
1935 scic->power_control.phys_waiting--;
1936 scic->power_control.phys_granted_power++;
1937 scic_sds_phy_consume_power_handler(sci_phy);
1938 }
1939
1940 /*
1941 * It doesn't matter if the power list is empty, we need to start the
1942 * timer in case another phy becomes ready.
1943 */
1944 sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1945 scic->power_control.timer_started = true;
1946
1947done:
1948 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001949}
1950
1951/**
1952 * This method inserts the phy in the stagger spinup control queue.
1953 * @scic:
1954 *
1955 *
1956 */
1957void scic_sds_controller_power_control_queue_insert(
1958 struct scic_sds_controller *scic,
1959 struct scic_sds_phy *sci_phy)
1960{
1961 BUG_ON(sci_phy == NULL);
1962
1963 if (scic->power_control.phys_granted_power <
1964 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
1965 scic->power_control.phys_granted_power++;
1966 scic_sds_phy_consume_power_handler(sci_phy);
1967
1968 /*
1969 * stop and start the power_control timer. When the timer fires, the
1970 * no_of_phys_granted_power will be set to 0
1971 */
Edmund Nadolski04736612011-05-19 20:17:47 -07001972 if (scic->power_control.timer_started)
1973 sci_del_timer(&scic->power_control.timer);
1974
1975 sci_mod_timer(&scic->power_control.timer,
1976 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1977 scic->power_control.timer_started = true;
1978
Dan Williamscc9203b2011-05-08 17:34:44 -07001979 } else {
1980 /* Add the phy in the waiting list */
1981 scic->power_control.requesters[sci_phy->phy_index] = sci_phy;
1982 scic->power_control.phys_waiting++;
1983 }
1984}
1985
1986/**
1987 * This method removes the phy from the stagger spinup control queue.
1988 * @scic:
1989 *
1990 *
1991 */
1992void scic_sds_controller_power_control_queue_remove(
1993 struct scic_sds_controller *scic,
1994 struct scic_sds_phy *sci_phy)
1995{
1996 BUG_ON(sci_phy == NULL);
1997
1998 if (scic->power_control.requesters[sci_phy->phy_index] != NULL) {
1999 scic->power_control.phys_waiting--;
2000 }
2001
2002 scic->power_control.requesters[sci_phy->phy_index] = NULL;
2003}
2004
2005#define AFE_REGISTER_WRITE_DELAY 10
2006
2007/* Initialize the AFE for this phy index. We need to read the AFE setup from
2008 * the OEM parameters
2009 */
2010static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
2011{
2012 const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
2013 u32 afe_status;
2014 u32 phy_id;
2015
2016 /* Clear DFX Status registers */
2017 writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
2018 udelay(AFE_REGISTER_WRITE_DELAY);
2019
2020 if (is_b0()) {
2021 /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
2022 * Timer, PM Stagger Timer */
2023 writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
2024 udelay(AFE_REGISTER_WRITE_DELAY);
2025 }
2026
2027 /* Configure bias currents to normal */
2028 if (is_a0())
2029 writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
2030 else if (is_a2())
2031 writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002032 else if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002033 writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
2034
2035 udelay(AFE_REGISTER_WRITE_DELAY);
2036
2037 /* Enable PLL */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002038 if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002039 writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
2040 else
2041 writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
2042
2043 udelay(AFE_REGISTER_WRITE_DELAY);
2044
2045 /* Wait for the PLL to lock */
2046 do {
2047 afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
2048 udelay(AFE_REGISTER_WRITE_DELAY);
2049 } while ((afe_status & 0x00001000) == 0);
2050
2051 if (is_a0() || is_a2()) {
2052 /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
2053 writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
2054 udelay(AFE_REGISTER_WRITE_DELAY);
2055 }
2056
2057 for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
2058 const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2059
2060 if (is_b0()) {
2061 /* Configure transmitter SSC parameters */
2062 writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2063 udelay(AFE_REGISTER_WRITE_DELAY);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002064 } else if (is_c0()) {
2065 /* Configure transmitter SSC parameters */
2066 writel(0x0003000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2067 udelay(AFE_REGISTER_WRITE_DELAY);
2068
2069 /*
2070 * All defaults, except the Receive Word Alignament/Comma Detect
2071 * Enable....(0xe800) */
2072 writel(0x00004500, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2073 udelay(AFE_REGISTER_WRITE_DELAY);
Dan Williamscc9203b2011-05-08 17:34:44 -07002074 } else {
2075 /*
2076 * All defaults, except the Receive Word Alignament/Comma Detect
2077 * Enable....(0xe800) */
2078 writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2079 udelay(AFE_REGISTER_WRITE_DELAY);
2080
2081 writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
2082 udelay(AFE_REGISTER_WRITE_DELAY);
2083 }
2084
2085 /*
2086 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2087 * & increase TX int & ext bias 20%....(0xe85c) */
2088 if (is_a0())
2089 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2090 else if (is_a2())
2091 writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002092 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002093 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002094 writel(0x000003D7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002095 udelay(AFE_REGISTER_WRITE_DELAY);
2096
2097 /*
2098 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2099 * & increase TX int & ext bias 20%....(0xe85c) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002100 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2101 } else {
2102 writel(0x000001E7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2103 udelay(AFE_REGISTER_WRITE_DELAY);
2104
2105 /*
2106 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2107 * & increase TX int & ext bias 20%....(0xe85c) */
2108 writel(0x000001E4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002109 }
2110 udelay(AFE_REGISTER_WRITE_DELAY);
2111
2112 if (is_a0() || is_a2()) {
2113 /* Enable TX equalization (0xe824) */
2114 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2115 udelay(AFE_REGISTER_WRITE_DELAY);
2116 }
2117
2118 /*
2119 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
2120 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
2121 writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2122 udelay(AFE_REGISTER_WRITE_DELAY);
2123
2124 /* Leave DFE/FFE on */
2125 if (is_a0())
2126 writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2127 else if (is_a2())
2128 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002129 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002130 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2131 udelay(AFE_REGISTER_WRITE_DELAY);
2132 /* Enable TX equalization (0xe824) */
2133 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002134 } else {
2135 writel(0x0140DF0F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
2136 udelay(AFE_REGISTER_WRITE_DELAY);
2137
2138 writel(0x3F6F103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2139 udelay(AFE_REGISTER_WRITE_DELAY);
2140
2141 /* Enable TX equalization (0xe824) */
2142 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002143 }
Adam Gruchaladbb07432011-06-01 22:31:03 +00002144
Dan Williamscc9203b2011-05-08 17:34:44 -07002145 udelay(AFE_REGISTER_WRITE_DELAY);
2146
2147 writel(oem_phy->afe_tx_amp_control0,
2148 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
2149 udelay(AFE_REGISTER_WRITE_DELAY);
2150
2151 writel(oem_phy->afe_tx_amp_control1,
2152 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
2153 udelay(AFE_REGISTER_WRITE_DELAY);
2154
2155 writel(oem_phy->afe_tx_amp_control2,
2156 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
2157 udelay(AFE_REGISTER_WRITE_DELAY);
2158
2159 writel(oem_phy->afe_tx_amp_control3,
2160 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
2161 udelay(AFE_REGISTER_WRITE_DELAY);
2162 }
2163
2164 /* Transfer control to the PEs */
2165 writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
2166 udelay(AFE_REGISTER_WRITE_DELAY);
2167}
2168
Dan Williamscc9203b2011-05-08 17:34:44 -07002169static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
2170{
Edmund Nadolski04736612011-05-19 20:17:47 -07002171 sci_init_timer(&scic->power_control.timer, power_control_timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07002172
2173 memset(scic->power_control.requesters, 0,
2174 sizeof(scic->power_control.requesters));
2175
2176 scic->power_control.phys_waiting = 0;
2177 scic->power_control.phys_granted_power = 0;
2178}
2179
2180static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
2181{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002182 struct sci_base_state_machine *sm = &scic->sm;
Dan Williamscc9203b2011-05-08 17:34:44 -07002183 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002184 enum sci_status result = SCI_FAILURE;
2185 unsigned long i, state, val;
Dan Williamscc9203b2011-05-08 17:34:44 -07002186
Edmund Nadolskie3013702011-06-02 00:10:43 +00002187 if (scic->sm.current_state_id != SCIC_RESET) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002188 dev_warn(scic_to_dev(scic),
2189 "SCIC Controller initialize operation requested "
2190 "in invalid state\n");
2191 return SCI_FAILURE_INVALID_STATE;
2192 }
2193
Edmund Nadolskie3013702011-06-02 00:10:43 +00002194 sci_change_state(sm, SCIC_INITIALIZING);
Dan Williamscc9203b2011-05-08 17:34:44 -07002195
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002196 sci_init_timer(&scic->phy_timer, phy_startup_timeout);
2197
2198 scic->next_phy_to_start = 0;
2199 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002200
2201 scic_sds_controller_initialize_power_control(scic);
2202
2203 /*
2204 * There is nothing to do here for B0 since we do not have to
2205 * program the AFE registers.
2206 * / @todo The AFE settings are supposed to be correct for the B0 but
2207 * / presently they seem to be wrong. */
2208 scic_sds_controller_afe_initialization(scic);
2209
Dan Williams7c78da32011-06-01 16:00:01 -07002210
2211 /* Take the hardware out of reset */
2212 writel(0, &scic->smu_registers->soft_reset_control);
2213
2214 /*
2215 * / @todo Provide meaningfull error code for hardware failure
2216 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2217 for (i = 100; i >= 1; i--) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002218 u32 status;
Dan Williamscc9203b2011-05-08 17:34:44 -07002219
Dan Williams7c78da32011-06-01 16:00:01 -07002220 /* Loop until the hardware reports success */
2221 udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2222 status = readl(&scic->smu_registers->control_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07002223
Dan Williams7c78da32011-06-01 16:00:01 -07002224 if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
2225 break;
Dan Williamscc9203b2011-05-08 17:34:44 -07002226 }
Dan Williams7c78da32011-06-01 16:00:01 -07002227 if (i == 0)
2228 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002229
Dan Williams7c78da32011-06-01 16:00:01 -07002230 /*
2231 * Determine what are the actaul device capacities that the
2232 * hardware will support */
2233 val = readl(&scic->smu_registers->device_context_capacity);
Dan Williamscc9203b2011-05-08 17:34:44 -07002234
Dan Williams7c78da32011-06-01 16:00:01 -07002235 /* Record the smaller of the two capacity values */
2236 scic->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2237 scic->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2238 scic->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
Dan Williamscc9203b2011-05-08 17:34:44 -07002239
Dan Williams7c78da32011-06-01 16:00:01 -07002240 /*
2241 * Make all PEs that are unassigned match up with the
2242 * logical ports
2243 */
2244 for (i = 0; i < scic->logical_port_entries; i++) {
2245 struct scu_port_task_scheduler_group_registers __iomem
2246 *ptsg = &scic->scu_registers->peg0.ptsg;
Dan Williamscc9203b2011-05-08 17:34:44 -07002247
Dan Williams7c78da32011-06-01 16:00:01 -07002248 writel(i, &ptsg->protocol_engine[i]);
Dan Williamscc9203b2011-05-08 17:34:44 -07002249 }
2250
2251 /* Initialize hardware PCI Relaxed ordering in DMA engines */
Dan Williams7c78da32011-06-01 16:00:01 -07002252 val = readl(&scic->scu_registers->sdma.pdma_configuration);
2253 val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2254 writel(val, &scic->scu_registers->sdma.pdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002255
Dan Williams7c78da32011-06-01 16:00:01 -07002256 val = readl(&scic->scu_registers->sdma.cdma_configuration);
2257 val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2258 writel(val, &scic->scu_registers->sdma.cdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002259
2260 /*
2261 * Initialize the PHYs before the PORTs because the PHY registers
2262 * are accessed during the port initialization.
2263 */
Dan Williams7c78da32011-06-01 16:00:01 -07002264 for (i = 0; i < SCI_MAX_PHYS; i++) {
2265 result = scic_sds_phy_initialize(&ihost->phys[i].sci,
2266 &scic->scu_registers->peg0.pe[i].tl,
2267 &scic->scu_registers->peg0.pe[i].ll);
2268 if (result != SCI_SUCCESS)
2269 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002270 }
2271
Dan Williams7c78da32011-06-01 16:00:01 -07002272 for (i = 0; i < scic->logical_port_entries; i++) {
2273 result = scic_sds_port_initialize(&ihost->ports[i].sci,
2274 &scic->scu_registers->peg0.ptsg.port[i],
2275 &scic->scu_registers->peg0.ptsg.protocol_engine,
2276 &scic->scu_registers->peg0.viit[i]);
2277
2278 if (result != SCI_SUCCESS)
2279 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002280 }
2281
Dan Williams7c78da32011-06-01 16:00:01 -07002282 result = scic_sds_port_configuration_agent_initialize(scic, &scic->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07002283
Dan Williams7c78da32011-06-01 16:00:01 -07002284 out:
Dan Williamscc9203b2011-05-08 17:34:44 -07002285 /* Advance the controller state machine */
2286 if (result == SCI_SUCCESS)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002287 state = SCIC_INITIALIZED;
Dan Williamscc9203b2011-05-08 17:34:44 -07002288 else
Edmund Nadolskie3013702011-06-02 00:10:43 +00002289 state = SCIC_FAILED;
2290 sci_change_state(sm, state);
Dan Williamscc9203b2011-05-08 17:34:44 -07002291
2292 return result;
2293}
2294
2295static enum sci_status scic_user_parameters_set(
2296 struct scic_sds_controller *scic,
2297 union scic_user_parameters *scic_parms)
2298{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002299 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07002300
Edmund Nadolskie3013702011-06-02 00:10:43 +00002301 if (state == SCIC_RESET ||
2302 state == SCIC_INITIALIZING ||
2303 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002304 u16 index;
2305
2306 /*
2307 * Validate the user parameters. If they are not legal, then
2308 * return a failure.
2309 */
2310 for (index = 0; index < SCI_MAX_PHYS; index++) {
2311 struct sci_phy_user_params *user_phy;
2312
2313 user_phy = &scic_parms->sds1.phys[index];
2314
2315 if (!((user_phy->max_speed_generation <=
2316 SCIC_SDS_PARM_MAX_SPEED) &&
2317 (user_phy->max_speed_generation >
2318 SCIC_SDS_PARM_NO_SPEED)))
2319 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2320
2321 if (user_phy->in_connection_align_insertion_frequency <
2322 3)
2323 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2324
2325 if ((user_phy->in_connection_align_insertion_frequency <
2326 3) ||
2327 (user_phy->align_insertion_frequency == 0) ||
2328 (user_phy->
2329 notify_enable_spin_up_insertion_frequency ==
2330 0))
2331 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2332 }
2333
2334 if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
2335 (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
2336 (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
2337 (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
2338 (scic_parms->sds1.no_outbound_task_timeout == 0))
2339 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2340
2341 memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
2342
2343 return SCI_SUCCESS;
2344 }
2345
2346 return SCI_FAILURE_INVALID_STATE;
2347}
2348
2349static int scic_controller_mem_init(struct scic_sds_controller *scic)
2350{
2351 struct device *dev = scic_to_dev(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002352 dma_addr_t dma;
2353 size_t size;
2354 int err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002355
Dan Williams7c78da32011-06-01 16:00:01 -07002356 size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2357 scic->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002358 if (!scic->completion_queue)
2359 return -ENOMEM;
2360
Dan Williams7c78da32011-06-01 16:00:01 -07002361 writel(lower_32_bits(dma), &scic->smu_registers->completion_queue_lower);
2362 writel(upper_32_bits(dma), &scic->smu_registers->completion_queue_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002363
Dan Williams7c78da32011-06-01 16:00:01 -07002364 size = scic->remote_node_entries * sizeof(union scu_remote_node_context);
2365 scic->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
2366 GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002367 if (!scic->remote_node_context_table)
2368 return -ENOMEM;
2369
Dan Williams7c78da32011-06-01 16:00:01 -07002370 writel(lower_32_bits(dma), &scic->smu_registers->remote_node_context_lower);
2371 writel(upper_32_bits(dma), &scic->smu_registers->remote_node_context_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002372
Dan Williams7c78da32011-06-01 16:00:01 -07002373 size = scic->task_context_entries * sizeof(struct scu_task_context),
2374 scic->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002375 if (!scic->task_context_table)
2376 return -ENOMEM;
2377
Dan Williams7c78da32011-06-01 16:00:01 -07002378 writel(lower_32_bits(dma), &scic->smu_registers->host_task_table_lower);
2379 writel(upper_32_bits(dma), &scic->smu_registers->host_task_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002380
Dan Williams7c78da32011-06-01 16:00:01 -07002381 err = scic_sds_unsolicited_frame_control_construct(scic);
2382 if (err)
2383 return err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002384
2385 /*
2386 * Inform the silicon as to the location of the UF headers and
2387 * address table.
2388 */
2389 writel(lower_32_bits(scic->uf_control.headers.physical_address),
2390 &scic->scu_registers->sdma.uf_header_base_address_lower);
2391 writel(upper_32_bits(scic->uf_control.headers.physical_address),
2392 &scic->scu_registers->sdma.uf_header_base_address_upper);
2393
2394 writel(lower_32_bits(scic->uf_control.address_table.physical_address),
2395 &scic->scu_registers->sdma.uf_address_table_lower);
2396 writel(upper_32_bits(scic->uf_control.address_table.physical_address),
2397 &scic->scu_registers->sdma.uf_address_table_upper);
2398
2399 return 0;
2400}
2401
Dan Williams6f231dd2011-07-02 22:56:22 -07002402int isci_host_init(struct isci_host *isci_host)
2403{
Dan Williamsd9c37392011-03-03 17:59:32 -08002404 int err = 0, i;
Dan Williams6f231dd2011-07-02 22:56:22 -07002405 enum sci_status status;
Dan Williams4711ba12011-03-11 10:43:57 -08002406 union scic_oem_parameters oem;
Dan Williams6f231dd2011-07-02 22:56:22 -07002407 union scic_user_parameters scic_user_params;
Dan Williamsd044af12011-03-08 09:52:49 -08002408 struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002409
Dan Williams6f231dd2011-07-02 22:56:22 -07002410 spin_lock_init(&isci_host->state_lock);
2411 spin_lock_init(&isci_host->scic_lock);
2412 spin_lock_init(&isci_host->queue_lock);
Dan Williams0cf89d12011-02-18 09:25:07 -08002413 init_waitqueue_head(&isci_host->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07002414
2415 isci_host_change_state(isci_host, isci_starting);
2416 isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
2417
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002418 status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
Dan Williams6f231dd2011-07-02 22:56:22 -07002419 smu_base(isci_host));
2420
2421 if (status != SCI_SUCCESS) {
2422 dev_err(&isci_host->pdev->dev,
2423 "%s: scic_controller_construct failed - status = %x\n",
2424 __func__,
2425 status);
Dave Jiang858d4aa2011-02-22 01:27:03 -08002426 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002427 }
2428
2429 isci_host->sas_ha.dev = &isci_host->pdev->dev;
2430 isci_host->sas_ha.lldd_ha = isci_host;
2431
Dan Williamsd044af12011-03-08 09:52:49 -08002432 /*
2433 * grab initial values stored in the controller object for OEM and USER
2434 * parameters
2435 */
Dave Jiangb5f18a22011-03-16 14:57:23 -07002436 isci_user_parameters_get(isci_host, &scic_user_params);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002437 status = scic_user_parameters_set(&isci_host->sci,
Dan Williamsd044af12011-03-08 09:52:49 -08002438 &scic_user_params);
2439 if (status != SCI_SUCCESS) {
2440 dev_warn(&isci_host->pdev->dev,
2441 "%s: scic_user_parameters_set failed\n",
2442 __func__);
2443 return -ENODEV;
2444 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002445
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002446 scic_oem_parameters_get(&isci_host->sci, &oem);
Dan Williamsd044af12011-03-08 09:52:49 -08002447
2448 /* grab any OEM parameters specified in orom */
2449 if (pci_info->orom) {
Dan Williams4711ba12011-03-11 10:43:57 -08002450 status = isci_parse_oem_parameters(&oem,
Dan Williamsd044af12011-03-08 09:52:49 -08002451 pci_info->orom,
2452 isci_host->id);
Dan Williams6f231dd2011-07-02 22:56:22 -07002453 if (status != SCI_SUCCESS) {
2454 dev_warn(&isci_host->pdev->dev,
2455 "parsing firmware oem parameters failed\n");
Dave Jiang858d4aa2011-02-22 01:27:03 -08002456 return -EINVAL;
Dan Williams6f231dd2011-07-02 22:56:22 -07002457 }
Dan Williams4711ba12011-03-11 10:43:57 -08002458 }
2459
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002460 status = scic_oem_parameters_set(&isci_host->sci, &oem);
Dan Williams4711ba12011-03-11 10:43:57 -08002461 if (status != SCI_SUCCESS) {
2462 dev_warn(&isci_host->pdev->dev,
2463 "%s: scic_oem_parameters_set failed\n",
2464 __func__);
2465 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002466 }
2467
Dan Williams6f231dd2011-07-02 22:56:22 -07002468 tasklet_init(&isci_host->completion_tasklet,
Dan Williamsc7ef4032011-02-18 09:25:05 -08002469 isci_host_completion_routine, (unsigned long)isci_host);
Dan Williams6f231dd2011-07-02 22:56:22 -07002470
Dan Williams6f231dd2011-07-02 22:56:22 -07002471 INIT_LIST_HEAD(&isci_host->requests_to_complete);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002472 INIT_LIST_HEAD(&isci_host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002473
Dan Williams7c40a802011-03-02 11:49:26 -08002474 spin_lock_irq(&isci_host->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002475 status = scic_controller_initialize(&isci_host->sci);
Dan Williams7c40a802011-03-02 11:49:26 -08002476 spin_unlock_irq(&isci_host->scic_lock);
2477 if (status != SCI_SUCCESS) {
2478 dev_warn(&isci_host->pdev->dev,
2479 "%s: scic_controller_initialize failed -"
2480 " status = 0x%x\n",
2481 __func__, status);
2482 return -ENODEV;
2483 }
2484
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002485 err = scic_controller_mem_init(&isci_host->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07002486 if (err)
Dave Jiang858d4aa2011-02-22 01:27:03 -08002487 return err;
Dan Williams6f231dd2011-07-02 22:56:22 -07002488
Dan Williams6f231dd2011-07-02 22:56:22 -07002489 isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
Dan Williams67ea8382011-05-08 11:47:15 -07002490 sizeof(struct isci_request),
Dan Williams6f231dd2011-07-02 22:56:22 -07002491 SLAB_HWCACHE_ALIGN, 0);
2492
Dave Jiang858d4aa2011-02-22 01:27:03 -08002493 if (!isci_host->dma_pool)
2494 return -ENOMEM;
Dan Williams6f231dd2011-07-02 22:56:22 -07002495
Dan Williamsd9c37392011-03-03 17:59:32 -08002496 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williamse5313812011-05-07 10:11:43 -07002497 isci_port_init(&isci_host->ports[i], isci_host, i);
Dan Williams6f231dd2011-07-02 22:56:22 -07002498
Dan Williamsd9c37392011-03-03 17:59:32 -08002499 for (i = 0; i < SCI_MAX_PHYS; i++)
2500 isci_phy_init(&isci_host->phys[i], isci_host, i);
2501
2502 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07002503 struct isci_remote_device *idev = &isci_host->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08002504
2505 INIT_LIST_HEAD(&idev->reqs_in_process);
2506 INIT_LIST_HEAD(&idev->node);
2507 spin_lock_init(&idev->state_lock);
2508 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002509
Dave Jiang858d4aa2011-02-22 01:27:03 -08002510 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -07002511}
Dan Williamscc9203b2011-05-08 17:34:44 -07002512
2513void scic_sds_controller_link_up(struct scic_sds_controller *scic,
2514 struct scic_sds_port *port, struct scic_sds_phy *phy)
2515{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002516 switch (scic->sm.current_state_id) {
2517 case SCIC_STARTING:
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002518 sci_del_timer(&scic->phy_timer);
2519 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002520 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2521 port, phy);
2522 scic_sds_controller_start_next_phy(scic);
2523 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002524 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002525 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2526 port, phy);
2527 break;
2528 default:
2529 dev_dbg(scic_to_dev(scic),
2530 "%s: SCIC Controller linkup event from phy %d in "
2531 "unexpected state %d\n", __func__, phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002532 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002533 }
2534}
2535
2536void scic_sds_controller_link_down(struct scic_sds_controller *scic,
2537 struct scic_sds_port *port, struct scic_sds_phy *phy)
2538{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002539 switch (scic->sm.current_state_id) {
2540 case SCIC_STARTING:
2541 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002542 scic->port_agent.link_down_handler(scic, &scic->port_agent,
2543 port, phy);
2544 break;
2545 default:
2546 dev_dbg(scic_to_dev(scic),
2547 "%s: SCIC Controller linkdown event from phy %d in "
2548 "unexpected state %d\n",
2549 __func__,
2550 phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002551 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002552 }
2553}
2554
2555/**
2556 * This is a helper method to determine if any remote devices on this
2557 * controller are still in the stopping state.
2558 *
2559 */
2560static bool scic_sds_controller_has_remote_devices_stopping(
2561 struct scic_sds_controller *controller)
2562{
2563 u32 index;
2564
2565 for (index = 0; index < controller->remote_node_entries; index++) {
2566 if ((controller->device_table[index] != NULL) &&
Edmund Nadolskie3013702011-06-02 00:10:43 +00002567 (controller->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
Dan Williamscc9203b2011-05-08 17:34:44 -07002568 return true;
2569 }
2570
2571 return false;
2572}
2573
2574/**
2575 * This method is called by the remote device to inform the controller
2576 * object that the remote device has stopped.
2577 */
2578void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
2579 struct scic_sds_remote_device *sci_dev)
2580{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002581 if (scic->sm.current_state_id != SCIC_STOPPING) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002582 dev_dbg(scic_to_dev(scic),
2583 "SCIC Controller 0x%p remote device stopped event "
2584 "from device 0x%p in unexpected state %d\n",
2585 scic, sci_dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002586 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002587 return;
2588 }
2589
2590 if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002591 sci_change_state(&scic->sm, SCIC_STOPPED);
Dan Williamscc9203b2011-05-08 17:34:44 -07002592 }
2593}
2594
2595/**
2596 * This method will write to the SCU PCP register the request value. The method
2597 * is used to suspend/resume ports, devices, and phys.
2598 * @scic:
2599 *
2600 *
2601 */
2602void scic_sds_controller_post_request(
2603 struct scic_sds_controller *scic,
2604 u32 request)
2605{
2606 dev_dbg(scic_to_dev(scic),
2607 "%s: SCIC Controller 0x%p post request 0x%08x\n",
2608 __func__,
2609 scic,
2610 request);
2611
2612 writel(request, &scic->smu_registers->post_context_port);
2613}
2614
2615/**
2616 * This method will copy the soft copy of the task context into the physical
2617 * memory accessible by the controller.
2618 * @scic: This parameter specifies the controller for which to copy
2619 * the task context.
2620 * @sci_req: This parameter specifies the request for which the task
2621 * context is being copied.
2622 *
2623 * After this call is made the SCIC_SDS_IO_REQUEST object will always point to
2624 * the physical memory version of the task context. Thus, all subsequent
2625 * updates to the task context are performed in the TC table (i.e. DMAable
2626 * memory). none
2627 */
2628void scic_sds_controller_copy_task_context(
2629 struct scic_sds_controller *scic,
2630 struct scic_sds_request *sci_req)
2631{
2632 struct scu_task_context *task_context_buffer;
2633
2634 task_context_buffer = scic_sds_controller_get_task_context_buffer(
2635 scic, sci_req->io_tag);
2636
2637 memcpy(task_context_buffer,
2638 sci_req->task_context_buffer,
2639 offsetof(struct scu_task_context, sgl_snapshot_ac));
2640
2641 /*
2642 * Now that the soft copy of the TC has been copied into the TC
2643 * table accessible by the silicon. Thus, any further changes to
2644 * the TC (e.g. TC termination) occur in the appropriate location. */
2645 sci_req->task_context_buffer = task_context_buffer;
2646}
2647
Dan Williamsdd047c82011-06-09 11:06:58 -07002648struct scu_task_context *scic_sds_controller_get_task_context_buffer(struct scic_sds_controller *scic,
2649 u16 io_tag)
2650{
2651 u16 tci = ISCI_TAG_TCI(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002652
Dan Williamsdd047c82011-06-09 11:06:58 -07002653 if (tci < scic->task_context_entries) {
2654 return &scic->task_context_table[tci];
Dan Williamscc9203b2011-05-08 17:34:44 -07002655 }
2656
2657 return NULL;
2658}
2659
Dan Williamsdd047c82011-06-09 11:06:58 -07002660struct scic_sds_request *scic_request_by_tag(struct scic_sds_controller *scic, u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07002661{
2662 u16 task_index;
2663 u16 task_sequence;
2664
Dan Williamsdd047c82011-06-09 11:06:58 -07002665 task_index = ISCI_TAG_TCI(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002666
Dan Williamsdd047c82011-06-09 11:06:58 -07002667 if (task_index < scic->task_context_entries) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002668 if (scic->io_request_table[task_index] != NULL) {
Dan Williamsdd047c82011-06-09 11:06:58 -07002669 task_sequence = ISCI_TAG_SEQ(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002670
2671 if (task_sequence == scic->io_request_sequence[task_index]) {
2672 return scic->io_request_table[task_index];
2673 }
2674 }
2675 }
2676
2677 return NULL;
2678}
2679
2680/**
2681 * This method allocates remote node index and the reserves the remote node
2682 * context space for use. This method can fail if there are no more remote
2683 * node index available.
2684 * @scic: This is the controller object which contains the set of
2685 * free remote node ids
2686 * @sci_dev: This is the device object which is requesting the a remote node
2687 * id
2688 * @node_id: This is the remote node id that is assinged to the device if one
2689 * is available
2690 *
2691 * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2692 * node index available.
2693 */
2694enum sci_status scic_sds_controller_allocate_remote_node_context(
2695 struct scic_sds_controller *scic,
2696 struct scic_sds_remote_device *sci_dev,
2697 u16 *node_id)
2698{
2699 u16 node_index;
2700 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2701
2702 node_index = scic_sds_remote_node_table_allocate_remote_node(
2703 &scic->available_remote_nodes, remote_node_count
2704 );
2705
2706 if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2707 scic->device_table[node_index] = sci_dev;
2708
2709 *node_id = node_index;
2710
2711 return SCI_SUCCESS;
2712 }
2713
2714 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2715}
2716
2717/**
2718 * This method frees the remote node index back to the available pool. Once
2719 * this is done the remote node context buffer is no longer valid and can
2720 * not be used.
2721 * @scic:
2722 * @sci_dev:
2723 * @node_id:
2724 *
2725 */
2726void scic_sds_controller_free_remote_node_context(
2727 struct scic_sds_controller *scic,
2728 struct scic_sds_remote_device *sci_dev,
2729 u16 node_id)
2730{
2731 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2732
2733 if (scic->device_table[node_id] == sci_dev) {
2734 scic->device_table[node_id] = NULL;
2735
2736 scic_sds_remote_node_table_release_remote_node_index(
2737 &scic->available_remote_nodes, remote_node_count, node_id
2738 );
2739 }
2740}
2741
2742/**
2743 * This method returns the union scu_remote_node_context for the specified remote
2744 * node id.
2745 * @scic:
2746 * @node_id:
2747 *
2748 * union scu_remote_node_context*
2749 */
2750union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
2751 struct scic_sds_controller *scic,
2752 u16 node_id
2753 ) {
2754 if (
2755 (node_id < scic->remote_node_entries)
2756 && (scic->device_table[node_id] != NULL)
2757 ) {
2758 return &scic->remote_node_context_table[node_id];
2759 }
2760
2761 return NULL;
2762}
2763
2764/**
2765 *
2766 * @resposne_buffer: This is the buffer into which the D2H register FIS will be
2767 * constructed.
2768 * @frame_header: This is the frame header returned by the hardware.
2769 * @frame_buffer: This is the frame buffer returned by the hardware.
2770 *
2771 * This method will combind the frame header and frame buffer to create a SATA
2772 * D2H register FIS none
2773 */
2774void scic_sds_controller_copy_sata_response(
2775 void *response_buffer,
2776 void *frame_header,
2777 void *frame_buffer)
2778{
2779 memcpy(response_buffer, frame_header, sizeof(u32));
2780
2781 memcpy(response_buffer + sizeof(u32),
2782 frame_buffer,
2783 sizeof(struct dev_to_host_fis) - sizeof(u32));
2784}
2785
2786/**
2787 * This method releases the frame once this is done the frame is available for
2788 * re-use by the hardware. The data contained in the frame header and frame
2789 * buffer is no longer valid. The UF queue get pointer is only updated if UF
2790 * control indicates this is appropriate.
2791 * @scic:
2792 * @frame_index:
2793 *
2794 */
2795void scic_sds_controller_release_frame(
2796 struct scic_sds_controller *scic,
2797 u32 frame_index)
2798{
2799 if (scic_sds_unsolicited_frame_control_release_frame(
2800 &scic->uf_control, frame_index) == true)
2801 writel(scic->uf_control.get,
2802 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
2803}
2804
2805/**
2806 * scic_controller_start_io() - This method is called by the SCI user to
2807 * send/start an IO request. If the method invocation is successful, then
2808 * the IO request has been queued to the hardware for processing.
2809 * @controller: the handle to the controller object for which to start an IO
2810 * request.
2811 * @remote_device: the handle to the remote device object for which to start an
2812 * IO request.
2813 * @io_request: the handle to the io request object to start.
2814 * @io_tag: This parameter specifies a previously allocated IO tag that the
2815 * user desires to be utilized for this request. This parameter is optional.
2816 * The user is allowed to supply SCI_CONTROLLER_INVALID_IO_TAG as the value
2817 * for this parameter.
2818 *
2819 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2820 * to ensure that each of the methods that may allocate or free available IO
2821 * tags are handled in a mutually exclusive manner. This method is one of said
2822 * methods requiring proper critical code section protection (e.g. semaphore,
2823 * spin-lock, etc.). - For SATA, the user is required to manage NCQ tags. As a
2824 * result, it is expected the user will have set the NCQ tag field in the host
2825 * to device register FIS prior to calling this method. There is also a
2826 * requirement for the user to call scic_stp_io_set_ncq_tag() prior to invoking
2827 * the scic_controller_start_io() method. scic_controller_allocate_tag() for
2828 * more information on allocating a tag. Indicate if the controller
2829 * successfully started the IO request. SCI_SUCCESS if the IO request was
2830 * successfully started. Determine the failure situations and return values.
2831 */
Dan Williamsdd047c82011-06-09 11:06:58 -07002832enum sci_status scic_controller_start_io(struct scic_sds_controller *scic,
2833 struct scic_sds_remote_device *rdev,
2834 struct scic_sds_request *req,
2835 u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07002836{
2837 enum sci_status status;
2838
Edmund Nadolskie3013702011-06-02 00:10:43 +00002839 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002840 dev_warn(scic_to_dev(scic), "invalid state to start I/O");
2841 return SCI_FAILURE_INVALID_STATE;
2842 }
2843
2844 status = scic_sds_remote_device_start_io(scic, rdev, req);
2845 if (status != SCI_SUCCESS)
2846 return status;
2847
Dan Williamsdd047c82011-06-09 11:06:58 -07002848 scic->io_request_table[ISCI_TAG_TCI(req->io_tag)] = req;
Dan Williamscc9203b2011-05-08 17:34:44 -07002849 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(req));
2850 return SCI_SUCCESS;
2851}
2852
2853/**
2854 * scic_controller_terminate_request() - This method is called by the SCI Core
2855 * user to terminate an ongoing (i.e. started) core IO request. This does
2856 * not abort the IO request at the target, but rather removes the IO request
2857 * from the host controller.
2858 * @controller: the handle to the controller object for which to terminate a
2859 * request.
2860 * @remote_device: the handle to the remote device object for which to
2861 * terminate a request.
2862 * @request: the handle to the io or task management request object to
2863 * terminate.
2864 *
2865 * Indicate if the controller successfully began the terminate process for the
2866 * IO request. SCI_SUCCESS if the terminate process was successfully started
2867 * for the request. Determine the failure situations and return values.
2868 */
2869enum sci_status scic_controller_terminate_request(
2870 struct scic_sds_controller *scic,
2871 struct scic_sds_remote_device *rdev,
2872 struct scic_sds_request *req)
2873{
2874 enum sci_status status;
2875
Edmund Nadolskie3013702011-06-02 00:10:43 +00002876 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002877 dev_warn(scic_to_dev(scic),
2878 "invalid state to terminate request\n");
2879 return SCI_FAILURE_INVALID_STATE;
2880 }
2881
2882 status = scic_sds_io_request_terminate(req);
2883 if (status != SCI_SUCCESS)
2884 return status;
2885
2886 /*
2887 * Utilize the original post context command and or in the POST_TC_ABORT
2888 * request sub-type.
2889 */
2890 scic_sds_controller_post_request(scic,
2891 scic_sds_request_get_post_context(req) |
2892 SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2893 return SCI_SUCCESS;
2894}
2895
2896/**
2897 * scic_controller_complete_io() - This method will perform core specific
2898 * completion operations for an IO request. After this method is invoked,
2899 * the user should consider the IO request as invalid until it is properly
2900 * reused (i.e. re-constructed).
2901 * @controller: The handle to the controller object for which to complete the
2902 * IO request.
2903 * @remote_device: The handle to the remote device object for which to complete
2904 * the IO request.
2905 * @io_request: the handle to the io request object to complete.
2906 *
2907 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2908 * to ensure that each of the methods that may allocate or free available IO
2909 * tags are handled in a mutually exclusive manner. This method is one of said
2910 * methods requiring proper critical code section protection (e.g. semaphore,
2911 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
2912 * Core user, using the scic_controller_allocate_io_tag() method, then it is
2913 * the responsibility of the caller to invoke the scic_controller_free_io_tag()
2914 * method to free the tag (i.e. this method will not free the IO tag). Indicate
2915 * if the controller successfully completed the IO request. SCI_SUCCESS if the
2916 * completion process was successful.
2917 */
2918enum sci_status scic_controller_complete_io(
2919 struct scic_sds_controller *scic,
2920 struct scic_sds_remote_device *rdev,
2921 struct scic_sds_request *request)
2922{
2923 enum sci_status status;
2924 u16 index;
2925
Edmund Nadolskie3013702011-06-02 00:10:43 +00002926 switch (scic->sm.current_state_id) {
2927 case SCIC_STOPPING:
Dan Williamscc9203b2011-05-08 17:34:44 -07002928 /* XXX: Implement this function */
2929 return SCI_FAILURE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002930 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002931 status = scic_sds_remote_device_complete_io(scic, rdev, request);
2932 if (status != SCI_SUCCESS)
2933 return status;
2934
Dan Williamsdd047c82011-06-09 11:06:58 -07002935 index = ISCI_TAG_TCI(request->io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002936 scic->io_request_table[index] = NULL;
2937 return SCI_SUCCESS;
2938 default:
2939 dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
2940 return SCI_FAILURE_INVALID_STATE;
2941 }
2942
2943}
2944
2945enum sci_status scic_controller_continue_io(struct scic_sds_request *sci_req)
2946{
2947 struct scic_sds_controller *scic = sci_req->owning_controller;
2948
Edmund Nadolskie3013702011-06-02 00:10:43 +00002949 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002950 dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
2951 return SCI_FAILURE_INVALID_STATE;
2952 }
2953
Dan Williamsdd047c82011-06-09 11:06:58 -07002954 scic->io_request_table[ISCI_TAG_TCI(sci_req->io_tag)] = sci_req;
Dan Williamscc9203b2011-05-08 17:34:44 -07002955 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(sci_req));
2956 return SCI_SUCCESS;
2957}
2958
2959/**
2960 * scic_controller_start_task() - This method is called by the SCIC user to
2961 * send/start a framework task management request.
2962 * @controller: the handle to the controller object for which to start the task
2963 * management request.
2964 * @remote_device: the handle to the remote device object for which to start
2965 * the task management request.
2966 * @task_request: the handle to the task request object to start.
2967 * @io_tag: This parameter specifies a previously allocated IO tag that the
2968 * user desires to be utilized for this request. Note this not the io_tag
2969 * of the request being managed. It is to be utilized for the task request
2970 * itself. This parameter is optional. The user is allowed to supply
2971 * SCI_CONTROLLER_INVALID_IO_TAG as the value for this parameter.
2972 *
2973 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2974 * to ensure that each of the methods that may allocate or free available IO
2975 * tags are handled in a mutually exclusive manner. This method is one of said
2976 * methods requiring proper critical code section protection (e.g. semaphore,
2977 * spin-lock, etc.). - The user must synchronize this task with completion
2978 * queue processing. If they are not synchronized then it is possible for the
2979 * io requests that are being managed by the task request can complete before
2980 * starting the task request. scic_controller_allocate_tag() for more
2981 * information on allocating a tag. Indicate if the controller successfully
2982 * started the IO request. SCI_TASK_SUCCESS if the task request was
2983 * successfully started. SCI_TASK_FAILURE_REQUIRES_SCSI_ABORT This value is
2984 * returned if there is/are task(s) outstanding that require termination or
2985 * completion before this request can succeed.
2986 */
2987enum sci_task_status scic_controller_start_task(
2988 struct scic_sds_controller *scic,
2989 struct scic_sds_remote_device *rdev,
2990 struct scic_sds_request *req,
2991 u16 task_tag)
2992{
2993 enum sci_status status;
2994
Edmund Nadolskie3013702011-06-02 00:10:43 +00002995 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002996 dev_warn(scic_to_dev(scic),
2997 "%s: SCIC Controller starting task from invalid "
2998 "state\n",
2999 __func__);
3000 return SCI_TASK_FAILURE_INVALID_STATE;
3001 }
3002
3003 status = scic_sds_remote_device_start_task(scic, rdev, req);
3004 switch (status) {
3005 case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
Dan Williamsdd047c82011-06-09 11:06:58 -07003006 scic->io_request_table[ISCI_TAG_TCI(req->io_tag)] = req;
Dan Williamscc9203b2011-05-08 17:34:44 -07003007
3008 /*
3009 * We will let framework know this task request started successfully,
3010 * although core is still woring on starting the request (to post tc when
3011 * RNC is resumed.)
3012 */
3013 return SCI_SUCCESS;
3014 case SCI_SUCCESS:
Dan Williamsdd047c82011-06-09 11:06:58 -07003015 scic->io_request_table[ISCI_TAG_TCI(req->io_tag)] = req;
Dan Williamscc9203b2011-05-08 17:34:44 -07003016
3017 scic_sds_controller_post_request(scic,
3018 scic_sds_request_get_post_context(req));
3019 break;
3020 default:
3021 break;
3022 }
3023
3024 return status;
3025}
3026
3027/**
3028 * scic_controller_allocate_io_tag() - This method will allocate a tag from the
3029 * pool of free IO tags. Direct allocation of IO tags by the SCI Core user
3030 * is optional. The scic_controller_start_io() method will allocate an IO
3031 * tag if this method is not utilized and the tag is not supplied to the IO
3032 * construct routine. Direct allocation of IO tags may provide additional
3033 * performance improvements in environments capable of supporting this usage
3034 * model. Additionally, direct allocation of IO tags also provides
3035 * additional flexibility to the SCI Core user. Specifically, the user may
3036 * retain IO tags across the lives of multiple IO requests.
3037 * @controller: the handle to the controller object for which to allocate the
3038 * tag.
3039 *
3040 * IO tags are a protected resource. It is incumbent upon the SCI Core user to
3041 * ensure that each of the methods that may allocate or free available IO tags
3042 * are handled in a mutually exclusive manner. This method is one of said
3043 * methods requiring proper critical code section protection (e.g. semaphore,
3044 * spin-lock, etc.). An unsigned integer representing an available IO tag.
3045 * SCI_CONTROLLER_INVALID_IO_TAG This value is returned if there are no
3046 * currently available tags to be allocated. All return other values indicate a
3047 * legitimate tag.
3048 */
Dan Williamsac668c62011-06-07 18:50:55 -07003049u16 scic_controller_allocate_io_tag(struct scic_sds_controller *scic)
Dan Williamscc9203b2011-05-08 17:34:44 -07003050{
Dan Williamsac668c62011-06-07 18:50:55 -07003051 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamscc9203b2011-05-08 17:34:44 -07003052
Dan Williamsac668c62011-06-07 18:50:55 -07003053 if (isci_tci_space(ihost)) {
Dan Williamsdd047c82011-06-09 11:06:58 -07003054 u16 tci = isci_tci_alloc(ihost);
3055 u8 seq = scic->io_request_sequence[tci];
Dan Williamscc9203b2011-05-08 17:34:44 -07003056
Dan Williamsdd047c82011-06-09 11:06:58 -07003057 return ISCI_TAG(seq, tci);
Dan Williamscc9203b2011-05-08 17:34:44 -07003058 }
3059
3060 return SCI_CONTROLLER_INVALID_IO_TAG;
3061}
3062
3063/**
3064 * scic_controller_free_io_tag() - This method will free an IO tag to the pool
3065 * of free IO tags. This method provides the SCI Core user more flexibility
3066 * with regards to IO tags. The user may desire to keep an IO tag after an
3067 * IO request has completed, because they plan on re-using the tag for a
3068 * subsequent IO request. This method is only legal if the tag was
3069 * allocated via scic_controller_allocate_io_tag().
3070 * @controller: This parameter specifies the handle to the controller object
3071 * for which to free/return the tag.
3072 * @io_tag: This parameter represents the tag to be freed to the pool of
3073 * available tags.
3074 *
3075 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
3076 * to ensure that each of the methods that may allocate or free available IO
3077 * tags are handled in a mutually exclusive manner. This method is one of said
3078 * methods requiring proper critical code section protection (e.g. semaphore,
3079 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
3080 * Core user, using the scic_controller_allocate_io_tag() method, then it is
3081 * the responsibility of the caller to invoke this method to free the tag. This
3082 * method returns an indication of whether the tag was successfully put back
3083 * (freed) to the pool of available tags. SCI_SUCCESS This return value
3084 * indicates the tag was successfully placed into the pool of available IO
3085 * tags. SCI_FAILURE_INVALID_IO_TAG This value is returned if the supplied tag
3086 * is not a valid IO tag value.
3087 */
Dan Williamsac668c62011-06-07 18:50:55 -07003088enum sci_status scic_controller_free_io_tag(struct scic_sds_controller *scic,
3089 u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07003090{
Dan Williamsac668c62011-06-07 18:50:55 -07003091 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamsdd047c82011-06-09 11:06:58 -07003092 u16 tci = ISCI_TAG_TCI(io_tag);
3093 u16 seq = ISCI_TAG_SEQ(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07003094
Dan Williamsac668c62011-06-07 18:50:55 -07003095 /* prevent tail from passing head */
3096 if (isci_tci_active(ihost) == 0)
3097 return SCI_FAILURE_INVALID_IO_TAG;
Dan Williamscc9203b2011-05-08 17:34:44 -07003098
Dan Williamsdd047c82011-06-09 11:06:58 -07003099 if (seq == scic->io_request_sequence[tci]) {
3100 scic->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
Dan Williamscc9203b2011-05-08 17:34:44 -07003101
Dan Williamsdd047c82011-06-09 11:06:58 -07003102 isci_tci_free(ihost, ISCI_TAG_TCI(io_tag));
Dan Williamsac668c62011-06-07 18:50:55 -07003103
3104 return SCI_SUCCESS;
Dan Williamscc9203b2011-05-08 17:34:44 -07003105 }
Dan Williamscc9203b2011-05-08 17:34:44 -07003106 return SCI_FAILURE_INVALID_IO_TAG;
3107}