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 */ 556f231ddaSDan Williams 560d84366fSDan Williams #ifndef _ISCI_REQUEST_H_ 576f231ddaSDan Williams #define _ISCI_REQUEST_H_ 586f231ddaSDan Williams 596f231ddaSDan Williams #include "isci.h" 60ce2b3261SDan Williams #include "host.h" 61f1f52e75SDan Williams #include "scu_task_context.h" 626f231ddaSDan Williams 636f231ddaSDan Williams /** 64ba7cb223SDan Williams * isci_stp_request - extra request infrastructure to handle pio/atapi protocol 65ba7cb223SDan Williams * @pio_len - number of bytes requested at PIO setup 66ba7cb223SDan Williams * @status - pio setup ending status value to tell us if we need 67ba7cb223SDan Williams * to wait for another fis or if the transfer is complete. Upon 68ba7cb223SDan Williams * receipt of a d2h fis this will be the status field of that fis. 69ba7cb223SDan Williams * @sgl - track pio transfer progress as we iterate through the sgl 705dec6f4eSDan Williams */ 71ba7cb223SDan Williams struct isci_stp_request { 72ba7cb223SDan Williams u32 pio_len; 73ba7cb223SDan Williams u8 status; 745dec6f4eSDan Williams 75ba7cb223SDan Williams struct isci_stp_pio_sgl { 76ba7cb223SDan Williams int index; 77ba7cb223SDan Williams u8 set; 78ba7cb223SDan Williams u32 offset; 79ba7cb223SDan Williams } sgl; 805dec6f4eSDan Williams }; 815dec6f4eSDan Williams 825076a1a9SDan Williams struct isci_request { 835076a1a9SDan Williams #define IREQ_COMPLETE_IN_TARGET 0 845076a1a9SDan Williams #define IREQ_TERMINATED 1 855076a1a9SDan Williams #define IREQ_TMF 2 865076a1a9SDan Williams #define IREQ_ACTIVE 3 87726980d5SJeff Skirvin #define IREQ_PENDING_ABORT 4 /* Set == device was not suspended yet */ 88726980d5SJeff Skirvin #define IREQ_TC_ABORT_POSTED 5 8914aaa9f0SJeff Skirvin #define IREQ_ABORT_PATH_ACTIVE 6 90*621120caSJeff Skirvin #define IREQ_NO_AUTO_FREE_TAG 7 /* Set when being explicitly managed */ 915076a1a9SDan Williams unsigned long flags; 925076a1a9SDan Williams /* XXX kill ttype and ttype_ptr, allocate full sas_task */ 935076a1a9SDan Williams union ttype_ptr_union { 945076a1a9SDan Williams struct sas_task *io_task_ptr; /* When ttype==io_task */ 955076a1a9SDan Williams struct isci_tmf *tmf_task_ptr; /* When ttype==tmf_task */ 965076a1a9SDan Williams } ttype_ptr; 975076a1a9SDan Williams struct isci_host *isci_host; 985076a1a9SDan Williams /* For use in the requests_to_{complete|abort} lists: */ 995076a1a9SDan Williams struct list_head completed_node; 1005076a1a9SDan Williams dma_addr_t request_daddr; 1015076a1a9SDan Williams dma_addr_t zero_scatter_daddr; 1025076a1a9SDan Williams unsigned int num_sg_entries; 1035076a1a9SDan Williams /* Note: "io_request_completion" is completed in two different ways 1045076a1a9SDan Williams * depending on whether this is a TMF or regular request. 1055076a1a9SDan Williams * - TMF requests are completed in the thread that started them; 1065076a1a9SDan Williams * - regular requests are completed in the request completion callback 1075076a1a9SDan Williams * function. 1085076a1a9SDan Williams * This difference in operation allows the aborter of a TMF request 1095076a1a9SDan Williams * to be sure that once the TMF request completes, the I/O that the 1105076a1a9SDan Williams * TMF was aborting is guaranteed to have completed. 1115076a1a9SDan Williams * 1125076a1a9SDan Williams * XXX kill io_request_completion 113f1f52e75SDan Williams */ 1145076a1a9SDan Williams struct completion *io_request_completion; 115e301370aSEdmund Nadolski struct sci_base_state_machine sm; 116d9dcb4baSDan Williams struct isci_host *owning_controller; 11778a6f06eSDan Williams struct isci_remote_device *target_device; 118f1f52e75SDan Williams u16 io_tag; 119c79dd80dSDan Williams enum sas_protocol protocol; 1205076a1a9SDan Williams u32 scu_status; /* hardware result */ 1215076a1a9SDan Williams u32 sci_status; /* upper layer disposition */ 122f1f52e75SDan Williams u32 post_context; 123312e0c24SDan Williams struct scu_task_context *tc; 124f1f52e75SDan Williams /* could be larger with sg chaining */ 1257c78da31SDan Williams #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2) 126f1f52e75SDan Williams struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32))); 1275076a1a9SDan Williams /* This field is a pointer to the stored rx frame data. It is used in 128e301370aSEdmund Nadolski * STP internal requests and SMP response frames. If this field is 129e301370aSEdmund Nadolski * non-NULL the saved frame must be released on IO request completion. 130f1f52e75SDan Williams */ 131f1f52e75SDan Williams u32 saved_rx_frame_index; 132f1f52e75SDan Williams 133f1f52e75SDan Williams union { 134f1f52e75SDan Williams struct { 135f1f52e75SDan Williams union { 136f1f52e75SDan Williams struct ssp_cmd_iu cmd; 137f1f52e75SDan Williams struct ssp_task_iu tmf; 138f1f52e75SDan Williams }; 139f1f52e75SDan Williams union { 140f1f52e75SDan Williams struct ssp_response_iu rsp; 141f1f52e75SDan Williams u8 rsp_buf[SSP_RESP_IU_MAX_SIZE]; 142f1f52e75SDan Williams }; 143f1f52e75SDan Williams } ssp; 144f1f52e75SDan Williams struct { 145ba7cb223SDan Williams struct isci_stp_request req; 146f1f52e75SDan Williams struct host_to_dev_fis cmd; 147f1f52e75SDan Williams struct dev_to_host_fis rsp; 148f1f52e75SDan Williams } stp; 149f1f52e75SDan Williams }; 150f1f52e75SDan Williams }; 151f1f52e75SDan Williams 1525076a1a9SDan Williams static inline struct isci_request *to_ireq(struct isci_stp_request *stp_req) 153f1f52e75SDan Williams { 1545076a1a9SDan Williams struct isci_request *ireq; 155f1f52e75SDan Williams 1565076a1a9SDan Williams ireq = container_of(stp_req, typeof(*ireq), stp.req); 15767ea838dSDan Williams return ireq; 15867ea838dSDan Williams } 15967ea838dSDan Williams 1606f231ddaSDan Williams /** 161d7a0ccddSDan Williams * enum sci_base_request_states - request state machine states 162f1f52e75SDan Williams * 163d7a0ccddSDan Williams * @SCI_REQ_INIT: Simply the initial state for the base request state machine. 164f1f52e75SDan Williams * 165d7a0ccddSDan Williams * @SCI_REQ_CONSTRUCTED: This state indicates that the request has been 166d7a0ccddSDan Williams * constructed. This state is entered from the INITIAL state. 167d7a0ccddSDan Williams * 168d7a0ccddSDan Williams * @SCI_REQ_STARTED: This state indicates that the request has been started. 169f1f52e75SDan Williams * This state is entered from the CONSTRUCTED state. 170d7a0ccddSDan Williams * 171d7a0ccddSDan Williams * @SCI_REQ_STP_UDMA_WAIT_TC_COMP: 172d7a0ccddSDan Williams * @SCI_REQ_STP_UDMA_WAIT_D2H: 173d7a0ccddSDan Williams * @SCI_REQ_STP_NON_DATA_WAIT_H2D: 174d7a0ccddSDan Williams * @SCI_REQ_STP_NON_DATA_WAIT_D2H: 175d7a0ccddSDan Williams * 176d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_WAIT_H2D: While in this state the IO request object is 177d7a0ccddSDan Williams * waiting for the TC completion notification for the H2D Register FIS 178d7a0ccddSDan Williams * 179d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_WAIT_FRAME: While in this state the IO request object is 180d7a0ccddSDan Williams * waiting for either a PIO Setup FIS or a D2H register FIS. The type of frame 181d7a0ccddSDan Williams * received is based on the result of the prior frame and line conditions. 182d7a0ccddSDan Williams * 183d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_DATA_IN: While in this state the IO request object is 184d7a0ccddSDan Williams * waiting for a DATA frame from the device. 185d7a0ccddSDan Williams * 186d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_DATA_OUT: While in this state the IO request object is 187d7a0ccddSDan Williams * waiting to transmit the next data frame to the device. 188d7a0ccddSDan Williams * 189d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_H2D: While in this state the IO request object is 190d7a0ccddSDan Williams * waiting for the TC completion notification for the H2D Register FIS 191d7a0ccddSDan Williams * 192d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_PIO_SETUP: While in this state the IO request object is 193d7a0ccddSDan Williams * waiting for either a PIO Setup. 194d7a0ccddSDan Williams * 195d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_D2H: The non-data IO transit to this state in this state 196d7a0ccddSDan Williams * after receiving TC completion. While in this state IO request object is 197d7a0ccddSDan Williams * waiting for D2H status frame as UF. 198d7a0ccddSDan Williams * 199d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_TC_COMP: When transmitting raw frames hardware reports 200d7a0ccddSDan Williams * task context completion after every frame submission, so in the 201d7a0ccddSDan Williams * non-accelerated case we need to expect the completion for the "cdb" frame. 202d7a0ccddSDan Williams * 203d7a0ccddSDan Williams * @SCI_REQ_TASK_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that 204d7a0ccddSDan Williams * the started raw task management request is waiting for the transmission of 205d7a0ccddSDan Williams * the initial frame (i.e. command, task, etc.). 206d7a0ccddSDan Williams * 207d7a0ccddSDan Williams * @SCI_REQ_TASK_WAIT_TC_RESP: This sub-state indicates that the started task 208d7a0ccddSDan Williams * management request is waiting for the reception of an unsolicited frame 209d7a0ccddSDan Williams * (i.e. response IU). 210d7a0ccddSDan Williams * 211d7a0ccddSDan Williams * @SCI_REQ_SMP_WAIT_RESP: This sub-state indicates that the started task 212d7a0ccddSDan Williams * management request is waiting for the reception of an unsolicited frame 213d7a0ccddSDan Williams * (i.e. response IU). 214d7a0ccddSDan Williams * 215d7a0ccddSDan Williams * @SCI_REQ_SMP_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that 216d7a0ccddSDan Williams * the started SMP request is waiting for the transmission of the initial frame 217d7a0ccddSDan Williams * (i.e. command, task, etc.). 218d7a0ccddSDan Williams * 219d7a0ccddSDan Williams * @SCI_REQ_COMPLETED: This state indicates that the request has completed. 220d7a0ccddSDan Williams * This state is entered from the STARTED state. This state is entered from the 221d7a0ccddSDan Williams * ABORTING state. 222d7a0ccddSDan Williams * 223d7a0ccddSDan Williams * @SCI_REQ_ABORTING: This state indicates that the request is in the process 224d7a0ccddSDan Williams * of being terminated/aborted. This state is entered from the CONSTRUCTED 225d7a0ccddSDan Williams * state. This state is entered from the STARTED state. 226d7a0ccddSDan Williams * 227d7a0ccddSDan Williams * @SCI_REQ_FINAL: Simply the final state for the base request state machine. 228f1f52e75SDan Williams */ 229d7a0ccddSDan Williams #define REQUEST_STATES {\ 230d7a0ccddSDan Williams C(REQ_INIT),\ 231d7a0ccddSDan Williams C(REQ_CONSTRUCTED),\ 232d7a0ccddSDan Williams C(REQ_STARTED),\ 233d7a0ccddSDan Williams C(REQ_STP_UDMA_WAIT_TC_COMP),\ 234d7a0ccddSDan Williams C(REQ_STP_UDMA_WAIT_D2H),\ 235d7a0ccddSDan Williams C(REQ_STP_NON_DATA_WAIT_H2D),\ 236d7a0ccddSDan Williams C(REQ_STP_NON_DATA_WAIT_D2H),\ 237d7a0ccddSDan Williams C(REQ_STP_PIO_WAIT_H2D),\ 238d7a0ccddSDan Williams C(REQ_STP_PIO_WAIT_FRAME),\ 239d7a0ccddSDan Williams C(REQ_STP_PIO_DATA_IN),\ 240d7a0ccddSDan Williams C(REQ_STP_PIO_DATA_OUT),\ 241d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_H2D),\ 242d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_PIO_SETUP),\ 243d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_D2H),\ 244d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_TC_COMP),\ 245d7a0ccddSDan Williams C(REQ_TASK_WAIT_TC_COMP),\ 246d7a0ccddSDan Williams C(REQ_TASK_WAIT_TC_RESP),\ 247d7a0ccddSDan Williams C(REQ_SMP_WAIT_RESP),\ 248d7a0ccddSDan Williams C(REQ_SMP_WAIT_TC_COMP),\ 249d7a0ccddSDan Williams C(REQ_COMPLETED),\ 250d7a0ccddSDan Williams C(REQ_ABORTING),\ 251d7a0ccddSDan Williams C(REQ_FINAL),\ 252d7a0ccddSDan Williams } 253d7a0ccddSDan Williams #undef C 254d7a0ccddSDan Williams #define C(a) SCI_##a 255d7a0ccddSDan Williams enum sci_base_request_states REQUEST_STATES; 256d7a0ccddSDan Williams #undef C 257d7a0ccddSDan Williams const char *req_state_name(enum sci_base_request_states state); 258f1f52e75SDan Williams 25989a7301fSDan Williams enum sci_status sci_request_start(struct isci_request *ireq); 26089a7301fSDan Williams enum sci_status sci_io_request_terminate(struct isci_request *ireq); 261e301370aSEdmund Nadolski enum sci_status 26289a7301fSDan Williams sci_io_request_event_handler(struct isci_request *ireq, 263f1f52e75SDan Williams u32 event_code); 264e301370aSEdmund Nadolski enum sci_status 26589a7301fSDan Williams sci_io_request_frame_handler(struct isci_request *ireq, 266f1f52e75SDan Williams u32 frame_index); 267e301370aSEdmund Nadolski enum sci_status 26889a7301fSDan Williams sci_task_request_terminate(struct isci_request *ireq); 269e301370aSEdmund Nadolski extern enum sci_status 27089a7301fSDan Williams sci_request_complete(struct isci_request *ireq); 271e301370aSEdmund Nadolski extern enum sci_status 27289a7301fSDan Williams sci_io_request_tc_completion(struct isci_request *ireq, u32 code); 273f1f52e75SDan Williams 274f1f52e75SDan Williams /* XXX open code in caller */ 275e301370aSEdmund Nadolski static inline dma_addr_t 27689a7301fSDan Williams sci_io_request_get_dma_addr(struct isci_request *ireq, void *virt_addr) 277f1f52e75SDan Williams { 278f1f52e75SDan Williams 279f1f52e75SDan Williams char *requested_addr = (char *)virt_addr; 280f1f52e75SDan Williams char *base_addr = (char *)ireq; 281f1f52e75SDan Williams 282f1f52e75SDan Williams BUG_ON(requested_addr < base_addr); 283f1f52e75SDan Williams BUG_ON((requested_addr - base_addr) >= sizeof(*ireq)); 284f1f52e75SDan Williams 285f1f52e75SDan Williams return ireq->request_daddr + (requested_addr - base_addr); 286f1f52e75SDan Williams } 287f1f52e75SDan Williams 288e301370aSEdmund Nadolski #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr) 2896f231ddaSDan Williams 290e301370aSEdmund Nadolski #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr) 2916f231ddaSDan Williams 292db056250SDan Williams struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost, 2936f231ddaSDan Williams struct isci_tmf *isci_tmf, 294db056250SDan Williams u16 tag); 295209fae14SDan Williams int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev, 296db056250SDan Williams struct sas_task *task, u16 tag); 297e301370aSEdmund Nadolski enum sci_status 29889a7301fSDan Williams sci_task_request_construct(struct isci_host *ihost, 29978a6f06eSDan Williams struct isci_remote_device *idev, 300f1f52e75SDan Williams u16 io_tag, 3015076a1a9SDan Williams struct isci_request *ireq); 30243a5ab15SDan Williams enum sci_status sci_task_request_construct_ssp(struct isci_request *ireq); 30389a7301fSDan Williams void sci_smp_request_copy_response(struct isci_request *ireq); 3049274f45eSJeff Skirvin 3059274f45eSJeff Skirvin static inline int isci_task_is_ncq_recovery(struct sas_task *task) 3069274f45eSJeff Skirvin { 3079274f45eSJeff Skirvin return (sas_protocol_ata(task->task_proto) && 3089274f45eSJeff Skirvin task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT && 3099274f45eSJeff Skirvin task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ); 3109274f45eSJeff Skirvin 3119274f45eSJeff Skirvin } 3126f231ddaSDan Williams #endif /* !defined(_ISCI_REQUEST_H_) */ 313