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
)
126 if (req
->sreq
!= NULL
) {
127 scsi_req_unref(req
->sreq
);
133 static vscsi_req
*vscsi_find_req(VSCSIState
*s
, SCSIRequest
*req
)
135 uint32_t tag
= req
->tag
;
136 if (tag
>= VSCSI_REQ_LIMIT
|| !s
->reqs
[tag
].active
) {
139 return &s
->reqs
[tag
];
142 static void vscsi_decode_id_lun(uint64_t srp_lun
, int *id
, int *lun
)
144 /* XXX Figure that one out properly ! This is crackpot */
145 *id
= (srp_lun
>> 56) & 0x7f;
146 *lun
= (srp_lun
>> 48) & 0xff;
149 static int vscsi_send_iu(VSCSIState
*s
, vscsi_req
*req
,
150 uint64_t length
, uint8_t format
)
154 /* First copy the SRP */
155 rc
= spapr_tce_dma_write(&s
->vdev
, req
->crq
.s
.IU_data_ptr
,
158 fprintf(stderr
, "vscsi_send_iu: DMA write failure !\n");
161 req
->crq
.s
.valid
= 0x80;
162 req
->crq
.s
.format
= format
;
163 req
->crq
.s
.reserved
= 0x00;
164 req
->crq
.s
.timeout
= cpu_to_be16(0x0000);
165 req
->crq
.s
.IU_length
= cpu_to_be16(length
);
166 req
->crq
.s
.IU_data_ptr
= req
->iu
.srp
.rsp
.tag
; /* right byte order */
169 req
->crq
.s
.status
= 0x99; /* Just needs to be non-zero */
171 req
->crq
.s
.status
= 0x00;
174 rc1
= spapr_vio_send_crq(&s
->vdev
, req
->crq
.raw
);
176 fprintf(stderr
, "vscsi_send_iu: Error sending response\n");
183 static void vscsi_makeup_sense(VSCSIState
*s
, vscsi_req
*req
,
184 uint8_t key
, uint8_t asc
, uint8_t ascq
)
186 req
->senselen
= SRP_RSP_SENSE_DATA_LEN
;
188 /* Valid bit and 'current errors' */
189 req
->sense
[0] = (0x1 << 7 | 0x70);
192 /* Additional sense length */
193 req
->sense
[7] = 0xa; /* 10 bytes */
194 /* Additional sense code */
195 req
->sense
[12] = asc
;
196 req
->sense
[13] = ascq
;
199 static int vscsi_send_rsp(VSCSIState
*s
, vscsi_req
*req
,
200 uint8_t status
, int32_t res_in
, int32_t res_out
)
202 union viosrp_iu
*iu
= &req
->iu
;
203 uint64_t tag
= iu
->srp
.rsp
.tag
;
204 int total_len
= sizeof(iu
->srp
.rsp
);
206 dprintf("VSCSI: Sending resp status: 0x%x, "
207 "res_in: %d, res_out: %d\n", status
, res_in
, res_out
);
209 memset(iu
, 0, sizeof(struct srp_rsp
));
210 iu
->srp
.rsp
.opcode
= SRP_RSP
;
211 iu
->srp
.rsp
.req_lim_delta
= cpu_to_be32(1);
212 iu
->srp
.rsp
.tag
= tag
;
214 /* Handle residuals */
216 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIUNDER
;
219 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DIOVER
;
222 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOUNDER
;
224 } else if (res_out
) {
225 iu
->srp
.rsp
.flags
|= SRP_RSP_FLAG_DOOVER
;
227 iu
->srp
.rsp
.data_in_res_cnt
= cpu_to_be32(res_in
);
228 iu
->srp
.rsp
.data_out_res_cnt
= cpu_to_be32(res_out
);
230 /* We don't do response data */
231 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
232 iu
->srp
.rsp
.resp_data_len
= cpu_to_be32(0);
234 /* Handle success vs. failure */
235 iu
->srp
.rsp
.status
= status
;
237 iu
->srp
.rsp
.sol_not
= (iu
->srp
.cmd
.sol_not
& 0x04) >> 2;
239 req
->iu
.srp
.rsp
.flags
|= SRP_RSP_FLAG_SNSVALID
;
240 req
->iu
.srp
.rsp
.sense_data_len
= cpu_to_be32(req
->senselen
);
241 memcpy(req
->iu
.srp
.rsp
.data
, req
->sense
, req
->senselen
);
242 total_len
+= req
->senselen
;
245 iu
->srp
.rsp
.sol_not
= (iu
->srp
.cmd
.sol_not
& 0x02) >> 1;
248 vscsi_send_iu(s
, req
, total_len
, VIOSRP_SRP_FORMAT
);
252 static inline void vscsi_swap_desc(struct srp_direct_buf
*desc
)
254 desc
->va
= be64_to_cpu(desc
->va
);
255 desc
->len
= be32_to_cpu(desc
->len
);
258 static int vscsi_srp_direct_data(VSCSIState
*s
, vscsi_req
*req
,
259 uint8_t *buf
, uint32_t len
)
261 struct srp_direct_buf
*md
= req
->cur_desc
;
265 dprintf("VSCSI: direct segment 0x%x bytes, va=0x%llx desc len=0x%x\n",
266 len
, (unsigned long long)md
->va
, md
->len
);
268 llen
= MIN(len
, md
->len
);
270 if (req
->writing
) { /* writing = to device = reading from memory */
271 rc
= spapr_tce_dma_read(&s
->vdev
, md
->va
, buf
, llen
);
273 rc
= spapr_tce_dma_write(&s
->vdev
, md
->va
, buf
, llen
);
285 static int vscsi_srp_indirect_data(VSCSIState
*s
, vscsi_req
*req
,
286 uint8_t *buf
, uint32_t len
)
288 struct srp_direct_buf
*td
= &req
->ind_desc
->table_desc
;
289 struct srp_direct_buf
*md
= req
->cur_desc
;
291 uint32_t llen
, total
= 0;
293 dprintf("VSCSI: indirect segment 0x%x bytes, td va=0x%llx len=0x%x\n",
294 len
, (unsigned long long)td
->va
, td
->len
);
296 /* While we have data ... */
298 /* If we have a descriptor but it's empty, go fetch a new one */
299 if (md
&& md
->len
== 0) {
300 /* More local available, use one */
301 if (req
->local_desc
) {
302 md
= ++req
->cur_desc
;
305 td
->va
+= sizeof(struct srp_direct_buf
);
307 md
= req
->cur_desc
= NULL
;
310 /* No descriptor at hand, fetch one */
312 if (!req
->total_desc
) {
313 dprintf("VSCSI: Out of descriptors !\n");
316 md
= req
->cur_desc
= &req
->ext_desc
;
317 dprintf("VSCSI: Reading desc from 0x%llx\n",
318 (unsigned long long)td
->va
);
319 rc
= spapr_tce_dma_read(&s
->vdev
, td
->va
, md
,
320 sizeof(struct srp_direct_buf
));
322 dprintf("VSCSI: tce_dma_read -> %d reading ext_desc\n", rc
);
326 td
->va
+= sizeof(struct srp_direct_buf
);
329 dprintf("VSCSI: [desc va=0x%llx,len=0x%x] remaining=0x%x\n",
330 (unsigned long long)md
->va
, md
->len
, len
);
332 /* Perform transfer */
333 llen
= MIN(len
, md
->len
);
334 if (req
->writing
) { /* writing = to device = reading from memory */
335 rc
= spapr_tce_dma_read(&s
->vdev
, md
->va
, buf
, llen
);
337 rc
= spapr_tce_dma_write(&s
->vdev
, md
->va
, buf
, llen
);
340 dprintf("VSCSI: tce_dma_r/w(%d) -> %d\n", req
->writing
, rc
);
343 dprintf("VSCSI: data: %02x %02x %02x %02x...\n",
344 buf
[0], buf
[1], buf
[2], buf
[3]);
352 return rc
? -1 : total
;
355 static int vscsi_srp_transfer_data(VSCSIState
*s
, vscsi_req
*req
,
356 int writing
, uint8_t *buf
, uint32_t len
)
360 switch (req
->dma_fmt
) {
361 case SRP_NO_DATA_DESC
:
362 dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len
);
364 case SRP_DATA_DESC_DIRECT
:
365 err
= vscsi_srp_direct_data(s
, req
, buf
, len
);
367 case SRP_DATA_DESC_INDIRECT
:
368 err
= vscsi_srp_indirect_data(s
, req
, buf
, len
);
374 /* Bits from linux srp */
375 static int data_out_desc_size(struct srp_cmd
*cmd
)
378 uint8_t fmt
= cmd
->buf_fmt
>> 4;
381 case SRP_NO_DATA_DESC
:
383 case SRP_DATA_DESC_DIRECT
:
384 size
= sizeof(struct srp_direct_buf
);
386 case SRP_DATA_DESC_INDIRECT
:
387 size
= sizeof(struct srp_indirect_buf
) +
388 sizeof(struct srp_direct_buf
)*cmd
->data_out_desc_cnt
;
396 static int vscsi_preprocess_desc(vscsi_req
*req
)
398 struct srp_cmd
*cmd
= &req
->iu
.srp
.cmd
;
401 offset
= cmd
->add_cdb_len
& ~3;
404 req
->dma_fmt
= cmd
->buf_fmt
>> 4;
406 offset
+= data_out_desc_size(cmd
);
407 req
->dma_fmt
= cmd
->buf_fmt
& ((1U << 4) - 1);
410 switch (req
->dma_fmt
) {
411 case SRP_NO_DATA_DESC
:
413 case SRP_DATA_DESC_DIRECT
:
414 req
->cur_desc
= (struct srp_direct_buf
*)(cmd
->add_data
+ offset
);
415 req
->total_desc
= req
->local_desc
= 1;
416 vscsi_swap_desc(req
->cur_desc
);
417 dprintf("VSCSI: using direct RDMA %s, 0x%x bytes MD: 0x%llx\n",
418 req
->writing
? "write" : "read",
419 req
->cur_desc
->len
, (unsigned long long)req
->cur_desc
->va
);
421 case SRP_DATA_DESC_INDIRECT
:
422 req
->ind_desc
= (struct srp_indirect_buf
*)(cmd
->add_data
+ offset
);
423 vscsi_swap_desc(&req
->ind_desc
->table_desc
);
424 req
->total_desc
= req
->ind_desc
->table_desc
.len
/
425 sizeof(struct srp_direct_buf
);
426 req
->local_desc
= req
->writing
? cmd
->data_out_desc_cnt
:
427 cmd
->data_in_desc_cnt
;
428 for (i
= 0; i
< req
->local_desc
; i
++) {
429 vscsi_swap_desc(&req
->ind_desc
->desc_list
[i
]);
431 req
->cur_desc
= req
->local_desc
? &req
->ind_desc
->desc_list
[0] : NULL
;
432 dprintf("VSCSI: using indirect RDMA %s, 0x%x bytes %d descs "
433 "(%d local) VA: 0x%llx\n",
434 req
->writing
? "read" : "write",
435 be32_to_cpu(req
->ind_desc
->len
),
436 req
->total_desc
, req
->local_desc
,
437 (unsigned long long)req
->ind_desc
->table_desc
.va
);
441 "vscsi_preprocess_desc: Unknown format %x\n", req
->dma_fmt
);
448 static void vscsi_send_request_sense(VSCSIState
*s
, vscsi_req
*req
)
450 uint8_t *cdb
= req
->iu
.srp
.cmd
.cdb
;
453 n
= scsi_req_get_sense(req
->sreq
, req
->sense
, sizeof(req
->sense
));
456 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
457 vscsi_put_req(s
, req
);
461 dprintf("VSCSI: Got CHECK_CONDITION, requesting sense...\n");
469 n
= scsi_req_enqueue(req
->sreq
, cdb
);
470 dprintf("VSCSI: Queued request sense tag 0x%x\n", req
->qtag
);
472 fprintf(stderr
, "VSCSI: REQUEST_SENSE wants write data !?!?!?\n");
473 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
474 scsi_req_abort(req
->sreq
, CHECK_CONDITION
);
479 scsi_req_continue(req
->sreq
);
482 /* Callback to indicate that the SCSI layer has completed a transfer. */
483 static void vscsi_transfer_data(SCSIRequest
*sreq
, uint32_t len
)
485 VSCSIState
*s
= DO_UPCAST(VSCSIState
, vdev
.qdev
, sreq
->bus
->qbus
.parent
);
486 vscsi_req
*req
= vscsi_find_req(s
, sreq
);
490 dprintf("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n",
491 sreq
->tag
, len
, req
);
493 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
498 uint8_t *buf
= scsi_req_get_buf(sreq
);
500 len
= MIN(len
, SCSI_SENSE_BUF_SIZE
);
501 dprintf("VSCSI: Sense data, %d bytes:\n", len
);
502 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
503 buf
[0], buf
[1], buf
[2], buf
[3],
504 buf
[4], buf
[5], buf
[6], buf
[7]);
505 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
506 buf
[8], buf
[9], buf
[10], buf
[11],
507 buf
[12], buf
[13], buf
[14], buf
[15]);
508 memcpy(req
->sense
, buf
, len
);
510 scsi_req_continue(req
->sreq
);
515 buf
= scsi_req_get_buf(sreq
);
516 rc
= vscsi_srp_transfer_data(s
, req
, req
->writing
, buf
, len
);
519 fprintf(stderr
, "VSCSI: RDMA error rc=%d!\n", rc
);
520 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
521 scsi_req_abort(req
->sreq
, CHECK_CONDITION
);
525 /* Start next chunk */
527 scsi_req_continue(sreq
);
530 /* Callback to indicate that the SCSI layer has completed a transfer. */
531 static void vscsi_command_complete(SCSIRequest
*sreq
, uint32_t status
)
533 VSCSIState
*s
= DO_UPCAST(VSCSIState
, vdev
.qdev
, sreq
->bus
->qbus
.parent
);
534 vscsi_req
*req
= vscsi_find_req(s
, sreq
);
535 int32_t res_in
= 0, res_out
= 0;
537 dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x status=0x%x, req=%p\n",
538 reason
, sreq
->tag
, status
, req
);
540 fprintf(stderr
, "VSCSI: Can't find request for tag 0x%x\n", sreq
->tag
);
544 if (!req
->sensing
&& status
== CHECK_CONDITION
) {
545 vscsi_send_request_sense(s
, req
);
550 dprintf("VSCSI: Sense done !\n");
551 status
= CHECK_CONDITION
;
553 dprintf("VSCSI: Command complete err=%d\n", status
);
555 /* We handle overflows, not underflows for normal commands,
556 * but hopefully nobody cares
559 res_out
= req
->data_len
;
561 res_in
= req
->data_len
;
565 vscsi_send_rsp(s
, req
, 0, res_in
, res_out
);
566 vscsi_put_req(s
, req
);
569 static void vscsi_request_cancelled(SCSIRequest
*sreq
)
571 VSCSIState
*s
= DO_UPCAST(VSCSIState
, vdev
.qdev
, sreq
->bus
->qbus
.parent
);
572 vscsi_req
*req
= vscsi_find_req(s
, sreq
);
574 vscsi_put_req(s
, req
);
577 static void vscsi_process_login(VSCSIState
*s
, vscsi_req
*req
)
579 union viosrp_iu
*iu
= &req
->iu
;
580 struct srp_login_rsp
*rsp
= &iu
->srp
.login_rsp
;
581 uint64_t tag
= iu
->srp
.rsp
.tag
;
583 dprintf("VSCSI: Got login, sendin response !\n");
585 /* TODO handle case that requested size is wrong and
586 * buffer format is wrong
588 memset(iu
, 0, sizeof(struct srp_login_rsp
));
589 rsp
->opcode
= SRP_LOGIN_RSP
;
590 /* Don't advertise quite as many request as we support to
591 * keep room for management stuff etc...
593 rsp
->req_lim_delta
= cpu_to_be32(VSCSI_REQ_LIMIT
-2);
595 rsp
->max_it_iu_len
= cpu_to_be32(sizeof(union srp_iu
));
596 rsp
->max_ti_iu_len
= cpu_to_be32(sizeof(union srp_iu
));
597 /* direct and indirect */
598 rsp
->buf_fmt
= cpu_to_be16(SRP_BUF_FORMAT_DIRECT
| SRP_BUF_FORMAT_INDIRECT
);
600 vscsi_send_iu(s
, req
, sizeof(*rsp
), VIOSRP_SRP_FORMAT
);
603 static void vscsi_inquiry_no_target(VSCSIState
*s
, vscsi_req
*req
)
605 uint8_t *cdb
= req
->iu
.srp
.cmd
.cdb
;
606 uint8_t resp_data
[36];
609 /* We dont do EVPD. Also check that page_code is 0 */
610 if ((cdb
[1] & 0x01) || (cdb
[1] & 0x01) || cdb
[2] != 0) {
611 /* Send INVALID FIELD IN CDB */
612 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0);
613 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
617 alen
= (alen
<< 8) | cdb
[4];
620 /* Fake up inquiry using PQ=3 */
621 memset(resp_data
, 0, 36);
622 resp_data
[0] = 0x7f; /* Not capable of supporting a device here */
623 resp_data
[2] = 0x06; /* SPS-4 */
624 resp_data
[3] = 0x02; /* Resp data format */
625 resp_data
[4] = 36 - 5; /* Additional length */
626 resp_data
[7] = 0x10; /* Sync transfers */
627 memcpy(&resp_data
[16], "QEMU EMPTY ", 16);
628 memcpy(&resp_data
[8], "QEMU ", 8);
631 vscsi_preprocess_desc(req
);
632 rc
= vscsi_srp_transfer_data(s
, req
, 0, resp_data
, len
);
634 vscsi_makeup_sense(s
, req
, HARDWARE_ERROR
, 0, 0);
635 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
637 vscsi_send_rsp(s
, req
, 0, 36 - rc
, 0);
641 static int vscsi_queue_cmd(VSCSIState
*s
, vscsi_req
*req
)
643 union srp_iu
*srp
= &req
->iu
.srp
;
647 vscsi_decode_id_lun(be64_to_cpu(srp
->cmd
.lun
), &id
, &lun
);
649 /* Qemu vs. linux issue with LUNs to be sorted out ... */
650 sdev
= (id
< 8 && lun
< 16) ? s
->bus
.devs
[id
] : NULL
;
652 dprintf("VSCSI: Command for id %d with no drive\n", id
);
653 if (srp
->cmd
.cdb
[0] == INQUIRY
) {
654 vscsi_inquiry_no_target(s
, req
);
656 vscsi_makeup_sense(s
, req
, ILLEGAL_REQUEST
, 0x24, 0x00);
657 vscsi_send_rsp(s
, req
, CHECK_CONDITION
, 0, 0);
662 req
->sreq
= scsi_req_new(sdev
, req
->qtag
, lun
);
663 n
= scsi_req_enqueue(req
->sreq
, srp
->cmd
.cdb
);
665 dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x ID %d LUN %d ret: %d\n",
666 req
->qtag
, srp
->cmd
.cdb
[0], id
, 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_tce_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 strncpy(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_tce_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",
846 /* XXX Handle failure differently ? */
847 if (spapr_tce_dma_read(&s
->vdev
, crq
->s
.IU_data_ptr
, &req
->iu
,
849 fprintf(stderr
, "vscsi_got_payload: DMA read failure !\n");
852 memcpy(&req
->crq
, crq
, sizeof(vscsi_crq
));
854 if (crq
->s
.format
== VIOSRP_MAD_FORMAT
) {
855 done
= vscsi_handle_mad_req(s
, req
);
857 done
= vscsi_handle_srp_req(s
, req
);
861 vscsi_put_req(s
, req
);
866 static int vscsi_do_crq(struct VIOsPAPRDevice
*dev
, uint8_t *crq_data
)
868 VSCSIState
*s
= DO_UPCAST(VSCSIState
, vdev
, dev
);
871 memcpy(crq
.raw
, crq_data
, 16);
872 crq
.s
.timeout
= be16_to_cpu(crq
.s
.timeout
);
873 crq
.s
.IU_length
= be16_to_cpu(crq
.s
.IU_length
);
874 crq
.s
.IU_data_ptr
= be64_to_cpu(crq
.s
.IU_data_ptr
);
876 dprintf("VSCSI: do_crq %02x %02x ...\n", crq
.raw
[0], crq
.raw
[1]);
878 switch (crq
.s
.valid
) {
879 case 0xc0: /* Init command/response */
881 /* Respond to initialization request */
882 if (crq
.s
.format
== 0x01) {
883 memset(crq
.raw
, 0, 16);
886 spapr_vio_send_crq(dev
, crq
.raw
);
889 /* Note that in hotplug cases, we might get a 0x02
890 * as a result of us emitting the init request
894 case 0xff: /* Link event */
896 /* Not handled for now */
899 case 0x80: /* Payloads */
900 switch (crq
.s
.format
) {
901 case VIOSRP_SRP_FORMAT
: /* AKA VSCSI request */
902 case VIOSRP_MAD_FORMAT
: /* AKA VSCSI response */
903 vscsi_got_payload(s
, &crq
);
905 case VIOSRP_OS400_FORMAT
:
906 case VIOSRP_AIX_FORMAT
:
907 case VIOSRP_LINUX_FORMAT
:
908 case VIOSRP_INLINE_FORMAT
:
909 fprintf(stderr
, "vscsi_do_srq: Unsupported payload format %02x\n",
913 fprintf(stderr
, "vscsi_do_srq: Unknown payload format %02x\n",
918 fprintf(stderr
, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
919 crq
.raw
[0], crq
.raw
[1]);
925 static const struct SCSIBusOps vscsi_scsi_ops
= {
926 .transfer_data
= vscsi_transfer_data
,
927 .complete
= vscsi_command_complete
,
928 .cancel
= vscsi_request_cancelled
931 static int spapr_vscsi_init(VIOsPAPRDevice
*dev
)
933 VSCSIState
*s
= DO_UPCAST(VSCSIState
, vdev
, dev
);
938 /* Initialize qemu request tags */
939 memset(s
->reqs
, 0, sizeof(s
->reqs
));
940 for (i
= 0; i
< VSCSI_REQ_LIMIT
; i
++) {
944 dev
->crq
.SendFunc
= vscsi_do_crq
;
946 scsi_bus_new(&s
->bus
, &dev
->qdev
, 1, VSCSI_REQ_LIMIT
,
948 if (!dev
->qdev
.hotplugged
) {
949 scsi_bus_legacy_handle_cmdline(&s
->bus
);
955 void spapr_vscsi_create(VIOsPAPRBus
*bus
, uint32_t reg
,
956 qemu_irq qirq
, uint32_t vio_irq_num
)
959 VIOsPAPRDevice
*sdev
;
961 dev
= qdev_create(&bus
->bus
, "spapr-vscsi");
962 qdev_prop_set_uint32(dev
, "reg", reg
);
964 qdev_init_nofail(dev
);
966 sdev
= (VIOsPAPRDevice
*)dev
;
968 sdev
->vio_irq_num
= vio_irq_num
;
971 static int spapr_vscsi_devnode(VIOsPAPRDevice
*dev
, void *fdt
, int node_off
)
975 ret
= fdt_setprop_cell(fdt
, node_off
, "#address-cells", 2);
980 ret
= fdt_setprop_cell(fdt
, node_off
, "#size-cells", 0);
988 static VIOsPAPRDeviceInfo spapr_vscsi
= {
989 .init
= spapr_vscsi_init
,
990 .devnode
= spapr_vscsi_devnode
,
993 .dt_compatible
= "IBM,v-scsi",
994 .signal_mask
= 0x00000001,
995 .qdev
.name
= "spapr-vscsi",
996 .qdev
.size
= sizeof(VSCSIState
),
997 .qdev
.props
= (Property
[]) {
998 DEFINE_PROP_UINT32("reg", VIOsPAPRDevice
, reg
, 0x2000),
999 DEFINE_PROP_UINT32("dma-window", VIOsPAPRDevice
,
1000 rtce_window_size
, 0x10000000),
1001 DEFINE_PROP_END_OF_LIST(),
1005 static void spapr_vscsi_register(void)
1007 spapr_vio_bus_register_withprop(&spapr_vscsi
);
1009 device_init(spapr_vscsi_register
);