162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
362306a36Sopenharmony_ci * redistributing this file, you may do so under either license.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * GPL LICENSE SUMMARY
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
1062306a36Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
1162306a36Sopenharmony_ci * published by the Free Software Foundation.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
1462306a36Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
1562306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1662306a36Sopenharmony_ci * General Public License for more details.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
1962306a36Sopenharmony_ci * along with this program; if not, write to the Free Software
2062306a36Sopenharmony_ci * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
2162306a36Sopenharmony_ci * The full GNU General Public License is included in this distribution
2262306a36Sopenharmony_ci * in the file called LICENSE.GPL.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * BSD LICENSE
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
2762306a36Sopenharmony_ci * All rights reserved.
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
3062306a36Sopenharmony_ci * modification, are permitted provided that the following conditions
3162306a36Sopenharmony_ci * are met:
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci *   * Redistributions of source code must retain the above copyright
3462306a36Sopenharmony_ci *     notice, this list of conditions and the following disclaimer.
3562306a36Sopenharmony_ci *   * Redistributions in binary form must reproduce the above copyright
3662306a36Sopenharmony_ci *     notice, this list of conditions and the following disclaimer in
3762306a36Sopenharmony_ci *     the documentation and/or other materials provided with the
3862306a36Sopenharmony_ci *     distribution.
3962306a36Sopenharmony_ci *   * Neither the name of Intel Corporation nor the names of its
4062306a36Sopenharmony_ci *     contributors may be used to endorse or promote products derived
4162306a36Sopenharmony_ci *     from this software without specific prior written permission.
4262306a36Sopenharmony_ci *
4362306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4462306a36Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4562306a36Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4662306a36Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4762306a36Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4862306a36Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4962306a36Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5062306a36Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5162306a36Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5262306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5362306a36Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#ifndef _ISCI_REQUEST_H_
5762306a36Sopenharmony_ci#define _ISCI_REQUEST_H_
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#include "isci.h"
6062306a36Sopenharmony_ci#include "host.h"
6162306a36Sopenharmony_ci#include "scu_task_context.h"
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/**
6462306a36Sopenharmony_ci * isci_stp_request - extra request infrastructure to handle pio/atapi protocol
6562306a36Sopenharmony_ci * @pio_len - number of bytes requested at PIO setup
6662306a36Sopenharmony_ci * @status - pio setup ending status value to tell us if we need
6762306a36Sopenharmony_ci *	     to wait for another fis or if the transfer is complete.  Upon
6862306a36Sopenharmony_ci *           receipt of a d2h fis this will be the status field of that fis.
6962306a36Sopenharmony_ci * @sgl - track pio transfer progress as we iterate through the sgl
7062306a36Sopenharmony_ci */
7162306a36Sopenharmony_cistruct isci_stp_request {
7262306a36Sopenharmony_ci	u32 pio_len;
7362306a36Sopenharmony_ci	u8 status;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	struct isci_stp_pio_sgl {
7662306a36Sopenharmony_ci		int index;
7762306a36Sopenharmony_ci		u8 set;
7862306a36Sopenharmony_ci		u32 offset;
7962306a36Sopenharmony_ci	} sgl;
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistruct isci_request {
8362306a36Sopenharmony_ci	#define IREQ_COMPLETE_IN_TARGET 0
8462306a36Sopenharmony_ci	#define IREQ_TERMINATED 1
8562306a36Sopenharmony_ci	#define IREQ_TMF 2
8662306a36Sopenharmony_ci	#define IREQ_ACTIVE 3
8762306a36Sopenharmony_ci	#define IREQ_PENDING_ABORT 4 /* Set == device was not suspended yet */
8862306a36Sopenharmony_ci	#define IREQ_TC_ABORT_POSTED 5
8962306a36Sopenharmony_ci	#define IREQ_ABORT_PATH_ACTIVE 6
9062306a36Sopenharmony_ci	#define IREQ_NO_AUTO_FREE_TAG 7 /* Set when being explicitly managed */
9162306a36Sopenharmony_ci	unsigned long flags;
9262306a36Sopenharmony_ci	/* XXX kill ttype and ttype_ptr, allocate full sas_task */
9362306a36Sopenharmony_ci	union ttype_ptr_union {
9462306a36Sopenharmony_ci		struct sas_task *io_task_ptr;   /* When ttype==io_task  */
9562306a36Sopenharmony_ci		struct isci_tmf *tmf_task_ptr;  /* When ttype==tmf_task */
9662306a36Sopenharmony_ci	} ttype_ptr;
9762306a36Sopenharmony_ci	struct isci_host *isci_host;
9862306a36Sopenharmony_ci	dma_addr_t request_daddr;
9962306a36Sopenharmony_ci	dma_addr_t zero_scatter_daddr;
10062306a36Sopenharmony_ci	unsigned int num_sg_entries;
10162306a36Sopenharmony_ci	/* Note: "io_request_completion" is completed in two different ways
10262306a36Sopenharmony_ci	 * depending on whether this is a TMF or regular request.
10362306a36Sopenharmony_ci	 * - TMF requests are completed in the thread that started them;
10462306a36Sopenharmony_ci	 * - regular requests are completed in the request completion callback
10562306a36Sopenharmony_ci	 *   function.
10662306a36Sopenharmony_ci	 * This difference in operation allows the aborter of a TMF request
10762306a36Sopenharmony_ci	 * to be sure that once the TMF request completes, the I/O that the
10862306a36Sopenharmony_ci	 * TMF was aborting is guaranteed to have completed.
10962306a36Sopenharmony_ci	 *
11062306a36Sopenharmony_ci	 * XXX kill io_request_completion
11162306a36Sopenharmony_ci	 */
11262306a36Sopenharmony_ci	struct completion *io_request_completion;
11362306a36Sopenharmony_ci	struct sci_base_state_machine sm;
11462306a36Sopenharmony_ci	struct isci_host *owning_controller;
11562306a36Sopenharmony_ci	struct isci_remote_device *target_device;
11662306a36Sopenharmony_ci	u16 io_tag;
11762306a36Sopenharmony_ci	enum sas_protocol protocol;
11862306a36Sopenharmony_ci	u32 scu_status; /* hardware result */
11962306a36Sopenharmony_ci	u32 sci_status; /* upper layer disposition */
12062306a36Sopenharmony_ci	u32 post_context;
12162306a36Sopenharmony_ci	struct scu_task_context *tc;
12262306a36Sopenharmony_ci	/* could be larger with sg chaining */
12362306a36Sopenharmony_ci	#define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
12462306a36Sopenharmony_ci	struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
12562306a36Sopenharmony_ci	/* This field is a pointer to the stored rx frame data.  It is used in
12662306a36Sopenharmony_ci	 * STP internal requests and SMP response frames.  If this field is
12762306a36Sopenharmony_ci	 * non-NULL the saved frame must be released on IO request completion.
12862306a36Sopenharmony_ci	 */
12962306a36Sopenharmony_ci	u32 saved_rx_frame_index;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	union {
13262306a36Sopenharmony_ci		struct {
13362306a36Sopenharmony_ci			union {
13462306a36Sopenharmony_ci				struct ssp_cmd_iu cmd;
13562306a36Sopenharmony_ci				struct ssp_task_iu tmf;
13662306a36Sopenharmony_ci			};
13762306a36Sopenharmony_ci			union {
13862306a36Sopenharmony_ci				struct ssp_response_iu rsp;
13962306a36Sopenharmony_ci				u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
14062306a36Sopenharmony_ci			};
14162306a36Sopenharmony_ci		} ssp;
14262306a36Sopenharmony_ci		struct {
14362306a36Sopenharmony_ci			struct isci_stp_request req;
14462306a36Sopenharmony_ci			struct host_to_dev_fis cmd;
14562306a36Sopenharmony_ci			struct dev_to_host_fis rsp;
14662306a36Sopenharmony_ci		} stp;
14762306a36Sopenharmony_ci	};
14862306a36Sopenharmony_ci};
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_cistatic inline struct isci_request *to_ireq(struct isci_stp_request *stp_req)
15162306a36Sopenharmony_ci{
15262306a36Sopenharmony_ci	struct isci_request *ireq;
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	ireq = container_of(stp_req, typeof(*ireq), stp.req);
15562306a36Sopenharmony_ci	return ireq;
15662306a36Sopenharmony_ci}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/**
15962306a36Sopenharmony_ci * enum sci_base_request_states - request state machine states
16062306a36Sopenharmony_ci *
16162306a36Sopenharmony_ci * @SCI_REQ_INIT: Simply the initial state for the base request state machine.
16262306a36Sopenharmony_ci *
16362306a36Sopenharmony_ci * @SCI_REQ_CONSTRUCTED: This state indicates that the request has been
16462306a36Sopenharmony_ci * constructed.  This state is entered from the INITIAL state.
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * @SCI_REQ_STARTED: This state indicates that the request has been started.
16762306a36Sopenharmony_ci * This state is entered from the CONSTRUCTED state.
16862306a36Sopenharmony_ci *
16962306a36Sopenharmony_ci * @SCI_REQ_STP_UDMA_WAIT_TC_COMP:
17062306a36Sopenharmony_ci * @SCI_REQ_STP_UDMA_WAIT_D2H:
17162306a36Sopenharmony_ci * @SCI_REQ_STP_NON_DATA_WAIT_H2D:
17262306a36Sopenharmony_ci * @SCI_REQ_STP_NON_DATA_WAIT_D2H:
17362306a36Sopenharmony_ci *
17462306a36Sopenharmony_ci * @SCI_REQ_STP_PIO_WAIT_H2D: While in this state the IO request object is
17562306a36Sopenharmony_ci * waiting for the TC completion notification for the H2D Register FIS
17662306a36Sopenharmony_ci *
17762306a36Sopenharmony_ci * @SCI_REQ_STP_PIO_WAIT_FRAME: While in this state the IO request object is
17862306a36Sopenharmony_ci * waiting for either a PIO Setup FIS or a D2H register FIS.  The type of frame
17962306a36Sopenharmony_ci * received is based on the result of the prior frame and line conditions.
18062306a36Sopenharmony_ci *
18162306a36Sopenharmony_ci * @SCI_REQ_STP_PIO_DATA_IN: While in this state the IO request object is
18262306a36Sopenharmony_ci * waiting for a DATA frame from the device.
18362306a36Sopenharmony_ci *
18462306a36Sopenharmony_ci * @SCI_REQ_STP_PIO_DATA_OUT: While in this state the IO request object is
18562306a36Sopenharmony_ci * waiting to transmit the next data frame to the device.
18662306a36Sopenharmony_ci *
18762306a36Sopenharmony_ci * @SCI_REQ_ATAPI_WAIT_H2D: While in this state the IO request object is
18862306a36Sopenharmony_ci * waiting for the TC completion notification for the H2D Register FIS
18962306a36Sopenharmony_ci *
19062306a36Sopenharmony_ci * @SCI_REQ_ATAPI_WAIT_PIO_SETUP: While in this state the IO request object is
19162306a36Sopenharmony_ci * waiting for either a PIO Setup.
19262306a36Sopenharmony_ci *
19362306a36Sopenharmony_ci * @SCI_REQ_ATAPI_WAIT_D2H: The non-data IO transit to this state in this state
19462306a36Sopenharmony_ci * after receiving TC completion. While in this state IO request object is
19562306a36Sopenharmony_ci * waiting for D2H status frame as UF.
19662306a36Sopenharmony_ci *
19762306a36Sopenharmony_ci * @SCI_REQ_ATAPI_WAIT_TC_COMP: When transmitting raw frames hardware reports
19862306a36Sopenharmony_ci * task context completion after every frame submission, so in the
19962306a36Sopenharmony_ci * non-accelerated case we need to expect the completion for the "cdb" frame.
20062306a36Sopenharmony_ci *
20162306a36Sopenharmony_ci * @SCI_REQ_TASK_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that
20262306a36Sopenharmony_ci * the started raw task management request is waiting for the transmission of
20362306a36Sopenharmony_ci * the initial frame (i.e. command, task, etc.).
20462306a36Sopenharmony_ci *
20562306a36Sopenharmony_ci * @SCI_REQ_TASK_WAIT_TC_RESP: This sub-state indicates that the started task
20662306a36Sopenharmony_ci * management request is waiting for the reception of an unsolicited frame
20762306a36Sopenharmony_ci * (i.e.  response IU).
20862306a36Sopenharmony_ci *
20962306a36Sopenharmony_ci * @SCI_REQ_SMP_WAIT_RESP: This sub-state indicates that the started task
21062306a36Sopenharmony_ci * management request is waiting for the reception of an unsolicited frame
21162306a36Sopenharmony_ci * (i.e.  response IU).
21262306a36Sopenharmony_ci *
21362306a36Sopenharmony_ci * @SCI_REQ_SMP_WAIT_TC_COMP: The AWAIT_TC_COMPLETION sub-state indicates that
21462306a36Sopenharmony_ci * the started SMP request is waiting for the transmission of the initial frame
21562306a36Sopenharmony_ci * (i.e.  command, task, etc.).
21662306a36Sopenharmony_ci *
21762306a36Sopenharmony_ci * @SCI_REQ_COMPLETED: This state indicates that the request has completed.
21862306a36Sopenharmony_ci * This state is entered from the STARTED state. This state is entered from the
21962306a36Sopenharmony_ci * ABORTING state.
22062306a36Sopenharmony_ci *
22162306a36Sopenharmony_ci * @SCI_REQ_ABORTING: This state indicates that the request is in the process
22262306a36Sopenharmony_ci * of being terminated/aborted.  This state is entered from the CONSTRUCTED
22362306a36Sopenharmony_ci * state.  This state is entered from the STARTED state.
22462306a36Sopenharmony_ci *
22562306a36Sopenharmony_ci * @SCI_REQ_FINAL: Simply the final state for the base request state machine.
22662306a36Sopenharmony_ci */
22762306a36Sopenharmony_ci#define REQUEST_STATES {\
22862306a36Sopenharmony_ci	C(REQ_INIT),\
22962306a36Sopenharmony_ci	C(REQ_CONSTRUCTED),\
23062306a36Sopenharmony_ci	C(REQ_STARTED),\
23162306a36Sopenharmony_ci	C(REQ_STP_UDMA_WAIT_TC_COMP),\
23262306a36Sopenharmony_ci	C(REQ_STP_UDMA_WAIT_D2H),\
23362306a36Sopenharmony_ci	C(REQ_STP_NON_DATA_WAIT_H2D),\
23462306a36Sopenharmony_ci	C(REQ_STP_NON_DATA_WAIT_D2H),\
23562306a36Sopenharmony_ci	C(REQ_STP_PIO_WAIT_H2D),\
23662306a36Sopenharmony_ci	C(REQ_STP_PIO_WAIT_FRAME),\
23762306a36Sopenharmony_ci	C(REQ_STP_PIO_DATA_IN),\
23862306a36Sopenharmony_ci	C(REQ_STP_PIO_DATA_OUT),\
23962306a36Sopenharmony_ci	C(REQ_ATAPI_WAIT_H2D),\
24062306a36Sopenharmony_ci	C(REQ_ATAPI_WAIT_PIO_SETUP),\
24162306a36Sopenharmony_ci	C(REQ_ATAPI_WAIT_D2H),\
24262306a36Sopenharmony_ci	C(REQ_ATAPI_WAIT_TC_COMP),\
24362306a36Sopenharmony_ci	C(REQ_TASK_WAIT_TC_COMP),\
24462306a36Sopenharmony_ci	C(REQ_TASK_WAIT_TC_RESP),\
24562306a36Sopenharmony_ci	C(REQ_SMP_WAIT_RESP),\
24662306a36Sopenharmony_ci	C(REQ_SMP_WAIT_TC_COMP),\
24762306a36Sopenharmony_ci	C(REQ_COMPLETED),\
24862306a36Sopenharmony_ci	C(REQ_ABORTING),\
24962306a36Sopenharmony_ci	C(REQ_FINAL),\
25062306a36Sopenharmony_ci	}
25162306a36Sopenharmony_ci#undef C
25262306a36Sopenharmony_ci#define C(a) SCI_##a
25362306a36Sopenharmony_cienum sci_base_request_states REQUEST_STATES;
25462306a36Sopenharmony_ci#undef C
25562306a36Sopenharmony_ciconst char *req_state_name(enum sci_base_request_states state);
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_cienum sci_status sci_request_start(struct isci_request *ireq);
25862306a36Sopenharmony_cienum sci_status sci_io_request_terminate(struct isci_request *ireq);
25962306a36Sopenharmony_cienum sci_status
26062306a36Sopenharmony_cisci_io_request_event_handler(struct isci_request *ireq,
26162306a36Sopenharmony_ci				  u32 event_code);
26262306a36Sopenharmony_cienum sci_status
26362306a36Sopenharmony_cisci_io_request_frame_handler(struct isci_request *ireq,
26462306a36Sopenharmony_ci				  u32 frame_index);
26562306a36Sopenharmony_cienum sci_status
26662306a36Sopenharmony_cisci_task_request_terminate(struct isci_request *ireq);
26762306a36Sopenharmony_ciextern enum sci_status
26862306a36Sopenharmony_cisci_request_complete(struct isci_request *ireq);
26962306a36Sopenharmony_ciextern enum sci_status
27062306a36Sopenharmony_cisci_io_request_tc_completion(struct isci_request *ireq, u32 code);
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci/* XXX open code in caller */
27362306a36Sopenharmony_cistatic inline dma_addr_t
27462306a36Sopenharmony_cisci_io_request_get_dma_addr(struct isci_request *ireq, void *virt_addr)
27562306a36Sopenharmony_ci{
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	char *requested_addr = (char *)virt_addr;
27862306a36Sopenharmony_ci	char *base_addr = (char *)ireq;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	BUG_ON(requested_addr < base_addr);
28162306a36Sopenharmony_ci	BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	return ireq->request_daddr + (requested_addr - base_addr);
28462306a36Sopenharmony_ci}
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci#define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci#define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_cistruct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
29162306a36Sopenharmony_ci					       struct isci_tmf *isci_tmf,
29262306a36Sopenharmony_ci					       u16 tag);
29362306a36Sopenharmony_ciint isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
29462306a36Sopenharmony_ci			 struct sas_task *task, struct isci_request *ireq);
29562306a36Sopenharmony_cistruct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
29662306a36Sopenharmony_ci					      struct sas_task *task,
29762306a36Sopenharmony_ci					      u16 tag);
29862306a36Sopenharmony_cienum sci_status
29962306a36Sopenharmony_cisci_task_request_construct(struct isci_host *ihost,
30062306a36Sopenharmony_ci			    struct isci_remote_device *idev,
30162306a36Sopenharmony_ci			    u16 io_tag,
30262306a36Sopenharmony_ci			    struct isci_request *ireq);
30362306a36Sopenharmony_cienum sci_status sci_task_request_construct_ssp(struct isci_request *ireq);
30462306a36Sopenharmony_civoid sci_smp_request_copy_response(struct isci_request *ireq);
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic inline int isci_task_is_ncq_recovery(struct sas_task *task)
30762306a36Sopenharmony_ci{
30862306a36Sopenharmony_ci	return (sas_protocol_ata(task->task_proto) &&
30962306a36Sopenharmony_ci		task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
31062306a36Sopenharmony_ci		task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci}
31362306a36Sopenharmony_ci#endif /* !defined(_ISCI_REQUEST_H_) */
314