/
iSCSITaskQueue.cpp
174 lines (137 loc) · 5.34 KB
/
iSCSITaskQueue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2016, Nareg Sinenian
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "iSCSITaskQueue.h"
#define super IOEventSource
struct iSCSITask {
queue_chain_t queueChain;
UInt32 initiatorTaskTag;
};
OSDefineMetaClassAndStructors(iSCSITaskQueue,IOEventSource);
bool iSCSITaskQueue::init(iSCSIVirtualHBA * owner,
iSCSITaskQueue::Action action,
iSCSISession * session,
iSCSIConnection * connection)
{
// Initialize superclass, check validity and store socket handle
if(!super::init(owner,(IOEventSource::Action) action))
return false;
iSCSITaskQueue::session = session;
iSCSITaskQueue::connection = connection;
// Initialize task queue to store parallel SCSI tasks for processing
queue_init(&taskQueue);
newTask = false;
return true;
}
/*! Queues a new iSCSI task for delayed processing.
* @param initiatorTaskTag the iSCSI task tag associated with the task. */
void iSCSITaskQueue::queueTask(UInt32 initiatorTaskTag)
{
// Signal the workloop thread that work is available only if this is
// the only task in the queue (otherwise the task preceding this is
// being processed; we'll get to this once that's done).
iSCSITask * task = (iSCSITask*)IOMalloc(sizeof(iSCSITask));
task->initiatorTaskTag = initiatorTaskTag;
if(!onThread())
OSDynamicCast(iSCSIVirtualHBA,owner)->GetCommandGate();
bool firstTaskInQueue = false;
if(queue_empty(&taskQueue))
firstTaskInQueue = true;
queue_enter(&taskQueue,task,iSCSITask *,queueChain);
// Signal the workloop to process a new task...
if(firstTaskInQueue) {
newTask = true;
if(getWorkLoop())
signalWorkAvailable();
}
}
/*! Removes a task from the queue (either the task has been successfully
* completed or aborted).
* @return the iSCSI task tag for the task that was just completed. */
UInt32 iSCSITaskQueue::completeCurrentTask()
{
UInt32 taskTag = 0;
iSCSITask * task = NULL;
// Do nothing if the queue is empty
if(queue_empty(&taskQueue))
return taskTag;
// Remove the completed task (at the head of the queue) and then
// move onto the next task if one exists
if(!onThread())
OSDynamicCast(iSCSIVirtualHBA,owner)->GetCommandGate();
queue_remove_first(&taskQueue,task,iSCSITask *, queueChain);
if(task) {
taskTag = task->initiatorTaskTag;
IOFree(task,sizeof(iSCSITask));
}
// If there are still tasks to process let the HBA know...
if(!queue_empty(&taskQueue)) {
newTask = true;
if(getWorkLoop())
signalWorkAvailable();
}
return taskTag;
}
bool iSCSITaskQueue::checkForWork()
{
if(!isEnabled())
return false;
// Check task flag before proceeding
if(!newTask)
return false;
newTask = false;
// Validate action & owner, then call action on our owner & pass in socket
// this function will continue processing the task
if(action && owner) {
UInt32 taskTag;
if(!onThread())
OSDynamicCast(iSCSIVirtualHBA,owner)->GetCommandGate();
if(queue_empty(&taskQueue))
return false;
iSCSITask * task = (iSCSITask *)queue_first(&taskQueue);
taskTag = task->initiatorTaskTag;
(*action)(owner,session,connection,taskTag);
}
// Tell workloop thread not to call us again until we signal again...
return false;
}
/*! Removes all tasks from the queue. */
void iSCSITaskQueue::clearTasksFromQueue()
{
// Ensure the event source is disabled before proceeding...
disable();
// Iterate over queue and clear all tasks (free memory for each task)
iSCSITask * task = NULL;
if(!onThread())
OSDynamicCast(iSCSIVirtualHBA,owner)->GetCommandGate();
while(!queue_empty(&taskQueue))
{
queue_remove_first(&taskQueue,task,iSCSITask *, queueChain);
if(task)
IOFree(task,sizeof(iSCSITask));
}
}