Statistics
| Branch: | Revision:

root / hw / spapr_vscsi.c @ 43a2b339

History | View | Annotate | Download (29.2 kB)

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.h"
35
#include "scsi.h"
36
#include "scsi-defs.h"
37
#include "net.h" /* Remove that when we can */
38
#include "srp.h"
39
#include "hw/qdev.h"
40
#include "hw/spapr.h"
41
#include "hw/spapr_vio.h"
42
#include "hw/ppc-viosrp.h"
43

    
44
#include <libfdt.h>
45

    
46
/*#define DEBUG_VSCSI*/
47

    
48
#ifdef DEBUG_VSCSI
49
#define dprintf(fmt, ...) \
50
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51
#else
52
#define dprintf(fmt, ...) \
53
    do { } while (0)
54
#endif
55

    
56
/*
57
 * Virtual SCSI device
58
 */
59

    
60
/* Random numbers */
61
#define VSCSI_MAX_SECTORS       4096
62
#define VSCSI_REQ_LIMIT         24
63

    
64
#define SCSI_SENSE_BUF_SIZE     96
65
#define SRP_RSP_SENSE_DATA_LEN  18
66

    
67
typedef union vscsi_crq {
68
    struct viosrp_crq s;
69
    uint8_t raw[16];
70
} vscsi_crq;
71

    
72
typedef struct vscsi_req {
73
    vscsi_crq               crq;
74
    union viosrp_iu         iu;
75

    
76
    /* SCSI request tracking */
77
    SCSIDevice              *sdev;
78
    SCSIRequest             *sreq;
79
    uint32_t                qtag; /* qemu tag != srp tag */
80
    int                     lun;
81
    int                     active;
82
    long                    data_len;
83
    int                     writing;
84
    int                     sensing;
85
    int                     senselen;
86
    uint8_t                 sense[SCSI_SENSE_BUF_SIZE];
87

    
88
    /* RDMA related bits */
89
    uint8_t                 dma_fmt;
90
    struct srp_direct_buf   ext_desc;
91
    struct srp_direct_buf   *cur_desc;
92
    struct srp_indirect_buf *ind_desc;
93
    int                     local_desc;
94
    int                     total_desc;
95
} vscsi_req;
96

    
97

    
98
typedef struct {
99
    VIOsPAPRDevice vdev;
100
    SCSIBus bus;
101
    vscsi_req reqs[VSCSI_REQ_LIMIT];
102
} VSCSIState;
103

    
104
/* XXX Debug only */
105
static VSCSIState *dbg_vscsi_state;
106

    
107

    
108
static struct vscsi_req *vscsi_get_req(VSCSIState *s)
109
{
110
    vscsi_req *req;
111
    int i;
112

    
113
    for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
114
        req = &s->reqs[i];
115
        if (!req->active) {
116
            memset(req, 0, sizeof(*req));
117
            req->qtag = i;
118
            req->active = 1;
119
            return req;
120
        }
121
    }
122
    return NULL;
123
}
124

    
125
static void vscsi_put_req(VSCSIState *s, vscsi_req *req)
126
{
127
    if (req->sreq != NULL) {
128
        scsi_req_unref(req->sreq);
129
    }
130
    req->sreq = NULL;
131
    req->active = 0;
132
}
133

    
134
static vscsi_req *vscsi_find_req(VSCSIState *s, SCSIRequest *req)
135
{
136
    uint32_t tag = req->tag;
137
    if (tag >= VSCSI_REQ_LIMIT || !s->reqs[tag].active) {
138
        return NULL;
139
    }
140
    return &s->reqs[tag];
141
}
142

    
143
static void vscsi_decode_id_lun(uint64_t srp_lun, int *id, int *lun)
144
{
145
    /* XXX Figure that one out properly ! This is crackpot */
146
    *id = (srp_lun >> 56) & 0x7f;
147
    *lun = (srp_lun >> 48) & 0xff;
148
}
149

    
150
static int vscsi_send_iu(VSCSIState *s, vscsi_req *req,
151
                         uint64_t length, uint8_t format)
