Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[pandora-kernel.git] / drivers / scsi / isci / request.h
1 /*
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
56 #ifndef _ISCI_REQUEST_H_
57 #define _ISCI_REQUEST_H_
58
59 #include "isci.h"
60 #include "host.h"
61 #include "scu_task_context.h"
62
63 /**
64  * struct isci_request_status - This enum defines the possible states of an I/O
65  *    request.
66  *
67  *
68  */
69 enum isci_request_status {
70         unallocated = 0x00,
71         allocated   = 0x01,
72         started     = 0x02,
73         completed   = 0x03,
74         aborting    = 0x04,
75         aborted     = 0x05,
76         terminating = 0x06,
77         dead        = 0x07
78 };
79
80 enum task_type {
81         io_task  = 0,
82         tmf_task = 1
83 };
84
85 enum sci_request_protocol {
86         SCIC_NO_PROTOCOL,
87         SCIC_SMP_PROTOCOL,
88         SCIC_SSP_PROTOCOL,
89         SCIC_STP_PROTOCOL
90 }; /* XXX remove me, use sas_task.{dev|task_proto} instead */;
91
92 /**
93  * isci_stp_request - extra request infrastructure to handle pio/atapi protocol
94  * @pio_len - number of bytes requested at PIO setup
95  * @status - pio setup ending status value to tell us if we need
96  *           to wait for another fis or if the transfer is complete.  Upon
97  *           receipt of a d2h fis this will be the status field of that fis.
98  * @sgl - track pio transfer progress as we iterate through the sgl
99  */
100 struct isci_stp_request {
101         u32 pio_len;
102         u8 status;
103
104         struct isci_stp_pio_sgl {
105                 int index;
106                 u8 set;
107                 u32 offset;
108         } sgl;
109 };
110
111 struct isci_request {
112         enum isci_request_status status;
113         #define IREQ_COMPLETE_IN_TARGET 0
114         #define IREQ_TERMINATED 1
115         #define IREQ_TMF 2
116         #define IREQ_ACTIVE 3
117         unsigned long flags;
118         /* XXX kill ttype and ttype_ptr, allocate full sas_task */
119         enum task_type ttype;
120         union ttype_ptr_union {
121                 struct sas_task *io_task_ptr;   /* When ttype==io_task  */
122                 struct isci_tmf *tmf_task_ptr;  /* When ttype==tmf_task */
123         } ttype_ptr;
124         struct isci_host *isci_host;
125         /* For use in the requests_to_{complete|abort} lists: */
126         struct list_head completed_node;
127         /* For use in the reqs_in_process list: */
128         struct list_head dev_node;
129         spinlock_t state_lock;
130         dma_addr_t request_daddr;
131         dma_addr_t zero_scatter_daddr;
132         unsigned int num_sg_entries;
133         /* Note: "io_request_completion" is completed in two different ways
134          * depending on whether this is a TMF or regular request.
135          * - TMF requests are completed in the thread that started them;
136          * - regular requests are completed in the request completion callback
137          *   function.
138          * This difference in operation allows the aborter of a TMF request
139          * to be sure that once the TMF request completes, the I/O that the
140          * TMF was aborting is guaranteed to have completed.
141          *
142          * XXX kill io_request_completion
143          */
144         struct completion *io_request_completion;
145         struct sci_base_state_machine sm;
146         struct isci_host *owning_controller;
147         struct isci_remote_device *target_device;
148         u16 io_tag;
149         enum sci_request_protocol protocol;
150         u32 scu_status; /* hardware result */
151         u32 sci_status; /* upper layer disposition */
152         u32 post_context;
153         struct scu_task_context *tc;
154         /* could be larger with sg chaining */
155         #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
156         struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
157         /* This field is a pointer to the stored rx frame data.  It is used in
158          * STP internal requests and SMP response frames.  If this field is
159          * non-NULL the saved frame must be released on IO request completion.
160          */
161         u32 saved_rx_frame_index;
162
163         union {
164                 struct {
165                         union {
166                                 struct ssp_cmd_iu cmd;
167                                 struct ssp_task_iu tmf;
168                         };
169                         union {
170                                 struct ssp_response_iu rsp;
171                                 u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
172                         };
173                 } ssp;
174                 struct {
175                         struct isci_stp_request req;
176                         struct host_to_dev_fis cmd;
177                         struct dev_to_host_fis rsp;
178                 } stp;
179         };
180 };
181
182 static inline struct isci_request *to_ireq(struct isci_stp_request *stp_req)
183 {
184         struct isci_request *ireq;
185
186         ireq = container_of(stp_req, typeof(*ireq), stp.req);
187         return ireq;
188 }
189
190 /**
191  * enum sci_base_request_states - This enumeration depicts all the states for
192  *    the common request state machine.
193  *
194  *
195  */
196 enum sci_base_request_states {
197         /*
198          * Simply the initial state for the base request state machine.
199          */
200         SCI_REQ_INIT,
201
202         /*
203          * This state indicates that the request has been constructed.
204          * This state is entered from the INITIAL state.
205          */
206         SCI_REQ_CONSTRUCTED,
207
208         /*
209          * This state indicates that the request has been started. This state
210          * is entered from the CONSTRUCTED state.
211          */
212         SCI_REQ_STARTED,
213
214         SCI_REQ_STP_UDMA_WAIT_TC_COMP,
215         SCI_REQ_STP_UDMA_WAIT_D2H,
216
217         SCI_REQ_STP_NON_DATA_WAIT_H2D,
218         SCI_REQ_STP_NON_DATA_WAIT_D2H,
219
220         SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED,
221         SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG,
222         SCI_REQ_STP_SOFT_RESET_WAIT_D2H,
223
224         /*
225          * While in this state the IO request object is waiting for the TC
226          * completion notification for the H2D Register FIS
227          */
228         SCI_REQ_STP_PIO_WAIT_H2D,
229
230         /*
231          * While in this state the IO request object is waiting for either a
232          * PIO Setup FIS or a D2H register FIS.  The type of frame received is
233          * based on the result of the prior frame and line conditions.
234          */
235         SCI_REQ_STP_PIO_WAIT_FRAME,
236
237         /*
238          * While in this state the IO request object is waiting for a DATA
239          * frame from the device.
240          */
241         SCI_REQ_STP_PIO_DATA_IN,
242
243         /*
244          * While in this state the IO request object is waiting to transmit
245          * the next data frame to the device.
246          */
247         SCI_REQ_STP_PIO_DATA_OUT,
248
249         /*
250          * While in this state the IO request object is waiting for the TC
251          * completion notification for the H2D Register FIS
252          */
253         SCI_REQ_ATAPI_WAIT_H2D,
254
255         /*
256          * While in this state the IO request object is waiting for either a
257          * PIO Setup.
258          */
259         SCI_REQ_ATAPI_WAIT_PIO_SETUP,
260
261         /*
262          * The non-data IO transit to this state in this state after receiving
263          * TC completion. While in this state IO request object is waiting for
264          * D2H status frame as UF.
265          */
266         SCI_REQ_ATAPI_WAIT_D2H,
267
268         /*
269          * When transmitting raw frames hardware reports task context completion
270          * after every frame submission, so in the non-accelerated case we need
271          * to expect the completion for the "cdb" frame.
272          */
273         SCI_REQ_ATAPI_WAIT_TC_COMP,
274
275         /*
276          * The AWAIT_TC_COMPLETION sub-state indicates that the started raw
277          * task management request is waiting for the transmission of the
278          * initial frame (i.e. command, task, etc.).
279          */
280         SCI_REQ_TASK_WAIT_TC_COMP,
281
282         /*
283          * This sub-state indicates that the started task management request
284          * is waiting for the reception of an unsolicited frame
285          * (i.e. response IU).
286          */
287         SCI_REQ_TASK_WAIT_TC_RESP,
288
289         /*
290          * This sub-state indicates that the started task management request
291          * is waiting for the reception of an unsolicited frame
292          * (i.e. response IU).
293          */
294         SCI_REQ_SMP_WAIT_RESP,
295
296         /*
297          * The AWAIT_TC_COMPLETION sub-state indicates that the started SMP
298          * request is waiting for the transmission of the initial frame
299          * (i.e. command, task, etc.).
300          */
301         SCI_REQ_SMP_WAIT_TC_COMP,
302
303         /*
304          * This state indicates that the request has completed.
305          * This state is entered from the STARTED state. This state is entered
306          * from the ABORTING state.
307          */
308         SCI_REQ_COMPLETED,
309
310         /*
311          * This state indicates that the request is in the process of being
312          * terminated/aborted.
313          * This state is entered from the CONSTRUCTED state.
314          * This state is entered from the STARTED state.
315          */
316         SCI_REQ_ABORTING,
317
318         /*
319          * Simply the final state for the base request state machine.
320          */
321         SCI_REQ_FINAL,
322 };
323
324 enum sci_status sci_request_start(struct isci_request *ireq);
325 enum sci_status sci_io_request_terminate(struct isci_request *ireq);
326 enum sci_status
327 sci_io_request_event_handler(struct isci_request *ireq,
328                                   u32 event_code);
329 enum sci_status
330 sci_io_request_frame_handler(struct isci_request *ireq,
331                                   u32 frame_index);
332 enum sci_status
333 sci_task_request_terminate(struct isci_request *ireq);
334 extern enum sci_status
335 sci_request_complete(struct isci_request *ireq);
336 extern enum sci_status
337 sci_io_request_tc_completion(struct isci_request *ireq, u32 code);
338
339 /* XXX open code in caller */
340 static inline dma_addr_t
341 sci_io_request_get_dma_addr(struct isci_request *ireq, void *virt_addr)
342 {
343
344         char *requested_addr = (char *)virt_addr;
345         char *base_addr = (char *)ireq;
346
347         BUG_ON(requested_addr < base_addr);
348         BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
349
350         return ireq->request_daddr + (requested_addr - base_addr);
351 }
352
353 /**
354  * isci_request_change_state() - This function sets the status of the request
355  *    object.
356  * @request: This parameter points to the isci_request object
357  * @status: This Parameter is the new status of the object
358  *
359  */
360 static inline enum isci_request_status
361 isci_request_change_state(struct isci_request *isci_request,
362                           enum isci_request_status status)
363 {
364         enum isci_request_status old_state;
365         unsigned long flags;
366
367         dev_dbg(&isci_request->isci_host->pdev->dev,
368                 "%s: isci_request = %p, state = 0x%x\n",
369                 __func__,
370                 isci_request,
371                 status);
372
373         BUG_ON(isci_request == NULL);
374
375         spin_lock_irqsave(&isci_request->state_lock, flags);
376         old_state = isci_request->status;
377         isci_request->status = status;
378         spin_unlock_irqrestore(&isci_request->state_lock, flags);
379
380         return old_state;
381 }
382
383 /**
384  * isci_request_change_started_to_newstate() - This function sets the status of
385  *    the request object.
386  * @request: This parameter points to the isci_request object
387  * @status: This Parameter is the new status of the object
388  *
389  * state previous to any change.
390  */
391 static inline enum isci_request_status
392 isci_request_change_started_to_newstate(struct isci_request *isci_request,
393                                         struct completion *completion_ptr,
394                                         enum isci_request_status newstate)
395 {
396         enum isci_request_status old_state;
397         unsigned long flags;
398
399         spin_lock_irqsave(&isci_request->state_lock, flags);
400
401         old_state = isci_request->status;
402
403         if (old_state == started || old_state == aborting) {
404                 BUG_ON(isci_request->io_request_completion != NULL);
405
406                 isci_request->io_request_completion = completion_ptr;
407                 isci_request->status = newstate;
408         }
409
410         spin_unlock_irqrestore(&isci_request->state_lock, flags);
411
412         dev_dbg(&isci_request->isci_host->pdev->dev,
413                 "%s: isci_request = %p, old_state = 0x%x\n",
414                 __func__,
415                 isci_request,
416                 old_state);
417
418         return old_state;
419 }
420
421 /**
422  * isci_request_change_started_to_aborted() - This function sets the status of
423  *    the request object.
424  * @request: This parameter points to the isci_request object
425  * @completion_ptr: This parameter is saved as the kernel completion structure
426  *    signalled when the old request completes.
427  *
428  * state previous to any change.
429  */
430 static inline enum isci_request_status
431 isci_request_change_started_to_aborted(struct isci_request *isci_request,
432                                        struct completion *completion_ptr)
433 {
434         return isci_request_change_started_to_newstate(isci_request,
435                                                        completion_ptr,
436                                                        aborted);
437 }
438
439 #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
440
441 #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
442
443 struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
444                                                struct isci_tmf *isci_tmf,
445                                                u16 tag);
446 int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
447                          struct sas_task *task, u16 tag);
448 void isci_terminate_pending_requests(struct isci_host *ihost,
449                                      struct isci_remote_device *idev);
450 enum sci_status
451 sci_task_request_construct(struct isci_host *ihost,
452                             struct isci_remote_device *idev,
453                             u16 io_tag,
454                             struct isci_request *ireq);
455 enum sci_status
456 sci_task_request_construct_ssp(struct isci_request *ireq);
457 enum sci_status
458 sci_task_request_construct_sata(struct isci_request *ireq);
459 void sci_smp_request_copy_response(struct isci_request *ireq);
460
461 static inline int isci_task_is_ncq_recovery(struct sas_task *task)
462 {
463         return (sas_protocol_ata(task->task_proto) &&
464                 task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
465                 task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
466
467 }
468
469 #endif /* !defined(_ISCI_REQUEST_H_) */