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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
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.
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.
56 #ifndef _ISCI_REQUEST_H_
57 #define _ISCI_REQUEST_H_
61 #include "scu_task_context.h"
64 * struct isci_request_status - This enum defines the possible states of an I/O
69 enum isci_request_status {
85 enum sci_request_protocol {
90 }; /* XXX remove me, use sas_task.{dev|task_proto} instead */;
92 struct scic_sds_stp_request {
98 struct scic_sds_stp_pio_request {
100 * Total transfer for the entire PIO request recorded
101 * at request constuction time.
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?
107 u32 total_transfer_bytes;
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.
115 u32 current_transfer_bytes;
118 * The number of bytes requested in the in the PIO
121 u32 pio_transfer_bytes;
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.
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.
138 struct scic_sds_request_pio_sgl {
139 struct scu_sgl_element_pair *sgl_pair;
147 * The number of bytes requested in the PIO setup
148 * before CDB data frame.
150 u32 device_preferred_cdb_length;
155 struct scic_sds_request {
157 * This field contains the information for the base request state
160 struct sci_base_state_machine sm;
163 * This field simply points to the controller to which this IO request
166 struct scic_sds_controller *owning_controller;
169 * This field simply points to the remote device to which this IO
170 * request is associated.
172 struct scic_sds_remote_device *target_device;
175 * This field is utilized to determine if the SCI user is managing
176 * the IO tag for this request or if the core is managing it.
178 bool was_tag_assigned_by_user;
181 * This field indicates the IO tag for this request. The IO tag is
182 * comprised of the task_index and a sequence count. The sequence count
183 * is utilized to help identify tasks from one life to another.
188 * This field specifies the protocol being utilized for this
191 enum sci_request_protocol protocol;
194 * This field indicates the completion status taken from the SCUs
195 * completion code. It indicates the completion result for the SCU
201 * This field indicates the completion status returned to the SCI user.
202 * It indicates the users view of the io request completion.
207 * This field contains the value to be utilized when posting
208 * (e.g. Post_TC, * Post_TC_Abort) this request to the silicon.
212 struct scu_task_context *task_context_buffer;
213 struct scu_task_context tc ____cacheline_aligned;
215 /* could be larger with sg chaining */
216 #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
217 struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
220 * This field indicates if this request is a task management request or
223 bool is_task_management_request;
226 * This field is a pointer to the stored rx frame data. It is used in
227 * STP internal requests and SMP response frames. If this field is
228 * non-NULL the saved frame must be released on IO request completion.
230 * @todo In the future do we want to keep a list of RX frame buffers?
232 u32 saved_rx_frame_index;
237 struct ssp_cmd_iu cmd;
238 struct ssp_task_iu tmf;
241 struct ssp_response_iu rsp;
242 u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
251 struct scic_sds_stp_request req;
252 struct host_to_dev_fis cmd;
253 struct dev_to_host_fis rsp;
258 static inline struct scic_sds_request *to_sci_req(struct scic_sds_stp_request *stp_req)
260 struct scic_sds_request *sci_req;
262 sci_req = container_of(stp_req, typeof(*sci_req), stp.req);
266 struct isci_request {
267 enum isci_request_status status;
268 enum task_type ttype;
269 unsigned short io_tag;
270 bool complete_in_target;
273 union ttype_ptr_union {
274 struct sas_task *io_task_ptr; /* When ttype==io_task */
275 struct isci_tmf *tmf_task_ptr; /* When ttype==tmf_task */
277 struct isci_host *isci_host;
278 /* For use in the requests_to_{complete|abort} lists: */
279 struct list_head completed_node;
280 /* For use in the reqs_in_process list: */
281 struct list_head dev_node;
282 spinlock_t state_lock;
283 dma_addr_t request_daddr;
284 dma_addr_t zero_scatter_daddr;
286 unsigned int num_sg_entries; /* returned by pci_alloc_sg */
288 /** Note: "io_request_completion" is completed in two different ways
289 * depending on whether this is a TMF or regular request.
290 * - TMF requests are completed in the thread that started them;
291 * - regular requests are completed in the request completion callback
293 * This difference in operation allows the aborter of a TMF request
294 * to be sure that once the TMF request completes, the I/O that the
295 * TMF was aborting is guaranteed to have completed.
297 struct completion *io_request_completion;
298 struct scic_sds_request sci;
301 static inline struct isci_request *sci_req_to_ireq(struct scic_sds_request *sci_req)
303 struct isci_request *ireq = container_of(sci_req, typeof(*ireq), sci);
309 * enum sci_base_request_states - This enumeration depicts all the states for
310 * the common request state machine.
314 enum sci_base_request_states {
316 * Simply the initial state for the base request state machine.
321 * This state indicates that the request has been constructed.
322 * This state is entered from the INITIAL state.
327 * This state indicates that the request has been started. This state
328 * is entered from the CONSTRUCTED state.
332 SCI_REQ_STP_UDMA_WAIT_TC_COMP,
333 SCI_REQ_STP_UDMA_WAIT_D2H,
335 SCI_REQ_STP_NON_DATA_WAIT_H2D,
336 SCI_REQ_STP_NON_DATA_WAIT_D2H,
338 SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED,
339 SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG,
340 SCI_REQ_STP_SOFT_RESET_WAIT_D2H,
343 * While in this state the IO request object is waiting for the TC
344 * completion notification for the H2D Register FIS
346 SCI_REQ_STP_PIO_WAIT_H2D,
349 * While in this state the IO request object is waiting for either a
350 * PIO Setup FIS or a D2H register FIS. The type of frame received is
351 * based on the result of the prior frame and line conditions.
353 SCI_REQ_STP_PIO_WAIT_FRAME,
356 * While in this state the IO request object is waiting for a DATA
357 * frame from the device.
359 SCI_REQ_STP_PIO_DATA_IN,
362 * While in this state the IO request object is waiting to transmit
363 * the next data frame to the device.
365 SCI_REQ_STP_PIO_DATA_OUT,
368 * The AWAIT_TC_COMPLETION sub-state indicates that the started raw
369 * task management request is waiting for the transmission of the
370 * initial frame (i.e. command, task, etc.).
372 SCI_REQ_TASK_WAIT_TC_COMP,
375 * This sub-state indicates that the started task management request
376 * is waiting for the reception of an unsolicited frame
377 * (i.e. response IU).
379 SCI_REQ_TASK_WAIT_TC_RESP,
382 * This sub-state indicates that the started task management request
383 * is waiting for the reception of an unsolicited frame
384 * (i.e. response IU).
386 SCI_REQ_SMP_WAIT_RESP,
389 * The AWAIT_TC_COMPLETION sub-state indicates that the started SMP
390 * request is waiting for the transmission of the initial frame
391 * (i.e. command, task, etc.).
393 SCI_REQ_SMP_WAIT_TC_COMP,
396 * This state indicates that the request has completed.
397 * This state is entered from the STARTED state. This state is entered
398 * from the ABORTING state.
403 * This state indicates that the request is in the process of being
404 * terminated/aborted.
405 * This state is entered from the CONSTRUCTED state.
406 * This state is entered from the STARTED state.
411 * Simply the final state for the base request state machine.
417 * scic_sds_request_get_controller() -
419 * This macro will return the controller for this io request object
421 #define scic_sds_request_get_controller(sci_req) \
422 ((sci_req)->owning_controller)
425 * scic_sds_request_get_device() -
427 * This macro will return the device for this io request object
429 #define scic_sds_request_get_device(sci_req) \
430 ((sci_req)->target_device)
433 * scic_sds_request_get_port() -
435 * This macro will return the port for this io request object
437 #define scic_sds_request_get_port(sci_req) \
438 scic_sds_remote_device_get_port(scic_sds_request_get_device(sci_req))
441 * scic_sds_request_get_post_context() -
443 * This macro returns the constructed post context result for the io request.
445 #define scic_sds_request_get_post_context(sci_req) \
446 ((sci_req)->post_context)
449 * scic_sds_request_get_task_context() -
451 * This is a helper macro to return the os handle for this request object.
453 #define scic_sds_request_get_task_context(request) \
454 ((request)->task_context_buffer)
457 * scic_sds_request_set_status() -
459 * This macro will set the scu hardware status and sci request completion
460 * status for an io request.
462 #define scic_sds_request_set_status(request, scu_status_code, sci_status_code) \
464 (request)->scu_status = (scu_status_code); \
465 (request)->sci_status = (sci_status_code); \
471 * This macro zeros the hardware SGL element data
473 #define SCU_SGL_ZERO(scu_sge) \
475 (scu_sge).length = 0; \
476 (scu_sge).address_lower = 0; \
477 (scu_sge).address_upper = 0; \
478 (scu_sge).address_modifier = 0; \
484 * This macro copys the SGL Element data from the host os to the hardware SGL
487 #define SCU_SGL_COPY(scu_sge, os_sge) \
489 (scu_sge).length = sg_dma_len(sg); \
490 (scu_sge).address_upper = \
491 upper_32_bits(sg_dma_address(sg)); \
492 (scu_sge).address_lower = \
493 lower_32_bits(sg_dma_address(sg)); \
494 (scu_sge).address_modifier = 0; \
497 enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req);
498 enum sci_status scic_sds_io_request_terminate(struct scic_sds_request *sci_req);
500 scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
503 scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
506 scic_sds_task_request_terminate(struct scic_sds_request *sci_req);
507 extern enum sci_status
508 scic_sds_request_complete(struct scic_sds_request *sci_req);
509 extern enum sci_status
510 scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req, u32 code);
512 /* XXX open code in caller */
513 static inline void *scic_request_get_virt_addr(struct scic_sds_request *sci_req,
514 dma_addr_t phys_addr)
516 struct isci_request *ireq = sci_req_to_ireq(sci_req);
519 BUG_ON(phys_addr < ireq->request_daddr);
521 offset = phys_addr - ireq->request_daddr;
523 BUG_ON(offset >= sizeof(*ireq));
525 return (char *)ireq + offset;
528 /* XXX open code in caller */
529 static inline dma_addr_t
530 scic_io_request_get_dma_addr(struct scic_sds_request *sci_req, void *virt_addr)
532 struct isci_request *ireq = sci_req_to_ireq(sci_req);
534 char *requested_addr = (char *)virt_addr;
535 char *base_addr = (char *)ireq;
537 BUG_ON(requested_addr < base_addr);
538 BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
540 return ireq->request_daddr + (requested_addr - base_addr);
544 * This function gets the status of the request object.
545 * @request: This parameter points to the isci_request object
547 * status of the object as a isci_request_status enum.
549 static inline enum isci_request_status
550 isci_request_get_state(struct isci_request *isci_request)
552 BUG_ON(isci_request == NULL);
554 /*probably a bad sign... */
555 if (isci_request->status == unallocated)
556 dev_warn(&isci_request->isci_host->pdev->dev,
557 "%s: isci_request->status == unallocated\n",
560 return isci_request->status;
565 * isci_request_change_state() - This function sets the status of the request
567 * @request: This parameter points to the isci_request object
568 * @status: This Parameter is the new status of the object
571 static inline enum isci_request_status
572 isci_request_change_state(struct isci_request *isci_request,
573 enum isci_request_status status)
575 enum isci_request_status old_state;
578 dev_dbg(&isci_request->isci_host->pdev->dev,
579 "%s: isci_request = %p, state = 0x%x\n",
584 BUG_ON(isci_request == NULL);
586 spin_lock_irqsave(&isci_request->state_lock, flags);
587 old_state = isci_request->status;
588 isci_request->status = status;
589 spin_unlock_irqrestore(&isci_request->state_lock, flags);
595 * isci_request_change_started_to_newstate() - This function sets the status of
596 * the request object.
597 * @request: This parameter points to the isci_request object
598 * @status: This Parameter is the new status of the object
600 * state previous to any change.
602 static inline enum isci_request_status
603 isci_request_change_started_to_newstate(struct isci_request *isci_request,
604 struct completion *completion_ptr,
605 enum isci_request_status newstate)
607 enum isci_request_status old_state;
610 spin_lock_irqsave(&isci_request->state_lock, flags);
612 old_state = isci_request->status;
614 if (old_state == started || old_state == aborting) {
615 BUG_ON(isci_request->io_request_completion != NULL);
617 isci_request->io_request_completion = completion_ptr;
618 isci_request->status = newstate;
621 spin_unlock_irqrestore(&isci_request->state_lock, flags);
623 dev_dbg(&isci_request->isci_host->pdev->dev,
624 "%s: isci_request = %p, old_state = 0x%x\n",
633 * isci_request_change_started_to_aborted() - This function sets the status of
634 * the request object.
635 * @request: This parameter points to the isci_request object
636 * @completion_ptr: This parameter is saved as the kernel completion structure
637 * signalled when the old request completes.
639 * state previous to any change.
641 static inline enum isci_request_status
642 isci_request_change_started_to_aborted(struct isci_request *isci_request,
643 struct completion *completion_ptr)
645 return isci_request_change_started_to_newstate(isci_request,
650 * isci_request_free() - This function frees the request object.
651 * @isci_host: This parameter specifies the ISCI host object
652 * @isci_request: This parameter points to the isci_request object
655 static inline void isci_request_free(struct isci_host *isci_host,
656 struct isci_request *isci_request)
661 /* release the dma memory if we fail. */
662 dma_pool_free(isci_host->dma_pool,
664 isci_request->request_daddr);
667 #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
669 #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
671 struct isci_request *isci_request_alloc_tmf(struct isci_host *ihost,
672 struct isci_tmf *isci_tmf,
674 int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
675 struct sas_task *task, gfp_t gfp_flags);
676 void isci_terminate_pending_requests(struct isci_host *ihost,
677 struct isci_remote_device *idev);
679 scic_task_request_construct(struct scic_sds_controller *scic,
680 struct scic_sds_remote_device *sci_dev,
682 struct scic_sds_request *sci_req);
684 scic_task_request_construct_ssp(struct scic_sds_request *sci_req);
686 scic_task_request_construct_sata(struct scic_sds_request *sci_req);
688 scic_stp_io_request_set_ncq_tag(struct scic_sds_request *sci_req, u16 ncq_tag);
689 void scic_sds_smp_request_copy_response(struct scic_sds_request *sci_req);
690 #endif /* !defined(_ISCI_REQUEST_H_) */