152
{
153
    long rc, rc1;
154

    
155
    /* First copy the SRP */
156
    rc = spapr_tce_dma_write(&s->vdev, req->crq.s.IU_data_ptr,
157
                             &req->iu, length);
158
    if (rc) {
159
        fprintf(stderr, "vscsi_send_iu: DMA write failure !\n");
160
    }
161

    
162
    req->crq.s.valid = 0x80;
163
    req->crq.s.format = format;
164
    req->crq.s.reserved = 0x00;
165
    req->crq.s.timeout = cpu_to_be16(0x0000);
166
    req->crq.s.IU_length = cpu_to_be16(length);
167
    req->crq.s.IU_data_ptr = req->iu.srp.rsp.tag; /* right byte order */
168

    
169
    if (rc == 0) {
170
        req->crq.s.status = 0x99; /* Just needs to be non-zero */
171
    } else {
172
        req->crq.s.status = 0x00;
173
    }
174

    
175
    rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw);
176
    if (rc1) {
177
        fprintf(stderr, "vscsi_send_iu: Error sending response\n");
178
        return rc1;
179
    }
180

    
181
    return rc;
182
}
183

    
184
static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req,
185
                               uint8_t key, uint8_t asc, uint8_t ascq)
186
{
187
    req->senselen = SRP_RSP_SENSE_DATA_LEN;
188

    
189
    /* Valid bit and 'current errors' */
190
    req->sense[0] = (0x1 << 7 | 0x70);
191
    /* Sense key */
192
    req->sense[2] = key;
193
    /* Additional sense length */
194
    req->sense[7] = 0xa; /* 10 bytes */
195
    /* Additional sense code */
196
    req->sense[12] = asc;
197
    req->sense[13] = ascq;
198
}
199

    
200
static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req,
201
                          uint8_t status, int32_t res_in, int32_t res_out)
202
{
203
    union viosrp_iu *iu = &req->iu;
204
    uint64_t tag = iu->srp.rsp.tag;
205
    int total_len = sizeof(iu->srp.rsp);
206

    
207
    dprintf("VSCSI: Sending resp status: 0x%x, "
208
            "res_in: %d, res_out: %d\n", status, res_in, res_out);
209

    
210
    memset(iu, 0, sizeof(struct srp_rsp));
211
    iu->srp.rsp.opcode = SRP_RSP;
212
    iu->srp.rsp.req_lim_delta = cpu_to_be32(1);
213
    iu->srp.rsp.tag = tag;
214

    
215
    /* Handle residuals */
216
    if (res_in < 0) {
217
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
218
        res_in = -res_in;
219
    } else if (res_in) {
220
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
221
    }
222
    if (res_out < 0) {
223
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
224
        res_out = -res_out;
225
    } else if (res_out) {
226
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
227
    }
228
    iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in);
229
    iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out);
230

    
231
    /* We don't do response data */
232
    /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */
233
    iu->srp.rsp.resp_data_len = cpu_to_be32(0);
234

    
235
    /* Handle success vs. failure */
236
    iu->srp.rsp.status = status;
237
    if (status) {
238
        iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x04) >> 2;
239
        if (req->senselen) {
240
            req->iu.srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID;
241
            req->iu.srp.rsp.sense_data_len = cpu_to_be32(req->senselen);
242
            memcpy(req->iu.srp.rsp.data, req->sense, req->senselen);
243
            total_len += req->senselen;
244
        }
245
    } else {
246
        iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x02) >> 1;
247
    }
248

    
249
    vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT);
250
    return 0;
251
}
252

    
253
static inline void vscsi_swap_desc(struct srp_direct_buf *desc)
254
{
255
    desc->va = be64_to_cpu(desc->va);
256
    desc->len = be32_to_cpu(desc->len);
257
}
258

    
259
static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req,
260
                                 uint8_t *buf, uint32_t len)
261
{
262
    struct srp_direct_buf *md = req->cur_desc;
263
    uint32_t llen;
264
    int rc = 0;
265

    
266
    dprintf("VSCSI: direct segment 0x%x bytes, va=0x%llx desc len=0x%x\n",
267
            len, (unsigned long long)md->va, md->len);
268

    
269
    llen = MIN(len, md->len);
270
    if (llen) {
271
        if (req->writing) { /* writing = to device = reading from memory */
272
            rc = spapr_tce_dma_read(&s->vdev, md->va, buf, llen);
273
        } else {
274
            rc = spapr_tce_dma_write(&s->vdev, md->va, buf, llen);
275
        }
276
    }
277
    md->len -= llen;
278
    md->va += llen;
279

    
280
    if (rc) {
281
        return -1;
282
    }
283
    return llen;
284
}
285

    
286
static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req,
287
                                   uint8_t *buf, uint32_t len)
