xref: /spdk/lib/iscsi/task.h (revision 73358c99e195547e53dd0617b15e304fd6874701)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef SPDK_ISCSI_TASK_H
36 #define SPDK_ISCSI_TASK_H
37 
38 #include "iscsi/iscsi.h"
39 #include "spdk/scsi.h"
40 
41 struct spdk_iscsi_task {
42 	struct spdk_scsi_task	scsi;
43 
44 	struct spdk_iscsi_conn *conn;
45 	struct spdk_iscsi_pdu *pdu;
46 	uint32_t outstanding_r2t;
47 
48 	uint32_t desired_data_transfer_length;
49 
50 	/* Only valid for Read/Write */
51 	uint32_t bytes_completed;
52 
53 	uint32_t data_out_cnt;
54 
55 	/*
56 	 * Tracks the current offset of large read io.
57 	 */
58 	uint32_t current_datain_offset;
59 
60 	/*
61 	 * next_expected_r2t_offset is used when we receive
62 	 * the DataOUT PDU.
63 	 */
64 	uint32_t next_expected_r2t_offset;
65 
66 	/*
67 	 * Tracks the length of the R2T that is in progress.
68 	 * Used to check that an R2T burst does not exceed
69 	 *  MaxBurstLength.
70 	 */
71 	uint32_t current_r2t_length;
72 
73 	/*
74 	 * next_r2t_offset is used when we are sending the
75 	 * R2T packet to keep track of next offset of r2t.
76 	 */
77 	uint32_t next_r2t_offset;
78 	uint32_t R2TSN;
79 	uint32_t r2t_datasn; /* record next datasn for a r2tsn */
80 	uint32_t acked_r2tsn; /* next r2tsn to be acked */
81 	uint32_t datain_datasn;
82 	uint32_t acked_data_sn; /* next expected datain datasn */
83 	uint32_t ttt;
84 
85 	uint32_t tag;
86 
87 	TAILQ_ENTRY(spdk_iscsi_task) link;
88 
89 	TAILQ_HEAD(subtask_list, spdk_iscsi_task) subtask_list;
90 	TAILQ_ENTRY(spdk_iscsi_task) subtask_link;
91 };
92 
93 static inline void
94 spdk_iscsi_task_put(struct spdk_iscsi_task *task)
95 {
96 	spdk_scsi_task_put(&task->scsi);
97 }
98 
99 static inline struct spdk_iscsi_pdu *
100 spdk_iscsi_task_get_pdu(struct spdk_iscsi_task *task)
101 {
102 	return task->pdu;
103 }
104 
105 static inline void
106 spdk_iscsi_task_set_pdu(struct spdk_iscsi_task *task, struct spdk_iscsi_pdu *pdu)
107 {
108 	task->pdu = pdu;
109 }
110 
111 static inline struct iscsi_bhs *
112 spdk_iscsi_task_get_bhs(struct spdk_iscsi_task *task)
113 {
114 	return &spdk_iscsi_task_get_pdu(task)->bhs;
115 }
116 
117 static inline void
118 spdk_iscsi_task_associate_pdu(struct spdk_iscsi_task *task, struct spdk_iscsi_pdu *pdu)
119 {
120 	spdk_iscsi_task_set_pdu(task, pdu);
121 	pdu->ref++;
122 }
123 
124 static inline void
125 spdk_iscsi_task_disassociate_pdu(struct spdk_iscsi_task *task)
126 {
127 	if (spdk_iscsi_task_get_pdu(task)) {
128 		spdk_put_pdu(spdk_iscsi_task_get_pdu(task));
129 		spdk_iscsi_task_set_pdu(task, NULL);
130 	}
131 }
132 
133 static inline int
134 spdk_iscsi_task_is_immediate(struct spdk_iscsi_task *task)
135 {
136 	struct iscsi_bhs_scsi_req *scsi_req;
137 
138 	scsi_req = (struct iscsi_bhs_scsi_req *)spdk_iscsi_task_get_bhs(task);
139 	return (scsi_req->immediate == 1);
140 }
141 
142 static inline int
143 spdk_iscsi_task_is_read(struct spdk_iscsi_task *task)
144 {
145 	struct iscsi_bhs_scsi_req *scsi_req;
146 
147 	scsi_req = (struct iscsi_bhs_scsi_req *)spdk_iscsi_task_get_bhs(task);
148 	return (scsi_req->read_bit == 1);
149 }
150 
151 static inline uint32_t
152 spdk_iscsi_task_get_cmdsn(struct spdk_iscsi_task *task)
153 {
154 	return spdk_iscsi_task_get_pdu(task)->cmd_sn;
155 }
156 
157 struct spdk_iscsi_task *spdk_iscsi_task_get(struct spdk_iscsi_conn *conn,
158 		struct spdk_iscsi_task *parent,
159 		spdk_scsi_task_cpl cpl_fn);
160 
161 static inline struct spdk_iscsi_task *
162 spdk_iscsi_task_from_scsi_task(struct spdk_scsi_task *task)
163 {
164 	return (struct spdk_iscsi_task *)((uintptr_t)task - offsetof(struct spdk_iscsi_task, scsi));
165 }
166 
167 static inline struct spdk_iscsi_task *
168 spdk_iscsi_task_get_primary(struct spdk_iscsi_task *task)
169 {
170 	struct spdk_scsi_task *scsi_task;
171 	struct spdk_scsi_task *scsi_primary_task;
172 
173 	scsi_task = &task->scsi;
174 	scsi_primary_task = spdk_scsi_task_get_primary(scsi_task);
175 	return spdk_iscsi_task_from_scsi_task(scsi_primary_task);
176 }
177 
178 #endif /* SPDK_ISCSI_TASK_H */
179