Statistics
| Branch: | Revision:

root / hw / spapr_vscsi.c @ c6df7102

History | View | Annotate | Download (29.4 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
    SCSIRequest             *sreq;
78
    uint32_t                qtag; /* qemu tag != srp tag */
79
    int                     lun;
80
    int                     active;
81
    long                    data_len;
82
    int                     writing;
83
    int                     sensing;
84
    int                     senselen;
85
    uint8_t                 sense[SCSI_SENSE_BUF_SIZE];
86

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

    
96

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

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

    
106

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

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

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

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

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

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

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

    
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 */
167

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

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

    
180
    return rc;
181
}
182

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

    
188
    /* Valid bit and 'current errors' */
189
    req->sense[0] = (0x1 << 7 | 0x70);
190
    /* Sense key */
191
    req->sense[2] = key;
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;
197
}
198

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

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

    
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;
213

    
214
    /* Handle residuals */
215
    if (res_in < 0) {
216
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER;
217
        res_in = -res_in;
218
    } else if (res_in) {
219
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER;
220
    }
221
    if (res_out < 0) {
222
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER;
223
        res_out = -res_out;
224
    } else if (res_out) {
225
        iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER;
226
    }
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);
229

    
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);
233

    
234
    /* Handle success vs. failure */
235
    iu->srp.rsp.status = status;
236
    if (status) {
237
        iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x04) >> 2;
238
        if (req->senselen) {
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;
243
        }
244
    } else {
245
        iu->srp.rsp.sol_not = (iu->srp.cmd.sol_not & 0x02) >> 1;
246
    }
247

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

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

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

    
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);
267

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

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

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

    
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);
295

    
296
    /* While we have data ... */
297
    while (len) {
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;
303
                --req->local_desc;
304
                --req->total_desc;
305
                td->va += sizeof(struct srp_direct_buf);
306
            } else {
307
                md = req->cur_desc = NULL;
308
            }
309
        }
310
        /* No descriptor at hand, fetch one */
311
        if (!md) {
312
            if (!req->total_desc) {
313
                dprintf("VSCSI:   Out of descriptors !\n");
314
                break;
315
            }
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));
321
            if (rc) {
322
                dprintf("VSCSI: tce_dma_read -> %d reading ext_desc\n", rc);
323
                break;
324
            }
325
            vscsi_swap_desc(md);
326
            td->va += sizeof(struct srp_direct_buf);
327
            --req->total_desc;
328
        }
329
        dprintf("VSCSI:   [desc va=0x%llx,len=0x%x] remaining=0x%x\n",
330
                (unsigned long long)md->va, md->len, len);
331

    
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);
336
        } else {
337
            rc = spapr_tce_dma_write(&s->vdev, md->va, buf, llen);
338
        }
339
        if (rc) {
340
            dprintf("VSCSI: tce_dma_r/w(%d) -> %d\n", req->writing, rc);
341
            break;
342
        }
343
        dprintf("VSCSI:     data: %02x %02x %02x %02x...\n",
344
                buf[0], buf[1], buf[2], buf[3]);
345

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

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

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

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

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

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

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

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

    
410
    switch (req->dma_fmt) {
411
    case SRP_NO_DATA_DESC:
412
        break;
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);
420
        break;
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]);
430
        }
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);
438
        break;
439
    default:
440
        fprintf(stderr,
441
                "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt);
442
        return -1;
443
    }
444

    
445
    return 0;
446
}
447

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

    
453
    n = scsi_req_get_sense(req->sreq, req->sense, sizeof(req->sense));
454
    if (n) {
455
        req->senselen = n;
456
        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
457
        vscsi_put_req(s, req);
458
        return;
459
    }
460

    
461
    dprintf("VSCSI: Got CHECK_CONDITION, requesting sense...\n");
462
    cdb[0] = 3;
463
    cdb[1] = 0;
464
    cdb[2] = 0;
465
    cdb[3] = 0;
466
    cdb[4] = 96;
467
    cdb[5] = 0;
468
    req->sensing = 1;
469
    n = scsi_req_enqueue(req->sreq, cdb);
470
    dprintf("VSCSI: Queued request sense tag 0x%x\n", req->qtag);
471
    if (n < 0) {
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);
475
        return;
476
    } else if (n == 0) {
477
        return;
478
    }
479
    scsi_req_continue(req->sreq);