288
{
289
    struct srp_direct_buf *td = &req->ind_desc->table_desc;
290
    struct srp_direct_buf *md = req->cur_desc;
291
    int rc = 0;
292
    uint32_t llen, total = 0;
293

    
294
    dprintf("VSCSI: indirect segment 0x%x bytes, td va=0x%llx len=0x%x\n",
295
            len, (unsigned long long)td->va, td->len);
296

    
297
    /* While we have data ... */
298
    while (len) {
299
        /* If we have a descriptor but it's empty, go fetch a new one */
300
        if (md && md->len == 0) {
301
            /* More local available, use one */
302
            if (req->local_desc) {
303
                md = ++req->cur_desc;
304
                --req->local_desc;
305
                --req->total_desc;
306
                td->va += sizeof(struct srp_direct_buf);
307
            } else {
308
                md = req->cur_desc = NULL;
309
            }
310
        }
311
        /* No descriptor at hand, fetch one */
312
        if (!md) {
313
            if (!req->total_desc) {
314
                dprintf("VSCSI:   Out of descriptors !\n");
315
                break;
316
            }
317
            md = req->cur_desc = &req->ext_desc;
318
            dprintf("VSCSI:   Reading desc from 0x%llx\n",
319
                    (unsigned long long)td->va);
320
            rc = spapr_tce_dma_read(&s->vdev, td->va, md,
321
                                    sizeof(struct srp_direct_buf));
322
            if (rc) {
323
                dprintf("VSCSI: tce_dma_read -> %d reading ext_desc\n", rc);
324
                break;
325
            }
326
            vscsi_swap_desc(md);
327
            td->va += sizeof(struct srp_direct_buf);
328
            --req->total_desc;
329
        }
330
        dprintf("VSCSI:   [desc va=0x%llx,len=0x%x] remaining=0x%x\n",
331
                (unsigned long long)md->va, md->len, len);
332

    
333
        /* Perform transfer */
334
        llen = MIN(len, md->len);
335
        if (req->writing) { /* writing = to device = reading from memory */
336
            rc = spapr_tce_dma_read(&s->vdev, md->va, buf, llen);
337
        } else {
338
            rc = spapr_tce_dma_write(&s->vdev, md->va, buf, llen);
339
        }
340
        if (rc) {
341
            dprintf("VSCSI: tce_dma_r/w(%d) -> %d\n", req->writing, rc);
342
            break;
343
        }
344
        dprintf("VSCSI:     data: %02x %02x %02x %02x...\n",
345
                buf[0], buf[1], buf[2], buf[3]);
346

    
347
        len -= llen;
348
        buf += llen;
349
        total += llen;
350
        md->va += llen;
351
        md->len -= llen;
352
    }
353
    return rc ? -1 : total;
354
}
355

    
356
static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req,
357
                                   int writing, uint8_t *buf, uint32_t len)
358
{
359
    int err = 0;
360

    
361
    switch (req->dma_fmt) {
362
    case SRP_NO_DATA_DESC:
363
        dprintf("VSCSI: no data desc transfer, skipping 0x%x bytes\n", len);
364
        break;
365
    case SRP_DATA_DESC_DIRECT:
366
        err = vscsi_srp_direct_data(s, req, buf, len);
367
        break;
368
    case SRP_DATA_DESC_INDIRECT:
369
        err = vscsi_srp_indirect_data(s, req, buf, len);
370
        break;
371
    }
372
    return err;
373
}
374

    
375
/* Bits from linux srp */
376
static int data_out_desc_size(struct srp_cmd *cmd)
377
{
378
    int size = 0;
379
    uint8_t fmt = cmd->buf_fmt >> 4;
380

    
381
    switch (fmt) {
382
    case SRP_NO_DATA_DESC:
383
        break;
384
    case SRP_DATA_DESC_DIRECT:
385
        size = sizeof(struct srp_direct_buf);
386
        break;
387
    case SRP_DATA_DESC_INDIRECT:
388
        size = sizeof(struct srp_indirect_buf) +
389
            sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt;
390
        break;
391
    default:
392
        break;
393
    }
394
    return size;
395
}
396

    
397
static int vscsi_preprocess_desc(vscsi_req *req)
398
{
399
    struct srp_cmd *cmd = &req->iu.srp.cmd;
400
    int offset, i;
401

    
402
    offset = cmd->add_cdb_len & ~3;
403

    
404
    if (req->writing) {
405
        req->dma_fmt = cmd->buf_fmt >> 4;
406
    } else {
407
        offset += data_out_desc_size(cmd);
408
        req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1);
409
    }
