scsi: remove devs array from SCSIBus
[qemu/wangdongxu.git] / hw / spapr_vscsi.c
blobea3bb085db3b008ed50ee01e04a6e378c3989804
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 SCSIRequest *sreq;
78 uint32_t qtag; /* qemu tag != srp tag */
79 int lun;
80 int active;
81 long data_len;
82 int writing;
83 int senselen;
84 uint8_t sense[SCSI_SENSE_BUF_SIZE];
86 /* RDMA related bits */
87 uint8_t dma_fmt;
88 struct srp_direct_buf ext_desc;
89 struct srp_direct_buf *cur_desc;
90 struct srp_indirect_buf *ind_desc;
91 int local_desc;
92 int total_desc;
93 } vscsi_req;
96 typedef struct {
97 VIOsPAPRDevice vdev;
98 SCSIBus bus;
99 vscsi_req reqs[VSCSI_REQ_LIMIT];
100 } VSCSIState;
102 /* XXX Debug only */
103 static VSCSIState *dbg_vscsi_state;
106 static struct vscsi_req *vscsi_get_req(VSCSIState *s)
108 vscsi_req *req;
109 int i;
111 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
112 req = &s->reqs[i];
113 if (!req->active) {
114 memset(req, 0, sizeof(*req));
115 req->qtag = i;
116 req->active = 1;
117 return req;
120 return NULL;
123 static void vscsi_put_req(vscsi_req *req)
125 if (req->sreq != NULL) {
126 scsi_req_unref(req->sreq);
128 req->sreq = NULL;
129 req->active = 0;
132 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun)
134 /* XXX Figure that one out properly ! This is crackpot */
135 int id = (srp_lun >> 56) & 0x7f;
136 *lun = (srp_lun >> 48) & 0xff;
137 return scsi_device_find(bus, id, *lun);
140 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
141 uint64_t length, uint8_t format)
143 long rc, rc1;
145 /* First copy the SRP */
146 rc = spapr_tce_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
147 &req->iu, length);
148 if (rc) {
149 fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
152 req->crq.s.valid = 0x80;
153 req->crq.s.format = format;
154 req->crq.s.reserved = 0x00;
155 req->crq.s.timeout = cpu_to_be16(0x0000);
156 req->crq.s.IU_length = cpu_to_be16(length);
157 req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
159 if (rc == 0) {
160 req->crq.s.status = 0x99; /* Just needs to be non-zero */
161 } else {
162 req->crq.s.status = 0x00;
165 rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
166 if (rc1) {
167 fprintf(stderr, "vscsi_send_iu: Error sending response\n");
168 return rc1;
171 return rc;
174 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
175 uint8_t key, uint8_t asc, uint8_t ascq)
177 req->senselen = SRP_RSP_SENSE_DATA_LEN;
179 /* Valid bit and 'current errors' */
180 req->sense[0] = (0x1 << 7 | 0x70);
181 /* Sense key */
182 req->sense[2] = key;
183 /* Additional sense length */
184 req->sense[7] = 0xa; /* 10 bytes */
185 /* Additional sense code */
186 req->sense[12] = asc;
187 req->sense[13] = ascq;
190 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
191 uint8_t status, int32_t res_in, int32_t res_out)
193 union viosrp_iu *iu = &req->iu;
194 uint64_t tag = iu->srp.rsp.tag;
195 int total_len = sizeof(iu->srp.rsp);
197 dprintf("VSCSI: Sending resp status: 0x%x, "
198 "res_in: %d, res_out: %d\n", status, res_in, res_out);
200 memset(iu, 0, sizeof(struct srp_rsp));
201 iu->srp.rsp.opcode = SRP_RSP;
202 iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
203 iu->srp.rsp.tag = tag;
205 /* Handle residuals */
206 if (res_in < 0) {
207 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
208 res_in = -res_in;
209 } else if (res_in) {
210 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
212 if (res_out < 0) {
213 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
214 res_out = -res_out;
215 } else if (res_out) {
216 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
218 iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
219 iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
221 /* We don't do response data */
222 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
223 iu->srp.rsp.resp_data_len = cpu_to_be32(0);
225 /* Handle success vs. failure */
226 iu->srp.rsp.status = status;
227 if (status) {
228 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x04) >> 2;
229 if (req->senselen) {
230 req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
231 req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
232 memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
233 total_len += req->senselen;
235 } else {
236 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x02) >> 1;
239 vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
240 return 0;
243 static inline void vscsi_swap_desc(struct srp_direct_buf *desc)
245 desc->va = be64_to_cpu(desc->va);
246 desc->len = be32_to_cpu(desc->len);
249 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
250 uint8_t *buf, uint32_t len)
252 struct srp_direct_buf *md = req->cur_desc;
253 uint32_t llen;
254 int rc = 0;
256 dprintf("VSCSI: direct segment 0x%x bytes, va=0x%llx desc len=0x%x\n",
257 len, (unsigned long long)md->va, md->len);
259 llen = MIN(len, md->len);
260 if (llen) {
261 if (req->writing) { /* writing = to device = reading from memory */
262 rc = spapr_tce_dma_read(&s->vdev, md->va, buf, llen);
263 } else {
264 rc = spapr_tce_dma_write(&s->vdev, md->va, buf, llen);
267 md->len -= llen;
268 md->va += llen;
270 if (rc) {
271 return -1;
273 return llen;
276 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
277 uint8_t *buf, uint32_t len)
279 struct srp_direct_buf *td = &req->ind_desc->table_desc;
280 struct srp_direct_buf *md = req->cur_desc;
281 int rc = 0;
282 uint32_t llen, total = 0;
284 dprintf("VSCSI: indirect segment 0x%x bytes, td va=0x%llx len=0x%x\n",
285 len, (unsigned long long)td->va, td->len);
287 /* While we have data ... */
288 while (len) {
289 /* If we have a descriptor but it's empty, go fetch a new one */
290 if (md && md->len == 0) {
291 /* More local available, use one */
292 if (req->local_desc) {
293 md = ++req->cur_desc;
294 --req->local_desc;
295 --req->total_desc;
296 td->va += sizeof(struct srp_direct_buf);
297 } else {
298 md = req->cur_desc = NULL;
301 /* No descriptor at hand, fetch one */
302 if (!md) {
303 if (!req->total_desc) {
304 dprintf("VSCSI: Out of descriptors !\n");
305 break;
307 md = req->cur_desc = &req->ext_desc;
308 dprintf("VSCSI: Reading desc from 0x%llx\n",
309 (unsigned long long)td->va);
310 rc = spapr_tce_dma_read(&s->vdev, td->va, md,
311 sizeof(struct srp_direct_buf));
312 if (rc) {
313 dprintf("VSCSI: tce_dma_read -> %d reading ext_desc\n", rc);
314 break;
316 vscsi_swap_desc(md);
317 td->va += sizeof(struct srp_direct_buf);
318 --req->total_desc;
320 dprintf("VSCSI: [desc va=0x%llx,len=0x%x] remaining=0x%x\n",
321 (unsigned long long)md->va, md->len, len);
323 /* Perform transfer */
324 llen = MIN(len, md->len);
325 if (req->writing) { /* writing = to device = reading from memory */
326 rc = spapr_tce_dma_read(&s->vdev, md->va, buf, llen);
327 } else {
328 rc = spapr_tce_dma_write(&s->vdev, md->va, buf, llen);
330 if (rc) {
331 dprintf("VSCSI: tce_dma_r/w(%d) -> %d\n", req->writing, rc);
332 break;
334 dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
335 buf[0], buf[1], buf[2], buf[3]);
337 len -= llen;
338 buf += llen;
339 total += llen;
340 md->va += llen;
341 md->len -= llen;
343 return rc ? -1 : total;
346 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
347 int writing, uint8_t *buf, uint32_t len)
349 int err = 0;
351 switch (req->dma_fmt) {
352 case SRP_NO_DATA_DESC:
353 dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len);
354 break;
355 case SRP_DATA_DESC_DIRECT:
356 err = vscsi_srp_direct_data(s, req, buf, len);
357 break;
358 case SRP_DATA_DESC_INDIRECT:
359 err = vscsi_srp_indirect_data(s, req, buf, len);
360 break;
362 return err;
365 /* Bits from linux srp */
366 static int data_out_desc_size(struct srp_cmd *cmd)
368 int size = 0;
369 uint8_t fmt = cmd->buf_fmt >> 4;
371 switch (fmt) {
372 case SRP_NO_DATA_DESC:
373 break;
374 case SRP_DATA_DESC_DIRECT:
375 size = sizeof(struct srp_direct_buf);
376 break;
377 case SRP_DATA_DESC_INDIRECT:
378 size = sizeof(struct srp_indirect_buf) +
379 sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
380 break;
381 default:
382 break;
384 return size;
387 static int vscsi_preprocess_desc(vscsi_req *req)
389 struct srp_cmd *cmd = &req->iu.srp.cmd;
390 int offset, i;
392 offset = cmd->add_cdb_len & ~3;
394 if (req->writing) {
395 req->dma_fmt = cmd->buf_fmt >> 4;
396 } else {
397 offset += data_out_desc_size(cmd);
398 req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
401 switch (req->dma_fmt) {
402 case SRP_NO_DATA_DESC:
403 break;
404 case SRP_DATA_DESC_DIRECT:
405 req->cur_desc = (struct srp_direct_buf *)(cmd->add_data + offset);
406 req->total_desc = req->local_desc = 1;
407 vscsi_swap_desc(req->cur_desc);
408 dprintf("VSCSI: using direct RDMA %s, 0x%x bytes MD: 0x%llx\n",
409 req->writing ? "write" : "read",
410 req->cur_desc->len, (unsigned long long)req->cur_desc->va);
411 break;
412 case SRP_DATA_DESC_INDIRECT:
413 req->ind_desc = (struct srp_indirect_buf *)(cmd->add_data + offset);
414 vscsi_swap_desc(&req->ind_desc->table_desc);
415 req->total_desc = req->ind_desc->table_desc.len /
416 sizeof(struct srp_direct_buf);
417 req->local_desc = req->writing ? cmd->data_out_desc_cnt :
418 cmd->data_in_desc_cnt;
419 for (i = 0; i < req->local_desc; i++) {
420 vscsi_swap_desc(&req->ind_desc->desc_list[i]);
422 req->cur_desc = req->local_desc ? &req->ind_desc->desc_list[0] : NULL;
423 dprintf("VSCSI: using indirect RDMA %s, 0x%x bytes %d descs "
424 "(%d local) VA: 0x%llx\n",
425 req->writing ? "read" : "write",
426 be32_to_cpu(req->ind_desc->len),
427 req->total_desc, req->local_desc,
428 (unsigned long long)req->ind_desc->table_desc.va);
429 break;
430 default:
431 fprintf(stderr,
432 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
433 return -1;
436 return 0;
439 /* Callback to indicate that the SCSI layer has completed a transfer. */
440 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len)
442 VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
443 vscsi_req *req = sreq->hba_private;
444 uint8_t *buf;
445 int rc = 0;
447 dprintf("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
448 sreq->tag, len, req);
449 if (req == NULL) {
450 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
451 return;
454 if (len) {
455 buf = scsi_req_get_buf(sreq);
456 rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len);
458 if (rc < 0) {
459 fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
460 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
461 scsi_req_abort(req->sreq, CHECK_CONDITION);
462 return;
465 /* Start next chunk */
466 req->data_len -= rc;
467 scsi_req_continue(sreq);
470 /* Callback to indicate that the SCSI layer has completed a transfer. */
471 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status)
473 VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
474 vscsi_req *req = sreq->hba_private;
475 int32_t res_in = 0, res_out = 0;
477 dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x status=0x%x, req=%p\n",
478 reason, sreq->tag, status, req);
479 if (req == NULL) {
480 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
481 return;
484 if (status == CHECK_CONDITION) {
485 req->senselen = scsi_req_get_sense(req->sreq, req->sense,
486 sizeof(req->sense));
487 dprintf("VSCSI: Sense data, %d bytes:\n", len);
488 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
489 req->sense[0], req->sense[1], req->sense[2], req->sense[3],
490 req->sense[4], req->sense[5], req->sense[6], req->sense[7]);
491 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
492 req->sense[8], req->sense[9], req->sense[10], req->sense[11],
493 req->sense[12], req->sense[13], req->sense[14], req->sense[15]);
496 dprintf("VSCSI: Command complete err=%d\n", status);
497 if (status == 0) {
498 /* We handle overflows, not underflows for normal commands,
499 * but hopefully nobody cares
501 if (req->writing) {
502 res_out = req->data_len;
503 } else {
504 res_in = req->data_len;
507 vscsi_send_rsp(s, req, status, res_in, res_out);
508 vscsi_put_req(req);
511 static void vscsi_request_cancelled(SCSIRequest *sreq)
513 vscsi_req *req = sreq->hba_private;
515 vscsi_put_req(req);
518 static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
520 union viosrp_iu *iu = &req->iu;
521 struct srp_login_rsp *rsp = &iu->srp.login_rsp;
522 uint64_t tag = iu->srp.rsp.tag;
524 dprintf("VSCSI: Got login, sendin response !\n");
526 /* TODO handle case that requested size is wrong and
527 * buffer format is wrong
529 memset(iu, 0, sizeof(struct srp_login_rsp));
530 rsp->opcode = SRP_LOGIN_RSP;
531 /* Don't advertise quite as many request as we support to
532 * keep room for management stuff etc...
534 rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
535 rsp->tag = tag;
536 rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
537 rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
538 /* direct and indirect */
539 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
541 vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
544 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
546 uint8_t *cdb = req->iu.srp.cmd.cdb;
547 uint8_t resp_data[36];
548 int rc, len, alen;
550 /* We dont do EVPD. Also check that page_code is 0 */
551 if ((cdb[1] & 0x01) || (cdb[1] & 0x01) || cdb[2] != 0) {
552 /* Send INVALID FIELD IN CDB */
553 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
554 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
555 return;
557 alen = cdb[3];
558 alen = (alen << 8) | cdb[4];
559 len = MIN(alen, 36);
561 /* Fake up inquiry using PQ=3 */
562 memset(resp_data, 0, 36);
563 resp_data[0] = 0x7f; /* Not capable of supporting a device here */
564 resp_data[2] = 0x06; /* SPS-4 */
565 resp_data[3] = 0x02; /* Resp data format */
566 resp_data[4] = 36 - 5; /* Additional length */
567 resp_data[7] = 0x10; /* Sync transfers */
568 memcpy(&resp_data[16], "QEMU EMPTY ", 16);
569 memcpy(&resp_data[8], "QEMU ", 8);
571 req->writing = 0;
572 vscsi_preprocess_desc(req);
573 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
574 if (rc < 0) {
575 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
576 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
577 } else {
578 vscsi_send_rsp(s, req, 0, 36 - rc, 0);
582 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
584 union srp_iu *srp = &req->iu.srp;
585 SCSIDevice *sdev;
586 int n, lun;
588 sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
589 if (!sdev) {
590 dprintf("VSCSI: Command for lun %08" PRIx64 " with no drive\n", be64_to_cpu(srp->cmd.lun));
591 if (srp->cmd.cdb[0] == INQUIRY) {
592 vscsi_inquiry_no_target(s, req);
593 } else {
594 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
595 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
596 } return 1;
599 req->lun = lun;
600 req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req);
601 n = scsi_req_enqueue(req->sreq);
603 dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x ID %d LUN %d ret: %d\n",
604 req->qtag, srp->cmd.cdb[0], id, lun, n);
606 if (n) {
607 /* Transfer direction must be set before preprocessing the
608 * descriptors
610 req->writing = (n < 1);
612 /* Preprocess RDMA descriptors */
613 vscsi_preprocess_desc(req);
615 /* Get transfer direction and initiate transfer */
616 if (n > 0) {
617 req->data_len = n;
618 } else if (n < 0) {
619 req->data_len = -n;
621 scsi_req_continue(req->sreq);
623 /* Don't touch req here, it may have been recycled already */
625 return 0;
628 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
630 union viosrp_iu *iu = &req->iu;
631 int fn;
633 fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
634 iu->srp.tsk_mgmt.tsk_mgmt_func);
636 switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
637 #if 0 /* We really don't deal with these for now */
638 case SRP_TSK_ABORT_TASK:
639 fn = ABORT_TASK;
640 break;
641 case SRP_TSK_ABORT_TASK_SET:
642 fn = ABORT_TASK_SET;
643 break;
644 case SRP_TSK_CLEAR_TASK_SET:
645 fn = CLEAR_TASK_SET;
646 break;
647 case SRP_TSK_LUN_RESET:
648 fn = LOGICAL_UNIT_RESET;
649 break;
650 case SRP_TSK_CLEAR_ACA:
651 fn = CLEAR_ACA;
652 break;
653 #endif
654 default:
655 fn = 0;
657 if (fn) {
658 /* XXX Send/Handle target task management */
660 } else {
661 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x20, 0);
662 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
664 return !fn;
667 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
669 union srp_iu *srp = &req->iu.srp;
670 int done = 1;
671 uint8_t opcode = srp->rsp.opcode;
673 switch (opcode) {
674 case SRP_LOGIN_REQ:
675 vscsi_process_login(s, req);
676 break;
677 case SRP_TSK_MGMT:
678 done = vscsi_process_tsk_mgmt(s, req);
679 break;
680 case SRP_CMD:
681 done = vscsi_queue_cmd(s, req);
682 break;
683 case SRP_LOGIN_RSP:
684 case SRP_I_LOGOUT:
685 case SRP_T_LOGOUT:
686 case SRP_RSP:
687 case SRP_CRED_REQ:
688 case SRP_CRED_RSP:
689 case SRP_AER_REQ:
690 case SRP_AER_RSP:
691 fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
692 break;
693 default:
694 fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
697 return done;
700 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
702 struct viosrp_adapter_info *sinfo;
703 struct mad_adapter_info_data info;
704 int rc;
706 sinfo = &req->iu.mad.adapter_info;
708 #if 0 /* What for ? */
709 rc = spapr_tce_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
710 &info, be16_to_cpu(sinfo->common.length));
711 if (rc) {
712 fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
714 #endif
715 memset(&info, 0, sizeof(info));
716 strcpy(info.srp_version, SRP_VERSION);
717 strncpy(info.partition_name, "qemu", sizeof("qemu"));
718 info.partition_number = cpu_to_be32(0);
719 info.mad_version = cpu_to_be32(1);
720 info.os_type = cpu_to_be32(2);
721 info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
723 rc = spapr_tce_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
724 &info, be16_to_cpu(sinfo->common.length));
725 if (rc) {
726 fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
729 sinfo->common.status = rc ? cpu_to_be32(1) : 0;
731 return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
734 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
736 union mad_iu *mad = &req->iu.mad;
738 switch (be32_to_cpu(mad->empty_iu.common.type)) {
739 case VIOSRP_EMPTY_IU_TYPE:
740 fprintf(stderr, "Unsupported EMPTY MAD IU\n");
741 break;
742 case VIOSRP_ERROR_LOG_TYPE:
743 fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
744 mad->error_log.common.status = cpu_to_be16(1);
745 vscsi_send_iu(s, req, sizeof(mad->error_log), VIOSRP_MAD_FORMAT);
746 break;
747 case VIOSRP_ADAPTER_INFO_TYPE:
748 vscsi_send_adapter_info(s, req);
749 break;
750 case VIOSRP_HOST_CONFIG_TYPE:
751 mad->host_config.common.status = cpu_to_be16(1);
752 vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
753 break;
754 default:
755 fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
756 be32_to_cpu(mad->empty_iu.common.type));
759 return 1;
762 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
764 vscsi_req *req;
765 int done;
767 req = vscsi_get_req(s);
768 if (req == NULL) {
769 fprintf(stderr, "VSCSI: Failed to get a request !\n");
770 return;
773 /* We only support a limited number of descriptors, we know
774 * the ibmvscsi driver uses up to 10 max, so it should fit
775 * in our 256 bytes IUs. If not we'll have to increase the size
776 * of the structure.
778 if (crq->s.IU_length > sizeof(union viosrp_iu)) {
779 fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
780 crq->s.IU_length);
781 return;
784 /* XXX Handle failure differently ? */
785 if (spapr_tce_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
786 crq->s.IU_length)) {
787 fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
788 g_free(req);
790 memcpy(&req->crq, crq, sizeof(vscsi_crq));
792 if (crq->s.format == VIOSRP_MAD_FORMAT) {
793 done = vscsi_handle_mad_req(s, req);
794 } else {
795 done = vscsi_handle_srp_req(s, req);
798 if (done) {
799 vscsi_put_req(req);
804 static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data)
806 VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
807 vscsi_crq crq;
809 memcpy(crq.raw, crq_data, 16);
810 crq.s.timeout = be16_to_cpu(crq.s.timeout);
811 crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
812 crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
814 dprintf("VSCSI: do_crq %02x %02x ...\n", crq.raw[0], crq.raw[1]);
816 switch (crq.s.valid) {
817 case 0xc0: /* Init command/response */
819 /* Respond to initialization request */
820 if (crq.s.format == 0x01) {
821 memset(crq.raw, 0, 16);
822 crq.s.valid = 0xc0;
823 crq.s.format = 0x02;
824 spapr_vio_send_crq(dev, crq.raw);
827 /* Note that in hotplug cases, we might get a 0x02
828 * as a result of us emitting the init request
831 break;
832 case 0xff: /* Link event */
834 /* Not handled for now */
836 break;
837 case 0x80: /* Payloads */
838 switch (crq.s.format) {
839 case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
840 case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
841 vscsi_got_payload(s, &crq);
842 break;
843 case VIOSRP_OS400_FORMAT:
844 case VIOSRP_AIX_FORMAT:
845 case VIOSRP_LINUX_FORMAT:
846 case VIOSRP_INLINE_FORMAT:
847 fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
848 crq.s.format);
849 break;
850 default:
851 fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
852 crq.s.format);
854 break;
855 default:
856 fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
857 crq.raw[0], crq.raw[1]);
860 return 0;
863 static const struct SCSIBusInfo vscsi_scsi_info = {
864 .tcq = true,
865 .ndev = VSCSI_REQ_LIMIT,
867 .transfer_data = vscsi_transfer_data,
868 .complete = vscsi_command_complete,
869 .cancel = vscsi_request_cancelled
872 static int spapr_vscsi_init(VIOsPAPRDevice *dev)
874 VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
875 int i;
877 dbg_vscsi_state = s;
879 /* Initialize qemu request tags */
880 memset(s->reqs, 0, sizeof(s->reqs));
881 for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
882 s->reqs[i].qtag = i;
885 dev->crq.SendFunc = vscsi_do_crq;
887 scsi_bus_new(&s->bus, &dev->qdev, &vscsi_scsi_info);
888 if (!dev->qdev.hotplugged) {
889 scsi_bus_legacy_handle_cmdline(&s->bus);
892 return 0;
895 void spapr_vscsi_create(VIOsPAPRBus *bus, uint32_t reg)
897 DeviceState *dev;
899 dev = qdev_create(&bus->bus, "spapr-vscsi");
900 qdev_prop_set_uint32(dev, "reg", reg);
902 qdev_init_nofail(dev);
905 static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
907 int ret;
909 ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
910 if (ret < 0) {
911 return ret;
914 ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
915 if (ret < 0) {
916 return ret;
919 return 0;
922 static VIOsPAPRDeviceInfo spapr_vscsi = {
923 .init = spapr_vscsi_init,
924 .devnode = spapr_vscsi_devnode,
925 .dt_name = "v-scsi",
926 .dt_type = "vscsi",
927 .dt_compatible = "IBM,v-scsi",
928 .signal_mask = 0x00000001,
929 .qdev.name = "spapr-vscsi",
930 .qdev.size = sizeof(VSCSIState),
931 .qdev.props = (Property[]) {
932 DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev, 0x2000, 0x10000000),
933 DEFINE_PROP_END_OF_LIST(),
937 static void spapr_vscsi_register(void)
939 spapr_vio_bus_register_withprop(&spapr_vscsi);
941 device_init(spapr_vscsi_register);