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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/* $OpenBSD: vscsi.c,v 1.4 2011/01/06 15:32:47 claudio Exp $ */
/*
* Copyright (c) 2009 Claudio Jeker <claudio@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <scsi/iscsi.h>
#include <scsi/scsi_all.h>
#include <dev/vscsivar.h>
#include <event.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "iscsid.h"
#include "log.h"
struct vscsi {
struct event ev;
int fd;
} v;
struct scsi_task {
struct task task;
int tag;
u_int target;
u_int lun;
size_t datalen;
};
void vscsi_callback(struct connection *, void *, struct pdu *);
void vscsi_dataout(struct session *, struct scsi_task *, u_int32_t, size_t);
void
vscsi_open(char *dev)
{
if ((v.fd = open(dev, O_RDWR)) == -1)
fatal("vscsi_open");
event_set(&v.ev, v.fd, EV_READ|EV_PERSIST, vscsi_dispatch, NULL);
event_add(&v.ev, NULL);
}
void
vscsi_dispatch(int fd, short event, void *arg)
{
struct vscsi_ioc_i2t i2t;
struct iscsi_pdu_scsi_request *sreq;
struct session *s;
struct scsi_task *t;
struct pdu *p;
#if 0
char *buf;
u_int32_t t32;
#endif
if (!(event & EV_READ)) {
log_debug("spurious read call");
return;
}
if (ioctl(v.fd, VSCSI_I2T, &i2t) == -1)
fatal("vscsi_dispatch");
s = initiator_t2s(i2t.target);
if (s == NULL)
fatalx("vscsi_dispatch: unknown target");
if (!(t = calloc(1, sizeof(*t))))
fatal("vscsi_dispatch");
t->tag = i2t.tag;
t->target = i2t.target;
t->lun = i2t.lun;
if (!(p = pdu_new()))
fatal("vscsi_dispatch");
if (!(sreq = pdu_gethdr(p)))
fatal("vscsi_dispatch");
sreq->opcode = ISCSI_OP_SCSI_REQUEST;
/* XXX use untagged commands, dlg sais so */
sreq->flags = ISCSI_SCSI_F_F | ISCSI_SCSI_ATTR_UNTAGGED;
if (i2t.direction == VSCSI_DIR_WRITE)
sreq->flags |= ISCSI_SCSI_F_W;
if (i2t.direction == VSCSI_DIR_READ)
sreq->flags |= ISCSI_SCSI_F_R;
sreq->bytes = htonl(i2t.datalen);
/* LUN handling: currently we only do single level LUNs < 256 */
if (t->lun >= 256)
fatal("vscsi_dispatch: I'm sorry, Dave. "
"I'm afraid I can't do that.");
sreq->lun[1] = t->lun;
bcopy(&i2t.cmd, sreq->cdb, i2t.cmdlen);
#if 0
if (i2t.direction == VSCSI_DIR_WRITE) {
if (!(buf = pdu_alloc(i2t.datalen)))
fatal("vscsi_dispatch");
t32 = htonl(i2t.datalen);
bcopy(&t32, &sreq->ahslen, sizeof(t32));
vscsi_data(VSCSI_DATA_WRITE, i2t.tag, buf, i2t.datalen);
pdu_addbuf(p, buf, i2t.datalen, PDU_DATA);
}
#endif
task_init(&t->task, s, 0, t, vscsi_callback);
task_pdu_add(&t->task, p);
session_task_issue(s, &t->task);
}
void
vscsi_data(unsigned long req, int tag, void *buf, size_t len)
{
struct vscsi_ioc_data data;
data.tag = tag;
data.data = buf;
data.datalen = len;
if (ioctl(v.fd, req, &data) == -1)
fatal("vscsi_data");
}
void
vscsi_status(int tag, int status, void *buf, size_t len)
{
struct vscsi_ioc_t2i t2i;
bzero(&t2i, sizeof(t2i));
t2i.tag = tag;
t2i.status = status;
if (buf) {
if (len > sizeof(t2i.sense))
len = sizeof(t2i.sense);
bcopy(buf, &t2i.sense, len);
}
if (ioctl(v.fd, VSCSI_T2I, &t2i) == -1)
fatal("vscsi_status");
}
void
vscsi_event(unsigned long req, u_int target, u_int lun)
{
struct vscsi_ioc_devevent devev;
devev.target = target;
devev.lun = lun;
if (ioctl(v.fd, req, &devev) == -1)
fatal("vscsi_event");
}
void
vscsi_callback(struct connection *c, void *arg, struct pdu *p)
{
struct scsi_task *t = arg;
struct iscsi_pdu_scsi_response *sresp;
struct iscsi_pdu_rt2 *r2t;
int status = VSCSI_STAT_DONE;
u_char *buf = NULL;
size_t size = 0, n;
int tag;
sresp = pdu_getbuf(p, NULL, PDU_HEADER);
switch (ISCSI_PDU_OPCODE(sresp->opcode)) {
case ISCSI_OP_SCSI_RESPONSE:
task_cleanup(&t->task, c);
tag = t->tag;
free(t);
if (!(sresp->flags & 0x80) || (sresp->flags & 0x06) == 0x06 ||
(sresp->flags & 0x18) == 0x18) {
log_debug("vscsi_callback: bad scsi response");
conn_fail(c);
break;
}
/* XXX handle the various serial numbers */
if (sresp->response) {
status = VSCSI_STAT_ERR;
goto send_status;
}
switch (sresp->status) {
case ISCSI_SCSI_STAT_GOOD:
break;
case ISCSI_SCSI_STAT_CHCK_COND:
status = VSCSI_STAT_SENSE;
/* stupid encoding of sense data in the data segment */
buf = pdu_getbuf(p, &n, PDU_DATA);
if (buf) {
size = buf[0] << 8 | buf[1];
buf += 2;
}
break;
default:
status = VSCSI_STAT_ERR;
break;
}
send_status:
vscsi_status(tag, status, buf, size);
break;
case ISCSI_OP_DATA_IN:
buf = pdu_getbuf(p, &n, PDU_DATA);
size = sresp->datalen[0] << 16 | sresp->datalen[1] << 8 |
sresp->datalen[2];
if (size > n)
fatal("This does not work as it should");
vscsi_data(VSCSI_DATA_READ, t->tag, buf, size);
if (sresp->flags & 1) {
log_debug("and a vscsi_status");
task_cleanup(&t->task, c);
vscsi_status(t->tag, status, NULL, 0);
free(t);
}
break;
case ISCSI_OP_R2T:
task_cleanup(&t->task, c);
r2t = (struct iscsi_pdu_rt2 *)sresp;
if (ntohl(r2t->buffer_offs))
fatalx("vscsi: r2t bummer failure");
size = ntohl(r2t->desired_datalen);
vscsi_dataout(c->session, t, r2t->ttt, size);
break;
default:
log_debug("scsi task: tag %d, target %d lun %d", t->tag,
t->target, t->lun);
log_pdu(p, 1);
}
pdu_free(p);
}
void
vscsi_dataout(struct session *s, struct scsi_task *t, u_int32_t ttt, size_t len)
{
struct pdu *p;
struct iscsi_pdu_data_out *dout;
u_char *buf = NULL;
size_t size, off;
u_int32_t t32, dsn = 0;
for (off = 0; off < len; off += size) {
size = len - off > 8 * 1024 ? 8 * 1024 : len - off;
if (!(p = pdu_new()))
fatal("vscsi_r2t");
if (!(dout = pdu_gethdr(p)))
fatal("vscsi_r2t");
dout->opcode = ISCSI_OP_DATA_OUT;
if (off + size == len)
dout->flags = 0x80; /* XXX magic value: F flag*/
/* LUN handling: currently we only do single level LUNs < 256 */
dout->lun[1] = t->lun;
dout->ttt = ttt;
dout->datasn = htonl(dsn++);
t32 = htonl(size);
bcopy(&t32, &dout->ahslen, sizeof(t32));
dout->buffer_offs = htonl(off);
if (!(buf = pdu_alloc(size)))
fatal("vscsi_r2t");
vscsi_data(VSCSI_DATA_WRITE, t->tag, buf, size);
pdu_addbuf(p, buf, size, PDU_DATA);
task_pdu_add(&t->task, p);
}
session_task_issue(s, &t->task);
}
|