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)
36 #include "scsi-defs.h"
37 #include "net.h" /* Remove that when we can */
41 #include "hw/spapr_vio.h"
42 #include "hw/ppc-viosrp.h"
46 /*#define DEBUG_VSCSI*/
49 #define dprintf(fmt, ...) \
50 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
52 #define dprintf(fmt, ...) \
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
{
72 typedef struct vscsi_req
{
76 /* SCSI request tracking */
78 uint32_t qtag
; /* qemu tag != srp tag */
85 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
87 /* RDMA related bits */
89 struct srp_direct_buf ext_desc
;
90 struct srp_direct_buf
*cur_desc
;
91 struct srp_indirect_buf
*ind_desc
;
100 vscsi_req reqs
[VSCSI_REQ_LIMIT
];
104 static VSCSIState
*dbg_vscsi_state
;
107 static struct vscsi_req
*vscsi_get_req(VSCSIState
*s
)
112 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
115 memset(req
, 0, sizeof(*req
));
124 static void vscsi_put_req(VSCSIState
*s
, vscsi_req
*req
)
129 static vscsi_req
*vscsi_find_req(VSCSIState
*s
, uint32_t tag
)
131 if (tag
>= VSCSI_REQ_LIMIT
|| !s
->reqs
[tag
].active
) {
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
)
149 /* First copy the SRP */
150 rc
= spapr_tce_dma_write(&s
->vdev
, req
->crq
.s
.IU_data_ptr
,
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 */
164 req
->crq
.s
.status
= 0x99; /* Just needs to be non-zero */
166 req
->crq
.s
.status
= 0x00;
169 rc1
= spapr_vio_send_crq(&s
->vdev
, req
->crq
.raw
);
171 fprintf(stderr
, "vscsi_send_iu: Error sending response\n");
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);
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 */
211 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIUNDER
;
214 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIOVER
;
217 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOUNDER
;
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
;
232 iu
->srp
.rsp
.sol_not
= (iu
->srp
.cmd
.sol_not
& 0x04) >> 2;
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
;
240 iu
->srp
.rsp
.sol_not
= (iu
->srp
.cmd
.sol_not
& 0x02) >> 1;
243 vscsi_send_iu(s
, req
, total_len
, VIOSRP_SRP_FORMAT
);
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
;
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
);
265 if (req
->writing
) { /* writing = to device = reading from memory */
266 rc
= spapr_tce_dma_read(&s
->vdev
, md
->va
, buf
, llen
);
268 rc
= spapr_tce_dma_write(&s
->vdev
, md
->va
, buf
, 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
;
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 ... */
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
;
300 td
->va
+= sizeof(struct srp_direct_buf
);
302 md
= req
->cur_desc
= NULL
;
305 /* No descriptor at hand, fetch one */
307 if (!req
->total_desc
) {
308 dprintf("VSCSI: Out of descriptors !\n");
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
));
317 dprintf("VSCSI: tce_dma_read -> %d reading ext_desc\n", rc
);
321 td
->va
+= sizeof(struct srp_direct_buf
);
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
);
332 rc
= spapr_tce_dma_write(&s
->vdev
, md
->va
, buf
, llen
);
335 dprintf("VSCSI: tce_dma_r/w(%d) -> %d\n", req
->writing
, rc
);
338 dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
339 buf
[0], buf
[1], buf
[2], buf
[3]);
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
)
355 switch (req
->dma_fmt
) {
356 case SRP_NO_DATA_DESC
:
357 dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len
);
359 case SRP_DATA_DESC_DIRECT
:
360 err
= vscsi_srp_direct_data(s
, req
, buf
, len
);
362 case SRP_DATA_DESC_INDIRECT
:
363 err
= vscsi_srp_indirect_data(s
, req
, buf
, len
);
369 /* Bits from linux srp */
370 static int data_out_desc_size(struct srp_cmd
*cmd
)
373 uint8_t fmt
= cmd
->buf_fmt
>> 4;
376 case SRP_NO_DATA_DESC
:
378 case SRP_DATA_DESC_DIRECT
:
379 size
= sizeof(struct srp_direct_buf
);
381 case SRP_DATA_DESC_INDIRECT
:
382 size
= sizeof(struct srp_indirect_buf
) +
383 sizeof(struct srp_direct_buf
)*cmd
->data_out_desc_cnt
;
391 static int vscsi_preprocess_desc(vscsi_req
*req
)
393 struct srp_cmd
*cmd
= &req
->iu
.srp
.cmd
;
396 offset
= cmd
->add_cdb_len
& ~3;
399 req
->dma_fmt
= cmd
->buf_fmt
>> 4;
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
:
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
);
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
);
436 "vscsi_preprocess_desc: Unknown format %x\n", req
->dma_fmt
);
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
;
456 n
= sdev
->info
->send_command(sdev
, req
->qtag
, cdb
, req
->lun
);
457 dprintf("VSCSI: Queued request sense tag 0x%x\n", req
->qtag
);
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
);
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
,
475 VSCSIState
*s
= DO_UPCAST(VSCSIState
, vdev
.qdev
, bus
->qbus
.parent
);
476 vscsi_req
*req
= vscsi_find_req(s
, tag
);
479 int32_t res_in
= 0, res_out
= 0;
482 dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x arg=0x%x, req=%p\n",
483 reason
, tag
, arg
, req
);
485 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", tag
);
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
);
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
);
508 sdev
->info
->read_data(sdev
, req
->qtag
);
513 if (reason
== SCSI_REASON_DONE
) {
514 dprintf("VSCSI: Command complete err=%d\n", arg
);
516 /* We handle overflows, not underflows for normal commands,
517 * but hopefully nobody cares
520 res_out
= req
->data_len
;
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
);
530 vscsi_send_rsp(s
, req
, arg
, 0, 0);
532 vscsi_put_req(s
, req
);
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)
540 buf
= sdev
->info
->get_buf(sdev
, tag
);
541 rc
= vscsi_srp_transfer_data(s
, req
, req
->writing
, buf
, arg
);
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
);
552 /* Start next chunk */
555 sdev
->info
->write_data(sdev
, req
->qtag
);
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);
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];
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);
601 alen
= (alen
<< 8) | cdb
[4];
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);
615 vscsi_preprocess_desc(req
);
616 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
618 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
619 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
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
;
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
;
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
);
640 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0x00);
641 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
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
);
653 /* Transfer direction must be set before preprocessing the
656 req
->writing
= (n
< 1);
658 /* Preprocess RDMA descriptors */
659 vscsi_preprocess_desc(req
);
662 /* Get transfer direction and initiate transfer */
665 sdev
->info
->read_data(sdev
, req
->qtag
);
668 sdev
->info
->write_data(sdev
, req
->qtag
);
670 /* Don't touch req here, it may have been recycled already */
675 static int vscsi_process_tsk_mgmt(VSCSIState
*s
, vscsi_req
*req
)
677 union viosrp_iu
*iu
= &req
->iu
;
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
:
688 case SRP_TSK_ABORT_TASK_SET
:
691 case SRP_TSK_CLEAR_TASK_SET
:
694 case SRP_TSK_LUN_RESET
:
695 fn
= LOGICAL_UNIT_RESET
;
697 case SRP_TSK_CLEAR_ACA
:
705 /* XXX Send/Handle target task management */
708 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x20, 0);
709 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
714 static int vscsi_handle_srp_req(VSCSIState
*s
, vscsi_req
*req
)
716 union srp_iu
*srp
= &req
->iu
.srp
;
718 uint8_t opcode
= srp
->rsp
.opcode
;
722 vscsi_process_login(s
, req
);
725 done
= vscsi_process_tsk_mgmt(s
, req
);
728 done
= vscsi_queue_cmd(s
, req
);
738 fprintf(stderr
, "VSCSI: Unsupported opcode %02x\n", opcode
);
741 fprintf(stderr
, "VSCSI: Unknown type %02x\n", opcode
);
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
;
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
));
759 fprintf(stderr
, "vscsi_send_adapter_info: DMA read failure !\n");
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
));
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");
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
);
794 case VIOSRP_ADAPTER_INFO_TYPE
:
795 vscsi_send_adapter_info(s
, req
);
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
);
802 fprintf(stderr
, "VSCSI: Unknown MAD type %02x\n",
803 be32_to_cpu(mad
->empty_iu
.common
.type
));
809 static void vscsi_got_payload(VSCSIState
*s
, vscsi_crq
*crq
)
814 req
= vscsi_get_req(s
);
816 fprintf(stderr
, "VSCSI: Failed to get a request !\n");
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
825 if (crq
->s
.IU_length
> sizeof(union viosrp_iu
)) {
826 fprintf(stderr
, "VSCSI: SRP IU too long (%d bytes) !\n",
831 /* XXX Handle failure differently ? */
832 if (spapr_tce_dma_read(&s
->vdev
, crq
->s
.IU_data_ptr
, &req
->iu
,
834 fprintf(stderr
, "vscsi_got_payload: DMA read failure !\n");
837 memcpy(&req
->crq
, crq
, sizeof(vscsi_crq
));
839 if (crq
->s
.format
== VIOSRP_MAD_FORMAT
) {
840 done
= vscsi_handle_mad_req(s
, req
);
842 done
= vscsi_handle_srp_req(s
, req
);
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
);
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);
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
879 case 0xff: /* Link event */
881 /* Not handled for now */
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
);
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",
898 fprintf(stderr
, "vscsi_do_srq: Unknown payload format %02x\n",
903 fprintf(stderr
, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
904 crq
.raw
[0], crq
.raw
[1]);
910 static int spapr_vscsi_init(VIOsPAPRDevice
*dev
)
912 VSCSIState
*s
= DO_UPCAST(VSCSIState
, vdev
, dev
);
917 /* Initialize qemu request tags */
918 memset(s
->reqs
, 0, sizeof(s
->reqs
));
919 for (i
= 0; i
< VSCSI_REQ_LIMIT
; 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
);
934 void spapr_vscsi_create(VIOsPAPRBus
*bus
, uint32_t reg
,
935 qemu_irq qirq
, uint32_t vio_irq_num
)
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
;
947 sdev
->vio_irq_num
= vio_irq_num
;
950 static int spapr_vscsi_devnode(VIOsPAPRDevice
*dev
, void *fdt
, int node_off
)
954 ret
= fdt_setprop_cell(fdt
, node_off
, "#address-cells", 2);
959 ret
= fdt_setprop_cell(fdt
, node_off
, "#size-cells", 0);
967 static VIOsPAPRDeviceInfo spapr_vscsi
= {
968 .init
= spapr_vscsi_init
,
969 .devnode
= spapr_vscsi_devnode
,
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
);