blob: 8d55f78010aa869798ffb7aec718e661c09706be [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
Dan Williams0d843662011-05-08 01:56:57 -070056#ifndef _ISCI_REQUEST_H_
Dan Williams6f231dd2011-07-02 22:56:22 -070057#define _ISCI_REQUEST_H_
58
59#include "isci.h"
Dan Williamsce2b3262011-05-08 15:49:15 -070060#include "host.h"
Dan Williamsf1f52e72011-05-10 02:28:45 -070061#include "scu_task_context.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070062
63/**
64 * struct isci_request_status - This enum defines the possible states of an I/O
65 * request.
66 *
67 *
68 */
69enum isci_request_status {
70 unallocated = 0x00,
71 allocated = 0x01,
72 started = 0x02,
73 completed = 0x03,
74 aborting = 0x04,
75 aborted = 0x05,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -080076 terminating = 0x06,
77 dead = 0x07
Dan Williams6f231dd2011-07-02 22:56:22 -070078};
79
Dan Williamsba7cb222011-06-27 11:56:41 -070080/**
81 * isci_stp_request - extra request infrastructure to handle pio/atapi protocol
82 * @pio_len - number of bytes requested at PIO setup
83 * @status - pio setup ending status value to tell us if we need
Dan Williams5076a1a2011-06-27 14:57:03 -070084 * to wait for another fis or if the transfer is complete. Upon
Dan Williamsba7cb222011-06-27 11:56:41 -070085 * receipt of a d2h fis this will be the status field of that fis.
86 * @sgl - track pio transfer progress as we iterate through the sgl
Dan Williamsba7cb222011-06-27 11:56:41 -070087 */
88struct isci_stp_request {
89 u32 pio_len;
90 u8 status;
Dan Williams5dec6f42011-05-10 02:28:49 -070091
Dan Williamsba7cb222011-06-27 11:56:41 -070092 struct isci_stp_pio_sgl {
93 int index;
94 u8 set;
95 u32 offset;
96 } sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -070097};
98
Dan Williams5076a1a2011-06-27 14:57:03 -070099struct isci_request {
100 enum isci_request_status status;
101 #define IREQ_COMPLETE_IN_TARGET 0
102 #define IREQ_TERMINATED 1
103 #define IREQ_TMF 2
104 #define IREQ_ACTIVE 3
Jeff Skirvin726980d2012-03-08 22:41:50 -0800105 #define IREQ_PENDING_ABORT 4 /* Set == device was not suspended yet */
106 #define IREQ_TC_ABORT_POSTED 5
Dan Williams5076a1a2011-06-27 14:57:03 -0700107 unsigned long flags;
108 /* XXX kill ttype and ttype_ptr, allocate full sas_task */
Dan Williams5076a1a2011-06-27 14:57:03 -0700109 union ttype_ptr_union {
110 struct sas_task *io_task_ptr; /* When ttype==io_task */
111 struct isci_tmf *tmf_task_ptr; /* When ttype==tmf_task */
112 } ttype_ptr;
113 struct isci_host *isci_host;
114 /* For use in the requests_to_{complete|abort} lists: */
115 struct list_head completed_node;
116 /* For use in the reqs_in_process list: */
117 struct list_head dev_node;
118 spinlock_t state_lock;
119 dma_addr_t request_daddr;
120 dma_addr_t zero_scatter_daddr;
121 unsigned int num_sg_entries;
122 /* Note: "io_request_completion" is completed in two different ways
123 * depending on whether this is a TMF or regular request.
124 * - TMF requests are completed in the thread that started them;
125 * - regular requests are completed in the request completion callback
126 * function.
127 * This difference in operation allows the aborter of a TMF request
128 * to be sure that once the TMF request completes, the I/O that the
129 * TMF was aborting is guaranteed to have completed.
130 *
131 * XXX kill io_request_completion
Dan Williamsf1f52e72011-05-10 02:28:45 -0700132 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700133 struct completion *io_request_completion;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000134 struct sci_base_state_machine sm;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700135 struct isci_host *owning_controller;
Dan Williams78a6f062011-06-30 16:31:37 -0700136 struct isci_remote_device *target_device;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700137 u16 io_tag;
Dan Williamsc79dd802012-02-01 00:44:14 -0800138 enum sas_protocol protocol;
Dan Williams5076a1a2011-06-27 14:57:03 -0700139 u32 scu_status; /* hardware result */
140 u32 sci_status; /* upper layer disposition */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700141 u32 post_context;
Dan Williams312e0c22011-06-28 13:47:09 -0700142 struct scu_task_context *tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700143 /* could be larger with sg chaining */
Dan Williams7c78da32011-06-01 16:00:01 -0700144 #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700145 struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
Dan Williams5076a1a2011-06-27 14:57:03 -0700146 /* This field is a pointer to the stored rx frame data. It is used in
Edmund Nadolskie3013702011-06-02 00:10:43 +0000147 * STP internal requests and SMP response frames. If this field is
148 * non-NULL the saved frame must be released on IO request completion.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700149 */
150 u32 saved_rx_frame_index;
151
Dan Williamsf1f52e72011-05-10 02:28:45 -0700152 union {
153 struct {
154 union {
155 struct ssp_cmd_iu cmd;
156 struct ssp_task_iu tmf;
157 };
158 union {
159 struct ssp_response_iu rsp;
160 u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
161 };
162 } ssp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700163 struct {
Dan Williamsba7cb222011-06-27 11:56:41 -0700164 struct isci_stp_request req;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700165 struct host_to_dev_fis cmd;
166 struct dev_to_host_fis rsp;
167 } stp;
168 };
Dan Williamsf1f52e72011-05-10 02:28:45 -0700169};
170
Dan Williams5076a1a2011-06-27 14:57:03 -0700171static inline struct isci_request *to_ireq(struct isci_stp_request *stp_req)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700172{
Dan Williams5076a1a2011-06-27 14:57:03 -0700173 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700174
Dan Williams5076a1a2011-06-27 14:57:03 -0700175 ireq = container_of(stp_req, typeof(*ireq), stp.req);
Dan Williams67ea8382011-05-08 11:47:15 -0700176 return ireq;
177}
178
Dan Williams6f231dd2011-07-02 22:56:22 -0700179/**
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800180 * enum sci_base_request_states - request state machine states
Dan Williamsf1f52e72011-05-10 02:28:45 -0700181 *
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800182 * @SCI_REQ_INIT: Simply the initial state for the base request state machine.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700183 *
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800184 * @SCI_REQ_CONSTRUCTED: This state indicates that the request has been
185 * constructed. This state is entered from the INITIAL state.
186 *
187 * @SCI_REQ_STARTED: This state indicates that the request has been started.
188 * This state is entered from the CONSTRUCTED state.
189 *
190 * @SCI_REQ_STP_UDMA_WAIT_TC_COMP:
191 * @SCI_REQ_STP_UDMA_WAIT_D2H:
192 * @SCI_REQ_STP_NON_DATA_WAIT_H2D:
193 * @SCI_REQ_STP_NON_DATA_WAIT_D2H:
194 *
195 * @SCI_REQ_STP_PIO_WAIT_H2D: While in this state the IO request object is
196 * waiting for the TC completion notification for the H2D Register FIS
197 *
198 * @SCI_REQ_STP_PIO_WAIT_FRAME: While in this state the IO request object is
199 * waiting for either a PIO Setup FIS or a D2H register FIS. The type of frame
200 * received is based on the result of the prior frame and line conditions.
201 *
202 * @SCI_REQ_STP_PIO_DATA_IN: While in this state the IO request object is
203 * waiting for a DATA frame from the device.
204 *
205 * @SCI_REQ_STP_PIO_DATA_OUT: While in this state the IO request object is
206 * waiting to transmit the next data frame to the device.
207 *
208 * @SCI_REQ_ATAPI_WAIT_H2D: While in this state the IO request object is
209 * waiting for the TC completion notification for the H2D Register FIS
210 *
211 * @SCI_REQ_ATAPI_WAIT_PIO_SETUP: While in this state the IO request object is
212 * waiting for either a PIO Setup.
213 *
214 * @SCI_REQ_ATAPI_WAIT_D2H: The non-data IO transit to this state in this state
215 * after receiving TC completion. While in this state IO request object is
216 * waiting for D2H status frame as UF.
217 *
218 * @SCI_REQ_ATAPI_WAIT_TC_COMP: When transmitting raw frames hardware reports
219 * task context completion after every frame submission, so in the
220 * non-accelerated case we need to expect the completion for the "cdb" frame.
221 *
222 * @SCI_REQ_TASK_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that
223 * the started raw task management request is waiting for the transmission of
224 * the initial frame (i.e. command, task, etc.).
225 *
226 * @SCI_REQ_TASK_WAIT_TC_RESP: This sub-state indicates that the started task
227 * management request is waiting for the reception of an unsolicited frame
228 * (i.e. response IU).
229 *
230 * @SCI_REQ_SMP_WAIT_RESP: This sub-state indicates that the started task
231 * management request is waiting for the reception of an unsolicited frame
232 * (i.e. response IU).
233 *
234 * @SCI_REQ_SMP_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that
235 * the started SMP request is waiting for the transmission of the initial frame
236 * (i.e. command, task, etc.).
237 *
238 * @SCI_REQ_COMPLETED: This state indicates that the request has completed.
239 * This state is entered from the STARTED state. This state is entered from the
240 * ABORTING state.
241 *
242 * @SCI_REQ_ABORTING: This state indicates that the request is in the process
243 * of being terminated/aborted. This state is entered from the CONSTRUCTED
244 * state. This state is entered from the STARTED state.
245 *
246 * @SCI_REQ_FINAL: Simply the final state for the base request state machine.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700247 */
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800248#define REQUEST_STATES {\
249 C(REQ_INIT),\
250 C(REQ_CONSTRUCTED),\
251 C(REQ_STARTED),\
252 C(REQ_STP_UDMA_WAIT_TC_COMP),\
253 C(REQ_STP_UDMA_WAIT_D2H),\
254 C(REQ_STP_NON_DATA_WAIT_H2D),\
255 C(REQ_STP_NON_DATA_WAIT_D2H),\
256 C(REQ_STP_PIO_WAIT_H2D),\
257 C(REQ_STP_PIO_WAIT_FRAME),\
258 C(REQ_STP_PIO_DATA_IN),\
259 C(REQ_STP_PIO_DATA_OUT),\
260 C(REQ_ATAPI_WAIT_H2D),\
261 C(REQ_ATAPI_WAIT_PIO_SETUP),\
262 C(REQ_ATAPI_WAIT_D2H),\
263 C(REQ_ATAPI_WAIT_TC_COMP),\
264 C(REQ_TASK_WAIT_TC_COMP),\
265 C(REQ_TASK_WAIT_TC_RESP),\
266 C(REQ_SMP_WAIT_RESP),\
267 C(REQ_SMP_WAIT_TC_COMP),\
268 C(REQ_COMPLETED),\
269 C(REQ_ABORTING),\
270 C(REQ_FINAL),\
271 }
272#undef C
273#define C(a) SCI_##a
274enum sci_base_request_states REQUEST_STATES;
275#undef C
276const char *req_state_name(enum sci_base_request_states state);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700277
Dan Williams89a73012011-06-30 19:14:33 -0700278enum sci_status sci_request_start(struct isci_request *ireq);
279enum sci_status sci_io_request_terminate(struct isci_request *ireq);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000280enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700281sci_io_request_event_handler(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000282 u32 event_code);
283enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700284sci_io_request_frame_handler(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000285 u32 frame_index);
286enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700287sci_task_request_terminate(struct isci_request *ireq);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000288extern enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700289sci_request_complete(struct isci_request *ireq);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000290extern enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700291sci_io_request_tc_completion(struct isci_request *ireq, u32 code);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700292
Dan Williamsf1f52e72011-05-10 02:28:45 -0700293/* XXX open code in caller */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000294static inline dma_addr_t
Dan Williams89a73012011-06-30 19:14:33 -0700295sci_io_request_get_dma_addr(struct isci_request *ireq, void *virt_addr)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700296{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700297
298 char *requested_addr = (char *)virt_addr;
299 char *base_addr = (char *)ireq;
300
301 BUG_ON(requested_addr < base_addr);
302 BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
303
304 return ireq->request_daddr + (requested_addr - base_addr);
305}
306
307/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700308 * isci_request_change_state() - This function sets the status of the request
309 * object.
310 * @request: This parameter points to the isci_request object
311 * @status: This Parameter is the new status of the object
312 *
313 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000314static inline enum isci_request_status
315isci_request_change_state(struct isci_request *isci_request,
316 enum isci_request_status status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700317{
318 enum isci_request_status old_state;
319 unsigned long flags;
320
321 dev_dbg(&isci_request->isci_host->pdev->dev,
322 "%s: isci_request = %p, state = 0x%x\n",
323 __func__,
324 isci_request,
325 status);
326
327 BUG_ON(isci_request == NULL);
328
329 spin_lock_irqsave(&isci_request->state_lock, flags);
330 old_state = isci_request->status;
331 isci_request->status = status;
332 spin_unlock_irqrestore(&isci_request->state_lock, flags);
333
334 return old_state;
335}
336
337/**
338 * isci_request_change_started_to_newstate() - This function sets the status of
339 * the request object.
340 * @request: This parameter points to the isci_request object
341 * @status: This Parameter is the new status of the object
342 *
343 * state previous to any change.
344 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000345static inline enum isci_request_status
346isci_request_change_started_to_newstate(struct isci_request *isci_request,
347 struct completion *completion_ptr,
348 enum isci_request_status newstate)
Dan Williams6f231dd2011-07-02 22:56:22 -0700349{
350 enum isci_request_status old_state;
351 unsigned long flags;
352
Dan Williams6f231dd2011-07-02 22:56:22 -0700353 spin_lock_irqsave(&isci_request->state_lock, flags);
354
355 old_state = isci_request->status;
356
Jeff Skirvinf219f012011-03-31 13:10:34 -0700357 if (old_state == started || old_state == aborting) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700358 BUG_ON(isci_request->io_request_completion != NULL);
359
360 isci_request->io_request_completion = completion_ptr;
361 isci_request->status = newstate;
362 }
Edmund Nadolskie3013702011-06-02 00:10:43 +0000363
Dan Williams6f231dd2011-07-02 22:56:22 -0700364 spin_unlock_irqrestore(&isci_request->state_lock, flags);
365
366 dev_dbg(&isci_request->isci_host->pdev->dev,
367 "%s: isci_request = %p, old_state = 0x%x\n",
368 __func__,
369 isci_request,
370 old_state);
371
372 return old_state;
373}
374
375/**
376 * isci_request_change_started_to_aborted() - This function sets the status of
377 * the request object.
378 * @request: This parameter points to the isci_request object
379 * @completion_ptr: This parameter is saved as the kernel completion structure
380 * signalled when the old request completes.
381 *
382 * state previous to any change.
383 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000384static inline enum isci_request_status
385isci_request_change_started_to_aborted(struct isci_request *isci_request,
386 struct completion *completion_ptr)
Dan Williams6f231dd2011-07-02 22:56:22 -0700387{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000388 return isci_request_change_started_to_newstate(isci_request,
389 completion_ptr,
390 aborted);
Dan Williams6f231dd2011-07-02 22:56:22 -0700391}
Dan Williams6f231dd2011-07-02 22:56:22 -0700392
Edmund Nadolskie3013702011-06-02 00:10:43 +0000393#define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
Dan Williams6f231dd2011-07-02 22:56:22 -0700394
Edmund Nadolskie3013702011-06-02 00:10:43 +0000395#define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
Dan Williams6f231dd2011-07-02 22:56:22 -0700396
Dan Williamsdb056252011-06-17 14:18:39 -0700397struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
398 struct isci_tmf *isci_tmf,
399 u16 tag);
Dan Williams209fae12011-06-13 17:39:44 -0700400int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
Dan Williamsdb056252011-06-17 14:18:39 -0700401 struct sas_task *task, u16 tag);
Dan Williamsddcc7e32011-06-17 10:40:43 -0700402void isci_terminate_pending_requests(struct isci_host *ihost,
403 struct isci_remote_device *idev);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000404enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700405sci_task_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -0700406 struct isci_remote_device *idev,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000407 u16 io_tag,
Dan Williams5076a1a2011-06-27 14:57:03 -0700408 struct isci_request *ireq);
Dan Williams43a5ab12011-12-08 23:20:44 -0800409enum sci_status sci_task_request_construct_ssp(struct isci_request *ireq);
Dan Williams89a73012011-06-30 19:14:33 -0700410void sci_smp_request_copy_response(struct isci_request *ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700411
412static inline int isci_task_is_ncq_recovery(struct sas_task *task)
413{
414 return (sas_protocol_ata(task->task_proto) &&
James Bottomleya5ec7f862011-07-03 14:14:45 -0500415 task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
416 task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
Jeff Skirvin9274f452011-06-23 17:09:02 -0700417
418}
Dan Williams6f231dd2011-07-02 22:56:22 -0700419#endif /* !defined(_ISCI_REQUEST_H_) */