xref: /openbmc/linux/drivers/scsi/isci/request.h (revision ddcc7e347a891937be65358b43f40b7f81185f8f)
16f231ddaSDan Williams /*
26f231ddaSDan Williams  * This file is provided under a dual BSD/GPLv2 license.  When using or
36f231ddaSDan Williams  * redistributing this file, you may do so under either license.
46f231ddaSDan Williams  *
56f231ddaSDan Williams  * GPL LICENSE SUMMARY
66f231ddaSDan Williams  *
76f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
86f231ddaSDan Williams  *
96f231ddaSDan Williams  * This program is free software; you can redistribute it and/or modify
106f231ddaSDan Williams  * it under the terms of version 2 of the GNU General Public License as
116f231ddaSDan Williams  * published by the Free Software Foundation.
126f231ddaSDan Williams  *
136f231ddaSDan Williams  * This program is distributed in the hope that it will be useful, but
146f231ddaSDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
156f231ddaSDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
166f231ddaSDan Williams  * General Public License for more details.
176f231ddaSDan Williams  *
186f231ddaSDan Williams  * You should have received a copy of the GNU General Public License
196f231ddaSDan Williams  * along with this program; if not, write to the Free Software
206f231ddaSDan Williams  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
216f231ddaSDan Williams  * The full GNU General Public License is included in this distribution
226f231ddaSDan Williams  * in the file called LICENSE.GPL.
236f231ddaSDan Williams  *
246f231ddaSDan Williams  * BSD LICENSE
256f231ddaSDan Williams  *
266f231ddaSDan Williams  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
276f231ddaSDan Williams  * All rights reserved.
286f231ddaSDan Williams  *
296f231ddaSDan Williams  * Redistribution and use in source and binary forms, with or without
306f231ddaSDan Williams  * modification, are permitted provided that the following conditions
316f231ddaSDan Williams  * are met:
326f231ddaSDan Williams  *
336f231ddaSDan Williams  *   * Redistributions of source code must retain the above copyright
346f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer.
356f231ddaSDan Williams  *   * Redistributions in binary form must reproduce the above copyright
366f231ddaSDan Williams  *     notice, this list of conditions and the following disclaimer in
376f231ddaSDan Williams  *     the documentation and/or other materials provided with the
386f231ddaSDan Williams  *     distribution.
396f231ddaSDan Williams  *   * Neither the name of Intel Corporation nor the names of its
406f231ddaSDan Williams  *     contributors may be used to endorse or promote products derived
416f231ddaSDan Williams  *     from this software without specific prior written permission.
426f231ddaSDan Williams  *
436f231ddaSDan Williams  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
446f231ddaSDan Williams  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
456f231ddaSDan Williams  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
466f231ddaSDan Williams  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
476f231ddaSDan Williams  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
486f231ddaSDan Williams  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
496f231ddaSDan Williams  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
506f231ddaSDan Williams  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
516f231ddaSDan Williams  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
526f231ddaSDan Williams  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
536f231ddaSDan Williams  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
546f231ddaSDan Williams  */
556f231ddaSDan Williams 
560d84366fSDan Williams #ifndef _ISCI_REQUEST_H_
576f231ddaSDan Williams #define _ISCI_REQUEST_H_
586f231ddaSDan Williams 
596f231ddaSDan Williams #include "isci.h"
60ce2b3261SDan Williams #include "host.h"
61f1f52e75SDan Williams #include "scu_task_context.h"
626f231ddaSDan Williams 
636f231ddaSDan Williams /**
646f231ddaSDan Williams  * struct isci_request_status - This enum defines the possible states of an I/O
656f231ddaSDan Williams  *    request.
666f231ddaSDan Williams  *
676f231ddaSDan Williams  *
686f231ddaSDan Williams  */
696f231ddaSDan Williams enum isci_request_status {
706f231ddaSDan Williams 	unallocated = 0x00,
716f231ddaSDan Williams 	allocated   = 0x01,
726f231ddaSDan Williams 	started     = 0x02,
736f231ddaSDan Williams 	completed   = 0x03,
746f231ddaSDan Williams 	aborting    = 0x04,
756f231ddaSDan Williams 	aborted     = 0x05,
764dc043c4SJeff Skirvin 	terminating = 0x06,
774dc043c4SJeff Skirvin 	dead        = 0x07
786f231ddaSDan Williams };
796f231ddaSDan Williams 
806f231ddaSDan Williams enum task_type {
816f231ddaSDan Williams 	io_task  = 0,
826f231ddaSDan Williams 	tmf_task = 1
836f231ddaSDan Williams };
846f231ddaSDan Williams 
85f1f52e75SDan Williams enum sci_request_protocol {
86f1f52e75SDan Williams 	SCIC_NO_PROTOCOL,
87f1f52e75SDan Williams 	SCIC_SMP_PROTOCOL,
88f1f52e75SDan Williams 	SCIC_SSP_PROTOCOL,
89f1f52e75SDan Williams 	SCIC_STP_PROTOCOL
90c72086e3SDan Williams }; /* XXX remove me, use sas_task.{dev|task_proto} instead */;
91f1f52e75SDan Williams 
925dec6f4eSDan Williams struct scic_sds_stp_request {
935dec6f4eSDan Williams 	union {
945dec6f4eSDan Williams 		u32 ncq;
955dec6f4eSDan Williams 
965dec6f4eSDan Williams 		u32 udma;
975dec6f4eSDan Williams 
985dec6f4eSDan Williams 		struct scic_sds_stp_pio_request {
99e301370aSEdmund Nadolski 			/*
100e301370aSEdmund Nadolski 			 * Total transfer for the entire PIO request recorded
101e301370aSEdmund Nadolski 			 * at request constuction time.
1025dec6f4eSDan Williams 			 *
103e301370aSEdmund Nadolski 			 * @todo Should we just decrement this value for each
104e301370aSEdmund Nadolski 			 * byte of data transitted or received to elemenate
105e301370aSEdmund Nadolski 			 * the current_transfer_bytes field?
1065dec6f4eSDan Williams 			 */
1075dec6f4eSDan Williams 			u32 total_transfer_bytes;
1085dec6f4eSDan Williams 
109e301370aSEdmund Nadolski 			/*
110e301370aSEdmund Nadolski 			 * Total number of bytes received/transmitted in data
111e301370aSEdmund Nadolski 			 * frames since the start of the IO request.  At the
112e301370aSEdmund Nadolski 			 * end of the IO request this should equal the
1135dec6f4eSDan Williams 			 * total_transfer_bytes.
1145dec6f4eSDan Williams 			 */
1155dec6f4eSDan Williams 			u32 current_transfer_bytes;
1165dec6f4eSDan Williams 
117e301370aSEdmund Nadolski 			/*
118e301370aSEdmund Nadolski 			 * The number of bytes requested in the in the PIO
119e301370aSEdmund Nadolski 			 * setup.
1205dec6f4eSDan Williams 			 */
1215dec6f4eSDan Williams 			u32 pio_transfer_bytes;
1225dec6f4eSDan Williams 
123e301370aSEdmund Nadolski 			/*
124e301370aSEdmund Nadolski 			 * PIO Setup ending status value to tell us if we need
125e301370aSEdmund Nadolski 			 * to wait for another FIS or if the transfer is
126e301370aSEdmund Nadolski 			 * complete. On the receipt of a D2H FIS this will be
1275dec6f4eSDan Williams 			 * the status field of that FIS.
1285dec6f4eSDan Williams 			 */
1295dec6f4eSDan Williams 			u8 ending_status;
1305dec6f4eSDan Williams 
131e301370aSEdmund Nadolski 			/*
132e301370aSEdmund Nadolski 			 * On receipt of a D2H FIS this will be the ending
133e301370aSEdmund Nadolski 			 * error field if the ending_status has the
134e301370aSEdmund Nadolski 			 * SATA_STATUS_ERR bit set.
1355dec6f4eSDan Williams 			 */
1365dec6f4eSDan Williams 			u8 ending_error;
1375dec6f4eSDan Williams 
1385dec6f4eSDan Williams 			struct scic_sds_request_pio_sgl {
1395dec6f4eSDan Williams 				struct scu_sgl_element_pair *sgl_pair;
1405dec6f4eSDan Williams 				u8 sgl_set;
1415dec6f4eSDan Williams 				u32 sgl_offset;
1425dec6f4eSDan Williams 			} request_current;
1435dec6f4eSDan Williams 		} pio;
1445dec6f4eSDan Williams 
1455dec6f4eSDan Williams 		struct {
146e301370aSEdmund Nadolski 			/*
147e301370aSEdmund Nadolski 			 * The number of bytes requested in the PIO setup
148e301370aSEdmund Nadolski 			 * before CDB data frame.
1495dec6f4eSDan Williams 			 */
1505dec6f4eSDan Williams 			u32 device_preferred_cdb_length;
1515dec6f4eSDan Williams 		} packet;
1525dec6f4eSDan Williams 	} type;
1535dec6f4eSDan Williams };
1545dec6f4eSDan Williams 
155f1f52e75SDan Williams struct scic_sds_request {
156e301370aSEdmund Nadolski 	/*
157e301370aSEdmund Nadolski 	 * This field contains the information for the base request state
158e301370aSEdmund Nadolski 	 * machine.
159f1f52e75SDan Williams 	 */
160e301370aSEdmund Nadolski 	struct sci_base_state_machine sm;
161f1f52e75SDan Williams 
162e301370aSEdmund Nadolski 	/*
163f1f52e75SDan Williams 	 * This field simply points to the controller to which this IO request
164f1f52e75SDan Williams 	 * is associated.
165f1f52e75SDan Williams 	 */
166f1f52e75SDan Williams 	struct scic_sds_controller *owning_controller;
167f1f52e75SDan Williams 
168e301370aSEdmund Nadolski 	/*
169e301370aSEdmund Nadolski 	 * This field simply points to the remote device to which this IO
170e301370aSEdmund Nadolski 	 * request is associated.
171f1f52e75SDan Williams 	 */
172f1f52e75SDan Williams 	struct scic_sds_remote_device *target_device;
173f1f52e75SDan Williams 
174e301370aSEdmund Nadolski 	/*
175f1f52e75SDan Williams 	 * This field is utilized to determine if the SCI user is managing
176f1f52e75SDan Williams 	 * the IO tag for this request or if the core is managing it.
177f1f52e75SDan Williams 	 */
178f1f52e75SDan Williams 	bool was_tag_assigned_by_user;
179f1f52e75SDan Williams 
180e301370aSEdmund Nadolski 	/*
181f1f52e75SDan Williams 	 * This field indicates the IO tag for this request.  The IO tag is
182f1f52e75SDan Williams 	 * comprised of the task_index and a sequence count. The sequence count
183f1f52e75SDan Williams 	 * is utilized to help identify tasks from one life to another.
184f1f52e75SDan Williams 	 */
185f1f52e75SDan Williams 	u16 io_tag;
186f1f52e75SDan Williams 
187e301370aSEdmund Nadolski 	/*
188f1f52e75SDan Williams 	 * This field specifies the protocol being utilized for this
189f1f52e75SDan Williams 	 * IO request.
190f1f52e75SDan Williams 	 */
191f1f52e75SDan Williams 	enum sci_request_protocol protocol;
192f1f52e75SDan Williams 
193e301370aSEdmund Nadolski 	/*
194f1f52e75SDan Williams 	 * This field indicates the completion status taken from the SCUs
195e301370aSEdmund Nadolski 	 * completion code.  It indicates the completion result for the SCU
196e301370aSEdmund Nadolski 	 * hardware.
197f1f52e75SDan Williams 	 */
198f1f52e75SDan Williams 	u32 scu_status;
199f1f52e75SDan Williams 
200e301370aSEdmund Nadolski 	/*
201e301370aSEdmund Nadolski 	 * This field indicates the completion status returned to the SCI user.
202e301370aSEdmund Nadolski 	 * It indicates the users view of the io request completion.
203f1f52e75SDan Williams 	 */
204f1f52e75SDan Williams 	u32 sci_status;
205f1f52e75SDan Williams 
206e301370aSEdmund Nadolski 	/*
207e301370aSEdmund Nadolski 	 * This field contains the value to be utilized when posting
208e301370aSEdmund Nadolski 	 * (e.g. Post_TC, * Post_TC_Abort) this request to the silicon.
209f1f52e75SDan Williams 	 */
210f1f52e75SDan Williams 	u32 post_context;
211f1f52e75SDan Williams 
212f1f52e75SDan Williams 	struct scu_task_context *task_context_buffer;
213f1f52e75SDan Williams 	struct scu_task_context tc ____cacheline_aligned;
214f1f52e75SDan Williams 
215f1f52e75SDan Williams 	/* could be larger with sg chaining */
2167c78da31SDan Williams 	#define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
217f1f52e75SDan Williams 	struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
218f1f52e75SDan Williams 
219e301370aSEdmund Nadolski 	/*
220f1f52e75SDan Williams 	 * This field indicates if this request is a task management request or
221f1f52e75SDan Williams 	 * normal IO request.
222f1f52e75SDan Williams 	 */
223f1f52e75SDan Williams 	bool is_task_management_request;
224f1f52e75SDan Williams 
225e301370aSEdmund Nadolski 	/*
226e301370aSEdmund Nadolski 	 * This field is a pointer to the stored rx frame data.  It is used in
227e301370aSEdmund Nadolski 	 * STP internal requests and SMP response frames.  If this field is
228e301370aSEdmund Nadolski 	 * non-NULL the saved frame must be released on IO request completion.
229f1f52e75SDan Williams 	 *
230f1f52e75SDan Williams 	 * @todo In the future do we want to keep a list of RX frame buffers?
231f1f52e75SDan Williams 	 */
232f1f52e75SDan Williams 	u32 saved_rx_frame_index;
233f1f52e75SDan Williams 
234f1f52e75SDan Williams 	union {
235f1f52e75SDan Williams 		struct {
236f1f52e75SDan Williams 			union {
237f1f52e75SDan Williams 				struct ssp_cmd_iu cmd;
238f1f52e75SDan Williams 				struct ssp_task_iu tmf;
239f1f52e75SDan Williams 			};
240f1f52e75SDan Williams 			union {
241f1f52e75SDan Williams 				struct ssp_response_iu rsp;
242f1f52e75SDan Williams 				u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
243f1f52e75SDan Williams 			};
244f1f52e75SDan Williams 		} ssp;
245f1f52e75SDan Williams 
246f1f52e75SDan Williams 		struct {
247f1f52e75SDan Williams 			struct smp_req cmd;
248f1f52e75SDan Williams 			struct smp_resp rsp;
249f1f52e75SDan Williams 		} smp;
250f1f52e75SDan Williams 
251f1f52e75SDan Williams 		struct {
252f1f52e75SDan Williams 			struct scic_sds_stp_request req;
253f1f52e75SDan Williams 			struct host_to_dev_fis cmd;
254f1f52e75SDan Williams 			struct dev_to_host_fis rsp;
255f1f52e75SDan Williams 		} stp;
256f1f52e75SDan Williams 	};
257f1f52e75SDan Williams };
258f1f52e75SDan Williams 
259f1f52e75SDan Williams static inline struct scic_sds_request *to_sci_req(struct scic_sds_stp_request *stp_req)
260f1f52e75SDan Williams {
261f1f52e75SDan Williams 	struct scic_sds_request *sci_req;
262f1f52e75SDan Williams 
263f1f52e75SDan Williams 	sci_req = container_of(stp_req, typeof(*sci_req), stp.req);
264f1f52e75SDan Williams 	return sci_req;
265f1f52e75SDan Williams }
266f1f52e75SDan Williams 
2676f231ddaSDan Williams struct isci_request {
2686f231ddaSDan Williams 	enum isci_request_status status;
2696f231ddaSDan Williams 	enum task_type ttype;
2706f231ddaSDan Williams 	unsigned short io_tag;
2716f231ddaSDan Williams 	bool complete_in_target;
27267ea838dSDan Williams 	bool terminated;
2736f231ddaSDan Williams 
2746f231ddaSDan Williams 	union ttype_ptr_union {
2756f231ddaSDan Williams 		struct sas_task *io_task_ptr;   /* When ttype==io_task  */
2766f231ddaSDan Williams 		struct isci_tmf *tmf_task_ptr;  /* When ttype==tmf_task */
2776f231ddaSDan Williams 	} ttype_ptr;
2786f231ddaSDan Williams 	struct isci_host *isci_host;
2796f231ddaSDan Williams 	/* For use in the requests_to_{complete|abort} lists: */
2806f231ddaSDan Williams 	struct list_head completed_node;
2816f231ddaSDan Williams 	/* For use in the reqs_in_process list: */
2826f231ddaSDan Williams 	struct list_head dev_node;
2836f231ddaSDan Williams 	spinlock_t state_lock;
2846f231ddaSDan Williams 	dma_addr_t request_daddr;
2856f231ddaSDan Williams 	dma_addr_t zero_scatter_daddr;
2866f231ddaSDan Williams 
2876f231ddaSDan Williams 	unsigned int num_sg_entries;			/* returned by pci_alloc_sg */
2886f231ddaSDan Williams 
2896f231ddaSDan Williams 	/** Note: "io_request_completion" is completed in two different ways
2906f231ddaSDan Williams 	 * depending on whether this is a TMF or regular request.
2916f231ddaSDan Williams 	 * - TMF requests are completed in the thread that started them;
2926f231ddaSDan Williams 	 * - regular requests are completed in the request completion callback
2936f231ddaSDan Williams 	 *   function.
2946f231ddaSDan Williams 	 * This difference in operation allows the aborter of a TMF request
2956f231ddaSDan Williams 	 * to be sure that once the TMF request completes, the I/O that the
2966f231ddaSDan Williams 	 * TMF was aborting is guaranteed to have completed.
2976f231ddaSDan Williams 	 */
2986f231ddaSDan Williams 	struct completion *io_request_completion;
29967ea838dSDan Williams 	struct scic_sds_request sci;
3006f231ddaSDan Williams };
3016f231ddaSDan Williams 
30267ea838dSDan Williams static inline struct isci_request *sci_req_to_ireq(struct scic_sds_request *sci_req)
30367ea838dSDan Williams {
30467ea838dSDan Williams 	struct isci_request *ireq = container_of(sci_req, typeof(*ireq), sci);
30567ea838dSDan Williams 
30667ea838dSDan Williams 	return ireq;
30767ea838dSDan Williams }
30867ea838dSDan Williams 
3096f231ddaSDan Williams /**
310f1f52e75SDan Williams  * enum sci_base_request_states - This enumeration depicts all the states for
311f1f52e75SDan Williams  *    the common request state machine.
312f1f52e75SDan Williams  *
313f1f52e75SDan Williams  *
314f1f52e75SDan Williams  */
315f1f52e75SDan Williams enum sci_base_request_states {
316e301370aSEdmund Nadolski 	/*
317f1f52e75SDan Williams 	 * Simply the initial state for the base request state machine.
318f1f52e75SDan Williams 	 */
319e301370aSEdmund Nadolski 	SCI_REQ_INIT,
320f1f52e75SDan Williams 
321e301370aSEdmund Nadolski 	/*
322e301370aSEdmund Nadolski 	 * This state indicates that the request has been constructed.
323e301370aSEdmund Nadolski 	 * This state is entered from the INITIAL state.
324f1f52e75SDan Williams 	 */
325e301370aSEdmund Nadolski 	SCI_REQ_CONSTRUCTED,
326f1f52e75SDan Williams 
327e301370aSEdmund Nadolski 	/*
328e301370aSEdmund Nadolski 	 * This state indicates that the request has been started. This state
329e301370aSEdmund Nadolski 	 * is entered from the CONSTRUCTED state.
330f1f52e75SDan Williams 	 */
331e301370aSEdmund Nadolski 	SCI_REQ_STARTED,
332f1f52e75SDan Williams 
333e301370aSEdmund Nadolski 	SCI_REQ_STP_UDMA_WAIT_TC_COMP,
334e301370aSEdmund Nadolski 	SCI_REQ_STP_UDMA_WAIT_D2H,
3355dec6f4eSDan Williams 
336e301370aSEdmund Nadolski 	SCI_REQ_STP_NON_DATA_WAIT_H2D,
337e301370aSEdmund Nadolski 	SCI_REQ_STP_NON_DATA_WAIT_D2H,
3385dec6f4eSDan Williams 
339e301370aSEdmund Nadolski 	SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED,
340e301370aSEdmund Nadolski 	SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG,
341e301370aSEdmund Nadolski 	SCI_REQ_STP_SOFT_RESET_WAIT_D2H,
3425dec6f4eSDan Williams 
343e301370aSEdmund Nadolski 	/*
344e301370aSEdmund Nadolski 	 * While in this state the IO request object is waiting for the TC
345e301370aSEdmund Nadolski 	 * completion notification for the H2D Register FIS
3465dec6f4eSDan Williams 	 */
347e301370aSEdmund Nadolski 	SCI_REQ_STP_PIO_WAIT_H2D,
3485dec6f4eSDan Williams 
349e301370aSEdmund Nadolski 	/*
350e301370aSEdmund Nadolski 	 * While in this state the IO request object is waiting for either a
351e301370aSEdmund Nadolski 	 * PIO Setup FIS or a D2H register FIS.  The type of frame received is
352e301370aSEdmund Nadolski 	 * based on the result of the prior frame and line conditions.
3535dec6f4eSDan Williams 	 */
354e301370aSEdmund Nadolski 	SCI_REQ_STP_PIO_WAIT_FRAME,
3555dec6f4eSDan Williams 
356e301370aSEdmund Nadolski 	/*
357e301370aSEdmund Nadolski 	 * While in this state the IO request object is waiting for a DATA
358e301370aSEdmund Nadolski 	 * frame from the device.
3595dec6f4eSDan Williams 	 */
360e301370aSEdmund Nadolski 	SCI_REQ_STP_PIO_DATA_IN,
3615dec6f4eSDan Williams 
362e301370aSEdmund Nadolski 	/*
363e301370aSEdmund Nadolski 	 * While in this state the IO request object is waiting to transmit
364e301370aSEdmund Nadolski 	 * the next data frame to the device.
3655dec6f4eSDan Williams 	 */
366e301370aSEdmund Nadolski 	SCI_REQ_STP_PIO_DATA_OUT,
3675dec6f4eSDan Williams 
368e301370aSEdmund Nadolski 	/*
369f139303dSDan Williams 	 * The AWAIT_TC_COMPLETION sub-state indicates that the started raw
370f139303dSDan Williams 	 * task management request is waiting for the transmission of the
371f139303dSDan Williams 	 * initial frame (i.e. command, task, etc.).
372f139303dSDan Williams 	 */
373e301370aSEdmund Nadolski 	SCI_REQ_TASK_WAIT_TC_COMP,
374f139303dSDan Williams 
375e301370aSEdmund Nadolski 	/*
376f139303dSDan Williams 	 * This sub-state indicates that the started task management request
377f139303dSDan Williams 	 * is waiting for the reception of an unsolicited frame
378f139303dSDan Williams 	 * (i.e. response IU).
379f139303dSDan Williams 	 */
380e301370aSEdmund Nadolski 	SCI_REQ_TASK_WAIT_TC_RESP,
381f139303dSDan Williams 
382e301370aSEdmund Nadolski 	/*
383c72086e3SDan Williams 	 * This sub-state indicates that the started task management request
384c72086e3SDan Williams 	 * is waiting for the reception of an unsolicited frame
385c72086e3SDan Williams 	 * (i.e. response IU).
386c72086e3SDan Williams 	 */
387e301370aSEdmund Nadolski 	SCI_REQ_SMP_WAIT_RESP,
388c72086e3SDan Williams 
389e301370aSEdmund Nadolski 	/*
390e301370aSEdmund Nadolski 	 * The AWAIT_TC_COMPLETION sub-state indicates that the started SMP
391e301370aSEdmund Nadolski 	 * request is waiting for the transmission of the initial frame
392e301370aSEdmund Nadolski 	 * (i.e. command, task, etc.).
393c72086e3SDan Williams 	 */
394e301370aSEdmund Nadolski 	SCI_REQ_SMP_WAIT_TC_COMP,
395c72086e3SDan Williams 
396e301370aSEdmund Nadolski 	/*
397f1f52e75SDan Williams 	 * This state indicates that the request has completed.
398e301370aSEdmund Nadolski 	 * This state is entered from the STARTED state. This state is entered
399e301370aSEdmund Nadolski 	 * from the ABORTING state.
400f1f52e75SDan Williams 	 */
401e301370aSEdmund Nadolski 	SCI_REQ_COMPLETED,
402f1f52e75SDan Williams 
403e301370aSEdmund Nadolski 	/*
404f1f52e75SDan Williams 	 * This state indicates that the request is in the process of being
405f1f52e75SDan Williams 	 * terminated/aborted.
406f1f52e75SDan Williams 	 * This state is entered from the CONSTRUCTED state.
407f1f52e75SDan Williams 	 * This state is entered from the STARTED state.
408f1f52e75SDan Williams 	 */
409e301370aSEdmund Nadolski 	SCI_REQ_ABORTING,
410f1f52e75SDan Williams 
411e301370aSEdmund Nadolski 	/*
412f1f52e75SDan Williams 	 * Simply the final state for the base request state machine.
413f1f52e75SDan Williams 	 */
414e301370aSEdmund Nadolski 	SCI_REQ_FINAL,
415f1f52e75SDan Williams };
416f1f52e75SDan Williams 
417f1f52e75SDan Williams /**
418f1f52e75SDan Williams  * scic_sds_request_get_controller() -
419f1f52e75SDan Williams  *
420f1f52e75SDan Williams  * This macro will return the controller for this io request object
421f1f52e75SDan Williams  */
422f1f52e75SDan Williams #define scic_sds_request_get_controller(sci_req) \
423f1f52e75SDan Williams 	((sci_req)->owning_controller)
424f1f52e75SDan Williams 
425f1f52e75SDan Williams /**
426f1f52e75SDan Williams  * scic_sds_request_get_device() -
427f1f52e75SDan Williams  *
428f1f52e75SDan Williams  * This macro will return the device for this io request object
429f1f52e75SDan Williams  */
430f1f52e75SDan Williams #define scic_sds_request_get_device(sci_req) \
431f1f52e75SDan Williams 	((sci_req)->target_device)
432f1f52e75SDan Williams 
433f1f52e75SDan Williams /**
434f1f52e75SDan Williams  * scic_sds_request_get_port() -
435f1f52e75SDan Williams  *
436f1f52e75SDan Williams  * This macro will return the port for this io request object
437f1f52e75SDan Williams  */
438f1f52e75SDan Williams #define scic_sds_request_get_port(sci_req)	\
439f1f52e75SDan Williams 	scic_sds_remote_device_get_port(scic_sds_request_get_device(sci_req))
440f1f52e75SDan Williams 
441f1f52e75SDan Williams /**
442f1f52e75SDan Williams  * scic_sds_request_get_post_context() -
443f1f52e75SDan Williams  *
444f1f52e75SDan Williams  * This macro returns the constructed post context result for the io request.
445f1f52e75SDan Williams  */
446f1f52e75SDan Williams #define scic_sds_request_get_post_context(sci_req)	\
447f1f52e75SDan Williams 	((sci_req)->post_context)
448f1f52e75SDan Williams 
449f1f52e75SDan Williams /**
450f1f52e75SDan Williams  * scic_sds_request_get_task_context() -
451f1f52e75SDan Williams  *
452f1f52e75SDan Williams  * This is a helper macro to return the os handle for this request object.
453f1f52e75SDan Williams  */
454f1f52e75SDan Williams #define scic_sds_request_get_task_context(request) \
455f1f52e75SDan Williams 	((request)->task_context_buffer)
456f1f52e75SDan Williams 
457f1f52e75SDan Williams /**
458f1f52e75SDan Williams  * scic_sds_request_set_status() -
459f1f52e75SDan Williams  *
460f1f52e75SDan Williams  * This macro will set the scu hardware status and sci request completion
461f1f52e75SDan Williams  * status for an io request.
462f1f52e75SDan Williams  */
463f1f52e75SDan Williams #define scic_sds_request_set_status(request, scu_status_code, sci_status_code) \
464f1f52e75SDan Williams 	{ \
465f1f52e75SDan Williams 		(request)->scu_status = (scu_status_code); \
466f1f52e75SDan Williams 		(request)->sci_status = (sci_status_code); \
467f1f52e75SDan Williams 	}
468f1f52e75SDan Williams 
469f1f52e75SDan Williams /**
470f1f52e75SDan Williams  * SCU_SGL_ZERO() -
471f1f52e75SDan Williams  *
472f1f52e75SDan Williams  * This macro zeros the hardware SGL element data
473f1f52e75SDan Williams  */
474f1f52e75SDan Williams #define SCU_SGL_ZERO(scu_sge) \
475f1f52e75SDan Williams 	{ \
476f1f52e75SDan Williams 		(scu_sge).length = 0; \
477f1f52e75SDan Williams 		(scu_sge).address_lower = 0; \
478f1f52e75SDan Williams 		(scu_sge).address_upper = 0; \
479f1f52e75SDan Williams 		(scu_sge).address_modifier = 0;	\
480f1f52e75SDan Williams 	}
481f1f52e75SDan Williams 
482f1f52e75SDan Williams /**
483f1f52e75SDan Williams  * SCU_SGL_COPY() -
484f1f52e75SDan Williams  *
485f1f52e75SDan Williams  * This macro copys the SGL Element data from the host os to the hardware SGL
486f1f52e75SDan Williams  * elment data
487f1f52e75SDan Williams  */
488f1f52e75SDan Williams #define SCU_SGL_COPY(scu_sge, os_sge) \
489f1f52e75SDan Williams 	{ \
490f1f52e75SDan Williams 		(scu_sge).length = sg_dma_len(sg); \
491f1f52e75SDan Williams 		(scu_sge).address_upper = \
492f1f52e75SDan Williams 			upper_32_bits(sg_dma_address(sg)); \
493f1f52e75SDan Williams 		(scu_sge).address_lower = \
494f1f52e75SDan Williams 			lower_32_bits(sg_dma_address(sg)); \
495f1f52e75SDan Williams 		(scu_sge).address_modifier = 0;	\
496f1f52e75SDan Williams 	}
497f1f52e75SDan Williams 
498f1f52e75SDan Williams enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req);
499f1f52e75SDan Williams enum sci_status scic_sds_io_request_terminate(struct scic_sds_request *sci_req);
500e301370aSEdmund Nadolski enum sci_status
501e301370aSEdmund Nadolski scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
502f1f52e75SDan Williams 				  u32 event_code);
503e301370aSEdmund Nadolski enum sci_status
504e301370aSEdmund Nadolski scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
505f1f52e75SDan Williams 				  u32 frame_index);
506e301370aSEdmund Nadolski enum sci_status
507e301370aSEdmund Nadolski scic_sds_task_request_terminate(struct scic_sds_request *sci_req);
508e301370aSEdmund Nadolski extern enum sci_status
509e301370aSEdmund Nadolski scic_sds_request_complete(struct scic_sds_request *sci_req);
510e301370aSEdmund Nadolski extern enum sci_status
511e301370aSEdmund Nadolski scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req, u32 code);
512f1f52e75SDan Williams 
513f1f52e75SDan Williams /* XXX open code in caller */
514f1f52e75SDan Williams static inline void *scic_request_get_virt_addr(struct scic_sds_request *sci_req,
515f1f52e75SDan Williams 					       dma_addr_t phys_addr)
516f1f52e75SDan Williams {
517f1f52e75SDan Williams 	struct isci_request *ireq = sci_req_to_ireq(sci_req);
518f1f52e75SDan Williams 	dma_addr_t offset;
519f1f52e75SDan Williams 
520f1f52e75SDan Williams 	BUG_ON(phys_addr < ireq->request_daddr);
521f1f52e75SDan Williams 
522f1f52e75SDan Williams 	offset = phys_addr - ireq->request_daddr;
523f1f52e75SDan Williams 
524f1f52e75SDan Williams 	BUG_ON(offset >= sizeof(*ireq));
525f1f52e75SDan Williams 
526f1f52e75SDan Williams 	return (char *)ireq + offset;
527f1f52e75SDan Williams }
528f1f52e75SDan Williams 
529f1f52e75SDan Williams /* XXX open code in caller */
530e301370aSEdmund Nadolski static inline dma_addr_t
531e301370aSEdmund Nadolski scic_io_request_get_dma_addr(struct scic_sds_request *sci_req, void *virt_addr)
532f1f52e75SDan Williams {
533f1f52e75SDan Williams 	struct isci_request *ireq = sci_req_to_ireq(sci_req);
534f1f52e75SDan Williams 
535f1f52e75SDan Williams 	char *requested_addr = (char *)virt_addr;
536f1f52e75SDan Williams 	char *base_addr = (char *)ireq;
537f1f52e75SDan Williams 
538f1f52e75SDan Williams 	BUG_ON(requested_addr < base_addr);
539f1f52e75SDan Williams 	BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
540f1f52e75SDan Williams 
541f1f52e75SDan Williams 	return ireq->request_daddr + (requested_addr - base_addr);
542f1f52e75SDan Williams }
543f1f52e75SDan Williams 
544f1f52e75SDan Williams /**
5456f231ddaSDan Williams  * This function gets the status of the request object.
5466f231ddaSDan Williams  * @request: This parameter points to the isci_request object
5476f231ddaSDan Williams  *
5486f231ddaSDan Williams  * status of the object as a isci_request_status enum.
5496f231ddaSDan Williams  */
550e301370aSEdmund Nadolski static inline enum isci_request_status
551e301370aSEdmund Nadolski isci_request_get_state(struct isci_request *isci_request)
5526f231ddaSDan Williams {
5536f231ddaSDan Williams 	BUG_ON(isci_request == NULL);
5546f231ddaSDan Williams 
5556f231ddaSDan Williams 	/*probably a bad sign...	*/
5566f231ddaSDan Williams 	if (isci_request->status == unallocated)
5576f231ddaSDan Williams 		dev_warn(&isci_request->isci_host->pdev->dev,
5586f231ddaSDan Williams 			 "%s: isci_request->status == unallocated\n",
5596f231ddaSDan Williams 			 __func__);
5606f231ddaSDan Williams 
5616f231ddaSDan Williams 	return isci_request->status;
5626f231ddaSDan Williams }
5636f231ddaSDan Williams 
5646f231ddaSDan Williams 
5656f231ddaSDan Williams /**
5666f231ddaSDan Williams  * isci_request_change_state() - This function sets the status of the request
5676f231ddaSDan Williams  *    object.
5686f231ddaSDan Williams  * @request: This parameter points to the isci_request object
5696f231ddaSDan Williams  * @status: This Parameter is the new status of the object
5706f231ddaSDan Williams  *
5716f231ddaSDan Williams  */
572e301370aSEdmund Nadolski static inline enum isci_request_status
573e301370aSEdmund Nadolski isci_request_change_state(struct isci_request *isci_request,
5746f231ddaSDan Williams 			  enum isci_request_status status)
5756f231ddaSDan Williams {
5766f231ddaSDan Williams 	enum isci_request_status old_state;
5776f231ddaSDan Williams 	unsigned long flags;
5786f231ddaSDan Williams 
5796f231ddaSDan Williams 	dev_dbg(&isci_request->isci_host->pdev->dev,
5806f231ddaSDan Williams 		"%s: isci_request = %p, state = 0x%x\n",
5816f231ddaSDan Williams 		__func__,
5826f231ddaSDan Williams 		isci_request,
5836f231ddaSDan Williams 		status);
5846f231ddaSDan Williams 
5856f231ddaSDan Williams 	BUG_ON(isci_request == NULL);
5866f231ddaSDan Williams 
5876f231ddaSDan Williams 	spin_lock_irqsave(&isci_request->state_lock, flags);
5886f231ddaSDan Williams 	old_state = isci_request->status;
5896f231ddaSDan Williams 	isci_request->status = status;
5906f231ddaSDan Williams 	spin_unlock_irqrestore(&isci_request->state_lock, flags);
5916f231ddaSDan Williams 
5926f231ddaSDan Williams 	return old_state;
5936f231ddaSDan Williams }
5946f231ddaSDan Williams 
5956f231ddaSDan Williams /**
5966f231ddaSDan Williams  * isci_request_change_started_to_newstate() - This function sets the status of
5976f231ddaSDan Williams  *    the request object.
5986f231ddaSDan Williams  * @request: This parameter points to the isci_request object
5996f231ddaSDan Williams  * @status: This Parameter is the new status of the object
6006f231ddaSDan Williams  *
6016f231ddaSDan Williams  * state previous to any change.
6026f231ddaSDan Williams  */
603e301370aSEdmund Nadolski static inline enum isci_request_status
604e301370aSEdmund Nadolski isci_request_change_started_to_newstate(struct isci_request *isci_request,
6056f231ddaSDan Williams 					struct completion *completion_ptr,
6066f231ddaSDan Williams 					enum isci_request_status newstate)
6076f231ddaSDan Williams {
6086f231ddaSDan Williams 	enum isci_request_status old_state;
6096f231ddaSDan Williams 	unsigned long flags;
6106f231ddaSDan Williams 
6116f231ddaSDan Williams 	spin_lock_irqsave(&isci_request->state_lock, flags);
6126f231ddaSDan Williams 
6136f231ddaSDan Williams 	old_state = isci_request->status;
6146f231ddaSDan Williams 
615f219f010SJeff Skirvin 	if (old_state == started || old_state == aborting) {
6166f231ddaSDan Williams 		BUG_ON(isci_request->io_request_completion != NULL);
6176f231ddaSDan Williams 
6186f231ddaSDan Williams 		isci_request->io_request_completion = completion_ptr;
6196f231ddaSDan Williams 		isci_request->status = newstate;
6206f231ddaSDan Williams 	}
621e301370aSEdmund Nadolski 
6226f231ddaSDan Williams 	spin_unlock_irqrestore(&isci_request->state_lock, flags);
6236f231ddaSDan Williams 
6246f231ddaSDan Williams 	dev_dbg(&isci_request->isci_host->pdev->dev,
6256f231ddaSDan Williams 		"%s: isci_request = %p, old_state = 0x%x\n",
6266f231ddaSDan Williams 		__func__,
6276f231ddaSDan Williams 		isci_request,
6286f231ddaSDan Williams 		old_state);
6296f231ddaSDan Williams 
6306f231ddaSDan Williams 	return old_state;
6316f231ddaSDan Williams }
6326f231ddaSDan Williams 
6336f231ddaSDan Williams /**
6346f231ddaSDan Williams  * isci_request_change_started_to_aborted() - This function sets the status of
6356f231ddaSDan Williams  *    the request object.
6366f231ddaSDan Williams  * @request: This parameter points to the isci_request object
6376f231ddaSDan Williams  * @completion_ptr: This parameter is saved as the kernel completion structure
6386f231ddaSDan Williams  *    signalled when the old request completes.
6396f231ddaSDan Williams  *
6406f231ddaSDan Williams  * state previous to any change.
6416f231ddaSDan Williams  */
642e301370aSEdmund Nadolski static inline enum isci_request_status
643e301370aSEdmund Nadolski isci_request_change_started_to_aborted(struct isci_request *isci_request,
6446f231ddaSDan Williams 				       struct completion *completion_ptr)
6456f231ddaSDan Williams {
646e301370aSEdmund Nadolski 	return isci_request_change_started_to_newstate(isci_request,
647e301370aSEdmund Nadolski 						       completion_ptr,
648e301370aSEdmund Nadolski 						       aborted);
6496f231ddaSDan Williams }
6506f231ddaSDan Williams /**
6516f231ddaSDan Williams  * isci_request_free() - This function frees the request object.
6526f231ddaSDan Williams  * @isci_host: This parameter specifies the ISCI host object
6536f231ddaSDan Williams  * @isci_request: This parameter points to the isci_request object
6546f231ddaSDan Williams  *
6556f231ddaSDan Williams  */
656e301370aSEdmund Nadolski static inline void isci_request_free(struct isci_host *isci_host,
6576f231ddaSDan Williams 				     struct isci_request *isci_request)
6586f231ddaSDan Williams {
6596cb4d6b3SBartosz Barcinski 	if (!isci_request)
6606cb4d6b3SBartosz Barcinski 		return;
6616f231ddaSDan Williams 
6626f231ddaSDan Williams 	/* release the dma memory if we fail. */
663e301370aSEdmund Nadolski 	dma_pool_free(isci_host->dma_pool,
664e301370aSEdmund Nadolski 		      isci_request,
6656f231ddaSDan Williams 		      isci_request->request_daddr);
6666f231ddaSDan Williams }
6676f231ddaSDan Williams 
668e301370aSEdmund Nadolski #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
6696f231ddaSDan Williams 
670e301370aSEdmund Nadolski #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
6716f231ddaSDan Williams 
6720d0cf14cSDan Williams struct isci_request *isci_request_alloc_tmf(struct isci_host *ihost,
6736f231ddaSDan Williams 					    struct isci_tmf *isci_tmf,
6746f231ddaSDan Williams 					    gfp_t gfp_flags);
675209fae14SDan Williams int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
676209fae14SDan Williams 			 struct sas_task *task, gfp_t gfp_flags);
677*ddcc7e34SDan Williams void isci_terminate_pending_requests(struct isci_host *ihost,
678980d3aebSDan Williams 				     struct isci_remote_device *idev);
679e301370aSEdmund Nadolski enum sci_status
680e301370aSEdmund Nadolski scic_task_request_construct(struct scic_sds_controller *scic,
681f1f52e75SDan Williams 			    struct scic_sds_remote_device *sci_dev,
682f1f52e75SDan Williams 			    u16 io_tag,
683f1f52e75SDan Williams 			    struct scic_sds_request *sci_req);
684e301370aSEdmund Nadolski enum sci_status
685e301370aSEdmund Nadolski scic_task_request_construct_ssp(struct scic_sds_request *sci_req);
686e301370aSEdmund Nadolski enum sci_status
687e301370aSEdmund Nadolski scic_task_request_construct_sata(struct scic_sds_request *sci_req);
688e301370aSEdmund Nadolski void
689e301370aSEdmund Nadolski scic_stp_io_request_set_ncq_tag(struct scic_sds_request *sci_req, u16 ncq_tag);
690f1f52e75SDan Williams void scic_sds_smp_request_copy_response(struct scic_sds_request *sci_req);
6916f231ddaSDan Williams #endif /* !defined(_ISCI_REQUEST_H_) */
692