pseries: rework PAPR virtual SCSI
[qemu/ar7.git] / hw / scsi / spapr_vscsi.c
blobb0858e929905e8f24be76f67f88d9c257455b911
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/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"
43 #include <libfdt.h>
45 /*#define DEBUG_VSCSI*/
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
56 * Virtual SCSI device
59 /* Random numbers */
60 #define VSCSI_MAX_SECTORS 4096
61 #define VSCSI_REQ_LIMIT 24
63 #define SCSI_SENSE_BUF_SIZE 96
64 #define SRP_RSP_SENSE_DATA_LEN 18
66 typedef union vscsi_crq {
67 struct viosrp_crq s;
68 uint8_t raw[16];
69 } vscsi_crq;
71 typedef struct vscsi_req {
72 vscsi_crq crq;
73 union viosrp_iu iu;
75 /* SCSI request tracking */
76 SCSIRequest *sreq;
77 uint32_t qtag; /* qemu tag != srp tag */
78 bool active;
79 uint32_t data_len;
80 bool writing;
81 uint32_t senselen;
82 uint8_t sense[SCSI_SENSE_BUF_SIZE];
84 /* RDMA related bits */
85 uint8_t dma_fmt;
86 uint16_t local_desc;
87 uint16_t total_desc;
88 uint16_t cdb_offset;
89 uint16_t cur_desc_num;
90 uint16_t cur_desc_offset;
91 } vscsi_req;
93 #define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi"
94 #define VIO_SPAPR_VSCSI_DEVICE(obj) \
95 OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE)
97 typedef struct {
98 VIOsPAPRDevice vdev;
99 SCSIBus bus;
100 vscsi_req reqs[VSCSI_REQ_LIMIT];
101 } VSCSIState;
103 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
105 vscsi_req *req;
106 int i;
108 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
109 req = &s->reqs[i];
110 if (!req->active) {
111 memset(req, 0, sizeof(*req));
112 req->qtag = i;
113 req->active = 1;
114 return req;
117 return NULL;
120 static void vscsi_put_req(vscsi_req *req)
122 if (req->sreq != NULL) {
123 scsi_req_unref(req->sreq);
125 req->sreq = NULL;
126 req->active = 0;
129 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun)
131 int channel = 0, id = 0;
133 retry:
134 switch (srp_lun >> 62) {
135 case 0:
136 if ((srp_lun >> 56) != 0) {
137 channel = (srp_lun >> 56) & 0x3f;
138 id = (srp_lun >> 48) & 0xff;
139 srp_lun <<= 16;
140 goto retry;
142 *lun = (srp_lun >> 48) & 0xff;
143 break;
145 case 1:
146 *lun = (srp_lun >> 48) & 0x3fff;
147 break;
148 case 2:
149 channel = (srp_lun >> 53) & 0x7;
150 id = (srp_lun >> 56) & 0x3f;
151 *lun = (srp_lun >> 48) & 0x1f;
152 break;
153 case 3:
154 *lun = -1;
155 return NULL;
156 default:
157 abort();
160 return scsi_device_find(bus, channel, id, *lun);
163 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
164 uint64_t length, uint8_t format)
166 long rc, rc1;
168 /* First copy the SRP */
169 rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
170 &req->iu, length);
171 if (rc) {
172 fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
175 req->crq.s.valid = 0x80;
176 req->crq.s.format = format;
177 req->crq.s.reserved = 0x00;
178 req->crq.s.timeout = cpu_to_be16(0x0000);
179 req->crq.s.IU_length = cpu_to_be16(length);
180 req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
182 if (rc == 0) {
183 req->crq.s.status = 0x99; /* Just needs to be non-zero */
184 } else {
185 req->crq.s.status = 0x00;
188 rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
189 if (rc1) {
190 fprintf(stderr, "vscsi_send_iu: Error sending response\n");
191 return rc1;
194 return rc;
197 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
198 uint8_t key, uint8_t asc, uint8_t ascq)
200 req->senselen = SRP_RSP_SENSE_DATA_LEN;
202 /* Valid bit and 'current errors' */
203 req->sense[0] = (0x1 << 7 | 0x70);
204 /* Sense key */
205 req->sense[2] = key;
206 /* Additional sense length */
207 req->sense[7] = 0xa; /* 10 bytes */
208 /* Additional sense code */
209 req->sense[12] = asc;
210 req->sense[13] = ascq;
213 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
214 uint8_t status, int32_t res_in, int32_t res_out)
216 union viosrp_iu *iu = &req->iu;
217 uint64_t tag = iu->srp.rsp.tag;
218 int total_len = sizeof(iu->srp.rsp);
220 dprintf("VSCSI: Sending resp status: 0x%x, "
221 "res_in: %d, res_out: %d\n", status, res_in, res_out);
223 memset(iu, 0, sizeof(struct srp_rsp));
224 iu->srp.rsp.opcode = SRP_RSP;
225 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
226 iu->srp.rsp.tag = tag;
228 /* Handle residuals */
229 if (res_in < 0) {
230 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
231 res_in = -res_in;
232 } else if (res_in) {
233 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
235 if (res_out < 0) {
236 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
237 res_out = -res_out;
238 } else if (res_out) {
239 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
241 iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
242 iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
244 /* We don't do response data */
245 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
246 iu->srp.rsp.resp_data_len = cpu_to_be32(0);
248 /* Handle success vs. failure */
249 iu->srp.rsp.status = status;
250 if (status) {
251 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x04) >> 2;
252 if (req->senselen) {
253 req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
254 req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
255 memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
256 total_len += req->senselen;
258 } else {
259 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x02) >> 1;
262 vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
263 return 0;
266 static inline struct srp_direct_buf vscsi_swap_desc(struct srp_direct_buf desc)
268 desc.va = be64_to_cpu(desc.va);
269 desc.len = be32_to_cpu(desc.len);
270 return desc;
273 static int vscsi_fetch_desc(VSCSIState *s, struct vscsi_req *req,
274 unsigned n, unsigned buf_offset,
275 struct srp_direct_buf *ret)
277 struct srp_cmd *cmd = &req->iu.srp.cmd;
279 switch (req->dma_fmt) {
280 case SRP_NO_DATA_DESC: {
281 dprintf("VSCSI: no data descriptor\n");
282 return 0;
284 case SRP_DATA_DESC_DIRECT: {
285 memcpy(ret, cmd->add_data + req->cdb_offset, sizeof(*ret));
286 assert(req->cur_desc_num == 0);
287 dprintf("VSCSI: direct segment\n");
288 break;
290 case SRP_DATA_DESC_INDIRECT: {
291 struct srp_indirect_buf *tmp = (struct srp_indirect_buf *)
292 (cmd->add_data + req->cdb_offset);
293 if (n < req->local_desc) {
294 *ret = tmp->desc_list[n];
295 dprintf("VSCSI: indirect segment local tag=0x%x desc#%d/%d\n",
296 req->qtag, n, req->local_desc);
298 } else if (n < req->total_desc) {
299 int rc;
300 struct srp_direct_buf tbl_desc = vscsi_swap_desc(tmp->table_desc);
301 unsigned desc_offset = n * sizeof(struct srp_direct_buf);
303 if (desc_offset >= tbl_desc.len) {
304 dprintf("VSCSI: #%d is ouf of range (%d bytes)\n",
305 n, desc_offset);
306 return -1;
308 rc = spapr_vio_dma_read(&s->vdev, tbl_desc.va + desc_offset,
309 ret, sizeof(struct srp_direct_buf));
310 if (rc) {
311 dprintf("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n",
312 rc);
313 return -1;
315 dprintf("VSCSI: indirect segment ext. tag=0x%x desc#%d/%d { va=%"PRIx64" len=%x }\n",
316 req->qtag, n, req->total_desc, tbl_desc.va, tbl_desc.len);
317 } else {
318 dprintf("VSCSI: Out of descriptors !\n");
319 return 0;
321 break;
323 default:
324 fprintf(stderr, "VSCSI: Unknown format %x\n", req->dma_fmt);
325 return -1;
328 *ret = vscsi_swap_desc(*ret);
329 if (buf_offset > ret->len) {
330 dprintf(" offset=%x is out of a descriptor #%d boundary=%x\n",
331 buf_offset, req->cur_desc_num, ret->len);
332 return -1;
334 ret->va += buf_offset;
335 ret->len -= buf_offset;
337 dprintf(" cur=%d offs=%x ret { va=%"PRIx64" len=%x }\n",
338 req->cur_desc_num, req->cur_desc_offset, ret->va, ret->len);
340 return ret->len ? 1 : 0;
343 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
344 uint8_t *buf, uint32_t len)
346 struct srp_direct_buf md;
347 uint32_t llen;
348 int rc = 0;
350 rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
351 if (rc < 0) {
352 return -1;
353 } else if (rc == 0) {
354 return 0;
357 llen = MIN(len, md.len);
358 if (llen) {
359 if (req->writing) { /* writing = to device = reading from memory */
360 rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
361 } else {
362 rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
366 if (rc) {
367 return -1;
369 req->cur_desc_offset += llen;
371 return llen;
374 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
375 uint8_t *buf, uint32_t len)
377 struct srp_direct_buf md;
378 int rc = 0;
379 uint32_t llen, total = 0;
381 dprintf("VSCSI: indirect segment 0x%x bytes\n", len);
383 /* While we have data ... */
384 while (len) {
385 rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md);
386 if (rc < 0) {
387 return -1;
388 } else if (rc == 0) {
389 break;
392 /* Perform transfer */
393 llen = MIN(len, md.len);
394 if (req->writing) { /* writing = to device = reading from memory */
395 rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen);
396 } else {
397 rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen);
399 if (rc) {
400 dprintf("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n", req->writing, rc);
401 break;
403 dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
404 buf[0], buf[1], buf[2], buf[3]);
406 len -= llen;
407 buf += llen;
409 total += llen;
411 /* Update current position in the current descriptor */
412 req->cur_desc_offset += llen;
413 if (md.len == llen) {
414 /* Go to the next descriptor if the current one finished */
415 ++req->cur_desc_num;
416 req->cur_desc_offset = 0;
420 return rc ? -1 : total;
423 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
424 int writing, uint8_t *buf, uint32_t len)
426 int err = 0;
428 switch (req->dma_fmt) {
429 case SRP_NO_DATA_DESC:
430 dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len);
431 break;
432 case SRP_DATA_DESC_DIRECT:
433 err = vscsi_srp_direct_data(s, req, buf, len);
434 break;
435 case SRP_DATA_DESC_INDIRECT:
436 err = vscsi_srp_indirect_data(s, req, buf, len);
437 break;
439 return err;
442 /* Bits from linux srp */
443 static int data_out_desc_size(struct srp_cmd *cmd)
445 int size = 0;
446 uint8_t fmt = cmd->buf_fmt >> 4;
448 switch (fmt) {
449 case SRP_NO_DATA_DESC:
450 break;
451 case SRP_DATA_DESC_DIRECT:
452 size = sizeof(struct srp_direct_buf);
453 break;
454 case SRP_DATA_DESC_INDIRECT:
455 size = sizeof(struct srp_indirect_buf) +
456 sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
457 break;
458 default:
459 break;
461 return size;
464 static int vscsi_preprocess_desc(vscsi_req *req)
466 struct srp_cmd *cmd = &req->iu.srp.cmd;
468 req->cdb_offset = cmd->add_cdb_len & ~3;
470 if (req->writing) {
471 req->dma_fmt = cmd->buf_fmt >> 4;
472 } else {
473 req->cdb_offset += data_out_desc_size(cmd);
474 req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
477 switch (req->dma_fmt) {
478 case SRP_NO_DATA_DESC:
479 break;
480 case SRP_DATA_DESC_DIRECT:
481 req->total_desc = req->local_desc = 1;
482 break;
483 case SRP_DATA_DESC_INDIRECT: {
484 struct srp_indirect_buf *ind_tmp = (struct srp_indirect_buf *)
485 (cmd->add_data + req->cdb_offset);
487 req->total_desc = be32_to_cpu(ind_tmp->table_desc.len) /
488 sizeof(struct srp_direct_buf);
489 req->local_desc = req->writing ? cmd->data_out_desc_cnt :
490 cmd->data_in_desc_cnt;
491 break;
493 default:
494 fprintf(stderr,
495 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
496 return -1;
499 return 0;
502 /* Callback to indicate that the SCSI layer has completed a transfer. */
503 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)
505 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
506 vscsi_req *req = sreq->hba_private;
507 uint8_t *buf;
508 int rc = 0;
510 dprintf("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
511 sreq->tag, len, req);
512 if (req == NULL) {
513 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
514 return;
517 if (len) {
518 buf = scsi_req_get_buf(sreq);
519 rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len);
521 if (rc < 0) {
522 fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
523 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
524 scsi_req_abort(req->sreq, CHECK_CONDITION);
525 return;
528 /* Start next chunk */
529 req->data_len -= rc;
530 scsi_req_continue(sreq);
533 /* Callback to indicate that the SCSI layer has completed a transfer. */
534 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid)
536 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent);
537 vscsi_req *req = sreq->hba_private;
538 int32_t res_in = 0, res_out = 0;
540 dprintf("VSCSI: SCSI cmd complete, tag=0x%x status=0x%x, req=%p\n",
541 sreq->tag, status, req);
542 if (req == NULL) {
543 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
544 return;
547 if (status == CHECK_CONDITION) {
548 req->senselen = scsi_req_get_sense(req->sreq, req->sense,
549 sizeof(req->sense));
550 dprintf("VSCSI: Sense data, %d bytes:\n", req->senselen);
551 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
552 req->sense[0], req->sense[1], req->sense[2], req->sense[3],
553 req->sense[4], req->sense[5], req->sense[6], req->sense[7]);
554 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
555 req->sense[8], req->sense[9], req->sense[10], req->sense[11],
556 req->sense[12], req->sense[13], req->sense[14], req->sense[15]);
559 dprintf("VSCSI: Command complete err=%d\n", status);
560 if (status == 0) {
561 /* We handle overflows, not underflows for normal commands,
562 * but hopefully nobody cares
564 if (req->writing) {
565 res_out = req->data_len;
566 } else {
567 res_in = req->data_len;
570 vscsi_send_rsp(s, req, status, res_in, res_out);
571 vscsi_put_req(req);
574 static void vscsi_request_cancelled(SCSIRequest *sreq)
576 vscsi_req *req = sreq->hba_private;
578 vscsi_put_req(req);
581 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
583 union viosrp_iu *iu = &req->iu;
584 struct srp_login_rsp *rsp = &iu->srp.login_rsp;
585 uint64_t tag = iu->srp.rsp.tag;
587 dprintf("VSCSI: Got login, sendin response !\n");
589 /* TODO handle case that requested size is wrong and
590 * buffer format is wrong
592 memset(iu, 0, sizeof(struct srp_login_rsp));
593 rsp->opcode = SRP_LOGIN_RSP;
594 /* Don't advertise quite as many request as we support to
595 * keep room for management stuff etc...
597 rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
598 rsp->tag = tag;
599 rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
600 rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
601 /* direct and indirect */
602 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
604 vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
607 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
609 uint8_t *cdb = req->iu.srp.cmd.cdb;
610 uint8_t resp_data[36];
611 int rc, len, alen;
613 /* We dont do EVPD. Also check that page_code is 0 */
614 if ((cdb[1] & 0x01) || (cdb[1] & 0x01) || cdb[2] != 0) {
615 /* Send INVALID FIELD IN CDB */
616 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
617 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
618 return;
620 alen = cdb[3];
621 alen = (alen << 8) | cdb[4];
622 len = MIN(alen, 36);
624 /* Fake up inquiry using PQ=3 */
625 memset(resp_data, 0, 36);
626 resp_data[0] = 0x7f; /* Not capable of supporting a device here */
627 resp_data[2] = 0x06; /* SPS-4 */
628 resp_data[3] = 0x02; /* Resp data format */
629 resp_data[4] = 36 - 5; /* Additional length */
630 resp_data[7] = 0x10; /* Sync transfers */
631 memcpy(&resp_data[16], "QEMU EMPTY ", 16);
632 memcpy(&resp_data[8], "QEMU ", 8);
634 req->writing = 0;
635 vscsi_preprocess_desc(req);
636 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
637 if (rc < 0) {
638 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
639 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
640 } else {
641 vscsi_send_rsp(s, req, 0, 36 - rc, 0);
645 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
647 union srp_iu *srp = &req->iu.srp;
648 SCSIDevice *sdev;
649 int n, lun;
651 sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
652 if (!sdev) {
653 dprintf("VSCSI: Command for lun %08" PRIx64 " with no drive\n", be64_to_cpu(srp->cmd.lun));
654 if (srp->cmd.cdb[0] == INQUIRY) {
655 vscsi_inquiry_no_target(s, req);
656 } else {
657 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
658 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
659 } return 1;
662 req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req);
663 n = scsi_req_enqueue(req->sreq);
665 dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x LUN %d ret: %d\n",
666 req->qtag, srp->cmd.cdb[0], lun, n);
668 if (n) {
669 /* Transfer direction must be set before preprocessing the
670 * descriptors
672 req->writing = (n < 1);
674 /* Preprocess RDMA descriptors */
675 vscsi_preprocess_desc(req);
677 /* Get transfer direction and initiate transfer */
678 if (n > 0) {
679 req->data_len = n;
680 } else if (n < 0) {
681 req->data_len = -n;
683 scsi_req_continue(req->sreq);
685 /* Don't touch req here, it may have been recycled already */
687 return 0;
690 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
692 union viosrp_iu *iu = &req->iu;
693 int fn;
695 fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
696 iu->srp.tsk_mgmt.tsk_mgmt_func);
698 switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
699 #if 0 /* We really don't deal with these for now */
700 case SRP_TSK_ABORT_TASK:
701 fn = ABORT_TASK;
702 break;
703 case SRP_TSK_ABORT_TASK_SET:
704 fn = ABORT_TASK_SET;
705 break;
706 case SRP_TSK_CLEAR_TASK_SET:
707 fn = CLEAR_TASK_SET;
708 break;
709 case SRP_TSK_LUN_RESET:
710 fn = LOGICAL_UNIT_RESET;
711 break;
712 case SRP_TSK_CLEAR_ACA:
713 fn = CLEAR_ACA;
714 break;
715 #endif
716 default:
717 fn = 0;
719 if (fn) {
720 /* XXX Send/Handle target task management */
722 } else {
723 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x20, 0);
724 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
726 return !fn;
729 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
731 union srp_iu *srp = &req->iu.srp;
732 int done = 1;
733 uint8_t opcode = srp->rsp.opcode;
735 switch (opcode) {
736 case SRP_LOGIN_REQ:
737 vscsi_process_login(s, req);
738 break;
739 case SRP_TSK_MGMT:
740 done = vscsi_process_tsk_mgmt(s, req);
741 break;
742 case SRP_CMD:
743 done = vscsi_queue_cmd(s, req);
744 break;
745 case SRP_LOGIN_RSP:
746 case SRP_I_LOGOUT:
747 case SRP_T_LOGOUT:
748 case SRP_RSP:
749 case SRP_CRED_REQ:
750 case SRP_CRED_RSP:
751 case SRP_AER_REQ:
752 case SRP_AER_RSP:
753 fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
754 break;
755 default:
756 fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
759 return done;
762 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
764 struct viosrp_adapter_info *sinfo;
765 struct mad_adapter_info_data info;
766 int rc;
768 sinfo = &req->iu.mad.adapter_info;
770 #if 0 /* What for ? */
771 rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
772 &info, be16_to_cpu(sinfo->common.length));
773 if (rc) {
774 fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
776 #endif
777 memset(&info, 0, sizeof(info));
778 strcpy(info.srp_version, SRP_VERSION);
779 memcpy(info.partition_name, "qemu", sizeof("qemu"));
780 info.partition_number = cpu_to_be32(0);
781 info.mad_version = cpu_to_be32(1);
782 info.os_type = cpu_to_be32(2);
783 info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
785 rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
786 &info, be16_to_cpu(sinfo->common.length));
787 if (rc) {
788 fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
791 sinfo->common.status = rc ? cpu_to_be32(1) : 0;
793 return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
796 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
798 union mad_iu *mad = &req->iu.mad;
800 switch (be32_to_cpu(mad->empty_iu.common.type)) {
801 case VIOSRP_EMPTY_IU_TYPE:
802 fprintf(stderr, "Unsupported EMPTY MAD IU\n");
803 break;
804 case VIOSRP_ERROR_LOG_TYPE:
805 fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
806 mad->error_log.common.status = cpu_to_be16(1);
807 vscsi_send_iu(s, req, sizeof(mad->error_log), VIOSRP_MAD_FORMAT);
808 break;
809 case VIOSRP_ADAPTER_INFO_TYPE:
810 vscsi_send_adapter_info(s, req);
811 break;
812 case VIOSRP_HOST_CONFIG_TYPE:
813 mad->host_config.common.status = cpu_to_be16(1);
814 vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
815 break;
816 default:
817 fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
818 be32_to_cpu(mad->empty_iu.common.type));
821 return 1;
824 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
826 vscsi_req *req;
827 int done;
829 req = vscsi_get_req(s);
830 if (req == NULL) {
831 fprintf(stderr, "VSCSI: Failed to get a request !\n");
832 return;
835 /* We only support a limited number of descriptors, we know
836 * the ibmvscsi driver uses up to 10 max, so it should fit
837 * in our 256 bytes IUs. If not we'll have to increase the size
838 * of the structure.
840 if (crq->s.IU_length > sizeof(union viosrp_iu)) {
841 fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
842 crq->s.IU_length);
843 vscsi_put_req(req);
844 return;
847 /* XXX Handle failure differently ? */
848 if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
849 crq->s.IU_length)) {
850 fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
851 vscsi_put_req(req);
852 return;
854 memcpy(&req->crq, crq, sizeof(vscsi_crq));
856 if (crq->s.format == VIOSRP_MAD_FORMAT) {
857 done = vscsi_handle_mad_req(s, req);
858 } else {
859 done = vscsi_handle_srp_req(s, req);
862 if (done) {
863 vscsi_put_req(req);
868 static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data)
870 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
871 vscsi_crq crq;
873 memcpy(crq.raw, crq_data, 16);
874 crq.s.timeout = be16_to_cpu(crq.s.timeout);
875 crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
876 crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
878 dprintf("VSCSI: do_crq %02x %02x ...\n", crq.raw[0], crq.raw[1]);
880 switch (crq.s.valid) {
881 case 0xc0: /* Init command/response */
883 /* Respond to initialization request */
884 if (crq.s.format == 0x01) {
885 memset(crq.raw, 0, 16);
886 crq.s.valid = 0xc0;
887 crq.s.format = 0x02;
888 spapr_vio_send_crq(dev, crq.raw);
891 /* Note that in hotplug cases, we might get a 0x02
892 * as a result of us emitting the init request
895 break;
896 case 0xff: /* Link event */
898 /* Not handled for now */
900 break;
901 case 0x80: /* Payloads */
902 switch (crq.s.format) {
903 case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
904 case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
905 vscsi_got_payload(s, &crq);
906 break;
907 case VIOSRP_OS400_FORMAT:
908 case VIOSRP_AIX_FORMAT:
909 case VIOSRP_LINUX_FORMAT:
910 case VIOSRP_INLINE_FORMAT:
911 fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
912 crq.s.format);
913 break;
914 default:
915 fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
916 crq.s.format);
918 break;
919 default:
920 fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
921 crq.raw[0], crq.raw[1]);
924 return 0;
927 static const struct SCSIBusInfo vscsi_scsi_info = {
928 .tcq = true,
929 .max_channel = 7, /* logical unit addressing format */
930 .max_target = 63,
931 .max_lun = 31,
933 .transfer_data = vscsi_transfer_data,
934 .complete = vscsi_command_complete,
935 .cancel = vscsi_request_cancelled
938 static void spapr_vscsi_reset(VIOsPAPRDevice *dev)
940 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
941 int i;
943 memset(s->reqs, 0, sizeof(s->reqs));
944 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
945 s->reqs[i].qtag = i;
949 static int spapr_vscsi_init(VIOsPAPRDevice *dev)
951 VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev);
952 Error *err = NULL;
954 dev->crq.SendFunc = vscsi_do_crq;
956 scsi_bus_new(&s->bus, &dev->qdev, &vscsi_scsi_info, NULL);
957 if (!dev->qdev.hotplugged) {
958 scsi_bus_legacy_handle_cmdline(&s->bus, &err);
959 if (err != NULL) {
960 error_free(err);
961 return -1;
965 return 0;
968 void spapr_vscsi_create(VIOsPAPRBus *bus)
970 DeviceState *dev;
972 dev = qdev_create(&bus->bus, "spapr-vscsi");
974 qdev_init_nofail(dev);
977 static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
979 int ret;
981 ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
982 if (ret < 0) {
983 return ret;
986 ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
987 if (ret < 0) {
988 return ret;
991 return 0;
994 static Property spapr_vscsi_properties[] = {
995 DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev),
996 DEFINE_PROP_END_OF_LIST(),
999 static void spapr_vscsi_class_init(ObjectClass *klass, void *data)
1001 DeviceClass *dc = DEVICE_CLASS(klass);
1002 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
1004 k->init = spapr_vscsi_init;
1005 k->reset = spapr_vscsi_reset;
1006 k->devnode = spapr_vscsi_devnode;
1007 k->dt_name = "v-scsi";
1008 k->dt_type = "vscsi";
1009 k->dt_compatible = "IBM,v-scsi";
1010 k->signal_mask = 0x00000001;
1011 dc->props = spapr_vscsi_properties;
1012 k->rtce_window_size = 0x10000000;
1015 static const TypeInfo spapr_vscsi_info = {
1016 .name = TYPE_VIO_SPAPR_VSCSI_DEVICE,
1017 .parent = TYPE_VIO_SPAPR_DEVICE,
1018 .instance_size = sizeof(VSCSIState),
1019 .class_init = spapr_vscsi_class_init,
1022 static void spapr_vscsi_register_types(void)
1024 type_register_static(&spapr_vscsi_info);
1027 type_init(spapr_vscsi_register_types)