480
}
481

    
482
/* Callback to indicate that the SCSI layer has completed a transfer.  */
483
static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t arg)
484
{
485
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
486
    vscsi_req *req = vscsi_find_req(s, sreq);
487
    uint8_t *buf;
488
    int len, rc = 0;
489

    
490
    dprintf("VSCSI: SCSI xfer complete tag=0x%x arg=0x%x, req=%p\n",
491
            sreq->tag, arg, req);
492
    if (req == NULL) {
493
        fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
494
        return;
495
    }
496

    
497
    if (req->sensing) {
498
        uint8_t *buf = scsi_req_get_buf(sreq);
499

    
500
        len = MIN(arg, 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);
509
        req->senselen = len;
510
        scsi_req_continue(req->sreq);
511
        return;
512
    }
513

    
514
    /* "arg" is how much we have read for reads and how much we want
515
     * to write for writes (ie, how much is to be DMA'd)
516
     */
517
    if (arg) {
518
        buf = scsi_req_get_buf(sreq);
519
        rc = vscsi_srp_transfer_data(s, req, req->writing, buf, arg);
520
    }
521
    if (rc < 0) {
522
        fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc);
523
        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
524
        scsi_req_abort(req->sreq, CHECK_CONDITION);
525
        return;
526
    }
527

    
528
    /* Start next chunk */
529
    req->data_len -= rc;
530
    scsi_req_continue(sreq);
531
}
532

    
533
/* Callback to indicate that the SCSI layer has completed a transfer.  */
534
static void vscsi_command_complete(SCSIRequest *sreq, uint32_t arg)
535
{
536
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
537
    vscsi_req *req = vscsi_find_req(s, sreq);
538
    int32_t res_in = 0, res_out = 0;
539

    
540
    dprintf("VSCSI: SCSI cmd complete, r=0x%x tag=0x%x arg=0x%x, req=%p\n",
541
            reason, sreq->tag, arg, req);
542
    if (req == NULL) {
543
        fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag);
544
        return;
545
    }
546

    
547
    if (!req->sensing && arg == CHECK_CONDITION) {
548
        vscsi_send_request_sense(s, req);
549
        return;
550
    }
551

    
552
    if (req->sensing) {
553
        dprintf("VSCSI: Sense done !\n");
554
        arg = CHECK_CONDITION;
555
    } else {
556
        dprintf("VSCSI: Command complete err=%d\n", arg);
557
        if (arg == 0) {
558
            /* We handle overflows, not underflows for normal commands,
559
             * but hopefully nobody cares
560
             */
561
            if (req->writing) {
562
                res_out = req->data_len;
563
            } else {
564
                res_in = req->data_len;
565
            }
566
        }
567
    }
568
    vscsi_send_rsp(s, req, 0, res_in, res_out);
569
    vscsi_put_req(s, req);
570
}
571

    
572
static void vscsi_request_cancelled(SCSIRequest *sreq)
573
{
574
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
575
    vscsi_req *req = vscsi_find_req(s, sreq);
576

    
577
    vscsi_put_req(s, req);
578
}
579

    
580
static void vscsi_process_login(VSCSIState *s, vscsi_req *req)
581
{
582
    union viosrp_iu *iu = &req->iu;
583
    struct srp_login_rsp *rsp = &iu->srp.login_rsp;
584
    uint64_t tag = iu->srp.rsp.tag;
585

    
586
    dprintf("VSCSI: Got login, sendin response !\n");
587

    
588
    /* TODO handle case that requested size is wrong and
589
     * buffer format is wrong
590
     */
591
    memset(iu, 0, sizeof(struct srp_login_rsp));
592
    rsp->opcode = SRP_LOGIN_RSP;
593
    /* Don't advertise quite as many request as we support to
594
     * keep room for management stuff etc...
595
     */
596
    rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2);
597
    rsp->tag = tag;
598
    rsp->max_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
599
    rsp->max_ti_iu_len = cpu_to_be32(sizeof(union srp_iu));
600
    /* direct and indirect */
601
    rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT);
602

    
603
    vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT);
604
}
605

    
606
static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
607
{
608
    uint8_t *cdb = req->iu.srp.cmd.cdb;
609
    uint8_t resp_data[36];
610
    int rc, len, alen;
611

    
612
    /* We dont do EVPD. Also check that page_code is 0 */
613
    if ((cdb[1] & 0x01) || (cdb[1] & 0x01) || cdb[2] != 0) {
614
        /* Send INVALID FIELD IN CDB */
615
        vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0);
616
        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
617
        return;
618
    }
619
    alen = cdb[3];
620
    alen = (alen << 8) | cdb[4];
621
    len = MIN(alen, 36);
622

    
623
    /* Fake up inquiry using PQ=3 */
624
    memset(resp_data, 0, 36);
625
    resp_data[0] = 0x7f;   /* Not capable of supporting a device here */
626
    resp_data[2] = 0x06;   /* SPS-4 */
627
    resp_data[3] = 0x02;   /* Resp data format */