410

    
411
    switch (req->dma_fmt) {
412
    case SRP_NO_DATA_DESC:
413
        break;
414
    case SRP_DATA_DESC_DIRECT:
415
        req->cur_desc = (struct srp_direct_buf *)(cmd->add_data + offset);
416
        req->total_desc = req->local_desc = 1;
417
        vscsi_swap_desc(req->cur_desc);
418
        dprintf("VSCSI: using direct RDMA %s, 0x%x bytes MD: 0x%llx\n",
419
                req->writing ? "write" : "read",
420
                req->cur_desc->len, (unsigned long long)req->cur_desc->va);
421
        break;
422
    case SRP_DATA_DESC_INDIRECT:
423
        req->ind_desc = (struct srp_indirect_buf *)(cmd->add_data + offset);
424
        vscsi_swap_desc(&req->ind_desc->table_desc);
425
        req->total_desc = req->ind_desc->table_desc.len /
426
            sizeof(struct srp_direct_buf);
427
        req->local_desc = req->writing ? cmd->data_out_desc_cnt :
428
            cmd->data_in_desc_cnt;
429
        for (i = 0; i < req->local_desc; i++) {
430
            vscsi_swap_desc(&req->ind_desc->desc_list[i]);
431
        }
432
        req->cur_desc = req->local_desc ? &req->ind_desc->desc_list[0] : NULL;
433
        dprintf("VSCSI: using indirect RDMA %s, 0x%x bytes %d descs "
434
                "(%d local) VA: 0x%llx\n",
435
                req->writing ? "read" : "write",
436
                be32_to_cpu(req->ind_desc->len),
437
                req->total_desc, req->local_desc,
438
                (unsigned long long)req->ind_desc->table_desc.va);
439
        break;
440
    default:
441
        fprintf(stderr,
442
                "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
443
        return -1;
444
    }
445

    
446
    return 0;
447
}
448

    
449
static void vscsi_send_request_sense(VSCSIState *s, vscsi_req *req)
450
{
451
    SCSIDevice *sdev = req->sdev;
452
    uint8_t *cdb = req->iu.srp.cmd.cdb;
453
    int n;
454

    
455
    cdb[0] = 3;
456
    cdb[1] = 0;
457
    cdb[2] = 0;
458
    cdb[3] = 0;
459
    cdb[4] = 96;
460
    cdb[5] = 0;
461
    req->sensing = 1;
462
    n = scsi_req_enqueue(req->sreq, cdb);
463
    dprintf("VSCSI: Queued request sense tag 0x%x\n", req->qtag);
464
    if (n < 0) {
465
        fprintf(stderr, "VSCSI: REQUEST_SENSE wants write data !?!?!?\n");
466
        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
467
        scsi_req_abort(req->sreq, CHECK_CONDITION);
468
        return;
469
    } else if (n == 0) {
470
        return;
471
    }
472
    sdev->info->read_data(req->sreq);
473
}
474

    
475
/* Callback to indicate that the SCSI layer has completed a transfer.  */
476
static void vscsi_command_complete(SCSIRequest *sreq, int reason, uint32_t arg)
477
{
478
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
479
    vscsi_req *req = vscsi_find_req(s, sreq);
480
    SCSIDevice *sdev;
481
    uint8_t *buf;
482
    int32_t res_in = 0, res_out = 0;
483
    int len, rc = 0;
484

    
485
    dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x arg=0x%x, req=%p\n",
486
            reason, sreq->tag, arg, req);
487
    if (req == NULL) {
488
        fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
489
        return;
490
    }
491
    sdev = req->sdev;
