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
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)
35 #include "hw/scsi/scsi.h"
36 #include "block/scsi.h"
39 #include "hw/ppc/spapr.h"
40 #include "hw/ppc/spapr_vio.h"
45 /*#define DEBUG_VSCSI*/
48 #define dprintf(fmt, ...) \
49 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #define dprintf(fmt, ...) \
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
{
71 typedef struct vscsi_req
{
75 /* SCSI request tracking */
77 uint32_t qtag
; /* qemu tag != srp tag */
82 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
84 /* RDMA related bits */
89 uint16_t cur_desc_num
;
90 uint16_t cur_desc_offset
;
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)
100 vscsi_req reqs
[VSCSI_REQ_LIMIT
];
103 static struct vscsi_req
*vscsi_get_req(VSCSIState
*s
)
108 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
111 memset(req
, 0, sizeof(*req
));
120 static void vscsi_put_req(vscsi_req
*req
)
122 if (req
->sreq
!= NULL
) {
123 scsi_req_unref(req
->sreq
);
129 static SCSIDevice
*vscsi_device_find(SCSIBus
*bus
, uint64_t srp_lun
, int *lun
)
131 int channel
= 0, id
= 0;
134 switch (srp_lun
>> 62) {
136 if ((srp_lun
>> 56) != 0) {
137 channel
= (srp_lun
>> 56) & 0x3f;
138 id
= (srp_lun
>> 48) & 0xff;
142 *lun
= (srp_lun
>> 48) & 0xff;
146 *lun
= (srp_lun
>> 48) & 0x3fff;
149 channel
= (srp_lun
>> 53) & 0x7;
150 id
= (srp_lun
>> 56) & 0x3f;
151 *lun
= (srp_lun
>> 48) & 0x1f;
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
)
168 /* First copy the SRP */
169 rc
= spapr_vio_dma_write(&s
->vdev
, req
->crq
.s
.IU_data_ptr
,
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 */
183 req
->crq
.s
.status
= 0x99; /* Just needs to be non-zero */
185 req
->crq
.s
.status
= 0x00;
188 rc1
= spapr_vio_send_crq(&s
->vdev
, req
->crq
.raw
);
190 fprintf(stderr
, "vscsi_send_iu: Error sending response\n");
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);
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 */
230 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIUNDER
;
233 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIOVER
;
236 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOUNDER
;
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
;
251 iu
->srp
.rsp
.sol_not
= (iu
->srp
.cmd
.sol_not
& 0x04) >> 2;
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
;
259 iu
->srp
.rsp
.sol_not
= (iu
->srp
.cmd
.sol_not
& 0x02) >> 1;
262 vscsi_send_iu(s
, req
, total_len
, VIOSRP_SRP_FORMAT
);
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
);
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");
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");
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
) {
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",
308 rc
= spapr_vio_dma_read(&s
->vdev
, tbl_desc
.va
+ desc_offset
,
309 ret
, sizeof(struct srp_direct_buf
));
311 dprintf("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n",
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
);
318 dprintf("VSCSI: Out of descriptors !\n");
324 fprintf(stderr
, "VSCSI: Unknown format %x\n", req
->dma_fmt
);
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
);
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
;
350 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
353 } else if (rc
== 0) {
357 llen
= MIN(len
, md
.len
);
359 if (req
->writing
) { /* writing = to device = reading from memory */
360 rc
= spapr_vio_dma_read(&s
->vdev
, md
.va
, buf
, llen
);
362 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
369 req
->cur_desc_offset
+= 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
;
379 uint32_t llen
, total
= 0;
381 dprintf("VSCSI: indirect segment 0x%x bytes\n", len
);
383 /* While we have data ... */
385 rc
= vscsi_fetch_desc(s
, req
, req
->cur_desc_num
, req
->cur_desc_offset
, &md
);
388 } else if (rc
== 0) {
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
);
397 rc
= spapr_vio_dma_write(&s
->vdev
, md
.va
, buf
, llen
);
400 dprintf("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n", req
->writing
, rc
);
403 dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
404 buf
[0], buf
[1], buf
[2], buf
[3]);
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 */
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
)
428 switch (req
->dma_fmt
) {
429 case SRP_NO_DATA_DESC
:
430 dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len
);
432 case SRP_DATA_DESC_DIRECT
:
433 err
= vscsi_srp_direct_data(s
, req
, buf
, len
);
435 case SRP_DATA_DESC_INDIRECT
:
436 err
= vscsi_srp_indirect_data(s
, req
, buf
, len
);
442 /* Bits from linux srp */
443 static int data_out_desc_size(struct srp_cmd
*cmd
)
446 uint8_t fmt
= cmd
->buf_fmt
>> 4;
449 case SRP_NO_DATA_DESC
:
451 case SRP_DATA_DESC_DIRECT
:
452 size
= sizeof(struct srp_direct_buf
);
454 case SRP_DATA_DESC_INDIRECT
:
455 size
= sizeof(struct srp_indirect_buf
) +
456 sizeof(struct srp_direct_buf
)*cmd
->data_out_desc_cnt
;
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;
471 req
->dma_fmt
= cmd
->buf_fmt
>> 4;
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
:
480 case SRP_DATA_DESC_DIRECT
:
481 req
->total_desc
= req
->local_desc
= 1;
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
;
495 "vscsi_preprocess_desc: Unknown format %x\n", req
->dma_fmt
);
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
;
510 dprintf("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
511 sreq
->tag
, len
, req
);
513 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
518 buf
= scsi_req_get_buf(sreq
);
519 rc
= vscsi_srp_transfer_data(s
, req
, req
->writing
, buf
, len
);
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
);
528 /* Start next chunk */
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
);
543 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
547 if (status
== CHECK_CONDITION
) {
548 req
->senselen
= scsi_req_get_sense(req
->sreq
, 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
);
561 /* We handle overflows, not underflows for normal commands,
562 * but hopefully nobody cares
565 res_out
= req
->data_len
;
567 res_in
= req
->data_len
;
570 vscsi_send_rsp(s
, req
, status
, res_in
, res_out
);
574 static void vscsi_request_cancelled(SCSIRequest
*sreq
)
576 vscsi_req
*req
= sreq
->hba_private
;
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);
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];
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);
621 alen
= (alen
<< 8) | cdb
[4];
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);
635 vscsi_preprocess_desc(req
);
636 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
638 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
639 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
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
;
651 sdev
= vscsi_device_find(&s
->bus
, be64_to_cpu(srp
->cmd
.lun
), &lun
);
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
);
657 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0x00);
658 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
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
);
669 /* Transfer direction must be set before preprocessing the
672 req
->writing
= (n
< 1);
674 /* Preprocess RDMA descriptors */
675 vscsi_preprocess_desc(req
);
677 /* Get transfer direction and initiate transfer */
683 scsi_req_continue(req
->sreq
);
685 /* Don't touch req here, it may have been recycled already */
690 static int vscsi_process_tsk_mgmt(VSCSIState
*s
, vscsi_req
*req
)
692 union viosrp_iu
*iu
= &req
->iu
;
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
:
703 case SRP_TSK_ABORT_TASK_SET
:
706 case SRP_TSK_CLEAR_TASK_SET
:
709 case SRP_TSK_LUN_RESET
:
710 fn
= LOGICAL_UNIT_RESET
;
712 case SRP_TSK_CLEAR_ACA
:
720 /* XXX Send/Handle target task management */
723 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x20, 0);
724 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
729 static int vscsi_handle_srp_req(VSCSIState
*s
, vscsi_req
*req
)
731 union srp_iu
*srp
= &req
->iu
.srp
;
733 uint8_t opcode
= srp
->rsp
.opcode
;
737 vscsi_process_login(s
, req
);
740 done
= vscsi_process_tsk_mgmt(s
, req
);
743 done
= vscsi_queue_cmd(s
, req
);
753 fprintf(stderr
, "VSCSI: Unsupported opcode %02x\n", opcode
);
756 fprintf(stderr
, "VSCSI: Unknown type %02x\n", opcode
);
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
;
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
));
774 fprintf(stderr
, "vscsi_send_adapter_info: DMA read failure !\n");
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
));
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");
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
);
809 case VIOSRP_ADAPTER_INFO_TYPE
:
810 vscsi_send_adapter_info(s
, req
);
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
);
817 fprintf(stderr
, "VSCSI: Unknown MAD type %02x\n",
818 be32_to_cpu(mad
->empty_iu
.common
.type
));
824 static void vscsi_got_payload(VSCSIState
*s
, vscsi_crq
*crq
)
829 req
= vscsi_get_req(s
);
831 fprintf(stderr
, "VSCSI: Failed to get a request !\n");
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
840 if (crq
->s
.IU_length
> sizeof(union viosrp_iu
)) {
841 fprintf(stderr
, "VSCSI: SRP IU too long (%d bytes) !\n",
847 /* XXX Handle failure differently ? */
848 if (spapr_vio_dma_read(&s
->vdev
, crq
->s
.IU_data_ptr
, &req
->iu
,
850 fprintf(stderr
, "vscsi_got_payload: DMA read failure !\n");
854 memcpy(&req
->crq
, crq
, sizeof(vscsi_crq
));
856 if (crq
->s
.format
== VIOSRP_MAD_FORMAT
) {
857 done
= vscsi_handle_mad_req(s
, req
);
859 done
= vscsi_handle_srp_req(s
, req
);
868 static int vscsi_do_crq(struct VIOsPAPRDevice
*dev
, uint8_t *crq_data
)
870 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
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);
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
896 case 0xff: /* Link event */
898 /* Not handled for now */
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
);
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",
915 fprintf(stderr
, "vscsi_do_srq: Unknown payload format %02x\n",
920 fprintf(stderr
, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
921 crq
.raw
[0], crq
.raw
[1]);
927 static const struct SCSIBusInfo vscsi_scsi_info
= {
929 .max_channel
= 7, /* logical unit addressing format */
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
);
943 memset(s
->reqs
, 0, sizeof(s
->reqs
));
944 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
949 static int spapr_vscsi_init(VIOsPAPRDevice
*dev
)
951 VSCSIState
*s
= VIO_SPAPR_VSCSI_DEVICE(dev
);
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
);
968 void spapr_vscsi_create(VIOsPAPRBus
*bus
)
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
)
981 ret
= fdt_setprop_cell(fdt
, node_off
, "#address-cells", 2);
986 ret
= fdt_setprop_cell(fdt
, node_off
, "#size-cells", 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
)