root / hw / scsi / spapr_vscsi.c @ fd506b4f
History | View | Annotate | Download (28.5 kB)
1 |
/*
|
---|---|
2 |
* QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
|
3 |
*
|
4 |
* PAPR Virtual SCSI, aka ibmvscsi
|
5 |
*
|
6 |
* Copyright (c) 2010,2011 Benjamin Herrenschmidt, IBM Corporation.
|
7 |
*
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 |
* of this software and associated documentation files (the "Software"), to deal
|
10 |
* in the Software without restriction, including without limitation the rights
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 |
* copies of the Software, and to permit persons to whom the Software is
|
13 |
* furnished to do so, subject to the following conditions:
|
14 |
*
|
15 |
* The above copyright notice and this permission notice shall be included in
|
16 |
* all copies or substantial portions of the Software.
|
17 |
*
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
21 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24 |
* THE SOFTWARE.
|
25 |
*
|
26 |
* TODO:
|
27 |
*
|
28 |
* - Cleanups :-)
|
29 |
* - Sort out better how to assign devices to VSCSI instances
|
30 |
* - Fix residual counts
|
31 |
* - Add indirect descriptors support
|
32 |
* - Maybe do autosense (PAPR seems to mandate it, linux doesn't care)
|
33 |
*/
|
34 |
#include "hw/hw.h" |
35 |
#include "hw/scsi/scsi.h" |
36 |
#include "block/scsi.h" |
37 |
#include "srp.h" |
38 |
#include "hw/qdev.h" |
39 |
#include "hw/ppc/spapr.h" |
40 |
#include "hw/ppc/spapr_vio.h" |
41 |
#include "viosrp.h" |
42 |
|
43 |
#include <libfdt.h> |
44 |
|
45 |
/*#define DEBUG_VSCSI*/
|
46 |
|
47 |
#ifdef DEBUG_VSCSI
|
48 |
#define dprintf(fmt, ...) \
|
49 |
do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
50 |
#else
|
51 |
#define dprintf(fmt, ...) \
|
52 |
do { } while (0) |
53 |
#endif
|
54 |
|
55 |
/*
|
56 |
* Virtual SCSI device
|
57 |
*/
|
58 |
|
59 |
/* Random numbers */
|
60 |
#define VSCSI_MAX_SECTORS 4096 |
61 |
#define VSCSI_REQ_LIMIT 24 |
62 |
|
63 |
#define SCSI_SENSE_BUF_SIZE 96 |
64 |
#define SRP_RSP_SENSE_DATA_LEN 18 |
65 |
|
66 |
typedef union vscsi_crq { |
67 |
struct viosrp_crq s;
|
68 |
uint8_t raw[16];
|
69 |
} vscsi_crq; |
70 |
|
71 |
typedef struct vscsi_req { |
72 |
vscsi_crq crq; |
73 |
union viosrp_iu iu;
|
74 |
|
75 |
/* SCSI request tracking */
|
76 |
SCSIRequest *sreq; |
77 |
uint32_t qtag; /* qemu tag != srp tag */
|
78 |
int lun;
|
79 |
int active;
|
80 |
long data_len;
|
81 |
int writing;
|
82 |
int senselen;
|
83 |
uint8_t sense[SCSI_SENSE_BUF_SIZE]; |
84 |
|
85 |
/* RDMA related bits */
|
86 |
uint8_t dma_fmt; |
87 |
struct srp_direct_buf ext_desc;
|
88 |
struct srp_direct_buf *cur_desc;
|
89 |
struct srp_indirect_buf *ind_desc;
|
90 |
int local_desc;
|
91 |
int total_desc;
|
92 |
} vscsi_req; |
93 |
|
94 |
#define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi" |
95 |
#define VIO_SPAPR_VSCSI_DEVICE(obj) \
|
96 |
OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE) |
97 |
|
98 |
typedef struct { |
99 |
VIOsPAPRDevice vdev; |
100 |
SCSIBus bus; |
101 |
vscsi_req reqs[VSCSI_REQ_LIMIT]; |
102 |
} VSCSIState; |
103 |
|
104 |
static struct vscsi_req *vscsi_get_req(VSCSIState *s) |
105 |
{ |
106 |
vscsi_req *req; |
107 |
int i;
|
108 |
|
109 |
for (i = 0; i < VSCSI_REQ_LIMIT; i++) { |
110 |
req = &s->reqs[i]; |
111 |
if (!req->active) {
|
112 |
memset(req, 0, sizeof(*req)); |
113 |
req->qtag = i; |
114 |
req->active = 1;
|
115 |
return req;
|
116 |
} |
117 |
} |
118 |
return NULL; |
119 |
} |
120 |
|
121 |
static void vscsi_put_req(vscsi_req *req) |
122 |
{ |
123 |
if (req->sreq != NULL) { |
124 |
scsi_req_unref(req->sreq); |
125 |
} |
126 |
req->sreq = NULL;
|
127 |
req->active = 0;
|
128 |
} |
129 |
|
130 |
static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun) |
131 |
{ |
132 |
int channel = 0, id = 0; |
133 |
|
134 |
retry:
|
135 |
switch (srp_lun >> 62) { |
136 |
case 0: |
137 |
if ((srp_lun >> 56) != 0) { |
138 |
channel = (srp_lun >> 56) & 0x3f; |
139 |
id = (srp_lun >> 48) & 0xff; |
140 |
srp_lun <<= 16;
|
141 |
goto retry;
|
142 |
} |
143 |
*lun = (srp_lun >> 48) & 0xff; |
144 |
break;
|
145 |
|
146 |
case 1: |
147 |
*lun = (srp_lun >> 48) & 0x3fff; |
148 |
break;
|
149 |
case 2: |
150 |
channel = (srp_lun >> 53) & 0x7; |
151 |
id = (srp_lun >> 56) & 0x3f; |
152 |
*lun = (srp_lun >> 48) & 0x1f; |
153 |
break;
|
154 |
case 3: |
155 |
*lun = -1;
|
156 |
return NULL; |
157 |
default:
|
158 |
abort(); |
159 |
} |
160 |
|
161 |
return scsi_device_find(bus, channel, id, *lun);
|
162 |
} |
163 |
|
164 |
static int vscsi_send_iu(VSCSIState *s, vscsi_req *req, |
165 |
uint64_t length, uint8_t format) |
166 |
{ |
167 |
long rc, rc1;
|
168 |
|
169 |
/* First copy the SRP */
|
170 |
rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr, |
171 |
&req->iu, length); |
172 |
if (rc) {
|
173 |
fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
|
174 |
} |
175 |
|
176 |
req->crq.s.valid = 0x80;
|
177 |
req->crq.s.format = format; |
178 |
req->crq.s.reserved = 0x00;
|
179 |
req->crq.s.timeout = cpu_to_be16(0x0000);
|
180 |
req->crq.s.IU_length = cpu_to_be16(length); |
181 |
req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
|
182 |
|
183 |
if (rc == 0) { |
184 |
req->crq.s.status = 0x99; /* Just needs to be non-zero */ |
185 |
} else {
|
186 |
req->crq.s.status = 0x00;
|
187 |
} |
188 |
|
189 |
rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw); |
190 |
if (rc1) {
|
191 |
fprintf(stderr, "vscsi_send_iu: Error sending response\n");
|
192 |
return rc1;
|
193 |
} |
194 |
|
195 |
return rc;
|
196 |
} |
197 |
|
198 |
static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req, |
199 |
uint8_t key, uint8_t asc, uint8_t ascq) |
200 |
{ |
201 |
req->senselen = SRP_RSP_SENSE_DATA_LEN; |
202 |
|
203 |
/* Valid bit and 'current errors' */
|
204 |
req->sense[0] = (0x1 << 7 | 0x70); |
205 |
/* Sense key */
|
206 |
req->sense[2] = key;
|
207 |
/* Additional sense length */
|
208 |
req->sense[7] = 0xa; /* 10 bytes */ |
209 |
/* Additional sense code */
|
210 |
req->sense[12] = asc;
|
211 |
req->sense[13] = ascq;
|
212 |
} |
213 |
|
214 |
static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req, |
215 |
uint8_t status, int32_t res_in, int32_t res_out) |
216 |
{ |
217 |
union viosrp_iu *iu = &req->iu;
|
218 |
uint64_t tag = iu->srp.rsp.tag; |
219 |
int total_len = sizeof(iu->srp.rsp); |
220 |
|
221 |
dprintf("VSCSI: Sending resp status: 0x%x, "
|
222 |
"res_in: %d, res_out: %d\n", status, res_in, res_out);
|
223 |
|
224 |
memset(iu, 0, sizeof(struct srp_rsp)); |
225 |
iu->srp.rsp.opcode = SRP_RSP; |
226 |
iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
|
227 |
iu->srp.rsp.tag = tag; |
228 |
|
229 |
/* Handle residuals */
|
230 |
if (res_in < 0) { |
231 |
iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER; |
232 |
res_in = -res_in; |
233 |
} else if (res_in) { |
234 |
iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER; |
235 |
} |
236 |
if (res_out < 0) { |
237 |
iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER; |
238 |
res_out = -res_out; |
239 |
} else if (res_out) { |
240 |
iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER; |
241 |
} |
242 |
iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in); |
243 |
iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out); |
244 |
|
245 |
/* We don't do response data */
|
246 |
/* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
|
247 |
iu->srp.rsp.resp_data_len = cpu_to_be32(0);
|
248 |
|
249 |
/* Handle success vs. failure */
|
250 |
iu->srp.rsp.status = status; |
251 |
if (status) {
|
252 |
iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x04) >> 2; |
253 |
if (req->senselen) {
|
254 |
req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID; |
255 |
req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen); |
256 |
memcpy(req->iu.srp.rsp.data, req->sense, req->senselen); |
257 |
total_len += req->senselen; |
258 |
} |
259 |
} else {
|
260 |
iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x02) >> 1; |
261 |
} |
262 |
|
263 |
vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT); |
264 |
return 0; |
265 |
} |
266 |
|
267 |
static inline void vscsi_swap_desc(struct srp_direct_buf *desc) |
268 |
{ |
269 |
desc->va = be64_to_cpu(desc->va); |
270 |
desc->len = be32_to_cpu(desc->len); |
271 |
} |
272 |
|
273 |
static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req, |
274 |
uint8_t *buf, uint32_t len) |
275 |
{ |
276 |
struct srp_direct_buf *md = req->cur_desc;
|
277 |
uint32_t llen; |
278 |
int rc = 0; |
279 |
|
280 |
dprintf("VSCSI: direct segment 0x%x bytes, va=0x%llx desc len=0x%x\n",
|
281 |
len, (unsigned long long)md->va, md->len); |
282 |
|
283 |
llen = MIN(len, md->len); |
284 |
if (llen) {
|
285 |
if (req->writing) { /* writing = to device = reading from memory */ |
286 |
rc = spapr_vio_dma_read(&s->vdev, md->va, buf, llen); |
287 |
} else {
|
288 |
rc = spapr_vio_dma_write(&s->vdev, md->va, buf, llen); |
289 |
} |
290 |
} |
291 |
md->len -= llen; |
292 |
md->va += llen; |
293 |
|
294 |
if (rc) {
|
295 |
return -1; |
296 |
} |
297 |
return llen;
|
298 |
} |
299 |
|
300 |
static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req, |
301 |
uint8_t *buf, uint32_t len) |
302 |
{ |
303 |
struct srp_direct_buf *td = &req->ind_desc->table_desc;
|
304 |
struct srp_direct_buf *md = req->cur_desc;
|
305 |
int rc = 0; |
306 |
uint32_t llen, total = 0;
|
307 |
|
308 |
dprintf("VSCSI: indirect segment 0x%x bytes, td va=0x%llx len=0x%x\n",
|
309 |
len, (unsigned long long)td->va, td->len); |
310 |
|
311 |
/* While we have data ... */
|
312 |
while (len) {
|
313 |
/* If we have a descriptor but it's empty, go fetch a new one */
|
314 |
if (md && md->len == 0) { |
315 |
/* More local available, use one */
|
316 |
if (req->local_desc) {
|
317 |
md = ++req->cur_desc; |
318 |
--req->local_desc; |
319 |
--req->total_desc; |
320 |
td->va += sizeof(struct srp_direct_buf); |
321 |
} else {
|
322 |
md = req->cur_desc = NULL;
|
323 |
} |
324 |
} |
325 |
/* No descriptor at hand, fetch one */
|
326 |
if (!md) {
|
327 |
if (!req->total_desc) {
|
328 |
dprintf("VSCSI: Out of descriptors !\n");
|
329 |
break;
|
330 |
} |
331 |
md = req->cur_desc = &req->ext_desc; |
332 |
dprintf("VSCSI: Reading desc from 0x%llx\n",
|
333 |
(unsigned long long)td->va); |
334 |
rc = spapr_vio_dma_read(&s->vdev, td->va, md, |
335 |
sizeof(struct srp_direct_buf)); |
336 |
if (rc) {
|
337 |
dprintf("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n",
|
338 |
rc); |
339 |
break;
|
340 |
} |
341 |
vscsi_swap_desc(md); |
342 |
td->va += sizeof(struct srp_direct_buf); |
343 |
--req->total_desc; |
344 |
} |
345 |
dprintf("VSCSI: [desc va=0x%llx,len=0x%x] remaining=0x%x\n",
|
346 |
(unsigned long long)md->va, md->len, len); |
347 |
|
348 |
/* Perform transfer */
|
349 |
llen = MIN(len, md->len); |
350 |
if (req->writing) { /* writing = to device = reading from memory */ |
351 |
rc = spapr_vio_dma_read(&s->vdev, md->va, buf, llen); |
352 |
} else {
|
353 |
rc = spapr_vio_dma_write(&s->vdev, md->va, buf, llen); |
354 |
} |
355 |
if (rc) {
|
356 |
dprintf("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n", req->writing, rc);
|
357 |
break;
|
358 |
} |
359 |
dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
|
360 |
buf[0], buf[1], buf[2], buf[3]); |
361 |
|
362 |
len -= llen; |
363 |
buf += llen; |
364 |
total += llen; |
365 |
md->va += llen; |
366 |
md->len -= llen; |
367 |
} |
368 |
return rc ? -1 : total; |
369 |
} |
370 |
|
371 |
static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req, |
372 |
int writing, uint8_t *buf, uint32_t len)
|
373 |
{ |
374 |
int err = 0; |
375 |
|
376 |
switch (req->dma_fmt) {
|
377 |
case SRP_NO_DATA_DESC:
|
378 |
dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len);
|
379 |
break;
|
380 |
case SRP_DATA_DESC_DIRECT:
|
381 |
err = vscsi_srp_direct_data(s, req, buf, len); |
382 |
break;
|
383 |
case SRP_DATA_DESC_INDIRECT:
|
384 |
err = vscsi_srp_indirect_data(s, req, buf, len); |
385 |
break;
|
386 |
} |
387 |
return err;
|
388 |
} |
389 |
|
390 |
/* Bits from linux srp */
|
391 |
static int data_out_desc_size(struct srp_cmd *cmd) |
392 |
{ |
393 |
int size = 0; |
394 |
uint8_t fmt = cmd->buf_fmt >> 4;
|
395 |
|
396 |
switch (fmt) {
|
397 |
case SRP_NO_DATA_DESC:
|
398 |
break;
|
399 |
case SRP_DATA_DESC_DIRECT:
|
400 |
size = sizeof(struct srp_direct_buf); |
401 |
break;
|
402 |
case SRP_DATA_DESC_INDIRECT:
|
403 |
size = sizeof(struct srp_indirect_buf) + |
404 |
sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt; |
405 |
break;
|
406 |
default:
|
407 |
break;
|
408 |
} |
409 |
return size;
|
410 |
} |
411 |
|
412 |
static int vscsi_preprocess_desc(vscsi_req *req) |
413 |
{ |
414 |
struct srp_cmd *cmd = &req->iu.srp.cmd;
|
415 |
int offset, i;
|
416 |
|
417 |
offset = cmd->add_cdb_len & ~3;
|
418 |
|
419 |
if (req->writing) {
|
420 |
req->dma_fmt = cmd->buf_fmt >> 4;
|
421 |
} else {
|
422 |
offset += data_out_desc_size(cmd); |
423 |
req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1); |
424 |
} |
425 |
|
426 |
switch (req->dma_fmt) {
|
427 |
case SRP_NO_DATA_DESC:
|
428 |
break;
|
429 |
case SRP_DATA_DESC_DIRECT:
|
430 |
req->cur_desc = (struct srp_direct_buf *)(cmd->add_data + offset);
|
431 |
req->total_desc = req->local_desc = 1;
|
432 |
vscsi_swap_desc(req->cur_desc); |
433 |
dprintf("VSCSI: using direct RDMA %s, 0x%x bytes MD: 0x%llx\n",
|
434 |
req->writing ? "write" : "read", |
435 |
req->cur_desc->len, (unsigned long long)req->cur_desc->va); |
436 |
break;
|
437 |
case SRP_DATA_DESC_INDIRECT:
|
438 |
req->ind_desc = (struct srp_indirect_buf *)(cmd->add_data + offset);
|
439 |
vscsi_swap_desc(&req->ind_desc->table_desc); |
440 |
req->total_desc = req->ind_desc->table_desc.len / |
441 |
sizeof(struct srp_direct_buf); |
442 |
req->local_desc = req->writing ? cmd->data_out_desc_cnt : |
443 |
cmd->data_in_desc_cnt; |
444 |
for (i = 0; i < req->local_desc; i++) { |
445 |
vscsi_swap_desc(&req->ind_desc->desc_list[i]); |
446 |
} |
447 |
req->cur_desc = req->local_desc ? &req->ind_desc->desc_list[0] : NULL; |
448 |
dprintf("VSCSI: using indirect RDMA %s, 0x%x bytes %d descs "
|
449 |
"(%d local) VA: 0x%llx\n",
|
450 |
req->writing ? "read" : "write", |
451 |
be32_to_cpu(req->ind_desc->len), |
452 |
req->total_desc, req->local_desc, |
453 |
(unsigned long long)req->ind_desc->table_desc.va); |
454 |
break;
|
455 |
default:
|
456 |
fprintf(stderr, |
457 |
"vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
|
458 |
return -1; |
459 |
} |
460 |
|
461 |
return 0; |
462 |
} |
463 |
|
464 |
/* Callback to indicate that the SCSI layer has completed a transfer. */
|
465 |
static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len) |
466 |
{ |
467 |
VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent); |
468 |
vscsi_req *req = sreq->hba_private; |
469 |
uint8_t *buf; |
470 |
int rc = 0; |
471 |
|
472 |
dprintf("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
|
473 |
sreq->tag, len, req); |
474 |
if (req == NULL) { |
475 |
fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
|
476 |
return;
|
477 |
} |
478 |
|
479 |
if (len) {
|
480 |
buf = scsi_req_get_buf(sreq); |
481 |
rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len); |
482 |
} |
483 |
if (rc < 0) { |
484 |
fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
|
485 |
vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0); |
486 |
scsi_req_abort(req->sreq, CHECK_CONDITION); |
487 |
return;
|
488 |
} |
489 |
|
490 |
/* Start next chunk */
|
491 |
req->data_len -= rc; |
492 |
scsi_req_continue(sreq); |
493 |
} |
494 |
|
495 |
/* Callback to indicate that the SCSI layer has completed a transfer. */
|
496 |
static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid) |
497 |
{ |
498 |
VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent); |
499 |
vscsi_req *req = sreq->hba_private; |
500 |
int32_t res_in = 0, res_out = 0; |
501 |
|
502 |
dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x status=0x%x, req=%p\n",
|
503 |
reason, sreq->tag, status, req); |
504 |
if (req == NULL) { |
505 |
fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
|
506 |
return;
|
507 |
} |
508 |
|
509 |
if (status == CHECK_CONDITION) {
|
510 |
req->senselen = scsi_req_get_sense(req->sreq, req->sense, |
511 |
sizeof(req->sense));
|
512 |
dprintf("VSCSI: Sense data, %d bytes:\n", len);
|
513 |
dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
|
514 |
req->sense[0], req->sense[1], req->sense[2], req->sense[3], |
515 |
req->sense[4], req->sense[5], req->sense[6], req->sense[7]); |
516 |
dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
|
517 |
req->sense[8], req->sense[9], req->sense[10], req->sense[11], |
518 |
req->sense[12], req->sense[13], req->sense[14], req->sense[15]); |
519 |
} |
520 |
|
521 |
dprintf("VSCSI: Command complete err=%d\n", status);
|
522 |
if (status == 0) { |
523 |
/* We handle overflows, not underflows for normal commands,
|
524 |
* but hopefully nobody cares
|
525 |
*/
|
526 |
if (req->writing) {
|
527 |
res_out = req->data_len; |
528 |
} else {
|
529 |
res_in = req->data_len; |
530 |
} |
531 |
} |
532 |
vscsi_send_rsp(s, req, status, res_in, res_out); |
533 |
vscsi_put_req(req); |
534 |
} |
535 |
|
536 |
static void vscsi_request_cancelled(SCSIRequest *sreq) |
537 |
{ |
538 |
vscsi_req *req = sreq->hba_private; |
539 |
|
540 |
vscsi_put_req(req); |
541 |
} |
542 |
|
543 |
static void vscsi_process_login(VSCSIState *s, vscsi_req *req) |
544 |
{ |
545 |
union viosrp_iu *iu = &req->iu;
|
546 |
struct srp_login_rsp *rsp = &iu->srp.login_rsp;
|
547 |
uint64_t tag = iu->srp.rsp.tag; |
548 |
|
549 |
dprintf("VSCSI: Got login, sendin response !\n");
|
550 |
|
551 |
/* TODO handle case that requested size is wrong and
|
552 |
* buffer format is wrong
|
553 |
*/
|
554 |
memset(iu, 0, sizeof(struct srp_login_rsp)); |
555 |
rsp->opcode = SRP_LOGIN_RSP; |
556 |
/* Don't advertise quite as many request as we support to
|
557 |
* keep room for management stuff etc...
|
558 |
*/
|
559 |
rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
|
560 |
rsp->tag = tag; |
561 |
rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu)); |
562 |
rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu)); |
563 |
/* direct and indirect */
|
564 |
rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT); |
565 |
|
566 |
vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
|
567 |
} |
568 |
|
569 |
static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) |
570 |
{ |
571 |
uint8_t *cdb = req->iu.srp.cmd.cdb; |
572 |
uint8_t resp_data[36];
|
573 |
int rc, len, alen;
|
574 |
|
575 |
/* We dont do EVPD. Also check that page_code is 0 */
|
576 |
if ((cdb[1] & 0x01) || (cdb[1] & 0x01) || cdb[2] != 0) { |
577 |
/* Send INVALID FIELD IN CDB */
|
578 |
vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0); |
579 |
vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); |
580 |
return;
|
581 |
} |
582 |
alen = cdb[3];
|
583 |
alen = (alen << 8) | cdb[4]; |
584 |
len = MIN(alen, 36);
|
585 |
|
586 |
/* Fake up inquiry using PQ=3 */
|
587 |
memset(resp_data, 0, 36); |
588 |
resp_data[0] = 0x7f; /* Not capable of supporting a device here */ |
589 |
resp_data[2] = 0x06; /* SPS-4 */ |
590 |
resp_data[3] = 0x02; /* Resp data format */ |
591 |
resp_data[4] = 36 - 5; /* Additional length */ |
592 |
resp_data[7] = 0x10; /* Sync transfers */ |
593 |
memcpy(&resp_data[16], "QEMU EMPTY ", 16); |
594 |
memcpy(&resp_data[8], "QEMU ", 8); |
595 |
|
596 |
req->writing = 0;
|
597 |
vscsi_preprocess_desc(req); |
598 |
rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
|
599 |
if (rc < 0) { |
600 |
vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0); |
601 |
vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); |
602 |
} else {
|
603 |
vscsi_send_rsp(s, req, 0, 36 - rc, 0); |
604 |
} |
605 |
} |
606 |
|
607 |
static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req) |
608 |
{ |
609 |
union srp_iu *srp = &req->iu.srp;
|
610 |
SCSIDevice *sdev; |
611 |
int n, lun;
|
612 |
|
613 |
sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun); |
614 |
if (!sdev) {
|
615 |
dprintf("VSCSI: Command for lun %08" PRIx64 " with no drive\n", be64_to_cpu(srp->cmd.lun)); |
616 |
if (srp->cmd.cdb[0] == INQUIRY) { |
617 |
vscsi_inquiry_no_target(s, req); |
618 |
} else {
|
619 |
vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00); |
620 |
vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); |
621 |
} return 1; |
622 |
} |
623 |
|
624 |
req->lun = lun; |
625 |
req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req); |
626 |
n = scsi_req_enqueue(req->sreq); |
627 |
|
628 |
dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x ID %d LUN %d ret: %d\n",
|
629 |
req->qtag, srp->cmd.cdb[0], id, lun, n);
|
630 |
|
631 |
if (n) {
|
632 |
/* Transfer direction must be set before preprocessing the
|
633 |
* descriptors
|
634 |
*/
|
635 |
req->writing = (n < 1);
|
636 |
|
637 |
/* Preprocess RDMA descriptors */
|
638 |
vscsi_preprocess_desc(req); |
639 |
|
640 |
/* Get transfer direction and initiate transfer */
|
641 |
if (n > 0) { |
642 |
req->data_len = n; |
643 |
} else if (n < 0) { |
644 |
req->data_len = -n; |
645 |
} |
646 |
scsi_req_continue(req->sreq); |
647 |
} |
648 |
/* Don't touch req here, it may have been recycled already */
|
649 |
|
650 |
return 0; |
651 |
} |
652 |
|
653 |
static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req) |
654 |
{ |
655 |
union viosrp_iu *iu = &req->iu;
|
656 |
int fn;
|
657 |
|
658 |
fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
|
659 |
iu->srp.tsk_mgmt.tsk_mgmt_func); |
660 |
|
661 |
switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
|
662 |
#if 0 /* We really don't deal with these for now */
|
663 |
case SRP_TSK_ABORT_TASK:
|
664 |
fn = ABORT_TASK;
|
665 |
break;
|
666 |
case SRP_TSK_ABORT_TASK_SET:
|
667 |
fn = ABORT_TASK_SET;
|
668 |
break;
|
669 |
case SRP_TSK_CLEAR_TASK_SET:
|
670 |
fn = CLEAR_TASK_SET;
|
671 |
break;
|
672 |
case SRP_TSK_LUN_RESET:
|
673 |
fn = LOGICAL_UNIT_RESET;
|
674 |
break;
|
675 |
case SRP_TSK_CLEAR_ACA:
|
676 |
fn = CLEAR_ACA;
|
677 |
break;
|
678 |
#endif
|
679 |
default:
|
680 |
fn = 0;
|
681 |
} |
682 |
if (fn) {
|
683 |
/* XXX Send/Handle target task management */
|
684 |
; |
685 |
} else {
|
686 |
vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x20, 0); |
687 |
vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); |
688 |
} |
689 |
return !fn;
|
690 |
} |
691 |
|
692 |
static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req) |
693 |
{ |
694 |
union srp_iu *srp = &req->iu.srp;
|
695 |
int done = 1; |
696 |
uint8_t opcode = srp->rsp.opcode; |
697 |
|
698 |
switch (opcode) {
|
699 |
case SRP_LOGIN_REQ:
|
700 |
vscsi_process_login(s, req); |
701 |
break;
|
702 |
case SRP_TSK_MGMT:
|
703 |
done = vscsi_process_tsk_mgmt(s, req); |
704 |
break;
|
705 |
case SRP_CMD:
|
706 |
done = vscsi_queue_cmd(s, req); |
707 |
break;
|
708 |
case SRP_LOGIN_RSP:
|
709 |
case SRP_I_LOGOUT:
|
710 |
case SRP_T_LOGOUT:
|
711 |
case SRP_RSP:
|
712 |
case SRP_CRED_REQ:
|
713 |
case SRP_CRED_RSP:
|
714 |
case SRP_AER_REQ:
|
715 |
case SRP_AER_RSP:
|
716 |
fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
|
717 |
break;
|
718 |
default:
|
719 |
fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
|
720 |
} |
721 |
|
722 |
return done;
|
723 |
} |
724 |
|
725 |
static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) |
726 |
{ |
727 |
struct viosrp_adapter_info *sinfo;
|
728 |
struct mad_adapter_info_data info;
|
729 |
int rc;
|
730 |
|
731 |
sinfo = &req->iu.mad.adapter_info; |
732 |
|
733 |
#if 0 /* What for ? */
|
734 |
rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
|
735 |
&info, be16_to_cpu(sinfo->common.length));
|
736 |
if (rc) {
|
737 |
fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
|
738 |
}
|
739 |
#endif
|
740 |
memset(&info, 0, sizeof(info)); |
741 |
strcpy(info.srp_version, SRP_VERSION); |
742 |
memcpy(info.partition_name, "qemu", sizeof("qemu")); |
743 |
info.partition_number = cpu_to_be32(0);
|
744 |
info.mad_version = cpu_to_be32(1);
|
745 |
info.os_type = cpu_to_be32(2);
|
746 |
info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9); |
747 |
|
748 |
rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer), |
749 |
&info, be16_to_cpu(sinfo->common.length)); |
750 |
if (rc) {
|
751 |
fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
|
752 |
} |
753 |
|
754 |
sinfo->common.status = rc ? cpu_to_be32(1) : 0; |
755 |
|
756 |
return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT); |
757 |
} |
758 |
|
759 |
static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req) |
760 |
{ |
761 |
union mad_iu *mad = &req->iu.mad;
|
762 |
|
763 |
switch (be32_to_cpu(mad->empty_iu.common.type)) {
|
764 |
case VIOSRP_EMPTY_IU_TYPE:
|
765 |
fprintf(stderr, "Unsupported EMPTY MAD IU\n");
|
766 |
break;
|
767 |
case VIOSRP_ERROR_LOG_TYPE:
|
768 |
fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
|
769 |
mad->error_log.common.status = cpu_to_be16(1);
|
770 |
vscsi_send_iu(s, req, sizeof(mad->error_log), VIOSRP_MAD_FORMAT);
|
771 |
break;
|
772 |
case VIOSRP_ADAPTER_INFO_TYPE:
|
773 |
vscsi_send_adapter_info(s, req); |
774 |
break;
|
775 |
case VIOSRP_HOST_CONFIG_TYPE:
|
776 |
mad->host_config.common.status = cpu_to_be16(1);
|
777 |
vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
|
778 |
break;
|
779 |
default:
|
780 |
fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
|
781 |
be32_to_cpu(mad->empty_iu.common.type)); |
782 |
} |
783 |
|
784 |
return 1; |
785 |
} |
786 |
|
787 |
static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq) |
788 |
{ |
789 |
vscsi_req *req; |
790 |
int done;
|
791 |
|
792 |
req = vscsi_get_req(s); |
793 |
if (req == NULL) { |
794 |
fprintf(stderr, "VSCSI: Failed to get a request !\n");
|
795 |
return;
|
796 |
} |
797 |
|
798 |
/* We only support a limited number of descriptors, we know
|
799 |
* the ibmvscsi driver uses up to 10 max, so it should fit
|
800 |
* in our 256 bytes IUs. If not we'll have to increase the size
|
801 |
* of the structure.
|
802 |
*/
|
803 |
if (crq->s.IU_length > sizeof(union viosrp_iu)) { |
804 |
fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
|
805 |
crq->s.IU_length); |
806 |
vscsi_put_req(req); |
807 |
return;
|
808 |
} |
809 |
|
810 |
/* XXX Handle failure differently ? */
|
811 |
if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
|
812 |
crq->s.IU_length)) { |
813 |
fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
|
814 |
vscsi_put_req(req); |
815 |
return;
|
816 |
} |
817 |
memcpy(&req->crq, crq, sizeof(vscsi_crq));
|
818 |
|
819 |
if (crq->s.format == VIOSRP_MAD_FORMAT) {
|
820 |
done = vscsi_handle_mad_req(s, req); |
821 |
} else {
|
822 |
done = vscsi_handle_srp_req(s, req); |
823 |
} |
824 |
|
825 |
if (done) {
|
826 |
vscsi_put_req(req); |
827 |
} |
828 |
} |
829 |
|
830 |
|
831 |
static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data) |
832 |
{ |
833 |
VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev); |
834 |
vscsi_crq crq; |
835 |
|
836 |
memcpy(crq.raw, crq_data, 16);
|
837 |
crq.s.timeout = be16_to_cpu(crq.s.timeout); |
838 |
crq.s.IU_length = be16_to_cpu(crq.s.IU_length); |
839 |
crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr); |
840 |
|
841 |
dprintf("VSCSI: do_crq %02x %02x ...\n", crq.raw[0], crq.raw[1]); |
842 |
|
843 |
switch (crq.s.valid) {
|
844 |
case 0xc0: /* Init command/response */ |
845 |
|
846 |
/* Respond to initialization request */
|
847 |
if (crq.s.format == 0x01) { |
848 |
memset(crq.raw, 0, 16); |
849 |
crq.s.valid = 0xc0;
|
850 |
crq.s.format = 0x02;
|
851 |
spapr_vio_send_crq(dev, crq.raw); |
852 |
} |
853 |
|
854 |
/* Note that in hotplug cases, we might get a 0x02
|
855 |
* as a result of us emitting the init request
|
856 |
*/
|
857 |
|
858 |
break;
|
859 |
case 0xff: /* Link event */ |
860 |
|
861 |
/* Not handled for now */
|
862 |
|
863 |
break;
|
864 |
case 0x80: /* Payloads */ |
865 |
switch (crq.s.format) {
|
866 |
case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */ |
867 |
case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */ |
868 |
vscsi_got_payload(s, &crq); |
869 |
break;
|
870 |
case VIOSRP_OS400_FORMAT:
|
871 |
case VIOSRP_AIX_FORMAT:
|
872 |
case VIOSRP_LINUX_FORMAT:
|
873 |
case VIOSRP_INLINE_FORMAT:
|
874 |
fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
|
875 |
crq.s.format); |
876 |
break;
|
877 |
default:
|
878 |
fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
|
879 |
crq.s.format); |
880 |
} |
881 |
break;
|
882 |
default:
|
883 |
fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
|
884 |
crq.raw[0], crq.raw[1]); |
885 |
}; |
886 |
|
887 |
return 0; |
888 |
} |
889 |
|
890 |
static const struct SCSIBusInfo vscsi_scsi_info = { |
891 |
.tcq = true,
|
892 |
.max_channel = 7, /* logical unit addressing format */ |
893 |
.max_target = 63,
|
894 |
.max_lun = 31,
|
895 |
|
896 |
.transfer_data = vscsi_transfer_data, |
897 |
.complete = vscsi_command_complete, |
898 |
.cancel = vscsi_request_cancelled |
899 |
}; |
900 |
|
901 |
static void spapr_vscsi_reset(VIOsPAPRDevice *dev) |
902 |
{ |
903 |
VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev); |
904 |
int i;
|
905 |
|
906 |
memset(s->reqs, 0, sizeof(s->reqs)); |
907 |
for (i = 0; i < VSCSI_REQ_LIMIT; i++) { |
908 |
s->reqs[i].qtag = i; |
909 |
} |
910 |
} |
911 |
|
912 |
static int spapr_vscsi_init(VIOsPAPRDevice *dev) |
913 |
{ |
914 |
VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev); |
915 |
|
916 |
dev->crq.SendFunc = vscsi_do_crq; |
917 |
|
918 |
scsi_bus_new(&s->bus, &dev->qdev, &vscsi_scsi_info); |
919 |
if (!dev->qdev.hotplugged) {
|
920 |
scsi_bus_legacy_handle_cmdline(&s->bus); |
921 |
} |
922 |
|
923 |
return 0; |
924 |
} |
925 |
|
926 |
void spapr_vscsi_create(VIOsPAPRBus *bus)
|
927 |
{ |
928 |
DeviceState *dev; |
929 |
|
930 |
dev = qdev_create(&bus->bus, "spapr-vscsi");
|
931 |
|
932 |
qdev_init_nofail(dev); |
933 |
} |
934 |
|
935 |
static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off) |
936 |
{ |
937 |
int ret;
|
938 |
|
939 |
ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2); |
940 |
if (ret < 0) { |
941 |
return ret;
|
942 |
} |
943 |
|
944 |
ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0); |
945 |
if (ret < 0) { |
946 |
return ret;
|
947 |
} |
948 |
|
949 |
return 0; |
950 |
} |
951 |
|
952 |
static Property spapr_vscsi_properties[] = {
|
953 |
DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev), |
954 |
DEFINE_PROP_END_OF_LIST(), |
955 |
}; |
956 |
|
957 |
static void spapr_vscsi_class_init(ObjectClass *klass, void *data) |
958 |
{ |
959 |
DeviceClass *dc = DEVICE_CLASS(klass); |
960 |
VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); |
961 |
|
962 |
k->init = spapr_vscsi_init; |
963 |
k->reset = spapr_vscsi_reset; |
964 |
k->devnode = spapr_vscsi_devnode; |
965 |
k->dt_name = "v-scsi";
|
966 |
k->dt_type = "vscsi";
|
967 |
k->dt_compatible = "IBM,v-scsi";
|
968 |
k->signal_mask = 0x00000001;
|
969 |
dc->props = spapr_vscsi_properties; |
970 |
k->rtce_window_size = 0x10000000;
|
971 |
} |
972 |
|
973 |
static const TypeInfo spapr_vscsi_info = { |
974 |
.name = TYPE_VIO_SPAPR_VSCSI_DEVICE, |
975 |
.parent = TYPE_VIO_SPAPR_DEVICE, |
976 |
.instance_size = sizeof(VSCSIState),
|
977 |
.class_init = spapr_vscsi_class_init, |
978 |
}; |
979 |
|
980 |
static void spapr_vscsi_register_types(void) |
981 |
{ |
982 |
type_register_static(&spapr_vscsi_info); |
983 |
} |
984 |
|
985 |
type_init(spapr_vscsi_register_types) |