blob: 343655bd1a6a5bfb1c61a4a591ac8fc85cfeff91 [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/**
126 * INCREMENT_COMPLETION_QUEUE_GET() -
127 *
128 * This macro will increment the controllers completion queue index value and
129 * possibly toggle the cycle bit if the completion queue index wraps back to 0.
130 */
131#define INCREMENT_COMPLETION_QUEUE_GET(controller, index, cycle) \
132 INCREMENT_QUEUE_GET(\
133 (index), \
134 (cycle), \
Dan Williams7c78da32011-06-01 16:00:01 -0700135 SCU_MAX_COMPLETION_QUEUE_ENTRIES, \
136 SMU_CQGR_CYCLE_BIT)
Dan Williamscc9203b2011-05-08 17:34:44 -0700137
138/**
139 * INCREMENT_EVENT_QUEUE_GET() -
140 *
141 * This macro will increment the controllers event queue index value and
142 * possibly toggle the event cycle bit if the event queue index wraps back to 0.
143 */
144#define INCREMENT_EVENT_QUEUE_GET(controller, index, cycle) \
145 INCREMENT_QUEUE_GET(\
146 (index), \
147 (cycle), \
Dan Williams7c78da32011-06-01 16:00:01 -0700148 SCU_MAX_EVENTS, \
Dan Williamscc9203b2011-05-08 17:34:44 -0700149 SMU_CQGR_EVENT_CYCLE_BIT \
150 )
151
152
153/**
154 * NORMALIZE_GET_POINTER() -
155 *
156 * This macro will normalize the completion queue get pointer so its value can
157 * be used as an index into an array
158 */
159#define NORMALIZE_GET_POINTER(x) \
160 ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
161
162/**
163 * NORMALIZE_GET_POINTER_CYCLE_BIT() -
164 *
165 * This macro will normalize the completion queue cycle pointer so it matches
166 * the completion queue cycle bit
167 */
168#define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
169 ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
170
171/**
172 * COMPLETION_QUEUE_CYCLE_BIT() -
173 *
174 * This macro will return the cycle bit of the completion queue entry
175 */
176#define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
177
Edmund Nadolski12ef6542011-06-02 00:10:50 +0000178/* Init the state machine and call the state entry function (if any) */
179void sci_init_sm(struct sci_base_state_machine *sm,
180 const struct sci_base_state *state_table, u32 initial_state)
181{
182 sci_state_transition_t handler;
183
184 sm->initial_state_id = initial_state;
185 sm->previous_state_id = initial_state;
186 sm->current_state_id = initial_state;
187 sm->state_table = state_table;
188
189 handler = sm->state_table[initial_state].enter_state;
190 if (handler)
191 handler(sm);
192}
193
194/* Call the state exit fn, update the current state, call the state entry fn */
195void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
196{
197 sci_state_transition_t handler;
198
199 handler = sm->state_table[sm->current_state_id].exit_state;
200 if (handler)
201 handler(sm);
202
203 sm->previous_state_id = sm->current_state_id;
204 sm->current_state_id = next_state;
205
206 handler = sm->state_table[sm->current_state_id].enter_state;
207 if (handler)
208 handler(sm);
209}
210
Dan Williamscc9203b2011-05-08 17:34:44 -0700211static bool scic_sds_controller_completion_queue_has_entries(
212 struct scic_sds_controller *scic)
213{
214 u32 get_value = scic->completion_queue_get;
215 u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
216
217 if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
218 COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
219 return true;
220
221 return false;
222}
223
224static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
225{
226 if (scic_sds_controller_completion_queue_has_entries(scic)) {
227 return true;
228 } else {
229 /*
230 * we have a spurious interrupt it could be that we have already
231 * emptied the completion queue from a previous interrupt */
232 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
233
234 /*
235 * There is a race in the hardware that could cause us not to be notified
236 * of an interrupt completion if we do not take this step. We will mask
237 * then unmask the interrupts so if there is another interrupt pending
238 * the clearing of the interrupt source we get the next interrupt message. */
239 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
240 writel(0, &scic->smu_registers->interrupt_mask);
241 }
242
243 return false;
244}
245
Dan Williamsc7ef4032011-02-18 09:25:05 -0800246irqreturn_t isci_msix_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700247{
Dan Williamsc7ef4032011-02-18 09:25:05 -0800248 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700249
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000250 if (scic_sds_controller_isr(&ihost->sci))
Dan Williams0cf89d12011-02-18 09:25:07 -0800251 tasklet_schedule(&ihost->completion_tasklet);
Dan Williams6f231dd2011-07-02 22:56:22 -0700252
Dan Williamsc7ef4032011-02-18 09:25:05 -0800253 return IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700254}
255
Dan Williamscc9203b2011-05-08 17:34:44 -0700256static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
257{
258 u32 interrupt_status;
259
260 interrupt_status =
261 readl(&scic->smu_registers->interrupt_status);
262 interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
263
264 if (interrupt_status != 0) {
265 /*
266 * There is an error interrupt pending so let it through and handle
267 * in the callback */
268 return true;
269 }
270
271 /*
272 * There is a race in the hardware that could cause us not to be notified
273 * of an interrupt completion if we do not take this step. We will mask
274 * then unmask the error interrupts so if there was another interrupt
275 * pending we will be notified.
276 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
277 writel(0xff, &scic->smu_registers->interrupt_mask);
278 writel(0, &scic->smu_registers->interrupt_mask);
279
280 return false;
281}
282
283static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
284 u32 completion_entry)
285{
286 u32 index;
287 struct scic_sds_request *io_request;
288
289 index = SCU_GET_COMPLETION_INDEX(completion_entry);
290 io_request = scic->io_request_table[index];
291
292 /* Make sure that we really want to process this IO request */
293 if (
294 (io_request != NULL)
295 && (io_request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG)
296 && (
297 scic_sds_io_tag_get_sequence(io_request->io_tag)
298 == scic->io_request_sequence[index]
299 )
300 ) {
301 /* Yep this is a valid io request pass it along to the io request handler */
302 scic_sds_io_request_tc_completion(io_request, completion_entry);
303 }
304}
305
306static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
307 u32 completion_entry)
308{
309 u32 index;
310 struct scic_sds_request *io_request;
311 struct scic_sds_remote_device *device;
312
313 index = SCU_GET_COMPLETION_INDEX(completion_entry);
314
315 switch (scu_get_command_request_type(completion_entry)) {
316 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
317 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
318 io_request = scic->io_request_table[index];
319 dev_warn(scic_to_dev(scic),
320 "%s: SCIC SDS Completion type SDMA %x for io request "
321 "%p\n",
322 __func__,
323 completion_entry,
324 io_request);
325 /* @todo For a post TC operation we need to fail the IO
326 * request
327 */
328 break;
329
330 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
331 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
332 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
333 device = scic->device_table[index];
334 dev_warn(scic_to_dev(scic),
335 "%s: SCIC SDS Completion type SDMA %x for remote "
336 "device %p\n",
337 __func__,
338 completion_entry,
339 device);
340 /* @todo For a port RNC operation we need to fail the
341 * device
342 */
343 break;
344
345 default:
346 dev_warn(scic_to_dev(scic),
347 "%s: SCIC SDS Completion unknown SDMA completion "
348 "type %x\n",
349 __func__,
350 completion_entry);
351 break;
352
353 }
354}
355
356static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
357 u32 completion_entry)
358{
359 u32 index;
360 u32 frame_index;
361
362 struct isci_host *ihost = scic_to_ihost(scic);
363 struct scu_unsolicited_frame_header *frame_header;
364 struct scic_sds_phy *phy;
365 struct scic_sds_remote_device *device;
366
367 enum sci_status result = SCI_FAILURE;
368
369 frame_index = SCU_GET_FRAME_INDEX(completion_entry);
370
371 frame_header = scic->uf_control.buffers.array[frame_index].header;
372 scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
373
374 if (SCU_GET_FRAME_ERROR(completion_entry)) {
375 /*
376 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
377 * / this cause a problem? We expect the phy initialization will
378 * / fail if there is an error in the frame. */
379 scic_sds_controller_release_frame(scic, frame_index);
380 return;
381 }
382
383 if (frame_header->is_address_frame) {
384 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
385 phy = &ihost->phys[index].sci;
386 result = scic_sds_phy_frame_handler(phy, frame_index);
387 } else {
388
389 index = SCU_GET_COMPLETION_INDEX(completion_entry);
390
391 if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
392 /*
393 * This is a signature fis or a frame from a direct attached SATA
394 * device that has not yet been created. In either case forwared
395 * the frame to the PE and let it take care of the frame data. */
396 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
397 phy = &ihost->phys[index].sci;
398 result = scic_sds_phy_frame_handler(phy, frame_index);
399 } else {
400 if (index < scic->remote_node_entries)
401 device = scic->device_table[index];
402 else
403 device = NULL;
404
405 if (device != NULL)
406 result = scic_sds_remote_device_frame_handler(device, frame_index);
407 else
408 scic_sds_controller_release_frame(scic, frame_index);
409 }
410 }
411
412 if (result != SCI_SUCCESS) {
413 /*
414 * / @todo Is there any reason to report some additional error message
415 * / when we get this failure notifiction? */
416 }
417}
418
419static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
420 u32 completion_entry)
421{
422 struct isci_host *ihost = scic_to_ihost(scic);
423 struct scic_sds_request *io_request;
424 struct scic_sds_remote_device *device;
425 struct scic_sds_phy *phy;
426 u32 index;
427
428 index = SCU_GET_COMPLETION_INDEX(completion_entry);
429
430 switch (scu_get_event_type(completion_entry)) {
431 case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
432 /* / @todo The driver did something wrong and we need to fix the condtion. */
433 dev_err(scic_to_dev(scic),
434 "%s: SCIC Controller 0x%p received SMU command error "
435 "0x%x\n",
436 __func__,
437 scic,
438 completion_entry);
439 break;
440
441 case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
442 case SCU_EVENT_TYPE_SMU_ERROR:
443 case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
444 /*
445 * / @todo This is a hardware failure and its likely that we want to
446 * / reset the controller. */
447 dev_err(scic_to_dev(scic),
448 "%s: SCIC Controller 0x%p received fatal controller "
449 "event 0x%x\n",
450 __func__,
451 scic,
452 completion_entry);
453 break;
454
455 case SCU_EVENT_TYPE_TRANSPORT_ERROR:
456 io_request = scic->io_request_table[index];
457 scic_sds_io_request_event_handler(io_request, completion_entry);
458 break;
459
460 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
461 switch (scu_get_event_specifier(completion_entry)) {
462 case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
463 case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
464 io_request = scic->io_request_table[index];
465 if (io_request != NULL)
466 scic_sds_io_request_event_handler(io_request, completion_entry);
467 else
468 dev_warn(scic_to_dev(scic),
469 "%s: SCIC Controller 0x%p received "
470 "event 0x%x for io request object "
471 "that doesnt exist.\n",
472 __func__,
473 scic,
474 completion_entry);
475
476 break;
477
478 case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
479 device = scic->device_table[index];
480 if (device != NULL)
481 scic_sds_remote_device_event_handler(device, completion_entry);
482 else
483 dev_warn(scic_to_dev(scic),
484 "%s: SCIC Controller 0x%p received "
485 "event 0x%x for remote device object "
486 "that doesnt exist.\n",
487 __func__,
488 scic,
489 completion_entry);
490
491 break;
492 }
493 break;
494
495 case SCU_EVENT_TYPE_BROADCAST_CHANGE:
496 /*
497 * direct the broadcast change event to the phy first and then let
498 * the phy redirect the broadcast change to the port object */
499 case SCU_EVENT_TYPE_ERR_CNT_EVENT:
500 /*
501 * direct error counter event to the phy object since that is where
502 * we get the event notification. This is a type 4 event. */
503 case SCU_EVENT_TYPE_OSSP_EVENT:
504 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
505 phy = &ihost->phys[index].sci;
506 scic_sds_phy_event_handler(phy, completion_entry);
507 break;
508
509 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
510 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
511 case SCU_EVENT_TYPE_RNC_OPS_MISC:
512 if (index < scic->remote_node_entries) {
513 device = scic->device_table[index];
514
515 if (device != NULL)
516 scic_sds_remote_device_event_handler(device, completion_entry);
517 } else
518 dev_err(scic_to_dev(scic),
519 "%s: SCIC Controller 0x%p received event 0x%x "
520 "for remote device object 0x%0x that doesnt "
521 "exist.\n",
522 __func__,
523 scic,
524 completion_entry,
525 index);
526
527 break;
528
529 default:
530 dev_warn(scic_to_dev(scic),
531 "%s: SCIC Controller received unknown event code %x\n",
532 __func__,
533 completion_entry);
534 break;
535 }
536}
537
538
539
540static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
541{
542 u32 completion_count = 0;
543 u32 completion_entry;
544 u32 get_index;
545 u32 get_cycle;
546 u32 event_index;
547 u32 event_cycle;
548
549 dev_dbg(scic_to_dev(scic),
550 "%s: completion queue begining get:0x%08x\n",
551 __func__,
552 scic->completion_queue_get);
553
554 /* Get the component parts of the completion queue */
555 get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
556 get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
557
558 event_index = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
559 event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
560
561 while (
562 NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
563 == COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
564 ) {
565 completion_count++;
566
567 completion_entry = scic->completion_queue[get_index];
568 INCREMENT_COMPLETION_QUEUE_GET(scic, get_index, get_cycle);
569
570 dev_dbg(scic_to_dev(scic),
571 "%s: completion queue entry:0x%08x\n",
572 __func__,
573 completion_entry);
574
575 switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
576 case SCU_COMPLETION_TYPE_TASK:
577 scic_sds_controller_task_completion(scic, completion_entry);
578 break;
579
580 case SCU_COMPLETION_TYPE_SDMA:
581 scic_sds_controller_sdma_completion(scic, completion_entry);
582 break;
583
584 case SCU_COMPLETION_TYPE_UFI:
585 scic_sds_controller_unsolicited_frame(scic, completion_entry);
586 break;
587
588 case SCU_COMPLETION_TYPE_EVENT:
589 INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
590 scic_sds_controller_event_completion(scic, completion_entry);
591 break;
592
593 case SCU_COMPLETION_TYPE_NOTIFY:
594 /*
595 * Presently we do the same thing with a notify event that we do with the
596 * other event codes. */
597 INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
598 scic_sds_controller_event_completion(scic, completion_entry);
599 break;
600
601 default:
602 dev_warn(scic_to_dev(scic),
603 "%s: SCIC Controller received unknown "
604 "completion type %x\n",
605 __func__,
606 completion_entry);
607 break;
608 }
609 }
610
611 /* Update the get register if we completed one or more entries */
612 if (completion_count > 0) {
613 scic->completion_queue_get =
614 SMU_CQGR_GEN_BIT(ENABLE) |
615 SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
616 event_cycle |
617 SMU_CQGR_GEN_VAL(EVENT_POINTER, event_index) |
618 get_cycle |
619 SMU_CQGR_GEN_VAL(POINTER, get_index);
620
621 writel(scic->completion_queue_get,
622 &scic->smu_registers->completion_queue_get);
623
624 }
625
626 dev_dbg(scic_to_dev(scic),
627 "%s: completion queue ending get:0x%08x\n",
628 __func__,
629 scic->completion_queue_get);
630
631}
632
633static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
634{
635 u32 interrupt_status;
636
637 interrupt_status =
638 readl(&scic->smu_registers->interrupt_status);
639
640 if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
641 scic_sds_controller_completion_queue_has_entries(scic)) {
642
643 scic_sds_controller_process_completions(scic);
644 writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
645 } else {
646 dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
647 interrupt_status);
648
Edmund Nadolskie3013702011-06-02 00:10:43 +0000649 sci_change_state(&scic->sm, SCIC_FAILED);
Dan Williamscc9203b2011-05-08 17:34:44 -0700650
651 return;
652 }
653
654 /* If we dont process any completions I am not sure that we want to do this.
655 * We are in the middle of a hardware fault and should probably be reset.
656 */
657 writel(0, &scic->smu_registers->interrupt_mask);
658}
659
Dan Williamsc7ef4032011-02-18 09:25:05 -0800660irqreturn_t isci_intx_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700661{
Dan Williams6f231dd2011-07-02 22:56:22 -0700662 irqreturn_t ret = IRQ_NONE;
Dan Williams31e824e2011-04-19 12:32:51 -0700663 struct isci_host *ihost = data;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000664 struct scic_sds_controller *scic = &ihost->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700665
Dan Williams31e824e2011-04-19 12:32:51 -0700666 if (scic_sds_controller_isr(scic)) {
667 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
668 tasklet_schedule(&ihost->completion_tasklet);
669 ret = IRQ_HANDLED;
670 } else if (scic_sds_controller_error_isr(scic)) {
671 spin_lock(&ihost->scic_lock);
672 scic_sds_controller_error_handler(scic);
673 spin_unlock(&ihost->scic_lock);
674 ret = IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700675 }
Dan Williams92f4f0f2011-02-18 09:25:11 -0800676
Dan Williams6f231dd2011-07-02 22:56:22 -0700677 return ret;
678}
679
Dan Williams92f4f0f2011-02-18 09:25:11 -0800680irqreturn_t isci_error_isr(int vec, void *data)
681{
682 struct isci_host *ihost = data;
Dan Williams92f4f0f2011-02-18 09:25:11 -0800683
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000684 if (scic_sds_controller_error_isr(&ihost->sci))
685 scic_sds_controller_error_handler(&ihost->sci);
Dan Williams92f4f0f2011-02-18 09:25:11 -0800686
687 return IRQ_HANDLED;
688}
Dan Williams6f231dd2011-07-02 22:56:22 -0700689
690/**
691 * isci_host_start_complete() - This function is called by the core library,
692 * through the ISCI Module, to indicate controller start status.
693 * @isci_host: This parameter specifies the ISCI host object
694 * @completion_status: This parameter specifies the completion status from the
695 * core library.
696 *
697 */
Dan Williamscc9203b2011-05-08 17:34:44 -0700698static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700699{
Dan Williams0cf89d12011-02-18 09:25:07 -0800700 if (completion_status != SCI_SUCCESS)
701 dev_info(&ihost->pdev->dev,
702 "controller start timed out, continuing...\n");
703 isci_host_change_state(ihost, isci_ready);
704 clear_bit(IHOST_START_PENDING, &ihost->flags);
705 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700706}
707
Dan Williamsc7ef4032011-02-18 09:25:05 -0800708int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
Dan Williams6f231dd2011-07-02 22:56:22 -0700709{
Dan Williams4393aa42011-03-31 13:10:44 -0700710 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williams6f231dd2011-07-02 22:56:22 -0700711
Edmund Nadolski77950f52011-02-18 09:25:09 -0800712 if (test_bit(IHOST_START_PENDING, &ihost->flags))
Dan Williams6f231dd2011-07-02 22:56:22 -0700713 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700714
Edmund Nadolski77950f52011-02-18 09:25:09 -0800715 /* todo: use sas_flush_discovery once it is upstream */
716 scsi_flush_work(shost);
717
718 scsi_flush_work(shost);
Dan Williams6f231dd2011-07-02 22:56:22 -0700719
Dan Williams0cf89d12011-02-18 09:25:07 -0800720 dev_dbg(&ihost->pdev->dev,
721 "%s: ihost->status = %d, time = %ld\n",
722 __func__, isci_host_get_state(ihost), time);
Dan Williams6f231dd2011-07-02 22:56:22 -0700723
Dan Williams6f231dd2011-07-02 22:56:22 -0700724 return 1;
725
726}
727
Dan Williamscc9203b2011-05-08 17:34:44 -0700728/**
729 * scic_controller_get_suggested_start_timeout() - This method returns the
730 * suggested scic_controller_start() timeout amount. The user is free to
731 * use any timeout value, but this method provides the suggested minimum
732 * start timeout value. The returned value is based upon empirical
733 * information determined as a result of interoperability testing.
734 * @controller: the handle to the controller object for which to return the
735 * suggested start timeout.
736 *
737 * This method returns the number of milliseconds for the suggested start
738 * operation timeout.
739 */
740static u32 scic_controller_get_suggested_start_timeout(
741 struct scic_sds_controller *sc)
742{
743 /* Validate the user supplied parameters. */
744 if (sc == NULL)
745 return 0;
746
747 /*
748 * The suggested minimum timeout value for a controller start operation:
749 *
750 * Signature FIS Timeout
751 * + Phy Start Timeout
752 * + Number of Phy Spin Up Intervals
753 * ---------------------------------
754 * Number of milliseconds for the controller start operation.
755 *
756 * NOTE: The number of phy spin up intervals will be equivalent
757 * to the number of phys divided by the number phys allowed
758 * per interval - 1 (once OEM parameters are supported).
759 * Currently we assume only 1 phy per interval. */
760
761 return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
762 + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
763 + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
764}
765
766static void scic_controller_enable_interrupts(
767 struct scic_sds_controller *scic)
768{
769 BUG_ON(scic->smu_registers == NULL);
770 writel(0, &scic->smu_registers->interrupt_mask);
771}
772
773void scic_controller_disable_interrupts(
774 struct scic_sds_controller *scic)
775{
776 BUG_ON(scic->smu_registers == NULL);
777 writel(0xffffffff, &scic->smu_registers->interrupt_mask);
778}
779
780static void scic_sds_controller_enable_port_task_scheduler(
781 struct scic_sds_controller *scic)
782{
783 u32 port_task_scheduler_value;
784
785 port_task_scheduler_value =
786 readl(&scic->scu_registers->peg0.ptsg.control);
787 port_task_scheduler_value |=
788 (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
789 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
790 writel(port_task_scheduler_value,
791 &scic->scu_registers->peg0.ptsg.control);
792}
793
794static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
795{
796 u32 task_assignment;
797
798 /*
799 * Assign all the TCs to function 0
800 * TODO: Do we actually need to read this register to write it back?
801 */
802
803 task_assignment =
804 readl(&scic->smu_registers->task_context_assignment[0]);
805
806 task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
807 (SMU_TCA_GEN_VAL(ENDING, scic->task_context_entries - 1)) |
808 (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
809
810 writel(task_assignment,
811 &scic->smu_registers->task_context_assignment[0]);
812
813}
814
815static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
816{
817 u32 index;
818 u32 completion_queue_control_value;
819 u32 completion_queue_get_value;
820 u32 completion_queue_put_value;
821
822 scic->completion_queue_get = 0;
823
Dan Williams7c78da32011-06-01 16:00:01 -0700824 completion_queue_control_value =
825 (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
826 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
Dan Williamscc9203b2011-05-08 17:34:44 -0700827
828 writel(completion_queue_control_value,
829 &scic->smu_registers->completion_queue_control);
830
831
832 /* Set the completion queue get pointer and enable the queue */
833 completion_queue_get_value = (
834 (SMU_CQGR_GEN_VAL(POINTER, 0))
835 | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
836 | (SMU_CQGR_GEN_BIT(ENABLE))
837 | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
838 );
839
840 writel(completion_queue_get_value,
841 &scic->smu_registers->completion_queue_get);
842
843 /* Set the completion queue put pointer */
844 completion_queue_put_value = (
845 (SMU_CQPR_GEN_VAL(POINTER, 0))
846 | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
847 );
848
849 writel(completion_queue_put_value,
850 &scic->smu_registers->completion_queue_put);
851
852 /* Initialize the cycle bit of the completion queue entries */
Dan Williams7c78da32011-06-01 16:00:01 -0700853 for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700854 /*
855 * If get.cycle_bit != completion_queue.cycle_bit
856 * its not a valid completion queue entry
857 * so at system start all entries are invalid */
858 scic->completion_queue[index] = 0x80000000;
859 }
860}
861
862static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
863{
864 u32 frame_queue_control_value;
865 u32 frame_queue_get_value;
866 u32 frame_queue_put_value;
867
868 /* Write the queue size */
869 frame_queue_control_value =
Dan Williams7c78da32011-06-01 16:00:01 -0700870 SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
Dan Williamscc9203b2011-05-08 17:34:44 -0700871
872 writel(frame_queue_control_value,
873 &scic->scu_registers->sdma.unsolicited_frame_queue_control);
874
875 /* Setup the get pointer for the unsolicited frame queue */
876 frame_queue_get_value = (
877 SCU_UFQGP_GEN_VAL(POINTER, 0)
878 | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
879 );
880
881 writel(frame_queue_get_value,
882 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
883 /* Setup the put pointer for the unsolicited frame queue */
884 frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
885 writel(frame_queue_put_value,
886 &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
887}
888
889/**
890 * This method will attempt to transition into the ready state for the
891 * controller and indicate that the controller start operation has completed
892 * if all criteria are met.
893 * @scic: This parameter indicates the controller object for which
894 * to transition to ready.
895 * @status: This parameter indicates the status value to be pass into the call
896 * to scic_cb_controller_start_complete().
897 *
898 * none.
899 */
900static void scic_sds_controller_transition_to_ready(
901 struct scic_sds_controller *scic,
902 enum sci_status status)
903{
904 struct isci_host *ihost = scic_to_ihost(scic);
905
Edmund Nadolskie3013702011-06-02 00:10:43 +0000906 if (scic->sm.current_state_id == SCIC_STARTING) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700907 /*
908 * We move into the ready state, because some of the phys/ports
909 * may be up and operational.
910 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000911 sci_change_state(&scic->sm, SCIC_READY);
Dan Williamscc9203b2011-05-08 17:34:44 -0700912
913 isci_host_start_complete(ihost, status);
914 }
915}
916
Adam Gruchala4a33c522011-05-10 23:54:23 +0000917static bool is_phy_starting(struct scic_sds_phy *sci_phy)
918{
919 enum scic_sds_phy_states state;
920
Edmund Nadolskie3013702011-06-02 00:10:43 +0000921 state = sci_phy->sm.current_state_id;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000922 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000923 case SCI_PHY_STARTING:
924 case SCI_PHY_SUB_INITIAL:
925 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
926 case SCI_PHY_SUB_AWAIT_IAF_UF:
927 case SCI_PHY_SUB_AWAIT_SAS_POWER:
928 case SCI_PHY_SUB_AWAIT_SATA_POWER:
929 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
930 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
931 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
932 case SCI_PHY_SUB_FINAL:
Adam Gruchala4a33c522011-05-10 23:54:23 +0000933 return true;
934 default:
935 return false;
936 }
937}
938
Dan Williamscc9203b2011-05-08 17:34:44 -0700939/**
940 * scic_sds_controller_start_next_phy - start phy
941 * @scic: controller
942 *
943 * If all the phys have been started, then attempt to transition the
944 * controller to the READY state and inform the user
945 * (scic_cb_controller_start_complete()).
946 */
947static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
948{
949 struct isci_host *ihost = scic_to_ihost(scic);
950 struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
951 struct scic_sds_phy *sci_phy;
952 enum sci_status status;
953
954 status = SCI_SUCCESS;
955
956 if (scic->phy_startup_timer_pending)
957 return status;
958
959 if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
960 bool is_controller_start_complete = true;
961 u32 state;
962 u8 index;
963
964 for (index = 0; index < SCI_MAX_PHYS; index++) {
965 sci_phy = &ihost->phys[index].sci;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000966 state = sci_phy->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -0700967
Dan Williams4f20ef42011-05-12 06:00:31 -0700968 if (!phy_get_non_dummy_port(sci_phy))
Dan Williamscc9203b2011-05-08 17:34:44 -0700969 continue;
970
971 /* The controller start operation is complete iff:
972 * - all links have been given an opportunity to start
973 * - have no indication of a connected device
974 * - have an indication of a connected device and it has
975 * finished the link training process.
976 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000977 if ((sci_phy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
978 (sci_phy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
979 (sci_phy->is_in_link_training == true && is_phy_starting(sci_phy))) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700980 is_controller_start_complete = false;
981 break;
982 }
983 }
984
985 /*
986 * The controller has successfully finished the start process.
987 * Inform the SCI Core user and transition to the READY state. */
988 if (is_controller_start_complete == true) {
989 scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700990 sci_del_timer(&scic->phy_timer);
991 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -0700992 }
993 } else {
994 sci_phy = &ihost->phys[scic->next_phy_to_start].sci;
995
996 if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dan Williams4f20ef42011-05-12 06:00:31 -0700997 if (phy_get_non_dummy_port(sci_phy) == NULL) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700998 scic->next_phy_to_start++;
999
1000 /* Caution recursion ahead be forwarned
1001 *
1002 * The PHY was never added to a PORT in MPC mode
1003 * so start the next phy in sequence This phy
1004 * will never go link up and will not draw power
1005 * the OEM parameters either configured the phy
1006 * incorrectly for the PORT or it was never
1007 * assigned to a PORT
1008 */
1009 return scic_sds_controller_start_next_phy(scic);
1010 }
1011 }
1012
1013 status = scic_sds_phy_start(sci_phy);
1014
1015 if (status == SCI_SUCCESS) {
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001016 sci_mod_timer(&scic->phy_timer,
1017 SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
1018 scic->phy_startup_timer_pending = true;
Dan Williamscc9203b2011-05-08 17:34:44 -07001019 } else {
1020 dev_warn(scic_to_dev(scic),
1021 "%s: Controller stop operation failed "
1022 "to stop phy %d because of status "
1023 "%d.\n",
1024 __func__,
1025 ihost->phys[scic->next_phy_to_start].sci.phy_index,
1026 status);
1027 }
1028
1029 scic->next_phy_to_start++;
1030 }
1031
1032 return status;
1033}
1034
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001035static void phy_startup_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001036{
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001037 struct sci_timer *tmr = (struct sci_timer *)data;
1038 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), phy_timer);
1039 struct isci_host *ihost = scic_to_ihost(scic);
1040 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001041 enum sci_status status;
1042
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001043 spin_lock_irqsave(&ihost->scic_lock, flags);
1044
1045 if (tmr->cancel)
1046 goto done;
1047
Dan Williamscc9203b2011-05-08 17:34:44 -07001048 scic->phy_startup_timer_pending = false;
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001049
1050 do {
Dan Williamscc9203b2011-05-08 17:34:44 -07001051 status = scic_sds_controller_start_next_phy(scic);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001052 } while (status != SCI_SUCCESS);
1053
1054done:
1055 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001056}
1057
Dan Williamsac668c62011-06-07 18:50:55 -07001058static void isci_tci_free(struct isci_host *ihost, u16 tci)
1059{
1060 u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
1061
1062 ihost->tci_pool[tail] = tci;
1063 ihost->tci_tail = tail + 1;
1064}
1065
1066static u16 isci_tci_alloc(struct isci_host *ihost)
1067{
1068 u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
1069 u16 tci = ihost->tci_pool[head];
1070
1071 ihost->tci_head = head + 1;
1072 return tci;
1073}
1074
1075static u16 isci_tci_active(struct isci_host *ihost)
1076{
1077 return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
1078}
1079
1080static u16 isci_tci_space(struct isci_host *ihost)
1081{
1082 return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
1083}
1084
Dan Williamscc9203b2011-05-08 17:34:44 -07001085static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
1086 u32 timeout)
1087{
1088 struct isci_host *ihost = scic_to_ihost(scic);
1089 enum sci_status result;
1090 u16 index;
1091
Edmund Nadolskie3013702011-06-02 00:10:43 +00001092 if (scic->sm.current_state_id != SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001093 dev_warn(scic_to_dev(scic),
1094 "SCIC Controller start operation requested in "
1095 "invalid state\n");
1096 return SCI_FAILURE_INVALID_STATE;
1097 }
1098
1099 /* Build the TCi free pool */
Dan Williamsac668c62011-06-07 18:50:55 -07001100 BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1101 ihost->tci_head = 0;
1102 ihost->tci_tail = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001103 for (index = 0; index < scic->task_context_entries; index++)
Dan Williamsac668c62011-06-07 18:50:55 -07001104 isci_tci_free(ihost, index);
Dan Williamscc9203b2011-05-08 17:34:44 -07001105
1106 /* Build the RNi free pool */
1107 scic_sds_remote_node_table_initialize(
1108 &scic->available_remote_nodes,
1109 scic->remote_node_entries);
1110
1111 /*
1112 * Before anything else lets make sure we will not be
1113 * interrupted by the hardware.
1114 */
1115 scic_controller_disable_interrupts(scic);
1116
1117 /* Enable the port task scheduler */
1118 scic_sds_controller_enable_port_task_scheduler(scic);
1119
1120 /* Assign all the task entries to scic physical function */
1121 scic_sds_controller_assign_task_entries(scic);
1122
1123 /* Now initialize the completion queue */
1124 scic_sds_controller_initialize_completion_queue(scic);
1125
1126 /* Initialize the unsolicited frame queue for use */
1127 scic_sds_controller_initialize_unsolicited_frame_queue(scic);
1128
1129 /* Start all of the ports on this controller */
1130 for (index = 0; index < scic->logical_port_entries; index++) {
1131 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1132
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001133 result = scic_sds_port_start(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001134 if (result)
1135 return result;
1136 }
1137
1138 scic_sds_controller_start_next_phy(scic);
1139
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001140 sci_mod_timer(&scic->timer, timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07001141
Edmund Nadolskie3013702011-06-02 00:10:43 +00001142 sci_change_state(&scic->sm, SCIC_STARTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001143
1144 return SCI_SUCCESS;
1145}
1146
Dan Williams6f231dd2011-07-02 22:56:22 -07001147void isci_host_scan_start(struct Scsi_Host *shost)
1148{
Dan Williams4393aa42011-03-31 13:10:44 -07001149 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001150 unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001151
Dan Williams0cf89d12011-02-18 09:25:07 -08001152 set_bit(IHOST_START_PENDING, &ihost->flags);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001153
1154 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001155 scic_controller_start(&ihost->sci, tmo);
1156 scic_controller_enable_interrupts(&ihost->sci);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001157 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001158}
1159
Dan Williamscc9203b2011-05-08 17:34:44 -07001160static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001161{
Dan Williams0cf89d12011-02-18 09:25:07 -08001162 isci_host_change_state(ihost, isci_stopped);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001163 scic_controller_disable_interrupts(&ihost->sci);
Dan Williams0cf89d12011-02-18 09:25:07 -08001164 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
1165 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001166}
1167
Dan Williamscc9203b2011-05-08 17:34:44 -07001168static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
1169{
1170 /* Empty out the completion queue */
1171 if (scic_sds_controller_completion_queue_has_entries(scic))
1172 scic_sds_controller_process_completions(scic);
1173
1174 /* Clear the interrupt and enable all interrupts again */
1175 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
1176 /* Could we write the value of SMU_ISR_COMPLETION? */
1177 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
1178 writel(0, &scic->smu_registers->interrupt_mask);
1179}
1180
Dan Williams6f231dd2011-07-02 22:56:22 -07001181/**
1182 * isci_host_completion_routine() - This function is the delayed service
1183 * routine that calls the sci core library's completion handler. It's
1184 * scheduled as a tasklet from the interrupt service routine when interrupts
1185 * in use, or set as the timeout function in polled mode.
1186 * @data: This parameter specifies the ISCI host object
1187 *
1188 */
1189static void isci_host_completion_routine(unsigned long data)
1190{
1191 struct isci_host *isci_host = (struct isci_host *)data;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001192 struct list_head completed_request_list;
1193 struct list_head errored_request_list;
1194 struct list_head *current_position;
1195 struct list_head *next_position;
Dan Williams6f231dd2011-07-02 22:56:22 -07001196 struct isci_request *request;
1197 struct isci_request *next_request;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001198 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -07001199
1200 INIT_LIST_HEAD(&completed_request_list);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001201 INIT_LIST_HEAD(&errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001202
1203 spin_lock_irq(&isci_host->scic_lock);
1204
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001205 scic_sds_controller_completion_handler(&isci_host->sci);
Dan Williamsc7ef4032011-02-18 09:25:05 -08001206
Dan Williams6f231dd2011-07-02 22:56:22 -07001207 /* Take the lists of completed I/Os from the host. */
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001208
Dan Williams6f231dd2011-07-02 22:56:22 -07001209 list_splice_init(&isci_host->requests_to_complete,
1210 &completed_request_list);
1211
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001212 /* Take the list of errored I/Os from the host. */
1213 list_splice_init(&isci_host->requests_to_errorback,
1214 &errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001215
1216 spin_unlock_irq(&isci_host->scic_lock);
1217
1218 /* Process any completions in the lists. */
1219 list_for_each_safe(current_position, next_position,
1220 &completed_request_list) {
1221
1222 request = list_entry(current_position, struct isci_request,
1223 completed_node);
1224 task = isci_request_access_task(request);
1225
1226 /* Normal notification (task_done) */
1227 dev_dbg(&isci_host->pdev->dev,
1228 "%s: Normal - request/task = %p/%p\n",
1229 __func__,
1230 request,
1231 task);
1232
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001233 /* Return the task to libsas */
1234 if (task != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001235
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001236 task->lldd_task = NULL;
1237 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1238
1239 /* If the task is already in the abort path,
1240 * the task_done callback cannot be called.
1241 */
1242 task->task_done(task);
1243 }
1244 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001245 /* Free the request object. */
1246 isci_request_free(isci_host, request);
1247 }
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001248 list_for_each_entry_safe(request, next_request, &errored_request_list,
Dan Williams6f231dd2011-07-02 22:56:22 -07001249 completed_node) {
1250
1251 task = isci_request_access_task(request);
1252
1253 /* Use sas_task_abort */
1254 dev_warn(&isci_host->pdev->dev,
1255 "%s: Error - request/task = %p/%p\n",
1256 __func__,
1257 request,
1258 task);
1259
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001260 if (task != NULL) {
1261
1262 /* Put the task into the abort path if it's not there
1263 * already.
1264 */
1265 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
1266 sas_task_abort(task);
1267
1268 } else {
1269 /* This is a case where the request has completed with a
1270 * status such that it needed further target servicing,
1271 * but the sas_task reference has already been removed
1272 * from the request. Since it was errored, it was not
1273 * being aborted, so there is nothing to do except free
1274 * it.
1275 */
1276
1277 spin_lock_irq(&isci_host->scic_lock);
1278 /* Remove the request from the remote device's list
1279 * of pending requests.
1280 */
1281 list_del_init(&request->dev_node);
1282 spin_unlock_irq(&isci_host->scic_lock);
1283
1284 /* Free the request object. */
1285 isci_request_free(isci_host, request);
1286 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001287 }
1288
1289}
1290
Dan Williamscc9203b2011-05-08 17:34:44 -07001291/**
1292 * scic_controller_stop() - This method will stop an individual controller
1293 * object.This method will invoke the associated user callback upon
1294 * completion. The completion callback is called when the following
1295 * conditions are met: -# the method return status is SCI_SUCCESS. -# the
1296 * controller has been quiesced. This method will ensure that all IO
1297 * requests are quiesced, phys are stopped, and all additional operation by
1298 * the hardware is halted.
1299 * @controller: the handle to the controller object to stop.
1300 * @timeout: This parameter specifies the number of milliseconds in which the
1301 * stop operation should complete.
1302 *
1303 * The controller must be in the STARTED or STOPPED state. Indicate if the
1304 * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1305 * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1306 * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1307 * controller is not either in the STARTED or STOPPED states.
1308 */
1309static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
1310 u32 timeout)
1311{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001312 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001313 dev_warn(scic_to_dev(scic),
1314 "SCIC Controller stop operation requested in "
1315 "invalid state\n");
1316 return SCI_FAILURE_INVALID_STATE;
1317 }
1318
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001319 sci_mod_timer(&scic->timer, timeout);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001320 sci_change_state(&scic->sm, SCIC_STOPPING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001321 return SCI_SUCCESS;
1322}
1323
1324/**
1325 * scic_controller_reset() - This method will reset the supplied core
1326 * controller regardless of the state of said controller. This operation is
1327 * considered destructive. In other words, all current operations are wiped
1328 * out. No IO completions for outstanding devices occur. Outstanding IO
1329 * requests are not aborted or completed at the actual remote device.
1330 * @controller: the handle to the controller object to reset.
1331 *
1332 * Indicate if the controller reset method succeeded or failed in some way.
1333 * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1334 * the controller reset operation is unable to complete.
1335 */
1336static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
1337{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001338 switch (scic->sm.current_state_id) {
1339 case SCIC_RESET:
1340 case SCIC_READY:
1341 case SCIC_STOPPED:
1342 case SCIC_FAILED:
Dan Williamscc9203b2011-05-08 17:34:44 -07001343 /*
1344 * The reset operation is not a graceful cleanup, just
1345 * perform the state transition.
1346 */
Edmund Nadolskie3013702011-06-02 00:10:43 +00001347 sci_change_state(&scic->sm, SCIC_RESETTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001348 return SCI_SUCCESS;
1349 default:
1350 dev_warn(scic_to_dev(scic),
1351 "SCIC Controller reset operation requested in "
1352 "invalid state\n");
1353 return SCI_FAILURE_INVALID_STATE;
1354 }
1355}
1356
Dan Williams0cf89d12011-02-18 09:25:07 -08001357void isci_host_deinit(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07001358{
1359 int i;
1360
Dan Williams0cf89d12011-02-18 09:25:07 -08001361 isci_host_change_state(ihost, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001362 for (i = 0; i < SCI_MAX_PORTS; i++) {
Dan Williamse5313812011-05-07 10:11:43 -07001363 struct isci_port *iport = &ihost->ports[i];
Dan Williams0cf89d12011-02-18 09:25:07 -08001364 struct isci_remote_device *idev, *d;
1365
Dan Williamse5313812011-05-07 10:11:43 -07001366 list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
Dan Williams0cf89d12011-02-18 09:25:07 -08001367 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001368 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001369 }
1370 }
1371
Dan Williams0cf89d12011-02-18 09:25:07 -08001372 set_bit(IHOST_STOP_PENDING, &ihost->flags);
Dan Williams7c40a802011-03-02 11:49:26 -08001373
1374 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001375 scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
Dan Williams7c40a802011-03-02 11:49:26 -08001376 spin_unlock_irq(&ihost->scic_lock);
1377
Dan Williams0cf89d12011-02-18 09:25:07 -08001378 wait_for_stop(ihost);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001379 scic_controller_reset(&ihost->sci);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001380
1381 /* Cancel any/all outstanding port timers */
1382 for (i = 0; i < ihost->sci.logical_port_entries; i++) {
1383 struct scic_sds_port *sci_port = &ihost->ports[i].sci;
1384 del_timer_sync(&sci_port->timer.timer);
1385 }
1386
Edmund Nadolskia628d472011-05-19 11:59:36 +00001387 /* Cancel any/all outstanding phy timers */
1388 for (i = 0; i < SCI_MAX_PHYS; i++) {
1389 struct scic_sds_phy *sci_phy = &ihost->phys[i].sci;
1390 del_timer_sync(&sci_phy->sata_timer.timer);
1391 }
1392
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -07001393 del_timer_sync(&ihost->sci.port_agent.timer.timer);
1394
Edmund Nadolski04736612011-05-19 20:17:47 -07001395 del_timer_sync(&ihost->sci.power_control.timer.timer);
1396
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001397 del_timer_sync(&ihost->sci.timer.timer);
1398
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001399 del_timer_sync(&ihost->sci.phy_timer.timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001400}
1401
Dan Williams6f231dd2011-07-02 22:56:22 -07001402static void __iomem *scu_base(struct isci_host *isci_host)
1403{
1404 struct pci_dev *pdev = isci_host->pdev;
1405 int id = isci_host->id;
1406
1407 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
1408}
1409
1410static void __iomem *smu_base(struct isci_host *isci_host)
1411{
1412 struct pci_dev *pdev = isci_host->pdev;
1413 int id = isci_host->id;
1414
1415 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
1416}
1417
Dave Jiangb5f18a22011-03-16 14:57:23 -07001418static void isci_user_parameters_get(
1419 struct isci_host *isci_host,
1420 union scic_user_parameters *scic_user_params)
1421{
1422 struct scic_sds_user_parameters *u = &scic_user_params->sds1;
1423 int i;
1424
1425 for (i = 0; i < SCI_MAX_PHYS; i++) {
1426 struct sci_phy_user_params *u_phy = &u->phys[i];
1427
1428 u_phy->max_speed_generation = phy_gen;
1429
1430 /* we are not exporting these for now */
1431 u_phy->align_insertion_frequency = 0x7f;
1432 u_phy->in_connection_align_insertion_frequency = 0xff;
1433 u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1434 }
1435
1436 u->stp_inactivity_timeout = stp_inactive_to;
1437 u->ssp_inactivity_timeout = ssp_inactive_to;
1438 u->stp_max_occupancy_timeout = stp_max_occ_to;
1439 u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1440 u->no_outbound_task_timeout = no_outbound_task_to;
1441 u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1442}
1443
Dan Williams9269e0e2011-05-12 07:42:17 -07001444static void scic_sds_controller_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001445{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001446 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001447
Edmund Nadolskie3013702011-06-02 00:10:43 +00001448 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001449}
1450
Dan Williams9269e0e2011-05-12 07:42:17 -07001451static inline void scic_sds_controller_starting_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001452{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001453 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001454
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001455 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001456}
1457
1458#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1459#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1460#define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
1461#define INTERRUPT_COALESCE_NUMBER_MAX 256
1462#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
1463#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
1464
1465/**
1466 * scic_controller_set_interrupt_coalescence() - This method allows the user to
1467 * configure the interrupt coalescence.
1468 * @controller: This parameter represents the handle to the controller object
1469 * for which its interrupt coalesce register is overridden.
1470 * @coalesce_number: Used to control the number of entries in the Completion
1471 * Queue before an interrupt is generated. If the number of entries exceed
1472 * this number, an interrupt will be generated. The valid range of the input
1473 * is [0, 256]. A setting of 0 results in coalescing being disabled.
1474 * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1475 * input is [0, 2700000] . A setting of 0 is allowed and results in no
1476 * interrupt coalescing timeout.
1477 *
1478 * Indicate if the user successfully set the interrupt coalesce parameters.
1479 * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1480 * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1481 */
1482static enum sci_status scic_controller_set_interrupt_coalescence(
1483 struct scic_sds_controller *scic_controller,
1484 u32 coalesce_number,
1485 u32 coalesce_timeout)
1486{
1487 u8 timeout_encode = 0;
1488 u32 min = 0;
1489 u32 max = 0;
1490
1491 /* Check if the input parameters fall in the range. */
1492 if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1493 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1494
1495 /*
1496 * Defined encoding for interrupt coalescing timeout:
1497 * Value Min Max Units
1498 * ----- --- --- -----
1499 * 0 - - Disabled
1500 * 1 13.3 20.0 ns
1501 * 2 26.7 40.0
1502 * 3 53.3 80.0
1503 * 4 106.7 160.0
1504 * 5 213.3 320.0
1505 * 6 426.7 640.0
1506 * 7 853.3 1280.0
1507 * 8 1.7 2.6 us
1508 * 9 3.4 5.1
1509 * 10 6.8 10.2
1510 * 11 13.7 20.5
1511 * 12 27.3 41.0
1512 * 13 54.6 81.9
1513 * 14 109.2 163.8
1514 * 15 218.5 327.7
1515 * 16 436.9 655.4
1516 * 17 873.8 1310.7
1517 * 18 1.7 2.6 ms
1518 * 19 3.5 5.2
1519 * 20 7.0 10.5
1520 * 21 14.0 21.0
1521 * 22 28.0 41.9
1522 * 23 55.9 83.9
1523 * 24 111.8 167.8
1524 * 25 223.7 335.5
1525 * 26 447.4 671.1
1526 * 27 894.8 1342.2
1527 * 28 1.8 2.7 s
1528 * Others Undefined */
1529
1530 /*
1531 * Use the table above to decide the encode of interrupt coalescing timeout
1532 * value for register writing. */
1533 if (coalesce_timeout == 0)
1534 timeout_encode = 0;
1535 else{
1536 /* make the timeout value in unit of (10 ns). */
1537 coalesce_timeout = coalesce_timeout * 100;
1538 min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1539 max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1540
1541 /* get the encode of timeout for register writing. */
1542 for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1543 timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1544 timeout_encode++) {
1545 if (min <= coalesce_timeout && max > coalesce_timeout)
1546 break;
1547 else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1548 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1549 if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1550 break;
1551 else{
1552 timeout_encode++;
1553 break;
1554 }
1555 } else {
1556 max = max * 2;
1557 min = min * 2;
1558 }
1559 }
1560
1561 if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1562 /* the value is out of range. */
1563 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1564 }
1565
1566 writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1567 SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1568 &scic_controller->smu_registers->interrupt_coalesce_control);
1569
1570
1571 scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
1572 scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
1573
1574 return SCI_SUCCESS;
1575}
1576
1577
Dan Williams9269e0e2011-05-12 07:42:17 -07001578static void scic_sds_controller_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001579{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001580 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001581
1582 /* set the default interrupt coalescence number and timeout value. */
1583 scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
1584}
1585
Dan Williams9269e0e2011-05-12 07:42:17 -07001586static void scic_sds_controller_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001587{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001588 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001589
1590 /* disable interrupt coalescence. */
1591 scic_controller_set_interrupt_coalescence(scic, 0, 0);
1592}
1593
1594static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
1595{
1596 u32 index;
1597 enum sci_status status;
1598 enum sci_status phy_status;
1599 struct isci_host *ihost = scic_to_ihost(scic);
1600
1601 status = SCI_SUCCESS;
1602
1603 for (index = 0; index < SCI_MAX_PHYS; index++) {
1604 phy_status = scic_sds_phy_stop(&ihost->phys[index].sci);
1605
1606 if (phy_status != SCI_SUCCESS &&
1607 phy_status != SCI_FAILURE_INVALID_STATE) {
1608 status = SCI_FAILURE;
1609
1610 dev_warn(scic_to_dev(scic),
1611 "%s: Controller stop operation failed to stop "
1612 "phy %d because of status %d.\n",
1613 __func__,
1614 ihost->phys[index].sci.phy_index, phy_status);
1615 }
1616 }
1617
1618 return status;
1619}
1620
1621static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
1622{
1623 u32 index;
1624 enum sci_status port_status;
1625 enum sci_status status = SCI_SUCCESS;
1626 struct isci_host *ihost = scic_to_ihost(scic);
1627
1628 for (index = 0; index < scic->logical_port_entries; index++) {
1629 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
Dan Williamscc9203b2011-05-08 17:34:44 -07001630
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001631 port_status = scic_sds_port_stop(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001632
1633 if ((port_status != SCI_SUCCESS) &&
1634 (port_status != SCI_FAILURE_INVALID_STATE)) {
1635 status = SCI_FAILURE;
1636
1637 dev_warn(scic_to_dev(scic),
1638 "%s: Controller stop operation failed to "
1639 "stop port %d because of status %d.\n",
1640 __func__,
1641 sci_port->logical_port_index,
1642 port_status);
1643 }
1644 }
1645
1646 return status;
1647}
1648
1649static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
1650{
1651 u32 index;
1652 enum sci_status status;
1653 enum sci_status device_status;
1654
1655 status = SCI_SUCCESS;
1656
1657 for (index = 0; index < scic->remote_node_entries; index++) {
1658 if (scic->device_table[index] != NULL) {
1659 /* / @todo What timeout value do we want to provide to this request? */
1660 device_status = scic_remote_device_stop(scic->device_table[index], 0);
1661
1662 if ((device_status != SCI_SUCCESS) &&
1663 (device_status != SCI_FAILURE_INVALID_STATE)) {
1664 dev_warn(scic_to_dev(scic),
1665 "%s: Controller stop operation failed "
1666 "to stop device 0x%p because of "
1667 "status %d.\n",
1668 __func__,
1669 scic->device_table[index], device_status);
1670 }
1671 }
1672 }
1673
1674 return status;
1675}
1676
Dan Williams9269e0e2011-05-12 07:42:17 -07001677static void scic_sds_controller_stopping_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001678{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001679 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001680
1681 /* Stop all of the components for this controller */
1682 scic_sds_controller_stop_phys(scic);
1683 scic_sds_controller_stop_ports(scic);
1684 scic_sds_controller_stop_devices(scic);
1685}
1686
Dan Williams9269e0e2011-05-12 07:42:17 -07001687static void scic_sds_controller_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001688{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001689 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001690
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001691 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001692}
1693
1694
1695/**
1696 * scic_sds_controller_reset_hardware() -
1697 *
1698 * This method will reset the controller hardware.
1699 */
1700static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
1701{
1702 /* Disable interrupts so we dont take any spurious interrupts */
1703 scic_controller_disable_interrupts(scic);
1704
1705 /* Reset the SCU */
1706 writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
1707
1708 /* Delay for 1ms to before clearing the CQP and UFQPR. */
1709 udelay(1000);
1710
1711 /* The write to the CQGR clears the CQP */
1712 writel(0x00000000, &scic->smu_registers->completion_queue_get);
1713
1714 /* The write to the UFQGP clears the UFQPR */
1715 writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
1716}
1717
Dan Williams9269e0e2011-05-12 07:42:17 -07001718static void scic_sds_controller_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001719{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001720 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001721
1722 scic_sds_controller_reset_hardware(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001723 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001724}
1725
1726static const struct sci_base_state scic_sds_controller_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001727 [SCIC_INITIAL] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001728 .enter_state = scic_sds_controller_initial_state_enter,
1729 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001730 [SCIC_RESET] = {},
1731 [SCIC_INITIALIZING] = {},
1732 [SCIC_INITIALIZED] = {},
1733 [SCIC_STARTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001734 .exit_state = scic_sds_controller_starting_state_exit,
1735 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001736 [SCIC_READY] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001737 .enter_state = scic_sds_controller_ready_state_enter,
1738 .exit_state = scic_sds_controller_ready_state_exit,
1739 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001740 [SCIC_RESETTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001741 .enter_state = scic_sds_controller_resetting_state_enter,
1742 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001743 [SCIC_STOPPING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001744 .enter_state = scic_sds_controller_stopping_state_enter,
1745 .exit_state = scic_sds_controller_stopping_state_exit,
1746 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001747 [SCIC_STOPPED] = {},
1748 [SCIC_FAILED] = {}
Dan Williamscc9203b2011-05-08 17:34:44 -07001749};
1750
1751static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
1752{
1753 /* these defaults are overridden by the platform / firmware */
1754 struct isci_host *ihost = scic_to_ihost(scic);
1755 u16 index;
1756
1757 /* Default to APC mode. */
1758 scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1759
1760 /* Default to APC mode. */
1761 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
1762
1763 /* Default to no SSC operation. */
1764 scic->oem_parameters.sds1.controller.do_enable_ssc = false;
1765
1766 /* Initialize all of the port parameter information to narrow ports. */
1767 for (index = 0; index < SCI_MAX_PORTS; index++) {
1768 scic->oem_parameters.sds1.ports[index].phy_mask = 0;
1769 }
1770
1771 /* Initialize all of the phy parameter information. */
1772 for (index = 0; index < SCI_MAX_PHYS; index++) {
1773 /* Default to 6G (i.e. Gen 3) for now. */
1774 scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
1775
1776 /* the frequencies cannot be 0 */
1777 scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
1778 scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
1779 scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1780
1781 /*
1782 * Previous Vitesse based expanders had a arbitration issue that
1783 * is worked around by having the upper 32-bits of SAS address
1784 * with a value greater then the Vitesse company identifier.
1785 * Hence, usage of 0x5FCFFFFF. */
1786 scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
1787 scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
1788 }
1789
1790 scic->user_parameters.sds1.stp_inactivity_timeout = 5;
1791 scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
1792 scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
1793 scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
1794 scic->user_parameters.sds1.no_outbound_task_timeout = 20;
1795}
1796
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001797static void controller_timeout(unsigned long data)
1798{
1799 struct sci_timer *tmr = (struct sci_timer *)data;
1800 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), timer);
1801 struct isci_host *ihost = scic_to_ihost(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001802 struct sci_base_state_machine *sm = &scic->sm;
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001803 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001804
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001805 spin_lock_irqsave(&ihost->scic_lock, flags);
1806
1807 if (tmr->cancel)
1808 goto done;
1809
Edmund Nadolskie3013702011-06-02 00:10:43 +00001810 if (sm->current_state_id == SCIC_STARTING)
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001811 scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001812 else if (sm->current_state_id == SCIC_STOPPING) {
1813 sci_change_state(sm, SCIC_FAILED);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001814 isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
1815 } else /* / @todo Now what do we want to do in this case? */
1816 dev_err(scic_to_dev(scic),
1817 "%s: Controller timer fired when controller was not "
1818 "in a state being timed.\n",
1819 __func__);
1820
1821done:
1822 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1823}
Dan Williamscc9203b2011-05-08 17:34:44 -07001824
1825/**
1826 * scic_controller_construct() - This method will attempt to construct a
1827 * controller object utilizing the supplied parameter information.
1828 * @c: This parameter specifies the controller to be constructed.
1829 * @scu_base: mapped base address of the scu registers
1830 * @smu_base: mapped base address of the smu registers
1831 *
1832 * Indicate if the controller was successfully constructed or if it failed in
1833 * some way. SCI_SUCCESS This value is returned if the controller was
1834 * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
1835 * if the interrupt coalescence timer may cause SAS compliance issues for SMP
1836 * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
1837 * This value is returned if the controller does not support the supplied type.
1838 * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
1839 * controller does not support the supplied initialization data version.
1840 */
1841static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
1842 void __iomem *scu_base,
1843 void __iomem *smu_base)
1844{
1845 struct isci_host *ihost = scic_to_ihost(scic);
1846 u8 i;
1847
Edmund Nadolski12ef6542011-06-02 00:10:50 +00001848 sci_init_sm(&scic->sm, scic_sds_controller_state_table, SCIC_INITIAL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001849
1850 scic->scu_registers = scu_base;
1851 scic->smu_registers = smu_base;
1852
1853 scic_sds_port_configuration_agent_construct(&scic->port_agent);
1854
1855 /* Construct the ports for this controller */
1856 for (i = 0; i < SCI_MAX_PORTS; i++)
1857 scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
1858 scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
1859
1860 /* Construct the phys for this controller */
1861 for (i = 0; i < SCI_MAX_PHYS; i++) {
1862 /* Add all the PHYs to the dummy port */
1863 scic_sds_phy_construct(&ihost->phys[i].sci,
1864 &ihost->ports[SCI_MAX_PORTS].sci, i);
1865 }
1866
1867 scic->invalid_phy_mask = 0;
1868
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001869 sci_init_timer(&scic->timer, controller_timeout);
1870
Dan Williamscc9203b2011-05-08 17:34:44 -07001871 /* Initialize the User and OEM parameters to default values. */
1872 scic_sds_controller_set_default_config_parameters(scic);
1873
1874 return scic_controller_reset(scic);
1875}
1876
1877int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
1878{
1879 int i;
1880
1881 for (i = 0; i < SCI_MAX_PORTS; i++)
1882 if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1883 return -EINVAL;
1884
1885 for (i = 0; i < SCI_MAX_PHYS; i++)
1886 if (oem->phys[i].sas_address.high == 0 &&
1887 oem->phys[i].sas_address.low == 0)
1888 return -EINVAL;
1889
1890 if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1891 for (i = 0; i < SCI_MAX_PHYS; i++)
1892 if (oem->ports[i].phy_mask != 0)
1893 return -EINVAL;
1894 } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1895 u8 phy_mask = 0;
1896
1897 for (i = 0; i < SCI_MAX_PHYS; i++)
1898 phy_mask |= oem->ports[i].phy_mask;
1899
1900 if (phy_mask == 0)
1901 return -EINVAL;
1902 } else
1903 return -EINVAL;
1904
1905 if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1906 return -EINVAL;
1907
1908 return 0;
1909}
1910
1911static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
1912 union scic_oem_parameters *scic_parms)
1913{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001914 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07001915
Edmund Nadolskie3013702011-06-02 00:10:43 +00001916 if (state == SCIC_RESET ||
1917 state == SCIC_INITIALIZING ||
1918 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001919
1920 if (scic_oem_parameters_validate(&scic_parms->sds1))
1921 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1922 scic->oem_parameters.sds1 = scic_parms->sds1;
1923
1924 return SCI_SUCCESS;
1925 }
1926
1927 return SCI_FAILURE_INVALID_STATE;
1928}
1929
1930void scic_oem_parameters_get(
1931 struct scic_sds_controller *scic,
1932 union scic_oem_parameters *scic_parms)
1933{
1934 memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
1935}
1936
Edmund Nadolski04736612011-05-19 20:17:47 -07001937static void power_control_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001938{
Edmund Nadolski04736612011-05-19 20:17:47 -07001939 struct sci_timer *tmr = (struct sci_timer *)data;
1940 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), power_control.timer);
1941 struct isci_host *ihost = scic_to_ihost(scic);
1942 struct scic_sds_phy *sci_phy;
1943 unsigned long flags;
1944 u8 i;
Dan Williamscc9203b2011-05-08 17:34:44 -07001945
Edmund Nadolski04736612011-05-19 20:17:47 -07001946 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001947
Edmund Nadolski04736612011-05-19 20:17:47 -07001948 if (tmr->cancel)
1949 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001950
1951 scic->power_control.phys_granted_power = 0;
1952
1953 if (scic->power_control.phys_waiting == 0) {
1954 scic->power_control.timer_started = false;
Edmund Nadolski04736612011-05-19 20:17:47 -07001955 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001956 }
Edmund Nadolski04736612011-05-19 20:17:47 -07001957
1958 for (i = 0; i < SCI_MAX_PHYS; i++) {
1959
1960 if (scic->power_control.phys_waiting == 0)
1961 break;
1962
1963 sci_phy = scic->power_control.requesters[i];
1964 if (sci_phy == NULL)
1965 continue;
1966
1967 if (scic->power_control.phys_granted_power >=
1968 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up)
1969 break;
1970
1971 scic->power_control.requesters[i] = NULL;
1972 scic->power_control.phys_waiting--;
1973 scic->power_control.phys_granted_power++;
1974 scic_sds_phy_consume_power_handler(sci_phy);
1975 }
1976
1977 /*
1978 * It doesn't matter if the power list is empty, we need to start the
1979 * timer in case another phy becomes ready.
1980 */
1981 sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1982 scic->power_control.timer_started = true;
1983
1984done:
1985 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001986}
1987
1988/**
1989 * This method inserts the phy in the stagger spinup control queue.
1990 * @scic:
1991 *
1992 *
1993 */
1994void scic_sds_controller_power_control_queue_insert(
1995 struct scic_sds_controller *scic,
1996 struct scic_sds_phy *sci_phy)
1997{
1998 BUG_ON(sci_phy == NULL);
1999
2000 if (scic->power_control.phys_granted_power <
2001 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
2002 scic->power_control.phys_granted_power++;
2003 scic_sds_phy_consume_power_handler(sci_phy);
2004
2005 /*
2006 * stop and start the power_control timer. When the timer fires, the
2007 * no_of_phys_granted_power will be set to 0
2008 */
Edmund Nadolski04736612011-05-19 20:17:47 -07002009 if (scic->power_control.timer_started)
2010 sci_del_timer(&scic->power_control.timer);
2011
2012 sci_mod_timer(&scic->power_control.timer,
2013 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
2014 scic->power_control.timer_started = true;
2015
Dan Williamscc9203b2011-05-08 17:34:44 -07002016 } else {
2017 /* Add the phy in the waiting list */
2018 scic->power_control.requesters[sci_phy->phy_index] = sci_phy;
2019 scic->power_control.phys_waiting++;
2020 }
2021}
2022
2023/**
2024 * This method removes the phy from the stagger spinup control queue.
2025 * @scic:
2026 *
2027 *
2028 */
2029void scic_sds_controller_power_control_queue_remove(
2030 struct scic_sds_controller *scic,
2031 struct scic_sds_phy *sci_phy)
2032{
2033 BUG_ON(sci_phy == NULL);
2034
2035 if (scic->power_control.requesters[sci_phy->phy_index] != NULL) {
2036 scic->power_control.phys_waiting--;
2037 }
2038
2039 scic->power_control.requesters[sci_phy->phy_index] = NULL;
2040}
2041
2042#define AFE_REGISTER_WRITE_DELAY 10
2043
2044/* Initialize the AFE for this phy index. We need to read the AFE setup from
2045 * the OEM parameters
2046 */
2047static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
2048{
2049 const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
2050 u32 afe_status;
2051 u32 phy_id;
2052
2053 /* Clear DFX Status registers */
2054 writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
2055 udelay(AFE_REGISTER_WRITE_DELAY);
2056
2057 if (is_b0()) {
2058 /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
2059 * Timer, PM Stagger Timer */
2060 writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
2061 udelay(AFE_REGISTER_WRITE_DELAY);
2062 }
2063
2064 /* Configure bias currents to normal */
2065 if (is_a0())
2066 writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
2067 else if (is_a2())
2068 writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002069 else if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002070 writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
2071
2072 udelay(AFE_REGISTER_WRITE_DELAY);
2073
2074 /* Enable PLL */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002075 if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002076 writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
2077 else
2078 writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
2079
2080 udelay(AFE_REGISTER_WRITE_DELAY);
2081
2082 /* Wait for the PLL to lock */
2083 do {
2084 afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
2085 udelay(AFE_REGISTER_WRITE_DELAY);
2086 } while ((afe_status & 0x00001000) == 0);
2087
2088 if (is_a0() || is_a2()) {
2089 /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
2090 writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
2091 udelay(AFE_REGISTER_WRITE_DELAY);
2092 }
2093
2094 for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
2095 const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2096
2097 if (is_b0()) {
2098 /* Configure transmitter SSC parameters */
2099 writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2100 udelay(AFE_REGISTER_WRITE_DELAY);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002101 } else if (is_c0()) {
2102 /* Configure transmitter SSC parameters */
2103 writel(0x0003000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2104 udelay(AFE_REGISTER_WRITE_DELAY);
2105
2106 /*
2107 * All defaults, except the Receive Word Alignament/Comma Detect
2108 * Enable....(0xe800) */
2109 writel(0x00004500, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2110 udelay(AFE_REGISTER_WRITE_DELAY);
Dan Williamscc9203b2011-05-08 17:34:44 -07002111 } else {
2112 /*
2113 * All defaults, except the Receive Word Alignament/Comma Detect
2114 * Enable....(0xe800) */
2115 writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2116 udelay(AFE_REGISTER_WRITE_DELAY);
2117
2118 writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
2119 udelay(AFE_REGISTER_WRITE_DELAY);
2120 }
2121
2122 /*
2123 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2124 * & increase TX int & ext bias 20%....(0xe85c) */
2125 if (is_a0())
2126 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2127 else if (is_a2())
2128 writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002129 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002130 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002131 writel(0x000003D7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002132 udelay(AFE_REGISTER_WRITE_DELAY);
2133
2134 /*
2135 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2136 * & increase TX int & ext bias 20%....(0xe85c) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002137 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2138 } else {
2139 writel(0x000001E7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2140 udelay(AFE_REGISTER_WRITE_DELAY);
2141
2142 /*
2143 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2144 * & increase TX int & ext bias 20%....(0xe85c) */
2145 writel(0x000001E4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002146 }
2147 udelay(AFE_REGISTER_WRITE_DELAY);
2148
2149 if (is_a0() || is_a2()) {
2150 /* Enable TX equalization (0xe824) */
2151 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2152 udelay(AFE_REGISTER_WRITE_DELAY);
2153 }
2154
2155 /*
2156 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
2157 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
2158 writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2159 udelay(AFE_REGISTER_WRITE_DELAY);
2160
2161 /* Leave DFE/FFE on */
2162 if (is_a0())
2163 writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2164 else if (is_a2())
2165 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002166 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002167 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2168 udelay(AFE_REGISTER_WRITE_DELAY);
2169 /* Enable TX equalization (0xe824) */
2170 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002171 } else {
2172 writel(0x0140DF0F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
2173 udelay(AFE_REGISTER_WRITE_DELAY);
2174
2175 writel(0x3F6F103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2176 udelay(AFE_REGISTER_WRITE_DELAY);
2177
2178 /* Enable TX equalization (0xe824) */
2179 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002180 }
Adam Gruchaladbb07432011-06-01 22:31:03 +00002181
Dan Williamscc9203b2011-05-08 17:34:44 -07002182 udelay(AFE_REGISTER_WRITE_DELAY);
2183
2184 writel(oem_phy->afe_tx_amp_control0,
2185 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
2186 udelay(AFE_REGISTER_WRITE_DELAY);
2187
2188 writel(oem_phy->afe_tx_amp_control1,
2189 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
2190 udelay(AFE_REGISTER_WRITE_DELAY);
2191
2192 writel(oem_phy->afe_tx_amp_control2,
2193 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
2194 udelay(AFE_REGISTER_WRITE_DELAY);
2195
2196 writel(oem_phy->afe_tx_amp_control3,
2197 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
2198 udelay(AFE_REGISTER_WRITE_DELAY);
2199 }
2200
2201 /* Transfer control to the PEs */
2202 writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
2203 udelay(AFE_REGISTER_WRITE_DELAY);
2204}
2205
Dan Williamscc9203b2011-05-08 17:34:44 -07002206static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
2207{
Edmund Nadolski04736612011-05-19 20:17:47 -07002208 sci_init_timer(&scic->power_control.timer, power_control_timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07002209
2210 memset(scic->power_control.requesters, 0,
2211 sizeof(scic->power_control.requesters));
2212
2213 scic->power_control.phys_waiting = 0;
2214 scic->power_control.phys_granted_power = 0;
2215}
2216
2217static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
2218{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002219 struct sci_base_state_machine *sm = &scic->sm;
Dan Williamscc9203b2011-05-08 17:34:44 -07002220 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002221 enum sci_status result = SCI_FAILURE;
2222 unsigned long i, state, val;
Dan Williamscc9203b2011-05-08 17:34:44 -07002223
Edmund Nadolskie3013702011-06-02 00:10:43 +00002224 if (scic->sm.current_state_id != SCIC_RESET) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002225 dev_warn(scic_to_dev(scic),
2226 "SCIC Controller initialize operation requested "
2227 "in invalid state\n");
2228 return SCI_FAILURE_INVALID_STATE;
2229 }
2230
Edmund Nadolskie3013702011-06-02 00:10:43 +00002231 sci_change_state(sm, SCIC_INITIALIZING);
Dan Williamscc9203b2011-05-08 17:34:44 -07002232
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002233 sci_init_timer(&scic->phy_timer, phy_startup_timeout);
2234
2235 scic->next_phy_to_start = 0;
2236 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002237
2238 scic_sds_controller_initialize_power_control(scic);
2239
2240 /*
2241 * There is nothing to do here for B0 since we do not have to
2242 * program the AFE registers.
2243 * / @todo The AFE settings are supposed to be correct for the B0 but
2244 * / presently they seem to be wrong. */
2245 scic_sds_controller_afe_initialization(scic);
2246
Dan Williams7c78da32011-06-01 16:00:01 -07002247
2248 /* Take the hardware out of reset */
2249 writel(0, &scic->smu_registers->soft_reset_control);
2250
2251 /*
2252 * / @todo Provide meaningfull error code for hardware failure
2253 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2254 for (i = 100; i >= 1; i--) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002255 u32 status;
Dan Williamscc9203b2011-05-08 17:34:44 -07002256
Dan Williams7c78da32011-06-01 16:00:01 -07002257 /* Loop until the hardware reports success */
2258 udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2259 status = readl(&scic->smu_registers->control_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07002260
Dan Williams7c78da32011-06-01 16:00:01 -07002261 if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
2262 break;
Dan Williamscc9203b2011-05-08 17:34:44 -07002263 }
Dan Williams7c78da32011-06-01 16:00:01 -07002264 if (i == 0)
2265 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002266
Dan Williams7c78da32011-06-01 16:00:01 -07002267 /*
2268 * Determine what are the actaul device capacities that the
2269 * hardware will support */
2270 val = readl(&scic->smu_registers->device_context_capacity);
Dan Williamscc9203b2011-05-08 17:34:44 -07002271
Dan Williams7c78da32011-06-01 16:00:01 -07002272 /* Record the smaller of the two capacity values */
2273 scic->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2274 scic->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2275 scic->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
Dan Williamscc9203b2011-05-08 17:34:44 -07002276
Dan Williams7c78da32011-06-01 16:00:01 -07002277 /*
2278 * Make all PEs that are unassigned match up with the
2279 * logical ports
2280 */
2281 for (i = 0; i < scic->logical_port_entries; i++) {
2282 struct scu_port_task_scheduler_group_registers __iomem
2283 *ptsg = &scic->scu_registers->peg0.ptsg;
Dan Williamscc9203b2011-05-08 17:34:44 -07002284
Dan Williams7c78da32011-06-01 16:00:01 -07002285 writel(i, &ptsg->protocol_engine[i]);
Dan Williamscc9203b2011-05-08 17:34:44 -07002286 }
2287
2288 /* Initialize hardware PCI Relaxed ordering in DMA engines */
Dan Williams7c78da32011-06-01 16:00:01 -07002289 val = readl(&scic->scu_registers->sdma.pdma_configuration);
2290 val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2291 writel(val, &scic->scu_registers->sdma.pdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002292
Dan Williams7c78da32011-06-01 16:00:01 -07002293 val = readl(&scic->scu_registers->sdma.cdma_configuration);
2294 val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2295 writel(val, &scic->scu_registers->sdma.cdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002296
2297 /*
2298 * Initialize the PHYs before the PORTs because the PHY registers
2299 * are accessed during the port initialization.
2300 */
Dan Williams7c78da32011-06-01 16:00:01 -07002301 for (i = 0; i < SCI_MAX_PHYS; i++) {
2302 result = scic_sds_phy_initialize(&ihost->phys[i].sci,
2303 &scic->scu_registers->peg0.pe[i].tl,
2304 &scic->scu_registers->peg0.pe[i].ll);
2305 if (result != SCI_SUCCESS)
2306 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002307 }
2308
Dan Williams7c78da32011-06-01 16:00:01 -07002309 for (i = 0; i < scic->logical_port_entries; i++) {
2310 result = scic_sds_port_initialize(&ihost->ports[i].sci,
2311 &scic->scu_registers->peg0.ptsg.port[i],
2312 &scic->scu_registers->peg0.ptsg.protocol_engine,
2313 &scic->scu_registers->peg0.viit[i]);
2314
2315 if (result != SCI_SUCCESS)
2316 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002317 }
2318
Dan Williams7c78da32011-06-01 16:00:01 -07002319 result = scic_sds_port_configuration_agent_initialize(scic, &scic->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07002320
Dan Williams7c78da32011-06-01 16:00:01 -07002321 out:
Dan Williamscc9203b2011-05-08 17:34:44 -07002322 /* Advance the controller state machine */
2323 if (result == SCI_SUCCESS)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002324 state = SCIC_INITIALIZED;
Dan Williamscc9203b2011-05-08 17:34:44 -07002325 else
Edmund Nadolskie3013702011-06-02 00:10:43 +00002326 state = SCIC_FAILED;
2327 sci_change_state(sm, state);
Dan Williamscc9203b2011-05-08 17:34:44 -07002328
2329 return result;
2330}
2331
2332static enum sci_status scic_user_parameters_set(
2333 struct scic_sds_controller *scic,
2334 union scic_user_parameters *scic_parms)
2335{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002336 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07002337
Edmund Nadolskie3013702011-06-02 00:10:43 +00002338 if (state == SCIC_RESET ||
2339 state == SCIC_INITIALIZING ||
2340 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002341 u16 index;
2342
2343 /*
2344 * Validate the user parameters. If they are not legal, then
2345 * return a failure.
2346 */
2347 for (index = 0; index < SCI_MAX_PHYS; index++) {
2348 struct sci_phy_user_params *user_phy;
2349
2350 user_phy = &scic_parms->sds1.phys[index];
2351
2352 if (!((user_phy->max_speed_generation <=
2353 SCIC_SDS_PARM_MAX_SPEED) &&
2354 (user_phy->max_speed_generation >
2355 SCIC_SDS_PARM_NO_SPEED)))
2356 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2357
2358 if (user_phy->in_connection_align_insertion_frequency <
2359 3)
2360 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2361
2362 if ((user_phy->in_connection_align_insertion_frequency <
2363 3) ||
2364 (user_phy->align_insertion_frequency == 0) ||
2365 (user_phy->
2366 notify_enable_spin_up_insertion_frequency ==
2367 0))
2368 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2369 }
2370
2371 if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
2372 (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
2373 (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
2374 (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
2375 (scic_parms->sds1.no_outbound_task_timeout == 0))
2376 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2377
2378 memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
2379
2380 return SCI_SUCCESS;
2381 }
2382
2383 return SCI_FAILURE_INVALID_STATE;
2384}
2385
2386static int scic_controller_mem_init(struct scic_sds_controller *scic)
2387{
2388 struct device *dev = scic_to_dev(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002389 dma_addr_t dma;
2390 size_t size;
2391 int err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002392
Dan Williams7c78da32011-06-01 16:00:01 -07002393 size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2394 scic->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002395 if (!scic->completion_queue)
2396 return -ENOMEM;
2397
Dan Williams7c78da32011-06-01 16:00:01 -07002398 writel(lower_32_bits(dma), &scic->smu_registers->completion_queue_lower);
2399 writel(upper_32_bits(dma), &scic->smu_registers->completion_queue_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002400
Dan Williams7c78da32011-06-01 16:00:01 -07002401 size = scic->remote_node_entries * sizeof(union scu_remote_node_context);
2402 scic->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
2403 GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002404 if (!scic->remote_node_context_table)
2405 return -ENOMEM;
2406
Dan Williams7c78da32011-06-01 16:00:01 -07002407 writel(lower_32_bits(dma), &scic->smu_registers->remote_node_context_lower);
2408 writel(upper_32_bits(dma), &scic->smu_registers->remote_node_context_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002409
Dan Williams7c78da32011-06-01 16:00:01 -07002410 size = scic->task_context_entries * sizeof(struct scu_task_context),
2411 scic->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002412 if (!scic->task_context_table)
2413 return -ENOMEM;
2414
Dan Williams7c78da32011-06-01 16:00:01 -07002415 writel(lower_32_bits(dma), &scic->smu_registers->host_task_table_lower);
2416 writel(upper_32_bits(dma), &scic->smu_registers->host_task_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002417
Dan Williams7c78da32011-06-01 16:00:01 -07002418 err = scic_sds_unsolicited_frame_control_construct(scic);
2419 if (err)
2420 return err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002421
2422 /*
2423 * Inform the silicon as to the location of the UF headers and
2424 * address table.
2425 */
2426 writel(lower_32_bits(scic->uf_control.headers.physical_address),
2427 &scic->scu_registers->sdma.uf_header_base_address_lower);
2428 writel(upper_32_bits(scic->uf_control.headers.physical_address),
2429 &scic->scu_registers->sdma.uf_header_base_address_upper);
2430
2431 writel(lower_32_bits(scic->uf_control.address_table.physical_address),
2432 &scic->scu_registers->sdma.uf_address_table_lower);
2433 writel(upper_32_bits(scic->uf_control.address_table.physical_address),
2434 &scic->scu_registers->sdma.uf_address_table_upper);
2435
2436 return 0;
2437}
2438
Dan Williams6f231dd2011-07-02 22:56:22 -07002439int isci_host_init(struct isci_host *isci_host)
2440{
Dan Williamsd9c37392011-03-03 17:59:32 -08002441 int err = 0, i;
Dan Williams6f231dd2011-07-02 22:56:22 -07002442 enum sci_status status;
Dan Williams4711ba12011-03-11 10:43:57 -08002443 union scic_oem_parameters oem;
Dan Williams6f231dd2011-07-02 22:56:22 -07002444 union scic_user_parameters scic_user_params;
Dan Williamsd044af12011-03-08 09:52:49 -08002445 struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002446
Dan Williams6f231dd2011-07-02 22:56:22 -07002447 spin_lock_init(&isci_host->state_lock);
2448 spin_lock_init(&isci_host->scic_lock);
2449 spin_lock_init(&isci_host->queue_lock);
Dan Williams0cf89d12011-02-18 09:25:07 -08002450 init_waitqueue_head(&isci_host->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07002451
2452 isci_host_change_state(isci_host, isci_starting);
2453 isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
2454
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002455 status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
Dan Williams6f231dd2011-07-02 22:56:22 -07002456 smu_base(isci_host));
2457
2458 if (status != SCI_SUCCESS) {
2459 dev_err(&isci_host->pdev->dev,
2460 "%s: scic_controller_construct failed - status = %x\n",
2461 __func__,
2462 status);
Dave Jiang858d4aa2011-02-22 01:27:03 -08002463 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002464 }
2465
2466 isci_host->sas_ha.dev = &isci_host->pdev->dev;
2467 isci_host->sas_ha.lldd_ha = isci_host;
2468
Dan Williamsd044af12011-03-08 09:52:49 -08002469 /*
2470 * grab initial values stored in the controller object for OEM and USER
2471 * parameters
2472 */
Dave Jiangb5f18a22011-03-16 14:57:23 -07002473 isci_user_parameters_get(isci_host, &scic_user_params);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002474 status = scic_user_parameters_set(&isci_host->sci,
Dan Williamsd044af12011-03-08 09:52:49 -08002475 &scic_user_params);
2476 if (status != SCI_SUCCESS) {
2477 dev_warn(&isci_host->pdev->dev,
2478 "%s: scic_user_parameters_set failed\n",
2479 __func__);
2480 return -ENODEV;
2481 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002482
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002483 scic_oem_parameters_get(&isci_host->sci, &oem);
Dan Williamsd044af12011-03-08 09:52:49 -08002484
2485 /* grab any OEM parameters specified in orom */
2486 if (pci_info->orom) {
Dan Williams4711ba12011-03-11 10:43:57 -08002487 status = isci_parse_oem_parameters(&oem,
Dan Williamsd044af12011-03-08 09:52:49 -08002488 pci_info->orom,
2489 isci_host->id);
Dan Williams6f231dd2011-07-02 22:56:22 -07002490 if (status != SCI_SUCCESS) {
2491 dev_warn(&isci_host->pdev->dev,
2492 "parsing firmware oem parameters failed\n");
Dave Jiang858d4aa2011-02-22 01:27:03 -08002493 return -EINVAL;
Dan Williams6f231dd2011-07-02 22:56:22 -07002494 }
Dan Williams4711ba12011-03-11 10:43:57 -08002495 }
2496
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002497 status = scic_oem_parameters_set(&isci_host->sci, &oem);
Dan Williams4711ba12011-03-11 10:43:57 -08002498 if (status != SCI_SUCCESS) {
2499 dev_warn(&isci_host->pdev->dev,
2500 "%s: scic_oem_parameters_set failed\n",
2501 __func__);
2502 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002503 }
2504
Dan Williams6f231dd2011-07-02 22:56:22 -07002505 tasklet_init(&isci_host->completion_tasklet,
Dan Williamsc7ef4032011-02-18 09:25:05 -08002506 isci_host_completion_routine, (unsigned long)isci_host);
Dan Williams6f231dd2011-07-02 22:56:22 -07002507
Dan Williams6f231dd2011-07-02 22:56:22 -07002508 INIT_LIST_HEAD(&isci_host->requests_to_complete);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002509 INIT_LIST_HEAD(&isci_host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002510
Dan Williams7c40a802011-03-02 11:49:26 -08002511 spin_lock_irq(&isci_host->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002512 status = scic_controller_initialize(&isci_host->sci);
Dan Williams7c40a802011-03-02 11:49:26 -08002513 spin_unlock_irq(&isci_host->scic_lock);
2514 if (status != SCI_SUCCESS) {
2515 dev_warn(&isci_host->pdev->dev,
2516 "%s: scic_controller_initialize failed -"
2517 " status = 0x%x\n",
2518 __func__, status);
2519 return -ENODEV;
2520 }
2521
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002522 err = scic_controller_mem_init(&isci_host->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07002523 if (err)
Dave Jiang858d4aa2011-02-22 01:27:03 -08002524 return err;
Dan Williams6f231dd2011-07-02 22:56:22 -07002525
Dan Williams6f231dd2011-07-02 22:56:22 -07002526 isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
Dan Williams67ea8382011-05-08 11:47:15 -07002527 sizeof(struct isci_request),
Dan Williams6f231dd2011-07-02 22:56:22 -07002528 SLAB_HWCACHE_ALIGN, 0);
2529
Dave Jiang858d4aa2011-02-22 01:27:03 -08002530 if (!isci_host->dma_pool)
2531 return -ENOMEM;
Dan Williams6f231dd2011-07-02 22:56:22 -07002532
Dan Williamsd9c37392011-03-03 17:59:32 -08002533 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williamse5313812011-05-07 10:11:43 -07002534 isci_port_init(&isci_host->ports[i], isci_host, i);
Dan Williams6f231dd2011-07-02 22:56:22 -07002535
Dan Williamsd9c37392011-03-03 17:59:32 -08002536 for (i = 0; i < SCI_MAX_PHYS; i++)
2537 isci_phy_init(&isci_host->phys[i], isci_host, i);
2538
2539 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07002540 struct isci_remote_device *idev = &isci_host->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08002541
2542 INIT_LIST_HEAD(&idev->reqs_in_process);
2543 INIT_LIST_HEAD(&idev->node);
2544 spin_lock_init(&idev->state_lock);
2545 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002546
Dave Jiang858d4aa2011-02-22 01:27:03 -08002547 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -07002548}
Dan Williamscc9203b2011-05-08 17:34:44 -07002549
2550void scic_sds_controller_link_up(struct scic_sds_controller *scic,
2551 struct scic_sds_port *port, struct scic_sds_phy *phy)
2552{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002553 switch (scic->sm.current_state_id) {
2554 case SCIC_STARTING:
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002555 sci_del_timer(&scic->phy_timer);
2556 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002557 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2558 port, phy);
2559 scic_sds_controller_start_next_phy(scic);
2560 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002561 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002562 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2563 port, phy);
2564 break;
2565 default:
2566 dev_dbg(scic_to_dev(scic),
2567 "%s: SCIC Controller linkup event from phy %d in "
2568 "unexpected state %d\n", __func__, phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002569 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002570 }
2571}
2572
2573void scic_sds_controller_link_down(struct scic_sds_controller *scic,
2574 struct scic_sds_port *port, struct scic_sds_phy *phy)
2575{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002576 switch (scic->sm.current_state_id) {
2577 case SCIC_STARTING:
2578 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002579 scic->port_agent.link_down_handler(scic, &scic->port_agent,
2580 port, phy);
2581 break;
2582 default:
2583 dev_dbg(scic_to_dev(scic),
2584 "%s: SCIC Controller linkdown event from phy %d in "
2585 "unexpected state %d\n",
2586 __func__,
2587 phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002588 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002589 }
2590}
2591
2592/**
2593 * This is a helper method to determine if any remote devices on this
2594 * controller are still in the stopping state.
2595 *
2596 */
2597static bool scic_sds_controller_has_remote_devices_stopping(
2598 struct scic_sds_controller *controller)
2599{
2600 u32 index;
2601
2602 for (index = 0; index < controller->remote_node_entries; index++) {
2603 if ((controller->device_table[index] != NULL) &&
Edmund Nadolskie3013702011-06-02 00:10:43 +00002604 (controller->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
Dan Williamscc9203b2011-05-08 17:34:44 -07002605 return true;
2606 }
2607
2608 return false;
2609}
2610
2611/**
2612 * This method is called by the remote device to inform the controller
2613 * object that the remote device has stopped.
2614 */
2615void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
2616 struct scic_sds_remote_device *sci_dev)
2617{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002618 if (scic->sm.current_state_id != SCIC_STOPPING) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002619 dev_dbg(scic_to_dev(scic),
2620 "SCIC Controller 0x%p remote device stopped event "
2621 "from device 0x%p in unexpected state %d\n",
2622 scic, sci_dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002623 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002624 return;
2625 }
2626
2627 if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002628 sci_change_state(&scic->sm, SCIC_STOPPED);
Dan Williamscc9203b2011-05-08 17:34:44 -07002629 }
2630}
2631
2632/**
2633 * This method will write to the SCU PCP register the request value. The method
2634 * is used to suspend/resume ports, devices, and phys.
2635 * @scic:
2636 *
2637 *
2638 */
2639void scic_sds_controller_post_request(
2640 struct scic_sds_controller *scic,
2641 u32 request)
2642{
2643 dev_dbg(scic_to_dev(scic),
2644 "%s: SCIC Controller 0x%p post request 0x%08x\n",
2645 __func__,
2646 scic,
2647 request);
2648
2649 writel(request, &scic->smu_registers->post_context_port);
2650}
2651
2652/**
2653 * This method will copy the soft copy of the task context into the physical
2654 * memory accessible by the controller.
2655 * @scic: This parameter specifies the controller for which to copy
2656 * the task context.
2657 * @sci_req: This parameter specifies the request for which the task
2658 * context is being copied.
2659 *
2660 * After this call is made the SCIC_SDS_IO_REQUEST object will always point to
2661 * the physical memory version of the task context. Thus, all subsequent
2662 * updates to the task context are performed in the TC table (i.e. DMAable
2663 * memory). none
2664 */
2665void scic_sds_controller_copy_task_context(
2666 struct scic_sds_controller *scic,
2667 struct scic_sds_request *sci_req)
2668{
2669 struct scu_task_context *task_context_buffer;
2670
2671 task_context_buffer = scic_sds_controller_get_task_context_buffer(
2672 scic, sci_req->io_tag);
2673
2674 memcpy(task_context_buffer,
2675 sci_req->task_context_buffer,
2676 offsetof(struct scu_task_context, sgl_snapshot_ac));
2677
2678 /*
2679 * Now that the soft copy of the TC has been copied into the TC
2680 * table accessible by the silicon. Thus, any further changes to
2681 * the TC (e.g. TC termination) occur in the appropriate location. */
2682 sci_req->task_context_buffer = task_context_buffer;
2683}
2684
2685/**
2686 * This method returns the task context buffer for the given io tag.
2687 * @scic:
2688 * @io_tag:
2689 *
2690 * struct scu_task_context*
2691 */
2692struct scu_task_context *scic_sds_controller_get_task_context_buffer(
2693 struct scic_sds_controller *scic,
2694 u16 io_tag
2695 ) {
2696 u16 task_index = scic_sds_io_tag_get_index(io_tag);
2697
2698 if (task_index < scic->task_context_entries) {
2699 return &scic->task_context_table[task_index];
2700 }
2701
2702 return NULL;
2703}
2704
2705struct scic_sds_request *scic_request_by_tag(struct scic_sds_controller *scic,
2706 u16 io_tag)
2707{
2708 u16 task_index;
2709 u16 task_sequence;
2710
2711 task_index = scic_sds_io_tag_get_index(io_tag);
2712
2713 if (task_index < scic->task_context_entries) {
2714 if (scic->io_request_table[task_index] != NULL) {
2715 task_sequence = scic_sds_io_tag_get_sequence(io_tag);
2716
2717 if (task_sequence == scic->io_request_sequence[task_index]) {
2718 return scic->io_request_table[task_index];
2719 }
2720 }
2721 }
2722
2723 return NULL;
2724}
2725
2726/**
2727 * This method allocates remote node index and the reserves the remote node
2728 * context space for use. This method can fail if there are no more remote
2729 * node index available.
2730 * @scic: This is the controller object which contains the set of
2731 * free remote node ids
2732 * @sci_dev: This is the device object which is requesting the a remote node
2733 * id
2734 * @node_id: This is the remote node id that is assinged to the device if one
2735 * is available
2736 *
2737 * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2738 * node index available.
2739 */
2740enum sci_status scic_sds_controller_allocate_remote_node_context(
2741 struct scic_sds_controller *scic,
2742 struct scic_sds_remote_device *sci_dev,
2743 u16 *node_id)
2744{
2745 u16 node_index;
2746 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2747
2748 node_index = scic_sds_remote_node_table_allocate_remote_node(
2749 &scic->available_remote_nodes, remote_node_count
2750 );
2751
2752 if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2753 scic->device_table[node_index] = sci_dev;
2754
2755 *node_id = node_index;
2756
2757 return SCI_SUCCESS;
2758 }
2759
2760 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2761}
2762
2763/**
2764 * This method frees the remote node index back to the available pool. Once
2765 * this is done the remote node context buffer is no longer valid and can
2766 * not be used.
2767 * @scic:
2768 * @sci_dev:
2769 * @node_id:
2770 *
2771 */
2772void scic_sds_controller_free_remote_node_context(
2773 struct scic_sds_controller *scic,
2774 struct scic_sds_remote_device *sci_dev,
2775 u16 node_id)
2776{
2777 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2778
2779 if (scic->device_table[node_id] == sci_dev) {
2780 scic->device_table[node_id] = NULL;
2781
2782 scic_sds_remote_node_table_release_remote_node_index(
2783 &scic->available_remote_nodes, remote_node_count, node_id
2784 );
2785 }
2786}
2787
2788/**
2789 * This method returns the union scu_remote_node_context for the specified remote
2790 * node id.
2791 * @scic:
2792 * @node_id:
2793 *
2794 * union scu_remote_node_context*
2795 */
2796union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
2797 struct scic_sds_controller *scic,
2798 u16 node_id
2799 ) {
2800 if (
2801 (node_id < scic->remote_node_entries)
2802 && (scic->device_table[node_id] != NULL)
2803 ) {
2804 return &scic->remote_node_context_table[node_id];
2805 }
2806
2807 return NULL;
2808}
2809
2810/**
2811 *
2812 * @resposne_buffer: This is the buffer into which the D2H register FIS will be
2813 * constructed.
2814 * @frame_header: This is the frame header returned by the hardware.
2815 * @frame_buffer: This is the frame buffer returned by the hardware.
2816 *
2817 * This method will combind the frame header and frame buffer to create a SATA
2818 * D2H register FIS none
2819 */
2820void scic_sds_controller_copy_sata_response(
2821 void *response_buffer,
2822 void *frame_header,
2823 void *frame_buffer)
2824{
2825 memcpy(response_buffer, frame_header, sizeof(u32));
2826
2827 memcpy(response_buffer + sizeof(u32),
2828 frame_buffer,
2829 sizeof(struct dev_to_host_fis) - sizeof(u32));
2830}
2831
2832/**
2833 * This method releases the frame once this is done the frame is available for
2834 * re-use by the hardware. The data contained in the frame header and frame
2835 * buffer is no longer valid. The UF queue get pointer is only updated if UF
2836 * control indicates this is appropriate.
2837 * @scic:
2838 * @frame_index:
2839 *
2840 */
2841void scic_sds_controller_release_frame(
2842 struct scic_sds_controller *scic,
2843 u32 frame_index)
2844{
2845 if (scic_sds_unsolicited_frame_control_release_frame(
2846 &scic->uf_control, frame_index) == true)
2847 writel(scic->uf_control.get,
2848 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
2849}
2850
2851/**
2852 * scic_controller_start_io() - This method is called by the SCI user to
2853 * send/start an IO request. If the method invocation is successful, then
2854 * the IO request has been queued to the hardware for processing.
2855 * @controller: the handle to the controller object for which to start an IO
2856 * request.
2857 * @remote_device: the handle to the remote device object for which to start an
2858 * IO request.
2859 * @io_request: the handle to the io request object to start.
2860 * @io_tag: This parameter specifies a previously allocated IO tag that the
2861 * user desires to be utilized for this request. This parameter is optional.
2862 * The user is allowed to supply SCI_CONTROLLER_INVALID_IO_TAG as the value
2863 * for this parameter.
2864 *
2865 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2866 * to ensure that each of the methods that may allocate or free available IO
2867 * tags are handled in a mutually exclusive manner. This method is one of said
2868 * methods requiring proper critical code section protection (e.g. semaphore,
2869 * spin-lock, etc.). - For SATA, the user is required to manage NCQ tags. As a
2870 * result, it is expected the user will have set the NCQ tag field in the host
2871 * to device register FIS prior to calling this method. There is also a
2872 * requirement for the user to call scic_stp_io_set_ncq_tag() prior to invoking
2873 * the scic_controller_start_io() method. scic_controller_allocate_tag() for
2874 * more information on allocating a tag. Indicate if the controller
2875 * successfully started the IO request. SCI_SUCCESS if the IO request was
2876 * successfully started. Determine the failure situations and return values.
2877 */
2878enum sci_status scic_controller_start_io(
2879 struct scic_sds_controller *scic,
2880 struct scic_sds_remote_device *rdev,
2881 struct scic_sds_request *req,
2882 u16 io_tag)
2883{
2884 enum sci_status status;
2885
Edmund Nadolskie3013702011-06-02 00:10:43 +00002886 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002887 dev_warn(scic_to_dev(scic), "invalid state to start I/O");
2888 return SCI_FAILURE_INVALID_STATE;
2889 }
2890
2891 status = scic_sds_remote_device_start_io(scic, rdev, req);
2892 if (status != SCI_SUCCESS)
2893 return status;
2894
2895 scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
2896 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(req));
2897 return SCI_SUCCESS;
2898}
2899
2900/**
2901 * scic_controller_terminate_request() - This method is called by the SCI Core
2902 * user to terminate an ongoing (i.e. started) core IO request. This does
2903 * not abort the IO request at the target, but rather removes the IO request
2904 * from the host controller.
2905 * @controller: the handle to the controller object for which to terminate a
2906 * request.
2907 * @remote_device: the handle to the remote device object for which to
2908 * terminate a request.
2909 * @request: the handle to the io or task management request object to
2910 * terminate.
2911 *
2912 * Indicate if the controller successfully began the terminate process for the
2913 * IO request. SCI_SUCCESS if the terminate process was successfully started
2914 * for the request. Determine the failure situations and return values.
2915 */
2916enum sci_status scic_controller_terminate_request(
2917 struct scic_sds_controller *scic,
2918 struct scic_sds_remote_device *rdev,
2919 struct scic_sds_request *req)
2920{
2921 enum sci_status status;
2922
Edmund Nadolskie3013702011-06-02 00:10:43 +00002923 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002924 dev_warn(scic_to_dev(scic),
2925 "invalid state to terminate request\n");
2926 return SCI_FAILURE_INVALID_STATE;
2927 }
2928
2929 status = scic_sds_io_request_terminate(req);
2930 if (status != SCI_SUCCESS)
2931 return status;
2932
2933 /*
2934 * Utilize the original post context command and or in the POST_TC_ABORT
2935 * request sub-type.
2936 */
2937 scic_sds_controller_post_request(scic,
2938 scic_sds_request_get_post_context(req) |
2939 SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2940 return SCI_SUCCESS;
2941}
2942
2943/**
2944 * scic_controller_complete_io() - This method will perform core specific
2945 * completion operations for an IO request. After this method is invoked,
2946 * the user should consider the IO request as invalid until it is properly
2947 * reused (i.e. re-constructed).
2948 * @controller: The handle to the controller object for which to complete the
2949 * IO request.
2950 * @remote_device: The handle to the remote device object for which to complete
2951 * the IO request.
2952 * @io_request: the handle to the io request object to complete.
2953 *
2954 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2955 * to ensure that each of the methods that may allocate or free available IO
2956 * tags are handled in a mutually exclusive manner. This method is one of said
2957 * methods requiring proper critical code section protection (e.g. semaphore,
2958 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
2959 * Core user, using the scic_controller_allocate_io_tag() method, then it is
2960 * the responsibility of the caller to invoke the scic_controller_free_io_tag()
2961 * method to free the tag (i.e. this method will not free the IO tag). Indicate
2962 * if the controller successfully completed the IO request. SCI_SUCCESS if the
2963 * completion process was successful.
2964 */
2965enum sci_status scic_controller_complete_io(
2966 struct scic_sds_controller *scic,
2967 struct scic_sds_remote_device *rdev,
2968 struct scic_sds_request *request)
2969{
2970 enum sci_status status;
2971 u16 index;
2972
Edmund Nadolskie3013702011-06-02 00:10:43 +00002973 switch (scic->sm.current_state_id) {
2974 case SCIC_STOPPING:
Dan Williamscc9203b2011-05-08 17:34:44 -07002975 /* XXX: Implement this function */
2976 return SCI_FAILURE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002977 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002978 status = scic_sds_remote_device_complete_io(scic, rdev, request);
2979 if (status != SCI_SUCCESS)
2980 return status;
2981
2982 index = scic_sds_io_tag_get_index(request->io_tag);
2983 scic->io_request_table[index] = NULL;
2984 return SCI_SUCCESS;
2985 default:
2986 dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
2987 return SCI_FAILURE_INVALID_STATE;
2988 }
2989
2990}
2991
2992enum sci_status scic_controller_continue_io(struct scic_sds_request *sci_req)
2993{
2994 struct scic_sds_controller *scic = sci_req->owning_controller;
2995
Edmund Nadolskie3013702011-06-02 00:10:43 +00002996 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002997 dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
2998 return SCI_FAILURE_INVALID_STATE;
2999 }
3000
3001 scic->io_request_table[scic_sds_io_tag_get_index(sci_req->io_tag)] = sci_req;
3002 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(sci_req));
3003 return SCI_SUCCESS;
3004}
3005
3006/**
3007 * scic_controller_start_task() - This method is called by the SCIC user to
3008 * send/start a framework task management request.
3009 * @controller: the handle to the controller object for which to start the task
3010 * management request.
3011 * @remote_device: the handle to the remote device object for which to start
3012 * the task management request.
3013 * @task_request: the handle to the task request object to start.
3014 * @io_tag: This parameter specifies a previously allocated IO tag that the
3015 * user desires to be utilized for this request. Note this not the io_tag
3016 * of the request being managed. It is to be utilized for the task request
3017 * itself. This parameter is optional. The user is allowed to supply
3018 * SCI_CONTROLLER_INVALID_IO_TAG as the value for this parameter.
3019 *
3020 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
3021 * to ensure that each of the methods that may allocate or free available IO
3022 * tags are handled in a mutually exclusive manner. This method is one of said
3023 * methods requiring proper critical code section protection (e.g. semaphore,
3024 * spin-lock, etc.). - The user must synchronize this task with completion
3025 * queue processing. If they are not synchronized then it is possible for the
3026 * io requests that are being managed by the task request can complete before
3027 * starting the task request. scic_controller_allocate_tag() for more
3028 * information on allocating a tag. Indicate if the controller successfully
3029 * started the IO request. SCI_TASK_SUCCESS if the task request was
3030 * successfully started. SCI_TASK_FAILURE_REQUIRES_SCSI_ABORT This value is
3031 * returned if there is/are task(s) outstanding that require termination or
3032 * completion before this request can succeed.
3033 */
3034enum sci_task_status scic_controller_start_task(
3035 struct scic_sds_controller *scic,
3036 struct scic_sds_remote_device *rdev,
3037 struct scic_sds_request *req,
3038 u16 task_tag)
3039{
3040 enum sci_status status;
3041
Edmund Nadolskie3013702011-06-02 00:10:43 +00003042 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07003043 dev_warn(scic_to_dev(scic),
3044 "%s: SCIC Controller starting task from invalid "
3045 "state\n",
3046 __func__);
3047 return SCI_TASK_FAILURE_INVALID_STATE;
3048 }
3049
3050 status = scic_sds_remote_device_start_task(scic, rdev, req);
3051 switch (status) {
3052 case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
3053 scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
3054
3055 /*
3056 * We will let framework know this task request started successfully,
3057 * although core is still woring on starting the request (to post tc when
3058 * RNC is resumed.)
3059 */
3060 return SCI_SUCCESS;
3061 case SCI_SUCCESS:
3062 scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
3063
3064 scic_sds_controller_post_request(scic,
3065 scic_sds_request_get_post_context(req));
3066 break;
3067 default:
3068 break;
3069 }
3070
3071 return status;
3072}
3073
3074/**
3075 * scic_controller_allocate_io_tag() - This method will allocate a tag from the
3076 * pool of free IO tags. Direct allocation of IO tags by the SCI Core user
3077 * is optional. The scic_controller_start_io() method will allocate an IO
3078 * tag if this method is not utilized and the tag is not supplied to the IO
3079 * construct routine. Direct allocation of IO tags may provide additional
3080 * performance improvements in environments capable of supporting this usage
3081 * model. Additionally, direct allocation of IO tags also provides
3082 * additional flexibility to the SCI Core user. Specifically, the user may
3083 * retain IO tags across the lives of multiple IO requests.
3084 * @controller: the handle to the controller object for which to allocate the
3085 * tag.
3086 *
3087 * IO tags are a protected resource. It is incumbent upon the SCI Core user to
3088 * ensure that each of the methods that may allocate or free available IO tags
3089 * are handled in a mutually exclusive manner. This method is one of said
3090 * methods requiring proper critical code section protection (e.g. semaphore,
3091 * spin-lock, etc.). An unsigned integer representing an available IO tag.
3092 * SCI_CONTROLLER_INVALID_IO_TAG This value is returned if there are no
3093 * currently available tags to be allocated. All return other values indicate a
3094 * legitimate tag.
3095 */
Dan Williamsac668c62011-06-07 18:50:55 -07003096u16 scic_controller_allocate_io_tag(struct scic_sds_controller *scic)
Dan Williamscc9203b2011-05-08 17:34:44 -07003097{
Dan Williamsac668c62011-06-07 18:50:55 -07003098 struct isci_host *ihost = scic_to_ihost(scic);
3099 u16 tci;
3100 u16 seq;
Dan Williamscc9203b2011-05-08 17:34:44 -07003101
Dan Williamsac668c62011-06-07 18:50:55 -07003102 if (isci_tci_space(ihost)) {
3103 tci = isci_tci_alloc(ihost);
3104 seq = scic->io_request_sequence[tci];
Dan Williamscc9203b2011-05-08 17:34:44 -07003105
Dan Williamsac668c62011-06-07 18:50:55 -07003106 return scic_sds_io_tag_construct(seq, tci);
Dan Williamscc9203b2011-05-08 17:34:44 -07003107 }
3108
3109 return SCI_CONTROLLER_INVALID_IO_TAG;
3110}
3111
3112/**
3113 * scic_controller_free_io_tag() - This method will free an IO tag to the pool
3114 * of free IO tags. This method provides the SCI Core user more flexibility
3115 * with regards to IO tags. The user may desire to keep an IO tag after an
3116 * IO request has completed, because they plan on re-using the tag for a
3117 * subsequent IO request. This method is only legal if the tag was
3118 * allocated via scic_controller_allocate_io_tag().
3119 * @controller: This parameter specifies the handle to the controller object
3120 * for which to free/return the tag.
3121 * @io_tag: This parameter represents the tag to be freed to the pool of
3122 * available tags.
3123 *
3124 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
3125 * to ensure that each of the methods that may allocate or free available IO
3126 * tags are handled in a mutually exclusive manner. This method is one of said
3127 * methods requiring proper critical code section protection (e.g. semaphore,
3128 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
3129 * Core user, using the scic_controller_allocate_io_tag() method, then it is
3130 * the responsibility of the caller to invoke this method to free the tag. This
3131 * method returns an indication of whether the tag was successfully put back
3132 * (freed) to the pool of available tags. SCI_SUCCESS This return value
3133 * indicates the tag was successfully placed into the pool of available IO
3134 * tags. SCI_FAILURE_INVALID_IO_TAG This value is returned if the supplied tag
3135 * is not a valid IO tag value.
3136 */
Dan Williamsac668c62011-06-07 18:50:55 -07003137enum sci_status scic_controller_free_io_tag(struct scic_sds_controller *scic,
3138 u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07003139{
Dan Williamsac668c62011-06-07 18:50:55 -07003140 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamscc9203b2011-05-08 17:34:44 -07003141 u16 sequence;
3142 u16 index;
3143
3144 BUG_ON(io_tag == SCI_CONTROLLER_INVALID_IO_TAG);
3145
3146 sequence = scic_sds_io_tag_get_sequence(io_tag);
3147 index = scic_sds_io_tag_get_index(io_tag);
3148
Dan Williamsac668c62011-06-07 18:50:55 -07003149 /* prevent tail from passing head */
3150 if (isci_tci_active(ihost) == 0)
3151 return SCI_FAILURE_INVALID_IO_TAG;
Dan Williamscc9203b2011-05-08 17:34:44 -07003152
Dan Williamsac668c62011-06-07 18:50:55 -07003153 if (sequence == scic->io_request_sequence[index]) {
3154 scic_sds_io_sequence_increment(scic->io_request_sequence[index]);
Dan Williamscc9203b2011-05-08 17:34:44 -07003155
Dan Williamsac668c62011-06-07 18:50:55 -07003156 isci_tci_free(ihost, index);
3157
3158 return SCI_SUCCESS;
Dan Williamscc9203b2011-05-08 17:34:44 -07003159 }
Dan Williamscc9203b2011-05-08 17:34:44 -07003160 return SCI_FAILURE_INVALID_IO_TAG;
3161}