492

    
493
    if (req->sensing) {
494
        if (reason == SCSI_REASON_DONE) {
495
            dprintf("VSCSI: Sense done !\n");
496
            vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
497
            vscsi_put_req(s, req);
498
        } else {
499
            uint8_t *buf = sdev->info->get_buf(sreq);
500

    
501
            len = MIN(arg, SCSI_SENSE_BUF_SIZE);
502
            dprintf("VSCSI: Sense data, %d bytes:\n", len);
503
            dprintf("       %02x  %02x  %02x  %02x  %02x  %02x  %02x  %02x\n",
504
                    buf[0], buf[1], buf[2], buf[3],
505
                    buf[4], buf[5], buf[6], buf[7]);
506
            dprintf("       %02x  %02x  %02x  %02x  %02x  %02x  %02x  %02x\n",
507
                    buf[8], buf[9], buf[10], buf[11],
508
                    buf[12], buf[13], buf[14], buf[15]);
509
            memcpy(req->sense, buf, len);
510
            req->senselen = len;
511
            sdev->info->read_data(sreq);
512
        }
513
        return;
514
    }
515

    
516
    if (reason == SCSI_REASON_DONE) {
517
        dprintf("VSCSI: Command complete err=%d\n", arg);
518
        if (arg == 0) {
519
            /* We handle overflows, not underflows for normal commands,
520
             * but hopefully nobody cares
521
             */
522
            if (req->writing) {
523
                res_out = req->data_len;
524
            } else {
525
                res_in = req->data_len;
526
            }
527
            vscsi_send_rsp(s, req, 0, res_in, res_out);
528
        } else if (arg == CHECK_CONDITION) {
529
            dprintf("VSCSI: Got CHECK_CONDITION, requesting sense...\n");
530
            vscsi_send_request_sense(s, req);
531
            return;
532
        } else {
533
            vscsi_send_rsp(s, req, arg, 0, 0);
534
        }
535
        vscsi_put_req(s, req);
536
        return;
537
    }
538

    
539
    /* "arg" is how much we have read for reads and how much we want
540
     * to write for writes (ie, how much is to be DMA'd)
541
     */
542
    if (arg) {
543
        buf = sdev->info->get_buf(sreq);
544
        rc = vscsi_srp_transfer_data(s, req, req->writing, buf, arg);
545
    }
546
    if (rc < 0) {
547
        fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
548
        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
549
        scsi_req_abort(req->sreq, CHECK_CONDITION);
550
        return;
551
    }
552

    
553
    /* Start next chunk */
554
    req->data_len -= rc;
555
    if (req->writing) {
556
        sdev->info->write_data(sreq);
557
    } else {
558
        sdev->info->read_data(sreq);
559
    }
