isci: combine request flags
[profile/ivi/kernel-adaptation-intel-automotive.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 struct scic_sds_stp_request {
93         union {
94                 u32 ncq;
95
96                 u32 udma;
97
98                 struct scic_sds_stp_pio_request {
99                         /*
100                          * Total transfer for the entire PIO request recorded
101                          * at request constuction time.
102                          *
103                          * @todo Should we just decrement this value for each
104                          * byte of data transitted or received to elemenate
105                          * the current_transfer_bytes field?
106                          */
107                         u32 total_transfer_bytes;
108
109                         /*
110                          * Total number of bytes received/transmitted in data
111                          * frames since the start of the IO request.  At the
112                          * end of the IO request this should equal the
113                          * total_transfer_bytes.
114                          */
115                         u32 current_transfer_bytes;
116
117                         /*
118                          * The number of bytes requested in the in the PIO
119                          * setup.
120                          */
121                         u32 pio_transfer_bytes;
122
123                         /*
124                          * PIO Setup ending status value to tell us if we need
125                          * to wait for another FIS or if the transfer is
126                          * complete. On the receipt of a D2H FIS this will be
127                          * the status field of that FIS.
128                          */
129                         u8 ending_status;
130
131                         /*
132                          * On receipt of a D2H FIS this will be the ending
133                          * error field if the ending_status has the
134                          * SATA_STATUS_ERR bit set.
135                          */
136                         u8 ending_error;
137
138                         struct scic_sds_request_pio_sgl {
139                                 int sgl_index;
140                                 u8 sgl_set;
141                                 u32 sgl_offset;
142                         } request_current;
143                 } pio;
144
145                 struct {
146                         /*
147                          * The number of bytes requested in the PIO setup
148                          * before CDB data frame.
149                          */
150                         u32 device_preferred_cdb_length;
151                 } packet;
152         } type;
153 };
154
155 struct scic_sds_request {
156         /*
157          * This field contains the information for the base request state
158          * machine.
159          */
160         struct sci_base_state_machine sm;
161
162         /*
163          * This field simply points to the controller to which this IO request
164          * is associated.
165          */
166         struct scic_sds_controller *owning_controller;
167
168         /*
169          * This field simply points to the remote device to which this IO
170          * request is associated.
171          */
172         struct scic_sds_remote_device *target_device;
173
174         /*
175          * This field indicates the IO tag for this request.  The IO tag is
176          * comprised of the task_index and a sequence count. The sequence count
177          * is utilized to help identify tasks from one life to another.
178          */
179         u16 io_tag;
180
181         /*
182          * This field specifies the protocol being utilized for this
183          * IO request.
184          */
185         enum sci_request_protocol protocol;
186
187         /*
188          * This field indicates the completion status taken from the SCUs
189          * completion code.  It indicates the completion result for the SCU
190          * hardware.
191          */
192         u32 scu_status;
193
194         /*
195          * This field indicates the completion status returned to the SCI user.
196          * It indicates the users view of the io request completion.
197          */
198         u32 sci_status;
199
200         /*
201          * This field contains the value to be utilized when posting
202          * (e.g. Post_TC, * Post_TC_Abort) this request to the silicon.
203          */
204         u32 post_context;
205
206         struct scu_task_context *tc;
207
208         /* could be larger with sg chaining */
209         #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
210         struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
211
212         /*
213          * This field is a pointer to the stored rx frame data.  It is used in
214          * STP internal requests and SMP response frames.  If this field is
215          * non-NULL the saved frame must be released on IO request completion.
216          *
217          * @todo In the future do we want to keep a list of RX frame buffers?
218          */
219         u32 saved_rx_frame_index;
220
221         union {
222                 struct {
223                         union {
224                                 struct ssp_cmd_iu cmd;
225                                 struct ssp_task_iu tmf;
226                         };
227                         union {
228                                 struct ssp_response_iu rsp;
229                                 u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
230                         };
231                 } ssp;
232
233                 struct {
234                         struct smp_resp rsp;
235                 } smp;
236
237                 struct {
238                         struct scic_sds_stp_request req;
239                         struct host_to_dev_fis cmd;
240                         struct dev_to_host_fis rsp;
241                 } stp;
242         };
243 };
244
245 static inline struct scic_sds_request *to_sci_req(struct scic_sds_stp_request *stp_req)
246 {
247         struct scic_sds_request *sci_req;
248
249         sci_req = container_of(stp_req, typeof(*sci_req), stp.req);
250         return sci_req;
251 }
252
253 struct isci_request {
254         enum isci_request_status status;
255         enum task_type ttype;
256         unsigned short io_tag;
257         #define IREQ_COMPLETE_IN_TARGET 0
258         #define IREQ_TERMINATED 1
259         #define IREQ_TMF 2
260         unsigned long flags;
261
262         union ttype_ptr_union {
263                 struct sas_task *io_task_ptr;   /* When ttype==io_task  */
264                 struct isci_tmf *tmf_task_ptr;  /* When ttype==tmf_task */
265         } ttype_ptr;
266         struct isci_host *isci_host;
267         /* For use in the requests_to_{complete|abort} lists: */
268         struct list_head completed_node;
269         /* For use in the reqs_in_process list: */
270         struct list_head dev_node;
271         spinlock_t state_lock;
272         dma_addr_t request_daddr;
273         dma_addr_t zero_scatter_daddr;
274
275         unsigned int num_sg_entries;                    /* returned by pci_alloc_sg */
276
277         /** Note: "io_request_completion" is completed in two different ways
278          * depending on whether this is a TMF or regular request.
279          * - TMF requests are completed in the thread that started them;
280          * - regular requests are completed in the request completion callback
281          *   function.
282          * This difference in operation allows the aborter of a TMF request
283          * to be sure that once the TMF request completes, the I/O that the
284          * TMF was aborting is guaranteed to have completed.
285          */
286         struct completion *io_request_completion;
287         struct scic_sds_request sci;
288 };
289
290 static inline struct isci_request *sci_req_to_ireq(struct scic_sds_request *sci_req)
291 {
292         struct isci_request *ireq = container_of(sci_req, typeof(*ireq), sci);
293
294         return ireq;
295 }
296
297 /**
298  * enum sci_base_request_states - This enumeration depicts all the states for
299  *    the common request state machine.
300  *
301  *
302  */
303 enum sci_base_request_states {
304         /*
305          * Simply the initial state for the base request state machine.
306          */
307         SCI_REQ_INIT,
308
309         /*
310          * This state indicates that the request has been constructed.
311          * This state is entered from the INITIAL state.
312          */
313         SCI_REQ_CONSTRUCTED,
314
315         /*
316          * This state indicates that the request has been started. This state
317          * is entered from the CONSTRUCTED state.
318          */
319         SCI_REQ_STARTED,
320
321         SCI_REQ_STP_UDMA_WAIT_TC_COMP,
322         SCI_REQ_STP_UDMA_WAIT_D2H,
323
324         SCI_REQ_STP_NON_DATA_WAIT_H2D,
325         SCI_REQ_STP_NON_DATA_WAIT_D2H,
326
327         SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED,
328         SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG,
329         SCI_REQ_STP_SOFT_RESET_WAIT_D2H,
330
331         /*
332          * While in this state the IO request object is waiting for the TC
333          * completion notification for the H2D Register FIS
334          */
335         SCI_REQ_STP_PIO_WAIT_H2D,
336
337         /*
338          * While in this state the IO request object is waiting for either a
339          * PIO Setup FIS or a D2H register FIS.  The type of frame received is
340          * based on the result of the prior frame and line conditions.
341          */
342         SCI_REQ_STP_PIO_WAIT_FRAME,
343
344         /*
345          * While in this state the IO request object is waiting for a DATA
346          * frame from the device.
347          */
348         SCI_REQ_STP_PIO_DATA_IN,
349
350         /*
351          * While in this state the IO request object is waiting to transmit
352          * the next data frame to the device.
353          */
354         SCI_REQ_STP_PIO_DATA_OUT,
355
356         /*
357          * The AWAIT_TC_COMPLETION sub-state indicates that the started raw
358          * task management request is waiting for the transmission of the
359          * initial frame (i.e. command, task, etc.).
360          */
361         SCI_REQ_TASK_WAIT_TC_COMP,
362
363         /*
364          * This sub-state indicates that the started task management request
365          * is waiting for the reception of an unsolicited frame
366          * (i.e. response IU).
367          */
368         SCI_REQ_TASK_WAIT_TC_RESP,
369
370         /*
371          * This sub-state indicates that the started task management request
372          * is waiting for the reception of an unsolicited frame
373          * (i.e. response IU).
374          */
375         SCI_REQ_SMP_WAIT_RESP,
376
377         /*
378          * The AWAIT_TC_COMPLETION sub-state indicates that the started SMP
379          * request is waiting for the transmission of the initial frame
380          * (i.e. command, task, etc.).
381          */
382         SCI_REQ_SMP_WAIT_TC_COMP,
383
384         /*
385          * This state indicates that the request has completed.
386          * This state is entered from the STARTED state. This state is entered
387          * from the ABORTING state.
388          */
389         SCI_REQ_COMPLETED,
390
391         /*
392          * This state indicates that the request is in the process of being
393          * terminated/aborted.
394          * This state is entered from the CONSTRUCTED state.
395          * This state is entered from the STARTED state.
396          */
397         SCI_REQ_ABORTING,
398
399         /*
400          * Simply the final state for the base request state machine.
401          */
402         SCI_REQ_FINAL,
403 };
404
405 /**
406  * scic_sds_request_get_controller() -
407  *
408  * This macro will return the controller for this io request object
409  */
410 #define scic_sds_request_get_controller(sci_req) \
411         ((sci_req)->owning_controller)
412
413 /**
414  * scic_sds_request_get_device() -
415  *
416  * This macro will return the device for this io request object
417  */
418 #define scic_sds_request_get_device(sci_req) \
419         ((sci_req)->target_device)
420
421 /**
422  * scic_sds_request_get_port() -
423  *
424  * This macro will return the port for this io request object
425  */
426 #define scic_sds_request_get_port(sci_req)      \
427         scic_sds_remote_device_get_port(scic_sds_request_get_device(sci_req))
428
429 /**
430  * scic_sds_request_get_post_context() -
431  *
432  * This macro returns the constructed post context result for the io request.
433  */
434 #define scic_sds_request_get_post_context(sci_req)      \
435         ((sci_req)->post_context)
436
437 /**
438  * scic_sds_request_get_task_context() -
439  *
440  * This is a helper macro to return the os handle for this request object.
441  */
442 #define scic_sds_request_get_task_context(request) \
443         ((request)->task_context_buffer)
444
445 /**
446  * scic_sds_request_set_status() -
447  *
448  * This macro will set the scu hardware status and sci request completion
449  * status for an io request.
450  */
451 #define scic_sds_request_set_status(request, scu_status_code, sci_status_code) \
452         { \
453                 (request)->scu_status = (scu_status_code); \
454                 (request)->sci_status = (sci_status_code); \
455         }
456
457 enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req);
458 enum sci_status scic_sds_io_request_terminate(struct scic_sds_request *sci_req);
459 enum sci_status
460 scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
461                                   u32 event_code);
462 enum sci_status
463 scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
464                                   u32 frame_index);
465 enum sci_status
466 scic_sds_task_request_terminate(struct scic_sds_request *sci_req);
467 extern enum sci_status
468 scic_sds_request_complete(struct scic_sds_request *sci_req);
469 extern enum sci_status
470 scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req, u32 code);
471
472 /* XXX open code in caller */
473 static inline dma_addr_t
474 scic_io_request_get_dma_addr(struct scic_sds_request *sci_req, void *virt_addr)
475 {
476         struct isci_request *ireq = sci_req_to_ireq(sci_req);
477
478         char *requested_addr = (char *)virt_addr;
479         char *base_addr = (char *)ireq;
480
481         BUG_ON(requested_addr < base_addr);
482         BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
483
484         return ireq->request_daddr + (requested_addr - base_addr);
485 }
486
487 /**
488  * This function gets the status of the request object.
489  * @request: This parameter points to the isci_request object
490  *
491  * status of the object as a isci_request_status enum.
492  */
493 static inline enum isci_request_status
494 isci_request_get_state(struct isci_request *isci_request)
495 {
496         BUG_ON(isci_request == NULL);
497
498         /*probably a bad sign...        */
499         if (isci_request->status == unallocated)
500                 dev_warn(&isci_request->isci_host->pdev->dev,
501                          "%s: isci_request->status == unallocated\n",
502                          __func__);
503
504         return isci_request->status;
505 }
506
507
508 /**
509  * isci_request_change_state() - This function sets the status of the request
510  *    object.
511  * @request: This parameter points to the isci_request object
512  * @status: This Parameter is the new status of the object
513  *
514  */
515 static inline enum isci_request_status
516 isci_request_change_state(struct isci_request *isci_request,
517                           enum isci_request_status status)
518 {
519         enum isci_request_status old_state;
520         unsigned long flags;
521
522         dev_dbg(&isci_request->isci_host->pdev->dev,
523                 "%s: isci_request = %p, state = 0x%x\n",
524                 __func__,
525                 isci_request,
526                 status);
527
528         BUG_ON(isci_request == NULL);
529
530         spin_lock_irqsave(&isci_request->state_lock, flags);
531         old_state = isci_request->status;
532         isci_request->status = status;
533         spin_unlock_irqrestore(&isci_request->state_lock, flags);
534
535         return old_state;
536 }
537
538 /**
539  * isci_request_change_started_to_newstate() - This function sets the status of
540  *    the request object.
541  * @request: This parameter points to the isci_request object
542  * @status: This Parameter is the new status of the object
543  *
544  * state previous to any change.
545  */
546 static inline enum isci_request_status
547 isci_request_change_started_to_newstate(struct isci_request *isci_request,
548                                         struct completion *completion_ptr,
549                                         enum isci_request_status newstate)
550 {
551         enum isci_request_status old_state;
552         unsigned long flags;
553
554         spin_lock_irqsave(&isci_request->state_lock, flags);
555
556         old_state = isci_request->status;
557
558         if (old_state == started || old_state == aborting) {
559                 BUG_ON(isci_request->io_request_completion != NULL);
560
561                 isci_request->io_request_completion = completion_ptr;
562                 isci_request->status = newstate;
563         }
564
565         spin_unlock_irqrestore(&isci_request->state_lock, flags);
566
567         dev_dbg(&isci_request->isci_host->pdev->dev,
568                 "%s: isci_request = %p, old_state = 0x%x\n",
569                 __func__,
570                 isci_request,
571                 old_state);
572
573         return old_state;
574 }
575
576 /**
577  * isci_request_change_started_to_aborted() - This function sets the status of
578  *    the request object.
579  * @request: This parameter points to the isci_request object
580  * @completion_ptr: This parameter is saved as the kernel completion structure
581  *    signalled when the old request completes.
582  *
583  * state previous to any change.
584  */
585 static inline enum isci_request_status
586 isci_request_change_started_to_aborted(struct isci_request *isci_request,
587                                        struct completion *completion_ptr)
588 {
589         return isci_request_change_started_to_newstate(isci_request,
590                                                        completion_ptr,
591                                                        aborted);
592 }
593 /**
594  * isci_request_free() - This function frees the request object.
595  * @isci_host: This parameter specifies the ISCI host object
596  * @isci_request: This parameter points to the isci_request object
597  *
598  */
599 static inline void isci_request_free(struct isci_host *isci_host,
600                                      struct isci_request *isci_request)
601 {
602         if (!isci_request)
603                 return;
604
605         /* release the dma memory if we fail. */
606         dma_pool_free(isci_host->dma_pool,
607                       isci_request,
608                       isci_request->request_daddr);
609 }
610
611 #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
612
613 #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
614
615 struct isci_request *isci_request_alloc_tmf(struct isci_host *ihost,
616                                             struct isci_tmf *isci_tmf,
617                                             gfp_t gfp_flags);
618 int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
619                          struct sas_task *task, u16 tag, gfp_t gfp_flags);
620 void isci_terminate_pending_requests(struct isci_host *ihost,
621                                      struct isci_remote_device *idev);
622 enum sci_status
623 scic_task_request_construct(struct scic_sds_controller *scic,
624                             struct scic_sds_remote_device *sci_dev,
625                             u16 io_tag,
626                             struct scic_sds_request *sci_req);
627 enum sci_status
628 scic_task_request_construct_ssp(struct scic_sds_request *sci_req);
629 enum sci_status
630 scic_task_request_construct_sata(struct scic_sds_request *sci_req);
631 void
632 scic_stp_io_request_set_ncq_tag(struct scic_sds_request *sci_req, u16 ncq_tag);
633 void scic_sds_smp_request_copy_response(struct scic_sds_request *sci_req);
634
635 static inline int isci_task_is_ncq_recovery(struct sas_task *task)
636 {
637         return (sas_protocol_ata(task->task_proto) &&
638                 task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
639                 task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
640
641 }
642
643 #endif /* !defined(_ISCI_REQUEST_H_) */