628
    resp_data[4] = 36 - 5; /* Additional length */
629
    resp_data[7] = 0x10;   /* Sync transfers */
630
    memcpy(&resp_data[16], "QEMU EMPTY      ", 16);
631
    memcpy(&resp_data[8], "QEMU    ", 8);
632

    
633
    req->writing = 0;
634
    vscsi_preprocess_desc(req);
635
    rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
636
    if (rc < 0) {
637
        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
638
        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
639
    } else {
640
        vscsi_send_rsp(s, req, 0, 36 - rc, 0);
641
    }
642
}
643

    
644
static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
645
{
646
    union srp_iu *srp = &req->iu.srp;
647
    SCSIDevice *sdev;
648
    int n, id, lun;
649

    
650
    vscsi_decode_id_lun(be64_to_cpu(srp->cmd.lun), &id, &lun);
651

    
652
    /* Qemu vs. linux issue with LUNs to be sorted out ... */
653
    sdev = (id < 8 && lun < 16) ? s->bus.devs[id] : NULL;
654
    if (!sdev) {
655
        dprintf("VSCSI: Command for id %d with no drive\n", id);
656
        if (srp->cmd.cdb[0] == INQUIRY) {
657
            vscsi_inquiry_no_target(s, req);
658
        } else {
659
            vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00);
660
            vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
661
        } return 1;
662
    }
663

    
664
    req->lun = lun;
665
    req->sreq = scsi_req_new(sdev, req->qtag, lun);
666
    n = scsi_req_enqueue(req->sreq, srp->cmd.cdb);
667

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

    
671
    if (n) {
672
        /* Transfer direction must be set before preprocessing the
673
         * descriptors
674
         */
675
        req->writing = (n < 1);
676

    
677
        /* Preprocess RDMA descriptors */
678
        vscsi_preprocess_desc(req);
679

    
680
        /* Get transfer direction and initiate transfer */
681
        if (n > 0) {
682
            req->data_len = n;
683
        } else if (n < 0) {
684
            req->data_len = -n;
685
        }
686
        scsi_req_continue(req->sreq);
687
    }
688
    /* Don't touch req here, it may have been recycled already */
689

    
690
    return 0;
691
}
692

    
693
static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req)
694
{
695
    union viosrp_iu *iu = &req->iu;
696
    int fn;
697

    
698
    fprintf(stderr, "vscsi_process_tsk_mgmt %02x\n",
699
            iu->srp.tsk_mgmt.tsk_mgmt_func);
700

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

    
732
static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req)
733
{
734
    union srp_iu *srp = &req->iu.srp;
735
    int done = 1;
736
    uint8_t opcode = srp->rsp.opcode;
737

    
738
    switch (opcode) {
739
    case SRP_LOGIN_REQ:
740
        vscsi_process_login(s, req);
741
        break;
742
    case SRP_TSK_MGMT:
743
        done = vscsi_process_tsk_mgmt(s, req);
744
        break;
745
    case SRP_CMD:
746
        done = vscsi_queue_cmd(s, req);
747
        break;
748
    case SRP_LOGIN_RSP:
749
    case SRP_I_LOGOUT:
750
    case SRP_T_LOGOUT:
751
    case SRP_RSP:
752
    case SRP_CRED_REQ:
753
    case SRP_CRED_RSP:
754
    case SRP_AER_REQ:
755
    case SRP_AER_RSP:
756
        fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode);
757
        break;
758
    default:
759
        fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode);
760
    }
761

    
762
    return done;
763
}
764

    
765
static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
766
{
767
    struct viosrp_adapter_info *sinfo;
768
    struct mad_adapter_info_data info;
769
    int rc;
770

    
771
    sinfo = &req->iu.mad.adapter_info;
772

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

    
788
    rc = spapr_tce_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer),
789
                             &info, be16_to_cpu(sinfo->common.length));
790
    if (rc)  {
791
        fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n");
792
    }
793

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

    
796
    return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
797
}
798

    
799
static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
800
{
801
    union mad_iu *mad = &req->iu.mad;
802

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

    
824
    return 1;
825
}
826

    
827
static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq)
828
{
829
    vscsi_req *req;
830
    int done;
831

    
832
    req = vscsi_get_req(s);
833
    if (req == NULL) {
834
        fprintf(stderr, "VSCSI: Failed to get a request !\n");
835
        return;
836
    }
837

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

    
849
    /* XXX Handle failure differently ? */
850
    if (spapr_tce_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->iu,
851
                           crq->s.IU_length)) {
852
        fprintf(stderr, "vscsi_got_payload: DMA read failure !\n");
853
        qemu_free(req);
854
    }
855
    memcpy(&req->crq, crq, sizeof(vscsi_crq));