560
}
561

    
562
static void vscsi_request_cancelled(SCSIRequest *sreq)
563
{
564
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
565
    vscsi_req *req = vscsi_find_req(s, sreq);
566

    
567
    vscsi_put_req(s, req);
568
}
569

    
570
static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
571
{
572
    union viosrp_iu *iu = &req->iu;
573
    struct srp_login_rsp *rsp = &iu->srp.login_rsp;
574
    uint64_t tag = iu->srp.rsp.tag;
575

    
576
    dprintf("VSCSI: Got login, sendin response !\n");
577

    
578
    /* TODO handle case that requested size is wrong and
579
     * buffer format is wrong
580
     */
581
    memset(iu, 0, sizeof(struct srp_login_rsp));
582
    rsp->opcode = SRP_LOGIN_RSP;
583
    /* Don't advertise quite as many request as we support to
584
     * keep room for management stuff etc...
585
     */
586
    rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
587
    rsp->tag = tag;
588
    rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
589
    rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
590
    /* direct and indirect */
591
    rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
592

    
593
    vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
594
}
595

    
596
static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
597
{
598
    uint8_t *cdb = req->iu.srp.cmd.cdb;
599
    uint8_t resp_data[36];
600
    int rc, len, alen;
601

    
602
    /* We dont do EVPD. Also check that page_code is 0 */
603
    if ((cdb[1] & 0x01) || (cdb[1] & 0x01) || cdb[2] != 0) {
604
        /* Send INVALID FIELD IN CDB */
605
        vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
606
        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
607
        return;
608
    }
609
    alen = cdb[3];
610
    alen = (alen << 8) | cdb[4];
611
    len = MIN(alen, 36);
612

    
613
    /* Fake up inquiry using PQ=3 */
614
    memset(resp_data, 0, 36);
615
    resp_data[0] = 0x7f;   /* Not capable of supporting a device here */
616
    resp_data[2] = 0x06;   /* SPS-4 */
617
    resp_data[3] = 0x02;   /* Resp data format */
618
    resp_data[4] = 36 - 5; /* Additional length */
619
    resp_data[7] = 0x10;   /* Sync transfers */
620
    memcpy(&resp_data[16], "QEMU EMPTY      ", 16);
621
    memcpy(&resp_data[8], "QEMU    ", 8);
622

    
623
    req->writing = 0;
624
    vscsi_preprocess_desc(req);
625
    rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
626
    if (rc < 0) {
627
        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
628
        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
629
    } else {
630
        vscsi_send_rsp(s, req, 0, 36 - rc, 0);
631
    }
632
}
633

    
634
static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
635
{
636
    union srp_iu *srp = &req->iu.srp;
637
    SCSIDevice *sdev;
638
    int n, id, lun;
639

    
640
    vscsi_decode_id_lun(be64_to_cpu(srp->cmd.lun), &id, &lun);
641

    
642
    /* Qemu vs. linux issue with LUNs to be sorted out ... */
643
    sdev = (id < 8 && lun < 16) ? s->bus.devs[id] : NULL;
644
    if (!sdev) {
645
        dprintf("VSCSI: Command for id %d with no drive\n", id);
646
        if (srp->cmd.cdb[0] == INQUIRY) {
647
            vscsi_inquiry_no_target(s, req);
648
        } else {
649
            vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
650
            vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
651
        } return 1;
652
    }
653

    
654
    req->sdev = sdev;
655
    req->lun = lun;
656
    req->sreq = scsi_req_new(sdev, req->qtag, lun);
657
    n = scsi_req_enqueue(req->sreq, srp->cmd.cdb);
658

    
659
    dprintf("VSCSI: Queued command tag 0x%x CMD 0x%x ID %d LUN %d ret: %d\n",
660
            req->qtag, srp->cmd.cdb[0], id, lun, n);
661

    
662
    if (n) {
663
        /* Transfer direction must be set before preprocessing the
664
         * descriptors
665
         */
666
        req->writing = (n < 1);
667

    
668
        /* Preprocess RDMA descriptors */
669
        vscsi_preprocess_desc(req);
670
    }
671

    
672
    /* Get transfer direction and initiate transfer */
673
    if (n > 0) {
674
        req->data_len = n;
675
        sdev->info->read_data(req->sreq);
676
    } else if (n < 0) {
677
        req->data_len = -n;
678
        sdev->info->write_data(req->sreq);
679
    }
680
    /* Don't touch req here, it may have been recycled already */
681

    
682
    return 0;
683
}
684

    
685
static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
686
{
687
    union viosrp_iu *iu = &req->iu;
688
    int fn;
689

    
690
    fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
691
            iu->srp.tsk_mgmt.tsk_mgmt_func);
692

    
693
    switch (iu->srp.tsk_mgmt.tsk_mgmt_func) {
694
#if 0 /* We really don't deal with these for now */
695
    case SRP_TSK_ABORT_TASK:
696
        fn = ABORT_TASK;
697
        break;
698
    case SRP_TSK_ABORT_TASK_SET:
699
        fn = ABORT_TASK_SET;
700
        break;
701
    case SRP_TSK_CLEAR_TASK_SET:
702
        fn = CLEAR_TASK_SET;
703
        break;
704
    case SRP_TSK_LUN_RESET:
705
        fn = LOGICAL_UNIT_RESET;
706
        break;
707
    case SRP_TSK_CLEAR_ACA:
708
        fn = CLEAR_ACA;
709
        break;
710
#endif
711
    default:
712
        fn = 0;
713
    }
714
    if (fn) {
715
        /* XXX Send/Handle target task management */
716
        ;
717
    } else {
718
        vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x20, 0);
719
        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
720
    }
721
    return !fn;
