blob: 3c7042b8bc0e2fc0e3f448196b9e8935a616ae7d [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
Dan Williamsac668c62011-06-07 18:50:55 -070055#include <linux/circ_buf.h>
Dan Williamscc9203b2011-05-08 17:34:44 -070056#include <linux/device.h>
57#include <scsi/sas.h>
58#include "host.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070059#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070060#include "port.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070061#include "host.h"
Dan Williamsd044af12011-03-08 09:52:49 -080062#include "probe_roms.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070063#include "remote_device.h"
64#include "request.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070065#include "scu_completion_codes.h"
66#include "scu_event_codes.h"
Dan Williams63a3a152011-05-08 21:36:46 -070067#include "registers.h"
Dan Williamscc9203b2011-05-08 17:34:44 -070068#include "scu_remote_node_context.h"
69#include "scu_task_context.h"
70#include "scu_unsolicited_frame.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070071
Dan Williamscc9203b2011-05-08 17:34:44 -070072#define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
73
Dan Williams7c78da32011-06-01 16:00:01 -070074#define smu_max_ports(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070075 (\
76 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
77 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
78 )
79
Dan Williams7c78da32011-06-01 16:00:01 -070080#define smu_max_task_contexts(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070081 (\
82 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
83 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
84 )
85
Dan Williams7c78da32011-06-01 16:00:01 -070086#define smu_max_rncs(dcc_value) \
Dan Williamscc9203b2011-05-08 17:34:44 -070087 (\
88 (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
89 >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
90 )
91
Dan Williamscc9203b2011-05-08 17:34:44 -070092#define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
93
94/**
95 *
96 *
97 * The number of milliseconds to wait while a given phy is consuming power
98 * before allowing another set of phys to consume power. Ultimately, this will
99 * be specified by OEM parameter.
100 */
101#define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
102
103/**
104 * NORMALIZE_PUT_POINTER() -
105 *
106 * This macro will normalize the completion queue put pointer so its value can
107 * be used as an array inde
108 */
109#define NORMALIZE_PUT_POINTER(x) \
110 ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
111
112
113/**
114 * NORMALIZE_EVENT_POINTER() -
115 *
116 * This macro will normalize the completion queue event entry so its value can
117 * be used as an index.
118 */
119#define NORMALIZE_EVENT_POINTER(x) \
120 (\
121 ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
122 >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
123 )
124
125/**
126 * INCREMENT_COMPLETION_QUEUE_GET() -
127 *
128 * This macro will increment the controllers completion queue index value and
129 * possibly toggle the cycle bit if the completion queue index wraps back to 0.
130 */
131#define INCREMENT_COMPLETION_QUEUE_GET(controller, index, cycle) \
132 INCREMENT_QUEUE_GET(\
133 (index), \
134 (cycle), \
Dan Williams7c78da32011-06-01 16:00:01 -0700135 SCU_MAX_COMPLETION_QUEUE_ENTRIES, \
136 SMU_CQGR_CYCLE_BIT)
Dan Williamscc9203b2011-05-08 17:34:44 -0700137
138/**
139 * INCREMENT_EVENT_QUEUE_GET() -
140 *
141 * This macro will increment the controllers event queue index value and
142 * possibly toggle the event cycle bit if the event queue index wraps back to 0.
143 */
144#define INCREMENT_EVENT_QUEUE_GET(controller, index, cycle) \
145 INCREMENT_QUEUE_GET(\
146 (index), \
147 (cycle), \
Dan Williams7c78da32011-06-01 16:00:01 -0700148 SCU_MAX_EVENTS, \
Dan Williamscc9203b2011-05-08 17:34:44 -0700149 SMU_CQGR_EVENT_CYCLE_BIT \
150 )
151
152
153/**
154 * NORMALIZE_GET_POINTER() -
155 *
156 * This macro will normalize the completion queue get pointer so its value can
157 * be used as an index into an array
158 */
159#define NORMALIZE_GET_POINTER(x) \
160 ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
161
162/**
163 * NORMALIZE_GET_POINTER_CYCLE_BIT() -
164 *
165 * This macro will normalize the completion queue cycle pointer so it matches
166 * the completion queue cycle bit
167 */
168#define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
169 ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
170
171/**
172 * COMPLETION_QUEUE_CYCLE_BIT() -
173 *
174 * This macro will return the cycle bit of the completion queue entry
175 */
176#define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
177
Edmund Nadolski12ef6542011-06-02 00:10:50 +0000178/* Init the state machine and call the state entry function (if any) */
179void sci_init_sm(struct sci_base_state_machine *sm,
180 const struct sci_base_state *state_table, u32 initial_state)
181{
182 sci_state_transition_t handler;
183
184 sm->initial_state_id = initial_state;
185 sm->previous_state_id = initial_state;
186 sm->current_state_id = initial_state;
187 sm->state_table = state_table;
188
189 handler = sm->state_table[initial_state].enter_state;
190 if (handler)
191 handler(sm);
192}
193
194/* Call the state exit fn, update the current state, call the state entry fn */
195void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
196{
197 sci_state_transition_t handler;
198
199 handler = sm->state_table[sm->current_state_id].exit_state;
200 if (handler)
201 handler(sm);
202
203 sm->previous_state_id = sm->current_state_id;
204 sm->current_state_id = next_state;
205
206 handler = sm->state_table[sm->current_state_id].enter_state;
207 if (handler)
208 handler(sm);
209}
210
Dan Williamscc9203b2011-05-08 17:34:44 -0700211static bool scic_sds_controller_completion_queue_has_entries(
212 struct scic_sds_controller *scic)
213{
214 u32 get_value = scic->completion_queue_get;
215 u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
216
217 if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
218 COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index]))
219 return true;
220
221 return false;
222}
223
224static bool scic_sds_controller_isr(struct scic_sds_controller *scic)
225{
226 if (scic_sds_controller_completion_queue_has_entries(scic)) {
227 return true;
228 } else {
229 /*
230 * we have a spurious interrupt it could be that we have already
231 * emptied the completion queue from a previous interrupt */
232 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
233
234 /*
235 * There is a race in the hardware that could cause us not to be notified
236 * of an interrupt completion if we do not take this step. We will mask
237 * then unmask the interrupts so if there is another interrupt pending
238 * the clearing of the interrupt source we get the next interrupt message. */
239 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
240 writel(0, &scic->smu_registers->interrupt_mask);
241 }
242
243 return false;
244}
245
Dan Williamsc7ef4032011-02-18 09:25:05 -0800246irqreturn_t isci_msix_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700247{
Dan Williamsc7ef4032011-02-18 09:25:05 -0800248 struct isci_host *ihost = data;
Dan Williams6f231dd2011-07-02 22:56:22 -0700249
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000250 if (scic_sds_controller_isr(&ihost->sci))
Dan Williams0cf89d12011-02-18 09:25:07 -0800251 tasklet_schedule(&ihost->completion_tasklet);
Dan Williams6f231dd2011-07-02 22:56:22 -0700252
Dan Williamsc7ef4032011-02-18 09:25:05 -0800253 return IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700254}
255
Dan Williamscc9203b2011-05-08 17:34:44 -0700256static bool scic_sds_controller_error_isr(struct scic_sds_controller *scic)
257{
258 u32 interrupt_status;
259
260 interrupt_status =
261 readl(&scic->smu_registers->interrupt_status);
262 interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
263
264 if (interrupt_status != 0) {
265 /*
266 * There is an error interrupt pending so let it through and handle
267 * in the callback */
268 return true;
269 }
270
271 /*
272 * There is a race in the hardware that could cause us not to be notified
273 * of an interrupt completion if we do not take this step. We will mask
274 * then unmask the error interrupts so if there was another interrupt
275 * pending we will be notified.
276 * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
277 writel(0xff, &scic->smu_registers->interrupt_mask);
278 writel(0, &scic->smu_registers->interrupt_mask);
279
280 return false;
281}
282
283static void scic_sds_controller_task_completion(struct scic_sds_controller *scic,
284 u32 completion_entry)
285{
286 u32 index;
Dan Williamsdd047c82011-06-09 11:06:58 -0700287 struct scic_sds_request *sci_req;
Dan Williamscc9203b2011-05-08 17:34:44 -0700288
289 index = SCU_GET_COMPLETION_INDEX(completion_entry);
Dan Williamsdd047c82011-06-09 11:06:58 -0700290 sci_req = scic->io_request_table[index];
Dan Williamscc9203b2011-05-08 17:34:44 -0700291
292 /* Make sure that we really want to process this IO request */
Dan Williamsdd047c82011-06-09 11:06:58 -0700293 if (sci_req && sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
294 ISCI_TAG_SEQ(sci_req->io_tag) == scic->io_request_sequence[index])
Dan Williamscc9203b2011-05-08 17:34:44 -0700295 /* Yep this is a valid io request pass it along to the io request handler */
Dan Williamsdd047c82011-06-09 11:06:58 -0700296 scic_sds_io_request_tc_completion(sci_req, completion_entry);
Dan Williamscc9203b2011-05-08 17:34:44 -0700297}
298
299static void scic_sds_controller_sdma_completion(struct scic_sds_controller *scic,
300 u32 completion_entry)
301{
302 u32 index;
303 struct scic_sds_request *io_request;
304 struct scic_sds_remote_device *device;
305
306 index = SCU_GET_COMPLETION_INDEX(completion_entry);
307
308 switch (scu_get_command_request_type(completion_entry)) {
309 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
310 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
311 io_request = scic->io_request_table[index];
312 dev_warn(scic_to_dev(scic),
313 "%s: SCIC SDS Completion type SDMA %x for io request "
314 "%p\n",
315 __func__,
316 completion_entry,
317 io_request);
318 /* @todo For a post TC operation we need to fail the IO
319 * request
320 */
321 break;
322
323 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
324 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
325 case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
326 device = scic->device_table[index];
327 dev_warn(scic_to_dev(scic),
328 "%s: SCIC SDS Completion type SDMA %x for remote "
329 "device %p\n",
330 __func__,
331 completion_entry,
332 device);
333 /* @todo For a port RNC operation we need to fail the
334 * device
335 */
336 break;
337
338 default:
339 dev_warn(scic_to_dev(scic),
340 "%s: SCIC SDS Completion unknown SDMA completion "
341 "type %x\n",
342 __func__,
343 completion_entry);
344 break;
345
346 }
347}
348
349static void scic_sds_controller_unsolicited_frame(struct scic_sds_controller *scic,
350 u32 completion_entry)
351{
352 u32 index;
353 u32 frame_index;
354
355 struct isci_host *ihost = scic_to_ihost(scic);
356 struct scu_unsolicited_frame_header *frame_header;
357 struct scic_sds_phy *phy;
358 struct scic_sds_remote_device *device;
359
360 enum sci_status result = SCI_FAILURE;
361
362 frame_index = SCU_GET_FRAME_INDEX(completion_entry);
363
364 frame_header = scic->uf_control.buffers.array[frame_index].header;
365 scic->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
366
367 if (SCU_GET_FRAME_ERROR(completion_entry)) {
368 /*
369 * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
370 * / this cause a problem? We expect the phy initialization will
371 * / fail if there is an error in the frame. */
372 scic_sds_controller_release_frame(scic, frame_index);
373 return;
374 }
375
376 if (frame_header->is_address_frame) {
377 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
378 phy = &ihost->phys[index].sci;
379 result = scic_sds_phy_frame_handler(phy, frame_index);
380 } else {
381
382 index = SCU_GET_COMPLETION_INDEX(completion_entry);
383
384 if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
385 /*
386 * This is a signature fis or a frame from a direct attached SATA
387 * device that has not yet been created. In either case forwared
388 * the frame to the PE and let it take care of the frame data. */
389 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
390 phy = &ihost->phys[index].sci;
391 result = scic_sds_phy_frame_handler(phy, frame_index);
392 } else {
393 if (index < scic->remote_node_entries)
394 device = scic->device_table[index];
395 else
396 device = NULL;
397
398 if (device != NULL)
399 result = scic_sds_remote_device_frame_handler(device, frame_index);
400 else
401 scic_sds_controller_release_frame(scic, frame_index);
402 }
403 }
404
405 if (result != SCI_SUCCESS) {
406 /*
407 * / @todo Is there any reason to report some additional error message
408 * / when we get this failure notifiction? */
409 }
410}
411
412static void scic_sds_controller_event_completion(struct scic_sds_controller *scic,
413 u32 completion_entry)
414{
415 struct isci_host *ihost = scic_to_ihost(scic);
416 struct scic_sds_request *io_request;
417 struct scic_sds_remote_device *device;
418 struct scic_sds_phy *phy;
419 u32 index;
420
421 index = SCU_GET_COMPLETION_INDEX(completion_entry);
422
423 switch (scu_get_event_type(completion_entry)) {
424 case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
425 /* / @todo The driver did something wrong and we need to fix the condtion. */
426 dev_err(scic_to_dev(scic),
427 "%s: SCIC Controller 0x%p received SMU command error "
428 "0x%x\n",
429 __func__,
430 scic,
431 completion_entry);
432 break;
433
434 case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
435 case SCU_EVENT_TYPE_SMU_ERROR:
436 case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
437 /*
438 * / @todo This is a hardware failure and its likely that we want to
439 * / reset the controller. */
440 dev_err(scic_to_dev(scic),
441 "%s: SCIC Controller 0x%p received fatal controller "
442 "event 0x%x\n",
443 __func__,
444 scic,
445 completion_entry);
446 break;
447
448 case SCU_EVENT_TYPE_TRANSPORT_ERROR:
449 io_request = scic->io_request_table[index];
450 scic_sds_io_request_event_handler(io_request, completion_entry);
451 break;
452
453 case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
454 switch (scu_get_event_specifier(completion_entry)) {
455 case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
456 case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
457 io_request = scic->io_request_table[index];
458 if (io_request != NULL)
459 scic_sds_io_request_event_handler(io_request, completion_entry);
460 else
461 dev_warn(scic_to_dev(scic),
462 "%s: SCIC Controller 0x%p received "
463 "event 0x%x for io request object "
464 "that doesnt exist.\n",
465 __func__,
466 scic,
467 completion_entry);
468
469 break;
470
471 case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
472 device = scic->device_table[index];
473 if (device != NULL)
474 scic_sds_remote_device_event_handler(device, completion_entry);
475 else
476 dev_warn(scic_to_dev(scic),
477 "%s: SCIC Controller 0x%p received "
478 "event 0x%x for remote device object "
479 "that doesnt exist.\n",
480 __func__,
481 scic,
482 completion_entry);
483
484 break;
485 }
486 break;
487
488 case SCU_EVENT_TYPE_BROADCAST_CHANGE:
489 /*
490 * direct the broadcast change event to the phy first and then let
491 * the phy redirect the broadcast change to the port object */
492 case SCU_EVENT_TYPE_ERR_CNT_EVENT:
493 /*
494 * direct error counter event to the phy object since that is where
495 * we get the event notification. This is a type 4 event. */
496 case SCU_EVENT_TYPE_OSSP_EVENT:
497 index = SCU_GET_PROTOCOL_ENGINE_INDEX(completion_entry);
498 phy = &ihost->phys[index].sci;
499 scic_sds_phy_event_handler(phy, completion_entry);
500 break;
501
502 case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
503 case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
504 case SCU_EVENT_TYPE_RNC_OPS_MISC:
505 if (index < scic->remote_node_entries) {
506 device = scic->device_table[index];
507
508 if (device != NULL)
509 scic_sds_remote_device_event_handler(device, completion_entry);
510 } else
511 dev_err(scic_to_dev(scic),
512 "%s: SCIC Controller 0x%p received event 0x%x "
513 "for remote device object 0x%0x that doesnt "
514 "exist.\n",
515 __func__,
516 scic,
517 completion_entry,
518 index);
519
520 break;
521
522 default:
523 dev_warn(scic_to_dev(scic),
524 "%s: SCIC Controller received unknown event code %x\n",
525 __func__,
526 completion_entry);
527 break;
528 }
529}
530
531
532
533static void scic_sds_controller_process_completions(struct scic_sds_controller *scic)
534{
535 u32 completion_count = 0;
536 u32 completion_entry;
537 u32 get_index;
538 u32 get_cycle;
539 u32 event_index;
540 u32 event_cycle;
541
542 dev_dbg(scic_to_dev(scic),
543 "%s: completion queue begining get:0x%08x\n",
544 __func__,
545 scic->completion_queue_get);
546
547 /* Get the component parts of the completion queue */
548 get_index = NORMALIZE_GET_POINTER(scic->completion_queue_get);
549 get_cycle = SMU_CQGR_CYCLE_BIT & scic->completion_queue_get;
550
551 event_index = NORMALIZE_EVENT_POINTER(scic->completion_queue_get);
552 event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & scic->completion_queue_get;
553
554 while (
555 NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
556 == COMPLETION_QUEUE_CYCLE_BIT(scic->completion_queue[get_index])
557 ) {
558 completion_count++;
559
560 completion_entry = scic->completion_queue[get_index];
561 INCREMENT_COMPLETION_QUEUE_GET(scic, get_index, get_cycle);
562
563 dev_dbg(scic_to_dev(scic),
564 "%s: completion queue entry:0x%08x\n",
565 __func__,
566 completion_entry);
567
568 switch (SCU_GET_COMPLETION_TYPE(completion_entry)) {
569 case SCU_COMPLETION_TYPE_TASK:
570 scic_sds_controller_task_completion(scic, completion_entry);
571 break;
572
573 case SCU_COMPLETION_TYPE_SDMA:
574 scic_sds_controller_sdma_completion(scic, completion_entry);
575 break;
576
577 case SCU_COMPLETION_TYPE_UFI:
578 scic_sds_controller_unsolicited_frame(scic, completion_entry);
579 break;
580
581 case SCU_COMPLETION_TYPE_EVENT:
582 INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
583 scic_sds_controller_event_completion(scic, completion_entry);
584 break;
585
586 case SCU_COMPLETION_TYPE_NOTIFY:
587 /*
588 * Presently we do the same thing with a notify event that we do with the
589 * other event codes. */
590 INCREMENT_EVENT_QUEUE_GET(scic, event_index, event_cycle);
591 scic_sds_controller_event_completion(scic, completion_entry);
592 break;
593
594 default:
595 dev_warn(scic_to_dev(scic),
596 "%s: SCIC Controller received unknown "
597 "completion type %x\n",
598 __func__,
599 completion_entry);
600 break;
601 }
602 }
603
604 /* Update the get register if we completed one or more entries */
605 if (completion_count > 0) {
606 scic->completion_queue_get =
607 SMU_CQGR_GEN_BIT(ENABLE) |
608 SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
609 event_cycle |
610 SMU_CQGR_GEN_VAL(EVENT_POINTER, event_index) |
611 get_cycle |
612 SMU_CQGR_GEN_VAL(POINTER, get_index);
613
614 writel(scic->completion_queue_get,
615 &scic->smu_registers->completion_queue_get);
616
617 }
618
619 dev_dbg(scic_to_dev(scic),
620 "%s: completion queue ending get:0x%08x\n",
621 __func__,
622 scic->completion_queue_get);
623
624}
625
626static void scic_sds_controller_error_handler(struct scic_sds_controller *scic)
627{
628 u32 interrupt_status;
629
630 interrupt_status =
631 readl(&scic->smu_registers->interrupt_status);
632
633 if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
634 scic_sds_controller_completion_queue_has_entries(scic)) {
635
636 scic_sds_controller_process_completions(scic);
637 writel(SMU_ISR_QUEUE_SUSPEND, &scic->smu_registers->interrupt_status);
638 } else {
639 dev_err(scic_to_dev(scic), "%s: status: %#x\n", __func__,
640 interrupt_status);
641
Edmund Nadolskie3013702011-06-02 00:10:43 +0000642 sci_change_state(&scic->sm, SCIC_FAILED);
Dan Williamscc9203b2011-05-08 17:34:44 -0700643
644 return;
645 }
646
647 /* If we dont process any completions I am not sure that we want to do this.
648 * We are in the middle of a hardware fault and should probably be reset.
649 */
650 writel(0, &scic->smu_registers->interrupt_mask);
651}
652
Dan Williamsc7ef4032011-02-18 09:25:05 -0800653irqreturn_t isci_intx_isr(int vec, void *data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700654{
Dan Williams6f231dd2011-07-02 22:56:22 -0700655 irqreturn_t ret = IRQ_NONE;
Dan Williams31e824e2011-04-19 12:32:51 -0700656 struct isci_host *ihost = data;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000657 struct scic_sds_controller *scic = &ihost->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700658
Dan Williams31e824e2011-04-19 12:32:51 -0700659 if (scic_sds_controller_isr(scic)) {
660 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
661 tasklet_schedule(&ihost->completion_tasklet);
662 ret = IRQ_HANDLED;
663 } else if (scic_sds_controller_error_isr(scic)) {
664 spin_lock(&ihost->scic_lock);
665 scic_sds_controller_error_handler(scic);
666 spin_unlock(&ihost->scic_lock);
667 ret = IRQ_HANDLED;
Dan Williams6f231dd2011-07-02 22:56:22 -0700668 }
Dan Williams92f4f0f2011-02-18 09:25:11 -0800669
Dan Williams6f231dd2011-07-02 22:56:22 -0700670 return ret;
671}
672
Dan Williams92f4f0f2011-02-18 09:25:11 -0800673irqreturn_t isci_error_isr(int vec, void *data)
674{
675 struct isci_host *ihost = data;
Dan Williams92f4f0f2011-02-18 09:25:11 -0800676
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000677 if (scic_sds_controller_error_isr(&ihost->sci))
678 scic_sds_controller_error_handler(&ihost->sci);
Dan Williams92f4f0f2011-02-18 09:25:11 -0800679
680 return IRQ_HANDLED;
681}
Dan Williams6f231dd2011-07-02 22:56:22 -0700682
683/**
684 * isci_host_start_complete() - This function is called by the core library,
685 * through the ISCI Module, to indicate controller start status.
686 * @isci_host: This parameter specifies the ISCI host object
687 * @completion_status: This parameter specifies the completion status from the
688 * core library.
689 *
690 */
Dan Williamscc9203b2011-05-08 17:34:44 -0700691static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700692{
Dan Williams0cf89d12011-02-18 09:25:07 -0800693 if (completion_status != SCI_SUCCESS)
694 dev_info(&ihost->pdev->dev,
695 "controller start timed out, continuing...\n");
696 isci_host_change_state(ihost, isci_ready);
697 clear_bit(IHOST_START_PENDING, &ihost->flags);
698 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700699}
700
Dan Williamsc7ef4032011-02-18 09:25:05 -0800701int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
Dan Williams6f231dd2011-07-02 22:56:22 -0700702{
Dan Williams4393aa42011-03-31 13:10:44 -0700703 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Dan Williams6f231dd2011-07-02 22:56:22 -0700704
Edmund Nadolski77950f52011-02-18 09:25:09 -0800705 if (test_bit(IHOST_START_PENDING, &ihost->flags))
Dan Williams6f231dd2011-07-02 22:56:22 -0700706 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700707
Edmund Nadolski77950f52011-02-18 09:25:09 -0800708 /* todo: use sas_flush_discovery once it is upstream */
709 scsi_flush_work(shost);
710
711 scsi_flush_work(shost);
Dan Williams6f231dd2011-07-02 22:56:22 -0700712
Dan Williams0cf89d12011-02-18 09:25:07 -0800713 dev_dbg(&ihost->pdev->dev,
714 "%s: ihost->status = %d, time = %ld\n",
715 __func__, isci_host_get_state(ihost), time);
Dan Williams6f231dd2011-07-02 22:56:22 -0700716
Dan Williams6f231dd2011-07-02 22:56:22 -0700717 return 1;
718
719}
720
Dan Williamscc9203b2011-05-08 17:34:44 -0700721/**
722 * scic_controller_get_suggested_start_timeout() - This method returns the
723 * suggested scic_controller_start() timeout amount. The user is free to
724 * use any timeout value, but this method provides the suggested minimum
725 * start timeout value. The returned value is based upon empirical
726 * information determined as a result of interoperability testing.
727 * @controller: the handle to the controller object for which to return the
728 * suggested start timeout.
729 *
730 * This method returns the number of milliseconds for the suggested start
731 * operation timeout.
732 */
733static u32 scic_controller_get_suggested_start_timeout(
734 struct scic_sds_controller *sc)
735{
736 /* Validate the user supplied parameters. */
737 if (sc == NULL)
738 return 0;
739
740 /*
741 * The suggested minimum timeout value for a controller start operation:
742 *
743 * Signature FIS Timeout
744 * + Phy Start Timeout
745 * + Number of Phy Spin Up Intervals
746 * ---------------------------------
747 * Number of milliseconds for the controller start operation.
748 *
749 * NOTE: The number of phy spin up intervals will be equivalent
750 * to the number of phys divided by the number phys allowed
751 * per interval - 1 (once OEM parameters are supported).
752 * Currently we assume only 1 phy per interval. */
753
754 return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
755 + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
756 + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
757}
758
759static void scic_controller_enable_interrupts(
760 struct scic_sds_controller *scic)
761{
762 BUG_ON(scic->smu_registers == NULL);
763 writel(0, &scic->smu_registers->interrupt_mask);
764}
765
766void scic_controller_disable_interrupts(
767 struct scic_sds_controller *scic)
768{
769 BUG_ON(scic->smu_registers == NULL);
770 writel(0xffffffff, &scic->smu_registers->interrupt_mask);
771}
772
773static void scic_sds_controller_enable_port_task_scheduler(
774 struct scic_sds_controller *scic)
775{
776 u32 port_task_scheduler_value;
777
778 port_task_scheduler_value =
779 readl(&scic->scu_registers->peg0.ptsg.control);
780 port_task_scheduler_value |=
781 (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
782 SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
783 writel(port_task_scheduler_value,
784 &scic->scu_registers->peg0.ptsg.control);
785}
786
787static void scic_sds_controller_assign_task_entries(struct scic_sds_controller *scic)
788{
789 u32 task_assignment;
790
791 /*
792 * Assign all the TCs to function 0
793 * TODO: Do we actually need to read this register to write it back?
794 */
795
796 task_assignment =
797 readl(&scic->smu_registers->task_context_assignment[0]);
798
799 task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
800 (SMU_TCA_GEN_VAL(ENDING, scic->task_context_entries - 1)) |
801 (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
802
803 writel(task_assignment,
804 &scic->smu_registers->task_context_assignment[0]);
805
806}
807
808static void scic_sds_controller_initialize_completion_queue(struct scic_sds_controller *scic)
809{
810 u32 index;
811 u32 completion_queue_control_value;
812 u32 completion_queue_get_value;
813 u32 completion_queue_put_value;
814
815 scic->completion_queue_get = 0;
816
Dan Williams7c78da32011-06-01 16:00:01 -0700817 completion_queue_control_value =
818 (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
819 SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
Dan Williamscc9203b2011-05-08 17:34:44 -0700820
821 writel(completion_queue_control_value,
822 &scic->smu_registers->completion_queue_control);
823
824
825 /* Set the completion queue get pointer and enable the queue */
826 completion_queue_get_value = (
827 (SMU_CQGR_GEN_VAL(POINTER, 0))
828 | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
829 | (SMU_CQGR_GEN_BIT(ENABLE))
830 | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
831 );
832
833 writel(completion_queue_get_value,
834 &scic->smu_registers->completion_queue_get);
835
836 /* Set the completion queue put pointer */
837 completion_queue_put_value = (
838 (SMU_CQPR_GEN_VAL(POINTER, 0))
839 | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
840 );
841
842 writel(completion_queue_put_value,
843 &scic->smu_registers->completion_queue_put);
844
845 /* Initialize the cycle bit of the completion queue entries */
Dan Williams7c78da32011-06-01 16:00:01 -0700846 for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700847 /*
848 * If get.cycle_bit != completion_queue.cycle_bit
849 * its not a valid completion queue entry
850 * so at system start all entries are invalid */
851 scic->completion_queue[index] = 0x80000000;
852 }
853}
854
855static void scic_sds_controller_initialize_unsolicited_frame_queue(struct scic_sds_controller *scic)
856{
857 u32 frame_queue_control_value;
858 u32 frame_queue_get_value;
859 u32 frame_queue_put_value;
860
861 /* Write the queue size */
862 frame_queue_control_value =
Dan Williams7c78da32011-06-01 16:00:01 -0700863 SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
Dan Williamscc9203b2011-05-08 17:34:44 -0700864
865 writel(frame_queue_control_value,
866 &scic->scu_registers->sdma.unsolicited_frame_queue_control);
867
868 /* Setup the get pointer for the unsolicited frame queue */
869 frame_queue_get_value = (
870 SCU_UFQGP_GEN_VAL(POINTER, 0)
871 | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
872 );
873
874 writel(frame_queue_get_value,
875 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
876 /* Setup the put pointer for the unsolicited frame queue */
877 frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
878 writel(frame_queue_put_value,
879 &scic->scu_registers->sdma.unsolicited_frame_put_pointer);
880}
881
882/**
883 * This method will attempt to transition into the ready state for the
884 * controller and indicate that the controller start operation has completed
885 * if all criteria are met.
886 * @scic: This parameter indicates the controller object for which
887 * to transition to ready.
888 * @status: This parameter indicates the status value to be pass into the call
889 * to scic_cb_controller_start_complete().
890 *
891 * none.
892 */
893static void scic_sds_controller_transition_to_ready(
894 struct scic_sds_controller *scic,
895 enum sci_status status)
896{
897 struct isci_host *ihost = scic_to_ihost(scic);
898
Edmund Nadolskie3013702011-06-02 00:10:43 +0000899 if (scic->sm.current_state_id == SCIC_STARTING) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700900 /*
901 * We move into the ready state, because some of the phys/ports
902 * may be up and operational.
903 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000904 sci_change_state(&scic->sm, SCIC_READY);
Dan Williamscc9203b2011-05-08 17:34:44 -0700905
906 isci_host_start_complete(ihost, status);
907 }
908}
909
Adam Gruchala4a33c522011-05-10 23:54:23 +0000910static bool is_phy_starting(struct scic_sds_phy *sci_phy)
911{
912 enum scic_sds_phy_states state;
913
Edmund Nadolskie3013702011-06-02 00:10:43 +0000914 state = sci_phy->sm.current_state_id;
Adam Gruchala4a33c522011-05-10 23:54:23 +0000915 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000916 case SCI_PHY_STARTING:
917 case SCI_PHY_SUB_INITIAL:
918 case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
919 case SCI_PHY_SUB_AWAIT_IAF_UF:
920 case SCI_PHY_SUB_AWAIT_SAS_POWER:
921 case SCI_PHY_SUB_AWAIT_SATA_POWER:
922 case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
923 case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
924 case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
925 case SCI_PHY_SUB_FINAL:
Adam Gruchala4a33c522011-05-10 23:54:23 +0000926 return true;
927 default:
928 return false;
929 }
930}
931
Dan Williamscc9203b2011-05-08 17:34:44 -0700932/**
933 * scic_sds_controller_start_next_phy - start phy
934 * @scic: controller
935 *
936 * If all the phys have been started, then attempt to transition the
937 * controller to the READY state and inform the user
938 * (scic_cb_controller_start_complete()).
939 */
940static enum sci_status scic_sds_controller_start_next_phy(struct scic_sds_controller *scic)
941{
942 struct isci_host *ihost = scic_to_ihost(scic);
943 struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
944 struct scic_sds_phy *sci_phy;
945 enum sci_status status;
946
947 status = SCI_SUCCESS;
948
949 if (scic->phy_startup_timer_pending)
950 return status;
951
952 if (scic->next_phy_to_start >= SCI_MAX_PHYS) {
953 bool is_controller_start_complete = true;
954 u32 state;
955 u8 index;
956
957 for (index = 0; index < SCI_MAX_PHYS; index++) {
958 sci_phy = &ihost->phys[index].sci;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000959 state = sci_phy->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -0700960
Dan Williams4f20ef42011-05-12 06:00:31 -0700961 if (!phy_get_non_dummy_port(sci_phy))
Dan Williamscc9203b2011-05-08 17:34:44 -0700962 continue;
963
964 /* The controller start operation is complete iff:
965 * - all links have been given an opportunity to start
966 * - have no indication of a connected device
967 * - have an indication of a connected device and it has
968 * finished the link training process.
969 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000970 if ((sci_phy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
971 (sci_phy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
972 (sci_phy->is_in_link_training == true && is_phy_starting(sci_phy))) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700973 is_controller_start_complete = false;
974 break;
975 }
976 }
977
978 /*
979 * The controller has successfully finished the start process.
980 * Inform the SCI Core user and transition to the READY state. */
981 if (is_controller_start_complete == true) {
982 scic_sds_controller_transition_to_ready(scic, SCI_SUCCESS);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -0700983 sci_del_timer(&scic->phy_timer);
984 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -0700985 }
986 } else {
987 sci_phy = &ihost->phys[scic->next_phy_to_start].sci;
988
989 if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
Dan Williams4f20ef42011-05-12 06:00:31 -0700990 if (phy_get_non_dummy_port(sci_phy) == NULL) {
Dan Williamscc9203b2011-05-08 17:34:44 -0700991 scic->next_phy_to_start++;
992
993 /* Caution recursion ahead be forwarned
994 *
995 * The PHY was never added to a PORT in MPC mode
996 * so start the next phy in sequence This phy
997 * will never go link up and will not draw power
998 * the OEM parameters either configured the phy
999 * incorrectly for the PORT or it was never
1000 * assigned to a PORT
1001 */
1002 return scic_sds_controller_start_next_phy(scic);
1003 }
1004 }
1005
1006 status = scic_sds_phy_start(sci_phy);
1007
1008 if (status == SCI_SUCCESS) {
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001009 sci_mod_timer(&scic->phy_timer,
1010 SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
1011 scic->phy_startup_timer_pending = true;
Dan Williamscc9203b2011-05-08 17:34:44 -07001012 } else {
1013 dev_warn(scic_to_dev(scic),
1014 "%s: Controller stop operation failed "
1015 "to stop phy %d because of status "
1016 "%d.\n",
1017 __func__,
1018 ihost->phys[scic->next_phy_to_start].sci.phy_index,
1019 status);
1020 }
1021
1022 scic->next_phy_to_start++;
1023 }
1024
1025 return status;
1026}
1027
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001028static void phy_startup_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001029{
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001030 struct sci_timer *tmr = (struct sci_timer *)data;
1031 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), phy_timer);
1032 struct isci_host *ihost = scic_to_ihost(scic);
1033 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001034 enum sci_status status;
1035
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001036 spin_lock_irqsave(&ihost->scic_lock, flags);
1037
1038 if (tmr->cancel)
1039 goto done;
1040
Dan Williamscc9203b2011-05-08 17:34:44 -07001041 scic->phy_startup_timer_pending = false;
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001042
1043 do {
Dan Williamscc9203b2011-05-08 17:34:44 -07001044 status = scic_sds_controller_start_next_phy(scic);
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001045 } while (status != SCI_SUCCESS);
1046
1047done:
1048 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001049}
1050
Dan Williamsac668c62011-06-07 18:50:55 -07001051static void isci_tci_free(struct isci_host *ihost, u16 tci)
1052{
1053 u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
1054
1055 ihost->tci_pool[tail] = tci;
1056 ihost->tci_tail = tail + 1;
1057}
1058
1059static u16 isci_tci_alloc(struct isci_host *ihost)
1060{
1061 u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
1062 u16 tci = ihost->tci_pool[head];
1063
1064 ihost->tci_head = head + 1;
1065 return tci;
1066}
1067
1068static u16 isci_tci_active(struct isci_host *ihost)
1069{
1070 return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
1071}
1072
1073static u16 isci_tci_space(struct isci_host *ihost)
1074{
1075 return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
1076}
1077
Dan Williamscc9203b2011-05-08 17:34:44 -07001078static enum sci_status scic_controller_start(struct scic_sds_controller *scic,
1079 u32 timeout)
1080{
1081 struct isci_host *ihost = scic_to_ihost(scic);
1082 enum sci_status result;
1083 u16 index;
1084
Edmund Nadolskie3013702011-06-02 00:10:43 +00001085 if (scic->sm.current_state_id != SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001086 dev_warn(scic_to_dev(scic),
1087 "SCIC Controller start operation requested in "
1088 "invalid state\n");
1089 return SCI_FAILURE_INVALID_STATE;
1090 }
1091
1092 /* Build the TCi free pool */
Dan Williamsac668c62011-06-07 18:50:55 -07001093 BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
1094 ihost->tci_head = 0;
1095 ihost->tci_tail = 0;
Dan Williamscc9203b2011-05-08 17:34:44 -07001096 for (index = 0; index < scic->task_context_entries; index++)
Dan Williamsac668c62011-06-07 18:50:55 -07001097 isci_tci_free(ihost, index);
Dan Williamscc9203b2011-05-08 17:34:44 -07001098
1099 /* Build the RNi free pool */
1100 scic_sds_remote_node_table_initialize(
1101 &scic->available_remote_nodes,
1102 scic->remote_node_entries);
1103
1104 /*
1105 * Before anything else lets make sure we will not be
1106 * interrupted by the hardware.
1107 */
1108 scic_controller_disable_interrupts(scic);
1109
1110 /* Enable the port task scheduler */
1111 scic_sds_controller_enable_port_task_scheduler(scic);
1112
1113 /* Assign all the task entries to scic physical function */
1114 scic_sds_controller_assign_task_entries(scic);
1115
1116 /* Now initialize the completion queue */
1117 scic_sds_controller_initialize_completion_queue(scic);
1118
1119 /* Initialize the unsolicited frame queue for use */
1120 scic_sds_controller_initialize_unsolicited_frame_queue(scic);
1121
1122 /* Start all of the ports on this controller */
1123 for (index = 0; index < scic->logical_port_entries; index++) {
1124 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
1125
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001126 result = scic_sds_port_start(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001127 if (result)
1128 return result;
1129 }
1130
1131 scic_sds_controller_start_next_phy(scic);
1132
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001133 sci_mod_timer(&scic->timer, timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07001134
Edmund Nadolskie3013702011-06-02 00:10:43 +00001135 sci_change_state(&scic->sm, SCIC_STARTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001136
1137 return SCI_SUCCESS;
1138}
1139
Dan Williams6f231dd2011-07-02 22:56:22 -07001140void isci_host_scan_start(struct Scsi_Host *shost)
1141{
Dan Williams4393aa42011-03-31 13:10:44 -07001142 struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001143 unsigned long tmo = scic_controller_get_suggested_start_timeout(&ihost->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001144
Dan Williams0cf89d12011-02-18 09:25:07 -08001145 set_bit(IHOST_START_PENDING, &ihost->flags);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001146
1147 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001148 scic_controller_start(&ihost->sci, tmo);
1149 scic_controller_enable_interrupts(&ihost->sci);
Edmund Nadolski77950f52011-02-18 09:25:09 -08001150 spin_unlock_irq(&ihost->scic_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07001151}
1152
Dan Williamscc9203b2011-05-08 17:34:44 -07001153static void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001154{
Dan Williams0cf89d12011-02-18 09:25:07 -08001155 isci_host_change_state(ihost, isci_stopped);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001156 scic_controller_disable_interrupts(&ihost->sci);
Dan Williams0cf89d12011-02-18 09:25:07 -08001157 clear_bit(IHOST_STOP_PENDING, &ihost->flags);
1158 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001159}
1160
Dan Williamscc9203b2011-05-08 17:34:44 -07001161static void scic_sds_controller_completion_handler(struct scic_sds_controller *scic)
1162{
1163 /* Empty out the completion queue */
1164 if (scic_sds_controller_completion_queue_has_entries(scic))
1165 scic_sds_controller_process_completions(scic);
1166
1167 /* Clear the interrupt and enable all interrupts again */
1168 writel(SMU_ISR_COMPLETION, &scic->smu_registers->interrupt_status);
1169 /* Could we write the value of SMU_ISR_COMPLETION? */
1170 writel(0xFF000000, &scic->smu_registers->interrupt_mask);
1171 writel(0, &scic->smu_registers->interrupt_mask);
1172}
1173
Dan Williams6f231dd2011-07-02 22:56:22 -07001174/**
1175 * isci_host_completion_routine() - This function is the delayed service
1176 * routine that calls the sci core library's completion handler. It's
1177 * scheduled as a tasklet from the interrupt service routine when interrupts
1178 * in use, or set as the timeout function in polled mode.
1179 * @data: This parameter specifies the ISCI host object
1180 *
1181 */
1182static void isci_host_completion_routine(unsigned long data)
1183{
1184 struct isci_host *isci_host = (struct isci_host *)data;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001185 struct list_head completed_request_list;
1186 struct list_head errored_request_list;
1187 struct list_head *current_position;
1188 struct list_head *next_position;
Dan Williams6f231dd2011-07-02 22:56:22 -07001189 struct isci_request *request;
1190 struct isci_request *next_request;
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001191 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -07001192
1193 INIT_LIST_HEAD(&completed_request_list);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001194 INIT_LIST_HEAD(&errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001195
1196 spin_lock_irq(&isci_host->scic_lock);
1197
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001198 scic_sds_controller_completion_handler(&isci_host->sci);
Dan Williamsc7ef4032011-02-18 09:25:05 -08001199
Dan Williams6f231dd2011-07-02 22:56:22 -07001200 /* Take the lists of completed I/Os from the host. */
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001201
Dan Williams6f231dd2011-07-02 22:56:22 -07001202 list_splice_init(&isci_host->requests_to_complete,
1203 &completed_request_list);
1204
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001205 /* Take the list of errored I/Os from the host. */
1206 list_splice_init(&isci_host->requests_to_errorback,
1207 &errored_request_list);
Dan Williams6f231dd2011-07-02 22:56:22 -07001208
1209 spin_unlock_irq(&isci_host->scic_lock);
1210
1211 /* Process any completions in the lists. */
1212 list_for_each_safe(current_position, next_position,
1213 &completed_request_list) {
1214
1215 request = list_entry(current_position, struct isci_request,
1216 completed_node);
1217 task = isci_request_access_task(request);
1218
1219 /* Normal notification (task_done) */
1220 dev_dbg(&isci_host->pdev->dev,
1221 "%s: Normal - request/task = %p/%p\n",
1222 __func__,
1223 request,
1224 task);
1225
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001226 /* Return the task to libsas */
1227 if (task != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001228
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001229 task->lldd_task = NULL;
1230 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1231
1232 /* If the task is already in the abort path,
1233 * the task_done callback cannot be called.
1234 */
1235 task->task_done(task);
1236 }
1237 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001238 /* Free the request object. */
1239 isci_request_free(isci_host, request);
1240 }
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001241 list_for_each_entry_safe(request, next_request, &errored_request_list,
Dan Williams6f231dd2011-07-02 22:56:22 -07001242 completed_node) {
1243
1244 task = isci_request_access_task(request);
1245
1246 /* Use sas_task_abort */
1247 dev_warn(&isci_host->pdev->dev,
1248 "%s: Error - request/task = %p/%p\n",
1249 __func__,
1250 request,
1251 task);
1252
Jeff Skirvin11b00c12011-03-04 14:06:40 -08001253 if (task != NULL) {
1254
1255 /* Put the task into the abort path if it's not there
1256 * already.
1257 */
1258 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
1259 sas_task_abort(task);
1260
1261 } else {
1262 /* This is a case where the request has completed with a
1263 * status such that it needed further target servicing,
1264 * but the sas_task reference has already been removed
1265 * from the request. Since it was errored, it was not
1266 * being aborted, so there is nothing to do except free
1267 * it.
1268 */
1269
1270 spin_lock_irq(&isci_host->scic_lock);
1271 /* Remove the request from the remote device's list
1272 * of pending requests.
1273 */
1274 list_del_init(&request->dev_node);
1275 spin_unlock_irq(&isci_host->scic_lock);
1276
1277 /* Free the request object. */
1278 isci_request_free(isci_host, request);
1279 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001280 }
1281
1282}
1283
Dan Williamscc9203b2011-05-08 17:34:44 -07001284/**
1285 * scic_controller_stop() - This method will stop an individual controller
1286 * object.This method will invoke the associated user callback upon
1287 * completion. The completion callback is called when the following
1288 * conditions are met: -# the method return status is SCI_SUCCESS. -# the
1289 * controller has been quiesced. This method will ensure that all IO
1290 * requests are quiesced, phys are stopped, and all additional operation by
1291 * the hardware is halted.
1292 * @controller: the handle to the controller object to stop.
1293 * @timeout: This parameter specifies the number of milliseconds in which the
1294 * stop operation should complete.
1295 *
1296 * The controller must be in the STARTED or STOPPED state. Indicate if the
1297 * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
1298 * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
1299 * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
1300 * controller is not either in the STARTED or STOPPED states.
1301 */
1302static enum sci_status scic_controller_stop(struct scic_sds_controller *scic,
1303 u32 timeout)
1304{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001305 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001306 dev_warn(scic_to_dev(scic),
1307 "SCIC Controller stop operation requested in "
1308 "invalid state\n");
1309 return SCI_FAILURE_INVALID_STATE;
1310 }
1311
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001312 sci_mod_timer(&scic->timer, timeout);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001313 sci_change_state(&scic->sm, SCIC_STOPPING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001314 return SCI_SUCCESS;
1315}
1316
1317/**
1318 * scic_controller_reset() - This method will reset the supplied core
1319 * controller regardless of the state of said controller. This operation is
1320 * considered destructive. In other words, all current operations are wiped
1321 * out. No IO completions for outstanding devices occur. Outstanding IO
1322 * requests are not aborted or completed at the actual remote device.
1323 * @controller: the handle to the controller object to reset.
1324 *
1325 * Indicate if the controller reset method succeeded or failed in some way.
1326 * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
1327 * the controller reset operation is unable to complete.
1328 */
1329static enum sci_status scic_controller_reset(struct scic_sds_controller *scic)
1330{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001331 switch (scic->sm.current_state_id) {
1332 case SCIC_RESET:
1333 case SCIC_READY:
1334 case SCIC_STOPPED:
1335 case SCIC_FAILED:
Dan Williamscc9203b2011-05-08 17:34:44 -07001336 /*
1337 * The reset operation is not a graceful cleanup, just
1338 * perform the state transition.
1339 */
Edmund Nadolskie3013702011-06-02 00:10:43 +00001340 sci_change_state(&scic->sm, SCIC_RESETTING);
Dan Williamscc9203b2011-05-08 17:34:44 -07001341 return SCI_SUCCESS;
1342 default:
1343 dev_warn(scic_to_dev(scic),
1344 "SCIC Controller reset operation requested in "
1345 "invalid state\n");
1346 return SCI_FAILURE_INVALID_STATE;
1347 }
1348}
1349
Dan Williams0cf89d12011-02-18 09:25:07 -08001350void isci_host_deinit(struct isci_host *ihost)
Dan Williams6f231dd2011-07-02 22:56:22 -07001351{
1352 int i;
1353
Dan Williams0cf89d12011-02-18 09:25:07 -08001354 isci_host_change_state(ihost, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -07001355 for (i = 0; i < SCI_MAX_PORTS; i++) {
Dan Williamse5313812011-05-07 10:11:43 -07001356 struct isci_port *iport = &ihost->ports[i];
Dan Williams0cf89d12011-02-18 09:25:07 -08001357 struct isci_remote_device *idev, *d;
1358
Dan Williamse5313812011-05-07 10:11:43 -07001359 list_for_each_entry_safe(idev, d, &iport->remote_dev_list, node) {
Dan Williams0cf89d12011-02-18 09:25:07 -08001360 isci_remote_device_change_state(idev, isci_stopping);
Dan Williams6ad31fe2011-03-04 12:10:29 -08001361 isci_remote_device_stop(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001362 }
1363 }
1364
Dan Williams0cf89d12011-02-18 09:25:07 -08001365 set_bit(IHOST_STOP_PENDING, &ihost->flags);
Dan Williams7c40a802011-03-02 11:49:26 -08001366
1367 spin_lock_irq(&ihost->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001368 scic_controller_stop(&ihost->sci, SCIC_CONTROLLER_STOP_TIMEOUT);
Dan Williams7c40a802011-03-02 11:49:26 -08001369 spin_unlock_irq(&ihost->scic_lock);
1370
Dan Williams0cf89d12011-02-18 09:25:07 -08001371 wait_for_stop(ihost);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001372 scic_controller_reset(&ihost->sci);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001373
1374 /* Cancel any/all outstanding port timers */
1375 for (i = 0; i < ihost->sci.logical_port_entries; i++) {
1376 struct scic_sds_port *sci_port = &ihost->ports[i].sci;
1377 del_timer_sync(&sci_port->timer.timer);
1378 }
1379
Edmund Nadolskia628d472011-05-19 11:59:36 +00001380 /* Cancel any/all outstanding phy timers */
1381 for (i = 0; i < SCI_MAX_PHYS; i++) {
1382 struct scic_sds_phy *sci_phy = &ihost->phys[i].sci;
1383 del_timer_sync(&sci_phy->sata_timer.timer);
1384 }
1385
Edmund Nadolskiac0eeb42011-05-19 20:00:51 -07001386 del_timer_sync(&ihost->sci.port_agent.timer.timer);
1387
Edmund Nadolski04736612011-05-19 20:17:47 -07001388 del_timer_sync(&ihost->sci.power_control.timer.timer);
1389
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001390 del_timer_sync(&ihost->sci.timer.timer);
1391
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07001392 del_timer_sync(&ihost->sci.phy_timer.timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001393}
1394
Dan Williams6f231dd2011-07-02 22:56:22 -07001395static void __iomem *scu_base(struct isci_host *isci_host)
1396{
1397 struct pci_dev *pdev = isci_host->pdev;
1398 int id = isci_host->id;
1399
1400 return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
1401}
1402
1403static void __iomem *smu_base(struct isci_host *isci_host)
1404{
1405 struct pci_dev *pdev = isci_host->pdev;
1406 int id = isci_host->id;
1407
1408 return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
1409}
1410
Dave Jiangb5f18a22011-03-16 14:57:23 -07001411static void isci_user_parameters_get(
1412 struct isci_host *isci_host,
1413 union scic_user_parameters *scic_user_params)
1414{
1415 struct scic_sds_user_parameters *u = &scic_user_params->sds1;
1416 int i;
1417
1418 for (i = 0; i < SCI_MAX_PHYS; i++) {
1419 struct sci_phy_user_params *u_phy = &u->phys[i];
1420
1421 u_phy->max_speed_generation = phy_gen;
1422
1423 /* we are not exporting these for now */
1424 u_phy->align_insertion_frequency = 0x7f;
1425 u_phy->in_connection_align_insertion_frequency = 0xff;
1426 u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
1427 }
1428
1429 u->stp_inactivity_timeout = stp_inactive_to;
1430 u->ssp_inactivity_timeout = ssp_inactive_to;
1431 u->stp_max_occupancy_timeout = stp_max_occ_to;
1432 u->ssp_max_occupancy_timeout = ssp_max_occ_to;
1433 u->no_outbound_task_timeout = no_outbound_task_to;
1434 u->max_number_concurrent_device_spin_up = max_concurr_spinup;
1435}
1436
Dan Williams9269e0e2011-05-12 07:42:17 -07001437static void scic_sds_controller_initial_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001438{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001439 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001440
Edmund Nadolskie3013702011-06-02 00:10:43 +00001441 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001442}
1443
Dan Williams9269e0e2011-05-12 07:42:17 -07001444static inline void scic_sds_controller_starting_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001445{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001446 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001447
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001448 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001449}
1450
1451#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
1452#define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
1453#define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
1454#define INTERRUPT_COALESCE_NUMBER_MAX 256
1455#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
1456#define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
1457
1458/**
1459 * scic_controller_set_interrupt_coalescence() - This method allows the user to
1460 * configure the interrupt coalescence.
1461 * @controller: This parameter represents the handle to the controller object
1462 * for which its interrupt coalesce register is overridden.
1463 * @coalesce_number: Used to control the number of entries in the Completion
1464 * Queue before an interrupt is generated. If the number of entries exceed
1465 * this number, an interrupt will be generated. The valid range of the input
1466 * is [0, 256]. A setting of 0 results in coalescing being disabled.
1467 * @coalesce_timeout: Timeout value in microseconds. The valid range of the
1468 * input is [0, 2700000] . A setting of 0 is allowed and results in no
1469 * interrupt coalescing timeout.
1470 *
1471 * Indicate if the user successfully set the interrupt coalesce parameters.
1472 * SCI_SUCCESS The user successfully updated the interrutp coalescence.
1473 * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
1474 */
1475static enum sci_status scic_controller_set_interrupt_coalescence(
1476 struct scic_sds_controller *scic_controller,
1477 u32 coalesce_number,
1478 u32 coalesce_timeout)
1479{
1480 u8 timeout_encode = 0;
1481 u32 min = 0;
1482 u32 max = 0;
1483
1484 /* Check if the input parameters fall in the range. */
1485 if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
1486 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1487
1488 /*
1489 * Defined encoding for interrupt coalescing timeout:
1490 * Value Min Max Units
1491 * ----- --- --- -----
1492 * 0 - - Disabled
1493 * 1 13.3 20.0 ns
1494 * 2 26.7 40.0
1495 * 3 53.3 80.0
1496 * 4 106.7 160.0
1497 * 5 213.3 320.0
1498 * 6 426.7 640.0
1499 * 7 853.3 1280.0
1500 * 8 1.7 2.6 us
1501 * 9 3.4 5.1
1502 * 10 6.8 10.2
1503 * 11 13.7 20.5
1504 * 12 27.3 41.0
1505 * 13 54.6 81.9
1506 * 14 109.2 163.8
1507 * 15 218.5 327.7
1508 * 16 436.9 655.4
1509 * 17 873.8 1310.7
1510 * 18 1.7 2.6 ms
1511 * 19 3.5 5.2
1512 * 20 7.0 10.5
1513 * 21 14.0 21.0
1514 * 22 28.0 41.9
1515 * 23 55.9 83.9
1516 * 24 111.8 167.8
1517 * 25 223.7 335.5
1518 * 26 447.4 671.1
1519 * 27 894.8 1342.2
1520 * 28 1.8 2.7 s
1521 * Others Undefined */
1522
1523 /*
1524 * Use the table above to decide the encode of interrupt coalescing timeout
1525 * value for register writing. */
1526 if (coalesce_timeout == 0)
1527 timeout_encode = 0;
1528 else{
1529 /* make the timeout value in unit of (10 ns). */
1530 coalesce_timeout = coalesce_timeout * 100;
1531 min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
1532 max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
1533
1534 /* get the encode of timeout for register writing. */
1535 for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
1536 timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
1537 timeout_encode++) {
1538 if (min <= coalesce_timeout && max > coalesce_timeout)
1539 break;
1540 else if (coalesce_timeout >= max && coalesce_timeout < min * 2
1541 && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
1542 if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
1543 break;
1544 else{
1545 timeout_encode++;
1546 break;
1547 }
1548 } else {
1549 max = max * 2;
1550 min = min * 2;
1551 }
1552 }
1553
1554 if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
1555 /* the value is out of range. */
1556 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1557 }
1558
1559 writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
1560 SMU_ICC_GEN_VAL(TIMER, timeout_encode),
1561 &scic_controller->smu_registers->interrupt_coalesce_control);
1562
1563
1564 scic_controller->interrupt_coalesce_number = (u16)coalesce_number;
1565 scic_controller->interrupt_coalesce_timeout = coalesce_timeout / 100;
1566
1567 return SCI_SUCCESS;
1568}
1569
1570
Dan Williams9269e0e2011-05-12 07:42:17 -07001571static void scic_sds_controller_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001572{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001573 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001574
1575 /* set the default interrupt coalescence number and timeout value. */
1576 scic_controller_set_interrupt_coalescence(scic, 0x10, 250);
1577}
1578
Dan Williams9269e0e2011-05-12 07:42:17 -07001579static void scic_sds_controller_ready_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001580{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001581 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001582
1583 /* disable interrupt coalescence. */
1584 scic_controller_set_interrupt_coalescence(scic, 0, 0);
1585}
1586
1587static enum sci_status scic_sds_controller_stop_phys(struct scic_sds_controller *scic)
1588{
1589 u32 index;
1590 enum sci_status status;
1591 enum sci_status phy_status;
1592 struct isci_host *ihost = scic_to_ihost(scic);
1593
1594 status = SCI_SUCCESS;
1595
1596 for (index = 0; index < SCI_MAX_PHYS; index++) {
1597 phy_status = scic_sds_phy_stop(&ihost->phys[index].sci);
1598
1599 if (phy_status != SCI_SUCCESS &&
1600 phy_status != SCI_FAILURE_INVALID_STATE) {
1601 status = SCI_FAILURE;
1602
1603 dev_warn(scic_to_dev(scic),
1604 "%s: Controller stop operation failed to stop "
1605 "phy %d because of status %d.\n",
1606 __func__,
1607 ihost->phys[index].sci.phy_index, phy_status);
1608 }
1609 }
1610
1611 return status;
1612}
1613
1614static enum sci_status scic_sds_controller_stop_ports(struct scic_sds_controller *scic)
1615{
1616 u32 index;
1617 enum sci_status port_status;
1618 enum sci_status status = SCI_SUCCESS;
1619 struct isci_host *ihost = scic_to_ihost(scic);
1620
1621 for (index = 0; index < scic->logical_port_entries; index++) {
1622 struct scic_sds_port *sci_port = &ihost->ports[index].sci;
Dan Williamscc9203b2011-05-08 17:34:44 -07001623
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001624 port_status = scic_sds_port_stop(sci_port);
Dan Williamscc9203b2011-05-08 17:34:44 -07001625
1626 if ((port_status != SCI_SUCCESS) &&
1627 (port_status != SCI_FAILURE_INVALID_STATE)) {
1628 status = SCI_FAILURE;
1629
1630 dev_warn(scic_to_dev(scic),
1631 "%s: Controller stop operation failed to "
1632 "stop port %d because of status %d.\n",
1633 __func__,
1634 sci_port->logical_port_index,
1635 port_status);
1636 }
1637 }
1638
1639 return status;
1640}
1641
1642static enum sci_status scic_sds_controller_stop_devices(struct scic_sds_controller *scic)
1643{
1644 u32 index;
1645 enum sci_status status;
1646 enum sci_status device_status;
1647
1648 status = SCI_SUCCESS;
1649
1650 for (index = 0; index < scic->remote_node_entries; index++) {
1651 if (scic->device_table[index] != NULL) {
1652 /* / @todo What timeout value do we want to provide to this request? */
1653 device_status = scic_remote_device_stop(scic->device_table[index], 0);
1654
1655 if ((device_status != SCI_SUCCESS) &&
1656 (device_status != SCI_FAILURE_INVALID_STATE)) {
1657 dev_warn(scic_to_dev(scic),
1658 "%s: Controller stop operation failed "
1659 "to stop device 0x%p because of "
1660 "status %d.\n",
1661 __func__,
1662 scic->device_table[index], device_status);
1663 }
1664 }
1665 }
1666
1667 return status;
1668}
1669
Dan Williams9269e0e2011-05-12 07:42:17 -07001670static void scic_sds_controller_stopping_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001671{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001672 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001673
1674 /* Stop all of the components for this controller */
1675 scic_sds_controller_stop_phys(scic);
1676 scic_sds_controller_stop_ports(scic);
1677 scic_sds_controller_stop_devices(scic);
1678}
1679
Dan Williams9269e0e2011-05-12 07:42:17 -07001680static void scic_sds_controller_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001681{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001682 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001683
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001684 sci_del_timer(&scic->timer);
Dan Williamscc9203b2011-05-08 17:34:44 -07001685}
1686
1687
1688/**
1689 * scic_sds_controller_reset_hardware() -
1690 *
1691 * This method will reset the controller hardware.
1692 */
1693static void scic_sds_controller_reset_hardware(struct scic_sds_controller *scic)
1694{
1695 /* Disable interrupts so we dont take any spurious interrupts */
1696 scic_controller_disable_interrupts(scic);
1697
1698 /* Reset the SCU */
1699 writel(0xFFFFFFFF, &scic->smu_registers->soft_reset_control);
1700
1701 /* Delay for 1ms to before clearing the CQP and UFQPR. */
1702 udelay(1000);
1703
1704 /* The write to the CQGR clears the CQP */
1705 writel(0x00000000, &scic->smu_registers->completion_queue_get);
1706
1707 /* The write to the UFQGP clears the UFQPR */
1708 writel(0, &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
1709}
1710
Dan Williams9269e0e2011-05-12 07:42:17 -07001711static void scic_sds_controller_resetting_state_enter(struct sci_base_state_machine *sm)
Dan Williamscc9203b2011-05-08 17:34:44 -07001712{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001713 struct scic_sds_controller *scic = container_of(sm, typeof(*scic), sm);
Dan Williamscc9203b2011-05-08 17:34:44 -07001714
1715 scic_sds_controller_reset_hardware(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001716 sci_change_state(&scic->sm, SCIC_RESET);
Dan Williamscc9203b2011-05-08 17:34:44 -07001717}
1718
1719static const struct sci_base_state scic_sds_controller_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001720 [SCIC_INITIAL] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001721 .enter_state = scic_sds_controller_initial_state_enter,
1722 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001723 [SCIC_RESET] = {},
1724 [SCIC_INITIALIZING] = {},
1725 [SCIC_INITIALIZED] = {},
1726 [SCIC_STARTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001727 .exit_state = scic_sds_controller_starting_state_exit,
1728 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001729 [SCIC_READY] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001730 .enter_state = scic_sds_controller_ready_state_enter,
1731 .exit_state = scic_sds_controller_ready_state_exit,
1732 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001733 [SCIC_RESETTING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001734 .enter_state = scic_sds_controller_resetting_state_enter,
1735 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001736 [SCIC_STOPPING] = {
Dan Williamscc9203b2011-05-08 17:34:44 -07001737 .enter_state = scic_sds_controller_stopping_state_enter,
1738 .exit_state = scic_sds_controller_stopping_state_exit,
1739 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001740 [SCIC_STOPPED] = {},
1741 [SCIC_FAILED] = {}
Dan Williamscc9203b2011-05-08 17:34:44 -07001742};
1743
1744static void scic_sds_controller_set_default_config_parameters(struct scic_sds_controller *scic)
1745{
1746 /* these defaults are overridden by the platform / firmware */
1747 struct isci_host *ihost = scic_to_ihost(scic);
1748 u16 index;
1749
1750 /* Default to APC mode. */
1751 scic->oem_parameters.sds1.controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
1752
1753 /* Default to APC mode. */
1754 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up = 1;
1755
1756 /* Default to no SSC operation. */
1757 scic->oem_parameters.sds1.controller.do_enable_ssc = false;
1758
1759 /* Initialize all of the port parameter information to narrow ports. */
1760 for (index = 0; index < SCI_MAX_PORTS; index++) {
1761 scic->oem_parameters.sds1.ports[index].phy_mask = 0;
1762 }
1763
1764 /* Initialize all of the phy parameter information. */
1765 for (index = 0; index < SCI_MAX_PHYS; index++) {
1766 /* Default to 6G (i.e. Gen 3) for now. */
1767 scic->user_parameters.sds1.phys[index].max_speed_generation = 3;
1768
1769 /* the frequencies cannot be 0 */
1770 scic->user_parameters.sds1.phys[index].align_insertion_frequency = 0x7f;
1771 scic->user_parameters.sds1.phys[index].in_connection_align_insertion_frequency = 0xff;
1772 scic->user_parameters.sds1.phys[index].notify_enable_spin_up_insertion_frequency = 0x33;
1773
1774 /*
1775 * Previous Vitesse based expanders had a arbitration issue that
1776 * is worked around by having the upper 32-bits of SAS address
1777 * with a value greater then the Vitesse company identifier.
1778 * Hence, usage of 0x5FCFFFFF. */
1779 scic->oem_parameters.sds1.phys[index].sas_address.low = 0x1 + ihost->id;
1780 scic->oem_parameters.sds1.phys[index].sas_address.high = 0x5FCFFFFF;
1781 }
1782
1783 scic->user_parameters.sds1.stp_inactivity_timeout = 5;
1784 scic->user_parameters.sds1.ssp_inactivity_timeout = 5;
1785 scic->user_parameters.sds1.stp_max_occupancy_timeout = 5;
1786 scic->user_parameters.sds1.ssp_max_occupancy_timeout = 20;
1787 scic->user_parameters.sds1.no_outbound_task_timeout = 20;
1788}
1789
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001790static void controller_timeout(unsigned long data)
1791{
1792 struct sci_timer *tmr = (struct sci_timer *)data;
1793 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), timer);
1794 struct isci_host *ihost = scic_to_ihost(scic);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001795 struct sci_base_state_machine *sm = &scic->sm;
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001796 unsigned long flags;
Dan Williamscc9203b2011-05-08 17:34:44 -07001797
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001798 spin_lock_irqsave(&ihost->scic_lock, flags);
1799
1800 if (tmr->cancel)
1801 goto done;
1802
Edmund Nadolskie3013702011-06-02 00:10:43 +00001803 if (sm->current_state_id == SCIC_STARTING)
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001804 scic_sds_controller_transition_to_ready(scic, SCI_FAILURE_TIMEOUT);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001805 else if (sm->current_state_id == SCIC_STOPPING) {
1806 sci_change_state(sm, SCIC_FAILED);
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001807 isci_host_stop_complete(ihost, SCI_FAILURE_TIMEOUT);
1808 } else /* / @todo Now what do we want to do in this case? */
1809 dev_err(scic_to_dev(scic),
1810 "%s: Controller timer fired when controller was not "
1811 "in a state being timed.\n",
1812 __func__);
1813
1814done:
1815 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1816}
Dan Williamscc9203b2011-05-08 17:34:44 -07001817
1818/**
1819 * scic_controller_construct() - This method will attempt to construct a
1820 * controller object utilizing the supplied parameter information.
1821 * @c: This parameter specifies the controller to be constructed.
1822 * @scu_base: mapped base address of the scu registers
1823 * @smu_base: mapped base address of the smu registers
1824 *
1825 * Indicate if the controller was successfully constructed or if it failed in
1826 * some way. SCI_SUCCESS This value is returned if the controller was
1827 * successfully constructed. SCI_WARNING_TIMER_CONFLICT This value is returned
1828 * if the interrupt coalescence timer may cause SAS compliance issues for SMP
1829 * Target mode response processing. SCI_FAILURE_UNSUPPORTED_CONTROLLER_TYPE
1830 * This value is returned if the controller does not support the supplied type.
1831 * SCI_FAILURE_UNSUPPORTED_INIT_DATA_VERSION This value is returned if the
1832 * controller does not support the supplied initialization data version.
1833 */
1834static enum sci_status scic_controller_construct(struct scic_sds_controller *scic,
1835 void __iomem *scu_base,
1836 void __iomem *smu_base)
1837{
1838 struct isci_host *ihost = scic_to_ihost(scic);
1839 u8 i;
1840
Edmund Nadolski12ef6542011-06-02 00:10:50 +00001841 sci_init_sm(&scic->sm, scic_sds_controller_state_table, SCIC_INITIAL);
Dan Williamscc9203b2011-05-08 17:34:44 -07001842
1843 scic->scu_registers = scu_base;
1844 scic->smu_registers = smu_base;
1845
1846 scic_sds_port_configuration_agent_construct(&scic->port_agent);
1847
1848 /* Construct the ports for this controller */
1849 for (i = 0; i < SCI_MAX_PORTS; i++)
1850 scic_sds_port_construct(&ihost->ports[i].sci, i, scic);
1851 scic_sds_port_construct(&ihost->ports[i].sci, SCIC_SDS_DUMMY_PORT, scic);
1852
1853 /* Construct the phys for this controller */
1854 for (i = 0; i < SCI_MAX_PHYS; i++) {
1855 /* Add all the PHYs to the dummy port */
1856 scic_sds_phy_construct(&ihost->phys[i].sci,
1857 &ihost->ports[SCI_MAX_PORTS].sci, i);
1858 }
1859
1860 scic->invalid_phy_mask = 0;
1861
Edmund Nadolski6cb58532011-05-19 11:59:56 +00001862 sci_init_timer(&scic->timer, controller_timeout);
1863
Dan Williamscc9203b2011-05-08 17:34:44 -07001864 /* Initialize the User and OEM parameters to default values. */
1865 scic_sds_controller_set_default_config_parameters(scic);
1866
1867 return scic_controller_reset(scic);
1868}
1869
1870int scic_oem_parameters_validate(struct scic_sds_oem_params *oem)
1871{
1872 int i;
1873
1874 for (i = 0; i < SCI_MAX_PORTS; i++)
1875 if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
1876 return -EINVAL;
1877
1878 for (i = 0; i < SCI_MAX_PHYS; i++)
1879 if (oem->phys[i].sas_address.high == 0 &&
1880 oem->phys[i].sas_address.low == 0)
1881 return -EINVAL;
1882
1883 if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
1884 for (i = 0; i < SCI_MAX_PHYS; i++)
1885 if (oem->ports[i].phy_mask != 0)
1886 return -EINVAL;
1887 } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
1888 u8 phy_mask = 0;
1889
1890 for (i = 0; i < SCI_MAX_PHYS; i++)
1891 phy_mask |= oem->ports[i].phy_mask;
1892
1893 if (phy_mask == 0)
1894 return -EINVAL;
1895 } else
1896 return -EINVAL;
1897
1898 if (oem->controller.max_concurrent_dev_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT)
1899 return -EINVAL;
1900
1901 return 0;
1902}
1903
1904static enum sci_status scic_oem_parameters_set(struct scic_sds_controller *scic,
1905 union scic_oem_parameters *scic_parms)
1906{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001907 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07001908
Edmund Nadolskie3013702011-06-02 00:10:43 +00001909 if (state == SCIC_RESET ||
1910 state == SCIC_INITIALIZING ||
1911 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07001912
1913 if (scic_oem_parameters_validate(&scic_parms->sds1))
1914 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
1915 scic->oem_parameters.sds1 = scic_parms->sds1;
1916
1917 return SCI_SUCCESS;
1918 }
1919
1920 return SCI_FAILURE_INVALID_STATE;
1921}
1922
1923void scic_oem_parameters_get(
1924 struct scic_sds_controller *scic,
1925 union scic_oem_parameters *scic_parms)
1926{
1927 memcpy(scic_parms, (&scic->oem_parameters), sizeof(*scic_parms));
1928}
1929
Edmund Nadolski04736612011-05-19 20:17:47 -07001930static void power_control_timeout(unsigned long data)
Dan Williamscc9203b2011-05-08 17:34:44 -07001931{
Edmund Nadolski04736612011-05-19 20:17:47 -07001932 struct sci_timer *tmr = (struct sci_timer *)data;
1933 struct scic_sds_controller *scic = container_of(tmr, typeof(*scic), power_control.timer);
1934 struct isci_host *ihost = scic_to_ihost(scic);
1935 struct scic_sds_phy *sci_phy;
1936 unsigned long flags;
1937 u8 i;
Dan Williamscc9203b2011-05-08 17:34:44 -07001938
Edmund Nadolski04736612011-05-19 20:17:47 -07001939 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001940
Edmund Nadolski04736612011-05-19 20:17:47 -07001941 if (tmr->cancel)
1942 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001943
1944 scic->power_control.phys_granted_power = 0;
1945
1946 if (scic->power_control.phys_waiting == 0) {
1947 scic->power_control.timer_started = false;
Edmund Nadolski04736612011-05-19 20:17:47 -07001948 goto done;
Dan Williamscc9203b2011-05-08 17:34:44 -07001949 }
Edmund Nadolski04736612011-05-19 20:17:47 -07001950
1951 for (i = 0; i < SCI_MAX_PHYS; i++) {
1952
1953 if (scic->power_control.phys_waiting == 0)
1954 break;
1955
1956 sci_phy = scic->power_control.requesters[i];
1957 if (sci_phy == NULL)
1958 continue;
1959
1960 if (scic->power_control.phys_granted_power >=
1961 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up)
1962 break;
1963
1964 scic->power_control.requesters[i] = NULL;
1965 scic->power_control.phys_waiting--;
1966 scic->power_control.phys_granted_power++;
1967 scic_sds_phy_consume_power_handler(sci_phy);
1968 }
1969
1970 /*
1971 * It doesn't matter if the power list is empty, we need to start the
1972 * timer in case another phy becomes ready.
1973 */
1974 sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
1975 scic->power_control.timer_started = true;
1976
1977done:
1978 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamscc9203b2011-05-08 17:34:44 -07001979}
1980
1981/**
1982 * This method inserts the phy in the stagger spinup control queue.
1983 * @scic:
1984 *
1985 *
1986 */
1987void scic_sds_controller_power_control_queue_insert(
1988 struct scic_sds_controller *scic,
1989 struct scic_sds_phy *sci_phy)
1990{
1991 BUG_ON(sci_phy == NULL);
1992
1993 if (scic->power_control.phys_granted_power <
1994 scic->oem_parameters.sds1.controller.max_concurrent_dev_spin_up) {
1995 scic->power_control.phys_granted_power++;
1996 scic_sds_phy_consume_power_handler(sci_phy);
1997
1998 /*
1999 * stop and start the power_control timer. When the timer fires, the
2000 * no_of_phys_granted_power will be set to 0
2001 */
Edmund Nadolski04736612011-05-19 20:17:47 -07002002 if (scic->power_control.timer_started)
2003 sci_del_timer(&scic->power_control.timer);
2004
2005 sci_mod_timer(&scic->power_control.timer,
2006 SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
2007 scic->power_control.timer_started = true;
2008
Dan Williamscc9203b2011-05-08 17:34:44 -07002009 } else {
2010 /* Add the phy in the waiting list */
2011 scic->power_control.requesters[sci_phy->phy_index] = sci_phy;
2012 scic->power_control.phys_waiting++;
2013 }
2014}
2015
2016/**
2017 * This method removes the phy from the stagger spinup control queue.
2018 * @scic:
2019 *
2020 *
2021 */
2022void scic_sds_controller_power_control_queue_remove(
2023 struct scic_sds_controller *scic,
2024 struct scic_sds_phy *sci_phy)
2025{
2026 BUG_ON(sci_phy == NULL);
2027
2028 if (scic->power_control.requesters[sci_phy->phy_index] != NULL) {
2029 scic->power_control.phys_waiting--;
2030 }
2031
2032 scic->power_control.requesters[sci_phy->phy_index] = NULL;
2033}
2034
2035#define AFE_REGISTER_WRITE_DELAY 10
2036
2037/* Initialize the AFE for this phy index. We need to read the AFE setup from
2038 * the OEM parameters
2039 */
2040static void scic_sds_controller_afe_initialization(struct scic_sds_controller *scic)
2041{
2042 const struct scic_sds_oem_params *oem = &scic->oem_parameters.sds1;
2043 u32 afe_status;
2044 u32 phy_id;
2045
2046 /* Clear DFX Status registers */
2047 writel(0x0081000f, &scic->scu_registers->afe.afe_dfx_master_control0);
2048 udelay(AFE_REGISTER_WRITE_DELAY);
2049
2050 if (is_b0()) {
2051 /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
2052 * Timer, PM Stagger Timer */
2053 writel(0x0007BFFF, &scic->scu_registers->afe.afe_pmsn_master_control2);
2054 udelay(AFE_REGISTER_WRITE_DELAY);
2055 }
2056
2057 /* Configure bias currents to normal */
2058 if (is_a0())
2059 writel(0x00005500, &scic->scu_registers->afe.afe_bias_control);
2060 else if (is_a2())
2061 writel(0x00005A00, &scic->scu_registers->afe.afe_bias_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002062 else if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002063 writel(0x00005F00, &scic->scu_registers->afe.afe_bias_control);
2064
2065 udelay(AFE_REGISTER_WRITE_DELAY);
2066
2067 /* Enable PLL */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002068 if (is_b0() || is_c0())
Dan Williamscc9203b2011-05-08 17:34:44 -07002069 writel(0x80040A08, &scic->scu_registers->afe.afe_pll_control0);
2070 else
2071 writel(0x80040908, &scic->scu_registers->afe.afe_pll_control0);
2072
2073 udelay(AFE_REGISTER_WRITE_DELAY);
2074
2075 /* Wait for the PLL to lock */
2076 do {
2077 afe_status = readl(&scic->scu_registers->afe.afe_common_block_status);
2078 udelay(AFE_REGISTER_WRITE_DELAY);
2079 } while ((afe_status & 0x00001000) == 0);
2080
2081 if (is_a0() || is_a2()) {
2082 /* Shorten SAS SNW lock time (RxLock timer value from 76 us to 50 us) */
2083 writel(0x7bcc96ad, &scic->scu_registers->afe.afe_pmsn_master_control0);
2084 udelay(AFE_REGISTER_WRITE_DELAY);
2085 }
2086
2087 for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
2088 const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
2089
2090 if (is_b0()) {
2091 /* Configure transmitter SSC parameters */
2092 writel(0x00030000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2093 udelay(AFE_REGISTER_WRITE_DELAY);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002094 } else if (is_c0()) {
2095 /* Configure transmitter SSC parameters */
2096 writel(0x0003000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_ssc_control);
2097 udelay(AFE_REGISTER_WRITE_DELAY);
2098
2099 /*
2100 * All defaults, except the Receive Word Alignament/Comma Detect
2101 * Enable....(0xe800) */
2102 writel(0x00004500, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2103 udelay(AFE_REGISTER_WRITE_DELAY);
Dan Williamscc9203b2011-05-08 17:34:44 -07002104 } else {
2105 /*
2106 * All defaults, except the Receive Word Alignament/Comma Detect
2107 * Enable....(0xe800) */
2108 writel(0x00004512, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2109 udelay(AFE_REGISTER_WRITE_DELAY);
2110
2111 writel(0x0050100F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control1);
2112 udelay(AFE_REGISTER_WRITE_DELAY);
2113 }
2114
2115 /*
2116 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2117 * & increase TX int & ext bias 20%....(0xe85c) */
2118 if (is_a0())
2119 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2120 else if (is_a2())
2121 writel(0x000003F0, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002122 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002123 /* Power down TX and RX (PWRDNTX and PWRDNRX) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002124 writel(0x000003D7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002125 udelay(AFE_REGISTER_WRITE_DELAY);
2126
2127 /*
2128 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2129 * & increase TX int & ext bias 20%....(0xe85c) */
Adam Gruchaladbb07432011-06-01 22:31:03 +00002130 writel(0x000003D4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2131 } else {
2132 writel(0x000001E7, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
2133 udelay(AFE_REGISTER_WRITE_DELAY);
2134
2135 /*
2136 * Power up TX and RX out from power down (PWRDNTX and PWRDNRX)
2137 * & increase TX int & ext bias 20%....(0xe85c) */
2138 writel(0x000001E4, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_channel_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002139 }
2140 udelay(AFE_REGISTER_WRITE_DELAY);
2141
2142 if (is_a0() || is_a2()) {
2143 /* Enable TX equalization (0xe824) */
2144 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
2145 udelay(AFE_REGISTER_WRITE_DELAY);
2146 }
2147
2148 /*
2149 * RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, TPD=0x0(TX Power On),
2150 * RDD=0x0(RX Detect Enabled) ....(0xe800) */
2151 writel(0x00004100, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_xcvr_control0);
2152 udelay(AFE_REGISTER_WRITE_DELAY);
2153
2154 /* Leave DFE/FFE on */
2155 if (is_a0())
2156 writel(0x3F09983F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2157 else if (is_a2())
2158 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002159 else if (is_b0()) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002160 writel(0x3F11103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2161 udelay(AFE_REGISTER_WRITE_DELAY);
2162 /* Enable TX equalization (0xe824) */
2163 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Adam Gruchaladbb07432011-06-01 22:31:03 +00002164 } else {
2165 writel(0x0140DF0F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control1);
2166 udelay(AFE_REGISTER_WRITE_DELAY);
2167
2168 writel(0x3F6F103F, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_rx_ssc_control0);
2169 udelay(AFE_REGISTER_WRITE_DELAY);
2170
2171 /* Enable TX equalization (0xe824) */
2172 writel(0x00040000, &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_control);
Dan Williamscc9203b2011-05-08 17:34:44 -07002173 }
Adam Gruchaladbb07432011-06-01 22:31:03 +00002174
Dan Williamscc9203b2011-05-08 17:34:44 -07002175 udelay(AFE_REGISTER_WRITE_DELAY);
2176
2177 writel(oem_phy->afe_tx_amp_control0,
2178 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control0);
2179 udelay(AFE_REGISTER_WRITE_DELAY);
2180
2181 writel(oem_phy->afe_tx_amp_control1,
2182 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control1);
2183 udelay(AFE_REGISTER_WRITE_DELAY);
2184
2185 writel(oem_phy->afe_tx_amp_control2,
2186 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control2);
2187 udelay(AFE_REGISTER_WRITE_DELAY);
2188
2189 writel(oem_phy->afe_tx_amp_control3,
2190 &scic->scu_registers->afe.scu_afe_xcvr[phy_id].afe_tx_amp_control3);
2191 udelay(AFE_REGISTER_WRITE_DELAY);
2192 }
2193
2194 /* Transfer control to the PEs */
2195 writel(0x00010f00, &scic->scu_registers->afe.afe_dfx_master_control0);
2196 udelay(AFE_REGISTER_WRITE_DELAY);
2197}
2198
Dan Williamscc9203b2011-05-08 17:34:44 -07002199static void scic_sds_controller_initialize_power_control(struct scic_sds_controller *scic)
2200{
Edmund Nadolski04736612011-05-19 20:17:47 -07002201 sci_init_timer(&scic->power_control.timer, power_control_timeout);
Dan Williamscc9203b2011-05-08 17:34:44 -07002202
2203 memset(scic->power_control.requesters, 0,
2204 sizeof(scic->power_control.requesters));
2205
2206 scic->power_control.phys_waiting = 0;
2207 scic->power_control.phys_granted_power = 0;
2208}
2209
2210static enum sci_status scic_controller_initialize(struct scic_sds_controller *scic)
2211{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002212 struct sci_base_state_machine *sm = &scic->sm;
Dan Williamscc9203b2011-05-08 17:34:44 -07002213 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002214 enum sci_status result = SCI_FAILURE;
2215 unsigned long i, state, val;
Dan Williamscc9203b2011-05-08 17:34:44 -07002216
Edmund Nadolskie3013702011-06-02 00:10:43 +00002217 if (scic->sm.current_state_id != SCIC_RESET) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002218 dev_warn(scic_to_dev(scic),
2219 "SCIC Controller initialize operation requested "
2220 "in invalid state\n");
2221 return SCI_FAILURE_INVALID_STATE;
2222 }
2223
Edmund Nadolskie3013702011-06-02 00:10:43 +00002224 sci_change_state(sm, SCIC_INITIALIZING);
Dan Williamscc9203b2011-05-08 17:34:44 -07002225
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002226 sci_init_timer(&scic->phy_timer, phy_startup_timeout);
2227
2228 scic->next_phy_to_start = 0;
2229 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002230
2231 scic_sds_controller_initialize_power_control(scic);
2232
2233 /*
2234 * There is nothing to do here for B0 since we do not have to
2235 * program the AFE registers.
2236 * / @todo The AFE settings are supposed to be correct for the B0 but
2237 * / presently they seem to be wrong. */
2238 scic_sds_controller_afe_initialization(scic);
2239
Dan Williams7c78da32011-06-01 16:00:01 -07002240
2241 /* Take the hardware out of reset */
2242 writel(0, &scic->smu_registers->soft_reset_control);
2243
2244 /*
2245 * / @todo Provide meaningfull error code for hardware failure
2246 * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
2247 for (i = 100; i >= 1; i--) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002248 u32 status;
Dan Williamscc9203b2011-05-08 17:34:44 -07002249
Dan Williams7c78da32011-06-01 16:00:01 -07002250 /* Loop until the hardware reports success */
2251 udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
2252 status = readl(&scic->smu_registers->control_status);
Dan Williamscc9203b2011-05-08 17:34:44 -07002253
Dan Williams7c78da32011-06-01 16:00:01 -07002254 if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
2255 break;
Dan Williamscc9203b2011-05-08 17:34:44 -07002256 }
Dan Williams7c78da32011-06-01 16:00:01 -07002257 if (i == 0)
2258 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002259
Dan Williams7c78da32011-06-01 16:00:01 -07002260 /*
2261 * Determine what are the actaul device capacities that the
2262 * hardware will support */
2263 val = readl(&scic->smu_registers->device_context_capacity);
Dan Williamscc9203b2011-05-08 17:34:44 -07002264
Dan Williams7c78da32011-06-01 16:00:01 -07002265 /* Record the smaller of the two capacity values */
2266 scic->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
2267 scic->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
2268 scic->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
Dan Williamscc9203b2011-05-08 17:34:44 -07002269
Dan Williams7c78da32011-06-01 16:00:01 -07002270 /*
2271 * Make all PEs that are unassigned match up with the
2272 * logical ports
2273 */
2274 for (i = 0; i < scic->logical_port_entries; i++) {
2275 struct scu_port_task_scheduler_group_registers __iomem
2276 *ptsg = &scic->scu_registers->peg0.ptsg;
Dan Williamscc9203b2011-05-08 17:34:44 -07002277
Dan Williams7c78da32011-06-01 16:00:01 -07002278 writel(i, &ptsg->protocol_engine[i]);
Dan Williamscc9203b2011-05-08 17:34:44 -07002279 }
2280
2281 /* Initialize hardware PCI Relaxed ordering in DMA engines */
Dan Williams7c78da32011-06-01 16:00:01 -07002282 val = readl(&scic->scu_registers->sdma.pdma_configuration);
2283 val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2284 writel(val, &scic->scu_registers->sdma.pdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002285
Dan Williams7c78da32011-06-01 16:00:01 -07002286 val = readl(&scic->scu_registers->sdma.cdma_configuration);
2287 val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
2288 writel(val, &scic->scu_registers->sdma.cdma_configuration);
Dan Williamscc9203b2011-05-08 17:34:44 -07002289
2290 /*
2291 * Initialize the PHYs before the PORTs because the PHY registers
2292 * are accessed during the port initialization.
2293 */
Dan Williams7c78da32011-06-01 16:00:01 -07002294 for (i = 0; i < SCI_MAX_PHYS; i++) {
2295 result = scic_sds_phy_initialize(&ihost->phys[i].sci,
2296 &scic->scu_registers->peg0.pe[i].tl,
2297 &scic->scu_registers->peg0.pe[i].ll);
2298 if (result != SCI_SUCCESS)
2299 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002300 }
2301
Dan Williams7c78da32011-06-01 16:00:01 -07002302 for (i = 0; i < scic->logical_port_entries; i++) {
2303 result = scic_sds_port_initialize(&ihost->ports[i].sci,
2304 &scic->scu_registers->peg0.ptsg.port[i],
2305 &scic->scu_registers->peg0.ptsg.protocol_engine,
2306 &scic->scu_registers->peg0.viit[i]);
2307
2308 if (result != SCI_SUCCESS)
2309 goto out;
Dan Williamscc9203b2011-05-08 17:34:44 -07002310 }
2311
Dan Williams7c78da32011-06-01 16:00:01 -07002312 result = scic_sds_port_configuration_agent_initialize(scic, &scic->port_agent);
Dan Williamscc9203b2011-05-08 17:34:44 -07002313
Dan Williams7c78da32011-06-01 16:00:01 -07002314 out:
Dan Williamscc9203b2011-05-08 17:34:44 -07002315 /* Advance the controller state machine */
2316 if (result == SCI_SUCCESS)
Edmund Nadolskie3013702011-06-02 00:10:43 +00002317 state = SCIC_INITIALIZED;
Dan Williamscc9203b2011-05-08 17:34:44 -07002318 else
Edmund Nadolskie3013702011-06-02 00:10:43 +00002319 state = SCIC_FAILED;
2320 sci_change_state(sm, state);
Dan Williamscc9203b2011-05-08 17:34:44 -07002321
2322 return result;
2323}
2324
2325static enum sci_status scic_user_parameters_set(
2326 struct scic_sds_controller *scic,
2327 union scic_user_parameters *scic_parms)
2328{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002329 u32 state = scic->sm.current_state_id;
Dan Williamscc9203b2011-05-08 17:34:44 -07002330
Edmund Nadolskie3013702011-06-02 00:10:43 +00002331 if (state == SCIC_RESET ||
2332 state == SCIC_INITIALIZING ||
2333 state == SCIC_INITIALIZED) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002334 u16 index;
2335
2336 /*
2337 * Validate the user parameters. If they are not legal, then
2338 * return a failure.
2339 */
2340 for (index = 0; index < SCI_MAX_PHYS; index++) {
2341 struct sci_phy_user_params *user_phy;
2342
2343 user_phy = &scic_parms->sds1.phys[index];
2344
2345 if (!((user_phy->max_speed_generation <=
2346 SCIC_SDS_PARM_MAX_SPEED) &&
2347 (user_phy->max_speed_generation >
2348 SCIC_SDS_PARM_NO_SPEED)))
2349 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2350
2351 if (user_phy->in_connection_align_insertion_frequency <
2352 3)
2353 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2354
2355 if ((user_phy->in_connection_align_insertion_frequency <
2356 3) ||
2357 (user_phy->align_insertion_frequency == 0) ||
2358 (user_phy->
2359 notify_enable_spin_up_insertion_frequency ==
2360 0))
2361 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2362 }
2363
2364 if ((scic_parms->sds1.stp_inactivity_timeout == 0) ||
2365 (scic_parms->sds1.ssp_inactivity_timeout == 0) ||
2366 (scic_parms->sds1.stp_max_occupancy_timeout == 0) ||
2367 (scic_parms->sds1.ssp_max_occupancy_timeout == 0) ||
2368 (scic_parms->sds1.no_outbound_task_timeout == 0))
2369 return SCI_FAILURE_INVALID_PARAMETER_VALUE;
2370
2371 memcpy(&scic->user_parameters, scic_parms, sizeof(*scic_parms));
2372
2373 return SCI_SUCCESS;
2374 }
2375
2376 return SCI_FAILURE_INVALID_STATE;
2377}
2378
2379static int scic_controller_mem_init(struct scic_sds_controller *scic)
2380{
2381 struct device *dev = scic_to_dev(scic);
Dan Williams7c78da32011-06-01 16:00:01 -07002382 dma_addr_t dma;
2383 size_t size;
2384 int err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002385
Dan Williams7c78da32011-06-01 16:00:01 -07002386 size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
2387 scic->completion_queue = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002388 if (!scic->completion_queue)
2389 return -ENOMEM;
2390
Dan Williams7c78da32011-06-01 16:00:01 -07002391 writel(lower_32_bits(dma), &scic->smu_registers->completion_queue_lower);
2392 writel(upper_32_bits(dma), &scic->smu_registers->completion_queue_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002393
Dan Williams7c78da32011-06-01 16:00:01 -07002394 size = scic->remote_node_entries * sizeof(union scu_remote_node_context);
2395 scic->remote_node_context_table = dmam_alloc_coherent(dev, size, &dma,
2396 GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002397 if (!scic->remote_node_context_table)
2398 return -ENOMEM;
2399
Dan Williams7c78da32011-06-01 16:00:01 -07002400 writel(lower_32_bits(dma), &scic->smu_registers->remote_node_context_lower);
2401 writel(upper_32_bits(dma), &scic->smu_registers->remote_node_context_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002402
Dan Williams7c78da32011-06-01 16:00:01 -07002403 size = scic->task_context_entries * sizeof(struct scu_task_context),
2404 scic->task_context_table = dmam_alloc_coherent(dev, size, &dma, GFP_KERNEL);
Dan Williamscc9203b2011-05-08 17:34:44 -07002405 if (!scic->task_context_table)
2406 return -ENOMEM;
2407
Dan Williams7c78da32011-06-01 16:00:01 -07002408 writel(lower_32_bits(dma), &scic->smu_registers->host_task_table_lower);
2409 writel(upper_32_bits(dma), &scic->smu_registers->host_task_table_upper);
Dan Williamscc9203b2011-05-08 17:34:44 -07002410
Dan Williams7c78da32011-06-01 16:00:01 -07002411 err = scic_sds_unsolicited_frame_control_construct(scic);
2412 if (err)
2413 return err;
Dan Williamscc9203b2011-05-08 17:34:44 -07002414
2415 /*
2416 * Inform the silicon as to the location of the UF headers and
2417 * address table.
2418 */
2419 writel(lower_32_bits(scic->uf_control.headers.physical_address),
2420 &scic->scu_registers->sdma.uf_header_base_address_lower);
2421 writel(upper_32_bits(scic->uf_control.headers.physical_address),
2422 &scic->scu_registers->sdma.uf_header_base_address_upper);
2423
2424 writel(lower_32_bits(scic->uf_control.address_table.physical_address),
2425 &scic->scu_registers->sdma.uf_address_table_lower);
2426 writel(upper_32_bits(scic->uf_control.address_table.physical_address),
2427 &scic->scu_registers->sdma.uf_address_table_upper);
2428
2429 return 0;
2430}
2431
Dan Williams6f231dd2011-07-02 22:56:22 -07002432int isci_host_init(struct isci_host *isci_host)
2433{
Dan Williamsd9c37392011-03-03 17:59:32 -08002434 int err = 0, i;
Dan Williams6f231dd2011-07-02 22:56:22 -07002435 enum sci_status status;
Dan Williams4711ba12011-03-11 10:43:57 -08002436 union scic_oem_parameters oem;
Dan Williams6f231dd2011-07-02 22:56:22 -07002437 union scic_user_parameters scic_user_params;
Dan Williamsd044af12011-03-08 09:52:49 -08002438 struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002439
Dan Williams6f231dd2011-07-02 22:56:22 -07002440 spin_lock_init(&isci_host->state_lock);
2441 spin_lock_init(&isci_host->scic_lock);
2442 spin_lock_init(&isci_host->queue_lock);
Dan Williams0cf89d12011-02-18 09:25:07 -08002443 init_waitqueue_head(&isci_host->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07002444
2445 isci_host_change_state(isci_host, isci_starting);
2446 isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
2447
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002448 status = scic_controller_construct(&isci_host->sci, scu_base(isci_host),
Dan Williams6f231dd2011-07-02 22:56:22 -07002449 smu_base(isci_host));
2450
2451 if (status != SCI_SUCCESS) {
2452 dev_err(&isci_host->pdev->dev,
2453 "%s: scic_controller_construct failed - status = %x\n",
2454 __func__,
2455 status);
Dave Jiang858d4aa2011-02-22 01:27:03 -08002456 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002457 }
2458
2459 isci_host->sas_ha.dev = &isci_host->pdev->dev;
2460 isci_host->sas_ha.lldd_ha = isci_host;
2461
Dan Williamsd044af12011-03-08 09:52:49 -08002462 /*
2463 * grab initial values stored in the controller object for OEM and USER
2464 * parameters
2465 */
Dave Jiangb5f18a22011-03-16 14:57:23 -07002466 isci_user_parameters_get(isci_host, &scic_user_params);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002467 status = scic_user_parameters_set(&isci_host->sci,
Dan Williamsd044af12011-03-08 09:52:49 -08002468 &scic_user_params);
2469 if (status != SCI_SUCCESS) {
2470 dev_warn(&isci_host->pdev->dev,
2471 "%s: scic_user_parameters_set failed\n",
2472 __func__);
2473 return -ENODEV;
2474 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002475
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002476 scic_oem_parameters_get(&isci_host->sci, &oem);
Dan Williamsd044af12011-03-08 09:52:49 -08002477
2478 /* grab any OEM parameters specified in orom */
2479 if (pci_info->orom) {
Dan Williams4711ba12011-03-11 10:43:57 -08002480 status = isci_parse_oem_parameters(&oem,
Dan Williamsd044af12011-03-08 09:52:49 -08002481 pci_info->orom,
2482 isci_host->id);
Dan Williams6f231dd2011-07-02 22:56:22 -07002483 if (status != SCI_SUCCESS) {
2484 dev_warn(&isci_host->pdev->dev,
2485 "parsing firmware oem parameters failed\n");
Dave Jiang858d4aa2011-02-22 01:27:03 -08002486 return -EINVAL;
Dan Williams6f231dd2011-07-02 22:56:22 -07002487 }
Dan Williams4711ba12011-03-11 10:43:57 -08002488 }
2489
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002490 status = scic_oem_parameters_set(&isci_host->sci, &oem);
Dan Williams4711ba12011-03-11 10:43:57 -08002491 if (status != SCI_SUCCESS) {
2492 dev_warn(&isci_host->pdev->dev,
2493 "%s: scic_oem_parameters_set failed\n",
2494 __func__);
2495 return -ENODEV;
Dan Williams6f231dd2011-07-02 22:56:22 -07002496 }
2497
Dan Williams6f231dd2011-07-02 22:56:22 -07002498 tasklet_init(&isci_host->completion_tasklet,
Dan Williamsc7ef4032011-02-18 09:25:05 -08002499 isci_host_completion_routine, (unsigned long)isci_host);
Dan Williams6f231dd2011-07-02 22:56:22 -07002500
Dan Williams6f231dd2011-07-02 22:56:22 -07002501 INIT_LIST_HEAD(&isci_host->requests_to_complete);
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002502 INIT_LIST_HEAD(&isci_host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002503
Dan Williams7c40a802011-03-02 11:49:26 -08002504 spin_lock_irq(&isci_host->scic_lock);
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002505 status = scic_controller_initialize(&isci_host->sci);
Dan Williams7c40a802011-03-02 11:49:26 -08002506 spin_unlock_irq(&isci_host->scic_lock);
2507 if (status != SCI_SUCCESS) {
2508 dev_warn(&isci_host->pdev->dev,
2509 "%s: scic_controller_initialize failed -"
2510 " status = 0x%x\n",
2511 __func__, status);
2512 return -ENODEV;
2513 }
2514
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00002515 err = scic_controller_mem_init(&isci_host->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07002516 if (err)
Dave Jiang858d4aa2011-02-22 01:27:03 -08002517 return err;
Dan Williams6f231dd2011-07-02 22:56:22 -07002518
Dan Williams6f231dd2011-07-02 22:56:22 -07002519 isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
Dan Williams67ea8382011-05-08 11:47:15 -07002520 sizeof(struct isci_request),
Dan Williams6f231dd2011-07-02 22:56:22 -07002521 SLAB_HWCACHE_ALIGN, 0);
2522
Dave Jiang858d4aa2011-02-22 01:27:03 -08002523 if (!isci_host->dma_pool)
2524 return -ENOMEM;
Dan Williams6f231dd2011-07-02 22:56:22 -07002525
Dan Williamsd9c37392011-03-03 17:59:32 -08002526 for (i = 0; i < SCI_MAX_PORTS; i++)
Dan Williamse5313812011-05-07 10:11:43 -07002527 isci_port_init(&isci_host->ports[i], isci_host, i);
Dan Williams6f231dd2011-07-02 22:56:22 -07002528
Dan Williamsd9c37392011-03-03 17:59:32 -08002529 for (i = 0; i < SCI_MAX_PHYS; i++)
2530 isci_phy_init(&isci_host->phys[i], isci_host, i);
2531
2532 for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
Dan Williams57f20f42011-04-21 18:14:45 -07002533 struct isci_remote_device *idev = &isci_host->devices[i];
Dan Williamsd9c37392011-03-03 17:59:32 -08002534
2535 INIT_LIST_HEAD(&idev->reqs_in_process);
2536 INIT_LIST_HEAD(&idev->node);
2537 spin_lock_init(&idev->state_lock);
2538 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002539
Dave Jiang858d4aa2011-02-22 01:27:03 -08002540 return 0;
Dan Williams6f231dd2011-07-02 22:56:22 -07002541}
Dan Williamscc9203b2011-05-08 17:34:44 -07002542
2543void scic_sds_controller_link_up(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:
Edmund Nadolskibb3dbdf2011-05-19 20:26:02 -07002548 sci_del_timer(&scic->phy_timer);
2549 scic->phy_startup_timer_pending = false;
Dan Williamscc9203b2011-05-08 17:34:44 -07002550 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2551 port, phy);
2552 scic_sds_controller_start_next_phy(scic);
2553 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002554 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002555 scic->port_agent.link_up_handler(scic, &scic->port_agent,
2556 port, phy);
2557 break;
2558 default:
2559 dev_dbg(scic_to_dev(scic),
2560 "%s: SCIC Controller linkup event from phy %d in "
2561 "unexpected state %d\n", __func__, phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002562 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002563 }
2564}
2565
2566void scic_sds_controller_link_down(struct scic_sds_controller *scic,
2567 struct scic_sds_port *port, struct scic_sds_phy *phy)
2568{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002569 switch (scic->sm.current_state_id) {
2570 case SCIC_STARTING:
2571 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002572 scic->port_agent.link_down_handler(scic, &scic->port_agent,
2573 port, phy);
2574 break;
2575 default:
2576 dev_dbg(scic_to_dev(scic),
2577 "%s: SCIC Controller linkdown event from phy %d in "
2578 "unexpected state %d\n",
2579 __func__,
2580 phy->phy_index,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002581 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002582 }
2583}
2584
2585/**
2586 * This is a helper method to determine if any remote devices on this
2587 * controller are still in the stopping state.
2588 *
2589 */
2590static bool scic_sds_controller_has_remote_devices_stopping(
2591 struct scic_sds_controller *controller)
2592{
2593 u32 index;
2594
2595 for (index = 0; index < controller->remote_node_entries; index++) {
2596 if ((controller->device_table[index] != NULL) &&
Edmund Nadolskie3013702011-06-02 00:10:43 +00002597 (controller->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
Dan Williamscc9203b2011-05-08 17:34:44 -07002598 return true;
2599 }
2600
2601 return false;
2602}
2603
2604/**
2605 * This method is called by the remote device to inform the controller
2606 * object that the remote device has stopped.
2607 */
2608void scic_sds_controller_remote_device_stopped(struct scic_sds_controller *scic,
2609 struct scic_sds_remote_device *sci_dev)
2610{
Edmund Nadolskie3013702011-06-02 00:10:43 +00002611 if (scic->sm.current_state_id != SCIC_STOPPING) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002612 dev_dbg(scic_to_dev(scic),
2613 "SCIC Controller 0x%p remote device stopped event "
2614 "from device 0x%p in unexpected state %d\n",
2615 scic, sci_dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002616 scic->sm.current_state_id);
Dan Williamscc9203b2011-05-08 17:34:44 -07002617 return;
2618 }
2619
2620 if (!scic_sds_controller_has_remote_devices_stopping(scic)) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002621 sci_change_state(&scic->sm, SCIC_STOPPED);
Dan Williamscc9203b2011-05-08 17:34:44 -07002622 }
2623}
2624
2625/**
2626 * This method will write to the SCU PCP register the request value. The method
2627 * is used to suspend/resume ports, devices, and phys.
2628 * @scic:
2629 *
2630 *
2631 */
2632void scic_sds_controller_post_request(
2633 struct scic_sds_controller *scic,
2634 u32 request)
2635{
2636 dev_dbg(scic_to_dev(scic),
2637 "%s: SCIC Controller 0x%p post request 0x%08x\n",
2638 __func__,
2639 scic,
2640 request);
2641
2642 writel(request, &scic->smu_registers->post_context_port);
2643}
2644
2645/**
2646 * This method will copy the soft copy of the task context into the physical
2647 * memory accessible by the controller.
2648 * @scic: This parameter specifies the controller for which to copy
2649 * the task context.
2650 * @sci_req: This parameter specifies the request for which the task
2651 * context is being copied.
2652 *
2653 * After this call is made the SCIC_SDS_IO_REQUEST object will always point to
2654 * the physical memory version of the task context. Thus, all subsequent
2655 * updates to the task context are performed in the TC table (i.e. DMAable
2656 * memory). none
2657 */
2658void scic_sds_controller_copy_task_context(
2659 struct scic_sds_controller *scic,
2660 struct scic_sds_request *sci_req)
2661{
2662 struct scu_task_context *task_context_buffer;
2663
2664 task_context_buffer = scic_sds_controller_get_task_context_buffer(
2665 scic, sci_req->io_tag);
2666
2667 memcpy(task_context_buffer,
2668 sci_req->task_context_buffer,
2669 offsetof(struct scu_task_context, sgl_snapshot_ac));
2670
2671 /*
2672 * Now that the soft copy of the TC has been copied into the TC
2673 * table accessible by the silicon. Thus, any further changes to
2674 * the TC (e.g. TC termination) occur in the appropriate location. */
2675 sci_req->task_context_buffer = task_context_buffer;
2676}
2677
Dan Williamsdd047c82011-06-09 11:06:58 -07002678struct scu_task_context *scic_sds_controller_get_task_context_buffer(struct scic_sds_controller *scic,
2679 u16 io_tag)
2680{
2681 u16 tci = ISCI_TAG_TCI(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002682
Dan Williamsdd047c82011-06-09 11:06:58 -07002683 if (tci < scic->task_context_entries) {
2684 return &scic->task_context_table[tci];
Dan Williamscc9203b2011-05-08 17:34:44 -07002685 }
2686
2687 return NULL;
2688}
2689
Dan Williamsdd047c82011-06-09 11:06:58 -07002690struct scic_sds_request *scic_request_by_tag(struct scic_sds_controller *scic, u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07002691{
2692 u16 task_index;
2693 u16 task_sequence;
2694
Dan Williamsdd047c82011-06-09 11:06:58 -07002695 task_index = ISCI_TAG_TCI(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002696
Dan Williamsdd047c82011-06-09 11:06:58 -07002697 if (task_index < scic->task_context_entries) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002698 if (scic->io_request_table[task_index] != NULL) {
Dan Williamsdd047c82011-06-09 11:06:58 -07002699 task_sequence = ISCI_TAG_SEQ(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002700
2701 if (task_sequence == scic->io_request_sequence[task_index]) {
2702 return scic->io_request_table[task_index];
2703 }
2704 }
2705 }
2706
2707 return NULL;
2708}
2709
2710/**
2711 * This method allocates remote node index and the reserves the remote node
2712 * context space for use. This method can fail if there are no more remote
2713 * node index available.
2714 * @scic: This is the controller object which contains the set of
2715 * free remote node ids
2716 * @sci_dev: This is the device object which is requesting the a remote node
2717 * id
2718 * @node_id: This is the remote node id that is assinged to the device if one
2719 * is available
2720 *
2721 * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
2722 * node index available.
2723 */
2724enum sci_status scic_sds_controller_allocate_remote_node_context(
2725 struct scic_sds_controller *scic,
2726 struct scic_sds_remote_device *sci_dev,
2727 u16 *node_id)
2728{
2729 u16 node_index;
2730 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2731
2732 node_index = scic_sds_remote_node_table_allocate_remote_node(
2733 &scic->available_remote_nodes, remote_node_count
2734 );
2735
2736 if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
2737 scic->device_table[node_index] = sci_dev;
2738
2739 *node_id = node_index;
2740
2741 return SCI_SUCCESS;
2742 }
2743
2744 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2745}
2746
2747/**
2748 * This method frees the remote node index back to the available pool. Once
2749 * this is done the remote node context buffer is no longer valid and can
2750 * not be used.
2751 * @scic:
2752 * @sci_dev:
2753 * @node_id:
2754 *
2755 */
2756void scic_sds_controller_free_remote_node_context(
2757 struct scic_sds_controller *scic,
2758 struct scic_sds_remote_device *sci_dev,
2759 u16 node_id)
2760{
2761 u32 remote_node_count = scic_sds_remote_device_node_count(sci_dev);
2762
2763 if (scic->device_table[node_id] == sci_dev) {
2764 scic->device_table[node_id] = NULL;
2765
2766 scic_sds_remote_node_table_release_remote_node_index(
2767 &scic->available_remote_nodes, remote_node_count, node_id
2768 );
2769 }
2770}
2771
2772/**
2773 * This method returns the union scu_remote_node_context for the specified remote
2774 * node id.
2775 * @scic:
2776 * @node_id:
2777 *
2778 * union scu_remote_node_context*
2779 */
2780union scu_remote_node_context *scic_sds_controller_get_remote_node_context_buffer(
2781 struct scic_sds_controller *scic,
2782 u16 node_id
2783 ) {
2784 if (
2785 (node_id < scic->remote_node_entries)
2786 && (scic->device_table[node_id] != NULL)
2787 ) {
2788 return &scic->remote_node_context_table[node_id];
2789 }
2790
2791 return NULL;
2792}
2793
2794/**
2795 *
2796 * @resposne_buffer: This is the buffer into which the D2H register FIS will be
2797 * constructed.
2798 * @frame_header: This is the frame header returned by the hardware.
2799 * @frame_buffer: This is the frame buffer returned by the hardware.
2800 *
2801 * This method will combind the frame header and frame buffer to create a SATA
2802 * D2H register FIS none
2803 */
2804void scic_sds_controller_copy_sata_response(
2805 void *response_buffer,
2806 void *frame_header,
2807 void *frame_buffer)
2808{
2809 memcpy(response_buffer, frame_header, sizeof(u32));
2810
2811 memcpy(response_buffer + sizeof(u32),
2812 frame_buffer,
2813 sizeof(struct dev_to_host_fis) - sizeof(u32));
2814}
2815
2816/**
2817 * This method releases the frame once this is done the frame is available for
2818 * re-use by the hardware. The data contained in the frame header and frame
2819 * buffer is no longer valid. The UF queue get pointer is only updated if UF
2820 * control indicates this is appropriate.
2821 * @scic:
2822 * @frame_index:
2823 *
2824 */
2825void scic_sds_controller_release_frame(
2826 struct scic_sds_controller *scic,
2827 u32 frame_index)
2828{
2829 if (scic_sds_unsolicited_frame_control_release_frame(
2830 &scic->uf_control, frame_index) == true)
2831 writel(scic->uf_control.get,
2832 &scic->scu_registers->sdma.unsolicited_frame_get_pointer);
2833}
2834
2835/**
2836 * scic_controller_start_io() - This method is called by the SCI user to
2837 * send/start an IO request. If the method invocation is successful, then
2838 * the IO request has been queued to the hardware for processing.
2839 * @controller: the handle to the controller object for which to start an IO
2840 * request.
2841 * @remote_device: the handle to the remote device object for which to start an
2842 * IO request.
2843 * @io_request: the handle to the io request object to start.
2844 * @io_tag: This parameter specifies a previously allocated IO tag that the
2845 * user desires to be utilized for this request. This parameter is optional.
2846 * The user is allowed to supply SCI_CONTROLLER_INVALID_IO_TAG as the value
2847 * for this parameter.
2848 *
2849 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2850 * to ensure that each of the methods that may allocate or free available IO
2851 * tags are handled in a mutually exclusive manner. This method is one of said
2852 * methods requiring proper critical code section protection (e.g. semaphore,
2853 * spin-lock, etc.). - For SATA, the user is required to manage NCQ tags. As a
2854 * result, it is expected the user will have set the NCQ tag field in the host
2855 * to device register FIS prior to calling this method. There is also a
2856 * requirement for the user to call scic_stp_io_set_ncq_tag() prior to invoking
2857 * the scic_controller_start_io() method. scic_controller_allocate_tag() for
2858 * more information on allocating a tag. Indicate if the controller
2859 * successfully started the IO request. SCI_SUCCESS if the IO request was
2860 * successfully started. Determine the failure situations and return values.
2861 */
Dan Williamsdd047c82011-06-09 11:06:58 -07002862enum sci_status scic_controller_start_io(struct scic_sds_controller *scic,
2863 struct scic_sds_remote_device *rdev,
2864 struct scic_sds_request *req,
2865 u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07002866{
2867 enum sci_status status;
2868
Edmund Nadolskie3013702011-06-02 00:10:43 +00002869 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002870 dev_warn(scic_to_dev(scic), "invalid state to start I/O");
2871 return SCI_FAILURE_INVALID_STATE;
2872 }
2873
2874 status = scic_sds_remote_device_start_io(scic, rdev, req);
2875 if (status != SCI_SUCCESS)
2876 return status;
2877
Dan Williamsdd047c82011-06-09 11:06:58 -07002878 scic->io_request_table[ISCI_TAG_TCI(req->io_tag)] = req;
Dan Williamscc9203b2011-05-08 17:34:44 -07002879 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(req));
2880 return SCI_SUCCESS;
2881}
2882
2883/**
2884 * scic_controller_terminate_request() - This method is called by the SCI Core
2885 * user to terminate an ongoing (i.e. started) core IO request. This does
2886 * not abort the IO request at the target, but rather removes the IO request
2887 * from the host controller.
2888 * @controller: the handle to the controller object for which to terminate a
2889 * request.
2890 * @remote_device: the handle to the remote device object for which to
2891 * terminate a request.
2892 * @request: the handle to the io or task management request object to
2893 * terminate.
2894 *
2895 * Indicate if the controller successfully began the terminate process for the
2896 * IO request. SCI_SUCCESS if the terminate process was successfully started
2897 * for the request. Determine the failure situations and return values.
2898 */
2899enum sci_status scic_controller_terminate_request(
2900 struct scic_sds_controller *scic,
2901 struct scic_sds_remote_device *rdev,
2902 struct scic_sds_request *req)
2903{
2904 enum sci_status status;
2905
Edmund Nadolskie3013702011-06-02 00:10:43 +00002906 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002907 dev_warn(scic_to_dev(scic),
2908 "invalid state to terminate request\n");
2909 return SCI_FAILURE_INVALID_STATE;
2910 }
2911
2912 status = scic_sds_io_request_terminate(req);
2913 if (status != SCI_SUCCESS)
2914 return status;
2915
2916 /*
2917 * Utilize the original post context command and or in the POST_TC_ABORT
2918 * request sub-type.
2919 */
2920 scic_sds_controller_post_request(scic,
2921 scic_sds_request_get_post_context(req) |
2922 SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
2923 return SCI_SUCCESS;
2924}
2925
2926/**
2927 * scic_controller_complete_io() - This method will perform core specific
2928 * completion operations for an IO request. After this method is invoked,
2929 * the user should consider the IO request as invalid until it is properly
2930 * reused (i.e. re-constructed).
2931 * @controller: The handle to the controller object for which to complete the
2932 * IO request.
2933 * @remote_device: The handle to the remote device object for which to complete
2934 * the IO request.
2935 * @io_request: the handle to the io request object to complete.
2936 *
2937 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
2938 * to ensure that each of the methods that may allocate or free available IO
2939 * tags are handled in a mutually exclusive manner. This method is one of said
2940 * methods requiring proper critical code section protection (e.g. semaphore,
2941 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
2942 * Core user, using the scic_controller_allocate_io_tag() method, then it is
2943 * the responsibility of the caller to invoke the scic_controller_free_io_tag()
2944 * method to free the tag (i.e. this method will not free the IO tag). Indicate
2945 * if the controller successfully completed the IO request. SCI_SUCCESS if the
2946 * completion process was successful.
2947 */
2948enum sci_status scic_controller_complete_io(
2949 struct scic_sds_controller *scic,
2950 struct scic_sds_remote_device *rdev,
2951 struct scic_sds_request *request)
2952{
2953 enum sci_status status;
2954 u16 index;
2955
Edmund Nadolskie3013702011-06-02 00:10:43 +00002956 switch (scic->sm.current_state_id) {
2957 case SCIC_STOPPING:
Dan Williamscc9203b2011-05-08 17:34:44 -07002958 /* XXX: Implement this function */
2959 return SCI_FAILURE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002960 case SCIC_READY:
Dan Williamscc9203b2011-05-08 17:34:44 -07002961 status = scic_sds_remote_device_complete_io(scic, rdev, request);
2962 if (status != SCI_SUCCESS)
2963 return status;
2964
Dan Williamsdd047c82011-06-09 11:06:58 -07002965 index = ISCI_TAG_TCI(request->io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07002966 scic->io_request_table[index] = NULL;
2967 return SCI_SUCCESS;
2968 default:
2969 dev_warn(scic_to_dev(scic), "invalid state to complete I/O");
2970 return SCI_FAILURE_INVALID_STATE;
2971 }
2972
2973}
2974
2975enum sci_status scic_controller_continue_io(struct scic_sds_request *sci_req)
2976{
2977 struct scic_sds_controller *scic = sci_req->owning_controller;
2978
Edmund Nadolskie3013702011-06-02 00:10:43 +00002979 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07002980 dev_warn(scic_to_dev(scic), "invalid state to continue I/O");
2981 return SCI_FAILURE_INVALID_STATE;
2982 }
2983
Dan Williamsdd047c82011-06-09 11:06:58 -07002984 scic->io_request_table[ISCI_TAG_TCI(sci_req->io_tag)] = sci_req;
Dan Williamscc9203b2011-05-08 17:34:44 -07002985 scic_sds_controller_post_request(scic, scic_sds_request_get_post_context(sci_req));
2986 return SCI_SUCCESS;
2987}
2988
2989/**
2990 * scic_controller_start_task() - This method is called by the SCIC user to
2991 * send/start a framework task management request.
2992 * @controller: the handle to the controller object for which to start the task
2993 * management request.
2994 * @remote_device: the handle to the remote device object for which to start
2995 * the task management request.
2996 * @task_request: the handle to the task request object to start.
2997 * @io_tag: This parameter specifies a previously allocated IO tag that the
2998 * user desires to be utilized for this request. Note this not the io_tag
2999 * of the request being managed. It is to be utilized for the task request
3000 * itself. This parameter is optional. The user is allowed to supply
3001 * SCI_CONTROLLER_INVALID_IO_TAG as the value for this parameter.
3002 *
3003 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
3004 * to ensure that each of the methods that may allocate or free available IO
3005 * tags are handled in a mutually exclusive manner. This method is one of said
3006 * methods requiring proper critical code section protection (e.g. semaphore,
3007 * spin-lock, etc.). - The user must synchronize this task with completion
3008 * queue processing. If they are not synchronized then it is possible for the
3009 * io requests that are being managed by the task request can complete before
3010 * starting the task request. scic_controller_allocate_tag() for more
3011 * information on allocating a tag. Indicate if the controller successfully
3012 * started the IO request. SCI_TASK_SUCCESS if the task request was
3013 * successfully started. SCI_TASK_FAILURE_REQUIRES_SCSI_ABORT This value is
3014 * returned if there is/are task(s) outstanding that require termination or
3015 * completion before this request can succeed.
3016 */
3017enum sci_task_status scic_controller_start_task(
3018 struct scic_sds_controller *scic,
3019 struct scic_sds_remote_device *rdev,
3020 struct scic_sds_request *req,
3021 u16 task_tag)
3022{
3023 enum sci_status status;
3024
Edmund Nadolskie3013702011-06-02 00:10:43 +00003025 if (scic->sm.current_state_id != SCIC_READY) {
Dan Williamscc9203b2011-05-08 17:34:44 -07003026 dev_warn(scic_to_dev(scic),
3027 "%s: SCIC Controller starting task from invalid "
3028 "state\n",
3029 __func__);
3030 return SCI_TASK_FAILURE_INVALID_STATE;
3031 }
3032
3033 status = scic_sds_remote_device_start_task(scic, rdev, req);
3034 switch (status) {
3035 case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
Dan Williamsdd047c82011-06-09 11:06:58 -07003036 scic->io_request_table[ISCI_TAG_TCI(req->io_tag)] = req;
Dan Williamscc9203b2011-05-08 17:34:44 -07003037
3038 /*
3039 * We will let framework know this task request started successfully,
3040 * although core is still woring on starting the request (to post tc when
3041 * RNC is resumed.)
3042 */
3043 return SCI_SUCCESS;
3044 case SCI_SUCCESS:
Dan Williamsdd047c82011-06-09 11:06:58 -07003045 scic->io_request_table[ISCI_TAG_TCI(req->io_tag)] = req;
Dan Williamscc9203b2011-05-08 17:34:44 -07003046
3047 scic_sds_controller_post_request(scic,
3048 scic_sds_request_get_post_context(req));
3049 break;
3050 default:
3051 break;
3052 }
3053
3054 return status;
3055}
3056
3057/**
3058 * scic_controller_allocate_io_tag() - This method will allocate a tag from the
3059 * pool of free IO tags. Direct allocation of IO tags by the SCI Core user
3060 * is optional. The scic_controller_start_io() method will allocate an IO
3061 * tag if this method is not utilized and the tag is not supplied to the IO
3062 * construct routine. Direct allocation of IO tags may provide additional
3063 * performance improvements in environments capable of supporting this usage
3064 * model. Additionally, direct allocation of IO tags also provides
3065 * additional flexibility to the SCI Core user. Specifically, the user may
3066 * retain IO tags across the lives of multiple IO requests.
3067 * @controller: the handle to the controller object for which to allocate the
3068 * tag.
3069 *
3070 * IO tags are a protected resource. It is incumbent upon the SCI Core user to
3071 * ensure that each of the methods that may allocate or free available IO tags
3072 * are handled in a mutually exclusive manner. This method is one of said
3073 * methods requiring proper critical code section protection (e.g. semaphore,
3074 * spin-lock, etc.). An unsigned integer representing an available IO tag.
3075 * SCI_CONTROLLER_INVALID_IO_TAG This value is returned if there are no
3076 * currently available tags to be allocated. All return other values indicate a
3077 * legitimate tag.
3078 */
Dan Williamsac668c62011-06-07 18:50:55 -07003079u16 scic_controller_allocate_io_tag(struct scic_sds_controller *scic)
Dan Williamscc9203b2011-05-08 17:34:44 -07003080{
Dan Williamsac668c62011-06-07 18:50:55 -07003081 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamscc9203b2011-05-08 17:34:44 -07003082
Dan Williamsac668c62011-06-07 18:50:55 -07003083 if (isci_tci_space(ihost)) {
Dan Williamsdd047c82011-06-09 11:06:58 -07003084 u16 tci = isci_tci_alloc(ihost);
3085 u8 seq = scic->io_request_sequence[tci];
Dan Williamscc9203b2011-05-08 17:34:44 -07003086
Dan Williamsdd047c82011-06-09 11:06:58 -07003087 return ISCI_TAG(seq, tci);
Dan Williamscc9203b2011-05-08 17:34:44 -07003088 }
3089
3090 return SCI_CONTROLLER_INVALID_IO_TAG;
3091}
3092
3093/**
3094 * scic_controller_free_io_tag() - This method will free an IO tag to the pool
3095 * of free IO tags. This method provides the SCI Core user more flexibility
3096 * with regards to IO tags. The user may desire to keep an IO tag after an
3097 * IO request has completed, because they plan on re-using the tag for a
3098 * subsequent IO request. This method is only legal if the tag was
3099 * allocated via scic_controller_allocate_io_tag().
3100 * @controller: This parameter specifies the handle to the controller object
3101 * for which to free/return the tag.
3102 * @io_tag: This parameter represents the tag to be freed to the pool of
3103 * available tags.
3104 *
3105 * - IO tags are a protected resource. It is incumbent upon the SCI Core user
3106 * to ensure that each of the methods that may allocate or free available IO
3107 * tags are handled in a mutually exclusive manner. This method is one of said
3108 * methods requiring proper critical code section protection (e.g. semaphore,
3109 * spin-lock, etc.). - If the IO tag for a request was allocated, by the SCI
3110 * Core user, using the scic_controller_allocate_io_tag() method, then it is
3111 * the responsibility of the caller to invoke this method to free the tag. This
3112 * method returns an indication of whether the tag was successfully put back
3113 * (freed) to the pool of available tags. SCI_SUCCESS This return value
3114 * indicates the tag was successfully placed into the pool of available IO
3115 * tags. SCI_FAILURE_INVALID_IO_TAG This value is returned if the supplied tag
3116 * is not a valid IO tag value.
3117 */
Dan Williamsac668c62011-06-07 18:50:55 -07003118enum sci_status scic_controller_free_io_tag(struct scic_sds_controller *scic,
3119 u16 io_tag)
Dan Williamscc9203b2011-05-08 17:34:44 -07003120{
Dan Williamsac668c62011-06-07 18:50:55 -07003121 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamsdd047c82011-06-09 11:06:58 -07003122 u16 tci = ISCI_TAG_TCI(io_tag);
3123 u16 seq = ISCI_TAG_SEQ(io_tag);
Dan Williamscc9203b2011-05-08 17:34:44 -07003124
Dan Williamsac668c62011-06-07 18:50:55 -07003125 /* prevent tail from passing head */
3126 if (isci_tci_active(ihost) == 0)
3127 return SCI_FAILURE_INVALID_IO_TAG;
Dan Williamscc9203b2011-05-08 17:34:44 -07003128
Dan Williamsdd047c82011-06-09 11:06:58 -07003129 if (seq == scic->io_request_sequence[tci]) {
3130 scic->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
Dan Williamscc9203b2011-05-08 17:34:44 -07003131
Dan Williamsdd047c82011-06-09 11:06:58 -07003132 isci_tci_free(ihost, ISCI_TAG_TCI(io_tag));
Dan Williamsac668c62011-06-07 18:50:55 -07003133
3134 return SCI_SUCCESS;
Dan Williamscc9203b2011-05-08 17:34:44 -07003135 }
Dan Williamscc9203b2011-05-08 17:34:44 -07003136 return SCI_FAILURE_INVALID_IO_TAG;
3137}