16f231ddaSDan Williams /* 26f231ddaSDan Williams * This file is provided under a dual BSD/GPLv2 license. When using or 36f231ddaSDan Williams * redistributing this file, you may do so under either license. 46f231ddaSDan Williams * 56f231ddaSDan Williams * GPL LICENSE SUMMARY 66f231ddaSDan Williams * 76f231ddaSDan Williams * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. 86f231ddaSDan Williams * 96f231ddaSDan Williams * This program is free software; you can redistribute it and/or modify 106f231ddaSDan Williams * it under the terms of version 2 of the GNU General Public License as 116f231ddaSDan Williams * published by the Free Software Foundation. 126f231ddaSDan Williams * 136f231ddaSDan Williams * This program is distributed in the hope that it will be useful, but 146f231ddaSDan Williams * WITHOUT ANY WARRANTY; without even the implied warranty of 156f231ddaSDan Williams * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 166f231ddaSDan Williams * General Public License for more details. 176f231ddaSDan Williams * 186f231ddaSDan Williams * You should have received a copy of the GNU General Public License 196f231ddaSDan Williams * along with this program; if not, write to the Free Software 206f231ddaSDan Williams * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 216f231ddaSDan Williams * The full GNU General Public License is included in this distribution 226f231ddaSDan Williams * in the file called LICENSE.GPL. 236f231ddaSDan Williams * 246f231ddaSDan Williams * BSD LICENSE 256f231ddaSDan Williams * 266f231ddaSDan Williams * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. 276f231ddaSDan Williams * All rights reserved. 286f231ddaSDan Williams * 296f231ddaSDan Williams * Redistribution and use in source and binary forms, with or without 306f231ddaSDan Williams * modification, are permitted provided that the following conditions 316f231ddaSDan Williams * are met: 326f231ddaSDan Williams * 336f231ddaSDan Williams * * Redistributions of source code must retain the above copyright 346f231ddaSDan Williams * notice, this list of conditions and the following disclaimer. 356f231ddaSDan Williams * * Redistributions in binary form must reproduce the above copyright 366f231ddaSDan Williams * notice, this list of conditions and the following disclaimer in 376f231ddaSDan Williams * the documentation and/or other materials provided with the 386f231ddaSDan Williams * distribution. 396f231ddaSDan Williams * * Neither the name of Intel Corporation nor the names of its 406f231ddaSDan Williams * contributors may be used to endorse or promote products derived 416f231ddaSDan Williams * from this software without specific prior written permission. 426f231ddaSDan Williams * 436f231ddaSDan Williams * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 446f231ddaSDan Williams * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 456f231ddaSDan Williams * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 466f231ddaSDan Williams * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 476f231ddaSDan Williams * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 486f231ddaSDan Williams * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 496f231ddaSDan Williams * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 506f231ddaSDan Williams * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 516f231ddaSDan Williams * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 526f231ddaSDan Williams * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 536f231ddaSDan Williams * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 546f231ddaSDan Williams */ 55ac668c69SDan Williams #include <linux/circ_buf.h> 56cc9203bfSDan Williams #include <linux/device.h> 57cc9203bfSDan Williams #include <scsi/sas.h> 58cc9203bfSDan Williams #include "host.h" 596f231ddaSDan Williams #include "isci.h" 606f231ddaSDan Williams #include "port.h" 61d044af17SDan Williams #include "probe_roms.h" 62cc9203bfSDan Williams #include "remote_device.h" 63cc9203bfSDan Williams #include "request.h" 64cc9203bfSDan Williams #include "scu_completion_codes.h" 65cc9203bfSDan Williams #include "scu_event_codes.h" 6663a3a15fSDan Williams #include "registers.h" 67cc9203bfSDan Williams #include "scu_remote_node_context.h" 68cc9203bfSDan Williams #include "scu_task_context.h" 696f231ddaSDan Williams 70cc9203bfSDan Williams #define SCU_CONTEXT_RAM_INIT_STALL_TIME 200 71cc9203bfSDan Williams 727c78da31SDan Williams #define smu_max_ports(dcc_value) \ 73cc9203bfSDan Williams (\ 74cc9203bfSDan Williams (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \ 75cc9203bfSDan Williams >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \ 76cc9203bfSDan Williams ) 77cc9203bfSDan Williams 787c78da31SDan Williams #define smu_max_task_contexts(dcc_value) \ 79cc9203bfSDan Williams (\ 80cc9203bfSDan Williams (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \ 81cc9203bfSDan Williams >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \ 82cc9203bfSDan Williams ) 83cc9203bfSDan Williams 847c78da31SDan Williams #define smu_max_rncs(dcc_value) \ 85cc9203bfSDan Williams (\ 86cc9203bfSDan Williams (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \ 87cc9203bfSDan Williams >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \ 88cc9203bfSDan Williams ) 89cc9203bfSDan Williams 90cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100 91cc9203bfSDan Williams 92cc9203bfSDan Williams /** 93cc9203bfSDan Williams * 94cc9203bfSDan Williams * 95cc9203bfSDan Williams * The number of milliseconds to wait while a given phy is consuming power 96cc9203bfSDan Williams * before allowing another set of phys to consume power. Ultimately, this will 97cc9203bfSDan Williams * be specified by OEM parameter. 98cc9203bfSDan Williams */ 99cc9203bfSDan Williams #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500 100cc9203bfSDan Williams 101cc9203bfSDan Williams /** 102cc9203bfSDan Williams * NORMALIZE_PUT_POINTER() - 103cc9203bfSDan Williams * 104cc9203bfSDan Williams * This macro will normalize the completion queue put pointer so its value can 105cc9203bfSDan Williams * be used as an array inde 106cc9203bfSDan Williams */ 107cc9203bfSDan Williams #define NORMALIZE_PUT_POINTER(x) \ 108cc9203bfSDan Williams ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK) 109cc9203bfSDan Williams 110cc9203bfSDan Williams 111cc9203bfSDan Williams /** 112cc9203bfSDan Williams * NORMALIZE_EVENT_POINTER() - 113cc9203bfSDan Williams * 114cc9203bfSDan Williams * This macro will normalize the completion queue event entry so its value can 115cc9203bfSDan Williams * be used as an index. 116cc9203bfSDan Williams */ 117cc9203bfSDan Williams #define NORMALIZE_EVENT_POINTER(x) \ 118cc9203bfSDan Williams (\ 119cc9203bfSDan Williams ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \ 120cc9203bfSDan Williams >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \ 121cc9203bfSDan Williams ) 122cc9203bfSDan Williams 123cc9203bfSDan Williams /** 124cc9203bfSDan Williams * NORMALIZE_GET_POINTER() - 125cc9203bfSDan Williams * 126cc9203bfSDan Williams * This macro will normalize the completion queue get pointer so its value can 127cc9203bfSDan Williams * be used as an index into an array 128cc9203bfSDan Williams */ 129cc9203bfSDan Williams #define NORMALIZE_GET_POINTER(x) \ 130cc9203bfSDan Williams ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK) 131cc9203bfSDan Williams 132cc9203bfSDan Williams /** 133cc9203bfSDan Williams * NORMALIZE_GET_POINTER_CYCLE_BIT() - 134cc9203bfSDan Williams * 135cc9203bfSDan Williams * This macro will normalize the completion queue cycle pointer so it matches 136cc9203bfSDan Williams * the completion queue cycle bit 137cc9203bfSDan Williams */ 138cc9203bfSDan Williams #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \ 139cc9203bfSDan Williams ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT)) 140cc9203bfSDan Williams 141cc9203bfSDan Williams /** 142cc9203bfSDan Williams * COMPLETION_QUEUE_CYCLE_BIT() - 143cc9203bfSDan Williams * 144cc9203bfSDan Williams * This macro will return the cycle bit of the completion queue entry 145cc9203bfSDan Williams */ 146cc9203bfSDan Williams #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000) 147cc9203bfSDan Williams 14812ef6544SEdmund Nadolski /* Init the state machine and call the state entry function (if any) */ 14912ef6544SEdmund Nadolski void sci_init_sm(struct sci_base_state_machine *sm, 15012ef6544SEdmund Nadolski const struct sci_base_state *state_table, u32 initial_state) 15112ef6544SEdmund Nadolski { 15212ef6544SEdmund Nadolski sci_state_transition_t handler; 15312ef6544SEdmund Nadolski 15412ef6544SEdmund Nadolski sm->initial_state_id = initial_state; 15512ef6544SEdmund Nadolski sm->previous_state_id = initial_state; 15612ef6544SEdmund Nadolski sm->current_state_id = initial_state; 15712ef6544SEdmund Nadolski sm->state_table = state_table; 15812ef6544SEdmund Nadolski 15912ef6544SEdmund Nadolski handler = sm->state_table[initial_state].enter_state; 16012ef6544SEdmund Nadolski if (handler) 16112ef6544SEdmund Nadolski handler(sm); 16212ef6544SEdmund Nadolski } 16312ef6544SEdmund Nadolski 16412ef6544SEdmund Nadolski /* Call the state exit fn, update the current state, call the state entry fn */ 16512ef6544SEdmund Nadolski void sci_change_state(struct sci_base_state_machine *sm, u32 next_state) 16612ef6544SEdmund Nadolski { 16712ef6544SEdmund Nadolski sci_state_transition_t handler; 16812ef6544SEdmund Nadolski 16912ef6544SEdmund Nadolski handler = sm->state_table[sm->current_state_id].exit_state; 17012ef6544SEdmund Nadolski if (handler) 17112ef6544SEdmund Nadolski handler(sm); 17212ef6544SEdmund Nadolski 17312ef6544SEdmund Nadolski sm->previous_state_id = sm->current_state_id; 17412ef6544SEdmund Nadolski sm->current_state_id = next_state; 17512ef6544SEdmund Nadolski 17612ef6544SEdmund Nadolski handler = sm->state_table[sm->current_state_id].enter_state; 17712ef6544SEdmund Nadolski if (handler) 17812ef6544SEdmund Nadolski handler(sm); 17912ef6544SEdmund Nadolski } 18012ef6544SEdmund Nadolski 18189a7301fSDan Williams static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost) 182cc9203bfSDan Williams { 183d9dcb4baSDan Williams u32 get_value = ihost->completion_queue_get; 184cc9203bfSDan Williams u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK; 185cc9203bfSDan Williams 186cc9203bfSDan Williams if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) == 187d9dcb4baSDan Williams COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])) 188cc9203bfSDan Williams return true; 189cc9203bfSDan Williams 190cc9203bfSDan Williams return false; 191cc9203bfSDan Williams } 192cc9203bfSDan Williams 19389a7301fSDan Williams static bool sci_controller_isr(struct isci_host *ihost) 194cc9203bfSDan Williams { 1952396a265SDan Williams if (sci_controller_completion_queue_has_entries(ihost)) 196cc9203bfSDan Williams return true; 1972396a265SDan Williams 1982396a265SDan Williams /* we have a spurious interrupt it could be that we have already 1992396a265SDan Williams * emptied the completion queue from a previous interrupt 2002396a265SDan Williams * FIXME: really!? 2012396a265SDan Williams */ 202d9dcb4baSDan Williams writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status); 203cc9203bfSDan Williams 2042396a265SDan Williams /* There is a race in the hardware that could cause us not to be 2052396a265SDan Williams * notified of an interrupt completion if we do not take this 2062396a265SDan Williams * step. We will mask then unmask the interrupts so if there is 2072396a265SDan Williams * another interrupt pending the clearing of the interrupt 2082396a265SDan Williams * source we get the next interrupt message. 2092396a265SDan Williams */ 2102396a265SDan Williams spin_lock(&ihost->scic_lock); 2112396a265SDan Williams if (test_bit(IHOST_IRQ_ENABLED, &ihost->flags)) { 212d9dcb4baSDan Williams writel(0xFF000000, &ihost->smu_registers->interrupt_mask); 213d9dcb4baSDan Williams writel(0, &ihost->smu_registers->interrupt_mask); 214cc9203bfSDan Williams } 2152396a265SDan Williams spin_unlock(&ihost->scic_lock); 216cc9203bfSDan Williams 217cc9203bfSDan Williams return false; 218cc9203bfSDan Williams } 219cc9203bfSDan Williams 220c7ef4031SDan Williams irqreturn_t isci_msix_isr(int vec, void *data) 2216f231ddaSDan Williams { 222c7ef4031SDan Williams struct isci_host *ihost = data; 2236f231ddaSDan Williams 22489a7301fSDan Williams if (sci_controller_isr(ihost)) 225c7ef4031SDan Williams tasklet_schedule(&ihost->completion_tasklet); 2266f231ddaSDan Williams 227c7ef4031SDan Williams return IRQ_HANDLED; 228c7ef4031SDan Williams } 229c7ef4031SDan Williams 23089a7301fSDan Williams static bool sci_controller_error_isr(struct isci_host *ihost) 231cc9203bfSDan Williams { 232cc9203bfSDan Williams u32 interrupt_status; 233cc9203bfSDan Williams 234cc9203bfSDan Williams interrupt_status = 235d9dcb4baSDan Williams readl(&ihost->smu_registers->interrupt_status); 236cc9203bfSDan Williams interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND); 237cc9203bfSDan Williams 238cc9203bfSDan Williams if (interrupt_status != 0) { 239cc9203bfSDan Williams /* 240cc9203bfSDan Williams * There is an error interrupt pending so let it through and handle 241cc9203bfSDan Williams * in the callback */ 242cc9203bfSDan Williams return true; 243cc9203bfSDan Williams } 244cc9203bfSDan Williams 245cc9203bfSDan Williams /* 246cc9203bfSDan Williams * There is a race in the hardware that could cause us not to be notified 247cc9203bfSDan Williams * of an interrupt completion if we do not take this step. We will mask 248cc9203bfSDan Williams * then unmask the error interrupts so if there was another interrupt 249cc9203bfSDan Williams * pending we will be notified. 250cc9203bfSDan Williams * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */ 251d9dcb4baSDan Williams writel(0xff, &ihost->smu_registers->interrupt_mask); 252d9dcb4baSDan Williams writel(0, &ihost->smu_registers->interrupt_mask); 253cc9203bfSDan Williams 254cc9203bfSDan Williams return false; 255cc9203bfSDan Williams } 256cc9203bfSDan Williams 25789a7301fSDan Williams static void sci_controller_task_completion(struct isci_host *ihost, u32 ent) 258cc9203bfSDan Williams { 25989a7301fSDan Williams u32 index = SCU_GET_COMPLETION_INDEX(ent); 260db056250SDan Williams struct isci_request *ireq = ihost->reqs[index]; 261cc9203bfSDan Williams 262cc9203bfSDan Williams /* Make sure that we really want to process this IO request */ 263db056250SDan Williams if (test_bit(IREQ_ACTIVE, &ireq->flags) && 2645076a1a9SDan Williams ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG && 265d9dcb4baSDan Williams ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index]) 26689a7301fSDan Williams /* Yep this is a valid io request pass it along to the 26789a7301fSDan Williams * io request handler 26889a7301fSDan Williams */ 26989a7301fSDan Williams sci_io_request_tc_completion(ireq, ent); 270cc9203bfSDan Williams } 271cc9203bfSDan Williams 27289a7301fSDan Williams static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent) 273cc9203bfSDan Williams { 274cc9203bfSDan Williams u32 index; 2755076a1a9SDan Williams struct isci_request *ireq; 27678a6f06eSDan Williams struct isci_remote_device *idev; 277cc9203bfSDan Williams 27889a7301fSDan Williams index = SCU_GET_COMPLETION_INDEX(ent); 279cc9203bfSDan Williams 28089a7301fSDan Williams switch (scu_get_command_request_type(ent)) { 281cc9203bfSDan Williams case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC: 282cc9203bfSDan Williams case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC: 283d9dcb4baSDan Williams ireq = ihost->reqs[index]; 284d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n", 28589a7301fSDan Williams __func__, ent, ireq); 286cc9203bfSDan Williams /* @todo For a post TC operation we need to fail the IO 287cc9203bfSDan Williams * request 288cc9203bfSDan Williams */ 289cc9203bfSDan Williams break; 290cc9203bfSDan Williams case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC: 291cc9203bfSDan Williams case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC: 292cc9203bfSDan Williams case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC: 293d9dcb4baSDan Williams idev = ihost->device_table[index]; 294d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n", 29589a7301fSDan Williams __func__, ent, idev); 296cc9203bfSDan Williams /* @todo For a port RNC operation we need to fail the 297cc9203bfSDan Williams * device 298cc9203bfSDan Williams */ 299cc9203bfSDan Williams break; 300cc9203bfSDan Williams default: 301d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n", 30289a7301fSDan Williams __func__, ent); 303cc9203bfSDan Williams break; 304cc9203bfSDan Williams } 305cc9203bfSDan Williams } 306cc9203bfSDan Williams 30789a7301fSDan Williams static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent) 308cc9203bfSDan Williams { 309cc9203bfSDan Williams u32 index; 310cc9203bfSDan Williams u32 frame_index; 311cc9203bfSDan Williams 312cc9203bfSDan Williams struct scu_unsolicited_frame_header *frame_header; 31385280955SDan Williams struct isci_phy *iphy; 31478a6f06eSDan Williams struct isci_remote_device *idev; 315cc9203bfSDan Williams 316cc9203bfSDan Williams enum sci_status result = SCI_FAILURE; 317cc9203bfSDan Williams 31889a7301fSDan Williams frame_index = SCU_GET_FRAME_INDEX(ent); 319cc9203bfSDan Williams 320d9dcb4baSDan Williams frame_header = ihost->uf_control.buffers.array[frame_index].header; 321d9dcb4baSDan Williams ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE; 322cc9203bfSDan Williams 32389a7301fSDan Williams if (SCU_GET_FRAME_ERROR(ent)) { 324cc9203bfSDan Williams /* 325cc9203bfSDan Williams * / @todo If the IAF frame or SIGNATURE FIS frame has an error will 326cc9203bfSDan Williams * / this cause a problem? We expect the phy initialization will 327cc9203bfSDan Williams * / fail if there is an error in the frame. */ 32889a7301fSDan Williams sci_controller_release_frame(ihost, frame_index); 329cc9203bfSDan Williams return; 330cc9203bfSDan Williams } 331cc9203bfSDan Williams 332cc9203bfSDan Williams if (frame_header->is_address_frame) { 33389a7301fSDan Williams index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent); 33485280955SDan Williams iphy = &ihost->phys[index]; 33589a7301fSDan Williams result = sci_phy_frame_handler(iphy, frame_index); 336cc9203bfSDan Williams } else { 337cc9203bfSDan Williams 33889a7301fSDan Williams index = SCU_GET_COMPLETION_INDEX(ent); 339cc9203bfSDan Williams 340cc9203bfSDan Williams if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) { 341cc9203bfSDan Williams /* 342cc9203bfSDan Williams * This is a signature fis or a frame from a direct attached SATA 343cc9203bfSDan Williams * device that has not yet been created. In either case forwared 344cc9203bfSDan Williams * the frame to the PE and let it take care of the frame data. */ 34589a7301fSDan Williams index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent); 34685280955SDan Williams iphy = &ihost->phys[index]; 34789a7301fSDan Williams result = sci_phy_frame_handler(iphy, frame_index); 348cc9203bfSDan Williams } else { 349d9dcb4baSDan Williams if (index < ihost->remote_node_entries) 350d9dcb4baSDan Williams idev = ihost->device_table[index]; 351cc9203bfSDan Williams else 35278a6f06eSDan Williams idev = NULL; 353cc9203bfSDan Williams 35478a6f06eSDan Williams if (idev != NULL) 35589a7301fSDan Williams result = sci_remote_device_frame_handler(idev, frame_index); 356cc9203bfSDan Williams else 35789a7301fSDan Williams sci_controller_release_frame(ihost, frame_index); 358cc9203bfSDan Williams } 359cc9203bfSDan Williams } 360cc9203bfSDan Williams 361cc9203bfSDan Williams if (result != SCI_SUCCESS) { 362cc9203bfSDan Williams /* 363cc9203bfSDan Williams * / @todo Is there any reason to report some additional error message 364cc9203bfSDan Williams * / when we get this failure notifiction? */ 365cc9203bfSDan Williams } 366cc9203bfSDan Williams } 367cc9203bfSDan Williams 36889a7301fSDan Williams static void sci_controller_event_completion(struct isci_host *ihost, u32 ent) 369cc9203bfSDan Williams { 37078a6f06eSDan Williams struct isci_remote_device *idev; 3715076a1a9SDan Williams struct isci_request *ireq; 37285280955SDan Williams struct isci_phy *iphy; 373cc9203bfSDan Williams u32 index; 374cc9203bfSDan Williams 37589a7301fSDan Williams index = SCU_GET_COMPLETION_INDEX(ent); 376cc9203bfSDan Williams 37789a7301fSDan Williams switch (scu_get_event_type(ent)) { 378cc9203bfSDan Williams case SCU_EVENT_TYPE_SMU_COMMAND_ERROR: 379cc9203bfSDan Williams /* / @todo The driver did something wrong and we need to fix the condtion. */ 380d9dcb4baSDan Williams dev_err(&ihost->pdev->dev, 381cc9203bfSDan Williams "%s: SCIC Controller 0x%p received SMU command error " 382cc9203bfSDan Williams "0x%x\n", 383cc9203bfSDan Williams __func__, 384d9dcb4baSDan Williams ihost, 38589a7301fSDan Williams ent); 386cc9203bfSDan Williams break; 387cc9203bfSDan Williams 388cc9203bfSDan Williams case SCU_EVENT_TYPE_SMU_PCQ_ERROR: 389cc9203bfSDan Williams case SCU_EVENT_TYPE_SMU_ERROR: 390cc9203bfSDan Williams case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR: 391cc9203bfSDan Williams /* 392cc9203bfSDan Williams * / @todo This is a hardware failure and its likely that we want to 393cc9203bfSDan Williams * / reset the controller. */ 394d9dcb4baSDan Williams dev_err(&ihost->pdev->dev, 395cc9203bfSDan Williams "%s: SCIC Controller 0x%p received fatal controller " 396cc9203bfSDan Williams "event 0x%x\n", 397cc9203bfSDan Williams __func__, 398d9dcb4baSDan Williams ihost, 39989a7301fSDan Williams ent); 400cc9203bfSDan Williams break; 401cc9203bfSDan Williams 402cc9203bfSDan Williams case SCU_EVENT_TYPE_TRANSPORT_ERROR: 4035076a1a9SDan Williams ireq = ihost->reqs[index]; 40489a7301fSDan Williams sci_io_request_event_handler(ireq, ent); 405cc9203bfSDan Williams break; 406cc9203bfSDan Williams 407cc9203bfSDan Williams case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT: 40889a7301fSDan Williams switch (scu_get_event_specifier(ent)) { 409cc9203bfSDan Williams case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE: 410cc9203bfSDan Williams case SCU_EVENT_SPECIFIC_TASK_TIMEOUT: 4115076a1a9SDan Williams ireq = ihost->reqs[index]; 4125076a1a9SDan Williams if (ireq != NULL) 41389a7301fSDan Williams sci_io_request_event_handler(ireq, ent); 414cc9203bfSDan Williams else 415d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 416cc9203bfSDan Williams "%s: SCIC Controller 0x%p received " 417cc9203bfSDan Williams "event 0x%x for io request object " 418cc9203bfSDan Williams "that doesnt exist.\n", 419cc9203bfSDan Williams __func__, 420d9dcb4baSDan Williams ihost, 42189a7301fSDan Williams ent); 422cc9203bfSDan Williams 423cc9203bfSDan Williams break; 424cc9203bfSDan Williams 425cc9203bfSDan Williams case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT: 426d9dcb4baSDan Williams idev = ihost->device_table[index]; 42778a6f06eSDan Williams if (idev != NULL) 42889a7301fSDan Williams sci_remote_device_event_handler(idev, ent); 429cc9203bfSDan Williams else 430d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 431cc9203bfSDan Williams "%s: SCIC Controller 0x%p received " 432cc9203bfSDan Williams "event 0x%x for remote device object " 433cc9203bfSDan Williams "that doesnt exist.\n", 434cc9203bfSDan Williams __func__, 435d9dcb4baSDan Williams ihost, 43689a7301fSDan Williams ent); 437cc9203bfSDan Williams 438cc9203bfSDan Williams break; 439cc9203bfSDan Williams } 440cc9203bfSDan Williams break; 441cc9203bfSDan Williams 442cc9203bfSDan Williams case SCU_EVENT_TYPE_BROADCAST_CHANGE: 443cc9203bfSDan Williams /* 444cc9203bfSDan Williams * direct the broadcast change event to the phy first and then let 445cc9203bfSDan Williams * the phy redirect the broadcast change to the port object */ 446cc9203bfSDan Williams case SCU_EVENT_TYPE_ERR_CNT_EVENT: 447cc9203bfSDan Williams /* 448cc9203bfSDan Williams * direct error counter event to the phy object since that is where 449cc9203bfSDan Williams * we get the event notification. This is a type 4 event. */ 450cc9203bfSDan Williams case SCU_EVENT_TYPE_OSSP_EVENT: 45189a7301fSDan Williams index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent); 45285280955SDan Williams iphy = &ihost->phys[index]; 45389a7301fSDan Williams sci_phy_event_handler(iphy, ent); 454cc9203bfSDan Williams break; 455cc9203bfSDan Williams 456cc9203bfSDan Williams case SCU_EVENT_TYPE_RNC_SUSPEND_TX: 457cc9203bfSDan Williams case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX: 458cc9203bfSDan Williams case SCU_EVENT_TYPE_RNC_OPS_MISC: 459d9dcb4baSDan Williams if (index < ihost->remote_node_entries) { 460d9dcb4baSDan Williams idev = ihost->device_table[index]; 461cc9203bfSDan Williams 46278a6f06eSDan Williams if (idev != NULL) 46389a7301fSDan Williams sci_remote_device_event_handler(idev, ent); 464cc9203bfSDan Williams } else 465d9dcb4baSDan Williams dev_err(&ihost->pdev->dev, 466cc9203bfSDan Williams "%s: SCIC Controller 0x%p received event 0x%x " 467cc9203bfSDan Williams "for remote device object 0x%0x that doesnt " 468cc9203bfSDan Williams "exist.\n", 469cc9203bfSDan Williams __func__, 470d9dcb4baSDan Williams ihost, 47189a7301fSDan Williams ent, 472cc9203bfSDan Williams index); 473cc9203bfSDan Williams 474cc9203bfSDan Williams break; 475cc9203bfSDan Williams 476cc9203bfSDan Williams default: 477d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 478cc9203bfSDan Williams "%s: SCIC Controller received unknown event code %x\n", 479cc9203bfSDan Williams __func__, 48089a7301fSDan Williams ent); 481cc9203bfSDan Williams break; 482cc9203bfSDan Williams } 483cc9203bfSDan Williams } 484cc9203bfSDan Williams 48589a7301fSDan Williams static void sci_controller_process_completions(struct isci_host *ihost) 486cc9203bfSDan Williams { 487cc9203bfSDan Williams u32 completion_count = 0; 48889a7301fSDan Williams u32 ent; 489cc9203bfSDan Williams u32 get_index; 490cc9203bfSDan Williams u32 get_cycle; 491994a9303SDan Williams u32 event_get; 492cc9203bfSDan Williams u32 event_cycle; 493cc9203bfSDan Williams 494d9dcb4baSDan Williams dev_dbg(&ihost->pdev->dev, 495cc9203bfSDan Williams "%s: completion queue begining get:0x%08x\n", 496cc9203bfSDan Williams __func__, 497d9dcb4baSDan Williams ihost->completion_queue_get); 498cc9203bfSDan Williams 499cc9203bfSDan Williams /* Get the component parts of the completion queue */ 500d9dcb4baSDan Williams get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get); 501d9dcb4baSDan Williams get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get; 502cc9203bfSDan Williams 503d9dcb4baSDan Williams event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get); 504d9dcb4baSDan Williams event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get; 505cc9203bfSDan Williams 506cc9203bfSDan Williams while ( 507cc9203bfSDan Williams NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle) 508d9dcb4baSDan Williams == COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]) 509cc9203bfSDan Williams ) { 510cc9203bfSDan Williams completion_count++; 511cc9203bfSDan Williams 51289a7301fSDan Williams ent = ihost->completion_queue[get_index]; 513994a9303SDan Williams 514994a9303SDan Williams /* increment the get pointer and check for rollover to toggle the cycle bit */ 515994a9303SDan Williams get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) << 516994a9303SDan Williams (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT); 517994a9303SDan Williams get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1); 518cc9203bfSDan Williams 519d9dcb4baSDan Williams dev_dbg(&ihost->pdev->dev, 520cc9203bfSDan Williams "%s: completion queue entry:0x%08x\n", 521cc9203bfSDan Williams __func__, 52289a7301fSDan Williams ent); 523cc9203bfSDan Williams 52489a7301fSDan Williams switch (SCU_GET_COMPLETION_TYPE(ent)) { 525cc9203bfSDan Williams case SCU_COMPLETION_TYPE_TASK: 52689a7301fSDan Williams sci_controller_task_completion(ihost, ent); 527cc9203bfSDan Williams break; 528cc9203bfSDan Williams 529cc9203bfSDan Williams case SCU_COMPLETION_TYPE_SDMA: 53089a7301fSDan Williams sci_controller_sdma_completion(ihost, ent); 531cc9203bfSDan Williams break; 532cc9203bfSDan Williams 533cc9203bfSDan Williams case SCU_COMPLETION_TYPE_UFI: 53489a7301fSDan Williams sci_controller_unsolicited_frame(ihost, ent); 535cc9203bfSDan Williams break; 536cc9203bfSDan Williams 537cc9203bfSDan Williams case SCU_COMPLETION_TYPE_EVENT: 53877cd72a5SDan Williams sci_controller_event_completion(ihost, ent); 53977cd72a5SDan Williams break; 54077cd72a5SDan Williams 541994a9303SDan Williams case SCU_COMPLETION_TYPE_NOTIFY: { 542994a9303SDan Williams event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) << 543994a9303SDan Williams (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT); 544994a9303SDan Williams event_get = (event_get+1) & (SCU_MAX_EVENTS-1); 545994a9303SDan Williams 54689a7301fSDan Williams sci_controller_event_completion(ihost, ent); 547cc9203bfSDan Williams break; 548994a9303SDan Williams } 549cc9203bfSDan Williams default: 550d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 551cc9203bfSDan Williams "%s: SCIC Controller received unknown " 552cc9203bfSDan Williams "completion type %x\n", 553cc9203bfSDan Williams __func__, 55489a7301fSDan Williams ent); 555cc9203bfSDan Williams break; 556cc9203bfSDan Williams } 557cc9203bfSDan Williams } 558cc9203bfSDan Williams 559cc9203bfSDan Williams /* Update the get register if we completed one or more entries */ 560cc9203bfSDan Williams if (completion_count > 0) { 561d9dcb4baSDan Williams ihost->completion_queue_get = 562cc9203bfSDan Williams SMU_CQGR_GEN_BIT(ENABLE) | 563cc9203bfSDan Williams SMU_CQGR_GEN_BIT(EVENT_ENABLE) | 564cc9203bfSDan Williams event_cycle | 565994a9303SDan Williams SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) | 566cc9203bfSDan Williams get_cycle | 567cc9203bfSDan Williams SMU_CQGR_GEN_VAL(POINTER, get_index); 568cc9203bfSDan Williams 569d9dcb4baSDan Williams writel(ihost->completion_queue_get, 570d9dcb4baSDan Williams &ihost->smu_registers->completion_queue_get); 571cc9203bfSDan Williams 572cc9203bfSDan Williams } 573cc9203bfSDan Williams 574d9dcb4baSDan Williams dev_dbg(&ihost->pdev->dev, 575cc9203bfSDan Williams "%s: completion queue ending get:0x%08x\n", 576cc9203bfSDan Williams __func__, 577d9dcb4baSDan Williams ihost->completion_queue_get); 578cc9203bfSDan Williams 579cc9203bfSDan Williams } 580cc9203bfSDan Williams 58189a7301fSDan Williams static void sci_controller_error_handler(struct isci_host *ihost) 582cc9203bfSDan Williams { 583cc9203bfSDan Williams u32 interrupt_status; 584cc9203bfSDan Williams 585cc9203bfSDan Williams interrupt_status = 586d9dcb4baSDan Williams readl(&ihost->smu_registers->interrupt_status); 587cc9203bfSDan Williams 588cc9203bfSDan Williams if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) && 58989a7301fSDan Williams sci_controller_completion_queue_has_entries(ihost)) { 590cc9203bfSDan Williams 59189a7301fSDan Williams sci_controller_process_completions(ihost); 592d9dcb4baSDan Williams writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status); 593cc9203bfSDan Williams } else { 594d9dcb4baSDan Williams dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__, 595cc9203bfSDan Williams interrupt_status); 596cc9203bfSDan Williams 597d9dcb4baSDan Williams sci_change_state(&ihost->sm, SCIC_FAILED); 598cc9203bfSDan Williams 599cc9203bfSDan Williams return; 600cc9203bfSDan Williams } 601cc9203bfSDan Williams 602cc9203bfSDan Williams /* If we dont process any completions I am not sure that we want to do this. 603cc9203bfSDan Williams * We are in the middle of a hardware fault and should probably be reset. 604cc9203bfSDan Williams */ 605d9dcb4baSDan Williams writel(0, &ihost->smu_registers->interrupt_mask); 606cc9203bfSDan Williams } 607cc9203bfSDan Williams 608c7ef4031SDan Williams irqreturn_t isci_intx_isr(int vec, void *data) 6096f231ddaSDan Williams { 6106f231ddaSDan Williams irqreturn_t ret = IRQ_NONE; 61131e824edSDan Williams struct isci_host *ihost = data; 6126f231ddaSDan Williams 61389a7301fSDan Williams if (sci_controller_isr(ihost)) { 614d9dcb4baSDan Williams writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status); 615c7ef4031SDan Williams tasklet_schedule(&ihost->completion_tasklet); 6166f231ddaSDan Williams ret = IRQ_HANDLED; 61789a7301fSDan Williams } else if (sci_controller_error_isr(ihost)) { 61892f4f0f5SDan Williams spin_lock(&ihost->scic_lock); 61989a7301fSDan Williams sci_controller_error_handler(ihost); 62092f4f0f5SDan Williams spin_unlock(&ihost->scic_lock); 62192f4f0f5SDan Williams ret = IRQ_HANDLED; 6226f231ddaSDan Williams } 62392f4f0f5SDan Williams 6246f231ddaSDan Williams return ret; 6256f231ddaSDan Williams } 6266f231ddaSDan Williams 62792f4f0f5SDan Williams irqreturn_t isci_error_isr(int vec, void *data) 62892f4f0f5SDan Williams { 62992f4f0f5SDan Williams struct isci_host *ihost = data; 63092f4f0f5SDan Williams 63189a7301fSDan Williams if (sci_controller_error_isr(ihost)) 63289a7301fSDan Williams sci_controller_error_handler(ihost); 63392f4f0f5SDan Williams 63492f4f0f5SDan Williams return IRQ_HANDLED; 63592f4f0f5SDan Williams } 6366f231ddaSDan Williams 6376f231ddaSDan Williams /** 6386f231ddaSDan Williams * isci_host_start_complete() - This function is called by the core library, 6396f231ddaSDan Williams * through the ISCI Module, to indicate controller start status. 6406f231ddaSDan Williams * @isci_host: This parameter specifies the ISCI host object 6416f231ddaSDan Williams * @completion_status: This parameter specifies the completion status from the 6426f231ddaSDan Williams * core library. 6436f231ddaSDan Williams * 6446f231ddaSDan Williams */ 645cc9203bfSDan Williams static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status) 6466f231ddaSDan Williams { 6470cf89d1dSDan Williams if (completion_status != SCI_SUCCESS) 6480cf89d1dSDan Williams dev_info(&ihost->pdev->dev, 6490cf89d1dSDan Williams "controller start timed out, continuing...\n"); 6500cf89d1dSDan Williams clear_bit(IHOST_START_PENDING, &ihost->flags); 6510cf89d1dSDan Williams wake_up(&ihost->eventq); 6526f231ddaSDan Williams } 6536f231ddaSDan Williams 654c7ef4031SDan Williams int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time) 6556f231ddaSDan Williams { 656b1124cd3SDan Williams struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 657b1124cd3SDan Williams struct isci_host *ihost = ha->lldd_ha; 6586f231ddaSDan Williams 65977950f51SEdmund Nadolski if (test_bit(IHOST_START_PENDING, &ihost->flags)) 6606f231ddaSDan Williams return 0; 6616f231ddaSDan Williams 662b1124cd3SDan Williams sas_drain_work(ha); 6636f231ddaSDan Williams 6646f231ddaSDan Williams return 1; 6656f231ddaSDan Williams } 6666f231ddaSDan Williams 667cc9203bfSDan Williams /** 66889a7301fSDan Williams * sci_controller_get_suggested_start_timeout() - This method returns the 66989a7301fSDan Williams * suggested sci_controller_start() timeout amount. The user is free to 670cc9203bfSDan Williams * use any timeout value, but this method provides the suggested minimum 671cc9203bfSDan Williams * start timeout value. The returned value is based upon empirical 672cc9203bfSDan Williams * information determined as a result of interoperability testing. 673cc9203bfSDan Williams * @controller: the handle to the controller object for which to return the 674cc9203bfSDan Williams * suggested start timeout. 675cc9203bfSDan Williams * 676cc9203bfSDan Williams * This method returns the number of milliseconds for the suggested start 677cc9203bfSDan Williams * operation timeout. 678cc9203bfSDan Williams */ 67989a7301fSDan Williams static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost) 680cc9203bfSDan Williams { 681cc9203bfSDan Williams /* Validate the user supplied parameters. */ 682d9dcb4baSDan Williams if (!ihost) 683cc9203bfSDan Williams return 0; 684cc9203bfSDan Williams 685cc9203bfSDan Williams /* 686cc9203bfSDan Williams * The suggested minimum timeout value for a controller start operation: 687cc9203bfSDan Williams * 688cc9203bfSDan Williams * Signature FIS Timeout 689cc9203bfSDan Williams * + Phy Start Timeout 690cc9203bfSDan Williams * + Number of Phy Spin Up Intervals 691cc9203bfSDan Williams * --------------------------------- 692cc9203bfSDan Williams * Number of milliseconds for the controller start operation. 693cc9203bfSDan Williams * 694cc9203bfSDan Williams * NOTE: The number of phy spin up intervals will be equivalent 695cc9203bfSDan Williams * to the number of phys divided by the number phys allowed 696cc9203bfSDan Williams * per interval - 1 (once OEM parameters are supported). 697cc9203bfSDan Williams * Currently we assume only 1 phy per interval. */ 698cc9203bfSDan Williams 699cc9203bfSDan Williams return SCIC_SDS_SIGNATURE_FIS_TIMEOUT 700cc9203bfSDan Williams + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 701cc9203bfSDan Williams + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL); 702cc9203bfSDan Williams } 703cc9203bfSDan Williams 70489a7301fSDan Williams static void sci_controller_enable_interrupts(struct isci_host *ihost) 705cc9203bfSDan Williams { 7062396a265SDan Williams set_bit(IHOST_IRQ_ENABLED, &ihost->flags); 707d9dcb4baSDan Williams writel(0, &ihost->smu_registers->interrupt_mask); 708cc9203bfSDan Williams } 709cc9203bfSDan Williams 71089a7301fSDan Williams void sci_controller_disable_interrupts(struct isci_host *ihost) 711cc9203bfSDan Williams { 7122396a265SDan Williams clear_bit(IHOST_IRQ_ENABLED, &ihost->flags); 713d9dcb4baSDan Williams writel(0xffffffff, &ihost->smu_registers->interrupt_mask); 7142396a265SDan Williams readl(&ihost->smu_registers->interrupt_mask); /* flush */ 715cc9203bfSDan Williams } 716cc9203bfSDan Williams 71789a7301fSDan Williams static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost) 718cc9203bfSDan Williams { 719cc9203bfSDan Williams u32 port_task_scheduler_value; 720cc9203bfSDan Williams 721cc9203bfSDan Williams port_task_scheduler_value = 722d9dcb4baSDan Williams readl(&ihost->scu_registers->peg0.ptsg.control); 723cc9203bfSDan Williams port_task_scheduler_value |= 724cc9203bfSDan Williams (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) | 725cc9203bfSDan Williams SCU_PTSGCR_GEN_BIT(PTSG_ENABLE)); 726cc9203bfSDan Williams writel(port_task_scheduler_value, 727d9dcb4baSDan Williams &ihost->scu_registers->peg0.ptsg.control); 728cc9203bfSDan Williams } 729cc9203bfSDan Williams 73089a7301fSDan Williams static void sci_controller_assign_task_entries(struct isci_host *ihost) 731cc9203bfSDan Williams { 732cc9203bfSDan Williams u32 task_assignment; 733cc9203bfSDan Williams 734cc9203bfSDan Williams /* 735cc9203bfSDan Williams * Assign all the TCs to function 0 736cc9203bfSDan Williams * TODO: Do we actually need to read this register to write it back? 737cc9203bfSDan Williams */ 738cc9203bfSDan Williams 739cc9203bfSDan Williams task_assignment = 740d9dcb4baSDan Williams readl(&ihost->smu_registers->task_context_assignment[0]); 741cc9203bfSDan Williams 742cc9203bfSDan Williams task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) | 743d9dcb4baSDan Williams (SMU_TCA_GEN_VAL(ENDING, ihost->task_context_entries - 1)) | 744cc9203bfSDan Williams (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE)); 745cc9203bfSDan Williams 746cc9203bfSDan Williams writel(task_assignment, 747d9dcb4baSDan Williams &ihost->smu_registers->task_context_assignment[0]); 748cc9203bfSDan Williams 749cc9203bfSDan Williams } 750cc9203bfSDan Williams 75189a7301fSDan Williams static void sci_controller_initialize_completion_queue(struct isci_host *ihost) 752cc9203bfSDan Williams { 753cc9203bfSDan Williams u32 index; 754cc9203bfSDan Williams u32 completion_queue_control_value; 755cc9203bfSDan Williams u32 completion_queue_get_value; 756cc9203bfSDan Williams u32 completion_queue_put_value; 757cc9203bfSDan Williams 758d9dcb4baSDan Williams ihost->completion_queue_get = 0; 759cc9203bfSDan Williams 7607c78da31SDan Williams completion_queue_control_value = 7617c78da31SDan Williams (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) | 7627c78da31SDan Williams SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1)); 763cc9203bfSDan Williams 764cc9203bfSDan Williams writel(completion_queue_control_value, 765d9dcb4baSDan Williams &ihost->smu_registers->completion_queue_control); 766cc9203bfSDan Williams 767cc9203bfSDan Williams 768cc9203bfSDan Williams /* Set the completion queue get pointer and enable the queue */ 769cc9203bfSDan Williams completion_queue_get_value = ( 770cc9203bfSDan Williams (SMU_CQGR_GEN_VAL(POINTER, 0)) 771cc9203bfSDan Williams | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0)) 772cc9203bfSDan Williams | (SMU_CQGR_GEN_BIT(ENABLE)) 773cc9203bfSDan Williams | (SMU_CQGR_GEN_BIT(EVENT_ENABLE)) 774cc9203bfSDan Williams ); 775cc9203bfSDan Williams 776cc9203bfSDan Williams writel(completion_queue_get_value, 777d9dcb4baSDan Williams &ihost->smu_registers->completion_queue_get); 778cc9203bfSDan Williams 779cc9203bfSDan Williams /* Set the completion queue put pointer */ 780cc9203bfSDan Williams completion_queue_put_value = ( 781cc9203bfSDan Williams (SMU_CQPR_GEN_VAL(POINTER, 0)) 782cc9203bfSDan Williams | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0)) 783cc9203bfSDan Williams ); 784cc9203bfSDan Williams 785cc9203bfSDan Williams writel(completion_queue_put_value, 786d9dcb4baSDan Williams &ihost->smu_registers->completion_queue_put); 787cc9203bfSDan Williams 788cc9203bfSDan Williams /* Initialize the cycle bit of the completion queue entries */ 7897c78da31SDan Williams for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) { 790cc9203bfSDan Williams /* 791cc9203bfSDan Williams * If get.cycle_bit != completion_queue.cycle_bit 792cc9203bfSDan Williams * its not a valid completion queue entry 793cc9203bfSDan Williams * so at system start all entries are invalid */ 794d9dcb4baSDan Williams ihost->completion_queue[index] = 0x80000000; 795cc9203bfSDan Williams } 796cc9203bfSDan Williams } 797cc9203bfSDan Williams 79889a7301fSDan Williams static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost) 799cc9203bfSDan Williams { 800cc9203bfSDan Williams u32 frame_queue_control_value; 801cc9203bfSDan Williams u32 frame_queue_get_value; 802cc9203bfSDan Williams u32 frame_queue_put_value; 803cc9203bfSDan Williams 804cc9203bfSDan Williams /* Write the queue size */ 805cc9203bfSDan Williams frame_queue_control_value = 8067c78da31SDan Williams SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES); 807cc9203bfSDan Williams 808cc9203bfSDan Williams writel(frame_queue_control_value, 809d9dcb4baSDan Williams &ihost->scu_registers->sdma.unsolicited_frame_queue_control); 810cc9203bfSDan Williams 811cc9203bfSDan Williams /* Setup the get pointer for the unsolicited frame queue */ 812cc9203bfSDan Williams frame_queue_get_value = ( 813cc9203bfSDan Williams SCU_UFQGP_GEN_VAL(POINTER, 0) 814cc9203bfSDan Williams | SCU_UFQGP_GEN_BIT(ENABLE_BIT) 815cc9203bfSDan Williams ); 816cc9203bfSDan Williams 817cc9203bfSDan Williams writel(frame_queue_get_value, 818d9dcb4baSDan Williams &ihost->scu_registers->sdma.unsolicited_frame_get_pointer); 819cc9203bfSDan Williams /* Setup the put pointer for the unsolicited frame queue */ 820cc9203bfSDan Williams frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0); 821cc9203bfSDan Williams writel(frame_queue_put_value, 822d9dcb4baSDan Williams &ihost->scu_registers->sdma.unsolicited_frame_put_pointer); 823cc9203bfSDan Williams } 824cc9203bfSDan Williams 82550a92d93SDan Williams void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status) 826cc9203bfSDan Williams { 827d9dcb4baSDan Williams if (ihost->sm.current_state_id == SCIC_STARTING) { 828cc9203bfSDan Williams /* 829cc9203bfSDan Williams * We move into the ready state, because some of the phys/ports 830cc9203bfSDan Williams * may be up and operational. 831cc9203bfSDan Williams */ 832d9dcb4baSDan Williams sci_change_state(&ihost->sm, SCIC_READY); 833cc9203bfSDan Williams 834cc9203bfSDan Williams isci_host_start_complete(ihost, status); 835cc9203bfSDan Williams } 836cc9203bfSDan Williams } 837cc9203bfSDan Williams 83885280955SDan Williams static bool is_phy_starting(struct isci_phy *iphy) 8394a33c525SAdam Gruchala { 84089a7301fSDan Williams enum sci_phy_states state; 8414a33c525SAdam Gruchala 84285280955SDan Williams state = iphy->sm.current_state_id; 8434a33c525SAdam Gruchala switch (state) { 844e301370aSEdmund Nadolski case SCI_PHY_STARTING: 845e301370aSEdmund Nadolski case SCI_PHY_SUB_INITIAL: 846e301370aSEdmund Nadolski case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN: 847e301370aSEdmund Nadolski case SCI_PHY_SUB_AWAIT_IAF_UF: 848e301370aSEdmund Nadolski case SCI_PHY_SUB_AWAIT_SAS_POWER: 849e301370aSEdmund Nadolski case SCI_PHY_SUB_AWAIT_SATA_POWER: 850e301370aSEdmund Nadolski case SCI_PHY_SUB_AWAIT_SATA_PHY_EN: 851e301370aSEdmund Nadolski case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN: 85250a92d93SDan Williams case SCI_PHY_SUB_AWAIT_OSSP_EN: 853e301370aSEdmund Nadolski case SCI_PHY_SUB_AWAIT_SIG_FIS_UF: 854e301370aSEdmund Nadolski case SCI_PHY_SUB_FINAL: 8554a33c525SAdam Gruchala return true; 8564a33c525SAdam Gruchala default: 8574a33c525SAdam Gruchala return false; 8584a33c525SAdam Gruchala } 8594a33c525SAdam Gruchala } 8604a33c525SAdam Gruchala 86150a92d93SDan Williams bool is_controller_start_complete(struct isci_host *ihost) 86250a92d93SDan Williams { 86350a92d93SDan Williams int i; 86450a92d93SDan Williams 86550a92d93SDan Williams for (i = 0; i < SCI_MAX_PHYS; i++) { 86650a92d93SDan Williams struct isci_phy *iphy = &ihost->phys[i]; 86750a92d93SDan Williams u32 state = iphy->sm.current_state_id; 86850a92d93SDan Williams 86950a92d93SDan Williams /* in apc mode we need to check every phy, in 87050a92d93SDan Williams * mpc mode we only need to check phys that have 87150a92d93SDan Williams * been configured into a port 87250a92d93SDan Williams */ 87350a92d93SDan Williams if (is_port_config_apc(ihost)) 87450a92d93SDan Williams /* pass */; 87550a92d93SDan Williams else if (!phy_get_non_dummy_port(iphy)) 87650a92d93SDan Williams continue; 87750a92d93SDan Williams 87850a92d93SDan Williams /* The controller start operation is complete iff: 87950a92d93SDan Williams * - all links have been given an opportunity to start 88050a92d93SDan Williams * - have no indication of a connected device 88150a92d93SDan Williams * - have an indication of a connected device and it has 88250a92d93SDan Williams * finished the link training process. 88350a92d93SDan Williams */ 88450a92d93SDan Williams if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) || 88550a92d93SDan Williams (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) || 88650a92d93SDan Williams (iphy->is_in_link_training == true && is_phy_starting(iphy)) || 88750a92d93SDan Williams (ihost->port_agent.phy_ready_mask != ihost->port_agent.phy_configured_mask)) 88850a92d93SDan Williams return false; 88950a92d93SDan Williams } 89050a92d93SDan Williams 89150a92d93SDan Williams return true; 89250a92d93SDan Williams } 89350a92d93SDan Williams 894cc9203bfSDan Williams /** 89589a7301fSDan Williams * sci_controller_start_next_phy - start phy 896cc9203bfSDan Williams * @scic: controller 897cc9203bfSDan Williams * 898cc9203bfSDan Williams * If all the phys have been started, then attempt to transition the 899cc9203bfSDan Williams * controller to the READY state and inform the user 90089a7301fSDan Williams * (sci_cb_controller_start_complete()). 901cc9203bfSDan Williams */ 90289a7301fSDan Williams static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost) 903cc9203bfSDan Williams { 90489a7301fSDan Williams struct sci_oem_params *oem = &ihost->oem_parameters; 90585280955SDan Williams struct isci_phy *iphy; 906cc9203bfSDan Williams enum sci_status status; 907cc9203bfSDan Williams 908cc9203bfSDan Williams status = SCI_SUCCESS; 909cc9203bfSDan Williams 910d9dcb4baSDan Williams if (ihost->phy_startup_timer_pending) 911cc9203bfSDan Williams return status; 912cc9203bfSDan Williams 913d9dcb4baSDan Williams if (ihost->next_phy_to_start >= SCI_MAX_PHYS) { 91450a92d93SDan Williams if (is_controller_start_complete(ihost)) { 91589a7301fSDan Williams sci_controller_transition_to_ready(ihost, SCI_SUCCESS); 916d9dcb4baSDan Williams sci_del_timer(&ihost->phy_timer); 917d9dcb4baSDan Williams ihost->phy_startup_timer_pending = false; 918cc9203bfSDan Williams } 919cc9203bfSDan Williams } else { 920d9dcb4baSDan Williams iphy = &ihost->phys[ihost->next_phy_to_start]; 921cc9203bfSDan Williams 922cc9203bfSDan Williams if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) { 92385280955SDan Williams if (phy_get_non_dummy_port(iphy) == NULL) { 924d9dcb4baSDan Williams ihost->next_phy_to_start++; 925cc9203bfSDan Williams 926cc9203bfSDan Williams /* Caution recursion ahead be forwarned 927cc9203bfSDan Williams * 928cc9203bfSDan Williams * The PHY was never added to a PORT in MPC mode 929cc9203bfSDan Williams * so start the next phy in sequence This phy 930cc9203bfSDan Williams * will never go link up and will not draw power 931cc9203bfSDan Williams * the OEM parameters either configured the phy 932cc9203bfSDan Williams * incorrectly for the PORT or it was never 933cc9203bfSDan Williams * assigned to a PORT 934cc9203bfSDan Williams */ 93589a7301fSDan Williams return sci_controller_start_next_phy(ihost); 936cc9203bfSDan Williams } 937cc9203bfSDan Williams } 938cc9203bfSDan Williams 93989a7301fSDan Williams status = sci_phy_start(iphy); 940cc9203bfSDan Williams 941cc9203bfSDan Williams if (status == SCI_SUCCESS) { 942d9dcb4baSDan Williams sci_mod_timer(&ihost->phy_timer, 943bb3dbdf6SEdmund Nadolski SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT); 944d9dcb4baSDan Williams ihost->phy_startup_timer_pending = true; 945cc9203bfSDan Williams } else { 946d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 947cc9203bfSDan Williams "%s: Controller stop operation failed " 948cc9203bfSDan Williams "to stop phy %d because of status " 949cc9203bfSDan Williams "%d.\n", 950cc9203bfSDan Williams __func__, 951d9dcb4baSDan Williams ihost->phys[ihost->next_phy_to_start].phy_index, 952cc9203bfSDan Williams status); 953cc9203bfSDan Williams } 954cc9203bfSDan Williams 955d9dcb4baSDan Williams ihost->next_phy_to_start++; 956cc9203bfSDan Williams } 957cc9203bfSDan Williams 958cc9203bfSDan Williams return status; 959cc9203bfSDan Williams } 960cc9203bfSDan Williams 961bb3dbdf6SEdmund Nadolski static void phy_startup_timeout(unsigned long data) 962cc9203bfSDan Williams { 963bb3dbdf6SEdmund Nadolski struct sci_timer *tmr = (struct sci_timer *)data; 964d9dcb4baSDan Williams struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer); 965bb3dbdf6SEdmund Nadolski unsigned long flags; 966cc9203bfSDan Williams enum sci_status status; 967cc9203bfSDan Williams 968bb3dbdf6SEdmund Nadolski spin_lock_irqsave(&ihost->scic_lock, flags); 969bb3dbdf6SEdmund Nadolski 970bb3dbdf6SEdmund Nadolski if (tmr->cancel) 971bb3dbdf6SEdmund Nadolski goto done; 972bb3dbdf6SEdmund Nadolski 973d9dcb4baSDan Williams ihost->phy_startup_timer_pending = false; 974bb3dbdf6SEdmund Nadolski 975bb3dbdf6SEdmund Nadolski do { 97689a7301fSDan Williams status = sci_controller_start_next_phy(ihost); 977bb3dbdf6SEdmund Nadolski } while (status != SCI_SUCCESS); 978bb3dbdf6SEdmund Nadolski 979bb3dbdf6SEdmund Nadolski done: 980bb3dbdf6SEdmund Nadolski spin_unlock_irqrestore(&ihost->scic_lock, flags); 981cc9203bfSDan Williams } 982cc9203bfSDan Williams 983ac668c69SDan Williams static u16 isci_tci_active(struct isci_host *ihost) 984ac668c69SDan Williams { 985ac668c69SDan Williams return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS); 986ac668c69SDan Williams } 987ac668c69SDan Williams 98889a7301fSDan Williams static enum sci_status sci_controller_start(struct isci_host *ihost, 989cc9203bfSDan Williams u32 timeout) 990cc9203bfSDan Williams { 991cc9203bfSDan Williams enum sci_status result; 992cc9203bfSDan Williams u16 index; 993cc9203bfSDan Williams 994d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_INITIALIZED) { 99514e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 99614e99b4aSDan Williams __func__, ihost->sm.current_state_id); 997cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 998cc9203bfSDan Williams } 999cc9203bfSDan Williams 1000cc9203bfSDan Williams /* Build the TCi free pool */ 1001ac668c69SDan Williams BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8); 1002ac668c69SDan Williams ihost->tci_head = 0; 1003ac668c69SDan Williams ihost->tci_tail = 0; 1004d9dcb4baSDan Williams for (index = 0; index < ihost->task_context_entries; index++) 1005ac668c69SDan Williams isci_tci_free(ihost, index); 1006cc9203bfSDan Williams 1007cc9203bfSDan Williams /* Build the RNi free pool */ 100889a7301fSDan Williams sci_remote_node_table_initialize(&ihost->available_remote_nodes, 1009d9dcb4baSDan Williams ihost->remote_node_entries); 1010cc9203bfSDan Williams 1011cc9203bfSDan Williams /* 1012cc9203bfSDan Williams * Before anything else lets make sure we will not be 1013cc9203bfSDan Williams * interrupted by the hardware. 1014cc9203bfSDan Williams */ 101589a7301fSDan Williams sci_controller_disable_interrupts(ihost); 1016cc9203bfSDan Williams 1017cc9203bfSDan Williams /* Enable the port task scheduler */ 101889a7301fSDan Williams sci_controller_enable_port_task_scheduler(ihost); 1019cc9203bfSDan Williams 1020d9dcb4baSDan Williams /* Assign all the task entries to ihost physical function */ 102189a7301fSDan Williams sci_controller_assign_task_entries(ihost); 1022cc9203bfSDan Williams 1023cc9203bfSDan Williams /* Now initialize the completion queue */ 102489a7301fSDan Williams sci_controller_initialize_completion_queue(ihost); 1025cc9203bfSDan Williams 1026cc9203bfSDan Williams /* Initialize the unsolicited frame queue for use */ 102789a7301fSDan Williams sci_controller_initialize_unsolicited_frame_queue(ihost); 1028cc9203bfSDan Williams 1029cc9203bfSDan Williams /* Start all of the ports on this controller */ 1030d9dcb4baSDan Williams for (index = 0; index < ihost->logical_port_entries; index++) { 1031ffe191c9SDan Williams struct isci_port *iport = &ihost->ports[index]; 1032cc9203bfSDan Williams 103389a7301fSDan Williams result = sci_port_start(iport); 1034cc9203bfSDan Williams if (result) 1035cc9203bfSDan Williams return result; 1036cc9203bfSDan Williams } 1037cc9203bfSDan Williams 103889a7301fSDan Williams sci_controller_start_next_phy(ihost); 1039cc9203bfSDan Williams 1040d9dcb4baSDan Williams sci_mod_timer(&ihost->timer, timeout); 1041cc9203bfSDan Williams 1042d9dcb4baSDan Williams sci_change_state(&ihost->sm, SCIC_STARTING); 1043cc9203bfSDan Williams 1044cc9203bfSDan Williams return SCI_SUCCESS; 1045cc9203bfSDan Williams } 1046cc9203bfSDan Williams 10476f231ddaSDan Williams void isci_host_scan_start(struct Scsi_Host *shost) 10486f231ddaSDan Williams { 10494393aa4eSDan Williams struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha; 105089a7301fSDan Williams unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost); 10516f231ddaSDan Williams 10520cf89d1dSDan Williams set_bit(IHOST_START_PENDING, &ihost->flags); 105377950f51SEdmund Nadolski 105477950f51SEdmund Nadolski spin_lock_irq(&ihost->scic_lock); 105589a7301fSDan Williams sci_controller_start(ihost, tmo); 105689a7301fSDan Williams sci_controller_enable_interrupts(ihost); 105777950f51SEdmund Nadolski spin_unlock_irq(&ihost->scic_lock); 10586f231ddaSDan Williams } 10596f231ddaSDan Williams 1060eb608c3cSDan Williams static void isci_host_stop_complete(struct isci_host *ihost) 10616f231ddaSDan Williams { 106289a7301fSDan Williams sci_controller_disable_interrupts(ihost); 10630cf89d1dSDan Williams clear_bit(IHOST_STOP_PENDING, &ihost->flags); 10640cf89d1dSDan Williams wake_up(&ihost->eventq); 10656f231ddaSDan Williams } 10666f231ddaSDan Williams 106789a7301fSDan Williams static void sci_controller_completion_handler(struct isci_host *ihost) 1068cc9203bfSDan Williams { 1069cc9203bfSDan Williams /* Empty out the completion queue */ 107089a7301fSDan Williams if (sci_controller_completion_queue_has_entries(ihost)) 107189a7301fSDan Williams sci_controller_process_completions(ihost); 1072cc9203bfSDan Williams 1073cc9203bfSDan Williams /* Clear the interrupt and enable all interrupts again */ 1074d9dcb4baSDan Williams writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status); 1075cc9203bfSDan Williams /* Could we write the value of SMU_ISR_COMPLETION? */ 1076d9dcb4baSDan Williams writel(0xFF000000, &ihost->smu_registers->interrupt_mask); 1077d9dcb4baSDan Williams writel(0, &ihost->smu_registers->interrupt_mask); 1078cc9203bfSDan Williams } 1079cc9203bfSDan Williams 10806f231ddaSDan Williams /** 10816f231ddaSDan Williams * isci_host_completion_routine() - This function is the delayed service 10826f231ddaSDan Williams * routine that calls the sci core library's completion handler. It's 10836f231ddaSDan Williams * scheduled as a tasklet from the interrupt service routine when interrupts 10846f231ddaSDan Williams * in use, or set as the timeout function in polled mode. 10856f231ddaSDan Williams * @data: This parameter specifies the ISCI host object 10866f231ddaSDan Williams * 10876f231ddaSDan Williams */ 1088abec912dSDan Williams void isci_host_completion_routine(unsigned long data) 10896f231ddaSDan Williams { 1090d9dcb4baSDan Williams struct isci_host *ihost = (struct isci_host *)data; 10916f231ddaSDan Williams struct list_head completed_request_list; 10926f231ddaSDan Williams struct list_head *current_position; 10936f231ddaSDan Williams struct list_head *next_position; 10946f231ddaSDan Williams struct isci_request *request; 10956f231ddaSDan Williams struct sas_task *task; 10969b4be528SDan Williams u16 active; 10976f231ddaSDan Williams 10986f231ddaSDan Williams INIT_LIST_HEAD(&completed_request_list); 10996f231ddaSDan Williams 1100d9dcb4baSDan Williams spin_lock_irq(&ihost->scic_lock); 11016f231ddaSDan Williams 110289a7301fSDan Williams sci_controller_completion_handler(ihost); 1103c7ef4031SDan Williams 11046f231ddaSDan Williams /* Take the lists of completed I/Os from the host. */ 1105d9dcb4baSDan Williams list_splice_init(&ihost->requests_to_complete, 11066f231ddaSDan Williams &completed_request_list); 11076f231ddaSDan Williams 1108d9dcb4baSDan Williams spin_unlock_irq(&ihost->scic_lock); 11096f231ddaSDan Williams 1110*14aaa9f0SJeff Skirvin /* Process any completions in the list. */ 11116f231ddaSDan Williams list_for_each_safe(current_position, next_position, 11126f231ddaSDan Williams &completed_request_list) { 11136f231ddaSDan Williams 11146f231ddaSDan Williams request = list_entry(current_position, struct isci_request, 11156f231ddaSDan Williams completed_node); 11166f231ddaSDan Williams task = isci_request_access_task(request); 11176f231ddaSDan Williams 11186f231ddaSDan Williams 111911b00c19SJeff Skirvin /* Return the task to libsas */ 112011b00c19SJeff Skirvin if (task != NULL) { 11216f231ddaSDan Williams 112211b00c19SJeff Skirvin task->lldd_task = NULL; 1123*14aaa9f0SJeff Skirvin if (!test_bit(IREQ_ABORT_PATH_ACTIVE, &request->flags) && 1124*14aaa9f0SJeff Skirvin !(task->task_state_flags & SAS_TASK_STATE_ABORTED)) { 1125*14aaa9f0SJeff Skirvin if (test_bit(IREQ_COMPLETE_IN_TARGET, 1126*14aaa9f0SJeff Skirvin &request->flags)) { 112711b00c19SJeff Skirvin 1128*14aaa9f0SJeff Skirvin /* Normal notification (task_done) */ 1129*14aaa9f0SJeff Skirvin dev_dbg(&ihost->pdev->dev, "%s: Normal" 1130*14aaa9f0SJeff Skirvin " - request/task = %p/%p\n", 1131*14aaa9f0SJeff Skirvin __func__, request, task); 1132*14aaa9f0SJeff Skirvin 113311b00c19SJeff Skirvin task->task_done(task); 113411b00c19SJeff Skirvin } else { 1135*14aaa9f0SJeff Skirvin dev_warn(&ihost->pdev->dev, 1136*14aaa9f0SJeff Skirvin "%s: Error - request/task" 1137*14aaa9f0SJeff Skirvin " = %p/%p\n", 1138*14aaa9f0SJeff Skirvin __func__, request, task); 1139*14aaa9f0SJeff Skirvin 1140*14aaa9f0SJeff Skirvin sas_task_abort(task); 1141*14aaa9f0SJeff Skirvin } 1142*14aaa9f0SJeff Skirvin } 1143*14aaa9f0SJeff Skirvin } 114411b00c19SJeff Skirvin 1145d9dcb4baSDan Williams spin_lock_irq(&ihost->scic_lock); 1146d9dcb4baSDan Williams isci_free_tag(ihost, request->io_tag); 1147d9dcb4baSDan Williams spin_unlock_irq(&ihost->scic_lock); 114811b00c19SJeff Skirvin } 11496f231ddaSDan Williams 11509b4be528SDan Williams /* the coalesence timeout doubles at each encoding step, so 11519b4be528SDan Williams * update it based on the ilog2 value of the outstanding requests 11529b4be528SDan Williams */ 11539b4be528SDan Williams active = isci_tci_active(ihost); 11549b4be528SDan Williams writel(SMU_ICC_GEN_VAL(NUMBER, active) | 11559b4be528SDan Williams SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)), 11569b4be528SDan Williams &ihost->smu_registers->interrupt_coalesce_control); 11576f231ddaSDan Williams } 11586f231ddaSDan Williams 1159cc9203bfSDan Williams /** 116089a7301fSDan Williams * sci_controller_stop() - This method will stop an individual controller 1161cc9203bfSDan Williams * object.This method will invoke the associated user callback upon 1162cc9203bfSDan Williams * completion. The completion callback is called when the following 1163cc9203bfSDan Williams * conditions are met: -# the method return status is SCI_SUCCESS. -# the 1164cc9203bfSDan Williams * controller has been quiesced. This method will ensure that all IO 1165cc9203bfSDan Williams * requests are quiesced, phys are stopped, and all additional operation by 1166cc9203bfSDan Williams * the hardware is halted. 1167cc9203bfSDan Williams * @controller: the handle to the controller object to stop. 1168cc9203bfSDan Williams * @timeout: This parameter specifies the number of milliseconds in which the 1169cc9203bfSDan Williams * stop operation should complete. 1170cc9203bfSDan Williams * 1171cc9203bfSDan Williams * The controller must be in the STARTED or STOPPED state. Indicate if the 1172cc9203bfSDan Williams * controller stop method succeeded or failed in some way. SCI_SUCCESS if the 1173cc9203bfSDan Williams * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the 1174cc9203bfSDan Williams * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the 1175cc9203bfSDan Williams * controller is not either in the STARTED or STOPPED states. 1176cc9203bfSDan Williams */ 117789a7301fSDan Williams static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout) 1178cc9203bfSDan Williams { 1179d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_READY) { 118014e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 118114e99b4aSDan Williams __func__, ihost->sm.current_state_id); 1182cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 1183cc9203bfSDan Williams } 1184cc9203bfSDan Williams 1185d9dcb4baSDan Williams sci_mod_timer(&ihost->timer, timeout); 1186d9dcb4baSDan Williams sci_change_state(&ihost->sm, SCIC_STOPPING); 1187cc9203bfSDan Williams return SCI_SUCCESS; 1188cc9203bfSDan Williams } 1189cc9203bfSDan Williams 1190cc9203bfSDan Williams /** 119189a7301fSDan Williams * sci_controller_reset() - This method will reset the supplied core 1192cc9203bfSDan Williams * controller regardless of the state of said controller. This operation is 1193cc9203bfSDan Williams * considered destructive. In other words, all current operations are wiped 1194cc9203bfSDan Williams * out. No IO completions for outstanding devices occur. Outstanding IO 1195cc9203bfSDan Williams * requests are not aborted or completed at the actual remote device. 1196cc9203bfSDan Williams * @controller: the handle to the controller object to reset. 1197cc9203bfSDan Williams * 1198cc9203bfSDan Williams * Indicate if the controller reset method succeeded or failed in some way. 1199cc9203bfSDan Williams * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if 1200cc9203bfSDan Williams * the controller reset operation is unable to complete. 1201cc9203bfSDan Williams */ 120289a7301fSDan Williams static enum sci_status sci_controller_reset(struct isci_host *ihost) 1203cc9203bfSDan Williams { 1204d9dcb4baSDan Williams switch (ihost->sm.current_state_id) { 1205e301370aSEdmund Nadolski case SCIC_RESET: 1206e301370aSEdmund Nadolski case SCIC_READY: 1207eb608c3cSDan Williams case SCIC_STOPPING: 1208e301370aSEdmund Nadolski case SCIC_FAILED: 1209cc9203bfSDan Williams /* 1210cc9203bfSDan Williams * The reset operation is not a graceful cleanup, just 1211cc9203bfSDan Williams * perform the state transition. 1212cc9203bfSDan Williams */ 1213d9dcb4baSDan Williams sci_change_state(&ihost->sm, SCIC_RESETTING); 1214cc9203bfSDan Williams return SCI_SUCCESS; 1215cc9203bfSDan Williams default: 121614e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 121714e99b4aSDan Williams __func__, ihost->sm.current_state_id); 1218cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 1219cc9203bfSDan Williams } 1220cc9203bfSDan Williams } 1221cc9203bfSDan Williams 1222eb608c3cSDan Williams static enum sci_status sci_controller_stop_phys(struct isci_host *ihost) 1223eb608c3cSDan Williams { 1224eb608c3cSDan Williams u32 index; 1225eb608c3cSDan Williams enum sci_status status; 1226eb608c3cSDan Williams enum sci_status phy_status; 1227eb608c3cSDan Williams 1228eb608c3cSDan Williams status = SCI_SUCCESS; 1229eb608c3cSDan Williams 1230eb608c3cSDan Williams for (index = 0; index < SCI_MAX_PHYS; index++) { 1231eb608c3cSDan Williams phy_status = sci_phy_stop(&ihost->phys[index]); 1232eb608c3cSDan Williams 1233eb608c3cSDan Williams if (phy_status != SCI_SUCCESS && 1234eb608c3cSDan Williams phy_status != SCI_FAILURE_INVALID_STATE) { 1235eb608c3cSDan Williams status = SCI_FAILURE; 1236eb608c3cSDan Williams 1237eb608c3cSDan Williams dev_warn(&ihost->pdev->dev, 1238eb608c3cSDan Williams "%s: Controller stop operation failed to stop " 1239eb608c3cSDan Williams "phy %d because of status %d.\n", 1240eb608c3cSDan Williams __func__, 1241eb608c3cSDan Williams ihost->phys[index].phy_index, phy_status); 1242eb608c3cSDan Williams } 1243eb608c3cSDan Williams } 1244eb608c3cSDan Williams 1245eb608c3cSDan Williams return status; 1246eb608c3cSDan Williams } 1247eb608c3cSDan Williams 1248eb608c3cSDan Williams 1249eb608c3cSDan Williams /** 1250eb608c3cSDan Williams * isci_host_deinit - shutdown frame reception and dma 1251eb608c3cSDan Williams * @ihost: host to take down 1252eb608c3cSDan Williams * 1253eb608c3cSDan Williams * This is called in either the driver shutdown or the suspend path. In 1254eb608c3cSDan Williams * the shutdown case libsas went through port teardown and normal device 1255eb608c3cSDan Williams * removal (i.e. physical links stayed up to service scsi_device removal 1256eb608c3cSDan Williams * commands). In the suspend case we disable the hardware without 1257eb608c3cSDan Williams * notifying libsas of the link down events since we want libsas to 1258eb608c3cSDan Williams * remember the domain across the suspend/resume cycle 1259eb608c3cSDan Williams */ 12600cf89d1dSDan Williams void isci_host_deinit(struct isci_host *ihost) 12616f231ddaSDan Williams { 12626f231ddaSDan Williams int i; 12636f231ddaSDan Williams 1264ad4f4c1dSDan Williams /* disable output data selects */ 1265ad4f4c1dSDan Williams for (i = 0; i < isci_gpio_count(ihost); i++) 1266ad4f4c1dSDan Williams writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]); 1267ad4f4c1dSDan Williams 12680cf89d1dSDan Williams set_bit(IHOST_STOP_PENDING, &ihost->flags); 12697c40a803SDan Williams 12707c40a803SDan Williams spin_lock_irq(&ihost->scic_lock); 127189a7301fSDan Williams sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT); 12727c40a803SDan Williams spin_unlock_irq(&ihost->scic_lock); 12737c40a803SDan Williams 12740cf89d1dSDan Williams wait_for_stop(ihost); 1275ad4f4c1dSDan Williams 1276eb608c3cSDan Williams /* phy stop is after controller stop to allow port and device to 1277eb608c3cSDan Williams * go idle before shutting down the phys, but the expectation is 1278eb608c3cSDan Williams * that i/o has been shut off well before we reach this 1279eb608c3cSDan Williams * function. 1280eb608c3cSDan Williams */ 1281eb608c3cSDan Williams sci_controller_stop_phys(ihost); 1282eb608c3cSDan Williams 1283ad4f4c1dSDan Williams /* disable sgpio: where the above wait should give time for the 1284ad4f4c1dSDan Williams * enclosure to sample the gpios going inactive 1285ad4f4c1dSDan Williams */ 1286ad4f4c1dSDan Williams writel(0, &ihost->scu_registers->peg0.sgpio.interface_control); 1287ad4f4c1dSDan Williams 12882396a265SDan Williams spin_lock_irq(&ihost->scic_lock); 128989a7301fSDan Williams sci_controller_reset(ihost); 12902396a265SDan Williams spin_unlock_irq(&ihost->scic_lock); 12915553ba2bSEdmund Nadolski 12925553ba2bSEdmund Nadolski /* Cancel any/all outstanding port timers */ 1293d9dcb4baSDan Williams for (i = 0; i < ihost->logical_port_entries; i++) { 1294ffe191c9SDan Williams struct isci_port *iport = &ihost->ports[i]; 1295ffe191c9SDan Williams del_timer_sync(&iport->timer.timer); 12965553ba2bSEdmund Nadolski } 12975553ba2bSEdmund Nadolski 1298a628d478SEdmund Nadolski /* Cancel any/all outstanding phy timers */ 1299a628d478SEdmund Nadolski for (i = 0; i < SCI_MAX_PHYS; i++) { 130085280955SDan Williams struct isci_phy *iphy = &ihost->phys[i]; 130185280955SDan Williams del_timer_sync(&iphy->sata_timer.timer); 1302a628d478SEdmund Nadolski } 1303a628d478SEdmund Nadolski 1304d9dcb4baSDan Williams del_timer_sync(&ihost->port_agent.timer.timer); 1305ac0eeb4fSEdmund Nadolski 1306d9dcb4baSDan Williams del_timer_sync(&ihost->power_control.timer.timer); 13070473661aSEdmund Nadolski 1308d9dcb4baSDan Williams del_timer_sync(&ihost->timer.timer); 13096cb5853dSEdmund Nadolski 1310d9dcb4baSDan Williams del_timer_sync(&ihost->phy_timer.timer); 13116f231ddaSDan Williams } 13126f231ddaSDan Williams 13136f231ddaSDan Williams static void __iomem *scu_base(struct isci_host *isci_host) 13146f231ddaSDan Williams { 13156f231ddaSDan Williams struct pci_dev *pdev = isci_host->pdev; 13166f231ddaSDan Williams int id = isci_host->id; 13176f231ddaSDan Williams 13186f231ddaSDan Williams return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id; 13196f231ddaSDan Williams } 13206f231ddaSDan Williams 13216f231ddaSDan Williams static void __iomem *smu_base(struct isci_host *isci_host) 13226f231ddaSDan Williams { 13236f231ddaSDan Williams struct pci_dev *pdev = isci_host->pdev; 13246f231ddaSDan Williams int id = isci_host->id; 13256f231ddaSDan Williams 13266f231ddaSDan Williams return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id; 13276f231ddaSDan Williams } 13286f231ddaSDan Williams 132989a7301fSDan Williams static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm) 1330cc9203bfSDan Williams { 1331d9dcb4baSDan Williams struct isci_host *ihost = container_of(sm, typeof(*ihost), sm); 1332cc9203bfSDan Williams 1333d9dcb4baSDan Williams sci_change_state(&ihost->sm, SCIC_RESET); 1334cc9203bfSDan Williams } 1335cc9203bfSDan Williams 133689a7301fSDan Williams static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm) 1337cc9203bfSDan Williams { 1338d9dcb4baSDan Williams struct isci_host *ihost = container_of(sm, typeof(*ihost), sm); 1339cc9203bfSDan Williams 1340d9dcb4baSDan Williams sci_del_timer(&ihost->timer); 1341cc9203bfSDan Williams } 1342cc9203bfSDan Williams 1343cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853 1344cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280 1345cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000 1346cc9203bfSDan Williams #define INTERRUPT_COALESCE_NUMBER_MAX 256 1347cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7 1348cc9203bfSDan Williams #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28 1349cc9203bfSDan Williams 1350cc9203bfSDan Williams /** 135189a7301fSDan Williams * sci_controller_set_interrupt_coalescence() - This method allows the user to 1352cc9203bfSDan Williams * configure the interrupt coalescence. 1353cc9203bfSDan Williams * @controller: This parameter represents the handle to the controller object 1354cc9203bfSDan Williams * for which its interrupt coalesce register is overridden. 1355cc9203bfSDan Williams * @coalesce_number: Used to control the number of entries in the Completion 1356cc9203bfSDan Williams * Queue before an interrupt is generated. If the number of entries exceed 1357cc9203bfSDan Williams * this number, an interrupt will be generated. The valid range of the input 1358cc9203bfSDan Williams * is [0, 256]. A setting of 0 results in coalescing being disabled. 1359cc9203bfSDan Williams * @coalesce_timeout: Timeout value in microseconds. The valid range of the 1360cc9203bfSDan Williams * input is [0, 2700000] . A setting of 0 is allowed and results in no 1361cc9203bfSDan Williams * interrupt coalescing timeout. 1362cc9203bfSDan Williams * 1363cc9203bfSDan Williams * Indicate if the user successfully set the interrupt coalesce parameters. 1364cc9203bfSDan Williams * SCI_SUCCESS The user successfully updated the interrutp coalescence. 1365cc9203bfSDan Williams * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range. 1366cc9203bfSDan Williams */ 1367d9dcb4baSDan Williams static enum sci_status 136889a7301fSDan Williams sci_controller_set_interrupt_coalescence(struct isci_host *ihost, 1369cc9203bfSDan Williams u32 coalesce_number, 1370cc9203bfSDan Williams u32 coalesce_timeout) 1371cc9203bfSDan Williams { 1372cc9203bfSDan Williams u8 timeout_encode = 0; 1373cc9203bfSDan Williams u32 min = 0; 1374cc9203bfSDan Williams u32 max = 0; 1375cc9203bfSDan Williams 1376cc9203bfSDan Williams /* Check if the input parameters fall in the range. */ 1377cc9203bfSDan Williams if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX) 1378cc9203bfSDan Williams return SCI_FAILURE_INVALID_PARAMETER_VALUE; 1379cc9203bfSDan Williams 1380cc9203bfSDan Williams /* 1381cc9203bfSDan Williams * Defined encoding for interrupt coalescing timeout: 1382cc9203bfSDan Williams * Value Min Max Units 1383cc9203bfSDan Williams * ----- --- --- ----- 1384cc9203bfSDan Williams * 0 - - Disabled 1385cc9203bfSDan Williams * 1 13.3 20.0 ns 1386cc9203bfSDan Williams * 2 26.7 40.0 1387cc9203bfSDan Williams * 3 53.3 80.0 1388cc9203bfSDan Williams * 4 106.7 160.0 1389cc9203bfSDan Williams * 5 213.3 320.0 1390cc9203bfSDan Williams * 6 426.7 640.0 1391cc9203bfSDan Williams * 7 853.3 1280.0 1392cc9203bfSDan Williams * 8 1.7 2.6 us 1393cc9203bfSDan Williams * 9 3.4 5.1 1394cc9203bfSDan Williams * 10 6.8 10.2 1395cc9203bfSDan Williams * 11 13.7 20.5 1396cc9203bfSDan Williams * 12 27.3 41.0 1397cc9203bfSDan Williams * 13 54.6 81.9 1398cc9203bfSDan Williams * 14 109.2 163.8 1399cc9203bfSDan Williams * 15 218.5 327.7 1400cc9203bfSDan Williams * 16 436.9 655.4 1401cc9203bfSDan Williams * 17 873.8 1310.7 1402cc9203bfSDan Williams * 18 1.7 2.6 ms 1403cc9203bfSDan Williams * 19 3.5 5.2 1404cc9203bfSDan Williams * 20 7.0 10.5 1405cc9203bfSDan Williams * 21 14.0 21.0 1406cc9203bfSDan Williams * 22 28.0 41.9 1407cc9203bfSDan Williams * 23 55.9 83.9 1408cc9203bfSDan Williams * 24 111.8 167.8 1409cc9203bfSDan Williams * 25 223.7 335.5 1410cc9203bfSDan Williams * 26 447.4 671.1 1411cc9203bfSDan Williams * 27 894.8 1342.2 1412cc9203bfSDan Williams * 28 1.8 2.7 s 1413cc9203bfSDan Williams * Others Undefined */ 1414cc9203bfSDan Williams 1415cc9203bfSDan Williams /* 1416cc9203bfSDan Williams * Use the table above to decide the encode of interrupt coalescing timeout 1417cc9203bfSDan Williams * value for register writing. */ 1418cc9203bfSDan Williams if (coalesce_timeout == 0) 1419cc9203bfSDan Williams timeout_encode = 0; 1420cc9203bfSDan Williams else{ 1421cc9203bfSDan Williams /* make the timeout value in unit of (10 ns). */ 1422cc9203bfSDan Williams coalesce_timeout = coalesce_timeout * 100; 1423cc9203bfSDan Williams min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10; 1424cc9203bfSDan Williams max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10; 1425cc9203bfSDan Williams 1426cc9203bfSDan Williams /* get the encode of timeout for register writing. */ 1427cc9203bfSDan Williams for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN; 1428cc9203bfSDan Williams timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX; 1429cc9203bfSDan Williams timeout_encode++) { 1430cc9203bfSDan Williams if (min <= coalesce_timeout && max > coalesce_timeout) 1431cc9203bfSDan Williams break; 1432cc9203bfSDan Williams else if (coalesce_timeout >= max && coalesce_timeout < min * 2 1433cc9203bfSDan Williams && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) { 1434cc9203bfSDan Williams if ((coalesce_timeout - max) < (2 * min - coalesce_timeout)) 1435cc9203bfSDan Williams break; 1436cc9203bfSDan Williams else{ 1437cc9203bfSDan Williams timeout_encode++; 1438cc9203bfSDan Williams break; 1439cc9203bfSDan Williams } 1440cc9203bfSDan Williams } else { 1441cc9203bfSDan Williams max = max * 2; 1442cc9203bfSDan Williams min = min * 2; 1443cc9203bfSDan Williams } 1444cc9203bfSDan Williams } 1445cc9203bfSDan Williams 1446cc9203bfSDan Williams if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1) 1447cc9203bfSDan Williams /* the value is out of range. */ 1448cc9203bfSDan Williams return SCI_FAILURE_INVALID_PARAMETER_VALUE; 1449cc9203bfSDan Williams } 1450cc9203bfSDan Williams 1451cc9203bfSDan Williams writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) | 1452cc9203bfSDan Williams SMU_ICC_GEN_VAL(TIMER, timeout_encode), 1453d9dcb4baSDan Williams &ihost->smu_registers->interrupt_coalesce_control); 1454cc9203bfSDan Williams 1455cc9203bfSDan Williams 1456d9dcb4baSDan Williams ihost->interrupt_coalesce_number = (u16)coalesce_number; 1457d9dcb4baSDan Williams ihost->interrupt_coalesce_timeout = coalesce_timeout / 100; 1458cc9203bfSDan Williams 1459cc9203bfSDan Williams return SCI_SUCCESS; 1460cc9203bfSDan Williams } 1461cc9203bfSDan Williams 1462cc9203bfSDan Williams 146389a7301fSDan Williams static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm) 1464cc9203bfSDan Williams { 1465d9dcb4baSDan Williams struct isci_host *ihost = container_of(sm, typeof(*ihost), sm); 1466e5cc6aa4SMarcin Tomczak u32 val; 1467e5cc6aa4SMarcin Tomczak 1468e5cc6aa4SMarcin Tomczak /* enable clock gating for power control of the scu unit */ 1469e5cc6aa4SMarcin Tomczak val = readl(&ihost->smu_registers->clock_gating_control); 1470e5cc6aa4SMarcin Tomczak val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) | 1471e5cc6aa4SMarcin Tomczak SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) | 1472e5cc6aa4SMarcin Tomczak SMU_CGUCR_GEN_BIT(XCLK_ENABLE)); 1473e5cc6aa4SMarcin Tomczak val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE); 1474e5cc6aa4SMarcin Tomczak writel(val, &ihost->smu_registers->clock_gating_control); 1475cc9203bfSDan Williams 1476cc9203bfSDan Williams /* set the default interrupt coalescence number and timeout value. */ 14779b4be528SDan Williams sci_controller_set_interrupt_coalescence(ihost, 0, 0); 1478cc9203bfSDan Williams } 1479cc9203bfSDan Williams 148089a7301fSDan Williams static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm) 1481cc9203bfSDan Williams { 1482d9dcb4baSDan Williams struct isci_host *ihost = container_of(sm, typeof(*ihost), sm); 1483cc9203bfSDan Williams 1484cc9203bfSDan Williams /* disable interrupt coalescence. */ 148589a7301fSDan Williams sci_controller_set_interrupt_coalescence(ihost, 0, 0); 1486cc9203bfSDan Williams } 1487cc9203bfSDan Williams 148889a7301fSDan Williams static enum sci_status sci_controller_stop_ports(struct isci_host *ihost) 1489cc9203bfSDan Williams { 1490cc9203bfSDan Williams u32 index; 1491cc9203bfSDan Williams enum sci_status port_status; 1492cc9203bfSDan Williams enum sci_status status = SCI_SUCCESS; 1493cc9203bfSDan Williams 1494d9dcb4baSDan Williams for (index = 0; index < ihost->logical_port_entries; index++) { 1495ffe191c9SDan Williams struct isci_port *iport = &ihost->ports[index]; 1496cc9203bfSDan Williams 149789a7301fSDan Williams port_status = sci_port_stop(iport); 1498cc9203bfSDan Williams 1499cc9203bfSDan Williams if ((port_status != SCI_SUCCESS) && 1500cc9203bfSDan Williams (port_status != SCI_FAILURE_INVALID_STATE)) { 1501cc9203bfSDan Williams status = SCI_FAILURE; 1502cc9203bfSDan Williams 1503d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 1504cc9203bfSDan Williams "%s: Controller stop operation failed to " 1505cc9203bfSDan Williams "stop port %d because of status %d.\n", 1506cc9203bfSDan Williams __func__, 1507ffe191c9SDan Williams iport->logical_port_index, 1508cc9203bfSDan Williams port_status); 1509cc9203bfSDan Williams } 1510cc9203bfSDan Williams } 1511cc9203bfSDan Williams 1512cc9203bfSDan Williams return status; 1513cc9203bfSDan Williams } 1514cc9203bfSDan Williams 151589a7301fSDan Williams static enum sci_status sci_controller_stop_devices(struct isci_host *ihost) 1516cc9203bfSDan Williams { 1517cc9203bfSDan Williams u32 index; 1518cc9203bfSDan Williams enum sci_status status; 1519cc9203bfSDan Williams enum sci_status device_status; 1520cc9203bfSDan Williams 1521cc9203bfSDan Williams status = SCI_SUCCESS; 1522cc9203bfSDan Williams 1523d9dcb4baSDan Williams for (index = 0; index < ihost->remote_node_entries; index++) { 1524d9dcb4baSDan Williams if (ihost->device_table[index] != NULL) { 1525cc9203bfSDan Williams /* / @todo What timeout value do we want to provide to this request? */ 152689a7301fSDan Williams device_status = sci_remote_device_stop(ihost->device_table[index], 0); 1527cc9203bfSDan Williams 1528cc9203bfSDan Williams if ((device_status != SCI_SUCCESS) && 1529cc9203bfSDan Williams (device_status != SCI_FAILURE_INVALID_STATE)) { 1530d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 1531cc9203bfSDan Williams "%s: Controller stop operation failed " 1532cc9203bfSDan Williams "to stop device 0x%p because of " 1533cc9203bfSDan Williams "status %d.\n", 1534cc9203bfSDan Williams __func__, 1535d9dcb4baSDan Williams ihost->device_table[index], device_status); 1536cc9203bfSDan Williams } 1537cc9203bfSDan Williams } 1538cc9203bfSDan Williams } 1539cc9203bfSDan Williams 1540cc9203bfSDan Williams return status; 1541cc9203bfSDan Williams } 1542cc9203bfSDan Williams 154389a7301fSDan Williams static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm) 1544cc9203bfSDan Williams { 1545d9dcb4baSDan Williams struct isci_host *ihost = container_of(sm, typeof(*ihost), sm); 1546cc9203bfSDan Williams 154789a7301fSDan Williams sci_controller_stop_devices(ihost); 1548eb608c3cSDan Williams sci_controller_stop_ports(ihost); 1549eb608c3cSDan Williams 1550eb608c3cSDan Williams if (!sci_controller_has_remote_devices_stopping(ihost)) 1551eb608c3cSDan Williams isci_host_stop_complete(ihost); 1552cc9203bfSDan Williams } 1553cc9203bfSDan Williams 155489a7301fSDan Williams static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm) 1555cc9203bfSDan Williams { 1556d9dcb4baSDan Williams struct isci_host *ihost = container_of(sm, typeof(*ihost), sm); 1557cc9203bfSDan Williams 1558d9dcb4baSDan Williams sci_del_timer(&ihost->timer); 1559cc9203bfSDan Williams } 1560cc9203bfSDan Williams 156189a7301fSDan Williams static void sci_controller_reset_hardware(struct isci_host *ihost) 1562cc9203bfSDan Williams { 1563cc9203bfSDan Williams /* Disable interrupts so we dont take any spurious interrupts */ 156489a7301fSDan Williams sci_controller_disable_interrupts(ihost); 1565cc9203bfSDan Williams 1566cc9203bfSDan Williams /* Reset the SCU */ 1567d9dcb4baSDan Williams writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control); 1568cc9203bfSDan Williams 1569cc9203bfSDan Williams /* Delay for 1ms to before clearing the CQP and UFQPR. */ 1570cc9203bfSDan Williams udelay(1000); 1571cc9203bfSDan Williams 1572cc9203bfSDan Williams /* The write to the CQGR clears the CQP */ 1573d9dcb4baSDan Williams writel(0x00000000, &ihost->smu_registers->completion_queue_get); 1574cc9203bfSDan Williams 1575cc9203bfSDan Williams /* The write to the UFQGP clears the UFQPR */ 1576d9dcb4baSDan Williams writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer); 15772396a265SDan Williams 15782396a265SDan Williams /* clear all interrupts */ 15792396a265SDan Williams writel(~SMU_INTERRUPT_STATUS_RESERVED_MASK, &ihost->smu_registers->interrupt_status); 1580cc9203bfSDan Williams } 1581cc9203bfSDan Williams 158289a7301fSDan Williams static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm) 1583cc9203bfSDan Williams { 1584d9dcb4baSDan Williams struct isci_host *ihost = container_of(sm, typeof(*ihost), sm); 1585cc9203bfSDan Williams 158689a7301fSDan Williams sci_controller_reset_hardware(ihost); 1587d9dcb4baSDan Williams sci_change_state(&ihost->sm, SCIC_RESET); 1588cc9203bfSDan Williams } 1589cc9203bfSDan Williams 159089a7301fSDan Williams static const struct sci_base_state sci_controller_state_table[] = { 1591e301370aSEdmund Nadolski [SCIC_INITIAL] = { 159289a7301fSDan Williams .enter_state = sci_controller_initial_state_enter, 1593cc9203bfSDan Williams }, 1594e301370aSEdmund Nadolski [SCIC_RESET] = {}, 1595e301370aSEdmund Nadolski [SCIC_INITIALIZING] = {}, 1596e301370aSEdmund Nadolski [SCIC_INITIALIZED] = {}, 1597e301370aSEdmund Nadolski [SCIC_STARTING] = { 159889a7301fSDan Williams .exit_state = sci_controller_starting_state_exit, 1599cc9203bfSDan Williams }, 1600e301370aSEdmund Nadolski [SCIC_READY] = { 160189a7301fSDan Williams .enter_state = sci_controller_ready_state_enter, 160289a7301fSDan Williams .exit_state = sci_controller_ready_state_exit, 1603cc9203bfSDan Williams }, 1604e301370aSEdmund Nadolski [SCIC_RESETTING] = { 160589a7301fSDan Williams .enter_state = sci_controller_resetting_state_enter, 1606cc9203bfSDan Williams }, 1607e301370aSEdmund Nadolski [SCIC_STOPPING] = { 160889a7301fSDan Williams .enter_state = sci_controller_stopping_state_enter, 160989a7301fSDan Williams .exit_state = sci_controller_stopping_state_exit, 1610cc9203bfSDan Williams }, 1611e301370aSEdmund Nadolski [SCIC_FAILED] = {} 1612cc9203bfSDan Williams }; 1613cc9203bfSDan Williams 16146cb5853dSEdmund Nadolski static void controller_timeout(unsigned long data) 16156cb5853dSEdmund Nadolski { 16166cb5853dSEdmund Nadolski struct sci_timer *tmr = (struct sci_timer *)data; 1617d9dcb4baSDan Williams struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer); 1618d9dcb4baSDan Williams struct sci_base_state_machine *sm = &ihost->sm; 16196cb5853dSEdmund Nadolski unsigned long flags; 1620cc9203bfSDan Williams 16216cb5853dSEdmund Nadolski spin_lock_irqsave(&ihost->scic_lock, flags); 16226cb5853dSEdmund Nadolski 16236cb5853dSEdmund Nadolski if (tmr->cancel) 16246cb5853dSEdmund Nadolski goto done; 16256cb5853dSEdmund Nadolski 1626e301370aSEdmund Nadolski if (sm->current_state_id == SCIC_STARTING) 162789a7301fSDan Williams sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT); 1628e301370aSEdmund Nadolski else if (sm->current_state_id == SCIC_STOPPING) { 1629e301370aSEdmund Nadolski sci_change_state(sm, SCIC_FAILED); 1630eb608c3cSDan Williams isci_host_stop_complete(ihost); 16316cb5853dSEdmund Nadolski } else /* / @todo Now what do we want to do in this case? */ 1632d9dcb4baSDan Williams dev_err(&ihost->pdev->dev, 16336cb5853dSEdmund Nadolski "%s: Controller timer fired when controller was not " 16346cb5853dSEdmund Nadolski "in a state being timed.\n", 16356cb5853dSEdmund Nadolski __func__); 16366cb5853dSEdmund Nadolski 16376cb5853dSEdmund Nadolski done: 16386cb5853dSEdmund Nadolski spin_unlock_irqrestore(&ihost->scic_lock, flags); 16396cb5853dSEdmund Nadolski } 1640cc9203bfSDan Williams 164189a7301fSDan Williams static enum sci_status sci_controller_construct(struct isci_host *ihost, 1642cc9203bfSDan Williams void __iomem *scu_base, 1643cc9203bfSDan Williams void __iomem *smu_base) 1644cc9203bfSDan Williams { 1645cc9203bfSDan Williams u8 i; 1646cc9203bfSDan Williams 164789a7301fSDan Williams sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL); 1648cc9203bfSDan Williams 1649d9dcb4baSDan Williams ihost->scu_registers = scu_base; 1650d9dcb4baSDan Williams ihost->smu_registers = smu_base; 1651cc9203bfSDan Williams 165289a7301fSDan Williams sci_port_configuration_agent_construct(&ihost->port_agent); 1653cc9203bfSDan Williams 1654cc9203bfSDan Williams /* Construct the ports for this controller */ 1655cc9203bfSDan Williams for (i = 0; i < SCI_MAX_PORTS; i++) 165689a7301fSDan Williams sci_port_construct(&ihost->ports[i], i, ihost); 165789a7301fSDan Williams sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost); 1658cc9203bfSDan Williams 1659cc9203bfSDan Williams /* Construct the phys for this controller */ 1660cc9203bfSDan Williams for (i = 0; i < SCI_MAX_PHYS; i++) { 1661cc9203bfSDan Williams /* Add all the PHYs to the dummy port */ 166289a7301fSDan Williams sci_phy_construct(&ihost->phys[i], 1663ffe191c9SDan Williams &ihost->ports[SCI_MAX_PORTS], i); 1664cc9203bfSDan Williams } 1665cc9203bfSDan Williams 1666d9dcb4baSDan Williams ihost->invalid_phy_mask = 0; 1667cc9203bfSDan Williams 1668d9dcb4baSDan Williams sci_init_timer(&ihost->timer, controller_timeout); 16696cb5853dSEdmund Nadolski 167089a7301fSDan Williams return sci_controller_reset(ihost); 1671cc9203bfSDan Williams } 1672cc9203bfSDan Williams 1673594e566aSDave Jiang int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version) 1674cc9203bfSDan Williams { 1675cc9203bfSDan Williams int i; 1676cc9203bfSDan Williams 1677cc9203bfSDan Williams for (i = 0; i < SCI_MAX_PORTS; i++) 1678cc9203bfSDan Williams if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX) 1679cc9203bfSDan Williams return -EINVAL; 1680cc9203bfSDan Williams 1681cc9203bfSDan Williams for (i = 0; i < SCI_MAX_PHYS; i++) 1682cc9203bfSDan Williams if (oem->phys[i].sas_address.high == 0 && 1683cc9203bfSDan Williams oem->phys[i].sas_address.low == 0) 1684cc9203bfSDan Williams return -EINVAL; 1685cc9203bfSDan Williams 1686cc9203bfSDan Williams if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) { 1687cc9203bfSDan Williams for (i = 0; i < SCI_MAX_PHYS; i++) 1688cc9203bfSDan Williams if (oem->ports[i].phy_mask != 0) 1689cc9203bfSDan Williams return -EINVAL; 1690cc9203bfSDan Williams } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) { 1691cc9203bfSDan Williams u8 phy_mask = 0; 1692cc9203bfSDan Williams 1693cc9203bfSDan Williams for (i = 0; i < SCI_MAX_PHYS; i++) 1694cc9203bfSDan Williams phy_mask |= oem->ports[i].phy_mask; 1695cc9203bfSDan Williams 1696cc9203bfSDan Williams if (phy_mask == 0) 1697cc9203bfSDan Williams return -EINVAL; 1698cc9203bfSDan Williams } else 1699cc9203bfSDan Williams return -EINVAL; 1700cc9203bfSDan Williams 17017000f7c7SAndrzej Jakowski if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT || 17027000f7c7SAndrzej Jakowski oem->controller.max_concurr_spin_up < 1) 1703cc9203bfSDan Williams return -EINVAL; 1704cc9203bfSDan Williams 1705594e566aSDave Jiang if (oem->controller.do_enable_ssc) { 1706594e566aSDave Jiang if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1) 1707594e566aSDave Jiang return -EINVAL; 1708594e566aSDave Jiang 1709594e566aSDave Jiang if (version >= ISCI_ROM_VER_1_1) { 1710594e566aSDave Jiang u8 test = oem->controller.ssc_sata_tx_spread_level; 1711594e566aSDave Jiang 1712594e566aSDave Jiang switch (test) { 1713594e566aSDave Jiang case 0: 1714594e566aSDave Jiang case 2: 1715594e566aSDave Jiang case 3: 1716594e566aSDave Jiang case 6: 1717594e566aSDave Jiang case 7: 1718594e566aSDave Jiang break; 1719594e566aSDave Jiang default: 1720594e566aSDave Jiang return -EINVAL; 1721594e566aSDave Jiang } 1722594e566aSDave Jiang 1723594e566aSDave Jiang test = oem->controller.ssc_sas_tx_spread_level; 1724594e566aSDave Jiang if (oem->controller.ssc_sas_tx_type == 0) { 1725594e566aSDave Jiang switch (test) { 1726594e566aSDave Jiang case 0: 1727594e566aSDave Jiang case 2: 1728594e566aSDave Jiang case 3: 1729594e566aSDave Jiang break; 1730594e566aSDave Jiang default: 1731594e566aSDave Jiang return -EINVAL; 1732594e566aSDave Jiang } 1733594e566aSDave Jiang } else if (oem->controller.ssc_sas_tx_type == 1) { 1734594e566aSDave Jiang switch (test) { 1735594e566aSDave Jiang case 0: 1736594e566aSDave Jiang case 3: 1737594e566aSDave Jiang case 6: 1738594e566aSDave Jiang break; 1739594e566aSDave Jiang default: 1740594e566aSDave Jiang return -EINVAL; 1741594e566aSDave Jiang } 1742594e566aSDave Jiang } 1743594e566aSDave Jiang } 1744594e566aSDave Jiang } 1745594e566aSDave Jiang 1746cc9203bfSDan Williams return 0; 1747cc9203bfSDan Williams } 1748cc9203bfSDan Williams 17497000f7c7SAndrzej Jakowski static u8 max_spin_up(struct isci_host *ihost) 17507000f7c7SAndrzej Jakowski { 17517000f7c7SAndrzej Jakowski if (ihost->user_parameters.max_concurr_spinup) 17527000f7c7SAndrzej Jakowski return min_t(u8, ihost->user_parameters.max_concurr_spinup, 17537000f7c7SAndrzej Jakowski MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT); 17547000f7c7SAndrzej Jakowski else 17557000f7c7SAndrzej Jakowski return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up, 17567000f7c7SAndrzej Jakowski MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT); 17577000f7c7SAndrzej Jakowski } 17587000f7c7SAndrzej Jakowski 17590473661aSEdmund Nadolski static void power_control_timeout(unsigned long data) 1760cc9203bfSDan Williams { 17610473661aSEdmund Nadolski struct sci_timer *tmr = (struct sci_timer *)data; 1762d9dcb4baSDan Williams struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer); 176385280955SDan Williams struct isci_phy *iphy; 17640473661aSEdmund Nadolski unsigned long flags; 17650473661aSEdmund Nadolski u8 i; 1766cc9203bfSDan Williams 17670473661aSEdmund Nadolski spin_lock_irqsave(&ihost->scic_lock, flags); 1768cc9203bfSDan Williams 17690473661aSEdmund Nadolski if (tmr->cancel) 17700473661aSEdmund Nadolski goto done; 1771cc9203bfSDan Williams 1772d9dcb4baSDan Williams ihost->power_control.phys_granted_power = 0; 1773cc9203bfSDan Williams 1774d9dcb4baSDan Williams if (ihost->power_control.phys_waiting == 0) { 1775d9dcb4baSDan Williams ihost->power_control.timer_started = false; 17760473661aSEdmund Nadolski goto done; 17770473661aSEdmund Nadolski } 1778cc9203bfSDan Williams 17790473661aSEdmund Nadolski for (i = 0; i < SCI_MAX_PHYS; i++) { 17800473661aSEdmund Nadolski 1781d9dcb4baSDan Williams if (ihost->power_control.phys_waiting == 0) 17820473661aSEdmund Nadolski break; 17830473661aSEdmund Nadolski 1784d9dcb4baSDan Williams iphy = ihost->power_control.requesters[i]; 178585280955SDan Williams if (iphy == NULL) 17860473661aSEdmund Nadolski continue; 17870473661aSEdmund Nadolski 17887000f7c7SAndrzej Jakowski if (ihost->power_control.phys_granted_power >= max_spin_up(ihost)) 17890473661aSEdmund Nadolski break; 17900473661aSEdmund Nadolski 1791d9dcb4baSDan Williams ihost->power_control.requesters[i] = NULL; 1792d9dcb4baSDan Williams ihost->power_control.phys_waiting--; 1793d9dcb4baSDan Williams ihost->power_control.phys_granted_power++; 179489a7301fSDan Williams sci_phy_consume_power_handler(iphy); 1795be778341SMarcin Tomczak 1796c79dd80dSDan Williams if (iphy->protocol == SAS_PROTOCOL_SSP) { 1797be778341SMarcin Tomczak u8 j; 1798be778341SMarcin Tomczak 1799be778341SMarcin Tomczak for (j = 0; j < SCI_MAX_PHYS; j++) { 1800be778341SMarcin Tomczak struct isci_phy *requester = ihost->power_control.requesters[j]; 1801be778341SMarcin Tomczak 1802be778341SMarcin Tomczak /* 1803be778341SMarcin Tomczak * Search the power_control queue to see if there are other phys 1804be778341SMarcin Tomczak * attached to the same remote device. If found, take all of 1805be778341SMarcin Tomczak * them out of await_sas_power state. 1806be778341SMarcin Tomczak */ 1807be778341SMarcin Tomczak if (requester != NULL && requester != iphy) { 1808be778341SMarcin Tomczak u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr, 1809be778341SMarcin Tomczak iphy->frame_rcvd.iaf.sas_addr, 1810be778341SMarcin Tomczak sizeof(requester->frame_rcvd.iaf.sas_addr)); 1811be778341SMarcin Tomczak 1812be778341SMarcin Tomczak if (other == 0) { 1813be778341SMarcin Tomczak ihost->power_control.requesters[j] = NULL; 1814be778341SMarcin Tomczak ihost->power_control.phys_waiting--; 1815be778341SMarcin Tomczak sci_phy_consume_power_handler(requester); 1816be778341SMarcin Tomczak } 1817be778341SMarcin Tomczak } 1818be778341SMarcin Tomczak } 1819be778341SMarcin Tomczak } 1820cc9203bfSDan Williams } 1821cc9203bfSDan Williams 1822cc9203bfSDan Williams /* 1823cc9203bfSDan Williams * It doesn't matter if the power list is empty, we need to start the 1824cc9203bfSDan Williams * timer in case another phy becomes ready. 1825cc9203bfSDan Williams */ 18260473661aSEdmund Nadolski sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL); 1827d9dcb4baSDan Williams ihost->power_control.timer_started = true; 18280473661aSEdmund Nadolski 18290473661aSEdmund Nadolski done: 18300473661aSEdmund Nadolski spin_unlock_irqrestore(&ihost->scic_lock, flags); 1831cc9203bfSDan Williams } 1832cc9203bfSDan Williams 183389a7301fSDan Williams void sci_controller_power_control_queue_insert(struct isci_host *ihost, 183485280955SDan Williams struct isci_phy *iphy) 1835cc9203bfSDan Williams { 183685280955SDan Williams BUG_ON(iphy == NULL); 1837cc9203bfSDan Williams 18387000f7c7SAndrzej Jakowski if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) { 1839d9dcb4baSDan Williams ihost->power_control.phys_granted_power++; 184089a7301fSDan Williams sci_phy_consume_power_handler(iphy); 1841cc9203bfSDan Williams 1842cc9203bfSDan Williams /* 1843cc9203bfSDan Williams * stop and start the power_control timer. When the timer fires, the 1844cc9203bfSDan Williams * no_of_phys_granted_power will be set to 0 1845cc9203bfSDan Williams */ 1846d9dcb4baSDan Williams if (ihost->power_control.timer_started) 1847d9dcb4baSDan Williams sci_del_timer(&ihost->power_control.timer); 18480473661aSEdmund Nadolski 1849d9dcb4baSDan Williams sci_mod_timer(&ihost->power_control.timer, 18500473661aSEdmund Nadolski SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL); 1851d9dcb4baSDan Williams ihost->power_control.timer_started = true; 18520473661aSEdmund Nadolski 1853cc9203bfSDan Williams } else { 1854be778341SMarcin Tomczak /* 1855be778341SMarcin Tomczak * There are phys, attached to the same sas address as this phy, are 1856be778341SMarcin Tomczak * already in READY state, this phy don't need wait. 1857be778341SMarcin Tomczak */ 1858be778341SMarcin Tomczak u8 i; 1859be778341SMarcin Tomczak struct isci_phy *current_phy; 1860be778341SMarcin Tomczak 1861be778341SMarcin Tomczak for (i = 0; i < SCI_MAX_PHYS; i++) { 1862be778341SMarcin Tomczak u8 other; 1863be778341SMarcin Tomczak current_phy = &ihost->phys[i]; 1864be778341SMarcin Tomczak 1865be778341SMarcin Tomczak other = memcmp(current_phy->frame_rcvd.iaf.sas_addr, 1866be778341SMarcin Tomczak iphy->frame_rcvd.iaf.sas_addr, 1867be778341SMarcin Tomczak sizeof(current_phy->frame_rcvd.iaf.sas_addr)); 1868be778341SMarcin Tomczak 1869be778341SMarcin Tomczak if (current_phy->sm.current_state_id == SCI_PHY_READY && 1870c79dd80dSDan Williams current_phy->protocol == SAS_PROTOCOL_SSP && 1871be778341SMarcin Tomczak other == 0) { 1872be778341SMarcin Tomczak sci_phy_consume_power_handler(iphy); 1873be778341SMarcin Tomczak break; 1874be778341SMarcin Tomczak } 1875be778341SMarcin Tomczak } 1876be778341SMarcin Tomczak 1877be778341SMarcin Tomczak if (i == SCI_MAX_PHYS) { 1878cc9203bfSDan Williams /* Add the phy in the waiting list */ 1879d9dcb4baSDan Williams ihost->power_control.requesters[iphy->phy_index] = iphy; 1880d9dcb4baSDan Williams ihost->power_control.phys_waiting++; 1881cc9203bfSDan Williams } 1882cc9203bfSDan Williams } 1883be778341SMarcin Tomczak } 1884cc9203bfSDan Williams 188589a7301fSDan Williams void sci_controller_power_control_queue_remove(struct isci_host *ihost, 188685280955SDan Williams struct isci_phy *iphy) 1887cc9203bfSDan Williams { 188885280955SDan Williams BUG_ON(iphy == NULL); 1889cc9203bfSDan Williams 189089a7301fSDan Williams if (ihost->power_control.requesters[iphy->phy_index]) 1891d9dcb4baSDan Williams ihost->power_control.phys_waiting--; 1892cc9203bfSDan Williams 1893d9dcb4baSDan Williams ihost->power_control.requesters[iphy->phy_index] = NULL; 1894cc9203bfSDan Williams } 1895cc9203bfSDan Williams 1896afd13a1fSJeff Skirvin static int is_long_cable(int phy, unsigned char selection_byte) 1897afd13a1fSJeff Skirvin { 18989fee607fSJeff Skirvin return !!(selection_byte & (1 << phy)); 1899afd13a1fSJeff Skirvin } 1900afd13a1fSJeff Skirvin 1901afd13a1fSJeff Skirvin static int is_medium_cable(int phy, unsigned char selection_byte) 1902afd13a1fSJeff Skirvin { 19039fee607fSJeff Skirvin return !!(selection_byte & (1 << (phy + 4))); 19049fee607fSJeff Skirvin } 19059fee607fSJeff Skirvin 19069fee607fSJeff Skirvin static enum cable_selections decode_selection_byte( 19079fee607fSJeff Skirvin int phy, 19089fee607fSJeff Skirvin unsigned char selection_byte) 19099fee607fSJeff Skirvin { 19109fee607fSJeff Skirvin return ((selection_byte & (1 << phy)) ? 1 : 0) 19119fee607fSJeff Skirvin + (selection_byte & (1 << (phy + 4)) ? 2 : 0); 19129fee607fSJeff Skirvin } 19139fee607fSJeff Skirvin 19149fee607fSJeff Skirvin static unsigned char *to_cable_select(struct isci_host *ihost) 19159fee607fSJeff Skirvin { 19169fee607fSJeff Skirvin if (is_cable_select_overridden()) 19179fee607fSJeff Skirvin return ((unsigned char *)&cable_selection_override) 19189fee607fSJeff Skirvin + ihost->id; 19199fee607fSJeff Skirvin else 19209fee607fSJeff Skirvin return &ihost->oem_parameters.controller.cable_selection_mask; 19219fee607fSJeff Skirvin } 19229fee607fSJeff Skirvin 19239fee607fSJeff Skirvin enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy) 19249fee607fSJeff Skirvin { 19259fee607fSJeff Skirvin return decode_selection_byte(phy, *to_cable_select(ihost)); 19269fee607fSJeff Skirvin } 19279fee607fSJeff Skirvin 19289fee607fSJeff Skirvin char *lookup_cable_names(enum cable_selections selection) 19299fee607fSJeff Skirvin { 19309fee607fSJeff Skirvin static char *cable_names[] = { 19319fee607fSJeff Skirvin [short_cable] = "short", 19329fee607fSJeff Skirvin [long_cable] = "long", 19339fee607fSJeff Skirvin [medium_cable] = "medium", 19349fee607fSJeff Skirvin [undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */ 19359fee607fSJeff Skirvin }; 19369fee607fSJeff Skirvin return (selection <= undefined_cable) ? cable_names[selection] 19379fee607fSJeff Skirvin : cable_names[undefined_cable]; 1938afd13a1fSJeff Skirvin } 1939afd13a1fSJeff Skirvin 1940cc9203bfSDan Williams #define AFE_REGISTER_WRITE_DELAY 10 1941cc9203bfSDan Williams 194289a7301fSDan Williams static void sci_controller_afe_initialization(struct isci_host *ihost) 1943cc9203bfSDan Williams { 19442e5da889SDan Williams struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe; 194589a7301fSDan Williams const struct sci_oem_params *oem = &ihost->oem_parameters; 1946dc00c8b6SDan Williams struct pci_dev *pdev = ihost->pdev; 1947cc9203bfSDan Williams u32 afe_status; 1948cc9203bfSDan Williams u32 phy_id; 19499fee607fSJeff Skirvin unsigned char cable_selection_mask = *to_cable_select(ihost); 1950cc9203bfSDan Williams 1951cc9203bfSDan Williams /* Clear DFX Status registers */ 19522e5da889SDan Williams writel(0x0081000f, &afe->afe_dfx_master_control0); 1953cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 1954cc9203bfSDan Williams 1955afd13a1fSJeff Skirvin if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) { 1956cc9203bfSDan Williams /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement 19572e5da889SDan Williams * Timer, PM Stagger Timer 19582e5da889SDan Williams */ 1959afd13a1fSJeff Skirvin writel(0x0007FFFF, &afe->afe_pmsn_master_control2); 1960cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 1961cc9203bfSDan Williams } 1962cc9203bfSDan Williams 1963cc9203bfSDan Williams /* Configure bias currents to normal */ 1964dc00c8b6SDan Williams if (is_a2(pdev)) 19652e5da889SDan Williams writel(0x00005A00, &afe->afe_bias_control); 1966dc00c8b6SDan Williams else if (is_b0(pdev) || is_c0(pdev)) 19672e5da889SDan Williams writel(0x00005F00, &afe->afe_bias_control); 1968afd13a1fSJeff Skirvin else if (is_c1(pdev)) 1969afd13a1fSJeff Skirvin writel(0x00005500, &afe->afe_bias_control); 1970cc9203bfSDan Williams 1971cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 1972cc9203bfSDan Williams 1973cc9203bfSDan Williams /* Enable PLL */ 1974afd13a1fSJeff Skirvin if (is_a2(pdev)) 19752e5da889SDan Williams writel(0x80040908, &afe->afe_pll_control0); 1976afd13a1fSJeff Skirvin else if (is_b0(pdev) || is_c0(pdev)) 1977afd13a1fSJeff Skirvin writel(0x80040A08, &afe->afe_pll_control0); 1978afd13a1fSJeff Skirvin else if (is_c1(pdev)) { 1979afd13a1fSJeff Skirvin writel(0x80000B08, &afe->afe_pll_control0); 1980afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 1981afd13a1fSJeff Skirvin writel(0x00000B08, &afe->afe_pll_control0); 1982afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 1983afd13a1fSJeff Skirvin writel(0x80000B08, &afe->afe_pll_control0); 1984afd13a1fSJeff Skirvin } 1985cc9203bfSDan Williams 1986cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 1987cc9203bfSDan Williams 1988cc9203bfSDan Williams /* Wait for the PLL to lock */ 1989cc9203bfSDan Williams do { 19902e5da889SDan Williams afe_status = readl(&afe->afe_common_block_status); 1991cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 1992cc9203bfSDan Williams } while ((afe_status & 0x00001000) == 0); 1993cc9203bfSDan Williams 1994dc00c8b6SDan Williams if (is_a2(pdev)) { 19952e5da889SDan Williams /* Shorten SAS SNW lock time (RxLock timer value from 76 19962e5da889SDan Williams * us to 50 us) 19972e5da889SDan Williams */ 19982e5da889SDan Williams writel(0x7bcc96ad, &afe->afe_pmsn_master_control0); 1999cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2000cc9203bfSDan Williams } 2001cc9203bfSDan Williams 2002cc9203bfSDan Williams for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) { 20032e5da889SDan Williams struct scu_afe_transceiver *xcvr = &afe->scu_afe_xcvr[phy_id]; 2004cc9203bfSDan Williams const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id]; 2005afd13a1fSJeff Skirvin int cable_length_long = 2006afd13a1fSJeff Skirvin is_long_cable(phy_id, cable_selection_mask); 2007afd13a1fSJeff Skirvin int cable_length_medium = 2008afd13a1fSJeff Skirvin is_medium_cable(phy_id, cable_selection_mask); 2009cc9203bfSDan Williams 2010afd13a1fSJeff Skirvin if (is_a2(pdev)) { 20112e5da889SDan Williams /* All defaults, except the Receive Word 20122e5da889SDan Williams * Alignament/Comma Detect Enable....(0xe800) 20132e5da889SDan Williams */ 20142e5da889SDan Williams writel(0x00004512, &xcvr->afe_xcvr_control0); 2015cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2016cc9203bfSDan Williams 20172e5da889SDan Williams writel(0x0050100F, &xcvr->afe_xcvr_control1); 2018cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2019afd13a1fSJeff Skirvin } else if (is_b0(pdev)) { 2020afd13a1fSJeff Skirvin /* Configure transmitter SSC parameters */ 2021afd13a1fSJeff Skirvin writel(0x00030000, &xcvr->afe_tx_ssc_control); 2022afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2023afd13a1fSJeff Skirvin } else if (is_c0(pdev)) { 2024afd13a1fSJeff Skirvin /* Configure transmitter SSC parameters */ 2025afd13a1fSJeff Skirvin writel(0x00010202, &xcvr->afe_tx_ssc_control); 2026afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2027afd13a1fSJeff Skirvin 2028afd13a1fSJeff Skirvin /* All defaults, except the Receive Word 2029afd13a1fSJeff Skirvin * Alignament/Comma Detect Enable....(0xe800) 2030afd13a1fSJeff Skirvin */ 2031afd13a1fSJeff Skirvin writel(0x00014500, &xcvr->afe_xcvr_control0); 2032afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2033afd13a1fSJeff Skirvin } else if (is_c1(pdev)) { 2034afd13a1fSJeff Skirvin /* Configure transmitter SSC parameters */ 2035afd13a1fSJeff Skirvin writel(0x00010202, &xcvr->afe_tx_ssc_control); 2036afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2037afd13a1fSJeff Skirvin 2038afd13a1fSJeff Skirvin /* All defaults, except the Receive Word 2039afd13a1fSJeff Skirvin * Alignament/Comma Detect Enable....(0xe800) 2040afd13a1fSJeff Skirvin */ 2041afd13a1fSJeff Skirvin writel(0x0001C500, &xcvr->afe_xcvr_control0); 2042afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2043cc9203bfSDan Williams } 2044cc9203bfSDan Williams 2045afd13a1fSJeff Skirvin /* Power up TX and RX out from power down (PWRDNTX and 2046afd13a1fSJeff Skirvin * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c) 20472e5da889SDan Williams */ 2048dc00c8b6SDan Williams if (is_a2(pdev)) 20492e5da889SDan Williams writel(0x000003F0, &xcvr->afe_channel_control); 2050dc00c8b6SDan Williams else if (is_b0(pdev)) { 20512e5da889SDan Williams writel(0x000003D7, &xcvr->afe_channel_control); 2052cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2053afd13a1fSJeff Skirvin 20542e5da889SDan Williams writel(0x000003D4, &xcvr->afe_channel_control); 2055afd13a1fSJeff Skirvin } else if (is_c0(pdev)) { 20562e5da889SDan Williams writel(0x000001E7, &xcvr->afe_channel_control); 2057dbb0743aSAdam Gruchala udelay(AFE_REGISTER_WRITE_DELAY); 2058afd13a1fSJeff Skirvin 20592e5da889SDan Williams writel(0x000001E4, &xcvr->afe_channel_control); 2060afd13a1fSJeff Skirvin } else if (is_c1(pdev)) { 2061afd13a1fSJeff Skirvin writel(cable_length_long ? 0x000002F7 : 0x000001F7, 2062afd13a1fSJeff Skirvin &xcvr->afe_channel_control); 2063afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2064afd13a1fSJeff Skirvin 2065afd13a1fSJeff Skirvin writel(cable_length_long ? 0x000002F4 : 0x000001F4, 2066afd13a1fSJeff Skirvin &xcvr->afe_channel_control); 2067cc9203bfSDan Williams } 2068cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2069cc9203bfSDan Williams 2070dc00c8b6SDan Williams if (is_a2(pdev)) { 2071cc9203bfSDan Williams /* Enable TX equalization (0xe824) */ 20722e5da889SDan Williams writel(0x00040000, &xcvr->afe_tx_control); 2073cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2074cc9203bfSDan Williams } 2075cc9203bfSDan Williams 2076afd13a1fSJeff Skirvin if (is_a2(pdev) || is_b0(pdev)) 2077afd13a1fSJeff Skirvin /* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0, 2078afd13a1fSJeff Skirvin * TPD=0x0(TX Power On), RDD=0x0(RX Detect 2079afd13a1fSJeff Skirvin * Enabled) ....(0xe800) 2080afd13a1fSJeff Skirvin */ 20812e5da889SDan Williams writel(0x00004100, &xcvr->afe_xcvr_control0); 2082afd13a1fSJeff Skirvin else if (is_c0(pdev)) 2083afd13a1fSJeff Skirvin writel(0x00014100, &xcvr->afe_xcvr_control0); 2084afd13a1fSJeff Skirvin else if (is_c1(pdev)) 2085afd13a1fSJeff Skirvin writel(0x0001C100, &xcvr->afe_xcvr_control0); 2086cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2087cc9203bfSDan Williams 2088cc9203bfSDan Williams /* Leave DFE/FFE on */ 2089dc00c8b6SDan Williams if (is_a2(pdev)) 20902e5da889SDan Williams writel(0x3F11103F, &xcvr->afe_rx_ssc_control0); 2091dc00c8b6SDan Williams else if (is_b0(pdev)) { 20922e5da889SDan Williams writel(0x3F11103F, &xcvr->afe_rx_ssc_control0); 2093cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2094cc9203bfSDan Williams /* Enable TX equalization (0xe824) */ 20952e5da889SDan Williams writel(0x00040000, &xcvr->afe_tx_control); 2096afd13a1fSJeff Skirvin } else if (is_c0(pdev)) { 2097afd13a1fSJeff Skirvin writel(0x01400C0F, &xcvr->afe_rx_ssc_control1); 2098dbb0743aSAdam Gruchala udelay(AFE_REGISTER_WRITE_DELAY); 2099dbb0743aSAdam Gruchala 21002e5da889SDan Williams writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0); 2101dbb0743aSAdam Gruchala udelay(AFE_REGISTER_WRITE_DELAY); 2102dbb0743aSAdam Gruchala 2103dbb0743aSAdam Gruchala /* Enable TX equalization (0xe824) */ 21042e5da889SDan Williams writel(0x00040000, &xcvr->afe_tx_control); 2105afd13a1fSJeff Skirvin } else if (is_c1(pdev)) { 2106afd13a1fSJeff Skirvin writel(cable_length_long ? 0x01500C0C : 2107afd13a1fSJeff Skirvin cable_length_medium ? 0x01400C0D : 0x02400C0D, 2108afd13a1fSJeff Skirvin &xcvr->afe_xcvr_control1); 2109afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2110afd13a1fSJeff Skirvin 2111afd13a1fSJeff Skirvin writel(0x000003E0, &xcvr->afe_dfx_rx_control1); 2112afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2113afd13a1fSJeff Skirvin 2114afd13a1fSJeff Skirvin writel(cable_length_long ? 0x33091C1F : 2115afd13a1fSJeff Skirvin cable_length_medium ? 0x3315181F : 0x2B17161F, 2116afd13a1fSJeff Skirvin &xcvr->afe_rx_ssc_control0); 2117afd13a1fSJeff Skirvin udelay(AFE_REGISTER_WRITE_DELAY); 2118afd13a1fSJeff Skirvin 2119afd13a1fSJeff Skirvin /* Enable TX equalization (0xe824) */ 2120afd13a1fSJeff Skirvin writel(0x00040000, &xcvr->afe_tx_control); 2121cc9203bfSDan Williams } 2122dbb0743aSAdam Gruchala 2123cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2124cc9203bfSDan Williams 21252e5da889SDan Williams writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0); 2126cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2127cc9203bfSDan Williams 21282e5da889SDan Williams writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1); 2129cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2130cc9203bfSDan Williams 21312e5da889SDan Williams writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2); 2132cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2133cc9203bfSDan Williams 21342e5da889SDan Williams writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3); 2135cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2136cc9203bfSDan Williams } 2137cc9203bfSDan Williams 2138cc9203bfSDan Williams /* Transfer control to the PEs */ 21392e5da889SDan Williams writel(0x00010f00, &afe->afe_dfx_master_control0); 2140cc9203bfSDan Williams udelay(AFE_REGISTER_WRITE_DELAY); 2141cc9203bfSDan Williams } 2142cc9203bfSDan Williams 214389a7301fSDan Williams static void sci_controller_initialize_power_control(struct isci_host *ihost) 2144cc9203bfSDan Williams { 2145d9dcb4baSDan Williams sci_init_timer(&ihost->power_control.timer, power_control_timeout); 2146cc9203bfSDan Williams 2147d9dcb4baSDan Williams memset(ihost->power_control.requesters, 0, 2148d9dcb4baSDan Williams sizeof(ihost->power_control.requesters)); 2149cc9203bfSDan Williams 2150d9dcb4baSDan Williams ihost->power_control.phys_waiting = 0; 2151d9dcb4baSDan Williams ihost->power_control.phys_granted_power = 0; 2152cc9203bfSDan Williams } 2153cc9203bfSDan Williams 215489a7301fSDan Williams static enum sci_status sci_controller_initialize(struct isci_host *ihost) 2155cc9203bfSDan Williams { 2156d9dcb4baSDan Williams struct sci_base_state_machine *sm = &ihost->sm; 21577c78da31SDan Williams enum sci_status result = SCI_FAILURE; 21587c78da31SDan Williams unsigned long i, state, val; 2159cc9203bfSDan Williams 2160d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_RESET) { 216114e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 216214e99b4aSDan Williams __func__, ihost->sm.current_state_id); 2163cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 2164cc9203bfSDan Williams } 2165cc9203bfSDan Williams 2166e301370aSEdmund Nadolski sci_change_state(sm, SCIC_INITIALIZING); 2167cc9203bfSDan Williams 2168d9dcb4baSDan Williams sci_init_timer(&ihost->phy_timer, phy_startup_timeout); 2169bb3dbdf6SEdmund Nadolski 2170d9dcb4baSDan Williams ihost->next_phy_to_start = 0; 2171d9dcb4baSDan Williams ihost->phy_startup_timer_pending = false; 2172cc9203bfSDan Williams 217389a7301fSDan Williams sci_controller_initialize_power_control(ihost); 2174cc9203bfSDan Williams 2175cc9203bfSDan Williams /* 2176cc9203bfSDan Williams * There is nothing to do here for B0 since we do not have to 2177cc9203bfSDan Williams * program the AFE registers. 2178cc9203bfSDan Williams * / @todo The AFE settings are supposed to be correct for the B0 but 2179cc9203bfSDan Williams * / presently they seem to be wrong. */ 218089a7301fSDan Williams sci_controller_afe_initialization(ihost); 2181cc9203bfSDan Williams 2182cc9203bfSDan Williams 2183cc9203bfSDan Williams /* Take the hardware out of reset */ 2184d9dcb4baSDan Williams writel(0, &ihost->smu_registers->soft_reset_control); 2185cc9203bfSDan Williams 2186cc9203bfSDan Williams /* 2187cc9203bfSDan Williams * / @todo Provide meaningfull error code for hardware failure 2188cc9203bfSDan Williams * result = SCI_FAILURE_CONTROLLER_HARDWARE; */ 21897c78da31SDan Williams for (i = 100; i >= 1; i--) { 21907c78da31SDan Williams u32 status; 2191cc9203bfSDan Williams 2192cc9203bfSDan Williams /* Loop until the hardware reports success */ 2193cc9203bfSDan Williams udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME); 2194d9dcb4baSDan Williams status = readl(&ihost->smu_registers->control_status); 2195cc9203bfSDan Williams 21967c78da31SDan Williams if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED) 21977c78da31SDan Williams break; 2198cc9203bfSDan Williams } 21997c78da31SDan Williams if (i == 0) 22007c78da31SDan Williams goto out; 2201cc9203bfSDan Williams 2202cc9203bfSDan Williams /* 2203cc9203bfSDan Williams * Determine what are the actaul device capacities that the 2204cc9203bfSDan Williams * hardware will support */ 2205d9dcb4baSDan Williams val = readl(&ihost->smu_registers->device_context_capacity); 2206cc9203bfSDan Williams 22077c78da31SDan Williams /* Record the smaller of the two capacity values */ 2208d9dcb4baSDan Williams ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS); 2209d9dcb4baSDan Williams ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS); 2210d9dcb4baSDan Williams ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES); 2211cc9203bfSDan Williams 2212cc9203bfSDan Williams /* 2213cc9203bfSDan Williams * Make all PEs that are unassigned match up with the 2214cc9203bfSDan Williams * logical ports 2215cc9203bfSDan Williams */ 2216d9dcb4baSDan Williams for (i = 0; i < ihost->logical_port_entries; i++) { 2217cc9203bfSDan Williams struct scu_port_task_scheduler_group_registers __iomem 2218d9dcb4baSDan Williams *ptsg = &ihost->scu_registers->peg0.ptsg; 2219cc9203bfSDan Williams 22207c78da31SDan Williams writel(i, &ptsg->protocol_engine[i]); 2221cc9203bfSDan Williams } 2222cc9203bfSDan Williams 2223cc9203bfSDan Williams /* Initialize hardware PCI Relaxed ordering in DMA engines */ 2224d9dcb4baSDan Williams val = readl(&ihost->scu_registers->sdma.pdma_configuration); 22257c78da31SDan Williams val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE); 2226d9dcb4baSDan Williams writel(val, &ihost->scu_registers->sdma.pdma_configuration); 2227cc9203bfSDan Williams 2228d9dcb4baSDan Williams val = readl(&ihost->scu_registers->sdma.cdma_configuration); 22297c78da31SDan Williams val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE); 2230d9dcb4baSDan Williams writel(val, &ihost->scu_registers->sdma.cdma_configuration); 2231cc9203bfSDan Williams 2232cc9203bfSDan Williams /* 2233cc9203bfSDan Williams * Initialize the PHYs before the PORTs because the PHY registers 2234cc9203bfSDan Williams * are accessed during the port initialization. 2235cc9203bfSDan Williams */ 22367c78da31SDan Williams for (i = 0; i < SCI_MAX_PHYS; i++) { 223789a7301fSDan Williams result = sci_phy_initialize(&ihost->phys[i], 2238d9dcb4baSDan Williams &ihost->scu_registers->peg0.pe[i].tl, 2239d9dcb4baSDan Williams &ihost->scu_registers->peg0.pe[i].ll); 22407c78da31SDan Williams if (result != SCI_SUCCESS) 22417c78da31SDan Williams goto out; 2242cc9203bfSDan Williams } 2243cc9203bfSDan Williams 2244d9dcb4baSDan Williams for (i = 0; i < ihost->logical_port_entries; i++) { 224589a7301fSDan Williams struct isci_port *iport = &ihost->ports[i]; 22467c78da31SDan Williams 224789a7301fSDan Williams iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i]; 224889a7301fSDan Williams iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0]; 224989a7301fSDan Williams iport->viit_registers = &ihost->scu_registers->peg0.viit[i]; 2250cc9203bfSDan Williams } 2251cc9203bfSDan Williams 225289a7301fSDan Williams result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent); 2253cc9203bfSDan Williams 22547c78da31SDan Williams out: 2255cc9203bfSDan Williams /* Advance the controller state machine */ 2256cc9203bfSDan Williams if (result == SCI_SUCCESS) 2257e301370aSEdmund Nadolski state = SCIC_INITIALIZED; 2258cc9203bfSDan Williams else 2259e301370aSEdmund Nadolski state = SCIC_FAILED; 2260e301370aSEdmund Nadolski sci_change_state(sm, state); 2261cc9203bfSDan Williams 2262cc9203bfSDan Williams return result; 2263cc9203bfSDan Williams } 2264cc9203bfSDan Williams 2265abec912dSDan Williams static int sci_controller_dma_alloc(struct isci_host *ihost) 2266cc9203bfSDan Williams { 2267abec912dSDan Williams struct device *dev = &ihost->pdev->dev; 2268abec912dSDan Williams size_t size; 2269abec912dSDan Williams int i; 2270cc9203bfSDan Williams 2271abec912dSDan Williams /* detect re-initialization */ 2272abec912dSDan Williams if (ihost->completion_queue) 2273abec912dSDan Williams return 0; 2274cc9203bfSDan Williams 2275abec912dSDan Williams size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32); 2276abec912dSDan Williams ihost->completion_queue = dmam_alloc_coherent(dev, size, &ihost->cq_dma, 2277abec912dSDan Williams GFP_KERNEL); 2278abec912dSDan Williams if (!ihost->completion_queue) 2279abec912dSDan Williams return -ENOMEM; 2280cc9203bfSDan Williams 2281abec912dSDan Williams size = ihost->remote_node_entries * sizeof(union scu_remote_node_context); 2282abec912dSDan Williams ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &ihost->rnc_dma, 2283abec912dSDan Williams GFP_KERNEL); 2284cc9203bfSDan Williams 2285abec912dSDan Williams if (!ihost->remote_node_context_table) 2286abec912dSDan Williams return -ENOMEM; 2287cc9203bfSDan Williams 2288abec912dSDan Williams size = ihost->task_context_entries * sizeof(struct scu_task_context), 2289abec912dSDan Williams ihost->task_context_table = dmam_alloc_coherent(dev, size, &ihost->tc_dma, 2290abec912dSDan Williams GFP_KERNEL); 2291abec912dSDan Williams if (!ihost->task_context_table) 2292abec912dSDan Williams return -ENOMEM; 2293cc9203bfSDan Williams 2294abec912dSDan Williams size = SCI_UFI_TOTAL_SIZE; 2295abec912dSDan Williams ihost->ufi_buf = dmam_alloc_coherent(dev, size, &ihost->ufi_dma, GFP_KERNEL); 2296abec912dSDan Williams if (!ihost->ufi_buf) 2297abec912dSDan Williams return -ENOMEM; 2298abec912dSDan Williams 2299abec912dSDan Williams for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) { 2300abec912dSDan Williams struct isci_request *ireq; 2301abec912dSDan Williams dma_addr_t dma; 2302abec912dSDan Williams 2303abec912dSDan Williams ireq = dmam_alloc_coherent(dev, sizeof(*ireq), &dma, GFP_KERNEL); 2304abec912dSDan Williams if (!ireq) 2305abec912dSDan Williams return -ENOMEM; 2306abec912dSDan Williams 2307abec912dSDan Williams ireq->tc = &ihost->task_context_table[i]; 2308abec912dSDan Williams ireq->owning_controller = ihost; 2309abec912dSDan Williams ireq->request_daddr = dma; 2310abec912dSDan Williams ireq->isci_host = ihost; 2311abec912dSDan Williams ihost->reqs[i] = ireq; 2312cc9203bfSDan Williams } 2313cc9203bfSDan Williams 2314abec912dSDan Williams return 0; 2315cc9203bfSDan Williams } 2316cc9203bfSDan Williams 231789a7301fSDan Williams static int sci_controller_mem_init(struct isci_host *ihost) 2318cc9203bfSDan Williams { 2319abec912dSDan Williams int err = sci_controller_dma_alloc(ihost); 2320cc9203bfSDan Williams 23217c78da31SDan Williams if (err) 23227c78da31SDan Williams return err; 2323cc9203bfSDan Williams 2324abec912dSDan Williams writel(lower_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_lower); 2325abec912dSDan Williams writel(upper_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_upper); 2326abec912dSDan Williams 2327abec912dSDan Williams writel(lower_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_lower); 2328abec912dSDan Williams writel(upper_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_upper); 2329abec912dSDan Williams 2330abec912dSDan Williams writel(lower_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_lower); 2331abec912dSDan Williams writel(upper_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_upper); 2332abec912dSDan Williams 2333abec912dSDan Williams sci_unsolicited_frame_control_construct(ihost); 2334abec912dSDan Williams 2335cc9203bfSDan Williams /* 2336cc9203bfSDan Williams * Inform the silicon as to the location of the UF headers and 2337cc9203bfSDan Williams * address table. 2338cc9203bfSDan Williams */ 2339d9dcb4baSDan Williams writel(lower_32_bits(ihost->uf_control.headers.physical_address), 2340d9dcb4baSDan Williams &ihost->scu_registers->sdma.uf_header_base_address_lower); 2341d9dcb4baSDan Williams writel(upper_32_bits(ihost->uf_control.headers.physical_address), 2342d9dcb4baSDan Williams &ihost->scu_registers->sdma.uf_header_base_address_upper); 2343cc9203bfSDan Williams 2344d9dcb4baSDan Williams writel(lower_32_bits(ihost->uf_control.address_table.physical_address), 2345d9dcb4baSDan Williams &ihost->scu_registers->sdma.uf_address_table_lower); 2346d9dcb4baSDan Williams writel(upper_32_bits(ihost->uf_control.address_table.physical_address), 2347d9dcb4baSDan Williams &ihost->scu_registers->sdma.uf_address_table_upper); 2348cc9203bfSDan Williams 2349cc9203bfSDan Williams return 0; 2350cc9203bfSDan Williams } 2351cc9203bfSDan Williams 2352abec912dSDan Williams /** 2353abec912dSDan Williams * isci_host_init - (re-)initialize hardware and internal (private) state 2354abec912dSDan Williams * @ihost: host to init 2355abec912dSDan Williams * 2356abec912dSDan Williams * Any public facing objects (like asd_sas_port, and asd_sas_phys), or 2357abec912dSDan Williams * one-time initialization objects like locks and waitqueues, are 2358abec912dSDan Williams * not touched (they are initialized in isci_host_alloc) 2359abec912dSDan Williams */ 2360d9dcb4baSDan Williams int isci_host_init(struct isci_host *ihost) 23616f231ddaSDan Williams { 2362abec912dSDan Williams int i, err; 23636f231ddaSDan Williams enum sci_status status; 23646f231ddaSDan Williams 23652396a265SDan Williams spin_lock_irq(&ihost->scic_lock); 2366abec912dSDan Williams status = sci_controller_construct(ihost, scu_base(ihost), smu_base(ihost)); 23672396a265SDan Williams spin_unlock_irq(&ihost->scic_lock); 23686f231ddaSDan Williams if (status != SCI_SUCCESS) { 2369d9dcb4baSDan Williams dev_err(&ihost->pdev->dev, 237089a7301fSDan Williams "%s: sci_controller_construct failed - status = %x\n", 23716f231ddaSDan Williams __func__, 23726f231ddaSDan Williams status); 2373858d4aa7SDave Jiang return -ENODEV; 23746f231ddaSDan Williams } 23756f231ddaSDan Williams 2376d9dcb4baSDan Williams spin_lock_irq(&ihost->scic_lock); 237789a7301fSDan Williams status = sci_controller_initialize(ihost); 2378d9dcb4baSDan Williams spin_unlock_irq(&ihost->scic_lock); 23797c40a803SDan Williams if (status != SCI_SUCCESS) { 2380d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 238189a7301fSDan Williams "%s: sci_controller_initialize failed -" 23827c40a803SDan Williams " status = 0x%x\n", 23837c40a803SDan Williams __func__, status); 23847c40a803SDan Williams return -ENODEV; 23857c40a803SDan Williams } 23867c40a803SDan Williams 238789a7301fSDan Williams err = sci_controller_mem_init(ihost); 23886f231ddaSDan Williams if (err) 2389858d4aa7SDave Jiang return err; 23906f231ddaSDan Williams 2391ad4f4c1dSDan Williams /* enable sgpio */ 2392ad4f4c1dSDan Williams writel(1, &ihost->scu_registers->peg0.sgpio.interface_control); 2393ad4f4c1dSDan Williams for (i = 0; i < isci_gpio_count(ihost); i++) 2394ad4f4c1dSDan Williams writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]); 2395ad4f4c1dSDan Williams writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code); 2396ad4f4c1dSDan Williams 2397858d4aa7SDave Jiang return 0; 23986f231ddaSDan Williams } 2399cc9203bfSDan Williams 240089a7301fSDan Williams void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport, 240189a7301fSDan Williams struct isci_phy *iphy) 2402cc9203bfSDan Williams { 2403d9dcb4baSDan Williams switch (ihost->sm.current_state_id) { 2404e301370aSEdmund Nadolski case SCIC_STARTING: 2405d9dcb4baSDan Williams sci_del_timer(&ihost->phy_timer); 2406d9dcb4baSDan Williams ihost->phy_startup_timer_pending = false; 2407d9dcb4baSDan Williams ihost->port_agent.link_up_handler(ihost, &ihost->port_agent, 2408ffe191c9SDan Williams iport, iphy); 240989a7301fSDan Williams sci_controller_start_next_phy(ihost); 2410cc9203bfSDan Williams break; 2411e301370aSEdmund Nadolski case SCIC_READY: 2412d9dcb4baSDan Williams ihost->port_agent.link_up_handler(ihost, &ihost->port_agent, 2413ffe191c9SDan Williams iport, iphy); 2414cc9203bfSDan Williams break; 2415cc9203bfSDan Williams default: 2416d9dcb4baSDan Williams dev_dbg(&ihost->pdev->dev, 2417cc9203bfSDan Williams "%s: SCIC Controller linkup event from phy %d in " 241885280955SDan Williams "unexpected state %d\n", __func__, iphy->phy_index, 2419d9dcb4baSDan Williams ihost->sm.current_state_id); 2420cc9203bfSDan Williams } 2421cc9203bfSDan Williams } 2422cc9203bfSDan Williams 242389a7301fSDan Williams void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport, 242489a7301fSDan Williams struct isci_phy *iphy) 2425cc9203bfSDan Williams { 2426d9dcb4baSDan Williams switch (ihost->sm.current_state_id) { 2427e301370aSEdmund Nadolski case SCIC_STARTING: 2428e301370aSEdmund Nadolski case SCIC_READY: 2429d9dcb4baSDan Williams ihost->port_agent.link_down_handler(ihost, &ihost->port_agent, 2430ffe191c9SDan Williams iport, iphy); 2431cc9203bfSDan Williams break; 2432cc9203bfSDan Williams default: 2433d9dcb4baSDan Williams dev_dbg(&ihost->pdev->dev, 2434cc9203bfSDan Williams "%s: SCIC Controller linkdown event from phy %d in " 2435cc9203bfSDan Williams "unexpected state %d\n", 2436cc9203bfSDan Williams __func__, 243785280955SDan Williams iphy->phy_index, 2438d9dcb4baSDan Williams ihost->sm.current_state_id); 2439cc9203bfSDan Williams } 2440cc9203bfSDan Williams } 2441cc9203bfSDan Williams 2442eb608c3cSDan Williams bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost) 2443cc9203bfSDan Williams { 2444cc9203bfSDan Williams u32 index; 2445cc9203bfSDan Williams 2446d9dcb4baSDan Williams for (index = 0; index < ihost->remote_node_entries; index++) { 2447d9dcb4baSDan Williams if ((ihost->device_table[index] != NULL) && 2448d9dcb4baSDan Williams (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING)) 2449cc9203bfSDan Williams return true; 2450cc9203bfSDan Williams } 2451cc9203bfSDan Williams 2452cc9203bfSDan Williams return false; 2453cc9203bfSDan Williams } 2454cc9203bfSDan Williams 245589a7301fSDan Williams void sci_controller_remote_device_stopped(struct isci_host *ihost, 245678a6f06eSDan Williams struct isci_remote_device *idev) 2457cc9203bfSDan Williams { 2458d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_STOPPING) { 2459d9dcb4baSDan Williams dev_dbg(&ihost->pdev->dev, 2460cc9203bfSDan Williams "SCIC Controller 0x%p remote device stopped event " 2461cc9203bfSDan Williams "from device 0x%p in unexpected state %d\n", 2462d9dcb4baSDan Williams ihost, idev, 2463d9dcb4baSDan Williams ihost->sm.current_state_id); 2464cc9203bfSDan Williams return; 2465cc9203bfSDan Williams } 2466cc9203bfSDan Williams 246789a7301fSDan Williams if (!sci_controller_has_remote_devices_stopping(ihost)) 2468eb608c3cSDan Williams isci_host_stop_complete(ihost); 2469cc9203bfSDan Williams } 2470cc9203bfSDan Williams 247189a7301fSDan Williams void sci_controller_post_request(struct isci_host *ihost, u32 request) 2472cc9203bfSDan Williams { 247389a7301fSDan Williams dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n", 247489a7301fSDan Williams __func__, ihost->id, request); 2475cc9203bfSDan Williams 2476d9dcb4baSDan Williams writel(request, &ihost->smu_registers->post_context_port); 2477cc9203bfSDan Williams } 2478cc9203bfSDan Williams 247989a7301fSDan Williams struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag) 2480cc9203bfSDan Williams { 2481cc9203bfSDan Williams u16 task_index; 2482cc9203bfSDan Williams u16 task_sequence; 2483cc9203bfSDan Williams 2484dd047c8eSDan Williams task_index = ISCI_TAG_TCI(io_tag); 2485cc9203bfSDan Williams 2486d9dcb4baSDan Williams if (task_index < ihost->task_context_entries) { 2487d9dcb4baSDan Williams struct isci_request *ireq = ihost->reqs[task_index]; 2488db056250SDan Williams 2489db056250SDan Williams if (test_bit(IREQ_ACTIVE, &ireq->flags)) { 2490dd047c8eSDan Williams task_sequence = ISCI_TAG_SEQ(io_tag); 2491cc9203bfSDan Williams 2492d9dcb4baSDan Williams if (task_sequence == ihost->io_request_sequence[task_index]) 24935076a1a9SDan Williams return ireq; 2494cc9203bfSDan Williams } 2495cc9203bfSDan Williams } 2496cc9203bfSDan Williams 2497cc9203bfSDan Williams return NULL; 2498cc9203bfSDan Williams } 2499cc9203bfSDan Williams 2500cc9203bfSDan Williams /** 2501cc9203bfSDan Williams * This method allocates remote node index and the reserves the remote node 2502cc9203bfSDan Williams * context space for use. This method can fail if there are no more remote 2503cc9203bfSDan Williams * node index available. 2504cc9203bfSDan Williams * @scic: This is the controller object which contains the set of 2505cc9203bfSDan Williams * free remote node ids 2506cc9203bfSDan Williams * @sci_dev: This is the device object which is requesting the a remote node 2507cc9203bfSDan Williams * id 2508cc9203bfSDan Williams * @node_id: This is the remote node id that is assinged to the device if one 2509cc9203bfSDan Williams * is available 2510cc9203bfSDan Williams * 2511cc9203bfSDan Williams * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote 2512cc9203bfSDan Williams * node index available. 2513cc9203bfSDan Williams */ 251489a7301fSDan Williams enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost, 251578a6f06eSDan Williams struct isci_remote_device *idev, 2516cc9203bfSDan Williams u16 *node_id) 2517cc9203bfSDan Williams { 2518cc9203bfSDan Williams u16 node_index; 251989a7301fSDan Williams u32 remote_node_count = sci_remote_device_node_count(idev); 2520cc9203bfSDan Williams 252189a7301fSDan Williams node_index = sci_remote_node_table_allocate_remote_node( 2522d9dcb4baSDan Williams &ihost->available_remote_nodes, remote_node_count 2523cc9203bfSDan Williams ); 2524cc9203bfSDan Williams 2525cc9203bfSDan Williams if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) { 2526d9dcb4baSDan Williams ihost->device_table[node_index] = idev; 2527cc9203bfSDan Williams 2528cc9203bfSDan Williams *node_id = node_index; 2529cc9203bfSDan Williams 2530cc9203bfSDan Williams return SCI_SUCCESS; 2531cc9203bfSDan Williams } 2532cc9203bfSDan Williams 2533cc9203bfSDan Williams return SCI_FAILURE_INSUFFICIENT_RESOURCES; 2534cc9203bfSDan Williams } 2535cc9203bfSDan Williams 253689a7301fSDan Williams void sci_controller_free_remote_node_context(struct isci_host *ihost, 253778a6f06eSDan Williams struct isci_remote_device *idev, 2538cc9203bfSDan Williams u16 node_id) 2539cc9203bfSDan Williams { 254089a7301fSDan Williams u32 remote_node_count = sci_remote_device_node_count(idev); 2541cc9203bfSDan Williams 2542d9dcb4baSDan Williams if (ihost->device_table[node_id] == idev) { 2543d9dcb4baSDan Williams ihost->device_table[node_id] = NULL; 2544cc9203bfSDan Williams 254589a7301fSDan Williams sci_remote_node_table_release_remote_node_index( 2546d9dcb4baSDan Williams &ihost->available_remote_nodes, remote_node_count, node_id 2547cc9203bfSDan Williams ); 2548cc9203bfSDan Williams } 2549cc9203bfSDan Williams } 2550cc9203bfSDan Williams 255189a7301fSDan Williams void sci_controller_copy_sata_response(void *response_buffer, 2552cc9203bfSDan Williams void *frame_header, 2553cc9203bfSDan Williams void *frame_buffer) 2554cc9203bfSDan Williams { 255589a7301fSDan Williams /* XXX type safety? */ 2556cc9203bfSDan Williams memcpy(response_buffer, frame_header, sizeof(u32)); 2557cc9203bfSDan Williams 2558cc9203bfSDan Williams memcpy(response_buffer + sizeof(u32), 2559cc9203bfSDan Williams frame_buffer, 2560cc9203bfSDan Williams sizeof(struct dev_to_host_fis) - sizeof(u32)); 2561cc9203bfSDan Williams } 2562cc9203bfSDan Williams 256389a7301fSDan Williams void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index) 2564cc9203bfSDan Williams { 256589a7301fSDan Williams if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index)) 2566d9dcb4baSDan Williams writel(ihost->uf_control.get, 2567d9dcb4baSDan Williams &ihost->scu_registers->sdma.unsolicited_frame_get_pointer); 2568cc9203bfSDan Williams } 2569cc9203bfSDan Williams 2570312e0c24SDan Williams void isci_tci_free(struct isci_host *ihost, u16 tci) 2571312e0c24SDan Williams { 2572312e0c24SDan Williams u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1); 2573312e0c24SDan Williams 2574312e0c24SDan Williams ihost->tci_pool[tail] = tci; 2575312e0c24SDan Williams ihost->tci_tail = tail + 1; 2576312e0c24SDan Williams } 2577312e0c24SDan Williams 2578312e0c24SDan Williams static u16 isci_tci_alloc(struct isci_host *ihost) 2579312e0c24SDan Williams { 2580312e0c24SDan Williams u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1); 2581312e0c24SDan Williams u16 tci = ihost->tci_pool[head]; 2582312e0c24SDan Williams 2583312e0c24SDan Williams ihost->tci_head = head + 1; 2584312e0c24SDan Williams return tci; 2585312e0c24SDan Williams } 2586312e0c24SDan Williams 2587312e0c24SDan Williams static u16 isci_tci_space(struct isci_host *ihost) 2588312e0c24SDan Williams { 2589312e0c24SDan Williams return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS); 2590312e0c24SDan Williams } 2591312e0c24SDan Williams 2592312e0c24SDan Williams u16 isci_alloc_tag(struct isci_host *ihost) 2593312e0c24SDan Williams { 2594312e0c24SDan Williams if (isci_tci_space(ihost)) { 2595312e0c24SDan Williams u16 tci = isci_tci_alloc(ihost); 2596d9dcb4baSDan Williams u8 seq = ihost->io_request_sequence[tci]; 2597312e0c24SDan Williams 2598312e0c24SDan Williams return ISCI_TAG(seq, tci); 2599312e0c24SDan Williams } 2600312e0c24SDan Williams 2601312e0c24SDan Williams return SCI_CONTROLLER_INVALID_IO_TAG; 2602312e0c24SDan Williams } 2603312e0c24SDan Williams 2604312e0c24SDan Williams enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag) 2605312e0c24SDan Williams { 2606312e0c24SDan Williams u16 tci = ISCI_TAG_TCI(io_tag); 2607312e0c24SDan Williams u16 seq = ISCI_TAG_SEQ(io_tag); 2608312e0c24SDan Williams 2609312e0c24SDan Williams /* prevent tail from passing head */ 2610312e0c24SDan Williams if (isci_tci_active(ihost) == 0) 2611312e0c24SDan Williams return SCI_FAILURE_INVALID_IO_TAG; 2612312e0c24SDan Williams 2613d9dcb4baSDan Williams if (seq == ihost->io_request_sequence[tci]) { 2614d9dcb4baSDan Williams ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1); 2615312e0c24SDan Williams 2616312e0c24SDan Williams isci_tci_free(ihost, tci); 2617312e0c24SDan Williams 2618312e0c24SDan Williams return SCI_SUCCESS; 2619312e0c24SDan Williams } 2620312e0c24SDan Williams return SCI_FAILURE_INVALID_IO_TAG; 2621312e0c24SDan Williams } 2622312e0c24SDan Williams 262389a7301fSDan Williams enum sci_status sci_controller_start_io(struct isci_host *ihost, 262478a6f06eSDan Williams struct isci_remote_device *idev, 26255076a1a9SDan Williams struct isci_request *ireq) 2626cc9203bfSDan Williams { 2627cc9203bfSDan Williams enum sci_status status; 2628cc9203bfSDan Williams 2629d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_READY) { 263014e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 263114e99b4aSDan Williams __func__, ihost->sm.current_state_id); 2632cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 2633cc9203bfSDan Williams } 2634cc9203bfSDan Williams 263589a7301fSDan Williams status = sci_remote_device_start_io(ihost, idev, ireq); 2636cc9203bfSDan Williams if (status != SCI_SUCCESS) 2637cc9203bfSDan Williams return status; 2638cc9203bfSDan Williams 26395076a1a9SDan Williams set_bit(IREQ_ACTIVE, &ireq->flags); 264034a99158SDan Williams sci_controller_post_request(ihost, ireq->post_context); 2641cc9203bfSDan Williams return SCI_SUCCESS; 2642cc9203bfSDan Williams } 2643cc9203bfSDan Williams 264489a7301fSDan Williams enum sci_status sci_controller_terminate_request(struct isci_host *ihost, 264578a6f06eSDan Williams struct isci_remote_device *idev, 26465076a1a9SDan Williams struct isci_request *ireq) 2647cc9203bfSDan Williams { 264889a7301fSDan Williams /* terminate an ongoing (i.e. started) core IO request. This does not 264989a7301fSDan Williams * abort the IO request at the target, but rather removes the IO 265089a7301fSDan Williams * request from the host controller. 265189a7301fSDan Williams */ 2652cc9203bfSDan Williams enum sci_status status; 2653cc9203bfSDan Williams 2654d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_READY) { 265514e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 265614e99b4aSDan Williams __func__, ihost->sm.current_state_id); 2657cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 2658cc9203bfSDan Williams } 265989a7301fSDan Williams status = sci_io_request_terminate(ireq); 2660*14aaa9f0SJeff Skirvin 2661*14aaa9f0SJeff Skirvin dev_dbg(&ihost->pdev->dev, "%s: status=%d; ireq=%p; flags=%lx\n", 2662*14aaa9f0SJeff Skirvin __func__, status, ireq, ireq->flags); 2663*14aaa9f0SJeff Skirvin 2664726980d5SJeff Skirvin if ((status == SCI_SUCCESS) && 2665726980d5SJeff Skirvin !test_bit(IREQ_PENDING_ABORT, &ireq->flags) && 2666726980d5SJeff Skirvin !test_and_set_bit(IREQ_TC_ABORT_POSTED, &ireq->flags)) { 2667726980d5SJeff Skirvin /* Utilize the original post context command and or in the 2668726980d5SJeff Skirvin * POST_TC_ABORT request sub-type. 2669cc9203bfSDan Williams */ 2670726980d5SJeff Skirvin sci_controller_post_request( 2671726980d5SJeff Skirvin ihost, ireq->post_context | 2672726980d5SJeff Skirvin SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT); 2673726980d5SJeff Skirvin } 2674726980d5SJeff Skirvin return status; 2675cc9203bfSDan Williams } 2676cc9203bfSDan Williams 2677cc9203bfSDan Williams /** 267889a7301fSDan Williams * sci_controller_complete_io() - This method will perform core specific 2679cc9203bfSDan Williams * completion operations for an IO request. After this method is invoked, 2680cc9203bfSDan Williams * the user should consider the IO request as invalid until it is properly 2681cc9203bfSDan Williams * reused (i.e. re-constructed). 268289a7301fSDan Williams * @ihost: The handle to the controller object for which to complete the 2683cc9203bfSDan Williams * IO request. 268489a7301fSDan Williams * @idev: The handle to the remote device object for which to complete 2685cc9203bfSDan Williams * the IO request. 268689a7301fSDan Williams * @ireq: the handle to the io request object to complete. 2687cc9203bfSDan Williams */ 268889a7301fSDan Williams enum sci_status sci_controller_complete_io(struct isci_host *ihost, 268978a6f06eSDan Williams struct isci_remote_device *idev, 26905076a1a9SDan Williams struct isci_request *ireq) 2691cc9203bfSDan Williams { 2692cc9203bfSDan Williams enum sci_status status; 2693cc9203bfSDan Williams u16 index; 2694cc9203bfSDan Williams 2695d9dcb4baSDan Williams switch (ihost->sm.current_state_id) { 2696e301370aSEdmund Nadolski case SCIC_STOPPING: 2697cc9203bfSDan Williams /* XXX: Implement this function */ 2698cc9203bfSDan Williams return SCI_FAILURE; 2699e301370aSEdmund Nadolski case SCIC_READY: 270089a7301fSDan Williams status = sci_remote_device_complete_io(ihost, idev, ireq); 2701cc9203bfSDan Williams if (status != SCI_SUCCESS) 2702cc9203bfSDan Williams return status; 2703cc9203bfSDan Williams 27045076a1a9SDan Williams index = ISCI_TAG_TCI(ireq->io_tag); 27055076a1a9SDan Williams clear_bit(IREQ_ACTIVE, &ireq->flags); 2706*14aaa9f0SJeff Skirvin if (test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags)) 2707*14aaa9f0SJeff Skirvin wake_up_all(&ihost->eventq); 2708cc9203bfSDan Williams return SCI_SUCCESS; 2709cc9203bfSDan Williams default: 271014e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 271114e99b4aSDan Williams __func__, ihost->sm.current_state_id); 2712cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 2713cc9203bfSDan Williams } 2714cc9203bfSDan Williams 2715cc9203bfSDan Williams } 2716cc9203bfSDan Williams 271789a7301fSDan Williams enum sci_status sci_controller_continue_io(struct isci_request *ireq) 2718cc9203bfSDan Williams { 2719d9dcb4baSDan Williams struct isci_host *ihost = ireq->owning_controller; 2720cc9203bfSDan Williams 2721d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_READY) { 272214e99b4aSDan Williams dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n", 272314e99b4aSDan Williams __func__, ihost->sm.current_state_id); 2724cc9203bfSDan Williams return SCI_FAILURE_INVALID_STATE; 2725cc9203bfSDan Williams } 2726cc9203bfSDan Williams 27275076a1a9SDan Williams set_bit(IREQ_ACTIVE, &ireq->flags); 272834a99158SDan Williams sci_controller_post_request(ihost, ireq->post_context); 2729cc9203bfSDan Williams return SCI_SUCCESS; 2730cc9203bfSDan Williams } 2731cc9203bfSDan Williams 2732cc9203bfSDan Williams /** 273389a7301fSDan Williams * sci_controller_start_task() - This method is called by the SCIC user to 2734cc9203bfSDan Williams * send/start a framework task management request. 2735cc9203bfSDan Williams * @controller: the handle to the controller object for which to start the task 2736cc9203bfSDan Williams * management request. 2737cc9203bfSDan Williams * @remote_device: the handle to the remote device object for which to start 2738cc9203bfSDan Williams * the task management request. 2739cc9203bfSDan Williams * @task_request: the handle to the task request object to start. 2740cc9203bfSDan Williams */ 274189a7301fSDan Williams enum sci_task_status sci_controller_start_task(struct isci_host *ihost, 274278a6f06eSDan Williams struct isci_remote_device *idev, 27435076a1a9SDan Williams struct isci_request *ireq) 2744cc9203bfSDan Williams { 2745cc9203bfSDan Williams enum sci_status status; 2746cc9203bfSDan Williams 2747d9dcb4baSDan Williams if (ihost->sm.current_state_id != SCIC_READY) { 2748d9dcb4baSDan Williams dev_warn(&ihost->pdev->dev, 2749cc9203bfSDan Williams "%s: SCIC Controller starting task from invalid " 2750cc9203bfSDan Williams "state\n", 2751cc9203bfSDan Williams __func__); 2752cc9203bfSDan Williams return SCI_TASK_FAILURE_INVALID_STATE; 2753cc9203bfSDan Williams } 2754cc9203bfSDan Williams 275589a7301fSDan Williams status = sci_remote_device_start_task(ihost, idev, ireq); 2756cc9203bfSDan Williams switch (status) { 2757cc9203bfSDan Williams case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS: 2758db056250SDan Williams set_bit(IREQ_ACTIVE, &ireq->flags); 2759cc9203bfSDan Williams 2760cc9203bfSDan Williams /* 2761cc9203bfSDan Williams * We will let framework know this task request started successfully, 2762cc9203bfSDan Williams * although core is still woring on starting the request (to post tc when 2763cc9203bfSDan Williams * RNC is resumed.) 2764cc9203bfSDan Williams */ 2765cc9203bfSDan Williams return SCI_SUCCESS; 2766cc9203bfSDan Williams case SCI_SUCCESS: 2767db056250SDan Williams set_bit(IREQ_ACTIVE, &ireq->flags); 276834a99158SDan Williams sci_controller_post_request(ihost, ireq->post_context); 2769cc9203bfSDan Williams break; 2770cc9203bfSDan Williams default: 2771cc9203bfSDan Williams break; 2772cc9203bfSDan Williams } 2773cc9203bfSDan Williams 2774cc9203bfSDan Williams return status; 2775cc9203bfSDan Williams } 2776ad4f4c1dSDan Williams 2777ad4f4c1dSDan Williams static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data) 2778ad4f4c1dSDan Williams { 2779ad4f4c1dSDan Williams int d; 2780ad4f4c1dSDan Williams 2781ad4f4c1dSDan Williams /* no support for TX_GP_CFG */ 2782ad4f4c1dSDan Williams if (reg_index == 0) 2783ad4f4c1dSDan Williams return -EINVAL; 2784ad4f4c1dSDan Williams 2785ad4f4c1dSDan Williams for (d = 0; d < isci_gpio_count(ihost); d++) { 2786ad4f4c1dSDan Williams u32 val = 0x444; /* all ODx.n clear */ 2787ad4f4c1dSDan Williams int i; 2788ad4f4c1dSDan Williams 2789ad4f4c1dSDan Williams for (i = 0; i < 3; i++) { 2790ad4f4c1dSDan Williams int bit = (i << 2) + 2; 2791ad4f4c1dSDan Williams 2792ad4f4c1dSDan Williams bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i), 2793ad4f4c1dSDan Williams write_data, reg_index, 2794ad4f4c1dSDan Williams reg_count); 2795ad4f4c1dSDan Williams if (bit < 0) 2796ad4f4c1dSDan Williams break; 2797ad4f4c1dSDan Williams 2798ad4f4c1dSDan Williams /* if od is set, clear the 'invert' bit */ 2799ad4f4c1dSDan Williams val &= ~(bit << ((i << 2) + 2)); 2800ad4f4c1dSDan Williams } 2801ad4f4c1dSDan Williams 2802ad4f4c1dSDan Williams if (i < 3) 2803ad4f4c1dSDan Williams break; 2804ad4f4c1dSDan Williams writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]); 2805ad4f4c1dSDan Williams } 2806ad4f4c1dSDan Williams 2807ad4f4c1dSDan Williams /* unless reg_index is > 1, we should always be able to write at 2808ad4f4c1dSDan Williams * least one register 2809ad4f4c1dSDan Williams */ 2810ad4f4c1dSDan Williams return d > 0; 2811ad4f4c1dSDan Williams } 2812ad4f4c1dSDan Williams 2813ad4f4c1dSDan Williams int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index, 2814ad4f4c1dSDan Williams u8 reg_count, u8 *write_data) 2815ad4f4c1dSDan Williams { 2816ad4f4c1dSDan Williams struct isci_host *ihost = sas_ha->lldd_ha; 2817ad4f4c1dSDan Williams int written; 2818ad4f4c1dSDan Williams 2819ad4f4c1dSDan Williams switch (reg_type) { 2820ad4f4c1dSDan Williams case SAS_GPIO_REG_TX_GP: 2821ad4f4c1dSDan Williams written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data); 2822ad4f4c1dSDan Williams break; 2823ad4f4c1dSDan Williams default: 2824ad4f4c1dSDan Williams written = -EINVAL; 2825ad4f4c1dSDan Williams } 2826ad4f4c1dSDan Williams 2827ad4f4c1dSDan Williams return written; 2828ad4f4c1dSDan Williams } 2829