722
}
723

    
724
static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
725
{
726
    union srp_iu *srp = &req->iu.srp;
727
    int done = 1;
728
    uint8_t opcode = srp->rsp.opcode;
729

    
730
    switch (opcode) {
731
    case SRP_LOGIN_REQ:
732
        vscsi_process_login(s, req);
733
        break;
734
    case SRP_TSK_MGMT:
735
        done = vscsi_process_tsk_mgmt(s, req);
736
        break;
737
    case SRP_CMD:
738
        done = vscsi_queue_cmd(s, req);
739
        break;
740
    case SRP_LOGIN_RSP:
741
    case SRP_I_LOGOUT:
742
    case SRP_T_LOGOUT:
743
    case SRP_RSP:
744
    case SRP_CRED_REQ:
745
    case SRP_CRED_RSP:
746
    case SRP_AER_REQ:
747
    case SRP_AER_RSP:
748
        fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
749
        break;
750
    default:
751
        fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
752
    }
753

    
754
    return done;
755
}
756

    
757
static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
758
{
759
    struct viosrp_adapter_info *sinfo;
760
    struct mad_adapter_info_data info;
761
    int rc;
762

    
763
    sinfo = &req->iu.mad.adapter_info;
764

    
765
#if 0 /* What for ? */
766
    rc = spapr_tce_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer),
767
                            &info, be16_to_cpu(sinfo->common.length));
768
    if (rc) {
769
        fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n");
770
    }
771
#endif
772
    memset(&info, 0, sizeof(info));
773
    strcpy(info.srp_version, SRP_VERSION);
774
    strncpy(info.partition_name, "qemu", sizeof("qemu"));
775
    info.partition_number = cpu_to_be32(0);
776
    info.mad_version = cpu_to_be32(1);
777
    info.os_type = cpu_to_be32(2);
778
    info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9);
779

    
780
    rc = spapr_tce_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
781
                             &info, be16_to_cpu(sinfo->common.length));
782
    if (rc)  {
783
        fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
784
    }
785

    
786
    sinfo->common.status = rc ? cpu_to_be32(1) : 0;
787

    
788
    return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
789
}
790

    
791
static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
792
{
793
    union mad_iu *mad = &req->iu.mad;
794

    
795
    switch (be32_to_cpu(mad->empty_iu.common.type)) {
796
    case VIOSRP_EMPTY_IU_TYPE:
797
        fprintf(stderr, "Unsupported EMPTY MAD IU\n");
798
        break;
799
    case VIOSRP_ERROR_LOG_TYPE:
800
        fprintf(stderr, "Unsupported ERROR LOG MAD IU\n");
801
        mad->error_log.common.status = cpu_to_be16(1);
802
        vscsi_send_iu(s, req, sizeof(mad->error_log), VIOSRP_MAD_FORMAT);
803
        break;
804
    case VIOSRP_ADAPTER_INFO_TYPE:
805
        vscsi_send_adapter_info(s, req);
806
        break;
807
    case VIOSRP_HOST_CONFIG_TYPE:
808
        mad->host_config.common.status = cpu_to_be16(1);
809
        vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
810
        break;
811
    default:
812
        fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
813
                be32_to_cpu(mad->empty_iu.common.type));
814
    }
815

    
816
    return 1;
817
}
818

    
819
static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
820
{
821
    vscsi_req *req;
822
    int done;
823

    
824
    req = vscsi_get_req(s);
825
    if (req == NULL) {
826
        fprintf(stderr, "VSCSI: Failed to get a request !\n");
827
        return;
828
    }
829

    
830
    /* We only support a limited number of descriptors, we know
831
     * the ibmvscsi driver uses up to 10 max, so it should fit
832
     * in our 256 bytes IUs. If not we'll have to increase the size
833
     * of the structure.
834
     */
835
    if (crq->s.IU_length > sizeof(union viosrp_iu)) {
836
        fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n",
837
                crq->s.IU_length);
838
        return;
839
    }
840

    
841
    /* XXX Handle failure differently ? */
842
    if (spapr_tce_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
843
                           crq->s.IU_length)) {
844
        fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
845
        qemu_free(req);
846
    }
847
    memcpy(&req->crq, crq, sizeof(vscsi_crq));
848

    
849
    if (crq->s.format == VIOSRP_MAD_FORMAT) {
850
        done = vscsi_handle_mad_req(s, req);
851
    } else {
852
        done = vscsi_handle_srp_req(s, req);
853
    }
854

    
855
    if (done) {
856
        vscsi_put_req(s, req);
857
    }
