File: task_mgmt.c

package info (click to toggle)
libiscsi 1.20.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,080 kB
  • sloc: ansic: 40,928; xml: 749; sh: 689; makefile: 352
file content (166 lines) | stat: -rw-r--r-- 4,016 bytes parent folder | download | duplicates (2)
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
/*
   Copyright (C) 2011 by Ronnie Sahlberg <ronniesahlberg@gmail.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdio.h>
#include "iscsi.h"
#include "iscsi-private.h"
#include "scsi-lowlevel.h"

int
iscsi_task_mgmt_async(struct iscsi_context *iscsi,
		      int lun, enum iscsi_task_mgmt_funcs function, 
		      uint32_t ritt, uint32_t rcmdsn,
		      iscsi_command_cb cb, void *private_data)
{
	struct iscsi_pdu *pdu;

	if (iscsi->is_loggedin == 0) {
		iscsi_set_error(iscsi, "trying to send task-mgmt while not "
				"logged in");
		return -1;
	}

	pdu = iscsi_allocate_pdu(iscsi,
				 ISCSI_PDU_SCSI_TASK_MANAGEMENT_REQUEST,
				 ISCSI_PDU_SCSI_TASK_MANAGEMENT_RESPONSE,
				 iscsi_itt_post_increment(iscsi),
				 ISCSI_PDU_DROP_ON_RECONNECT);
	if (pdu == NULL) {
		iscsi_set_error(iscsi, "Failed to allocate task mgmt pdu");
		return -1;
	}

	/* immediate flag */
	iscsi_pdu_set_immediate(pdu);

	/* flags */
	iscsi_pdu_set_pduflags(pdu, 0x80 | function);

	/* lun */
	iscsi_pdu_set_lun(pdu, lun);

	/* ritt */
	iscsi_pdu_set_ritt(pdu, ritt);

	/* cmdsn is not increased if Immediate delivery*/
	iscsi_pdu_set_cmdsn(pdu, iscsi->cmdsn);

	/* rcmdsn */
	iscsi_pdu_set_rcmdsn(pdu, rcmdsn);

	pdu->callback     = cb;
	pdu->private_data = private_data;

	if (iscsi_queue_pdu(iscsi, pdu) != 0) {
		iscsi_set_error(iscsi, "failed to queue iscsi taskmgmt pdu");
		iscsi->drv->free_pdu(iscsi, pdu);
		return -1;
	}

	return 0;
}

int
iscsi_process_task_mgmt_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu,
			    struct iscsi_in_pdu *in)
{
	uint32_t response = in->hdr[2];

	if (pdu->callback) {
		pdu->callback(iscsi, SCSI_STATUS_GOOD, &response, pdu->private_data);
	}
	return 0;
}

int
iscsi_task_mgmt_abort_task_async(struct iscsi_context *iscsi,
		      struct scsi_task *task,
		      iscsi_command_cb cb, void *private_data)
{
	return iscsi_task_mgmt_async(iscsi,
		      task->lun, ISCSI_TM_ABORT_TASK,
		      task->itt, task->cmdsn,
		      cb, private_data);
}

int
iscsi_task_mgmt_abort_task_set_async(struct iscsi_context *iscsi,
		      uint32_t lun,
		      iscsi_command_cb cb, void *private_data)
{
	iscsi_scsi_cancel_all_tasks(iscsi);

	return iscsi_task_mgmt_async(iscsi,
		      lun, ISCSI_TM_ABORT_TASK_SET,
		      0xffffffff, 0,
		      cb, private_data);
}

int
iscsi_task_mgmt_lun_reset_async(struct iscsi_context *iscsi,
		      uint32_t lun,
		      iscsi_command_cb cb, void *private_data)
{
	iscsi_scsi_cancel_all_tasks(iscsi);

	return iscsi_task_mgmt_async(iscsi,
		      lun, ISCSI_TM_LUN_RESET,
		      0xffffffff, 0,
		      cb, private_data);
}

int
iscsi_task_mgmt_target_warm_reset_async(struct iscsi_context *iscsi,
		      iscsi_command_cb cb, void *private_data)
{
	iscsi_scsi_cancel_all_tasks(iscsi);

	return iscsi_task_mgmt_async(iscsi,
		      0, ISCSI_TM_TARGET_WARM_RESET,
		      0xffffffff, 0,
		      cb, private_data);
}


int
iscsi_task_mgmt_target_cold_reset_async(struct iscsi_context *iscsi,
		      iscsi_command_cb cb, void *private_data)
{
	iscsi_scsi_cancel_all_tasks(iscsi);

	return iscsi_task_mgmt_async(iscsi,
		      0, ISCSI_TM_TARGET_COLD_RESET,
		      0xffffffff, 0,
		      cb, private_data);
}