tests/test-mmap.c: Check mmap() return value before using it
[qemu/ar7.git] / hw / spapr_vscsi.c
blob992833450c98d6adc47e35eecff33ce3db3f25ce
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * PAPR Virtual SCSI, aka ibmvscsi
6 * Copyright (c) 2010,2011 Benjamin Herrenschmidt, IBM Corporation.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * TODO:
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)
34 #include "hw.h"
35 #include "scsi.h"
36 #include "scsi-defs.h"
37 #include "net.h" /* Remove that when we can */
38 #include "srp.h"
39 #include "hw/qdev.h"
40 #include "hw/spapr.h"
41 #include "hw/spapr_vio.h"
42 #include "hw/ppc-viosrp.h"
44 #include <libfdt.h>
46 /*#define DEBUG_VSCSI*/
48 #ifdef DEBUG_VSCSI
49 #define dprintf(fmt, ...) \
50 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #else
52 #define dprintf(fmt, ...) \
53 do { } while (0)
54 #endif
57 * Virtual SCSI device
60 /* Random numbers */
61 #define VSCSI_MAX_SECTORS 4096
62 #define VSCSI_REQ_LIMIT 24
64 #define SCSI_SENSE_BUF_SIZE 96
65 #define SRP_RSP_SENSE_DATA_LEN 18
67 typedef union vscsi_crq {
68 struct viosrp_crq s;
69 uint8_t raw[16];
70 } vscsi_crq;
72 typedef struct vscsi_req {
73 vscsi_crq crq;
74 union viosrp_iu iu;
76 /* SCSI request tracking */
77 SCSIDevice *sdev;
78 uint32_t qtag; /* qemu tag != srp tag */
79 int lun;
80 int active;
81 long data_len;
82 int writing;
83 int sensing;
84 int senselen;
85 uint8_t sense[SCSI_SENSE_BUF_SIZE];
87 /* RDMA related bits */
88 uint8_t dma_fmt;
89 struct srp_direct_buf ext_desc;
90 struct srp_direct_buf *cur_desc;
91 struct srp_indirect_buf *ind_desc;
92 int local_desc;
93 int total_desc;
94 } vscsi_req;
97 typedef struct {
98 VIOsPAPRDevice vdev;
99 SCSIBus bus;
100 vscsi_req reqs[VSCSI_REQ_LIMIT];
101 } VSCSIState;
103 /* XXX Debug only */
104 static VSCSIState *dbg_vscsi_state;
107 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
109 vscsi_req *req;
110 int i;
112 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
113 req = &s->reqs[i];
114 if (!req->active) {
115 memset(req, 0, sizeof(*req));
116 req->qtag = i;
117 req->active = 1;
118 return req;
121 return NULL;
124 static void vscsi_put_req(VSCSIState *s, vscsi_req *req)
126 req->active = 0;
129 static vscsi_req *vscsi_find_req(VSCSIState *s, uint32_t tag)
131 if (tag >= VSCSI_REQ_LIMIT || !s->reqs[tag].active) {
132 return NULL;
134 return &s->reqs[tag];
137 static void vscsi_decode_id_lun(uint64_t srp_lun, int *id, int *lun)
139 /* XXX Figure that one out properly ! This is crackpot */
140 *id = (srp_lun >> 56) & 0x7f;
141 *lun = (srp_lun >> 48) & 0xff;
144 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
145 uint64_t length, uint8_t format)
147 long rc, rc1;
149 /* First copy the SRP */
150 rc = spapr_tce_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
151 &req->iu, length);
152 if (rc) {
153 fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
156 req->crq.s.valid = 0x80;
157 req->crq.s.format = format;
158 req->crq.s.reserved = 0x00;
159 req->crq.s.timeout = cpu_to_be16(0x0000);
160 req->crq.s.IU_length = cpu_to_be16(length);
161 req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
163 if (rc == 0) {
164 req->crq.s.status = 0x99; /* Just needs to be non-zero */
165 } else {
166 req->crq.s.status = 0x00;
169 rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
170 if (rc1) {
171 fprintf(stderr, "vscsi_send_iu: Error sending response\n");
172 return rc1;
175 return rc;
178 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
179 uint8_t key, uint8_t asc, uint8_t ascq)
181 req->senselen = SRP_RSP_SENSE_DATA_LEN;
183 /* Valid bit and 'current errors' */
184 req->sense[0] = (0x1 << 7 | 0x70);
185 /* Sense key */
186 req->sense[2] = key;
187 /* Additional sense length */
188 req->sense[7] = 0xa; /* 10 bytes */
189 /* Additional sense code */
190 req->sense[12] = asc;
191 req->sense[13] = ascq;
194 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
195 uint8_t status, int32_t res_in, int32_t res_out)
197 union viosrp_iu *iu = &req->iu;
198 uint64_t tag = iu->srp.rsp.tag;
199 int total_len = sizeof(iu->srp.rsp);
201 dprintf("VSCSI: Sending resp status: 0x%x, "
202 "res_in: %d, res_out: %d\n", status, res_in, res_out);
204 memset(iu, 0, sizeof(struct srp_rsp));
205 iu->srp.rsp.opcode = SRP_RSP;
206 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
207 iu->srp.rsp.tag = tag;
209 /* Handle residuals */
210 if (res_in < 0) {
211 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
212 res_in = -res_in;
213 } else if (res_in) {
214 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
216 if (res_out < 0) {
217 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
218 res_out = -res_out;
219 } else if (res_out) {
220 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
222 iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
223 iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
225 /* We don't do response data */
226 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
227 iu->srp.rsp.resp_data_len = cpu_to_be32(0);
229 /* Handle success vs. failure */
230 iu->srp.rsp.status = status;
231 if (status) {
232 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x04) >> 2;
233 if (req->senselen) {
234 req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
235 req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
236 memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
237 total_len += req->senselen;
239 } else {
240 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x02) >> 1;
243 vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
244 return 0;
247 static inline void vscsi_swap_desc(struct srp_direct_buf *desc)
249 desc->va = be64_to_cpu(desc->va);
250 desc->len = be32_to_cpu(desc->len);
253 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
254 uint8_t *buf, uint32_t len)
256 struct srp_direct_buf *md = req->cur_desc;
257 uint32_t llen;
258 int rc = 0;
260 dprintf("VSCSI: direct segment 0x%x bytes, va=0x%llx desc len=0x%x\n",
261 len, (unsigned long long)md->va, md->len);
263 llen = MIN(len, md->len);
264 if (llen) {
265 if (req->writing) { /* writing = to device = reading from memory */
266 rc = spapr_tce_dma_read(&s->vdev, md->va, buf, llen);
267 } else {
268 rc = spapr_tce_dma_write(&s->vdev, md->va, buf, llen);
271 md->len -= llen;
272 md->va += llen;
274 if (rc) {
275 return -1;
277 return llen;
280 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
281 uint8_t *buf, uint32_t len)
283 struct srp_direct_buf *td = &req->ind_desc->table_desc;
284 struct srp_direct_buf *md = req->cur_desc;
285 int rc = 0;
286 uint32_t llen, total = 0;
288 dprintf("VSCSI: indirect segment 0x%x bytes, td va=0x%llx len=0x%x\n",
289 len, (unsigned long long)td->va, td->len);
291 /* While we have data ... */
292 while (len) {
293 /* If we have a descriptor but it's empty, go fetch a new one */
294 if (md && md->len == 0) {
295 /* More local available, use one */
296 if (req->local_desc) {
297 md = ++req->cur_desc;
298 --req->local_desc;
299 --req->total_desc;
300 td->va += sizeof(struct srp_direct_buf);
301 } else {
302 md = req->cur_desc = NULL;
305 /* No descriptor at hand, fetch one */
306 if (!md) {
307 if (!req->total_desc) {
308 dprintf("VSCSI: Out of descriptors !\n");
309 break;
311 md = req->cur_desc = &req->ext_desc;
312 dprintf("VSCSI: Reading desc from 0x%llx\n",
313 (unsigned long long)td->va);
314 rc = spapr_tce_dma_read(&s->vdev, td->va, md,
315 sizeof(struct srp_direct_buf));
316 if (rc) {
317 dprintf("VSCSI: tce_dma_read -> %d reading ext_desc\n", rc);
318 break;
320 vscsi_swap_desc(md);
321 td->va += sizeof(struct srp_direct_buf);
322 --req->total_desc;
324 dprintf("VSCSI: [desc va=0x%llx,len=0x%x] remaining=0x%x\n",
325 (unsigned long long)md->va, md->len, len);
327 /* Perform transfer */
328 llen = MIN(len, md->len);
329 if (req->writing) { /* writing = to device = reading from memory */
330 rc = spapr_tce_dma_read(&s->vdev, md->va, buf, llen);
331 } else {
332 rc = spapr_tce_dma_write(&s->vdev, md->va, buf, llen);
334 if (rc) {
335 dprintf("VSCSI: tce_dma_r/w(%d) -> %d\n", req->writing, rc);
336 break;
338 dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
339 buf[0], buf[1], buf[2], buf[3]);
341 len -= llen;
342 buf += llen;
343 total += llen;
344 md->va += llen;
345 md->len -= llen;
347 return rc ? -1 : total;
350 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
351 int writing, uint8_t *buf, uint32_t len)
353 int err = 0;
355 switch (req->dma_fmt) {
356 case SRP_NO_DATA_DESC:
357 dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len);
358 break;
359 case SRP_DATA_DESC_DIRECT:
360 err = vscsi_srp_direct_data(s, req, buf, len);
361 break;
362 case SRP_DATA_DESC_INDIRECT:
363 err = vscsi_srp_indirect_data(s, req, buf, len);
364 break;
366 return err;
369 /* Bits from linux srp */
370 static int data_out_desc_size(struct srp_cmd *cmd)
372 int size = 0;
373 uint8_t fmt = cmd->buf_fmt >> 4;
375 switch (fmt) {
376 case SRP_NO_DATA_DESC:
377 break;
378 case SRP_DATA_DESC_DIRECT:
379 size = sizeof(struct srp_direct_buf);
380 break;
381 case SRP_DATA_DESC_INDIRECT:
382 size = sizeof(struct srp_indirect_buf) +
383 sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
384 break;
385 default:
386 break;
388 return size;
391 static int vscsi_preprocess_desc(vscsi_req *req)
393 struct srp_cmd *cmd = &req->iu.srp.cmd;
394 int offset, i;
396 offset = cmd->add_cdb_len & ~3;
398 if (req->writing) {
399 req->dma_fmt = cmd->buf_fmt >> 4;
400 } else {
401 offset += data_out_desc_size(cmd);
402 req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
405 switch (req->dma_fmt) {
406 case SRP_NO_DATA_DESC:
407 break;
408 case SRP_DATA_DESC_DIRECT:
409 req->cur_desc = (struct srp_direct_buf *)(cmd->add_data + offset);
410 req->total_desc = req->local_desc = 1;
411 vscsi_swap_desc(req->cur_desc);
412 dprintf("VSCSI: using direct RDMA %s, 0x%x bytes MD: 0x%llx\n",
413 req->writing ? "write" : "read",
414 req->cur_desc->len, (unsigned long long)req->cur_desc->va);
415 break;
416 case SRP_DATA_DESC_INDIRECT:
417 req->ind_desc = (struct srp_indirect_buf *)(cmd->add_data + offset);
418 vscsi_swap_desc(&req->ind_desc->table_desc);
419 req->total_desc = req->ind_desc->table_desc.len /
420 sizeof(struct srp_direct_buf);
421 req->local_desc = req->writing ? cmd->data_out_desc_cnt :
422 cmd->data_in_desc_cnt;
423 for (i = 0; i < req->local_desc; i++) {
424 vscsi_swap_desc(&req->ind_desc->desc_list[i]);
426 req->cur_desc = req->local_desc ? &req->ind_desc->desc_list[0] : NULL;
427 dprintf("VSCSI: using indirect RDMA %s, 0x%x bytes %d descs "
428 "(%d local) VA: 0x%llx\n",
429 req->writing ? "read" : "write",
430 be32_to_cpu(req->ind_desc->len),
431 req->total_desc, req->local_desc,
432 (unsigned long long)req->ind_desc->table_desc.va);
433 break;
434 default:
435 fprintf(stderr,
436 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
437 return -1;
440 return 0;
443 static void vscsi_send_request_sense(VSCSIState *s, vscsi_req *req)
445 SCSIDevice *sdev = req->sdev;
446 uint8_t *cdb = req->iu.srp.cmd.cdb;
447 int n;
449 cdb[0] = 3;
450 cdb[1] = 0;
451 cdb[2] = 0;
452 cdb[3] = 0;
453 cdb[4] = 96;
454 cdb[5] = 0;
455 req->sensing = 1;
456 n = sdev->info->send_command(sdev, req->qtag, cdb, req->lun);
457 dprintf("VSCSI: Queued request sense tag 0x%x\n", req->qtag);
458 if (n < 0) {
459 fprintf(stderr, "VSCSI: REQUEST_SENSE wants write data !?!?!?\n");
460 sdev->info->cancel_io(sdev, req->qtag);
461 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
462 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
463 vscsi_put_req(s, req);
464 return;
465 } else if (n == 0) {
466 return;
468 sdev->info->read_data(sdev, req->qtag);
471 /* Callback to indicate that the SCSI layer has completed a transfer. */
472 static void vscsi_command_complete(SCSIBus *bus, int reason, uint32_t tag,
473 uint32_t arg)
475 VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, bus->qbus.parent);
476 vscsi_req *req = vscsi_find_req(s, tag);
477 SCSIDevice *sdev;
478 uint8_t *buf;
479 int32_t res_in = 0, res_out = 0;
480 int len, rc = 0;
482 dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x arg=0x%x, req=%p\n",
483 reason, tag, arg, req);
484 if (req == NULL) {
485 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", tag);
486 return;
488 sdev = req->sdev;
490 if (req->sensing) {
491 if (reason == SCSI_REASON_DONE) {
492 dprintf("VSCSI: Sense done !\n");
493 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
494 vscsi_put_req(s, req);
495 } else {
496 uint8_t *buf = sdev->info->get_buf(sdev, tag);
498 len = MIN(arg, SCSI_SENSE_BUF_SIZE);
499 dprintf("VSCSI: Sense data, %d bytes:\n", len);
500 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
501 buf[0], buf[1], buf[2], buf[3],
502 buf[4], buf[5], buf[6], buf[7]);
503 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
504 buf[8], buf[9], buf[10], buf[11],
505 buf[12], buf[13], buf[14], buf[15]);
506 memcpy(req->sense, buf, len);
507 req->senselen = len;
508 sdev->info->read_data(sdev, req->qtag);
510 return;
513 if (reason == SCSI_REASON_DONE) {
514 dprintf("VSCSI: Command complete err=%d\n", arg);
515 if (arg == 0) {
516 /* We handle overflows, not underflows for normal commands,
517 * but hopefully nobody cares
519 if (req->writing) {
520 res_out = req->data_len;
521 } else {
522 res_in = req->data_len;
524 vscsi_send_rsp(s, req, 0, res_in, res_out);
525 } else if (arg == CHECK_CONDITION) {
526 dprintf("VSCSI: Got CHECK_CONDITION, requesting sense...\n");
527 vscsi_send_request_sense(s, req);
528 return;
529 } else {
530 vscsi_send_rsp(s, req, arg, 0, 0);
532 vscsi_put_req(s, req);
533 return;
536 /* "arg" is how much we have read for reads and how much we want
537 * to write for writes (ie, how much is to be DMA'd)
539 if (arg) {
540 buf = sdev->info->get_buf(sdev, tag);
541 rc = vscsi_srp_transfer_data(s, req, req->writing, buf, arg);
543 if (rc < 0) {
544 fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
545 sdev->info->cancel_io(sdev, req->qtag);
546 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
547 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
548 vscsi_put_req(s, req);
549 return;
552 /* Start next chunk */
553 req->data_len -= rc;
554 if (req->writing) {
555 sdev->info->write_data(sdev, req->qtag);
556 } else {
557 sdev->info->read_data(sdev, req->qtag);
561 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
563 union viosrp_iu *iu = &req->iu;
564 struct srp_login_rsp *rsp = &iu->srp.login_rsp;
565 uint64_t tag = iu->srp.rsp.tag;
567 dprintf("VSCSI: Got login, sendin response !\n");
569 /* TODO handle case that requested size is wrong and
570 * buffer format is wrong
572 memset(iu, 0, sizeof(struct srp_login_rsp));
573 rsp->opcode = SRP_LOGIN_RSP;
574 /* Don't advertise quite as many request as we support to
575 * keep room for management stuff etc...
577 rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
578 rsp->tag = tag;
579 rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
580 rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
581 /* direct and indirect */
582 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
584 vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
587 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
589 uint8_t *cdb = req->iu.srp.cmd.cdb;
590 uint8_t resp_data[36];
591 int rc, len, alen;
593 /* We dont do EVPD. Also check that page_code is 0 */
594 if ((cdb[1] & 0x01) || (cdb[1] & 0x01) || cdb[2] != 0) {
595 /* Send INVALID FIELD IN CDB */
596 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
597 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
598 return;
600 alen = cdb[3];
601 alen = (alen << 8) | cdb[4];
602 len = MIN(alen, 36);
604 /* Fake up inquiry using PQ=3 */
605 memset(resp_data, 0, 36);
606 resp_data[0] = 0x7f; /* Not capable of supporting a device here */
607 resp_data[2] = 0x06; /* SPS-4 */
608 resp_data[3] = 0x02; /* Resp data format */
609 resp_data[4] = 36 - 5; /* Additional length */
610 resp_data[7] = 0x10; /* Sync transfers */
611 memcpy(&resp_data[16], "QEMU EMPTY ", 16);
612 memcpy(&resp_data[8], "QEMU ", 8);
614 req->writing = 0;
615 vscsi_preprocess_desc(req);
616 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
617 if (rc < 0) {
618 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
619 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
620 } else {
621 vscsi_send_rsp(s, req, 0, 36 - rc, 0);
625 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
627 union srp_iu *srp = &req->iu.srp;
628 SCSIDevice *sdev;
629 int n, id, lun;
631 vscsi_decode_id_lun(be64_to_cpu(srp->cmd.lun), &id, &lun);
633 /* Qemu vs. linux issue with LUNs to be sorted out ... */
634 sdev = (id < 8 && lun < 16) ? s->bus.devs[id] : NULL;
635 if (!sdev) {
636 dprintf("VSCSI: Command for id %d with no drive\n", id);
637 if (srp->cmd.cdb[0] == INQUIRY) {
638 vscsi_inquiry_no_target(s, req);
639 } else {
640 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
641 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
642 } return 1;
645 req->sdev = sdev;
646 req->lun = lun;
647 n = sdev->info->send_command(sdev, req->qtag, srp->cmd.cdb, lun);
649 dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x ID %d LUN %d ret: %d\n",
650 req->qtag, srp->cmd.cdb[0], id, lun, n);
652 if (n) {
653 /* Transfer direction must be set before preprocessing the
654 * descriptors
656 req->writing = (n < 1);
658 /* Preprocess RDMA descriptors */
659 vscsi_preprocess_desc(req);
662 /* Get transfer direction and initiate transfer */
663 if (n > 0) {
664 req->data_len = n;
665 sdev->info->read_data(sdev, req->qtag);
666 } else if (n < 0) {
667 req->data_len = -n;
668 sdev->info->write_data(sdev, req->qtag);
670 /* Don't touch req here, it may have been recycled already */
672 return 0;
675 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
677 union viosrp_iu *iu = &req->iu;
678 int fn;
680 fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
681 iu->srp.tsk_mgmt.tsk_mgmt_func);
683 switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
684 #if 0 /* We really don't deal with these for now */
685 case SRP_TSK_ABORT_TASK:
686 fn = ABORT_TASK;
687 break;
688 case SRP_TSK_ABORT_TASK_SET:
689 fn = ABORT_TASK_SET;
690 break;
691 case SRP_TSK_CLEAR_TASK_SET:
692 fn = CLEAR_TASK_SET;
693 break;
694 case SRP_TSK_LUN_RESET:
695 fn = LOGICAL_UNIT_RESET;
696 break;
697 case SRP_TSK_CLEAR_ACA:
698 fn = CLEAR_ACA;
699 break;
700 #endif
701 default:
702 fn = 0;
704 if (fn) {
705 /* XXX Send/Handle target task management */
707 } else {
708 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x20, 0);
709 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
711 return !fn;
714 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
716 union srp_iu *srp = &req->iu.srp;
717 int done = 1;
718 uint8_t opcode = srp->rsp.opcode;
720 switch (opcode) {
721 case SRP_LOGIN_REQ:
722 vscsi_process_login(s, req);
723 break;
724 case SRP_TSK_MGMT:
725 done = vscsi_process_tsk_mgmt(s, req);
726 break;
727 case SRP_CMD:
728 done = vscsi_queue_cmd(s, req);
729 break;
730 case SRP_LOGIN_RSP:
731 case SRP_I_LOGOUT:
732 case SRP_T_LOGOUT:
733 case SRP_RSP:
734 case SRP_CRED_REQ:
735 case SRP_CRED_RSP:
736 case SRP_AER_REQ:
737 case SRP_AER_RSP:
738 fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
739 break;
740 default:
741 fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
744 return done;
747 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
749 struct viosrp_adapter_info *sinfo;
750 struct mad_adapter_info_data info;
751 int rc;
753 sinfo = &req->iu.mad.adapter_info;
755 #if 0 /* What for ? */
756 rc = spapr_tce_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
757 &info, be16_to_cpu(sinfo->common.length));
758 if (rc) {
759 fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
761 #endif
762 memset(&info, 0, sizeof(info));
763 strcpy(info.srp_version, SRP_VERSION);
764 strncpy(info.partition_name, "qemu", sizeof("qemu"));
765 info.partition_number = cpu_to_be32(0);
766 info.mad_version = cpu_to_be32(1);
767 info.os_type = cpu_to_be32(2);
768 info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
770 rc = spapr_tce_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
771 &info, be16_to_cpu(sinfo->common.length));
772 if (rc) {
773 fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
776 sinfo->common.status = rc ? cpu_to_be32(1) : 0;
778 return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
781 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
783 union mad_iu *mad = &req->iu.mad;
785 switch (be32_to_cpu(mad->empty_iu.common.type)) {
786 case VIOSRP_EMPTY_IU_TYPE:
787 fprintf(stderr, "Unsupported EMPTY MAD IU\n");
788 break;
789 case VIOSRP_ERROR_LOG_TYPE:
790 fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
791 mad->error_log.common.status = cpu_to_be16(1);
792 vscsi_send_iu(s, req, sizeof(mad->error_log), VIOSRP_MAD_FORMAT);
793 break;
794 case VIOSRP_ADAPTER_INFO_TYPE:
795 vscsi_send_adapter_info(s, req);
796 break;
797 case VIOSRP_HOST_CONFIG_TYPE:
798 mad->host_config.common.status = cpu_to_be16(1);
799 vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
800 break;
801 default:
802 fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
803 be32_to_cpu(mad->empty_iu.common.type));
806 return 1;
809 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
811 vscsi_req *req;
812 int done;
814 req = vscsi_get_req(s);
815 if (req == NULL) {
816 fprintf(stderr, "VSCSI: Failed to get a request !\n");
817 return;
820 /* We only support a limited number of descriptors, we know
821 * the ibmvscsi driver uses up to 10 max, so it should fit
822 * in our 256 bytes IUs. If not we'll have to increase the size
823 * of the structure.
825 if (crq->s.IU_length > sizeof(union viosrp_iu)) {
826 fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
827 crq->s.IU_length);
828 return;
831 /* XXX Handle failure differently ? */
832 if (spapr_tce_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
833 crq->s.IU_length)) {
834 fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
835 qemu_free(req);
837 memcpy(&req->crq, crq, sizeof(vscsi_crq));
839 if (crq->s.format == VIOSRP_MAD_FORMAT) {
840 done = vscsi_handle_mad_req(s, req);
841 } else {
842 done = vscsi_handle_srp_req(s, req);
845 if (done) {
846 vscsi_put_req(s, req);
851 static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data)
853 VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
854 vscsi_crq crq;
856 memcpy(crq.raw, crq_data, 16);
857 crq.s.timeout = be16_to_cpu(crq.s.timeout);
858 crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
859 crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
861 dprintf("VSCSI: do_crq %02x %02x ...\n", crq.raw[0], crq.raw[1]);
863 switch (crq.s.valid) {
864 case 0xc0: /* Init command/response */
866 /* Respond to initialization request */
867 if (crq.s.format == 0x01) {
868 memset(crq.raw, 0, 16);
869 crq.s.valid = 0xc0;
870 crq.s.format = 0x02;
871 spapr_vio_send_crq(dev, crq.raw);
874 /* Note that in hotplug cases, we might get a 0x02
875 * as a result of us emitting the init request
878 break;
879 case 0xff: /* Link event */
881 /* Not handled for now */
883 break;
884 case 0x80: /* Payloads */
885 switch (crq.s.format) {
886 case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
887 case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
888 vscsi_got_payload(s, &crq);
889 break;
890 case VIOSRP_OS400_FORMAT:
891 case VIOSRP_AIX_FORMAT:
892 case VIOSRP_LINUX_FORMAT:
893 case VIOSRP_INLINE_FORMAT:
894 fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
895 crq.s.format);
896 break;
897 default:
898 fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
899 crq.s.format);
901 break;
902 default:
903 fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
904 crq.raw[0], crq.raw[1]);
907 return 0;
910 static int spapr_vscsi_init(VIOsPAPRDevice *dev)
912 VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
913 int i;
915 dbg_vscsi_state = s;
917 /* Initialize qemu request tags */
918 memset(s->reqs, 0, sizeof(s->reqs));
919 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
920 s->reqs[i].qtag = i;
923 dev->crq.SendFunc = vscsi_do_crq;
925 scsi_bus_new(&s->bus, &dev->qdev, 1, VSCSI_REQ_LIMIT,
926 vscsi_command_complete);
927 if (!dev->qdev.hotplugged) {
928 scsi_bus_legacy_handle_cmdline(&s->bus);
931 return 0;
934 void spapr_vscsi_create(VIOsPAPRBus *bus, uint32_t reg,
935 qemu_irq qirq, uint32_t vio_irq_num)
937 DeviceState *dev;
938 VIOsPAPRDevice *sdev;
940 dev = qdev_create(&bus->bus, "spapr-vscsi");
941 qdev_prop_set_uint32(dev, "reg", reg);
943 qdev_init_nofail(dev);
945 sdev = (VIOsPAPRDevice *)dev;
946 sdev->qirq = qirq;
947 sdev->vio_irq_num = vio_irq_num;
950 static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
952 int ret;
954 ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
955 if (ret < 0) {
956 return ret;
959 ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
960 if (ret < 0) {
961 return ret;
964 return 0;
967 static VIOsPAPRDeviceInfo spapr_vscsi = {
968 .init = spapr_vscsi_init,
969 .devnode = spapr_vscsi_devnode,
970 .dt_name = "v-scsi",
971 .dt_type = "vscsi",
972 .dt_compatible = "IBM,v-scsi",
973 .signal_mask = 0x00000001,
974 .qdev.name = "spapr-vscsi",
975 .qdev.size = sizeof(VSCSIState),
976 .qdev.props = (Property[]) {
977 DEFINE_PROP_UINT32("reg", VIOsPAPRDevice, reg, 0x2000),
978 DEFINE_PROP_UINT32("dma-window", VIOsPAPRDevice,
979 rtce_window_size, 0x10000000),
980 DEFINE_PROP_END_OF_LIST(),
984 static void spapr_vscsi_register(void)
986 spapr_vio_bus_register_withprop(&spapr_vscsi);
988 device_init(spapr_vscsi_register);