858
}
859

    
860

    
861
static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data)
862
{
863
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
864
    vscsi_crq crq;
865

    
866
    memcpy(crq.raw, crq_data, 16);
867
    crq.s.timeout = be16_to_cpu(crq.s.timeout);
868
    crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
869
    crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
870

    
871
    dprintf("VSCSI: do_crq %02x %02x ...\n", crq.raw[0], crq.raw[1]);
872

    
873
    switch (crq.s.valid) {
874
    case 0xc0: /* Init command/response */
875

    
876
        /* Respond to initialization request */
877
        if (crq.s.format == 0x01) {
878
            memset(crq.raw, 0, 16);
879
            crq.s.valid = 0xc0;
880
            crq.s.format = 0x02;
881
            spapr_vio_send_crq(dev, crq.raw);
882
        }
883

    
884
        /* Note that in hotplug cases, we might get a 0x02
885
         * as a result of us emitting the init request
886
         */
887

    
888
        break;
889
    case 0xff: /* Link event */
890

    
891
        /* Not handled for now */
892

    
893
        break;
894
    case 0x80: /* Payloads */
895
        switch (crq.s.format) {
896
        case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */
897
        case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */
898
            vscsi_got_payload(s, &crq);
899
            break;
900
        case VIOSRP_OS400_FORMAT:
901
        case VIOSRP_AIX_FORMAT:
902
        case VIOSRP_LINUX_FORMAT:
903
        case VIOSRP_INLINE_FORMAT:
904
            fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n",
905
                    crq.s.format);
906
            break;
907
        default:
908
            fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n",
909
                    crq.s.format);
910
        }
911
        break;
912
    default:
913
        fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n",
914
                crq.raw[0], crq.raw[1]);
915
    };
916

    
917
    return 0;
918
}
919

    
920
static const struct SCSIBusOps vscsi_scsi_ops = {
921
    .complete = vscsi_command_complete,
922
    .cancel = vscsi_request_cancelled
923
};
924

    
925
static int spapr_vscsi_init(VIOsPAPRDevice *dev)
926
{
927
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
928
    int i;
929

    
930
    dbg_vscsi_state = s;
931

    
932
    /* Initialize qemu request tags */
933
    memset(s->reqs, 0, sizeof(s->reqs));
934
    for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
935
        s->reqs[i].qtag = i;
936
    }
937

    
938
    dev->crq.SendFunc = vscsi_do_crq;
939

    
940
    scsi_bus_new(&s->bus, &dev->qdev, 1, VSCSI_REQ_LIMIT,
941
                 &vscsi_scsi_ops);
942
    if (!dev->qdev.hotplugged) {
943
        scsi_bus_legacy_handle_cmdline(&s->bus);
944
    }
945

    
946
    return 0;
947
}
948

    
949
void spapr_vscsi_create(VIOsPAPRBus *bus, uint32_t reg,
950
                        qemu_irq qirq, uint32_t vio_irq_num)
951
{
952
    DeviceState *dev;
953
    VIOsPAPRDevice *sdev;
954

    
955
    dev = qdev_create(&bus->bus, "spapr-vscsi");
956
    qdev_prop_set_uint32(dev, "reg", reg);
957

    
958
    qdev_init_nofail(dev);
959

    
960
    sdev = (VIOsPAPRDevice *)dev;
961
    sdev->qirq = qirq;
962
    sdev->vio_irq_num = vio_irq_num;
963
}
964

    
965
static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
966
{
967
    int ret;
968

    
969
    ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
970
    if (ret < 0) {
971
        return ret;
972
    }
973

    
974
    ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
975
    if (ret < 0) {
976
        return ret;
977
    }
978

    
979
    return 0;
980
}
981

    
982
static VIOsPAPRDeviceInfo spapr_vscsi = {
983
    .init = spapr_vscsi_init,
984
    .devnode = spapr_vscsi_devnode,
985
    .dt_name = "v-scsi",
986
    .dt_type = "vscsi",
987
    .dt_compatible = "IBM,v-scsi",
988
    .signal_mask = 0x00000001,
989
    .qdev.name = "spapr-vscsi",
990
    .qdev.size = sizeof(VSCSIState),
991
    .qdev.props = (Property[]) {
992
        DEFINE_PROP_UINT32("reg", VIOsPAPRDevice, reg, 0x2000),
993
        DEFINE_PROP_UINT32("dma-window", VIOsPAPRDevice,
994
                           rtce_window_size, 0x10000000),
995
        DEFINE_PROP_END_OF_LIST(),
996
    },
997
};
998

    
999
static void spapr_vscsi_register(void)
1000
{
1001
    spapr_vio_bus_register_withprop(&spapr_vscsi);
1002
}
1003
device_init(spapr_vscsi_register);