856

    
857
    if (crq->s.format == VIOSRP_MAD_FORMAT) {
858
        done = vscsi_handle_mad_req(s, req);
859
    } else {
860
        done = vscsi_handle_srp_req(s, req);
861
    }
862

    
863
    if (done) {
864
        vscsi_put_req(s, req);
865
    }
866
}
867

    
868

    
869
static int vscsi_do_crq(struct VIOsPAPRDevice *dev, uint8_t *crq_data)
870
{
871
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
872
    vscsi_crq crq;
873

    
874
    memcpy(crq.raw, crq_data, 16);
875
    crq.s.timeout = be16_to_cpu(crq.s.timeout);
876
    crq.s.IU_length = be16_to_cpu(crq.s.IU_length);
877
    crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr);
878

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

    
881
    switch (crq.s.valid) {
882
    case 0xc0: /* Init command/response */
883

    
884
        /* Respond to initialization request */
885
        if (crq.s.format == 0x01) {
886
            memset(crq.raw, 0, 16);
887
            crq.s.valid = 0xc0;
888
            crq.s.format = 0x02;
889
            spapr_vio_send_crq(dev, crq.raw);
890
        }
891

    
892
        /* Note that in hotplug cases, we might get a 0x02
893
         * as a result of us emitting the init request
894
         */
895

    
896
        break;
897
    case 0xff: /* Link event */
898

    
899
        /* Not handled for now */
900

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

    
925
    return 0;
926
}
927

    
928
static const struct SCSIBusOps vscsi_scsi_ops = {
929
    .transfer_data = vscsi_transfer_data,
930
    .complete = vscsi_command_complete,
931
    .cancel = vscsi_request_cancelled
932
};
933

    
934
static int spapr_vscsi_init(VIOsPAPRDevice *dev)
935
{
936
    VSCSIState *s = DO_UPCAST(VSCSIState, vdev, dev);
937
    int i;
938

    
939
    dbg_vscsi_state = s;
940

    
941
    /* Initialize qemu request tags */
942
    memset(s->reqs, 0, sizeof(s->reqs));
943
    for (i = 0; i < VSCSI_REQ_LIMIT; i++) {
944
        s->reqs[i].qtag = i;
945
    }
946

    
947
    dev->crq.SendFunc = vscsi_do_crq;
948

    
949
    scsi_bus_new(&s->bus, &dev->qdev, 1, VSCSI_REQ_LIMIT,
950
                 &vscsi_scsi_ops);
951
    if (!dev->qdev.hotplugged) {
952
        scsi_bus_legacy_handle_cmdline(&s->bus);
953
    }
954

    
955
    return 0;
956
}
957

    
958
void spapr_vscsi_create(VIOsPAPRBus *bus, uint32_t reg,
959
                        qemu_irq qirq, uint32_t vio_irq_num)
960
{
961
    DeviceState *dev;
962
    VIOsPAPRDevice *sdev;
963

    
964
    dev = qdev_create(&bus->bus, "spapr-vscsi");
965
    qdev_prop_set_uint32(dev, "reg", reg);
966

    
967
    qdev_init_nofail(dev);
968

    
969
    sdev = (VIOsPAPRDevice *)dev;
970
    sdev->qirq = qirq;
971
    sdev->vio_irq_num = vio_irq_num;
972
}
973

    
974
static int spapr_vscsi_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
975
{
976
    int ret;
977

    
978
    ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2);
979
    if (ret < 0) {
980
        return ret;
981
    }
982

    
983
    ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0);
984
    if (ret < 0) {
985
        return ret;
986
    }
987

    
988
    return 0;
989
}
990

    
991
static VIOsPAPRDeviceInfo spapr_vscsi = {
992
    .init = spapr_vscsi_init,
993
    .devnode = spapr_vscsi_devnode,
994
    .dt_name = "v-scsi",
995
    .dt_type = "vscsi",
996
    .dt_compatible = "IBM,v-scsi",
997
    .signal_mask = 0x00000001,
998
    .qdev.name = "spapr-vscsi",
999
    .qdev.size = sizeof(VSCSIState),
1000
    .qdev.props = (Property[]) {
1001
        DEFINE_PROP_UINT32("reg", VIOsPAPRDevice, reg, 0x2000),
1002
        DEFINE_PROP_UINT32("dma-window", VIOsPAPRDevice,
1003
                           rtce_window_size, 0x10000000),
1004
        DEFINE_PROP_END_OF_LIST(),
1005
    },
1006
};
1007

    
1008
static void spapr_vscsi_register(void)
1009
{
1010
    spapr_vio_bus_register_withprop(&spapr_vscsi);
1011
}
1012
device_init(spapr_vscsi_register);