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
90621120caSJeff 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 dma_addr_t request_daddr;
995076a1a9SDan Williams dma_addr_t zero_scatter_daddr;
1005076a1a9SDan Williams unsigned int num_sg_entries;
1015076a1a9SDan Williams /* Note: "io_request_completion" is completed in two different ways
1025076a1a9SDan Williams * depending on whether this is a TMF or regular request.
1035076a1a9SDan Williams * - TMF requests are completed in the thread that started them;
1045076a1a9SDan Williams * - regular requests are completed in the request completion callback
1055076a1a9SDan Williams * function.
1065076a1a9SDan Williams * This difference in operation allows the aborter of a TMF request
1075076a1a9SDan Williams * to be sure that once the TMF request completes, the I/O that the
1085076a1a9SDan Williams * TMF was aborting is guaranteed to have completed.
1095076a1a9SDan Williams *
1105076a1a9SDan Williams * XXX kill io_request_completion
111f1f52e75SDan Williams */
1125076a1a9SDan Williams struct completion *io_request_completion;
113e301370aSEdmund Nadolski struct sci_base_state_machine sm;
114d9dcb4baSDan Williams struct isci_host *owning_controller;
11578a6f06eSDan Williams struct isci_remote_device *target_device;
116f1f52e75SDan Williams u16 io_tag;
117c79dd80dSDan Williams enum sas_protocol protocol;
1185076a1a9SDan Williams u32 scu_status; /* hardware result */
1195076a1a9SDan Williams u32 sci_status; /* upper layer disposition */
120f1f52e75SDan Williams u32 post_context;
121312e0c24SDan Williams struct scu_task_context *tc;
122f1f52e75SDan Williams /* could be larger with sg chaining */
1237c78da31SDan Williams #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
124f1f52e75SDan Williams struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
1255076a1a9SDan Williams /* This field is a pointer to the stored rx frame data. It is used in
126e301370aSEdmund Nadolski * STP internal requests and SMP response frames. If this field is
127e301370aSEdmund Nadolski * non-NULL the saved frame must be released on IO request completion.
128f1f52e75SDan Williams */
129f1f52e75SDan Williams u32 saved_rx_frame_index;
130f1f52e75SDan Williams
131f1f52e75SDan Williams union {
132f1f52e75SDan Williams struct {
133f1f52e75SDan Williams union {
134f1f52e75SDan Williams struct ssp_cmd_iu cmd;
135f1f52e75SDan Williams struct ssp_task_iu tmf;
136f1f52e75SDan Williams };
137f1f52e75SDan Williams union {
138f1f52e75SDan Williams struct ssp_response_iu rsp;
139f1f52e75SDan Williams u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
140f1f52e75SDan Williams };
141f1f52e75SDan Williams } ssp;
142f1f52e75SDan Williams struct {
143ba7cb223SDan Williams struct isci_stp_request req;
144f1f52e75SDan Williams struct host_to_dev_fis cmd;
145f1f52e75SDan Williams struct dev_to_host_fis rsp;
146f1f52e75SDan Williams } stp;
147f1f52e75SDan Williams };
148f1f52e75SDan Williams };
149f1f52e75SDan Williams
to_ireq(struct isci_stp_request * stp_req)1505076a1a9SDan Williams static inline struct isci_request *to_ireq(struct isci_stp_request *stp_req)
151f1f52e75SDan Williams {
1525076a1a9SDan Williams struct isci_request *ireq;
153f1f52e75SDan Williams
1545076a1a9SDan Williams ireq = container_of(stp_req, typeof(*ireq), stp.req);
15567ea838dSDan Williams return ireq;
15667ea838dSDan Williams }
15767ea838dSDan Williams
1586f231ddaSDan Williams /**
159d7a0ccddSDan Williams * enum sci_base_request_states - request state machine states
160f1f52e75SDan Williams *
161d7a0ccddSDan Williams * @SCI_REQ_INIT: Simply the initial state for the base request state machine.
162f1f52e75SDan Williams *
163d7a0ccddSDan Williams * @SCI_REQ_CONSTRUCTED: This state indicates that the request has been
164d7a0ccddSDan Williams * constructed. This state is entered from the INITIAL state.
165d7a0ccddSDan Williams *
166d7a0ccddSDan Williams * @SCI_REQ_STARTED: This state indicates that the request has been started.
167f1f52e75SDan Williams * This state is entered from the CONSTRUCTED state.
168d7a0ccddSDan Williams *
169d7a0ccddSDan Williams * @SCI_REQ_STP_UDMA_WAIT_TC_COMP:
170d7a0ccddSDan Williams * @SCI_REQ_STP_UDMA_WAIT_D2H:
171d7a0ccddSDan Williams * @SCI_REQ_STP_NON_DATA_WAIT_H2D:
172d7a0ccddSDan Williams * @SCI_REQ_STP_NON_DATA_WAIT_D2H:
173d7a0ccddSDan Williams *
174d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_WAIT_H2D: While in this state the IO request object is
175d7a0ccddSDan Williams * waiting for the TC completion notification for the H2D Register FIS
176d7a0ccddSDan Williams *
177d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_WAIT_FRAME: While in this state the IO request object is
178d7a0ccddSDan Williams * waiting for either a PIO Setup FIS or a D2H register FIS. The type of frame
179d7a0ccddSDan Williams * received is based on the result of the prior frame and line conditions.
180d7a0ccddSDan Williams *
181d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_DATA_IN: While in this state the IO request object is
182d7a0ccddSDan Williams * waiting for a DATA frame from the device.
183d7a0ccddSDan Williams *
184d7a0ccddSDan Williams * @SCI_REQ_STP_PIO_DATA_OUT: While in this state the IO request object is
185d7a0ccddSDan Williams * waiting to transmit the next data frame to the device.
186d7a0ccddSDan Williams *
187d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_H2D: While in this state the IO request object is
188d7a0ccddSDan Williams * waiting for the TC completion notification for the H2D Register FIS
189d7a0ccddSDan Williams *
190d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_PIO_SETUP: While in this state the IO request object is
191d7a0ccddSDan Williams * waiting for either a PIO Setup.
192d7a0ccddSDan Williams *
193d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_D2H: The non-data IO transit to this state in this state
194d7a0ccddSDan Williams * after receiving TC completion. While in this state IO request object is
195d7a0ccddSDan Williams * waiting for D2H status frame as UF.
196d7a0ccddSDan Williams *
197d7a0ccddSDan Williams * @SCI_REQ_ATAPI_WAIT_TC_COMP: When transmitting raw frames hardware reports
198d7a0ccddSDan Williams * task context completion after every frame submission, so in the
199d7a0ccddSDan Williams * non-accelerated case we need to expect the completion for the "cdb" frame.
200d7a0ccddSDan Williams *
201d7a0ccddSDan Williams * @SCI_REQ_TASK_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that
202d7a0ccddSDan Williams * the started raw task management request is waiting for the transmission of
203d7a0ccddSDan Williams * the initial frame (i.e. command, task, etc.).
204d7a0ccddSDan Williams *
205d7a0ccddSDan Williams * @SCI_REQ_TASK_WAIT_TC_RESP: This sub-state indicates that the started task
206d7a0ccddSDan Williams * management request is waiting for the reception of an unsolicited frame
207d7a0ccddSDan Williams * (i.e. response IU).
208d7a0ccddSDan Williams *
209d7a0ccddSDan Williams * @SCI_REQ_SMP_WAIT_RESP: This sub-state indicates that the started task
210d7a0ccddSDan Williams * management request is waiting for the reception of an unsolicited frame
211d7a0ccddSDan Williams * (i.e. response IU).
212d7a0ccddSDan Williams *
213d7a0ccddSDan Williams * @SCI_REQ_SMP_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that
214d7a0ccddSDan Williams * the started SMP request is waiting for the transmission of the initial frame
215d7a0ccddSDan Williams * (i.e. command, task, etc.).
216d7a0ccddSDan Williams *
217d7a0ccddSDan Williams * @SCI_REQ_COMPLETED: This state indicates that the request has completed.
218d7a0ccddSDan Williams * This state is entered from the STARTED state. This state is entered from the
219d7a0ccddSDan Williams * ABORTING state.
220d7a0ccddSDan Williams *
221d7a0ccddSDan Williams * @SCI_REQ_ABORTING: This state indicates that the request is in the process
222d7a0ccddSDan Williams * of being terminated/aborted. This state is entered from the CONSTRUCTED
223d7a0ccddSDan Williams * state. This state is entered from the STARTED state.
224d7a0ccddSDan Williams *
225d7a0ccddSDan Williams * @SCI_REQ_FINAL: Simply the final state for the base request state machine.
226f1f52e75SDan Williams */
227d7a0ccddSDan Williams #define REQUEST_STATES {\
228d7a0ccddSDan Williams C(REQ_INIT),\
229d7a0ccddSDan Williams C(REQ_CONSTRUCTED),\
230d7a0ccddSDan Williams C(REQ_STARTED),\
231d7a0ccddSDan Williams C(REQ_STP_UDMA_WAIT_TC_COMP),\
232d7a0ccddSDan Williams C(REQ_STP_UDMA_WAIT_D2H),\
233d7a0ccddSDan Williams C(REQ_STP_NON_DATA_WAIT_H2D),\
234d7a0ccddSDan Williams C(REQ_STP_NON_DATA_WAIT_D2H),\
235d7a0ccddSDan Williams C(REQ_STP_PIO_WAIT_H2D),\
236d7a0ccddSDan Williams C(REQ_STP_PIO_WAIT_FRAME),\
237d7a0ccddSDan Williams C(REQ_STP_PIO_DATA_IN),\
238d7a0ccddSDan Williams C(REQ_STP_PIO_DATA_OUT),\
239d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_H2D),\
240d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_PIO_SETUP),\
241d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_D2H),\
242d7a0ccddSDan Williams C(REQ_ATAPI_WAIT_TC_COMP),\
243d7a0ccddSDan Williams C(REQ_TASK_WAIT_TC_COMP),\
244d7a0ccddSDan Williams C(REQ_TASK_WAIT_TC_RESP),\
245d7a0ccddSDan Williams C(REQ_SMP_WAIT_RESP),\
246d7a0ccddSDan Williams C(REQ_SMP_WAIT_TC_COMP),\
247d7a0ccddSDan Williams C(REQ_COMPLETED),\
248d7a0ccddSDan Williams C(REQ_ABORTING),\
249d7a0ccddSDan Williams C(REQ_FINAL),\
250d7a0ccddSDan Williams }
251d7a0ccddSDan Williams #undef C
252d7a0ccddSDan Williams #define C(a) SCI_##a
253d7a0ccddSDan Williams enum sci_base_request_states REQUEST_STATES;
254d7a0ccddSDan Williams #undef C
255d7a0ccddSDan Williams const char *req_state_name(enum sci_base_request_states state);
256f1f52e75SDan Williams
25789a7301fSDan Williams enum sci_status sci_request_start(struct isci_request *ireq);
25889a7301fSDan Williams enum sci_status sci_io_request_terminate(struct isci_request *ireq);
259e301370aSEdmund Nadolski enum sci_status
26089a7301fSDan Williams sci_io_request_event_handler(struct isci_request *ireq,
261f1f52e75SDan Williams u32 event_code);
262e301370aSEdmund Nadolski enum sci_status
26389a7301fSDan Williams sci_io_request_frame_handler(struct isci_request *ireq,
264f1f52e75SDan Williams u32 frame_index);
265e301370aSEdmund Nadolski enum sci_status
26689a7301fSDan Williams sci_task_request_terminate(struct isci_request *ireq);
267e301370aSEdmund Nadolski extern enum sci_status
26889a7301fSDan Williams sci_request_complete(struct isci_request *ireq);
269e301370aSEdmund Nadolski extern enum sci_status
27089a7301fSDan Williams sci_io_request_tc_completion(struct isci_request *ireq, u32 code);
271f1f52e75SDan Williams
272f1f52e75SDan Williams /* XXX open code in caller */
273e301370aSEdmund Nadolski static inline dma_addr_t
sci_io_request_get_dma_addr(struct isci_request * ireq,void * virt_addr)27489a7301fSDan Williams sci_io_request_get_dma_addr(struct isci_request *ireq, void *virt_addr)
275f1f52e75SDan Williams {
276f1f52e75SDan Williams
277f1f52e75SDan Williams char *requested_addr = (char *)virt_addr;
278f1f52e75SDan Williams char *base_addr = (char *)ireq;
279f1f52e75SDan Williams
280f1f52e75SDan Williams BUG_ON(requested_addr < base_addr);
281f1f52e75SDan Williams BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
282f1f52e75SDan Williams
283f1f52e75SDan Williams return ireq->request_daddr + (requested_addr - base_addr);
284f1f52e75SDan Williams }
285f1f52e75SDan Williams
286e301370aSEdmund Nadolski #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
2876f231ddaSDan Williams
288e301370aSEdmund Nadolski #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
2896f231ddaSDan Williams
290db056250SDan Williams struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
2916f231ddaSDan Williams struct isci_tmf *isci_tmf,
292db056250SDan Williams u16 tag);
293209fae14SDan Williams int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
294*c39d5aa4SJohn Garry struct sas_task *task, struct isci_request *ireq);
295*c39d5aa4SJohn Garry struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
296*c39d5aa4SJohn Garry struct sas_task *task,
297*c39d5aa4SJohn Garry u16 tag);
298e301370aSEdmund Nadolski enum sci_status
29989a7301fSDan Williams sci_task_request_construct(struct isci_host *ihost,
30078a6f06eSDan Williams struct isci_remote_device *idev,
301f1f52e75SDan Williams u16 io_tag,
3025076a1a9SDan Williams struct isci_request *ireq);
30343a5ab15SDan Williams enum sci_status sci_task_request_construct_ssp(struct isci_request *ireq);
30489a7301fSDan Williams void sci_smp_request_copy_response(struct isci_request *ireq);
3059274f45eSJeff Skirvin
isci_task_is_ncq_recovery(struct sas_task * task)3069274f45eSJeff Skirvin static inline int isci_task_is_ncq_recovery(struct sas_task *task)
3079274f45eSJeff Skirvin {
3089274f45eSJeff Skirvin return (sas_protocol_ata(task->task_proto) &&
3099274f45eSJeff Skirvin task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
3109274f45eSJeff Skirvin task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
3119274f45eSJeff Skirvin
3129274f45eSJeff Skirvin }
3136f231ddaSDan Williams #endif /* !defined(_ISCI_REQUEST_H_) */
314