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