blob: 41a7c5099deaaf7f87d5a398d70ebd80a1035efb [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 Williamscc9203b2011-05-08 17:34:44 -070055#include <linux/device.h>
56#include <scsi/sas.h>
57#include "host.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070059#include "port.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070060#include "host.h"
Dan Williamsd044af12011-03-08 09:52:49 -080061#include "probe_roms.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070062#include "remote_device.h"
63#include "request.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070064#include "scu_completion_codes.h"
65#include "scu_event_codes.h"
Dan Williams63a3a152011-05-08 21:36:46 -070066#include "registers.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070067#include "scu_remote_node_context.h"
68#include "scu_task_context.h"
69#include "scu_unsolicited_frame.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070070
Dan Williamscc9203b2011-05-08 17:34:44 -070071#define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
72
Dan Williams7c78da32011-06-01 16:00:01 -070073#define smu_max_ports(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070074 (\
75 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
76 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
77 )
78
Dan Williams7c78da32011-06-01 16:00:01 -070079#define smu_max_task_contexts(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070080 (\
81 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
82 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
83 )
84
Dan Williams7c78da32011-06-01 16:00:01 -070085#define smu_max_rncs(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070086 (\
87 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
88 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
89 )
90
Dan Williamscc9203b2011-05-08 17:34:44 -070091#define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
92
93/**
94 *
95 *
96 * The number of milliseconds to wait while a given phy is consuming power
97 * before allowing another set of phys to consume power. Ultimately, this will
98 * be specified by OEM parameter.
99 */
100#define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
101
102/**
103 * NORMALIZE_PUT_POINTER() -
104 *
105 * This macro will normalize the completion queue put pointer so its value can
106 * be used as an array inde
107 */
108#define NORMALIZE_PUT_POINTER(x) \
109 ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
110
111
112/**
113 * NORMALIZE_EVENT_POINTER() -
114 *
115 * This macro will normalize the completion queue event entry so its value can
116 * be used as an index.
117 */
118#define NORMALIZE_EVENT_POINTER(x) \
119 (\
120 ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
121 >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
122 )
123
124/**
125 * INCREMENT_COMPLETION_QUEUE_GET() -
126 *
127 * This macro will increment the controllers completion queue index value and
128 * possibly toggle the cycle bit if the completion queue index wraps back to 0.
129 */
130#define INCREMENT_COMPLETION_QUEUE_GET(controller, index, cycle) \
131 INCREMENT_QUEUE_GET(\
132 (index), \
133 (cycle), \
Dan Williams7c78da32011-06-01 16:00:01 -0700134 SCU_MAX_COMPLETION_QUEUE_ENTRIES, \
135 SMU_CQGR_CYCLE_BIT)
Dan Williamscc9203b2011-05-08 17:34:44 -0700136
137/**
138 * INCREMENT_EVENT_QUEUE_GET() -
139 *
140 * This macro will increment the controllers event queue index value and
141 * possibly toggle the event cycle bit if the event queue index wraps back to 0.
142 */
143#define INCREMENT_EVENT_QUEUE_GET(controller, index, cycle) \
144 INCREMENT_QUEUE_GET(\
145 (index), \
146 (cycle), \
Dan Williams7c78da32011-06-01 16:00:01 -0700147 SCU_MAX_EVENTS, \
Dan Williamscc9203b2011-05-08 17:34:44 -0700148 SMU_CQGR_EVENT_CYCLE_BIT \
149 )
150
151
152/**
153 * NORMALIZE_GET_POINTER() -
154 *
155 * This macro will normalize the completion queue get pointer so its value can
156 * be used as an index into an array
157 */
158#define NORMALIZE_GET_POINTER(x) \
159 ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
160
161/**
162 * NORMALIZE_GET_POINTER_CYCLE_BIT() -
163 *
164 * This macro will normalize the completion queue cycle pointer so it matches
165 * the completion queue cycle bit
166 */
167#define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
168 ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
169
170/**
171 * COMPLETION_QUEUE_CYCLE_BIT() -
172 *
173 * This macro will return the cycle bit of the completion queue entry
174 */
175#define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
176
Edmund Nadolski12ef6542011-06-02 00:10:50 +0000177/* Init the state machine and call the state entry function (if any) */
178void sci_init_sm(struct sci_base_state_machine *sm,
179 const struct sci_base_state *state_table, u32 initial_state)
180{
181 sci_state_transition_t handler;
182
183 sm->initial_state_id = initial_state;
184 sm->previous_state_id = initial_state;
185 sm->current_state_id = initial_state;
186 sm->state_table = state_table;
187
188 handler = sm->state_table[initial_state].enter_state;
189 if (handler)
190 handler(sm);
191}
192
193/* Call the state exit fn, update the current state, call the state entry fn */
194void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
195{
196 sci_state_transition_t handler;
197
198 handler = sm->state_table[sm->current_state_id].exit_state;
199 if (handler)
200 handler(sm);
201
202 sm->previous_state_id = sm->current_state_id;
203 sm->current_state_id = next_state;
204
205 handler = sm->state_table[sm->current_state_id].enter_state;
206 if (handler)
207 handler(sm);
208}
209
Dan Williamscc9203b2011-05-08 17:34:44 -0700210static bool scic_sds_controller_completion_queue_has_entries(
211 struct scic_sds_controller *scic)
212{
213 u32 get_value = scic->completion_queue_get;
214 u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
215
216 if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
217 COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
218 return true;
219
220 return false;
221}
222
223static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
224{
225 if (scic_sds_controller_completion_queue_has_entries(scic)) {
226 return true;
227 } else {
228 /*
229 * we have a spurious interrupt it could be that we have already
230 * emptied the completion queue from a previous interrupt */
231 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
232
233 /*
234 * There is a race in the hardware that could cause us not to be notified
235 * of an interrupt completion if we do not take this step. We will mask
236 * then unmask the interrupts so if there is another interrupt pending
237 * the clearing of the interrupt source we get the next interrupt message. */
238 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
239 writel(0, &scic->smu_registers->interrupt_mask);
240 }
241
242 return false;
243}
244
Dan Williamsc7ef4032011-02-18 09:25:05 -0800245irqreturn_t isci_msix_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700246{
Dan Williamsc7ef4032011-02-18 09:25:05 -0800247 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700248
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000249 if (scic_sds_controller_isr(&ihost->sci))
Dan Williams0cf89d12011-02-18 09:25:07 -0800250 tasklet_schedule(&ihost->completion_tasklet);
Dan Williams6f231dd2011-07-02 22:56:22 -0700251
Dan Williamsc7ef4032011-02-18 09:25:05 -0800252 return IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700253}
254
Dan Williamscc9203b2011-05-08 17:34:44 -0700255static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
256{
257 u32 interrupt_status;
258
259 interrupt_status =
260 readl(&scic->smu_registers->interrupt_status);
261 interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
262
263 if (interrupt_status != 0) {
264 /*
265 * There is an error interrupt pending so let it through and handle
266 * in the callback */
267 return true;
268 }
269
270 /*
271 * There is a race in the hardware that could cause us not to be notified
272 * of an interrupt completion if we do not take this step. We will mask
273 * then unmask the error interrupts so if there was another interrupt
274 * pending we will be notified.
275 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
276 writel(0xff, &scic->smu_registers->interrupt_mask);
277 writel(0, &scic->smu_registers->interrupt_mask);
278
279 return false;
280}
281
282static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
283 u32 completion_entry)
284{
285 u32 index;
286 struct scic_sds_request *io_request;
287
288 index = SCU_GET_COMPLETION_INDEX(completion_entry);
289 io_request = scic->io_request_table[index];
290
291 /* Make sure that we really want to process this IO request */
292 if (
293 (io_request != NULL)
294 && (io_request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG)
295 && (
296 scic_sds_io_tag_get_sequence(io_request->io_tag)
297 == scic->io_request_sequence[index]
298 )
299 ) {
300 /* Yep this is a valid io request pass it along to the io request handler */
301 scic_sds_io_request_tc_completion(io_request, completion_entry);
302 }
303}
304
305static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
306 u32 completion_entry)
307{
308 u32 index;
309 struct scic_sds_request *io_request;
310 struct scic_sds_remote_device *device;
311
312 index = SCU_GET_COMPLETION_INDEX(completion_entry);
313
314 switch (scu_get_command_request_type(completion_entry)) {
315 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
316 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
317 io_request = scic->io_request_table[index];
318 dev_warn(scic_to_dev(scic),
319 "%s: SCIC SDS Completion type SDMA %x for io request "
320 "%p\n",
321 __func__,
322 completion_entry,
323 io_request);
324 /* @todo For a post TC operation we need to fail the IO
325 * request
326 */
327 break;
328
329 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
330 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
331 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
332 device = scic->device_table[index];
333 dev_warn(scic_to_dev(scic),
334 "%s: SCIC SDS Completion type SDMA %x for remote "
335 "device %p\n",
336 __func__,
337 completion_entry,
338 device);
339 /* @todo For a port RNC operation we need to fail the
340 * device
341 */
342 break;
343
344 default:
345 dev_warn(scic_to_dev(scic),
346 "%s: SCIC SDS Completion unknown SDMA completion "
347 "type %x\n",
348 __func__,
349 completion_entry);
350 break;
351
352 }
353}
354
355static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
356 u32 completion_entry)
357{
358 u32 index;
359 u32 frame_index;
360
361 struct isci_host *ihost = scic_to_ihost(scic);
362 struct scu_unsolicited_frame_header *frame_header;
363 struct scic_sds_phy *phy;
364 struct scic_sds_remote_device *device;
365
366 enum sci_status result = SCI_FAILURE;
367
368 frame_index = SCU_GET_FRAME_INDEX(completion_entry);
369
370 frame_header = scic->uf_control.buffers.array[frame_index].header;
371 scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
372
373 if (SCU_GET_FRAME_ERROR(completion_entry)) {
374 /*
375 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
376 * / this cause a problem? We expect the phy initialization will
377 * / fail if there is an error in the frame. */
378 scic_sds_controller_release_frame(scic, frame_index);
379 return;
380 }
381
382 if (frame_header->is_address_frame) {
383 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
384 phy = &ihost->phys[index].sci;
385 result = scic_sds_phy_frame_handler(phy, frame_index);
386 } else {
387
388 index = SCU_GET_COMPLETION_INDEX(completion_entry);
389
390 if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
391 /*
392 * This is a signature fis or a frame from a direct attached SATA
393 * device that has not yet been created. In either case forwared
394 * the frame to the PE and let it take care of the frame data. */
395 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
396 phy = &ihost->phys[index].sci;
397 result = scic_sds_phy_frame_handler(phy, frame_index);
398 } else {
399 if (index < scic->remote_node_entries)
400 device = scic->device_table[index];
401 else
402 device = NULL;
403
404 if (device != NULL)
405 result = scic_sds_remote_device_frame_handler(device, frame_index);
406 else
407 scic_sds_controller_release_frame(scic, frame_index);
408 }
409 }
410
411 if (result != SCI_SUCCESS) {
412 /*
413 * / @todo Is there any reason to report some additional error message
414 * / when we get this failure notifiction? */
415 }
416}
417
418static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
419 u32 completion_entry)
420{
421 struct isci_host *ihost = scic_to_ihost(scic);
422 struct scic_sds_request *io_request;
423 struct scic_sds_remote_device *device;
424 struct scic_sds_phy *phy;
425 u32 index;
426
427 index = SCU_GET_COMPLETION_INDEX(completion_entry);
428
429 switch (scu_get_event_type(completion_entry)) {
430 case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
431 /* / @todo The driver did something wrong and we need to fix the condtion. */
432 dev_err(scic_to_dev(scic),
433 "%s: SCIC Controller 0x%p received SMU command error "
434 "0x%x\n",
435 __func__,
436 scic,
437 completion_entry);
438 break;
439
440 case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
441 case SCU_EVENT_TYPE_SMU_ERROR:
442 case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
443 /*
444 * / @todo This is a hardware failure and its likely that we want to
445 * / reset the controller. */
446 dev_err(scic_to_dev(scic),
447 "%s: SCIC Controller 0x%p received fatal controller "
448 "event 0x%x\n",
449 __func__,
450 scic,
451 completion_entry);
452 break;
453
454 case SCU_EVENT_TYPE_TRANSPORT_ERROR:
455 io_request = scic->io_request_table[index];
456 scic_sds_io_request_event_handler(io_request, completion_entry);
457 break;
458
459 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
460 switch (scu_get_event_specifier(completion_entry)) {
461 case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
462 case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
463 io_request = scic->io_request_table[index];
464 if (io_request != NULL)
465 scic_sds_io_request_event_handler(io_request, completion_entry);
466 else
467 dev_warn(scic_to_dev(scic),
468 "%s: SCIC Controller 0x%p received "
469 "event 0x%x for io request object "
470 "that doesnt exist.\n",
471 __func__,
472 scic,
473 completion_entry);
474
475 break;
476
477 case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
478 device = scic->device_table[index];
479 if (device != NULL)
480 scic_sds_remote_device_event_handler(device, completion_entry);
481 else
482 dev_warn(scic_to_dev(scic),
483 "%s: SCIC Controller 0x%p received "
484 "event 0x%x for remote device object "
485 "that doesnt exist.\n",
486 __func__,
487 scic,
488 completion_entry);
489
490 break;
491 }
492 break;
493
494 case SCU_EVENT_TYPE_BROADCAST_CHANGE:
495 /*
496 * direct the broadcast change event to the phy first and then let
497 * the phy redirect the broadcast change to the port object */
498 case SCU_EVENT_TYPE_ERR_CNT_EVENT:
499 /*
500 * direct error counter event to the phy object since that is where
501 * we get the event notification. This is a type 4 event. */
502 case SCU_EVENT_TYPE_OSSP_EVENT:
503 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
504 phy = &ihost->phys[index].sci;
505 scic_sds_phy_event_handler(phy, completion_entry);
506 break;
507
508 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
509 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
510 case SCU_EVENT_TYPE_RNC_OPS_MISC:
511 if (index < scic->remote_node_entries) {
512 device = scic->device_table[index];
513
514 if (device != NULL)
515 scic_sds_remote_device_event_handler(device, completion_entry);
516 } else
517 dev_err(scic_to_dev(scic),
518 "%s: SCIC Controller 0x%p received event 0x%x "
519 "for remote device object 0x%0x that doesnt "
520 "exist.\n",
521 __func__,
522 scic,
523 completion_entry,
524 index);
525
526 break;
527
528 default:
529 dev_warn(scic_to_dev(scic),
530 "%s: SCIC Controller received unknown event code %x\n",
531 __func__,
532 completion_entry);
533 break;
534 }
535}
536
537
538
539static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
540{
541 u32 completion_count = 0;
542 u32 completion_entry;
543 u32 get_index;
544 u32 get_cycle;
545 u32 event_index;
546 u32 event_cycle;
547
548 dev_dbg(scic_to_dev(scic),
549 "%s: completion queue begining get:0x%08x\n",
550 __func__,
551 scic->completion_queue_get);
552
553 /* Get the component parts of the completion queue */
554 get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
555 get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
556
557 event_index = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
558 event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
559
560 while (
561 NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
562 == COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
563 ) {
564 completion_count++;
565
566 completion_entry = scic->completion_queue[get_index];
567 INCREMENT_COMPLETION_QUEUE_GET(scic, get_index, get_cycle);
568
569 dev_dbg(scic_to_dev(scic),
570 "%s: completion queue entry:0x%08x\n",
571 __func__,
572 completion_entry);
573
574 switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
575 case SCU_COMPLETION_TYPE_TASK:
576 scic_sds_controller_task_completion(scic, completion_entry);
577 break;
578
579 case SCU_COMPLETION_TYPE_SDMA:
580 scic_sds_controller_sdma_completion(scic, completion_entry);
581 break;
582
583 case SCU_COMPLETION_TYPE_UFI:
584 scic_sds_controller_unsolicited_frame(scic, completion_entry);
585 break;
586
587 case SCU_COMPLETION_TYPE_EVENT:
588 INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
589 scic_sds_controller_event_completion(scic, completion_entry);
590 break;
591
592 case SCU_COMPLETION_TYPE_NOTIFY:
593 /*
594 * Presently we do the same thing with a notify event that we do with the
595 * other event codes. */
596 INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
597 scic_sds_controller_event_completion(scic, completion_entry);
598 break;
599
600 default:
601 dev_warn(scic_to_dev(scic),
602 "%s: SCIC Controller received unknown "
603 "completion type %x\n",
604 __func__,
605 completion_entry);
606 break;
607 }
608 }
609
610 /* Update the get register if we completed one or more entries */
611 if (completion_count > 0) {
612 scic->completion_queue_get =
613 SMU_CQGR_GEN_BIT(ENABLE) |
614 SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
615 event_cycle |
616 SMU_CQGR_GEN_VAL(EVENT_POINTER, event_index) |
617 get_cycle |
618 SMU_CQGR_GEN_VAL(POINTER, get_index);
619
620 writel(scic->completion_queue_get,
621 &scic->smu_registers->completion_queue_get);
622
623 }
624
625 dev_dbg(scic_to_dev(scic),
626 "%s: completion queue ending get:0x%08x\n",
627 __func__,
628 scic->completion_queue_get);
629
630}
631
632static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
633{
634 u32 interrupt_status;
635
636 interrupt_status =
637 readl(&scic->smu_registers->interrupt_status);
638
639 if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
640 scic_sds_controller_completion_queue_has_entries(scic)) {
641
642 scic_sds_controller_process_completions(scic);
643 writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
644 } else {
645 dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
646 interrupt_status);
647
Edmund Nadolskie3013702011-06-02 00:10:43 +0000648 sci_change_state(&scic->sm, SCIC_FAILED);
Dan Williamscc9203b2011-05-08 17:34:44 -0700649
650 return;
651 }
652
653 /* If we dont process any completions I am not sure that we want to do this.
654 * We are in the middle of a hardware fault and should probably be reset.
655 */
656 writel(0, &scic->smu_registers->interrupt_mask);
657}
658
Dan Williamsc7ef4032011-02-18 09:25:05 -0800659irqreturn_t isci_intx_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700660{
Dan Williams6f231dd2011-07-02 22:56:22 -0700661 irqreturn_t ret = IRQ_NONE;
Dan Williams31e824e2011-04-19 12:32:51 -0700662 struct isci_host *ihost = data;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000663 struct scic_sds_controller *scic = &ihost->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700664
Dan Williams31e824e2011-04-19 12:32:51 -0700665 if (scic_sds_controller_isr(scic)) {
666 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
667 tasklet_schedule(&ihost->completion_tasklet);
668 ret = IRQ_HANDLED;
669 } else if (scic_sds_controller_error_isr(scic)) {
670 spin_lock(&ihost->scic_lock);
671 scic_sds_controller_error_handler(scic);
672 spin_unlock(&ihost->scic_lock);
673 ret = IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700674 }
Dan Williams92f4f0f2011-02-18 09:25:11 -0800675
Dan Williams6f231dd2011-07-02 22:56:22 -0700676 return ret;
677}
678
Dan Williams92f4f0f2011-02-18 09:25:11 -0800679irqreturn_t isci_error_isr(int vec, void *data)
680{
681 struct isci_host *ihost = data;
Dan Williams92f4f0f2011-02-18 09:25:11 -0800682
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000683 if (scic_sds_controller_error_isr(&ihost->sci))
684 scic_sds_controller_error_handler(&ihost->sci);
Dan Williams92f4f0f2011-02-18 09:25:11 -0800685
686 return IRQ_HANDLED;
687}
Dan Williams6f231dd2011-07-02 22:56:22 -0700688
689/**
690 * isci_host_start_complete() - This function is called by the core library,
691 * through the ISCI Module, to indicate controller start status.
692 * @isci_host: This parameter specifies the ISCI host object
693 * @completion_status: This parameter specifies the completion status from the
694 * core library.
695 *
696 */
Dan Williamscc9203b2011-05-08 17:34:44 -0700697static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700698{
Dan Williams0cf89d12011-02-18 09:25:07 -0800699 if (completion_status != SCI_SUCCESS)
700 dev_info(&ihost->pdev->dev,
701 "controller start timed out, continuing...\n");
702 isci_host_change_state(ihost, isci_ready);
703 clear_bit(IHOST_START_PENDING, &ihost->flags);
704 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700705}
706
Dan Williamsc7ef4032011-02-18 09:25:05 -0800707int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
Dan Williams6f231dd2011-07-02 22:56:22 -0700708{
Dan Williams4393aa42011-03-31 13:10:44 -0700709 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williams6f231dd2011-07-02 22:56:22 -0700710
Edmund Nadolski77950f52011-02-18 09:25:09 -0800711 if (test_bit(IHOST_START_PENDING, &ihost->flags))
Dan Williams6f231dd2011-07-02 22:56:22 -0700712 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700713
Edmund Nadolski77950f52011-02-18 09:25:09 -0800714 /* todo: use sas_flush_discovery once it is upstream */
715 scsi_flush_work(shost);
716
717 scsi_flush_work(shost);
Dan Williams6f231dd2011-07-02 22:56:22 -0700718
Dan Williams0cf89d12011-02-18 09:25:07 -0800719 dev_dbg(&ihost->pdev->dev,
720 "%s: ihost->status = %d, time = %ld\n",
721 __func__, isci_host_get_state(ihost), time);
Dan Williams6f231dd2011-07-02 22:56:22 -0700722
Dan Williams6f231dd2011-07-02 22:56:22 -0700723 return 1;
724
725}
726
Dan Williamscc9203b2011-05-08 17:34:44 -0700727/**
728 * scic_controller_get_suggested_start_timeout() - This method returns the
729 * suggested scic_controller_start() timeout amount. The user is free to
730 * use any timeout value, but this method provides the suggested minimum
731 * start timeout value. The returned value is based upon empirical
732 * information determined as a result of interoperability testing.
733 * @controller: the handle to the controller object for which to return the
734 * suggested start timeout.
735 *
736 * This method returns the number of milliseconds for the suggested start
737 * operation timeout.
738 */
739static u32 scic_controller_get_suggested_start_timeout(
740 struct scic_sds_controller *sc)
741{
742 /* Validate the user supplied parameters. */
743 if (sc == NULL)
744 return 0;
745
746 /*
747 * The suggested minimum timeout value for a controller start operation:
748 *
749 * Signature FIS Timeout
750 * + Phy Start Timeout
751 * + Number of Phy Spin Up Intervals
752 * ---------------------------------
753 * Number of milliseconds for the controller start operation.
754 *
755 * NOTE: The number of phy spin up intervals will be equivalent
756 * to the number of phys divided by the number phys allowed
757 * per interval - 1 (once OEM parameters are supported).
758 * Currently we assume only 1 phy per interval. */
759
760 return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
761 + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
762 + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
763}
764
765static void scic_controller_enable_interrupts(
766 struct scic_sds_controller *scic)
767{
768 BUG_ON(scic->smu_registers == NULL);
769 writel(0, &scic->smu_registers->interrupt_mask);
770}
771
772void scic_controller_disable_interrupts(
773 struct scic_sds_controller *scic)
774{
775 BUG_ON(scic->smu_registers == NULL);
776 writel(0xffffffff, &scic->smu_registers->interrupt_mask);
777}
778
779static void scic_sds_controller_enable_port_task_scheduler(
780 struct scic_sds_controller *scic)
781{
782 u32 port_task_scheduler_value;
783
784 port_task_scheduler_value =
785 readl(&scic->scu_registers->peg0.ptsg.control);
786 port_task_scheduler_value |=
787 (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
788 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
789 writel(port_task_scheduler_value,
790 &scic->scu_registers->peg0.ptsg.control);
791}
792
793static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
794{
795 u32 task_assignment;
796
797 /*
798 * Assign all the TCs to function 0
799 * TODO: Do we actually need to read this register to write it back?
800 */
801
802 task_assignment =
803 readl(&scic->smu_registers->task_context_assignment[0]);
804
805 task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
806 (SMU_TCA_GEN_VAL(ENDING, scic->task_context_entries - 1)) |
807 (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
808
809 writel(task_assignment,
810 &scic->smu_registers->task_context_assignment[0]);
811
812}
813
814static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
815{
816 u32 index;
817 u32 completion_queue_control_value;
818 u32 completion_queue_get_value;
819 u32 completion_queue_put_value;
820
821 scic->completion_queue_get = 0;
822
Dan Williams7c78da32011-06-01 16:00:01 -0700823 completion_queue_control_value =
824 (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
825 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
Dan Williamscc9203b2011-05-08 17:34:44 -0700826
827 writel(completion_queue_control_value,
828 &scic->smu_registers->completion_queue_control);
829
830
831 /* Set the completion queue get pointer and enable the queue */
832 completion_queue_get_value = (
833 (SMU_CQGR_GEN_VAL(POINTER, 0))
834 | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
835 | (SMU_CQGR_GEN_BIT(ENABLE))
836 | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
837 );
838
839 writel(completion_queue_get_value,
840 &scic->smu_registers->completion_queue_get);
841
842 /* Set the completion queue put pointer */
843 completion_queue_put_value = (
844 (SMU_CQPR_GEN_VAL(POINTER, 0))
845 | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
846 );
847
848 writel(completion_queue_put_value,
849 &scic->smu_registers->completion_queue_put);
850
851 /* Initialize the cycle bit of the completion queue entries */
Dan Williams7c78da32011-06-01 16:00:01 -0700852 for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700853 /*
854 * If get.cycle_bit != completion_queue.cycle_bit
855 * its not a valid completion queue entry
856 * so at system start all entries are invalid */
857 scic->completion_queue[index] = 0x80000000;
858 }
859}
860
861static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
862{
863 u32 frame_queue_control_value;
864 u32 frame_queue_get_value;
865 u32 frame_queue_put_value;
866
867 /* Write the queue size */
868 frame_queue_control_value =
Dan Williams7c78da32011-06-01 16:00:01 -0700869 SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
Dan Williamscc9203b2011-05-08 17:34:44 -0700870
871 writel(frame_queue_control_value,
872 &scic->scu_registers->sdma.unsolicited_frame_queue_control);
873
874 /* Setup the get pointer for the unsolicited frame queue */
875 frame_queue_get_value = (
876 SCU_UFQGP_GEN_VAL(POINTER, 0)
877 | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
878 );
879
880 writel(frame_queue_get_value,
881 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
882 /* Setup the put pointer for the unsolicited frame queue */
883 frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
884 writel(frame_queue_put_value,
885 &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
886}
887
888/**
889 * This method will attempt to transition into the ready state for the
890 * controller and indicate that the controller start operation has completed
891 * if all criteria are met.
892 * @scic: This parameter indicates the controller object for which
893 * to transition to ready.
894 * @status: This parameter indicates the status value to be pass into the call
895 * to scic_cb_controller_start_complete().
896 *
897 * none.
898 */
899static void scic_sds_controller_transition_to_ready(
900 struct scic_sds_controller *scic,
901 enum sci_status status)
902{
903 struct isci_host *ihost = scic_to_ihost(scic);
904
Edmund Nadolskie3013702011-06-02 00:10:43 +0000905 if (scic->sm.current_state_id == SCIC_STARTING) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700906 /*
907 * We move into the ready state, because some of the phys/ports
908 * may be up and operational.
909 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000910 sci_change_state(&scic->sm, SCIC_READY);
Dan Williamscc9203b2011-05-08 17:34:44 -0700911
912 isci_host_start_complete(ihost, status);
913 }
914}
915
Adam Gruchala4a33c522011-05-10 23:54:23 +0000916static bool is_phy_starting(struct scic_sds_phy *sci_phy)
917{
918 enum scic_sds_phy_states state;
919
Edmund Nadolskie3013702011-06-02 00:10:43 +0000920 state = sci_phy->sm.current_state_id;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000921 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000922 case SCI_PHY_STARTING:
923 case SCI_PHY_SUB_INITIAL:
924 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
925 case SCI_PHY_SUB_AWAIT_IAF_UF:
926 case SCI_PHY_SUB_AWAIT_SAS_POWER:
927 case SCI_PHY_SUB_AWAIT_SATA_POWER:
928 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
929 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
930 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
931 case SCI_PHY_SUB_FINAL:
Adam Gruchala4a33c522011-05-10 23:54:23 +0000932 return true;
933 default:
934 return false;
935 }
936}
937
Dan Williamscc9203b2011-05-08 17:34:44 -0700938/**
939 * scic_sds_controller_start_next_phy - start phy
940 * @scic: controller
941 *
942 * If all the phys have been started, then attempt to transition the
943 * controller to the READY state and inform the user
944 * (scic_cb_controller_start_complete()).
945 */
946static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
947{
948 struct isci_host *ihost = scic_to_ihost(scic);
949 struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
950 struct scic_sds_phy *sci_phy;
951 enum sci_status status;
952
953 status = SCI_SUCCESS;
954
955 if (scic->phy_startup_timer_pending)
956 return status;
957
958 if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
959 bool is_controller_start_complete = true;
960 u32 state;
961 u8 index;
962
963 for (index = 0; index < SCI_MAX_PHYS; index++) {
964 sci_phy = &ihost->phys[index].sci;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000965 state = sci_phy->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -0700966
Dan Williams4f20ef42011-05-12 06:00:31 -0700967 if (!phy_get_non_dummy_port(sci_phy))
Dan Williamscc9203b2011-05-08 17:34:44 -0700968 continue;
969
970 /* The controller start operation is complete iff:
971 * - all links have been given an opportunity to start
972 * - have no indication of a connected device
973 * - have an indication of a connected device and it has
974 * finished the link training process.
975 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000976 if ((sci_phy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
977 (sci_phy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
978 (sci_phy->is_in_link_training == true && is_phy_starting(sci_phy))) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700979 is_controller_start_complete = false;
980 break;
981 }
982 }
983
984 /*
985 * The controller has successfully finished the start process.
986 * Inform the SCI Core user and transition to the READY state. */
987 if (is_controller_start_complete == true) {
988 scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700989 sci_del_timer(&scic->phy_timer);
990 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -0700991 }
992 } else {
993 sci_phy = &ihost->phys[scic->next_phy_to_start].sci;
994
995 if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dan Williams4f20ef42011-05-12 06:00:31 -0700996 if (phy_get_non_dummy_port(sci_phy) == NULL) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700997 scic->next_phy_to_start++;
998
999 /* Caution recursion ahead be forwarned
1000 *
1001 * The PHY was never added to a PORT in MPC mode
1002 * so start the next phy in sequence This phy
1003 * will never go link up and will not draw power
1004 * the OEM parameters either configured the phy
1005 * incorrectly for the PORT or it was never
1006 * assigned to a PORT
1007 */
1008 return scic_sds_controller_start_next_phy(scic);
1009 }
1010 }
1011
1012 status = scic_sds_phy_start(sci_phy);
1013
1014 if (status == SCI_SUCCESS) {
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001015 sci_mod_timer(&scic->phy_timer,
1016 SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
1017 scic->phy_startup_timer_pending = true;
Dan Williamscc9203b2011-05-08 17:34:44 -07001018 } else {
1019 dev_warn(scic_to_dev(scic),
1020 "%s: Controller stop operation failed "
1021 "to stop phy %d because of status "
1022 "%d.\n",
1023 __func__,
1024 ihost->phys[scic->next_phy_to_start].sci.phy_index,
1025 status);
1026 }
1027
1028 scic->next_phy_to_start++;
1029 }
1030
1031 return status;
1032}
1033
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001034static void phy_startup_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001035{
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001036 struct sci_timer *tmr = (struct sci_timer *)data;
1037 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), phy_timer);
1038 struct isci_host *ihost = scic_to_ihost(scic);
1039 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001040 enum sci_status status;
1041
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001042 spin_lock_irqsave(&ihost->scic_lock, flags);
1043
1044 if (tmr->cancel)
1045 goto done;
1046
Dan Williamscc9203b2011-05-08 17:34:44 -07001047 scic->phy_startup_timer_pending = false;
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001048
1049 do {
Dan Williamscc9203b2011-05-08 17:34:44 -07001050 status = scic_sds_controller_start_next_phy(scic);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001051 } while (status != SCI_SUCCESS);
1052
1053done:
1054 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001055}
1056
1057static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
1058 u32 timeout)
1059{
1060 struct isci_host *ihost = scic_to_ihost(scic);
1061 enum sci_status result;
1062 u16 index;
1063
Edmund Nadolskie3013702011-06-02 00:10:43 +00001064 if (scic->sm.current_state_id != SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001065 dev_warn(scic_to_dev(scic),
1066 "SCIC Controller start operation requested in "
1067 "invalid state\n");
1068 return SCI_FAILURE_INVALID_STATE;
1069 }
1070
1071 /* Build the TCi free pool */
1072 sci_pool_initialize(scic->tci_pool);
1073 for (index = 0; index < scic->task_context_entries; index++)
1074 sci_pool_put(scic->tci_pool, index);
1075
1076 /* Build the RNi free pool */
1077 scic_sds_remote_node_table_initialize(
1078 &scic->available_remote_nodes,
1079 scic->remote_node_entries);
1080
1081 /*
1082 * Before anything else lets make sure we will not be
1083 * interrupted by the hardware.
1084 */
1085 scic_controller_disable_interrupts(scic);
1086
1087 /* Enable the port task scheduler */
1088 scic_sds_controller_enable_port_task_scheduler(scic);
1089
1090 /* Assign all the task entries to scic physical function */
1091 scic_sds_controller_assign_task_entries(scic);
1092
1093 /* Now initialize the completion queue */
1094 scic_sds_controller_initialize_completion_queue(scic);
1095
1096 /* Initialize the unsolicited frame queue for use */
1097 scic_sds_controller_initialize_unsolicited_frame_queue(scic);
1098
1099 /* Start all of the ports on this controller */
1100 for (index = 0; index < scic->logical_port_entries; index++) {
1101 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1102
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001103 result = scic_sds_port_start(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001104 if (result)
1105 return result;
1106 }
1107
1108 scic_sds_controller_start_next_phy(scic);
1109
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001110 sci_mod_timer(&scic->timer, timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07001111
Edmund Nadolskie3013702011-06-02 00:10:43 +00001112 sci_change_state(&scic->sm, SCIC_STARTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001113
1114 return SCI_SUCCESS;
1115}
1116
Dan Williams6f231dd2011-07-02 22:56:22 -07001117void isci_host_scan_start(struct Scsi_Host *shost)
1118{
Dan Williams4393aa42011-03-31 13:10:44 -07001119 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001120 unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001121
Dan Williams0cf89d12011-02-18 09:25:07 -08001122 set_bit(IHOST_START_PENDING, &ihost->flags);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001123
1124 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001125 scic_controller_start(&ihost->sci, tmo);
1126 scic_controller_enable_interrupts(&ihost->sci);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001127 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001128}
1129
Dan Williamscc9203b2011-05-08 17:34:44 -07001130static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001131{
Dan Williams0cf89d12011-02-18 09:25:07 -08001132 isci_host_change_state(ihost, isci_stopped);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001133 scic_controller_disable_interrupts(&ihost->sci);
Dan Williams0cf89d12011-02-18 09:25:07 -08001134 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
1135 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001136}
1137
Dan Williamscc9203b2011-05-08 17:34:44 -07001138static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
1139{
1140 /* Empty out the completion queue */
1141 if (scic_sds_controller_completion_queue_has_entries(scic))
1142 scic_sds_controller_process_completions(scic);
1143
1144 /* Clear the interrupt and enable all interrupts again */
1145 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
1146 /* Could we write the value of SMU_ISR_COMPLETION? */
1147 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
1148 writel(0, &scic->smu_registers->interrupt_mask);
1149}
1150
Dan Williams6f231dd2011-07-02 22:56:22 -07001151/**
1152 * isci_host_completion_routine() - This function is the delayed service
1153 * routine that calls the sci core library's completion handler. It's
1154 * scheduled as a tasklet from the interrupt service routine when interrupts
1155 * in use, or set as the timeout function in polled mode.
1156 * @data: This parameter specifies the ISCI host object
1157 *
1158 */
1159static void isci_host_completion_routine(unsigned long data)
1160{
1161 struct isci_host *isci_host = (struct isci_host *)data;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001162 struct list_head completed_request_list;
1163 struct list_head errored_request_list;
1164 struct list_head *current_position;
1165 struct list_head *next_position;
Dan Williams6f231dd2011-07-02 22:56:22 -07001166 struct isci_request *request;
1167 struct isci_request *next_request;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001168 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -07001169
1170 INIT_LIST_HEAD(&completed_request_list);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001171 INIT_LIST_HEAD(&errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001172
1173 spin_lock_irq(&isci_host->scic_lock);
1174
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001175 scic_sds_controller_completion_handler(&isci_host->sci);
Dan Williamsc7ef4032011-02-18 09:25:05 -08001176
Dan Williams6f231dd2011-07-02 22:56:22 -07001177 /* Take the lists of completed I/Os from the host. */
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001178
Dan Williams6f231dd2011-07-02 22:56:22 -07001179 list_splice_init(&isci_host->requests_to_complete,
1180 &completed_request_list);
1181
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001182 /* Take the list of errored I/Os from the host. */
1183 list_splice_init(&isci_host->requests_to_errorback,
1184 &errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001185
1186 spin_unlock_irq(&isci_host->scic_lock);
1187
1188 /* Process any completions in the lists. */
1189 list_for_each_safe(current_position, next_position,
1190 &completed_request_list) {
1191
1192 request = list_entry(current_position, struct isci_request,
1193 completed_node);
1194 task = isci_request_access_task(request);
1195
1196 /* Normal notification (task_done) */
1197 dev_dbg(&isci_host->pdev->dev,
1198 "%s: Normal - request/task = %p/%p\n",
1199 __func__,
1200 request,
1201 task);
1202
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001203 /* Return the task to libsas */
1204 if (task != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001205
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001206 task->lldd_task = NULL;
1207 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1208
1209 /* If the task is already in the abort path,
1210 * the task_done callback cannot be called.
1211 */
1212 task->task_done(task);
1213 }
1214 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001215 /* Free the request object. */
1216 isci_request_free(isci_host, request);
1217 }
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001218 list_for_each_entry_safe(request, next_request, &errored_request_list,
Dan Williams6f231dd2011-07-02 22:56:22 -07001219 completed_node) {
1220
1221 task = isci_request_access_task(request);
1222
1223 /* Use sas_task_abort */
1224 dev_warn(&isci_host->pdev->dev,
1225 "%s: Error - request/task = %p/%p\n",
1226 __func__,
1227 request,
1228 task);
1229
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001230 if (task != NULL) {
1231
1232 /* Put the task into the abort path if it's not there
1233 * already.
1234 */
1235 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
1236 sas_task_abort(task);
1237
1238 } else {
1239 /* This is a case where the request has completed with a
1240 * status such that it needed further target servicing,
1241 * but the sas_task reference has already been removed
1242 * from the request. Since it was errored, it was not
1243 * being aborted, so there is nothing to do except free
1244 * it.
1245 */
1246
1247 spin_lock_irq(&isci_host->scic_lock);
1248 /* Remove the request from the remote device's list
1249 * of pending requests.
1250 */
1251 list_del_init(&request->dev_node);
1252 spin_unlock_irq(&isci_host->scic_lock);
1253
1254 /* Free the request object. */
1255 isci_request_free(isci_host, request);
1256 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001257 }
1258
1259}
1260
Dan Williamscc9203b2011-05-08 17:34:44 -07001261/**
1262 * scic_controller_stop() - This method will stop an individual controller
1263 * object.This method will invoke the associated user callback upon
1264 * completion. The completion callback is called when the following
1265 * conditions are met: -# the method return status is SCI_SUCCESS. -# the
1266 * controller has been quiesced. This method will ensure that all IO
1267 * requests are quiesced, phys are stopped, and all additional operation by
1268 * the hardware is halted.
1269 * @controller: the handle to the controller object to stop.
1270 * @timeout: This parameter specifies the number of milliseconds in which the
1271 * stop operation should complete.
1272 *
1273 * The controller must be in the STARTED or STOPPED state. Indicate if the
1274 * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1275 * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1276 * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1277 * controller is not either in the STARTED or STOPPED states.
1278 */
1279static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
1280 u32 timeout)
1281{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001282 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001283 dev_warn(scic_to_dev(scic),
1284 "SCIC Controller stop operation requested in "
1285 "invalid state\n");
1286 return SCI_FAILURE_INVALID_STATE;
1287 }
1288
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001289 sci_mod_timer(&scic->timer, timeout);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001290 sci_change_state(&scic->sm, SCIC_STOPPING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001291 return SCI_SUCCESS;
1292}
1293
1294/**
1295 * scic_controller_reset() - This method will reset the supplied core
1296 * controller regardless of the state of said controller. This operation is
1297 * considered destructive. In other words, all current operations are wiped
1298 * out. No IO completions for outstanding devices occur. Outstanding IO
1299 * requests are not aborted or completed at the actual remote device.
1300 * @controller: the handle to the controller object to reset.
1301 *
1302 * Indicate if the controller reset method succeeded or failed in some way.
1303 * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1304 * the controller reset operation is unable to complete.
1305 */
1306static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
1307{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001308 switch (scic->sm.current_state_id) {
1309 case SCIC_RESET:
1310 case SCIC_READY:
1311 case SCIC_STOPPED:
1312 case SCIC_FAILED:
Dan Williamscc9203b2011-05-08 17:34:44 -07001313 /*
1314 * The reset operation is not a graceful cleanup, just
1315 * perform the state transition.
1316 */
Edmund Nadolskie3013702011-06-02 00:10:43 +00001317 sci_change_state(&scic->sm, SCIC_RESETTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001318 return SCI_SUCCESS;
1319 default:
1320 dev_warn(scic_to_dev(scic),
1321 "SCIC Controller reset operation requested in "
1322 "invalid state\n");
1323 return SCI_FAILURE_INVALID_STATE;
1324 }
1325}
1326
Dan Williams0cf89d12011-02-18 09:25:07 -08001327void isci_host_deinit(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07001328{
1329 int i;
1330
Dan Williams0cf89d12011-02-18 09:25:07 -08001331 isci_host_change_state(ihost, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001332 for (i = 0; i < SCI_MAX_PORTS; i++) {
Dan Williamse5313812011-05-07 10:11:43 -07001333 struct isci_port *iport = &ihost->ports[i];
Dan Williams0cf89d12011-02-18 09:25:07 -08001334 struct isci_remote_device *idev, *d;
1335
Dan Williamse5313812011-05-07 10:11:43 -07001336 list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
Dan Williams0cf89d12011-02-18 09:25:07 -08001337 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001338 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001339 }
1340 }
1341
Dan Williams0cf89d12011-02-18 09:25:07 -08001342 set_bit(IHOST_STOP_PENDING, &ihost->flags);
Dan Williams7c40a802011-03-02 11:49:26 -08001343
1344 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001345 scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
Dan Williams7c40a802011-03-02 11:49:26 -08001346 spin_unlock_irq(&ihost->scic_lock);
1347
Dan Williams0cf89d12011-02-18 09:25:07 -08001348 wait_for_stop(ihost);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001349 scic_controller_reset(&ihost->sci);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001350
1351 /* Cancel any/all outstanding port timers */
1352 for (i = 0; i < ihost->sci.logical_port_entries; i++) {
1353 struct scic_sds_port *sci_port = &ihost->ports[i].sci;
1354 del_timer_sync(&sci_port->timer.timer);
1355 }
1356
Edmund Nadolskia628d472011-05-19 11:59:36 +00001357 /* Cancel any/all outstanding phy timers */
1358 for (i = 0; i < SCI_MAX_PHYS; i++) {
1359 struct scic_sds_phy *sci_phy = &ihost->phys[i].sci;
1360 del_timer_sync(&sci_phy->sata_timer.timer);
1361 }
1362
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -07001363 del_timer_sync(&ihost->sci.port_agent.timer.timer);
1364
Edmund Nadolski04736612011-05-19 20:17:47 -07001365 del_timer_sync(&ihost->sci.power_control.timer.timer);
1366
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001367 del_timer_sync(&ihost->sci.timer.timer);
1368
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001369 del_timer_sync(&ihost->sci.phy_timer.timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001370}
1371
Dan Williams6f231dd2011-07-02 22:56:22 -07001372static void __iomem *scu_base(struct isci_host *isci_host)
1373{
1374 struct pci_dev *pdev = isci_host->pdev;
1375 int id = isci_host->id;
1376
1377 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
1378}
1379
1380static void __iomem *smu_base(struct isci_host *isci_host)
1381{
1382 struct pci_dev *pdev = isci_host->pdev;
1383 int id = isci_host->id;
1384
1385 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
1386}
1387
Dave Jiangb5f18a22011-03-16 14:57:23 -07001388static void isci_user_parameters_get(
1389 struct isci_host *isci_host,
1390 union scic_user_parameters *scic_user_params)
1391{
1392 struct scic_sds_user_parameters *u = &scic_user_params->sds1;
1393 int i;
1394
1395 for (i = 0; i < SCI_MAX_PHYS; i++) {
1396 struct sci_phy_user_params *u_phy = &u->phys[i];
1397
1398 u_phy->max_speed_generation = phy_gen;
1399
1400 /* we are not exporting these for now */
1401 u_phy->align_insertion_frequency = 0x7f;
1402 u_phy->in_connection_align_insertion_frequency = 0xff;
1403 u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1404 }
1405
1406 u->stp_inactivity_timeout = stp_inactive_to;
1407 u->ssp_inactivity_timeout = ssp_inactive_to;
1408 u->stp_max_occupancy_timeout = stp_max_occ_to;
1409 u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1410 u->no_outbound_task_timeout = no_outbound_task_to;
1411 u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1412}
1413
Dan Williams9269e0e2011-05-12 07:42:17 -07001414static void scic_sds_controller_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001415{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001416 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001417
Edmund Nadolskie3013702011-06-02 00:10:43 +00001418 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001419}
1420
Dan Williams9269e0e2011-05-12 07:42:17 -07001421static inline void scic_sds_controller_starting_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001422{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001423 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001424
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001425 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001426}
1427
1428#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1429#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1430#define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
1431#define INTERRUPT_COALESCE_NUMBER_MAX 256
1432#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
1433#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
1434
1435/**
1436 * scic_controller_set_interrupt_coalescence() - This method allows the user to
1437 * configure the interrupt coalescence.
1438 * @controller: This parameter represents the handle to the controller object
1439 * for which its interrupt coalesce register is overridden.
1440 * @coalesce_number: Used to control the number of entries in the Completion
1441 * Queue before an interrupt is generated. If the number of entries exceed
1442 * this number, an interrupt will be generated. The valid range of the input
1443 * is [0, 256]. A setting of 0 results in coalescing being disabled.
1444 * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1445 * input is [0, 2700000] . A setting of 0 is allowed and results in no
1446 * interrupt coalescing timeout.
1447 *
1448 * Indicate if the user successfully set the interrupt coalesce parameters.
1449 * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1450 * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1451 */
1452static enum sci_status scic_controller_set_interrupt_coalescence(
1453 struct scic_sds_controller *scic_controller,
1454 u32 coalesce_number,
1455 u32 coalesce_timeout)
1456{
1457 u8 timeout_encode = 0;
1458 u32 min = 0;
1459 u32 max = 0;
1460
1461 /* Check if the input parameters fall in the range. */
1462 if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1463 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1464
1465 /*
1466 * Defined encoding for interrupt coalescing timeout:
1467 * Value Min Max Units
1468 * ----- --- --- -----
1469 * 0 - - Disabled
1470 * 1 13.3 20.0 ns
1471 * 2 26.7 40.0
1472 * 3 53.3 80.0
1473 * 4 106.7 160.0
1474 * 5 213.3 320.0
1475 * 6 426.7 640.0
1476 * 7 853.3 1280.0
1477 * 8 1.7 2.6 us
1478 * 9 3.4 5.1
1479 * 10 6.8 10.2
1480 * 11 13.7 20.5
1481 * 12 27.3 41.0
1482 * 13 54.6 81.9
1483 * 14 109.2 163.8
1484 * 15 218.5 327.7
1485 * 16 436.9 655.4
1486 * 17 873.8 1310.7
1487 * 18 1.7 2.6 ms
1488 * 19 3.5 5.2
1489 * 20 7.0 10.5
1490 * 21 14.0 21.0
1491 * 22 28.0 41.9
1492 * 23 55.9 83.9
1493 * 24 111.8 167.8
1494 * 25 223.7 335.5
1495 * 26 447.4 671.1
1496 * 27 894.8 1342.2
1497 * 28 1.8 2.7 s
1498 * Others Undefined */
1499
1500 /*
1501 * Use the table above to decide the encode of interrupt coalescing timeout
1502 * value for register writing. */
1503 if (coalesce_timeout == 0)
1504 timeout_encode = 0;
1505 else{
1506 /* make the timeout value in unit of (10 ns). */
1507 coalesce_timeout = coalesce_timeout * 100;
1508 min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1509 max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1510
1511 /* get the encode of timeout for register writing. */
1512 for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1513 timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1514 timeout_encode++) {
1515 if (min <= coalesce_timeout && max > coalesce_timeout)
1516 break;
1517 else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1518 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1519 if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1520 break;
1521 else{
1522 timeout_encode++;
1523 break;
1524 }
1525 } else {
1526 max = max * 2;
1527 min = min * 2;
1528 }
1529 }
1530
1531 if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1532 /* the value is out of range. */
1533 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1534 }
1535
1536 writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1537 SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1538 &scic_controller->smu_registers->interrupt_coalesce_control);
1539
1540
1541 scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
1542 scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
1543
1544 return SCI_SUCCESS;
1545}
1546
1547
Dan Williams9269e0e2011-05-12 07:42:17 -07001548static void scic_sds_controller_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001549{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001550 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001551
1552 /* set the default interrupt coalescence number and timeout value. */
1553 scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
1554}
1555
Dan Williams9269e0e2011-05-12 07:42:17 -07001556static void scic_sds_controller_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001557{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001558 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001559
1560 /* disable interrupt coalescence. */
1561 scic_controller_set_interrupt_coalescence(scic, 0, 0);
1562}
1563
1564static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
1565{
1566 u32 index;
1567 enum sci_status status;
1568 enum sci_status phy_status;
1569 struct isci_host *ihost = scic_to_ihost(scic);
1570
1571 status = SCI_SUCCESS;
1572
1573 for (index = 0; index < SCI_MAX_PHYS; index++) {
1574 phy_status = scic_sds_phy_stop(&ihost->phys[index].sci);
1575
1576 if (phy_status != SCI_SUCCESS &&
1577 phy_status != SCI_FAILURE_INVALID_STATE) {
1578 status = SCI_FAILURE;
1579
1580 dev_warn(scic_to_dev(scic),
1581 "%s: Controller stop operation failed to stop "
1582 "phy %d because of status %d.\n",
1583 __func__,
1584 ihost->phys[index].sci.phy_index, phy_status);
1585 }
1586 }
1587
1588 return status;
1589}
1590
1591static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
1592{
1593 u32 index;
1594 enum sci_status port_status;
1595 enum sci_status status = SCI_SUCCESS;
1596 struct isci_host *ihost = scic_to_ihost(scic);
1597
1598 for (index = 0; index < scic->logical_port_entries; index++) {
1599 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
Dan Williamscc9203b2011-05-08 17:34:44 -07001600
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001601 port_status = scic_sds_port_stop(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001602
1603 if ((port_status != SCI_SUCCESS) &&
1604 (port_status != SCI_FAILURE_INVALID_STATE)) {
1605 status = SCI_FAILURE;
1606
1607 dev_warn(scic_to_dev(scic),
1608 "%s: Controller stop operation failed to "
1609 "stop port %d because of status %d.\n",
1610 __func__,
1611 sci_port->logical_port_index,
1612 port_status);
1613 }
1614 }
1615
1616 return status;
1617}
1618
1619static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
1620{
1621 u32 index;
1622 enum sci_status status;
1623 enum sci_status device_status;
1624
1625 status = SCI_SUCCESS;
1626
1627 for (index = 0; index < scic->remote_node_entries; index++) {
1628 if (scic->device_table[index] != NULL) {
1629 /* / @todo What timeout value do we want to provide to this request? */
1630 device_status = scic_remote_device_stop(scic->device_table[index], 0);
1631
1632 if ((device_status != SCI_SUCCESS) &&
1633 (device_status != SCI_FAILURE_INVALID_STATE)) {
1634 dev_warn(scic_to_dev(scic),
1635 "%s: Controller stop operation failed "
1636 "to stop device 0x%p because of "
1637 "status %d.\n",
1638 __func__,
1639 scic->device_table[index], device_status);
1640 }
1641 }
1642 }
1643
1644 return status;
1645}
1646
Dan Williams9269e0e2011-05-12 07:42:17 -07001647static void scic_sds_controller_stopping_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001648{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001649 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001650
1651 /* Stop all of the components for this controller */
1652 scic_sds_controller_stop_phys(scic);
1653 scic_sds_controller_stop_ports(scic);
1654 scic_sds_controller_stop_devices(scic);
1655}
1656
Dan Williams9269e0e2011-05-12 07:42:17 -07001657static void scic_sds_controller_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001658{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001659 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001660
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001661 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001662}
1663
1664
1665/**
1666 * scic_sds_controller_reset_hardware() -
1667 *
1668 * This method will reset the controller hardware.
1669 */
1670static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
1671{
1672 /* Disable interrupts so we dont take any spurious interrupts */
1673 scic_controller_disable_interrupts(scic);
1674
1675 /* Reset the SCU */
1676 writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
1677
1678 /* Delay for 1ms to before clearing the CQP and UFQPR. */
1679 udelay(1000);
1680
1681 /* The write to the CQGR clears the CQP */
1682 writel(0x00000000, &scic->smu_registers->completion_queue_get);
1683
1684 /* The write to the UFQGP clears the UFQPR */
1685 writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
1686}
1687
Dan Williams9269e0e2011-05-12 07:42:17 -07001688static void scic_sds_controller_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001689{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001690 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001691
1692 scic_sds_controller_reset_hardware(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001693 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001694}
1695
1696static const struct sci_base_state scic_sds_controller_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001697 [SCIC_INITIAL] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001698 .enter_state = scic_sds_controller_initial_state_enter,
1699 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001700 [SCIC_RESET] = {},
1701 [SCIC_INITIALIZING] = {},
1702 [SCIC_INITIALIZED] = {},
1703 [SCIC_STARTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001704 .exit_state = scic_sds_controller_starting_state_exit,
1705 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001706 [SCIC_READY] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001707 .enter_state = scic_sds_controller_ready_state_enter,
1708 .exit_state = scic_sds_controller_ready_state_exit,
1709 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001710 [SCIC_RESETTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001711 .enter_state = scic_sds_controller_resetting_state_enter,
1712 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001713 [SCIC_STOPPING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001714 .enter_state = scic_sds_controller_stopping_state_enter,
1715 .exit_state = scic_sds_controller_stopping_state_exit,
1716 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001717 [SCIC_STOPPED] = {},
1718 [SCIC_FAILED] = {}
Dan Williamscc9203b2011-05-08 17:34:44 -07001719};
1720
1721static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
1722{
1723 /* these defaults are overridden by the platform / firmware */
1724 struct isci_host *ihost = scic_to_ihost(scic);
1725 u16 index;
1726
1727 /* Default to APC mode. */
1728 scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1729
1730 /* Default to APC mode. */
1731 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
1732
1733 /* Default to no SSC operation. */
1734 scic->oem_parameters.sds1.controller.do_enable_ssc = false;
1735
1736 /* Initialize all of the port parameter information to narrow ports. */
1737 for (index = 0; index < SCI_MAX_PORTS; index++) {
1738 scic->oem_parameters.sds1.ports[index].phy_mask = 0;
1739 }
1740
1741 /* Initialize all of the phy parameter information. */
1742 for (index = 0; index < SCI_MAX_PHYS; index++) {
1743 /* Default to 6G (i.e. Gen 3) for now. */
1744 scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
1745
1746 /* the frequencies cannot be 0 */
1747 scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
1748 scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
1749 scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1750
1751 /*
1752 * Previous Vitesse based expanders had a arbitration issue that
1753 * is worked around by having the upper 32-bits of SAS address
1754 * with a value greater then the Vitesse company identifier.
1755 * Hence, usage of 0x5FCFFFFF. */
1756 scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
1757 scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
1758 }
1759
1760 scic->user_parameters.sds1.stp_inactivity_timeout = 5;
1761 scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
1762 scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
1763 scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
1764 scic->user_parameters.sds1.no_outbound_task_timeout = 20;
1765}
1766
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001767static void controller_timeout(unsigned long data)
1768{
1769 struct sci_timer *tmr = (struct sci_timer *)data;
1770 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), timer);
1771 struct isci_host *ihost = scic_to_ihost(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001772 struct sci_base_state_machine *sm = &scic->sm;
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001773 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001774
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001775 spin_lock_irqsave(&ihost->scic_lock, flags);
1776
1777 if (tmr->cancel)
1778 goto done;
1779
Edmund Nadolskie3013702011-06-02 00:10:43 +00001780 if (sm->current_state_id == SCIC_STARTING)
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001781 scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001782 else if (sm->current_state_id == SCIC_STOPPING) {
1783 sci_change_state(sm, SCIC_FAILED);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001784 isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
1785 } else /* / @todo Now what do we want to do in this case? */
1786 dev_err(scic_to_dev(scic),
1787 "%s: Controller timer fired when controller was not "
1788 "in a state being timed.\n",
1789 __func__);
1790
1791done:
1792 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1793}
Dan Williamscc9203b2011-05-08 17:34:44 -07001794
1795/**
1796 * scic_controller_construct() - This method will attempt to construct a
1797 * controller object utilizing the supplied parameter information.
1798 * @c: This parameter specifies the controller to be constructed.
1799 * @scu_base: mapped base address of the scu registers
1800 * @smu_base: mapped base address of the smu registers
1801 *
1802 * Indicate if the controller was successfully constructed or if it failed in
1803 * some way. SCI_SUCCESS This value is returned if the controller was
1804 * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
1805 * if the interrupt coalescence timer may cause SAS compliance issues for SMP
1806 * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
1807 * This value is returned if the controller does not support the supplied type.
1808 * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
1809 * controller does not support the supplied initialization data version.
1810 */
1811static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
1812 void __iomem *scu_base,
1813 void __iomem *smu_base)
1814{
1815 struct isci_host *ihost = scic_to_ihost(scic);
1816 u8 i;
1817
Edmund Nadolski12ef6542011-06-02 00:10:50 +00001818 sci_init_sm(&scic->sm, scic_sds_controller_state_table, SCIC_INITIAL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001819
1820 scic->scu_registers = scu_base;
1821 scic->smu_registers = smu_base;
1822
1823 scic_sds_port_configuration_agent_construct(&scic->port_agent);
1824
1825 /* Construct the ports for this controller */
1826 for (i = 0; i < SCI_MAX_PORTS; i++)
1827 scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
1828 scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
1829
1830 /* Construct the phys for this controller */
1831 for (i = 0; i < SCI_MAX_PHYS; i++) {
1832 /* Add all the PHYs to the dummy port */
1833 scic_sds_phy_construct(&ihost->phys[i].sci,
1834 &ihost->ports[SCI_MAX_PORTS].sci, i);
1835 }
1836
1837 scic->invalid_phy_mask = 0;
1838
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001839 sci_init_timer(&scic->timer, controller_timeout);
1840
Dan Williamscc9203b2011-05-08 17:34:44 -07001841 /* Initialize the User and OEM parameters to default values. */
1842 scic_sds_controller_set_default_config_parameters(scic);
1843
1844 return scic_controller_reset(scic);
1845}
1846
1847int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
1848{
1849 int i;
1850
1851 for (i = 0; i < SCI_MAX_PORTS; i++)
1852 if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1853 return -EINVAL;
1854
1855 for (i = 0; i < SCI_MAX_PHYS; i++)
1856 if (oem->phys[i].sas_address.high == 0 &&
1857 oem->phys[i].sas_address.low == 0)
1858 return -EINVAL;
1859
1860 if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1861 for (i = 0; i < SCI_MAX_PHYS; i++)
1862 if (oem->ports[i].phy_mask != 0)
1863 return -EINVAL;
1864 } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1865 u8 phy_mask = 0;
1866
1867 for (i = 0; i < SCI_MAX_PHYS; i++)
1868 phy_mask |= oem->ports[i].phy_mask;
1869
1870 if (phy_mask == 0)
1871 return -EINVAL;
1872 } else
1873 return -EINVAL;
1874
1875 if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1876 return -EINVAL;
1877
1878 return 0;
1879}
1880
1881static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
1882 union scic_oem_parameters *scic_parms)
1883{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001884 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07001885
Edmund Nadolskie3013702011-06-02 00:10:43 +00001886 if (state == SCIC_RESET ||
1887 state == SCIC_INITIALIZING ||
1888 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001889
1890 if (scic_oem_parameters_validate(&scic_parms->sds1))
1891 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1892 scic->oem_parameters.sds1 = scic_parms->sds1;
1893
1894 return SCI_SUCCESS;
1895 }
1896
1897 return SCI_FAILURE_INVALID_STATE;
1898}
1899
1900void scic_oem_parameters_get(
1901 struct scic_sds_controller *scic,
1902 union scic_oem_parameters *scic_parms)
1903{
1904 memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
1905}
1906
Edmund Nadolski04736612011-05-19 20:17:47 -07001907static void power_control_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001908{
Edmund Nadolski04736612011-05-19 20:17:47 -07001909 struct sci_timer *tmr = (struct sci_timer *)data;
1910 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), power_control.timer);
1911 struct isci_host *ihost = scic_to_ihost(scic);
1912 struct scic_sds_phy *sci_phy;
1913 unsigned long flags;
1914 u8 i;
Dan Williamscc9203b2011-05-08 17:34:44 -07001915
Edmund Nadolski04736612011-05-19 20:17:47 -07001916 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001917
Edmund Nadolski04736612011-05-19 20:17:47 -07001918 if (tmr->cancel)
1919 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001920
1921 scic->power_control.phys_granted_power = 0;
1922
1923 if (scic->power_control.phys_waiting == 0) {
1924 scic->power_control.timer_started = false;
Edmund Nadolski04736612011-05-19 20:17:47 -07001925 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001926 }
Edmund Nadolski04736612011-05-19 20:17:47 -07001927
1928 for (i = 0; i < SCI_MAX_PHYS; i++) {
1929
1930 if (scic->power_control.phys_waiting == 0)
1931 break;
1932
1933 sci_phy = scic->power_control.requesters[i];
1934 if (sci_phy == NULL)
1935 continue;
1936
1937 if (scic->power_control.phys_granted_power >=
1938 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up)
1939 break;
1940
1941 scic->power_control.requesters[i] = NULL;
1942 scic->power_control.phys_waiting--;
1943 scic->power_control.phys_granted_power++;
1944 scic_sds_phy_consume_power_handler(sci_phy);
1945 }
1946
1947 /*
1948 * It doesn't matter if the power list is empty, we need to start the
1949 * timer in case another phy becomes ready.
1950 */
1951 sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1952 scic->power_control.timer_started = true;
1953
1954done:
1955 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001956}
1957
1958/**
1959 * This method inserts the phy in the stagger spinup control queue.
1960 * @scic:
1961 *
1962 *
1963 */
1964void scic_sds_controller_power_control_queue_insert(
1965 struct scic_sds_controller *scic,
1966 struct scic_sds_phy *sci_phy)
1967{
1968 BUG_ON(sci_phy == NULL);
1969
1970 if (scic->power_control.phys_granted_power <
1971 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
1972 scic->power_control.phys_granted_power++;
1973 scic_sds_phy_consume_power_handler(sci_phy);
1974
1975 /*
1976 * stop and start the power_control timer. When the timer fires, the
1977 * no_of_phys_granted_power will be set to 0
1978 */
Edmund Nadolski04736612011-05-19 20:17:47 -07001979 if (scic->power_control.timer_started)
1980 sci_del_timer(&scic->power_control.timer);
1981
1982 sci_mod_timer(&scic->power_control.timer,
1983 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1984 scic->power_control.timer_started = true;
1985
Dan Williamscc9203b2011-05-08 17:34:44 -07001986 } else {
1987 /* Add the phy in the waiting list */
1988 scic->power_control.requesters[sci_phy->phy_index] = sci_phy;
1989 scic->power_control.phys_waiting++;
1990 }
1991}
1992
1993/**
1994 * This method removes the phy from the stagger spinup control queue.
1995 * @scic:
1996 *
1997 *
1998 */
1999void scic_sds_controller_power_control_queue_remove(
2000 struct scic_sds_controller *scic,
2001 struct scic_sds_phy *sci_phy)
2002{
2003 BUG_ON(sci_phy == NULL);
2004
2005 if (scic->power_control.requesters[sci_phy->phy_index] != NULL) {
2006 scic->power_control.phys_waiting--;
2007 }
2008
2009 scic->power_control.requesters[sci_phy->phy_index] = NULL;
2010}
2011
2012#define AFE_REGISTER_WRITE_DELAY 10
2013
2014/* Initialize the AFE for this phy index. We need to read the AFE setup from
2015 * the OEM parameters
2016 */
2017static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
2018{
2019 const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
2020 u32 afe_status;
2021 u32 phy_id;
2022
2023 /* Clear DFX Status registers */
2024 writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
2025 udelay(AFE_REGISTER_WRITE_DELAY);
2026
2027 if (is_b0()) {
2028 /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
2029 * Timer, PM Stagger Timer */
2030 writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
2031 udelay(AFE_REGISTER_WRITE_DELAY);
2032 }
2033
2034 /* Configure bias currents to normal */
2035 if (is_a0())
2036 writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
2037 else if (is_a2())
2038 writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002039 else if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002040 writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
2041
2042 udelay(AFE_REGISTER_WRITE_DELAY);
2043
2044 /* Enable PLL */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002045 if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002046 writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
2047 else
2048 writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
2049
2050 udelay(AFE_REGISTER_WRITE_DELAY);
2051
2052 /* Wait for the PLL to lock */
2053 do {
2054 afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
2055 udelay(AFE_REGISTER_WRITE_DELAY);
2056 } while ((afe_status & 0x00001000) == 0);
2057
2058 if (is_a0() || is_a2()) {
2059 /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
2060 writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
2061 udelay(AFE_REGISTER_WRITE_DELAY);
2062 }
2063
2064 for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
2065 const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2066
2067 if (is_b0()) {
2068 /* Configure transmitter SSC parameters */
2069 writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2070 udelay(AFE_REGISTER_WRITE_DELAY);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002071 } else if (is_c0()) {
2072 /* Configure transmitter SSC parameters */
2073 writel(0x0003000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2074 udelay(AFE_REGISTER_WRITE_DELAY);
2075
2076 /*
2077 * All defaults, except the Receive Word Alignament/Comma Detect
2078 * Enable....(0xe800) */
2079 writel(0x00004500, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2080 udelay(AFE_REGISTER_WRITE_DELAY);
Dan Williamscc9203b2011-05-08 17:34:44 -07002081 } else {
2082 /*
2083 * All defaults, except the Receive Word Alignament/Comma Detect
2084 * Enable....(0xe800) */
2085 writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2086 udelay(AFE_REGISTER_WRITE_DELAY);
2087
2088 writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
2089 udelay(AFE_REGISTER_WRITE_DELAY);
2090 }
2091
2092 /*
2093 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2094 * & increase TX int & ext bias 20%....(0xe85c) */
2095 if (is_a0())
2096 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2097 else if (is_a2())
2098 writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002099 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002100 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002101 writel(0x000003D7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002102 udelay(AFE_REGISTER_WRITE_DELAY);
2103
2104 /*
2105 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2106 * & increase TX int & ext bias 20%....(0xe85c) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002107 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2108 } else {
2109 writel(0x000001E7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2110 udelay(AFE_REGISTER_WRITE_DELAY);
2111
2112 /*
2113 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2114 * & increase TX int & ext bias 20%....(0xe85c) */
2115 writel(0x000001E4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002116 }
2117 udelay(AFE_REGISTER_WRITE_DELAY);
2118
2119 if (is_a0() || is_a2()) {
2120 /* Enable TX equalization (0xe824) */
2121 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2122 udelay(AFE_REGISTER_WRITE_DELAY);
2123 }
2124
2125 /*
2126 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
2127 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
2128 writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2129 udelay(AFE_REGISTER_WRITE_DELAY);
2130
2131 /* Leave DFE/FFE on */
2132 if (is_a0())
2133 writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2134 else if (is_a2())
2135 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002136 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002137 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2138 udelay(AFE_REGISTER_WRITE_DELAY);
2139 /* Enable TX equalization (0xe824) */
2140 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002141 } else {
2142 writel(0x0140DF0F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
2143 udelay(AFE_REGISTER_WRITE_DELAY);
2144
2145 writel(0x3F6F103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2146 udelay(AFE_REGISTER_WRITE_DELAY);
2147
2148 /* Enable TX equalization (0xe824) */
2149 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002150 }
Adam Gruchaladbb07432011-06-01 22:31:03 +00002151
Dan Williamscc9203b2011-05-08 17:34:44 -07002152 udelay(AFE_REGISTER_WRITE_DELAY);
2153
2154 writel(oem_phy->afe_tx_amp_control0,
2155 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
2156 udelay(AFE_REGISTER_WRITE_DELAY);
2157
2158 writel(oem_phy->afe_tx_amp_control1,
2159 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
2160 udelay(AFE_REGISTER_WRITE_DELAY);
2161
2162 writel(oem_phy->afe_tx_amp_control2,
2163 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
2164 udelay(AFE_REGISTER_WRITE_DELAY);
2165
2166 writel(oem_phy->afe_tx_amp_control3,
2167 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
2168 udelay(AFE_REGISTER_WRITE_DELAY);
2169 }
2170
2171 /* Transfer control to the PEs */
2172 writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
2173 udelay(AFE_REGISTER_WRITE_DELAY);
2174}
2175
Dan Williamscc9203b2011-05-08 17:34:44 -07002176static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
2177{
Edmund Nadolski04736612011-05-19 20:17:47 -07002178 sci_init_timer(&scic->power_control.timer, power_control_timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07002179
2180 memset(scic->power_control.requesters, 0,
2181 sizeof(scic->power_control.requesters));
2182
2183 scic->power_control.phys_waiting = 0;
2184 scic->power_control.phys_granted_power = 0;
2185}
2186
2187static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
2188{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002189 struct sci_base_state_machine *sm = &scic->sm;
Dan Williamscc9203b2011-05-08 17:34:44 -07002190 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002191 enum sci_status result = SCI_FAILURE;
2192 unsigned long i, state, val;
Dan Williamscc9203b2011-05-08 17:34:44 -07002193
Edmund Nadolskie3013702011-06-02 00:10:43 +00002194 if (scic->sm.current_state_id != SCIC_RESET) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002195 dev_warn(scic_to_dev(scic),
2196 "SCIC Controller initialize operation requested "
2197 "in invalid state\n");
2198 return SCI_FAILURE_INVALID_STATE;
2199 }
2200
Edmund Nadolskie3013702011-06-02 00:10:43 +00002201 sci_change_state(sm, SCIC_INITIALIZING);
Dan Williamscc9203b2011-05-08 17:34:44 -07002202
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002203 sci_init_timer(&scic->phy_timer, phy_startup_timeout);
2204
2205 scic->next_phy_to_start = 0;
2206 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002207
2208 scic_sds_controller_initialize_power_control(scic);
2209
2210 /*
2211 * There is nothing to do here for B0 since we do not have to
2212 * program the AFE registers.
2213 * / @todo The AFE settings are supposed to be correct for the B0 but
2214 * / presently they seem to be wrong. */
2215 scic_sds_controller_afe_initialization(scic);
2216
Dan Williams7c78da32011-06-01 16:00:01 -07002217
2218 /* Take the hardware out of reset */
2219 writel(0, &scic->smu_registers->soft_reset_control);
2220
2221 /*
2222 * / @todo Provide meaningfull error code for hardware failure
2223 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2224 for (i = 100; i >= 1; i--) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002225 u32 status;
Dan Williamscc9203b2011-05-08 17:34:44 -07002226
Dan Williams7c78da32011-06-01 16:00:01 -07002227 /* Loop until the hardware reports success */
2228 udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2229 status = readl(&scic->smu_registers->control_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07002230
Dan Williams7c78da32011-06-01 16:00:01 -07002231 if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
2232 break;
Dan Williamscc9203b2011-05-08 17:34:44 -07002233 }
Dan Williams7c78da32011-06-01 16:00:01 -07002234 if (i == 0)
2235 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002236
Dan Williams7c78da32011-06-01 16:00:01 -07002237 /*
2238 * Determine what are the actaul device capacities that the
2239 * hardware will support */
2240 val = readl(&scic->smu_registers->device_context_capacity);
Dan Williamscc9203b2011-05-08 17:34:44 -07002241
Dan Williams7c78da32011-06-01 16:00:01 -07002242 /* Record the smaller of the two capacity values */
2243 scic->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2244 scic->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2245 scic->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
Dan Williamscc9203b2011-05-08 17:34:44 -07002246
Dan Williams7c78da32011-06-01 16:00:01 -07002247 /*
2248 * Make all PEs that are unassigned match up with the
2249 * logical ports
2250 */
2251 for (i = 0; i < scic->logical_port_entries; i++) {
2252 struct scu_port_task_scheduler_group_registers __iomem
2253 *ptsg = &scic->scu_registers->peg0.ptsg;
Dan Williamscc9203b2011-05-08 17:34:44 -07002254
Dan Williams7c78da32011-06-01 16:00:01 -07002255 writel(i, &ptsg->protocol_engine[i]);
Dan Williamscc9203b2011-05-08 17:34:44 -07002256 }
2257
2258 /* Initialize hardware PCI Relaxed ordering in DMA engines */
Dan Williams7c78da32011-06-01 16:00:01 -07002259 val = readl(&scic->scu_registers->sdma.pdma_configuration);
2260 val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2261 writel(val, &scic->scu_registers->sdma.pdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002262
Dan Williams7c78da32011-06-01 16:00:01 -07002263 val = readl(&scic->scu_registers->sdma.cdma_configuration);
2264 val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2265 writel(val, &scic->scu_registers->sdma.cdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002266
2267 /*
2268 * Initialize the PHYs before the PORTs because the PHY registers
2269 * are accessed during the port initialization.
2270 */
Dan Williams7c78da32011-06-01 16:00:01 -07002271 for (i = 0; i < SCI_MAX_PHYS; i++) {
2272 result = scic_sds_phy_initialize(&ihost->phys[i].sci,
2273 &scic->scu_registers->peg0.pe[i].tl,
2274 &scic->scu_registers->peg0.pe[i].ll);
2275 if (result != SCI_SUCCESS)
2276 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002277 }
2278
Dan Williams7c78da32011-06-01 16:00:01 -07002279 for (i = 0; i < scic->logical_port_entries; i++) {
2280 result = scic_sds_port_initialize(&ihost->ports[i].sci,
2281 &scic->scu_registers->peg0.ptsg.port[i],
2282 &scic->scu_registers->peg0.ptsg.protocol_engine,
2283 &scic->scu_registers->peg0.viit[i]);
2284
2285 if (result != SCI_SUCCESS)
2286 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002287 }
2288
Dan Williams7c78da32011-06-01 16:00:01 -07002289 result = scic_sds_port_configuration_agent_initialize(scic, &scic->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07002290
Dan Williams7c78da32011-06-01 16:00:01 -07002291 out:
Dan Williamscc9203b2011-05-08 17:34:44 -07002292 /* Advance the controller state machine */
2293 if (result == SCI_SUCCESS)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002294 state = SCIC_INITIALIZED;
Dan Williamscc9203b2011-05-08 17:34:44 -07002295 else
Edmund Nadolskie3013702011-06-02 00:10:43 +00002296 state = SCIC_FAILED;
2297 sci_change_state(sm, state);
Dan Williamscc9203b2011-05-08 17:34:44 -07002298
2299 return result;
2300}
2301
2302static enum sci_status scic_user_parameters_set(
2303 struct scic_sds_controller *scic,
2304 union scic_user_parameters *scic_parms)
2305{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002306 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07002307
Edmund Nadolskie3013702011-06-02 00:10:43 +00002308 if (state == SCIC_RESET ||
2309 state == SCIC_INITIALIZING ||
2310 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002311 u16 index;
2312
2313 /*
2314 * Validate the user parameters. If they are not legal, then
2315 * return a failure.
2316 */
2317 for (index = 0; index < SCI_MAX_PHYS; index++) {
2318 struct sci_phy_user_params *user_phy;
2319
2320 user_phy = &scic_parms->sds1.phys[index];
2321
2322 if (!((user_phy->max_speed_generation <=
2323 SCIC_SDS_PARM_MAX_SPEED) &&
2324 (user_phy->max_speed_generation >
2325 SCIC_SDS_PARM_NO_SPEED)))
2326 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2327
2328 if (user_phy->in_connection_align_insertion_frequency <
2329 3)
2330 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2331
2332 if ((user_phy->in_connection_align_insertion_frequency <
2333 3) ||
2334 (user_phy->align_insertion_frequency == 0) ||
2335 (user_phy->
2336 notify_enable_spin_up_insertion_frequency ==
2337 0))
2338 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2339 }
2340
2341 if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
2342 (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
2343 (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
2344 (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
2345 (scic_parms->sds1.no_outbound_task_timeout == 0))
2346 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2347
2348 memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
2349
2350 return SCI_SUCCESS;
2351 }
2352
2353 return SCI_FAILURE_INVALID_STATE;
2354}
2355
2356static int scic_controller_mem_init(struct scic_sds_controller *scic)
2357{
2358 struct device *dev = scic_to_dev(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002359 dma_addr_t dma;
2360 size_t size;
2361 int err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002362
Dan Williams7c78da32011-06-01 16:00:01 -07002363 size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2364 scic->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002365 if (!scic->completion_queue)
2366 return -ENOMEM;
2367
Dan Williams7c78da32011-06-01 16:00:01 -07002368 writel(lower_32_bits(dma), &scic->smu_registers->completion_queue_lower);
2369 writel(upper_32_bits(dma), &scic->smu_registers->completion_queue_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002370
Dan Williams7c78da32011-06-01 16:00:01 -07002371 size = scic->remote_node_entries * sizeof(union scu_remote_node_context);
2372 scic->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
2373 GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002374 if (!scic->remote_node_context_table)
2375 return -ENOMEM;
2376
Dan Williams7c78da32011-06-01 16:00:01 -07002377 writel(lower_32_bits(dma), &scic->smu_registers->remote_node_context_lower);
2378 writel(upper_32_bits(dma), &scic->smu_registers->remote_node_context_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002379
Dan Williams7c78da32011-06-01 16:00:01 -07002380 size = scic->task_context_entries * sizeof(struct scu_task_context),
2381 scic->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002382 if (!scic->task_context_table)
2383 return -ENOMEM;
2384
Dan Williams7c78da32011-06-01 16:00:01 -07002385 writel(lower_32_bits(dma), &scic->smu_registers->host_task_table_lower);
2386 writel(upper_32_bits(dma), &scic->smu_registers->host_task_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002387
Dan Williams7c78da32011-06-01 16:00:01 -07002388 err = scic_sds_unsolicited_frame_control_construct(scic);
2389 if (err)
2390 return err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002391
2392 /*
2393 * Inform the silicon as to the location of the UF headers and
2394 * address table.
2395 */
2396 writel(lower_32_bits(scic->uf_control.headers.physical_address),
2397 &scic->scu_registers->sdma.uf_header_base_address_lower);
2398 writel(upper_32_bits(scic->uf_control.headers.physical_address),
2399 &scic->scu_registers->sdma.uf_header_base_address_upper);
2400
2401 writel(lower_32_bits(scic->uf_control.address_table.physical_address),
2402 &scic->scu_registers->sdma.uf_address_table_lower);
2403 writel(upper_32_bits(scic->uf_control.address_table.physical_address),
2404 &scic->scu_registers->sdma.uf_address_table_upper);
2405
2406 return 0;
2407}
2408
Dan Williams6f231dd2011-07-02 22:56:22 -07002409int isci_host_init(struct isci_host *isci_host)
2410{
Dan Williamsd9c37392011-03-03 17:59:32 -08002411 int err = 0, i;
Dan Williams6f231dd2011-07-02 22:56:22 -07002412 enum sci_status status;
Dan Williams4711ba12011-03-11 10:43:57 -08002413 union scic_oem_parameters oem;
Dan Williams6f231dd2011-07-02 22:56:22 -07002414 union scic_user_parameters scic_user_params;
Dan Williamsd044af12011-03-08 09:52:49 -08002415 struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002416
Dan Williams6f231dd2011-07-02 22:56:22 -07002417 spin_lock_init(&isci_host->state_lock);
2418 spin_lock_init(&isci_host->scic_lock);
2419 spin_lock_init(&isci_host->queue_lock);
Dan Williams0cf89d12011-02-18 09:25:07 -08002420 init_waitqueue_head(&isci_host->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07002421
2422 isci_host_change_state(isci_host, isci_starting);
2423 isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
2424
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002425 status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
Dan Williams6f231dd2011-07-02 22:56:22 -07002426 smu_base(isci_host));
2427
2428 if (status != SCI_SUCCESS) {
2429 dev_err(&isci_host->pdev->dev,
2430 "%s: scic_controller_construct failed - status = %x\n",
2431 __func__,
2432 status);
Dave Jiang858d4aa2011-02-22 01:27:03 -08002433 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002434 }
2435
2436 isci_host->sas_ha.dev = &isci_host->pdev->dev;
2437 isci_host->sas_ha.lldd_ha = isci_host;
2438
Dan Williamsd044af12011-03-08 09:52:49 -08002439 /*
2440 * grab initial values stored in the controller object for OEM and USER
2441 * parameters
2442 */
Dave Jiangb5f18a22011-03-16 14:57:23 -07002443 isci_user_parameters_get(isci_host, &scic_user_params);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002444 status = scic_user_parameters_set(&isci_host->sci,
Dan Williamsd044af12011-03-08 09:52:49 -08002445 &scic_user_params);
2446 if (status != SCI_SUCCESS) {
2447 dev_warn(&isci_host->pdev->dev,
2448 "%s: scic_user_parameters_set failed\n",
2449 __func__);
2450 return -ENODEV;
2451 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002452
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002453 scic_oem_parameters_get(&isci_host->sci, &oem);
Dan Williamsd044af12011-03-08 09:52:49 -08002454
2455 /* grab any OEM parameters specified in orom */
2456 if (pci_info->orom) {
Dan Williams4711ba12011-03-11 10:43:57 -08002457 status = isci_parse_oem_parameters(&oem,
Dan Williamsd044af12011-03-08 09:52:49 -08002458 pci_info->orom,
2459 isci_host->id);
Dan Williams6f231dd2011-07-02 22:56:22 -07002460 if (status != SCI_SUCCESS) {
2461 dev_warn(&isci_host->pdev->dev,
2462 "parsing firmware oem parameters failed\n");
Dave Jiang858d4aa2011-02-22 01:27:03 -08002463 return -EINVAL;
Dan Williams6f231dd2011-07-02 22:56:22 -07002464 }
Dan Williams4711ba12011-03-11 10:43:57 -08002465 }
2466
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002467 status = scic_oem_parameters_set(&isci_host->sci, &oem);
Dan Williams4711ba12011-03-11 10:43:57 -08002468 if (status != SCI_SUCCESS) {
2469 dev_warn(&isci_host->pdev->dev,
2470 "%s: scic_oem_parameters_set failed\n",
2471 __func__);
2472 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002473 }
2474
Dan Williams6f231dd2011-07-02 22:56:22 -07002475 tasklet_init(&isci_host->completion_tasklet,
Dan Williamsc7ef4032011-02-18 09:25:05 -08002476 isci_host_completion_routine, (unsigned long)isci_host);
Dan Williams6f231dd2011-07-02 22:56:22 -07002477
Dan Williams6f231dd2011-07-02 22:56:22 -07002478 INIT_LIST_HEAD(&isci_host->requests_to_complete);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002479 INIT_LIST_HEAD(&isci_host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002480
Dan Williams7c40a802011-03-02 11:49:26 -08002481 spin_lock_irq(&isci_host->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002482 status = scic_controller_initialize(&isci_host->sci);
Dan Williams7c40a802011-03-02 11:49:26 -08002483 spin_unlock_irq(&isci_host->scic_lock);
2484 if (status != SCI_SUCCESS) {
2485 dev_warn(&isci_host->pdev->dev,
2486 "%s: scic_controller_initialize failed -"
2487 " status = 0x%x\n",
2488 __func__, status);
2489 return -ENODEV;
2490 }
2491
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002492 err = scic_controller_mem_init(&isci_host->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07002493 if (err)
Dave Jiang858d4aa2011-02-22 01:27:03 -08002494 return err;
Dan Williams6f231dd2011-07-02 22:56:22 -07002495
Dan Williams6f231dd2011-07-02 22:56:22 -07002496 isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
Dan Williams67ea8382011-05-08 11:47:15 -07002497 sizeof(struct isci_request),
Dan Williams6f231dd2011-07-02 22:56:22 -07002498 SLAB_HWCACHE_ALIGN, 0);
2499
Dave Jiang858d4aa2011-02-22 01:27:03 -08002500 if (!isci_host->dma_pool)
2501 return -ENOMEM;
Dan Williams6f231dd2011-07-02 22:56:22 -07002502
Dan Williamsd9c37392011-03-03 17:59:32 -08002503 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williamse5313812011-05-07 10:11:43 -07002504 isci_port_init(&isci_host->ports[i], isci_host, i);
Dan Williams6f231dd2011-07-02 22:56:22 -07002505
Dan Williamsd9c37392011-03-03 17:59:32 -08002506 for (i = 0; i < SCI_MAX_PHYS; i++)
2507 isci_phy_init(&isci_host->phys[i], isci_host, i);
2508
2509 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07002510 struct isci_remote_device *idev = &isci_host->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08002511
2512 INIT_LIST_HEAD(&idev->reqs_in_process);
2513 INIT_LIST_HEAD(&idev->node);
2514 spin_lock_init(&idev->state_lock);
2515 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002516
Dave Jiang858d4aa2011-02-22 01:27:03 -08002517 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -07002518}
Dan Williamscc9203b2011-05-08 17:34:44 -07002519
2520void scic_sds_controller_link_up(struct scic_sds_controller *scic,
2521 struct scic_sds_port *port, struct scic_sds_phy *phy)
2522{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002523 switch (scic->sm.current_state_id) {
2524 case SCIC_STARTING:
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002525 sci_del_timer(&scic->phy_timer);
2526 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002527 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2528 port, phy);
2529 scic_sds_controller_start_next_phy(scic);
2530 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002531 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002532 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2533 port, phy);
2534 break;
2535 default:
2536 dev_dbg(scic_to_dev(scic),
2537 "%s: SCIC Controller linkup event from phy %d in "
2538 "unexpected state %d\n", __func__, phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002539 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002540 }
2541}
2542
2543void scic_sds_controller_link_down(struct scic_sds_controller *scic,
2544 struct scic_sds_port *port, struct scic_sds_phy *phy)
2545{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002546 switch (scic->sm.current_state_id) {
2547 case SCIC_STARTING:
2548 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002549 scic->port_agent.link_down_handler(scic, &scic->port_agent,
2550 port, phy);
2551 break;
2552 default:
2553 dev_dbg(scic_to_dev(scic),
2554 "%s: SCIC Controller linkdown event from phy %d in "
2555 "unexpected state %d\n",
2556 __func__,
2557 phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002558 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002559 }
2560}
2561
2562/**
2563 * This is a helper method to determine if any remote devices on this
2564 * controller are still in the stopping state.
2565 *
2566 */
2567static bool scic_sds_controller_has_remote_devices_stopping(
2568 struct scic_sds_controller *controller)
2569{
2570 u32 index;
2571
2572 for (index = 0; index < controller->remote_node_entries; index++) {
2573 if ((controller->device_table[index] != NULL) &&
Edmund Nadolskie3013702011-06-02 00:10:43 +00002574 (controller->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
Dan Williamscc9203b2011-05-08 17:34:44 -07002575 return true;
2576 }
2577
2578 return false;
2579}
2580
2581/**
2582 * This method is called by the remote device to inform the controller
2583 * object that the remote device has stopped.
2584 */
2585void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
2586 struct scic_sds_remote_device *sci_dev)
2587{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002588 if (scic->sm.current_state_id != SCIC_STOPPING) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002589 dev_dbg(scic_to_dev(scic),
2590 "SCIC Controller 0x%p remote device stopped event "
2591 "from device 0x%p in unexpected state %d\n",
2592 scic, sci_dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002593 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002594 return;
2595 }
2596
2597 if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002598 sci_change_state(&scic->sm, SCIC_STOPPED);
Dan Williamscc9203b2011-05-08 17:34:44 -07002599 }
2600}
2601
2602/**
2603 * This method will write to the SCU PCP register the request value. The method
2604 * is used to suspend/resume ports, devices, and phys.
2605 * @scic:
2606 *
2607 *
2608 */
2609void scic_sds_controller_post_request(
2610 struct scic_sds_controller *scic,
2611 u32 request)
2612{
2613 dev_dbg(scic_to_dev(scic),
2614 "%s: SCIC Controller 0x%p post request 0x%08x\n",
2615 __func__,
2616 scic,
2617 request);
2618
2619 writel(request, &scic->smu_registers->post_context_port);
2620}
2621
2622/**
2623 * This method will copy the soft copy of the task context into the physical
2624 * memory accessible by the controller.
2625 * @scic: This parameter specifies the controller for which to copy
2626 * the task context.
2627 * @sci_req: This parameter specifies the request for which the task
2628 * context is being copied.
2629 *
2630 * After this call is made the SCIC_SDS_IO_REQUEST object will always point to
2631 * the physical memory version of the task context. Thus, all subsequent
2632 * updates to the task context are performed in the TC table (i.e. DMAable
2633 * memory). none
2634 */
2635void scic_sds_controller_copy_task_context(
2636 struct scic_sds_controller *scic,
2637 struct scic_sds_request *sci_req)
2638{
2639 struct scu_task_context *task_context_buffer;
2640
2641 task_context_buffer = scic_sds_controller_get_task_context_buffer(
2642 scic, sci_req->io_tag);
2643
2644 memcpy(task_context_buffer,
2645 sci_req->task_context_buffer,
2646 offsetof(struct scu_task_context, sgl_snapshot_ac));
2647
2648 /*
2649 * Now that the soft copy of the TC has been copied into the TC
2650 * table accessible by the silicon. Thus, any further changes to
2651 * the TC (e.g. TC termination) occur in the appropriate location. */
2652 sci_req->task_context_buffer = task_context_buffer;
2653}
2654
2655/**
2656 * This method returns the task context buffer for the given io tag.
2657 * @scic:
2658 * @io_tag:
2659 *
2660 * struct scu_task_context*
2661 */
2662struct scu_task_context *scic_sds_controller_get_task_context_buffer(
2663 struct scic_sds_controller *scic,
2664 u16 io_tag
2665 ) {
2666 u16 task_index = scic_sds_io_tag_get_index(io_tag);
2667
2668 if (task_index < scic->task_context_entries) {
2669 return &scic->task_context_table[task_index];
2670 }
2671
2672 return NULL;
2673}
2674
2675struct scic_sds_request *scic_request_by_tag(struct scic_sds_controller *scic,
2676 u16 io_tag)
2677{
2678 u16 task_index;
2679 u16 task_sequence;
2680
2681 task_index = scic_sds_io_tag_get_index(io_tag);
2682
2683 if (task_index < scic->task_context_entries) {
2684 if (scic->io_request_table[task_index] != NULL) {
2685 task_sequence = scic_sds_io_tag_get_sequence(io_tag);
2686
2687 if (task_sequence == scic->io_request_sequence[task_index]) {
2688 return scic->io_request_table[task_index];
2689 }
2690 }
2691 }
2692
2693 return NULL;
2694}
2695
2696/**
2697 * This method allocates remote node index and the reserves the remote node
2698 * context space for use. This method can fail if there are no more remote
2699 * node index available.
2700 * @scic: This is the controller object which contains the set of
2701 * free remote node ids
2702 * @sci_dev: This is the device object which is requesting the a remote node
2703 * id
2704 * @node_id: This is the remote node id that is assinged to the device if one
2705 * is available
2706 *
2707 * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2708 * node index available.
2709 */
2710enum sci_status scic_sds_controller_allocate_remote_node_context(
2711 struct scic_sds_controller *scic,
2712 struct scic_sds_remote_device *sci_dev,
2713 u16 *node_id)
2714{
2715 u16 node_index;
2716 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2717
2718 node_index = scic_sds_remote_node_table_allocate_remote_node(
2719 &scic->available_remote_nodes, remote_node_count
2720 );
2721
2722 if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2723 scic->device_table[node_index] = sci_dev;
2724
2725 *node_id = node_index;
2726
2727 return SCI_SUCCESS;
2728 }
2729
2730 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2731}
2732
2733/**
2734 * This method frees the remote node index back to the available pool. Once
2735 * this is done the remote node context buffer is no longer valid and can
2736 * not be used.
2737 * @scic:
2738 * @sci_dev:
2739 * @node_id:
2740 *
2741 */
2742void scic_sds_controller_free_remote_node_context(
2743 struct scic_sds_controller *scic,
2744 struct scic_sds_remote_device *sci_dev,
2745 u16 node_id)
2746{
2747 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2748
2749 if (scic->device_table[node_id] == sci_dev) {
2750 scic->device_table[node_id] = NULL;
2751
2752 scic_sds_remote_node_table_release_remote_node_index(
2753 &scic->available_remote_nodes, remote_node_count, node_id
2754 );
2755 }
2756}
2757
2758/**
2759 * This method returns the union scu_remote_node_context for the specified remote
2760 * node id.
2761 * @scic:
2762 * @node_id:
2763 *
2764 * union scu_remote_node_context*
2765 */
2766union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
2767 struct scic_sds_controller *scic,
2768 u16 node_id
2769 ) {
2770 if (
2771 (node_id < scic->remote_node_entries)
2772 && (scic->device_table[node_id] != NULL)
2773 ) {
2774 return &scic->remote_node_context_table[node_id];
2775 }
2776
2777 return NULL;
2778}
2779
2780/**
2781 *
2782 * @resposne_buffer: This is the buffer into which the D2H register FIS will be
2783 * constructed.
2784 * @frame_header: This is the frame header returned by the hardware.
2785 * @frame_buffer: This is the frame buffer returned by the hardware.
2786 *
2787 * This method will combind the frame header and frame buffer to create a SATA
2788 * D2H register FIS none
2789 */
2790void scic_sds_controller_copy_sata_response(
2791 void *response_buffer,
2792 void *frame_header,
2793 void *frame_buffer)
2794{
2795 memcpy(response_buffer, frame_header, sizeof(u32));
2796
2797 memcpy(response_buffer + sizeof(u32),
2798 frame_buffer,
2799 sizeof(struct dev_to_host_fis) - sizeof(u32));
2800}
2801
2802/**
2803 * This method releases the frame once this is done the frame is available for
2804 * re-use by the hardware. The data contained in the frame header and frame
2805 * buffer is no longer valid. The UF queue get pointer is only updated if UF
2806 * control indicates this is appropriate.
2807 * @scic:
2808 * @frame_index:
2809 *
2810 */
2811void scic_sds_controller_release_frame(
2812 struct scic_sds_controller *scic,
2813 u32 frame_index)
2814{
2815 if (scic_sds_unsolicited_frame_control_release_frame(
2816 &scic->uf_control, frame_index) == true)
2817 writel(scic->uf_control.get,
2818 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
2819}
2820
2821/**
2822 * scic_controller_start_io() - This method is called by the SCI user to
2823 * send/start an IO request. If the method invocation is successful, then
2824 * the IO request has been queued to the hardware for processing.
2825 * @controller: the handle to the controller object for which to start an IO
2826 * request.
2827 * @remote_device: the handle to the remote device object for which to start an
2828 * IO request.
2829 * @io_request: the handle to the io request object to start.
2830 * @io_tag: This parameter specifies a previously allocated IO tag that the
2831 * user desires to be utilized for this request. This parameter is optional.
2832 * The user is allowed to supply SCI_CONTROLLER_INVALID_IO_TAG as the value
2833 * for this parameter.
2834 *
2835 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2836 * to ensure that each of the methods that may allocate or free available IO
2837 * tags are handled in a mutually exclusive manner. This method is one of said
2838 * methods requiring proper critical code section protection (e.g. semaphore,
2839 * spin-lock, etc.). - For SATA, the user is required to manage NCQ tags. As a
2840 * result, it is expected the user will have set the NCQ tag field in the host
2841 * to device register FIS prior to calling this method. There is also a
2842 * requirement for the user to call scic_stp_io_set_ncq_tag() prior to invoking
2843 * the scic_controller_start_io() method. scic_controller_allocate_tag() for
2844 * more information on allocating a tag. Indicate if the controller
2845 * successfully started the IO request. SCI_SUCCESS if the IO request was
2846 * successfully started. Determine the failure situations and return values.
2847 */
2848enum sci_status scic_controller_start_io(
2849 struct scic_sds_controller *scic,
2850 struct scic_sds_remote_device *rdev,
2851 struct scic_sds_request *req,
2852 u16 io_tag)
2853{
2854 enum sci_status status;
2855
Edmund Nadolskie3013702011-06-02 00:10:43 +00002856 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002857 dev_warn(scic_to_dev(scic), "invalid state to start I/O");
2858 return SCI_FAILURE_INVALID_STATE;
2859 }
2860
2861 status = scic_sds_remote_device_start_io(scic, rdev, req);
2862 if (status != SCI_SUCCESS)
2863 return status;
2864
2865 scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
2866 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(req));
2867 return SCI_SUCCESS;
2868}
2869
2870/**
2871 * scic_controller_terminate_request() - This method is called by the SCI Core
2872 * user to terminate an ongoing (i.e. started) core IO request. This does
2873 * not abort the IO request at the target, but rather removes the IO request
2874 * from the host controller.
2875 * @controller: the handle to the controller object for which to terminate a
2876 * request.
2877 * @remote_device: the handle to the remote device object for which to
2878 * terminate a request.
2879 * @request: the handle to the io or task management request object to
2880 * terminate.
2881 *
2882 * Indicate if the controller successfully began the terminate process for the
2883 * IO request. SCI_SUCCESS if the terminate process was successfully started
2884 * for the request. Determine the failure situations and return values.
2885 */
2886enum sci_status scic_controller_terminate_request(
2887 struct scic_sds_controller *scic,
2888 struct scic_sds_remote_device *rdev,
2889 struct scic_sds_request *req)
2890{
2891 enum sci_status status;
2892
Edmund Nadolskie3013702011-06-02 00:10:43 +00002893 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002894 dev_warn(scic_to_dev(scic),
2895 "invalid state to terminate request\n");
2896 return SCI_FAILURE_INVALID_STATE;
2897 }
2898
2899 status = scic_sds_io_request_terminate(req);
2900 if (status != SCI_SUCCESS)
2901 return status;
2902
2903 /*
2904 * Utilize the original post context command and or in the POST_TC_ABORT
2905 * request sub-type.
2906 */
2907 scic_sds_controller_post_request(scic,
2908 scic_sds_request_get_post_context(req) |
2909 SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2910 return SCI_SUCCESS;
2911}
2912
2913/**
2914 * scic_controller_complete_io() - This method will perform core specific
2915 * completion operations for an IO request. After this method is invoked,
2916 * the user should consider the IO request as invalid until it is properly
2917 * reused (i.e. re-constructed).
2918 * @controller: The handle to the controller object for which to complete the
2919 * IO request.
2920 * @remote_device: The handle to the remote device object for which to complete
2921 * the IO request.
2922 * @io_request: the handle to the io request object to complete.
2923 *
2924 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2925 * to ensure that each of the methods that may allocate or free available IO
2926 * tags are handled in a mutually exclusive manner. This method is one of said
2927 * methods requiring proper critical code section protection (e.g. semaphore,
2928 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
2929 * Core user, using the scic_controller_allocate_io_tag() method, then it is
2930 * the responsibility of the caller to invoke the scic_controller_free_io_tag()
2931 * method to free the tag (i.e. this method will not free the IO tag). Indicate
2932 * if the controller successfully completed the IO request. SCI_SUCCESS if the
2933 * completion process was successful.
2934 */
2935enum sci_status scic_controller_complete_io(
2936 struct scic_sds_controller *scic,
2937 struct scic_sds_remote_device *rdev,
2938 struct scic_sds_request *request)
2939{
2940 enum sci_status status;
2941 u16 index;
2942
Edmund Nadolskie3013702011-06-02 00:10:43 +00002943 switch (scic->sm.current_state_id) {
2944 case SCIC_STOPPING:
Dan Williamscc9203b2011-05-08 17:34:44 -07002945 /* XXX: Implement this function */
2946 return SCI_FAILURE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002947 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002948 status = scic_sds_remote_device_complete_io(scic, rdev, request);
2949 if (status != SCI_SUCCESS)
2950 return status;
2951
2952 index = scic_sds_io_tag_get_index(request->io_tag);
2953 scic->io_request_table[index] = NULL;
2954 return SCI_SUCCESS;
2955 default:
2956 dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
2957 return SCI_FAILURE_INVALID_STATE;
2958 }
2959
2960}
2961
2962enum sci_status scic_controller_continue_io(struct scic_sds_request *sci_req)
2963{
2964 struct scic_sds_controller *scic = sci_req->owning_controller;
2965
Edmund Nadolskie3013702011-06-02 00:10:43 +00002966 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002967 dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
2968 return SCI_FAILURE_INVALID_STATE;
2969 }
2970
2971 scic->io_request_table[scic_sds_io_tag_get_index(sci_req->io_tag)] = sci_req;
2972 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(sci_req));
2973 return SCI_SUCCESS;
2974}
2975
2976/**
2977 * scic_controller_start_task() - This method is called by the SCIC user to
2978 * send/start a framework task management request.
2979 * @controller: the handle to the controller object for which to start the task
2980 * management request.
2981 * @remote_device: the handle to the remote device object for which to start
2982 * the task management request.
2983 * @task_request: the handle to the task request object to start.
2984 * @io_tag: This parameter specifies a previously allocated IO tag that the
2985 * user desires to be utilized for this request. Note this not the io_tag
2986 * of the request being managed. It is to be utilized for the task request
2987 * itself. This parameter is optional. The user is allowed to supply
2988 * SCI_CONTROLLER_INVALID_IO_TAG as the value for this parameter.
2989 *
2990 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2991 * to ensure that each of the methods that may allocate or free available IO
2992 * tags are handled in a mutually exclusive manner. This method is one of said
2993 * methods requiring proper critical code section protection (e.g. semaphore,
2994 * spin-lock, etc.). - The user must synchronize this task with completion
2995 * queue processing. If they are not synchronized then it is possible for the
2996 * io requests that are being managed by the task request can complete before
2997 * starting the task request. scic_controller_allocate_tag() for more
2998 * information on allocating a tag. Indicate if the controller successfully
2999 * started the IO request. SCI_TASK_SUCCESS if the task request was
3000 * successfully started. SCI_TASK_FAILURE_REQUIRES_SCSI_ABORT This value is
3001 * returned if there is/are task(s) outstanding that require termination or
3002 * completion before this request can succeed.
3003 */
3004enum sci_task_status scic_controller_start_task(
3005 struct scic_sds_controller *scic,
3006 struct scic_sds_remote_device *rdev,
3007 struct scic_sds_request *req,
3008 u16 task_tag)
3009{
3010 enum sci_status status;
3011
Edmund Nadolskie3013702011-06-02 00:10:43 +00003012 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07003013 dev_warn(scic_to_dev(scic),
3014 "%s: SCIC Controller starting task from invalid "
3015 "state\n",
3016 __func__);
3017 return SCI_TASK_FAILURE_INVALID_STATE;
3018 }
3019
3020 status = scic_sds_remote_device_start_task(scic, rdev, req);
3021 switch (status) {
3022 case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
3023 scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
3024
3025 /*
3026 * We will let framework know this task request started successfully,
3027 * although core is still woring on starting the request (to post tc when
3028 * RNC is resumed.)
3029 */
3030 return SCI_SUCCESS;
3031 case SCI_SUCCESS:
3032 scic->io_request_table[scic_sds_io_tag_get_index(req->io_tag)] = req;
3033
3034 scic_sds_controller_post_request(scic,
3035 scic_sds_request_get_post_context(req));
3036 break;
3037 default:
3038 break;
3039 }
3040
3041 return status;
3042}
3043
3044/**
3045 * scic_controller_allocate_io_tag() - This method will allocate a tag from the
3046 * pool of free IO tags. Direct allocation of IO tags by the SCI Core user
3047 * is optional. The scic_controller_start_io() method will allocate an IO
3048 * tag if this method is not utilized and the tag is not supplied to the IO
3049 * construct routine. Direct allocation of IO tags may provide additional
3050 * performance improvements in environments capable of supporting this usage
3051 * model. Additionally, direct allocation of IO tags also provides
3052 * additional flexibility to the SCI Core user. Specifically, the user may
3053 * retain IO tags across the lives of multiple IO requests.
3054 * @controller: the handle to the controller object for which to allocate the
3055 * tag.
3056 *
3057 * IO tags are a protected resource. It is incumbent upon the SCI Core user to
3058 * ensure that each of the methods that may allocate or free available IO tags
3059 * are handled in a mutually exclusive manner. This method is one of said
3060 * methods requiring proper critical code section protection (e.g. semaphore,
3061 * spin-lock, etc.). An unsigned integer representing an available IO tag.
3062 * SCI_CONTROLLER_INVALID_IO_TAG This value is returned if there are no
3063 * currently available tags to be allocated. All return other values indicate a
3064 * legitimate tag.
3065 */
3066u16 scic_controller_allocate_io_tag(
3067 struct scic_sds_controller *scic)
3068{
3069 u16 task_context;
3070 u16 sequence_count;
3071
3072 if (!sci_pool_empty(scic->tci_pool)) {
3073 sci_pool_get(scic->tci_pool, task_context);
3074
3075 sequence_count = scic->io_request_sequence[task_context];
3076
3077 return scic_sds_io_tag_construct(sequence_count, task_context);
3078 }
3079
3080 return SCI_CONTROLLER_INVALID_IO_TAG;
3081}
3082
3083/**
3084 * scic_controller_free_io_tag() - This method will free an IO tag to the pool
3085 * of free IO tags. This method provides the SCI Core user more flexibility
3086 * with regards to IO tags. The user may desire to keep an IO tag after an
3087 * IO request has completed, because they plan on re-using the tag for a
3088 * subsequent IO request. This method is only legal if the tag was
3089 * allocated via scic_controller_allocate_io_tag().
3090 * @controller: This parameter specifies the handle to the controller object
3091 * for which to free/return the tag.
3092 * @io_tag: This parameter represents the tag to be freed to the pool of
3093 * available tags.
3094 *
3095 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
3096 * to ensure that each of the methods that may allocate or free available IO
3097 * tags are handled in a mutually exclusive manner. This method is one of said
3098 * methods requiring proper critical code section protection (e.g. semaphore,
3099 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
3100 * Core user, using the scic_controller_allocate_io_tag() method, then it is
3101 * the responsibility of the caller to invoke this method to free the tag. This
3102 * method returns an indication of whether the tag was successfully put back
3103 * (freed) to the pool of available tags. SCI_SUCCESS This return value
3104 * indicates the tag was successfully placed into the pool of available IO
3105 * tags. SCI_FAILURE_INVALID_IO_TAG This value is returned if the supplied tag
3106 * is not a valid IO tag value.
3107 */
3108enum sci_status scic_controller_free_io_tag(
3109 struct scic_sds_controller *scic,
3110 u16 io_tag)
3111{
3112 u16 sequence;
3113 u16 index;
3114
3115 BUG_ON(io_tag == SCI_CONTROLLER_INVALID_IO_TAG);
3116
3117 sequence = scic_sds_io_tag_get_sequence(io_tag);
3118 index = scic_sds_io_tag_get_index(io_tag);
3119
3120 if (!sci_pool_full(scic->tci_pool)) {
3121 if (sequence == scic->io_request_sequence[index]) {
3122 scic_sds_io_sequence_increment(
3123 scic->io_request_sequence[index]);
3124
3125 sci_pool_put(scic->tci_pool, index);
3126
3127 return SCI_SUCCESS;
3128 }
3129 }
3130
3131 return SCI_FAILURE_INVALID_IO_TAG;
3132}
3133
3134