1 /* 2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator 3 * 4 * PAPR Virtual SCSI, aka ibmvscsi 5 * 6 * Copyright (c) 2010,2011 Benjamin Herrenschmidt, IBM Corporation. 7 * 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: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 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. 25 * 26 * TODO: 27 * 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) 33 */ 34 #include "hw/hw.h" 35 #include "hw/scsi/scsi.h" 36 #include "block/scsi.h" 37 #include "srp.h" 38 #include "hw/qdev.h" 39 #include "hw/ppc/spapr.h" 40 #include "hw/ppc/spapr_vio.h" 41 #include "viosrp.h" 42 43 #include <libfdt.h> 44 45 /*#define DEBUG_VSCSI*/ 46 47 #ifdef DEBUG_VSCSI 48 #define dprintf(fmt, ...) \ 49 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) 50 #else 51 #define dprintf(fmt, ...) \ 52 do { } while (0) 53 #endif 54 55 /* 56 * Virtual SCSI device 57 */ 58 59 /* Random numbers */ 60 #define VSCSI_MAX_SECTORS 4096 61 #define VSCSI_REQ_LIMIT 24 62 63 #define SCSI_SENSE_BUF_SIZE 96 64 #define SRP_RSP_SENSE_DATA_LEN 18 65 66 typedef union vscsi_crq { 67 struct viosrp_crq s; 68 uint8_t raw[16]; 69 } vscsi_crq; 70 71 typedef struct vscsi_req { 72 vscsi_crq crq; 73 union viosrp_iu iu; 74 75 /* SCSI request tracking */ 76 SCSIRequest *sreq; 77 uint32_t qtag; /* qemu tag != srp tag */ 78 int lun; 79 int active; 80 long data_len; 81 int writing; 82 int senselen; 83 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 84 85 /* RDMA related bits */ 86 uint8_t dma_fmt; 87 struct srp_direct_buf ext_desc; 88 struct srp_direct_buf *cur_desc; 89 struct srp_indirect_buf *ind_desc; 90 int local_desc; 91 int total_desc; 92 } vscsi_req; 93 94 95 typedef struct { 96 VIOsPAPRDevice vdev; 97 SCSIBus bus; 98 vscsi_req reqs[VSCSI_REQ_LIMIT]; 99 } VSCSIState; 100 101 static struct vscsi_req *vscsi_get_req(VSCSIState *s) 102 { 103 vscsi_req *req; 104 int i; 105 106 for (i = 0; i < VSCSI_REQ_LIMIT; i++) { 107 req = &s->reqs[i]; 108 if (!req->active) { 109 memset(req, 0, sizeof(*req)); 110 req->qtag = i; 111 req->active = 1; 112 return req; 113 } 114 } 115 return NULL; 116 } 117 118 static void vscsi_put_req(vscsi_req *req) 119 { 120 if (req->sreq != NULL) { 121 scsi_req_unref(req->sreq); 122 } 123 req->sreq = NULL; 124 req->active = 0; 125 } 126 127 static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun) 128 { 129 int channel = 0, id = 0; 130 131 retry: 132 switch (srp_lun >> 62) { 133 case 0: 134 if ((srp_lun >> 56) != 0) { 135 channel = (srp_lun >> 56) & 0x3f; 136 id = (srp_lun >> 48) & 0xff; 137 srp_lun <<= 16; 138 goto retry; 139 } 140 *lun = (srp_lun >> 48) & 0xff; 141 break; 142 143 case 1: 144 *lun = (srp_lun >> 48) & 0x3fff; 145 break; 146 case 2: 147 channel = (srp_lun >> 53) & 0x7; 148 id = (srp_lun >> 56) & 0x3f; 149 *lun = (srp_lun >> 48) & 0x1f; 150 break; 151 case 3: 152 *lun = -1; 153 return NULL; 154 default: 155 abort(); 156 } 157 158 return scsi_device_find(bus, channel, id, *lun); 159 } 160 161 static int vscsi_send_iu(VSCSIState *s, vscsi_req *req, 162 uint64_t length, uint8_t format) 163 { 164 long rc, rc1; 165 166 /* First copy the SRP */ 167 rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr, 168 &req->iu, length); 169 if (rc) { 170 fprintf(stderr, "vscsi_send_iu: DMA write failure !\n"); 171 } 172 173 req->crq.s.valid = 0x80; 174 req->crq.s.format = format; 175 req->crq.s.reserved = 0x00; 176 req->crq.s.timeout = cpu_to_be16(0x0000); 177 req->crq.s.IU_length = cpu_to_be16(length); 178 req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */ 179 180 if (rc == 0) { 181 req->crq.s.status = 0x99; /* Just needs to be non-zero */ 182 } else { 183 req->crq.s.status = 0x00; 184 } 185 186 rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw); 187 if (rc1) { 188 fprintf(stderr, "vscsi_send_iu: Error sending response\n"); 189 return rc1; 190 } 191 192 return rc; 193 } 194 195 static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req, 196 uint8_t key, uint8_t asc, uint8_t ascq) 197 { 198 req->senselen = SRP_RSP_SENSE_DATA_LEN; 199 200 /* Valid bit and 'current errors' */ 201 req->sense[0] = (0x1 << 7 | 0x70); 202 /* Sense key */ 203 req->sense[2] = key; 204 /* Additional sense length */ 205 req->sense[7] = 0xa; /* 10 bytes */ 206 /* Additional sense code */ 207 req->sense[12] = asc; 208 req->sense[13] = ascq; 209 } 210 211 static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req, 212 uint8_t status, int32_t res_in, int32_t res_out) 213 { 214 union viosrp_iu *iu = &req->iu; 215 uint64_t tag = iu->srp.rsp.tag; 216 int total_len = sizeof(iu->srp.rsp); 217 218 dprintf("VSCSI: Sending resp status: 0x%x, " 219 "res_in: %d, res_out: %d\n", status, res_in, res_out); 220 221 memset(iu, 0, sizeof(struct srp_rsp)); 222 iu->srp.rsp.opcode = SRP_RSP; 223 iu->srp.rsp.req_lim_delta = cpu_to_be32(1); 224 iu->srp.rsp.tag = tag; 225 226 /* Handle residuals */ 227 if (res_in < 0) { 228 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER; 229 res_in = -res_in; 230 } else if (res_in) { 231 iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER; 232 } 233 if (res_out < 0) { 234 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER; 235 res_out = -res_out; 236 } else if (res_out) { 237 iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER; 238 } 239 iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in); 240 iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out); 241 242 /* We don't do response data */ 243 /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */ 244 iu->srp.rsp.resp_data_len = cpu_to_be32(0); 245 246 /* Handle success vs. failure */ 247 iu->srp.rsp.status = status; 248 if (status) { 249 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x04) >> 2; 250 if (req->senselen) { 251 req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID; 252 req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen); 253 memcpy(req->iu.srp.rsp.data, req->sense, req->senselen); 254 total_len += req->senselen; 255 } 256 } else { 257 iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x02) >> 1; 258 } 259 260 vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT); 261 return 0; 262 } 263 264 static inline void vscsi_swap_desc(struct srp_direct_buf *desc) 265 { 266 desc->va = be64_to_cpu(desc->va); 267 desc->len = be32_to_cpu(desc->len); 268 } 269 270 static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req, 271 uint8_t *buf, uint32_t len) 272 { 273 struct srp_direct_buf *md = req->cur_desc; 274 uint32_t llen; 275 int rc = 0; 276 277 dprintf("VSCSI: direct segment 0x%x bytes, va=0x%llx desc len=0x%x\n", 278 len, (unsigned long long)md->va, md->len); 279 280 llen = MIN(len, md->len); 281 if (llen) { 282 if (req->writing) { /* writing = to device = reading from memory */ 283 rc = spapr_vio_dma_read(&s->vdev, md->va, buf, llen); 284 } else { 285 rc = spapr_vio_dma_write(&s->vdev, md->va, buf, llen); 286 } 287 } 288 md->len -= llen; 289 md->va += llen; 290 291 if (rc) { 292 return -1; 293 } 294 return llen; 295 } 296 297 static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req, 298 uint8_t *buf, uint32_t len) 299 { 300 struct srp_direct_buf *td = &req->ind_desc->table_desc; 301 struct srp_direct_buf *md = req->cur_desc; 302 int rc = 0; 303 uint32_t llen, total = 0; 304 305 dprintf("VSCSI: indirect segment 0x%x bytes, td va=0x%llx len=0x%x\n", 306 len, (unsigned long long)td->va, td->len); 307 308 /* While we have data ... */ 309 while (len) { 310 /* If we have a descriptor but it's empty, go fetch a new one */ 311 if (md && md->len == 0) { 312 /* More local available, use one */ 313 if (req->local_desc) { 314 md = ++req->cur_desc; 315 --req->local_desc; 316 --req->total_desc; 317 td->va += sizeof(struct srp_direct_buf); 318 } else { 319 md = req->cur_desc = NULL; 320 } 321 } 322 /* No descriptor at hand, fetch one */ 323 if (!md) { 324 if (!req->total_desc) { 325 dprintf("VSCSI: Out of descriptors !\n"); 326 break; 327 } 328 md = req->cur_desc = &req->ext_desc; 329 dprintf("VSCSI: Reading desc from 0x%llx\n", 330 (unsigned long long)td->va); 331 rc = spapr_vio_dma_read(&s->vdev, td->va, md, 332 sizeof(struct srp_direct_buf)); 333 if (rc) { 334 dprintf("VSCSI: spapr_vio_dma_read -> %d reading ext_desc\n", 335 rc); 336 break; 337 } 338 vscsi_swap_desc(md); 339 td->va += sizeof(struct srp_direct_buf); 340 --req->total_desc; 341 } 342 dprintf("VSCSI: [desc va=0x%llx,len=0x%x] remaining=0x%x\n", 343 (unsigned long long)md->va, md->len, len); 344 345 /* Perform transfer */ 346 llen = MIN(len, md->len); 347 if (req->writing) { /* writing = to device = reading from memory */ 348 rc = spapr_vio_dma_read(&s->vdev, md->va, buf, llen); 349 } else { 350 rc = spapr_vio_dma_write(&s->vdev, md->va, buf, llen); 351 } 352 if (rc) { 353 dprintf("VSCSI: spapr_vio_dma_r/w(%d) -> %d\n", req->writing, rc); 354 break; 355 } 356 dprintf("VSCSI: data: %02x %02x %02x %02x...\n", 357 buf[0], buf[1], buf[2], buf[3]); 358 359 len -= llen; 360 buf += llen; 361 total += llen; 362 md->va += llen; 363 md->len -= llen; 364 } 365 return rc ? -1 : total; 366 } 367 368 static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req, 369 int writing, uint8_t *buf, uint32_t len) 370 { 371 int err = 0; 372 373 switch (req->dma_fmt) { 374 case SRP_NO_DATA_DESC: 375 dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len); 376 break; 377 case SRP_DATA_DESC_DIRECT: 378 err = vscsi_srp_direct_data(s, req, buf, len); 379 break; 380 case SRP_DATA_DESC_INDIRECT: 381 err = vscsi_srp_indirect_data(s, req, buf, len); 382 break; 383 } 384 return err; 385 } 386 387 /* Bits from linux srp */ 388 static int data_out_desc_size(struct srp_cmd *cmd) 389 { 390 int size = 0; 391 uint8_t fmt = cmd->buf_fmt >> 4; 392 393 switch (fmt) { 394 case SRP_NO_DATA_DESC: 395 break; 396 case SRP_DATA_DESC_DIRECT: 397 size = sizeof(struct srp_direct_buf); 398 break; 399 case SRP_DATA_DESC_INDIRECT: 400 size = sizeof(struct srp_indirect_buf) + 401 sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt; 402 break; 403 default: 404 break; 405 } 406 return size; 407 } 408 409 static int vscsi_preprocess_desc(vscsi_req *req) 410 { 411 struct srp_cmd *cmd = &req->iu.srp.cmd; 412 int offset, i; 413 414 offset = cmd->add_cdb_len & ~3; 415 416 if (req->writing) { 417 req->dma_fmt = cmd->buf_fmt >> 4; 418 } else { 419 offset += data_out_desc_size(cmd); 420 req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1); 421 } 422 423 switch (req->dma_fmt) { 424 case SRP_NO_DATA_DESC: 425 break; 426 case SRP_DATA_DESC_DIRECT: 427 req->cur_desc = (struct srp_direct_buf *)(cmd->add_data + offset); 428 req->total_desc = req->local_desc = 1; 429 vscsi_swap_desc(req->cur_desc); 430 dprintf("VSCSI: using direct RDMA %s, 0x%x bytes MD: 0x%llx\n", 431 req->writing ? "write" : "read", 432 req->cur_desc->len, (unsigned long long)req->cur_desc->va); 433 break; 434 case SRP_DATA_DESC_INDIRECT: 435 req->ind_desc = (struct srp_indirect_buf *)(cmd->add_data + offset); 436 vscsi_swap_desc(&req->ind_desc->table_desc); 437 req->total_desc = req->ind_desc->table_desc.len / 438 sizeof(struct srp_direct_buf); 439 req->local_desc = req->writing ? cmd->data_out_desc_cnt : 440 cmd->data_in_desc_cnt; 441 for (i = 0; i < req->local_desc; i++) { 442 vscsi_swap_desc(&req->ind_desc->desc_list[i]); 443 } 444 req->cur_desc = req->local_desc ? &req->ind_desc->desc_list[0] : NULL; 445 dprintf("VSCSI: using indirect RDMA %s, 0x%x bytes %d descs " 446 "(%d local) VA: 0x%llx\n", 447 req->writing ? "read" : "write", 448 be32_to_cpu(req->ind_desc->len), 449 req->total_desc, req->local_desc, 450 (unsigned long long)req->ind_desc->table_desc.va); 451 break; 452 default: 453 fprintf(stderr, 454 "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt); 455 return -1; 456 } 457 458 return 0; 459 } 460 461 /* Callback to indicate that the SCSI layer has completed a transfer. */ 462 static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len) 463 { 464 VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent); 465 vscsi_req *req = sreq->hba_private; 466 uint8_t *buf; 467 int rc = 0; 468 469 dprintf("VSCSI: SCSI xfer complete tag=0x%x len=0x%x, req=%p\n", 470 sreq->tag, len, req); 471 if (req == NULL) { 472 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag); 473 return; 474 } 475 476 if (len) { 477 buf = scsi_req_get_buf(sreq); 478 rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len); 479 } 480 if (rc < 0) { 481 fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc); 482 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0); 483 scsi_req_abort(req->sreq, CHECK_CONDITION); 484 return; 485 } 486 487 /* Start next chunk */ 488 req->data_len -= rc; 489 scsi_req_continue(sreq); 490 } 491 492 /* Callback to indicate that the SCSI layer has completed a transfer. */ 493 static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid) 494 { 495 VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent); 496 vscsi_req *req = sreq->hba_private; 497 int32_t res_in = 0, res_out = 0; 498 499 dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x status=0x%x, req=%p\n", 500 reason, sreq->tag, status, req); 501 if (req == NULL) { 502 fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag); 503 return; 504 } 505 506 if (status == CHECK_CONDITION) { 507 req->senselen = scsi_req_get_sense(req->sreq, req->sense, 508 sizeof(req->sense)); 509 dprintf("VSCSI: Sense data, %d bytes:\n", len); 510 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n", 511 req->sense[0], req->sense[1], req->sense[2], req->sense[3], 512 req->sense[4], req->sense[5], req->sense[6], req->sense[7]); 513 dprintf(" %02x %02x %02x %02x %02x %02x %02x %02x\n", 514 req->sense[8], req->sense[9], req->sense[10], req->sense[11], 515 req->sense[12], req->sense[13], req->sense[14], req->sense[15]); 516 } 517 518 dprintf("VSCSI: Command complete err=%d\n", status); 519 if (status == 0) { 520 /* We handle overflows, not underflows for normal commands, 521 * but hopefully nobody cares 522 */ 523 if (req->writing) { 524 res_out = req->data_len; 525 } else { 526 res_in = req->data_len; 527 } 528 } 529 vscsi_send_rsp(s, req, status, res_in, res_out); 530 vscsi_put_req(req); 531 } 532 533 static void vscsi_request_cancelled(SCSIRequest *sreq) 534 { 535 vscsi_req *req = sreq->hba_private; 536 537 vscsi_put_req(req); 538 } 539 540 static void vscsi_process_login(VSCSIState *s, vscsi_req *req) 541 { 542 union viosrp_iu *iu = &req->iu; 543 struct srp_login_rsp *rsp = &iu->srp.login_rsp; 544 uint64_t tag = iu->srp.rsp.tag; 545 546 dprintf("VSCSI: Got login, sendin response !\n"); 547 548 /* TODO handle case that requested size is wrong and 549 * buffer format is wrong 550 */ 551 memset(iu, 0, sizeof(struct srp_login_rsp)); 552 rsp->opcode = SRP_LOGIN_RSP; 553 /* Don't advertise quite as many request as we support to 554 * keep room for management stuff etc... 555 */ 556 rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2); 557 rsp->tag = tag; 558 rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu)); 559 rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu)); 560 /* direct and indirect */ 561 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT); 562 563 vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT); 564 } 565 566 static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) 567 { 568 uint8_t *cdb = req->iu.srp.cmd.cdb; 569 uint8_t resp_data[36]; 570 int rc, len, alen; 571 572 /* We dont do EVPD. Also check that page_code is 0 */ 573 if ((cdb[1] & 0x01) || (cdb[1] & 0x01) || cdb[2] != 0) { 574 /* Send INVALID FIELD IN CDB */ 575 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0); 576 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); 577 return; 578 } 579 alen = cdb[3]; 580 alen = (alen << 8) | cdb[4]; 581 len = MIN(alen, 36); 582 583 /* Fake up inquiry using PQ=3 */ 584 memset(resp_data, 0, 36); 585 resp_data[0] = 0x7f; /* Not capable of supporting a device here */ 586 resp_data[2] = 0x06; /* SPS-4 */ 587 resp_data[3] = 0x02; /* Resp data format */ 588 resp_data[4] = 36 - 5; /* Additional length */ 589 resp_data[7] = 0x10; /* Sync transfers */ 590 memcpy(&resp_data[16], "QEMU EMPTY ", 16); 591 memcpy(&resp_data[8], "QEMU ", 8); 592 593 req->writing = 0; 594 vscsi_preprocess_desc(req); 595 rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len); 596 if (rc < 0) { 597 vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0); 598 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); 599 } else { 600 vscsi_send_rsp(s, req, 0, 36 - rc, 0); 601 } 602 } 603 604 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req) 605 { 606 union srp_iu *srp = &req->iu.srp; 607 SCSIDevice *sdev; 608 int n, lun; 609 610 sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun); 611 if (!sdev) { 612 dprintf("VSCSI: Command for lun %08" PRIx64 " with no drive\n", be64_to_cpu(srp->cmd.lun)); 613 if (srp->cmd.cdb[0] == INQUIRY) { 614 vscsi_inquiry_no_target(s, req); 615 } else { 616 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00); 617 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); 618 } return 1; 619 } 620 621 req->lun = lun; 622 req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req); 623 n = scsi_req_enqueue(req->sreq); 624 625 dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x ID %d LUN %d ret: %d\n", 626 req->qtag, srp->cmd.cdb[0], id, lun, n); 627 628 if (n) { 629 /* Transfer direction must be set before preprocessing the 630 * descriptors 631 */ 632 req->writing = (n < 1); 633 634 /* Preprocess RDMA descriptors */ 635 vscsi_preprocess_desc(req); 636 637 /* Get transfer direction and initiate transfer */ 638 if (n > 0) { 639 req->data_len = n; 640 } else if (n < 0) { 641 req->data_len = -n; 642 } 643 scsi_req_continue(req->sreq); 644 } 645 /* Don't touch req here, it may have been recycled already */ 646 647 return 0; 648 } 649 650 static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req) 651 { 652 union viosrp_iu *iu = &req->iu; 653 int fn; 654 655 fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n", 656 iu->srp.tsk_mgmt.tsk_mgmt_func); 657 658 switch (iu->srp.tsk_mgmt.tsk_mgmt_func) { 659 #if 0 /* We really don't deal with these for now */ 660 case SRP_TSK_ABORT_TASK: 661 fn = ABORT_TASK; 662 break; 663 case SRP_TSK_ABORT_TASK_SET: 664 fn = ABORT_TASK_SET; 665 break; 666 case SRP_TSK_CLEAR_TASK_SET: 667 fn = CLEAR_TASK_SET; 668 break; 669 case SRP_TSK_LUN_RESET: 670 fn = LOGICAL_UNIT_RESET; 671 break; 672 case SRP_TSK_CLEAR_ACA: 673 fn = CLEAR_ACA; 674 break; 675 #endif 676 default: 677 fn = 0; 678 } 679 if (fn) { 680 /* XXX Send/Handle target task management */ 681 ; 682 } else { 683 vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x20, 0); 684 vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); 685 } 686 return !fn; 687 } 688 689 static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req) 690 { 691 union srp_iu *srp = &req->iu.srp; 692 int done = 1; 693 uint8_t opcode = srp->rsp.opcode; 694 695 switch (opcode) { 696 case SRP_LOGIN_REQ: 697 vscsi_process_login(s, req); 698 break; 699 case SRP_TSK_MGMT: 700 done = vscsi_process_tsk_mgmt(s, req); 701 break; 702 case SRP_CMD: 703 done = vscsi_queue_cmd(s, req); 704 break; 705 case SRP_LOGIN_RSP: 706 case SRP_I_LOGOUT: 707 case SRP_T_LOGOUT: 708 case SRP_RSP: 709 case SRP_CRED_REQ: 710 case SRP_CRED_RSP: 711 case SRP_AER_REQ: 712 case SRP_AER_RSP: 713 fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode); 714 break; 715 default: 716 fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode); 717 } 718 719 return done; 720 } 721 722 static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) 723 { 724 struct viosrp_adapter_info *sinfo; 725 struct mad_adapter_info_data info; 726 int rc; 727 728 sinfo = &req->iu.mad.adapter_info; 729 730 #if 0 /* What for ? */ 731 rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer), 732 &info, be16_to_cpu(sinfo->common.length)); 733 if (rc) { 734 fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n"); 735 } 736 #endif 737 memset(&info, 0, sizeof(info)); 738 strcpy(info.srp_version, SRP_VERSION); 739 memcpy(info.partition_name, "qemu", sizeof("qemu")); 740 info.partition_number = cpu_to_be32(0); 741 info.mad_version = cpu_to_be32(1); 742 info.os_type = cpu_to_be32(2); 743 info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9); 744 745 rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer), 746 &info, be16_to_cpu(sinfo->common.length)); 747 if (rc) { 748 fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n"); 749 } 750 751 sinfo->common.status = rc ? cpu_to_be32(1) : 0; 752 753 return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT); 754 } 755 756 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req) 757 { 758 union mad_iu *mad = &req->iu.mad; 759 760 switch (be32_to_cpu(mad->empty_iu.common.type)) { 761 case VIOSRP_EMPTY_IU_TYPE: 762 fprintf(stderr, "Unsupported EMPTY MAD IU\n"); 763 break; 764 case VIOSRP_ERROR_LOG_TYPE: 765 fprintf(stderr, "Unsupported ERROR LOG MAD IU\n"); 766 mad->error_log.common.status = cpu_to_be16(1); 767 vscsi_send_iu(s, req, sizeof(mad->error_log), VIOSRP_MAD_FORMAT); 768 break; 769 case VIOSRP_ADAPTER_INFO_TYPE: 770 vscsi_send_adapter_info(s, req); 771 break; 772 case VIOSRP_HOST_CONFIG_TYPE: 773 mad->host_config.common.status = cpu_to_be16(1); 774 vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT); 775 break; 776 default: 777 fprintf(stderr, "VSCSI: Unknown MAD type %02x\n", 778 be32_to_cpu(mad->empty_iu.common.type)); 779 } 780 781 return 1; 782 } 783 784 static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq) 785 { 786 vscsi_req *req; 787 int done; 788 789 req = vscsi_get_req(s); 790 if (req == NULL) { 791 fprintf(stderr, "VSCSI: Failed to get a request !\n"); 792 return; 793 } 794 795 /* We only support a limited number of descriptors, we know 796 * the ibmvscsi driver uses up to 10 max, so it should fit 797 * in our 256 bytes IUs. If not we'll have to increase the size 798 * of the structure. 799 */ 800 if (crq->s.IU_length > sizeof(union viosrp_iu)) { 801 fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n", 802 crq->s.IU_length); 803 vscsi_put_req(req); 804 return; 805 } 806 807 /* XXX Handle failure differently ? */ 808 if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu, 809 crq->s.IU_length)) { 810 fprintf(stderr, "vscsi_got_payload: DMA read failure !\n"); 811 vscsi_put_req(req); 812 return; 813 } 814 memcpy(&req->crq, crq, sizeof(vscsi_crq)); 815 816 if (crq->s.format == VIOSRP_MAD_FORMAT) { 817 done = vscsi_handle_mad_req(s, req); 818 } else { 819 done = vscsi_handle_srp_req(s, req); 820 } 821 822 if (done) { 823 vscsi_put_req(req); 824 } 825 } 826 827 828 static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data) 829 { 830 VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev); 831 vscsi_crq crq; 832 833 memcpy(crq.raw, crq_data, 16); 834 crq.s.timeout = be16_to_cpu(crq.s.timeout); 835 crq.s.IU_length = be16_to_cpu(crq.s.IU_length); 836 crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr); 837 838 dprintf("VSCSI: do_crq %02x %02x ...\n", crq.raw[0], crq.raw[1]); 839 840 switch (crq.s.valid) { 841 case 0xc0: /* Init command/response */ 842 843 /* Respond to initialization request */ 844 if (crq.s.format == 0x01) { 845 memset(crq.raw, 0, 16); 846 crq.s.valid = 0xc0; 847 crq.s.format = 0x02; 848 spapr_vio_send_crq(dev, crq.raw); 849 } 850 851 /* Note that in hotplug cases, we might get a 0x02 852 * as a result of us emitting the init request 853 */ 854 855 break; 856 case 0xff: /* Link event */ 857 858 /* Not handled for now */ 859 860 break; 861 case 0x80: /* Payloads */ 862 switch (crq.s.format) { 863 case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */ 864 case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */ 865 vscsi_got_payload(s, &crq); 866 break; 867 case VIOSRP_OS400_FORMAT: 868 case VIOSRP_AIX_FORMAT: 869 case VIOSRP_LINUX_FORMAT: 870 case VIOSRP_INLINE_FORMAT: 871 fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n", 872 crq.s.format); 873 break; 874 default: 875 fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n", 876 crq.s.format); 877 } 878 break; 879 default: 880 fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n", 881 crq.raw[0], crq.raw[1]); 882 }; 883 884 return 0; 885 } 886 887 static const struct SCSIBusInfo vscsi_scsi_info = { 888 .tcq = true, 889 .max_channel = 7, /* logical unit addressing format */ 890 .max_target = 63, 891 .max_lun = 31, 892 893 .transfer_data = vscsi_transfer_data, 894 .complete = vscsi_command_complete, 895 .cancel = vscsi_request_cancelled 896 }; 897 898 static void spapr_vscsi_reset(VIOsPAPRDevice *dev) 899 { 900 VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev); 901 int i; 902 903 memset(s->reqs, 0, sizeof(s->reqs)); 904 for (i = 0; i < VSCSI_REQ_LIMIT; i++) { 905 s->reqs[i].qtag = i; 906 } 907 } 908 909 static int spapr_vscsi_init(VIOsPAPRDevice *dev) 910 { 911 VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev); 912 913 dev->crq.SendFunc = vscsi_do_crq; 914 915 scsi_bus_new(&s->bus, &dev->qdev, &vscsi_scsi_info); 916 if (!dev->qdev.hotplugged) { 917 scsi_bus_legacy_handle_cmdline(&s->bus); 918 } 919 920 return 0; 921 } 922 923 void spapr_vscsi_create(VIOsPAPRBus *bus) 924 { 925 DeviceState *dev; 926 927 dev = qdev_create(&bus->bus, "spapr-vscsi"); 928 929 qdev_init_nofail(dev); 930 } 931 932 static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off) 933 { 934 int ret; 935 936 ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2); 937 if (ret < 0) { 938 return ret; 939 } 940 941 ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0); 942 if (ret < 0) { 943 return ret; 944 } 945 946 return 0; 947 } 948 949 static Property spapr_vscsi_properties[] = { 950 DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev), 951 DEFINE_PROP_END_OF_LIST(), 952 }; 953 954 static void spapr_vscsi_class_init(ObjectClass *klass, void *data) 955 { 956 DeviceClass *dc = DEVICE_CLASS(klass); 957 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); 958 959 k->init = spapr_vscsi_init; 960 k->reset = spapr_vscsi_reset; 961 k->devnode = spapr_vscsi_devnode; 962 k->dt_name = "v-scsi"; 963 k->dt_type = "vscsi"; 964 k->dt_compatible = "IBM,v-scsi"; 965 k->signal_mask = 0x00000001; 966 dc->props = spapr_vscsi_properties; 967 k->rtce_window_size = 0x10000000; 968 } 969 970 static const TypeInfo spapr_vscsi_info = { 971 .name = "spapr-vscsi", 972 .parent = TYPE_VIO_SPAPR_DEVICE, 973 .instance_size = sizeof(VSCSIState), 974 .class_init = spapr_vscsi_class_init, 975 }; 976 977 static void spapr_vscsi_register_types(void) 978 { 979 type_register_static(&spapr_vscsi_info); 980 } 981 982 type_init(spapr_vscsi_register_types) 983