xref: /spdk/lib/iscsi/task.h (revision 13a58c41ad5c01eefaf68f049b6c7da2b9d19426)
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 #include "spdk/util.h"
41 
42 struct spdk_iscsi_task {
43 	struct spdk_scsi_task	scsi;
44 
45 	struct spdk_iscsi_task *parent;
46 
47 	uint8_t rsp_scsi_status;
48 	uint8_t rsp_sense_data[32];
49 	size_t rsp_sense_data_len;
50 
51 	struct spdk_iscsi_conn *conn;
52 	struct spdk_iscsi_pdu *pdu;
53 	uint32_t outstanding_r2t;
54 
55 	uint32_t desired_data_transfer_length;
56 
57 	/* Only valid for Read/Write */
58 	uint32_t bytes_completed;
59 
60 	uint32_t data_out_cnt;
61 
62 	/*
63 	 * Tracks the current offset of large read io.
64 	 */
65 	uint32_t current_datain_offset;
66 
67 	/*
68 	 * next_expected_r2t_offset is used when we receive
69 	 * the DataOUT PDU.
70 	 */
71 	uint32_t next_expected_r2t_offset;
72 
73 	/*
74 	 * Tracks the length of the R2T that is in progress.
75 	 * Used to check that an R2T burst does not exceed
76 	 *  MaxBurstLength.
77 	 */
78 	uint32_t current_r2t_length;
79 
80 	/*
81 	 * next_r2t_offset is used when we are sending the
82 	 * R2T packet to keep track of next offset of r2t.
83 	 */
84 	uint32_t next_r2t_offset;
85 	uint32_t R2TSN;
86 	uint32_t r2t_datasn; /* record next datasn for a r2tsn */
87 	uint32_t acked_r2tsn; /* next r2tsn to be acked */
88 	uint32_t datain_datasn;
89 	uint32_t acked_data_sn; /* next expected datain datasn */
90 	uint32_t ttt;
91 
92 	uint32_t tag;
93 
94 	/**
95 	 * Record the lun id just in case the lun is invalid,
96 	 * which will happen when hot removing the lun.
97 	 */
98 	int lun_id;
99 
100 	struct spdk_poller *mgmt_poller;
101 
102 	TAILQ_ENTRY(spdk_iscsi_task) link;
103 
104 	TAILQ_HEAD(subtask_list, spdk_iscsi_task) subtask_list;
105 	TAILQ_ENTRY(spdk_iscsi_task) subtask_link;
106 	bool is_queued; /* is queued in scsi layer for handling */
107 };
108 
109 static inline void
110 spdk_iscsi_task_put(struct spdk_iscsi_task *task)
111 {
112 	spdk_scsi_task_put(&task->scsi);
113 }
114 
115 static inline struct spdk_iscsi_pdu *
116 spdk_iscsi_task_get_pdu(struct spdk_iscsi_task *task)
117 {
118 	return task->pdu;
119 }
120 
121 static inline void
122 spdk_iscsi_task_set_pdu(struct spdk_iscsi_task *task, struct spdk_iscsi_pdu *pdu)
123 {
124 	task->pdu = pdu;
125 }
126 
127 static inline struct iscsi_bhs *
128 spdk_iscsi_task_get_bhs(struct spdk_iscsi_task *task)
129 {
130 	return &spdk_iscsi_task_get_pdu(task)->bhs;
131 }
132 
133 static inline void
134 spdk_iscsi_task_associate_pdu(struct spdk_iscsi_task *task, struct spdk_iscsi_pdu *pdu)
135 {
136 	spdk_iscsi_task_set_pdu(task, pdu);
137 	pdu->ref++;
138 }
139 
140 static inline void
141 spdk_iscsi_task_disassociate_pdu(struct spdk_iscsi_task *task)
142 {
143 	if (spdk_iscsi_task_get_pdu(task)) {
144 		spdk_put_pdu(spdk_iscsi_task_get_pdu(task));
145 		spdk_iscsi_task_set_pdu(task, NULL);
146 	}
147 }
148 
149 static inline int
150 spdk_iscsi_task_is_immediate(struct spdk_iscsi_task *task)
151 {
152 	struct iscsi_bhs_scsi_req *scsi_req;
153 
154 	scsi_req = (struct iscsi_bhs_scsi_req *)spdk_iscsi_task_get_bhs(task);
155 	return (scsi_req->immediate == 1);
156 }
157 
158 static inline int
159 spdk_iscsi_task_is_read(struct spdk_iscsi_task *task)
160 {
161 	struct iscsi_bhs_scsi_req *scsi_req;
162 
163 	scsi_req = (struct iscsi_bhs_scsi_req *)spdk_iscsi_task_get_bhs(task);
164 	return (scsi_req->read_bit == 1);
165 }
166 
167 static inline uint32_t
168 spdk_iscsi_task_get_cmdsn(struct spdk_iscsi_task *task)
169 {
170 	return spdk_iscsi_task_get_pdu(task)->cmd_sn;
171 }
172 
173 struct spdk_iscsi_task *spdk_iscsi_task_get(struct spdk_iscsi_conn *conn,
174 		struct spdk_iscsi_task *parent,
175 		spdk_scsi_task_cpl cpl_fn);
176 
177 static inline struct spdk_iscsi_task *
178 spdk_iscsi_task_from_scsi_task(struct spdk_scsi_task *task)
179 {
180 	return SPDK_CONTAINEROF(task, struct spdk_iscsi_task, scsi);
181 }
182 
183 static inline struct spdk_iscsi_task *
184 spdk_iscsi_task_get_primary(struct spdk_iscsi_task *task)
185 {
186 	if (task->parent) {
187 		return task->parent;
188 	} else {
189 		return task;
190 	}
191 }
192 
193 #endif /* SPDK_ISCSI_TASK_H */
194