]>
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 | */ | |
0b8fa32f | 34 | |
0d75590d | 35 | #include "qemu/osdep.h" |
0b8fa32f | 36 | #include "qemu/module.h" |
4771d756 | 37 | #include "cpu.h" |
0d09e41a | 38 | #include "hw/scsi/scsi.h" |
d6454270 | 39 | #include "migration/vmstate.h" |
08e2c9f1 | 40 | #include "scsi/constants.h" |
47b43a1f | 41 | #include "srp.h" |
0d09e41a PB |
42 | #include "hw/ppc/spapr.h" |
43 | #include "hw/ppc/spapr_vio.h" | |
a27bd6c7 | 44 | #include "hw/qdev-properties.h" |
47b43a1f | 45 | #include "viosrp.h" |
f19661c8 | 46 | #include "trace.h" |
6e270446 BH |
47 | |
48 | #include <libfdt.h> | |
49 | ||
6e270446 BH |
50 | /* |
51 | * Virtual SCSI device | |
52 | */ | |
53 | ||
54 | /* Random numbers */ | |
55 | #define VSCSI_MAX_SECTORS 4096 | |
56 | #define VSCSI_REQ_LIMIT 24 | |
57 | ||
13a54905 PMD |
58 | /* Maximum size of a IU payload */ |
59 | #define SRP_MAX_IU_DATA_LEN (SRP_MAX_IU_LEN - sizeof(union srp_iu)) | |
6e270446 BH |
60 | #define SRP_RSP_SENSE_DATA_LEN 18 |
61 | ||
3052f0d5 NW |
62 | #define SRP_REPORT_LUNS_WLUN 0xc10100000000000ULL |
63 | ||
6e270446 BH |
64 | typedef union vscsi_crq { |
65 | struct viosrp_crq s; | |
66 | uint8_t raw[16]; | |
67 | } vscsi_crq; | |
68 | ||
69 | typedef struct vscsi_req { | |
70 | vscsi_crq crq; | |
ff78b728 | 71 | uint8_t viosrp_iu_buf[SRP_MAX_IU_LEN]; |
6e270446 BH |
72 | |
73 | /* SCSI request tracking */ | |
5c6c0e51 | 74 | SCSIRequest *sreq; |
6e270446 | 75 | uint32_t qtag; /* qemu tag != srp tag */ |
8ca8a17c | 76 | bool active; |
8ca8a17c | 77 | bool writing; |
eda470e4 FZ |
78 | bool dma_error; |
79 | uint32_t data_len; | |
8ca8a17c | 80 | uint32_t senselen; |
6e270446 BH |
81 | uint8_t sense[SCSI_SENSE_BUF_SIZE]; |
82 | ||
83 | /* RDMA related bits */ | |
84 | uint8_t dma_fmt; | |
8ca8a17c AK |
85 | uint16_t local_desc; |
86 | uint16_t total_desc; | |
87 | uint16_t cdb_offset; | |
88 | uint16_t cur_desc_num; | |
89 | uint16_t cur_desc_offset; | |
6e270446 BH |
90 | } vscsi_req; |
91 | ||
fd506b4f DG |
92 | #define TYPE_VIO_SPAPR_VSCSI_DEVICE "spapr-vscsi" |
93 | #define VIO_SPAPR_VSCSI_DEVICE(obj) \ | |
94 | OBJECT_CHECK(VSCSIState, (obj), TYPE_VIO_SPAPR_VSCSI_DEVICE) | |
6e270446 BH |
95 | |
96 | typedef struct { | |
ce2918cb | 97 | SpaprVioDevice vdev; |
6e270446 BH |
98 | SCSIBus bus; |
99 | vscsi_req reqs[VSCSI_REQ_LIMIT]; | |
100 | } VSCSIState; | |
101 | ||
81e70549 PMD |
102 | static union viosrp_iu *req_iu(vscsi_req *req) |
103 | { | |
ff78b728 | 104 | return (union viosrp_iu *)req->viosrp_iu_buf; |
81e70549 PMD |
105 | } |
106 | ||
6e270446 BH |
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 | ||
eb37f146 AK |
124 | static struct vscsi_req *vscsi_find_req(VSCSIState *s, uint64_t srp_tag) |
125 | { | |
126 | vscsi_req *req; | |
127 | int i; | |
128 | ||
129 | for (i = 0; i < VSCSI_REQ_LIMIT; i++) { | |
130 | req = &s->reqs[i]; | |
81e70549 | 131 | if (req_iu(req)->srp.cmd.tag == srp_tag) { |
eb37f146 AK |
132 | return req; |
133 | } | |
134 | } | |
135 | return NULL; | |
136 | } | |
137 | ||
c5bf71a9 | 138 | static void vscsi_put_req(vscsi_req *req) |
6e270446 | 139 | { |
5c6c0e51 HR |
140 | if (req->sreq != NULL) { |
141 | scsi_req_unref(req->sreq); | |
142 | } | |
143 | req->sreq = NULL; | |
6e270446 BH |
144 | req->active = 0; |
145 | } | |
146 | ||
f48a7a6e | 147 | static SCSIDevice *vscsi_device_find(SCSIBus *bus, uint64_t srp_lun, int *lun) |
6e270446 | 148 | { |
7e0380b9 PB |
149 | int channel = 0, id = 0; |
150 | ||
151 | retry: | |
152 | switch (srp_lun >> 62) { | |
153 | case 0: | |
154 | if ((srp_lun >> 56) != 0) { | |
155 | channel = (srp_lun >> 56) & 0x3f; | |
156 | id = (srp_lun >> 48) & 0xff; | |
157 | srp_lun <<= 16; | |
158 | goto retry; | |
159 | } | |
160 | *lun = (srp_lun >> 48) & 0xff; | |
161 | break; | |
162 | ||
163 | case 1: | |
164 | *lun = (srp_lun >> 48) & 0x3fff; | |
165 | break; | |
166 | case 2: | |
167 | channel = (srp_lun >> 53) & 0x7; | |
168 | id = (srp_lun >> 56) & 0x3f; | |
169 | *lun = (srp_lun >> 48) & 0x1f; | |
170 | break; | |
171 | case 3: | |
172 | *lun = -1; | |
173 | return NULL; | |
174 | default: | |
175 | abort(); | |
176 | } | |
177 | ||
0d3545e7 | 178 | return scsi_device_find(bus, channel, id, *lun); |
6e270446 BH |
179 | } |
180 | ||
181 | static int vscsi_send_iu(VSCSIState *s, vscsi_req *req, | |
182 | uint64_t length, uint8_t format) | |
183 | { | |
184 | long rc, rc1; | |
185 | ||
13a54905 PMD |
186 | assert(length <= SRP_MAX_IU_LEN); |
187 | ||
6e270446 | 188 | /* First copy the SRP */ |
ad0ebb91 | 189 | rc = spapr_vio_dma_write(&s->vdev, req->crq.s.IU_data_ptr, |
ff78b728 | 190 | &req->viosrp_iu_buf, length); |
6e270446 BH |
191 | if (rc) { |
192 | fprintf(stderr, "vscsi_send_iu: DMA write failure !\n"); | |
193 | } | |
194 | ||
195 | req->crq.s.valid = 0x80; | |
196 | req->crq.s.format = format; | |
197 | req->crq.s.reserved = 0x00; | |
198 | req->crq.s.timeout = cpu_to_be16(0x0000); | |
199 | req->crq.s.IU_length = cpu_to_be16(length); | |
81e70549 | 200 | req->crq.s.IU_data_ptr = req_iu(req)->srp.rsp.tag; /* right byte order */ |
6e270446 BH |
201 | |
202 | if (rc == 0) { | |
22956a37 | 203 | req->crq.s.status = VIOSRP_OK; |
6e270446 | 204 | } else { |
22956a37 | 205 | req->crq.s.status = VIOSRP_ADAPTER_FAIL; |
6e270446 BH |
206 | } |
207 | ||
208 | rc1 = spapr_vio_send_crq(&s->vdev, req->crq.raw); | |
209 | if (rc1) { | |
210 | fprintf(stderr, "vscsi_send_iu: Error sending response\n"); | |
211 | return rc1; | |
212 | } | |
213 | ||
214 | return rc; | |
215 | } | |
216 | ||
217 | static void vscsi_makeup_sense(VSCSIState *s, vscsi_req *req, | |
218 | uint8_t key, uint8_t asc, uint8_t ascq) | |
219 | { | |
220 | req->senselen = SRP_RSP_SENSE_DATA_LEN; | |
221 | ||
222 | /* Valid bit and 'current errors' */ | |
223 | req->sense[0] = (0x1 << 7 | 0x70); | |
224 | /* Sense key */ | |
225 | req->sense[2] = key; | |
226 | /* Additional sense length */ | |
227 | req->sense[7] = 0xa; /* 10 bytes */ | |
228 | /* Additional sense code */ | |
229 | req->sense[12] = asc; | |
230 | req->sense[13] = ascq; | |
231 | } | |
232 | ||
233 | static int vscsi_send_rsp(VSCSIState *s, vscsi_req *req, | |
234 | uint8_t status, int32_t res_in, int32_t res_out) | |
235 | { | |
81e70549 | 236 | union viosrp_iu *iu = req_iu(req); |
6e270446 BH |
237 | uint64_t tag = iu->srp.rsp.tag; |
238 | int total_len = sizeof(iu->srp.rsp); | |
dbd94f8e | 239 | uint8_t sol_not = iu->srp.cmd.sol_not; |
6e270446 | 240 | |
f19661c8 | 241 | trace_spapr_vscsi_send_rsp(status, res_in, res_out); |
6e270446 BH |
242 | |
243 | memset(iu, 0, sizeof(struct srp_rsp)); | |
244 | iu->srp.rsp.opcode = SRP_RSP; | |
245 | iu->srp.rsp.req_lim_delta = cpu_to_be32(1); | |
246 | iu->srp.rsp.tag = tag; | |
247 | ||
248 | /* Handle residuals */ | |
249 | if (res_in < 0) { | |
250 | iu->srp.rsp.flags |= SRP_RSP_FLAG_DIUNDER; | |
251 | res_in = -res_in; | |
252 | } else if (res_in) { | |
253 | iu->srp.rsp.flags |= SRP_RSP_FLAG_DIOVER; | |
254 | } | |
255 | if (res_out < 0) { | |
256 | iu->srp.rsp.flags |= SRP_RSP_FLAG_DOUNDER; | |
257 | res_out = -res_out; | |
258 | } else if (res_out) { | |
259 | iu->srp.rsp.flags |= SRP_RSP_FLAG_DOOVER; | |
260 | } | |
261 | iu->srp.rsp.data_in_res_cnt = cpu_to_be32(res_in); | |
262 | iu->srp.rsp.data_out_res_cnt = cpu_to_be32(res_out); | |
263 | ||
264 | /* We don't do response data */ | |
265 | /* iu->srp.rsp.flags &= ~SRP_RSP_FLAG_RSPVALID; */ | |
266 | iu->srp.rsp.resp_data_len = cpu_to_be32(0); | |
267 | ||
268 | /* Handle success vs. failure */ | |
269 | iu->srp.rsp.status = status; | |
270 | if (status) { | |
dbd94f8e | 271 | iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2; |
6e270446 | 272 | if (req->senselen) { |
13a54905 PMD |
273 | int sense_data_len = MIN(req->senselen, SRP_MAX_IU_DATA_LEN); |
274 | ||
06109ab3 | 275 | iu->srp.rsp.flags |= SRP_RSP_FLAG_SNSVALID; |
13a54905 PMD |
276 | iu->srp.rsp.sense_data_len = cpu_to_be32(sense_data_len); |
277 | memcpy(iu->srp.rsp.data, req->sense, sense_data_len); | |
278 | total_len += sense_data_len; | |
6e270446 BH |
279 | } |
280 | } else { | |
dbd94f8e | 281 | iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1; |
6e270446 BH |
282 | } |
283 | ||
284 | vscsi_send_iu(s, req, total_len, VIOSRP_SRP_FORMAT); | |
285 | return 0; | |
286 | } | |
287 | ||
8ca8a17c | 288 | static inline struct srp_direct_buf vscsi_swap_desc(struct srp_direct_buf desc) |
6e270446 | 289 | { |
8ca8a17c AK |
290 | desc.va = be64_to_cpu(desc.va); |
291 | desc.len = be32_to_cpu(desc.len); | |
292 | return desc; | |
293 | } | |
294 | ||
295 | static int vscsi_fetch_desc(VSCSIState *s, struct vscsi_req *req, | |
296 | unsigned n, unsigned buf_offset, | |
297 | struct srp_direct_buf *ret) | |
298 | { | |
81e70549 | 299 | struct srp_cmd *cmd = &req_iu(req)->srp.cmd; |
8ca8a17c AK |
300 | |
301 | switch (req->dma_fmt) { | |
302 | case SRP_NO_DATA_DESC: { | |
f19661c8 | 303 | trace_spapr_vscsi_fetch_desc_no_data(); |
8ca8a17c AK |
304 | return 0; |
305 | } | |
306 | case SRP_DATA_DESC_DIRECT: { | |
307 | memcpy(ret, cmd->add_data + req->cdb_offset, sizeof(*ret)); | |
308 | assert(req->cur_desc_num == 0); | |
f19661c8 | 309 | trace_spapr_vscsi_fetch_desc_direct(); |
8ca8a17c AK |
310 | break; |
311 | } | |
312 | case SRP_DATA_DESC_INDIRECT: { | |
313 | struct srp_indirect_buf *tmp = (struct srp_indirect_buf *) | |
314 | (cmd->add_data + req->cdb_offset); | |
315 | if (n < req->local_desc) { | |
316 | *ret = tmp->desc_list[n]; | |
f19661c8 LV |
317 | trace_spapr_vscsi_fetch_desc_indirect(req->qtag, n, |
318 | req->local_desc); | |
8ca8a17c AK |
319 | } else if (n < req->total_desc) { |
320 | int rc; | |
321 | struct srp_direct_buf tbl_desc = vscsi_swap_desc(tmp->table_desc); | |
322 | unsigned desc_offset = n * sizeof(struct srp_direct_buf); | |
323 | ||
324 | if (desc_offset >= tbl_desc.len) { | |
f19661c8 | 325 | trace_spapr_vscsi_fetch_desc_out_of_range(n, desc_offset); |
8ca8a17c AK |
326 | return -1; |
327 | } | |
328 | rc = spapr_vio_dma_read(&s->vdev, tbl_desc.va + desc_offset, | |
329 | ret, sizeof(struct srp_direct_buf)); | |
330 | if (rc) { | |
f19661c8 | 331 | trace_spapr_vscsi_fetch_desc_dma_read_error(rc); |
8ca8a17c AK |
332 | return -1; |
333 | } | |
f19661c8 LV |
334 | trace_spapr_vscsi_fetch_desc_indirect_seg_ext(req->qtag, n, |
335 | req->total_desc, | |
336 | tbl_desc.va, | |
337 | tbl_desc.len); | |
8ca8a17c | 338 | } else { |
f19661c8 | 339 | trace_spapr_vscsi_fetch_desc_out_of_desc(); |
8ca8a17c AK |
340 | return 0; |
341 | } | |
342 | break; | |
343 | } | |
344 | default: | |
345 | fprintf(stderr, "VSCSI: Unknown format %x\n", req->dma_fmt); | |
346 | return -1; | |
347 | } | |
348 | ||
349 | *ret = vscsi_swap_desc(*ret); | |
350 | if (buf_offset > ret->len) { | |
f19661c8 LV |
351 | trace_spapr_vscsi_fetch_desc_out_of_desc_boundary(buf_offset, |
352 | req->cur_desc_num, | |
353 | ret->len); | |
8ca8a17c AK |
354 | return -1; |
355 | } | |
356 | ret->va += buf_offset; | |
357 | ret->len -= buf_offset; | |
358 | ||
f19661c8 LV |
359 | trace_spapr_vscsi_fetch_desc_done(req->cur_desc_num, req->cur_desc_offset, |
360 | ret->va, ret->len); | |
8ca8a17c AK |
361 | |
362 | return ret->len ? 1 : 0; | |
6e270446 BH |
363 | } |
364 | ||
365 | static int vscsi_srp_direct_data(VSCSIState *s, vscsi_req *req, | |
366 | uint8_t *buf, uint32_t len) | |
367 | { | |
8ca8a17c | 368 | struct srp_direct_buf md; |
6e270446 | 369 | uint32_t llen; |
8804f57b | 370 | int rc = 0; |
6e270446 | 371 | |
8ca8a17c AK |
372 | rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md); |
373 | if (rc < 0) { | |
374 | return -1; | |
375 | } else if (rc == 0) { | |
376 | return 0; | |
377 | } | |
6e270446 | 378 | |
8ca8a17c | 379 | llen = MIN(len, md.len); |
6e270446 BH |
380 | if (llen) { |
381 | if (req->writing) { /* writing = to device = reading from memory */ | |
8ca8a17c | 382 | rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen); |
6e270446 | 383 | } else { |
8ca8a17c | 384 | rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen); |
6e270446 BH |
385 | } |
386 | } | |
6e270446 BH |
387 | |
388 | if (rc) { | |
389 | return -1; | |
390 | } | |
8ca8a17c AK |
391 | req->cur_desc_offset += llen; |
392 | ||
6e270446 BH |
393 | return llen; |
394 | } | |
395 | ||
396 | static int vscsi_srp_indirect_data(VSCSIState *s, vscsi_req *req, | |
397 | uint8_t *buf, uint32_t len) | |
398 | { | |
8ca8a17c | 399 | struct srp_direct_buf md; |
6e270446 BH |
400 | int rc = 0; |
401 | uint32_t llen, total = 0; | |
402 | ||
f19661c8 | 403 | trace_spapr_vscsi_srp_indirect_data(len); |
6e270446 BH |
404 | |
405 | /* While we have data ... */ | |
406 | while (len) { | |
8ca8a17c AK |
407 | rc = vscsi_fetch_desc(s, req, req->cur_desc_num, req->cur_desc_offset, &md); |
408 | if (rc < 0) { | |
409 | return -1; | |
410 | } else if (rc == 0) { | |
411 | break; | |
6e270446 | 412 | } |
6e270446 BH |
413 | |
414 | /* Perform transfer */ | |
8ca8a17c | 415 | llen = MIN(len, md.len); |
6e270446 | 416 | if (req->writing) { /* writing = to device = reading from memory */ |
8ca8a17c | 417 | rc = spapr_vio_dma_read(&s->vdev, md.va, buf, llen); |
6e270446 | 418 | } else { |
8ca8a17c | 419 | rc = spapr_vio_dma_write(&s->vdev, md.va, buf, llen); |
6e270446 BH |
420 | } |
421 | if (rc) { | |
f19661c8 | 422 | trace_spapr_vscsi_srp_indirect_data_rw(req->writing, rc); |
6e270446 BH |
423 | break; |
424 | } | |
f19661c8 | 425 | trace_spapr_vscsi_srp_indirect_data_buf(buf[0], buf[1], buf[2], buf[3]); |
6e270446 BH |
426 | |
427 | len -= llen; | |
428 | buf += llen; | |
8ca8a17c | 429 | |
6e270446 | 430 | total += llen; |
8ca8a17c AK |
431 | |
432 | /* Update current position in the current descriptor */ | |
433 | req->cur_desc_offset += llen; | |
434 | if (md.len == llen) { | |
435 | /* Go to the next descriptor if the current one finished */ | |
436 | ++req->cur_desc_num; | |
437 | req->cur_desc_offset = 0; | |
438 | } | |
6e270446 | 439 | } |
8ca8a17c | 440 | |
6e270446 BH |
441 | return rc ? -1 : total; |
442 | } | |
443 | ||
444 | static int vscsi_srp_transfer_data(VSCSIState *s, vscsi_req *req, | |
445 | int writing, uint8_t *buf, uint32_t len) | |
446 | { | |
447 | int err = 0; | |
448 | ||
449 | switch (req->dma_fmt) { | |
450 | case SRP_NO_DATA_DESC: | |
f19661c8 | 451 | trace_spapr_vscsi_srp_transfer_data(len); |
6e270446 BH |
452 | break; |
453 | case SRP_DATA_DESC_DIRECT: | |
454 | err = vscsi_srp_direct_data(s, req, buf, len); | |
455 | break; | |
456 | case SRP_DATA_DESC_INDIRECT: | |
457 | err = vscsi_srp_indirect_data(s, req, buf, len); | |
458 | break; | |
459 | } | |
460 | return err; | |
461 | } | |
462 | ||
463 | /* Bits from linux srp */ | |
464 | static int data_out_desc_size(struct srp_cmd *cmd) | |
465 | { | |
466 | int size = 0; | |
467 | uint8_t fmt = cmd->buf_fmt >> 4; | |
468 | ||
469 | switch (fmt) { | |
470 | case SRP_NO_DATA_DESC: | |
471 | break; | |
472 | case SRP_DATA_DESC_DIRECT: | |
473 | size = sizeof(struct srp_direct_buf); | |
474 | break; | |
475 | case SRP_DATA_DESC_INDIRECT: | |
476 | size = sizeof(struct srp_indirect_buf) + | |
477 | sizeof(struct srp_direct_buf)*cmd->data_out_desc_cnt; | |
478 | break; | |
479 | default: | |
480 | break; | |
481 | } | |
482 | return size; | |
483 | } | |
484 | ||
485 | static int vscsi_preprocess_desc(vscsi_req *req) | |
486 | { | |
81e70549 | 487 | struct srp_cmd *cmd = &req_iu(req)->srp.cmd; |
6e270446 | 488 | |
8ca8a17c | 489 | req->cdb_offset = cmd->add_cdb_len & ~3; |
6e270446 BH |
490 | |
491 | if (req->writing) { | |
492 | req->dma_fmt = cmd->buf_fmt >> 4; | |
493 | } else { | |
8ca8a17c | 494 | req->cdb_offset += data_out_desc_size(cmd); |
6e270446 BH |
495 | req->dma_fmt = cmd->buf_fmt & ((1U << 4) - 1); |
496 | } | |
497 | ||
498 | switch (req->dma_fmt) { | |
499 | case SRP_NO_DATA_DESC: | |
500 | break; | |
501 | case SRP_DATA_DESC_DIRECT: | |
6e270446 | 502 | req->total_desc = req->local_desc = 1; |
6e270446 | 503 | break; |
8ca8a17c AK |
504 | case SRP_DATA_DESC_INDIRECT: { |
505 | struct srp_indirect_buf *ind_tmp = (struct srp_indirect_buf *) | |
506 | (cmd->add_data + req->cdb_offset); | |
507 | ||
508 | req->total_desc = be32_to_cpu(ind_tmp->table_desc.len) / | |
509 | sizeof(struct srp_direct_buf); | |
6e270446 | 510 | req->local_desc = req->writing ? cmd->data_out_desc_cnt : |
8ca8a17c | 511 | cmd->data_in_desc_cnt; |
6e270446 | 512 | break; |
8ca8a17c | 513 | } |
6e270446 BH |
514 | default: |
515 | fprintf(stderr, | |
516 | "vscsi_preprocess_desc: Unknown format %x\n", req->dma_fmt); | |
517 | return -1; | |
518 | } | |
519 | ||
520 | return 0; | |
521 | } | |
522 | ||
6e270446 | 523 | /* Callback to indicate that the SCSI layer has completed a transfer. */ |
aba1f023 | 524 | static void vscsi_transfer_data(SCSIRequest *sreq, uint32_t len) |
6e270446 | 525 | { |
fd506b4f | 526 | VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent); |
c5bf71a9 | 527 | vscsi_req *req = sreq->hba_private; |
6e270446 | 528 | uint8_t *buf; |
aba1f023 | 529 | int rc = 0; |
6e270446 | 530 | |
f19661c8 | 531 | trace_spapr_vscsi_transfer_data(sreq->tag, len, req); |
6e270446 | 532 | if (req == NULL) { |
5c6c0e51 | 533 | fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag); |
6e270446 BH |
534 | return; |
535 | } | |
6e270446 | 536 | |
aba1f023 | 537 | if (len) { |
0c34459b | 538 | buf = scsi_req_get_buf(sreq); |
aba1f023 | 539 | rc = vscsi_srp_transfer_data(s, req, req->writing, buf, len); |
6e270446 BH |
540 | } |
541 | if (rc < 0) { | |
542 | fprintf(stderr, "VSCSI: RDMA error rc=%d!\n", rc); | |
eda470e4 FZ |
543 | req->dma_error = true; |
544 | scsi_req_cancel(req->sreq); | |
6e270446 BH |
545 | return; |
546 | } | |
547 | ||
548 | /* Start next chunk */ | |
549 | req->data_len -= rc; | |
ad3376cc | 550 | scsi_req_continue(sreq); |
6e270446 BH |
551 | } |
552 | ||
c6df7102 | 553 | /* Callback to indicate that the SCSI layer has completed a transfer. */ |
01e95455 | 554 | static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, size_t resid) |
c6df7102 | 555 | { |
fd506b4f | 556 | VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent); |
c5bf71a9 | 557 | vscsi_req *req = sreq->hba_private; |
c6df7102 PB |
558 | int32_t res_in = 0, res_out = 0; |
559 | ||
f19661c8 | 560 | trace_spapr_vscsi_command_complete(sreq->tag, status, req); |
c6df7102 PB |
561 | if (req == NULL) { |
562 | fprintf(stderr, "VSCSI: Can't find request for tag 0x%x\n", sreq->tag); | |
563 | return; | |
564 | } | |
565 | ||
05751d3f PB |
566 | if (status == CHECK_CONDITION) { |
567 | req->senselen = scsi_req_get_sense(req->sreq, req->sense, | |
568 | sizeof(req->sense)); | |
f19661c8 | 569 | trace_spapr_vscsi_command_complete_sense_data1(req->senselen, |
05751d3f PB |
570 | req->sense[0], req->sense[1], req->sense[2], req->sense[3], |
571 | req->sense[4], req->sense[5], req->sense[6], req->sense[7]); | |
f19661c8 | 572 | trace_spapr_vscsi_command_complete_sense_data2( |
05751d3f PB |
573 | req->sense[8], req->sense[9], req->sense[10], req->sense[11], |
574 | req->sense[12], req->sense[13], req->sense[14], req->sense[15]); | |
c6df7102 PB |
575 | } |
576 | ||
f19661c8 | 577 | trace_spapr_vscsi_command_complete_status(status); |
05751d3f PB |
578 | if (status == 0) { |
579 | /* We handle overflows, not underflows for normal commands, | |
580 | * but hopefully nobody cares | |
581 | */ | |
582 | if (req->writing) { | |
583 | res_out = req->data_len; | |
584 | } else { | |
585 | res_in = req->data_len; | |
c6df7102 PB |
586 | } |
587 | } | |
05751d3f | 588 | vscsi_send_rsp(s, req, status, res_in, res_out); |
c5bf71a9 | 589 | vscsi_put_req(req); |
c6df7102 PB |
590 | } |
591 | ||
94d3f98a PB |
592 | static void vscsi_request_cancelled(SCSIRequest *sreq) |
593 | { | |
c5bf71a9 | 594 | vscsi_req *req = sreq->hba_private; |
94d3f98a | 595 | |
eda470e4 FZ |
596 | if (req->dma_error) { |
597 | VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(sreq->bus->qbus.parent); | |
598 | ||
599 | vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0); | |
600 | vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); | |
601 | } | |
c5bf71a9 | 602 | vscsi_put_req(req); |
94d3f98a PB |
603 | } |
604 | ||
1168ec7d DG |
605 | static const VMStateDescription vmstate_spapr_vscsi_req = { |
606 | .name = "spapr_vscsi_req", | |
607 | .version_id = 1, | |
608 | .minimum_version_id = 1, | |
3aff6c2f | 609 | .fields = (VMStateField[]) { |
1168ec7d | 610 | VMSTATE_BUFFER(crq.raw, vscsi_req), |
ff78b728 | 611 | VMSTATE_BUFFER(viosrp_iu_buf, vscsi_req), |
1168ec7d DG |
612 | VMSTATE_UINT32(qtag, vscsi_req), |
613 | VMSTATE_BOOL(active, vscsi_req), | |
614 | VMSTATE_UINT32(data_len, vscsi_req), | |
615 | VMSTATE_BOOL(writing, vscsi_req), | |
616 | VMSTATE_UINT32(senselen, vscsi_req), | |
617 | VMSTATE_BUFFER(sense, vscsi_req), | |
618 | VMSTATE_UINT8(dma_fmt, vscsi_req), | |
619 | VMSTATE_UINT16(local_desc, vscsi_req), | |
620 | VMSTATE_UINT16(total_desc, vscsi_req), | |
621 | VMSTATE_UINT16(cdb_offset, vscsi_req), | |
622 | /*Restart SCSI request from the beginning for now */ | |
623 | /*VMSTATE_UINT16(cur_desc_num, vscsi_req), | |
624 | VMSTATE_UINT16(cur_desc_offset, vscsi_req),*/ | |
625 | VMSTATE_END_OF_LIST() | |
626 | }, | |
627 | }; | |
628 | ||
629 | static void vscsi_save_request(QEMUFile *f, SCSIRequest *sreq) | |
630 | { | |
631 | vscsi_req *req = sreq->hba_private; | |
632 | assert(req->active); | |
633 | ||
8118f095 | 634 | vmstate_save_state(f, &vmstate_spapr_vscsi_req, req, NULL); |
1168ec7d | 635 | |
f19661c8 LV |
636 | trace_spapr_vscsi_save_request(req->qtag, req->cur_desc_num, |
637 | req->cur_desc_offset); | |
1168ec7d DG |
638 | } |
639 | ||
640 | static void *vscsi_load_request(QEMUFile *f, SCSIRequest *sreq) | |
641 | { | |
642 | SCSIBus *bus = sreq->bus; | |
643 | VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(bus->qbus.parent); | |
644 | vscsi_req *req; | |
645 | int rc; | |
646 | ||
647 | assert(sreq->tag < VSCSI_REQ_LIMIT); | |
648 | req = &s->reqs[sreq->tag]; | |
649 | assert(!req->active); | |
650 | ||
651 | memset(req, 0, sizeof(*req)); | |
652 | rc = vmstate_load_state(f, &vmstate_spapr_vscsi_req, req, 1); | |
653 | if (rc) { | |
654 | fprintf(stderr, "VSCSI: failed loading request tag#%u\n", sreq->tag); | |
655 | return NULL; | |
656 | } | |
657 | assert(req->active); | |
658 | ||
659 | req->sreq = scsi_req_ref(sreq); | |
660 | ||
f19661c8 LV |
661 | trace_spapr_vscsi_load_request(req->qtag, req->cur_desc_num, |
662 | req->cur_desc_offset); | |
1168ec7d DG |
663 | |
664 | return req; | |
665 | } | |
666 | ||
6e270446 BH |
667 | static void vscsi_process_login(VSCSIState *s, vscsi_req *req) |
668 | { | |
81e70549 | 669 | union viosrp_iu *iu = req_iu(req); |
6e270446 BH |
670 | struct srp_login_rsp *rsp = &iu->srp.login_rsp; |
671 | uint64_t tag = iu->srp.rsp.tag; | |
672 | ||
196fe237 | 673 | trace_spapr_vscsi_process_login(); |
6e270446 BH |
674 | |
675 | /* TODO handle case that requested size is wrong and | |
676 | * buffer format is wrong | |
677 | */ | |
678 | memset(iu, 0, sizeof(struct srp_login_rsp)); | |
679 | rsp->opcode = SRP_LOGIN_RSP; | |
680 | /* Don't advertise quite as many request as we support to | |
681 | * keep room for management stuff etc... | |
682 | */ | |
683 | rsp->req_lim_delta = cpu_to_be32(VSCSI_REQ_LIMIT-2); | |
684 | rsp->tag = tag; | |
0dc55698 PMD |
685 | rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN); |
686 | rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN); | |
6e270446 BH |
687 | /* direct and indirect */ |
688 | rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT); | |
689 | ||
690 | vscsi_send_iu(s, req, sizeof(*rsp), VIOSRP_SRP_FORMAT); | |
691 | } | |
692 | ||
693 | static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) | |
694 | { | |
81e70549 | 695 | uint8_t *cdb = req_iu(req)->srp.cmd.cdb; |
6e270446 BH |
696 | uint8_t resp_data[36]; |
697 | int rc, len, alen; | |
698 | ||
cb8d4c8f | 699 | /* We don't do EVPD. Also check that page_code is 0 */ |
ec8929a5 | 700 | if ((cdb[1] & 0x01) || cdb[2] != 0) { |
6e270446 BH |
701 | /* Send INVALID FIELD IN CDB */ |
702 | vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0); | |
703 | vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); | |
704 | return; | |
705 | } | |
706 | alen = cdb[3]; | |
707 | alen = (alen << 8) | cdb[4]; | |
708 | len = MIN(alen, 36); | |
709 | ||
710 | /* Fake up inquiry using PQ=3 */ | |
711 | memset(resp_data, 0, 36); | |
712 | resp_data[0] = 0x7f; /* Not capable of supporting a device here */ | |
713 | resp_data[2] = 0x06; /* SPS-4 */ | |
714 | resp_data[3] = 0x02; /* Resp data format */ | |
715 | resp_data[4] = 36 - 5; /* Additional length */ | |
716 | resp_data[7] = 0x10; /* Sync transfers */ | |
717 | memcpy(&resp_data[16], "QEMU EMPTY ", 16); | |
718 | memcpy(&resp_data[8], "QEMU ", 8); | |
719 | ||
720 | req->writing = 0; | |
721 | vscsi_preprocess_desc(req); | |
722 | rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len); | |
723 | if (rc < 0) { | |
724 | vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0); | |
725 | vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); | |
726 | } else { | |
727 | vscsi_send_rsp(s, req, 0, 36 - rc, 0); | |
728 | } | |
729 | } | |
730 | ||
3052f0d5 NW |
731 | static void vscsi_report_luns(VSCSIState *s, vscsi_req *req) |
732 | { | |
733 | BusChild *kid; | |
734 | int i, len, n, rc; | |
735 | uint8_t *resp_data; | |
736 | bool found_lun0; | |
737 | ||
738 | n = 0; | |
739 | found_lun0 = false; | |
740 | QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) { | |
741 | SCSIDevice *dev = SCSI_DEVICE(kid->child); | |
742 | ||
743 | n += 8; | |
744 | if (dev->channel == 0 && dev->id == 0 && dev->lun == 0) { | |
745 | found_lun0 = true; | |
746 | } | |
747 | } | |
748 | if (!found_lun0) { | |
749 | n += 8; | |
750 | } | |
751 | len = n+8; | |
752 | ||
753 | resp_data = g_malloc0(len); | |
3052f0d5 NW |
754 | stl_be_p(resp_data, n); |
755 | i = found_lun0 ? 8 : 16; | |
756 | QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) { | |
757 | DeviceState *qdev = kid->child; | |
758 | SCSIDevice *dev = SCSI_DEVICE(qdev); | |
759 | ||
760 | if (dev->id == 0 && dev->channel == 0) { | |
761 | resp_data[i] = 0; /* Use simple LUN for 0 (SAM5 4.7.7.1) */ | |
762 | } else { | |
763 | resp_data[i] = (2 << 6); /* Otherwise LUN addressing (4.7.7.4) */ | |
764 | } | |
765 | resp_data[i] |= dev->id; | |
766 | resp_data[i+1] = (dev->channel << 5); | |
767 | resp_data[i+1] |= dev->lun; | |
768 | i += 8; | |
769 | } | |
770 | ||
771 | vscsi_preprocess_desc(req); | |
772 | rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len); | |
773 | g_free(resp_data); | |
774 | if (rc < 0) { | |
775 | vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0); | |
776 | vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); | |
777 | } else { | |
778 | vscsi_send_rsp(s, req, 0, len - rc, 0); | |
779 | } | |
780 | } | |
781 | ||
6e270446 BH |
782 | static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req) |
783 | { | |
81e70549 | 784 | union srp_iu *srp = &req_iu(req)->srp; |
6e270446 | 785 | SCSIDevice *sdev; |
f48a7a6e | 786 | int n, lun; |
6e270446 | 787 | |
3052f0d5 NW |
788 | if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN) |
789 | && srp->cmd.cdb[0] == REPORT_LUNS) { | |
790 | vscsi_report_luns(s, req); | |
791 | return 0; | |
792 | } | |
793 | ||
f48a7a6e | 794 | sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun); |
6e270446 | 795 | if (!sdev) { |
f19661c8 | 796 | trace_spapr_vscsi_queue_cmd_no_drive(be64_to_cpu(srp->cmd.lun)); |
6e270446 BH |
797 | if (srp->cmd.cdb[0] == INQUIRY) { |
798 | vscsi_inquiry_no_target(s, req); | |
799 | } else { | |
800 | vscsi_makeup_sense(s, req, ILLEGAL_REQUEST, 0x24, 0x00); | |
801 | vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0); | |
802 | } return 1; | |
803 | } | |
804 | ||
c39ce112 PB |
805 | req->sreq = scsi_req_new(sdev, req->qtag, lun, srp->cmd.cdb, req); |
806 | n = scsi_req_enqueue(req->sreq); | |
6e270446 | 807 | |
f19661c8 LV |
808 | trace_spapr_vscsi_queue_cmd(req->qtag, srp->cmd.cdb[0], |
809 | scsi_command_name(srp->cmd.cdb[0]), lun, n); | |
6e270446 BH |
810 | |
811 | if (n) { | |
812 | /* Transfer direction must be set before preprocessing the | |
813 | * descriptors | |
814 | */ | |
815 | req->writing = (n < 1); | |
816 | ||
817 | /* Preprocess RDMA descriptors */ | |
818 | vscsi_preprocess_desc(req); | |
6e270446 | 819 | |
ad3376cc PB |
820 | /* Get transfer direction and initiate transfer */ |
821 | if (n > 0) { | |
822 | req->data_len = n; | |
823 | } else if (n < 0) { | |
824 | req->data_len = -n; | |
825 | } | |
826 | scsi_req_continue(req->sreq); | |
6e270446 BH |
827 | } |
828 | /* Don't touch req here, it may have been recycled already */ | |
829 | ||
830 | return 0; | |
831 | } | |
832 | ||
833 | static int vscsi_process_tsk_mgmt(VSCSIState *s, vscsi_req *req) | |
834 | { | |
81e70549 | 835 | union viosrp_iu *iu = req_iu(req); |
eb37f146 AK |
836 | vscsi_req *tmpreq; |
837 | int i, lun = 0, resp = SRP_TSK_MGMT_COMPLETE; | |
838 | SCSIDevice *d; | |
839 | uint64_t tag = iu->srp.rsp.tag; | |
840 | uint8_t sol_not = iu->srp.cmd.sol_not; | |
6e270446 | 841 | |
a7017b20 | 842 | trace_spapr_vscsi_process_tsk_mgmt(iu->srp.tsk_mgmt.tsk_mgmt_func); |
81e70549 PMD |
843 | d = vscsi_device_find(&s->bus, |
844 | be64_to_cpu(req_iu(req)->srp.tsk_mgmt.lun), &lun); | |
eb37f146 AK |
845 | if (!d) { |
846 | resp = SRP_TSK_MGMT_FIELDS_INVALID; | |
847 | } else { | |
848 | switch (iu->srp.tsk_mgmt.tsk_mgmt_func) { | |
849 | case SRP_TSK_ABORT_TASK: | |
850 | if (d->lun != lun) { | |
851 | resp = SRP_TSK_MGMT_FIELDS_INVALID; | |
852 | break; | |
853 | } | |
854 | ||
81e70549 | 855 | tmpreq = vscsi_find_req(s, req_iu(req)->srp.tsk_mgmt.task_tag); |
eb37f146 AK |
856 | if (tmpreq && tmpreq->sreq) { |
857 | assert(tmpreq->sreq->hba_private); | |
858 | scsi_req_cancel(tmpreq->sreq); | |
859 | } | |
860 | break; | |
861 | ||
862 | case SRP_TSK_LUN_RESET: | |
863 | if (d->lun != lun) { | |
864 | resp = SRP_TSK_MGMT_FIELDS_INVALID; | |
865 | break; | |
866 | } | |
867 | ||
868 | qdev_reset_all(&d->qdev); | |
869 | break; | |
870 | ||
871 | case SRP_TSK_ABORT_TASK_SET: | |
872 | case SRP_TSK_CLEAR_TASK_SET: | |
873 | if (d->lun != lun) { | |
874 | resp = SRP_TSK_MGMT_FIELDS_INVALID; | |
875 | break; | |
876 | } | |
877 | ||
878 | for (i = 0; i < VSCSI_REQ_LIMIT; i++) { | |
879 | tmpreq = &s->reqs[i]; | |
81e70549 PMD |
880 | if (req_iu(tmpreq)->srp.cmd.lun |
881 | != req_iu(req)->srp.tsk_mgmt.lun) { | |
eb37f146 AK |
882 | continue; |
883 | } | |
884 | if (!tmpreq->active || !tmpreq->sreq) { | |
885 | continue; | |
886 | } | |
887 | assert(tmpreq->sreq->hba_private); | |
888 | scsi_req_cancel(tmpreq->sreq); | |
889 | } | |
890 | break; | |
891 | ||
892 | case SRP_TSK_CLEAR_ACA: | |
893 | resp = SRP_TSK_MGMT_NOT_SUPPORTED; | |
894 | break; | |
895 | ||
896 | default: | |
897 | resp = SRP_TSK_MGMT_FIELDS_INVALID; | |
898 | break; | |
899 | } | |
6e270446 | 900 | } |
eb37f146 AK |
901 | |
902 | /* Compose the response here as */ | |
13a54905 | 903 | QEMU_BUILD_BUG_ON(SRP_MAX_IU_DATA_LEN < 4); |
eb37f146 AK |
904 | memset(iu, 0, sizeof(struct srp_rsp) + 4); |
905 | iu->srp.rsp.opcode = SRP_RSP; | |
906 | iu->srp.rsp.req_lim_delta = cpu_to_be32(1); | |
907 | iu->srp.rsp.tag = tag; | |
908 | iu->srp.rsp.flags |= SRP_RSP_FLAG_RSPVALID; | |
909 | iu->srp.rsp.resp_data_len = cpu_to_be32(4); | |
910 | if (resp) { | |
911 | iu->srp.rsp.sol_not = (sol_not & 0x04) >> 2; | |
6e270446 | 912 | } else { |
eb37f146 | 913 | iu->srp.rsp.sol_not = (sol_not & 0x02) >> 1; |
6e270446 | 914 | } |
eb37f146 AK |
915 | |
916 | iu->srp.rsp.status = GOOD; | |
917 | iu->srp.rsp.data[3] = resp; | |
918 | ||
919 | vscsi_send_iu(s, req, sizeof(iu->srp.rsp) + 4, VIOSRP_SRP_FORMAT); | |
920 | ||
921 | return 1; | |
6e270446 BH |
922 | } |
923 | ||
924 | static int vscsi_handle_srp_req(VSCSIState *s, vscsi_req *req) | |
925 | { | |
81e70549 | 926 | union srp_iu *srp = &req_iu(req)->srp; |
6e270446 BH |
927 | int done = 1; |
928 | uint8_t opcode = srp->rsp.opcode; | |
929 | ||
930 | switch (opcode) { | |
931 | case SRP_LOGIN_REQ: | |
932 | vscsi_process_login(s, req); | |
933 | break; | |
934 | case SRP_TSK_MGMT: | |
935 | done = vscsi_process_tsk_mgmt(s, req); | |
936 | break; | |
937 | case SRP_CMD: | |
938 | done = vscsi_queue_cmd(s, req); | |
939 | break; | |
940 | case SRP_LOGIN_RSP: | |
941 | case SRP_I_LOGOUT: | |
942 | case SRP_T_LOGOUT: | |
943 | case SRP_RSP: | |
944 | case SRP_CRED_REQ: | |
945 | case SRP_CRED_RSP: | |
946 | case SRP_AER_REQ: | |
947 | case SRP_AER_RSP: | |
948 | fprintf(stderr, "VSCSI: Unsupported opcode %02x\n", opcode); | |
949 | break; | |
950 | default: | |
951 | fprintf(stderr, "VSCSI: Unknown type %02x\n", opcode); | |
952 | } | |
953 | ||
954 | return done; | |
955 | } | |
956 | ||
957 | static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) | |
958 | { | |
959 | struct viosrp_adapter_info *sinfo; | |
960 | struct mad_adapter_info_data info; | |
961 | int rc; | |
962 | ||
81e70549 | 963 | sinfo = &req_iu(req)->mad.adapter_info; |
6e270446 BH |
964 | |
965 | #if 0 /* What for ? */ | |
ad0ebb91 | 966 | rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(sinfo->buffer), |
6e270446 BH |
967 | &info, be16_to_cpu(sinfo->common.length)); |
968 | if (rc) { | |
969 | fprintf(stderr, "vscsi_send_adapter_info: DMA read failure !\n"); | |
970 | } | |
971 | #endif | |
972 | memset(&info, 0, sizeof(info)); | |
973 | strcpy(info.srp_version, SRP_VERSION); | |
9d055d8a | 974 | memcpy(info.partition_name, "qemu", sizeof("qemu")); |
6e270446 BH |
975 | info.partition_number = cpu_to_be32(0); |
976 | info.mad_version = cpu_to_be32(1); | |
977 | info.os_type = cpu_to_be32(2); | |
978 | info.port_max_txu[0] = cpu_to_be32(VSCSI_MAX_SECTORS << 9); | |
979 | ||
ad0ebb91 | 980 | rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(sinfo->buffer), |
6e270446 BH |
981 | &info, be16_to_cpu(sinfo->common.length)); |
982 | if (rc) { | |
983 | fprintf(stderr, "vscsi_send_adapter_info: DMA write failure !\n"); | |
984 | } | |
985 | ||
986 | sinfo->common.status = rc ? cpu_to_be32(1) : 0; | |
987 | ||
988 | return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT); | |
989 | } | |
990 | ||
26573a0c ND |
991 | static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req) |
992 | { | |
993 | struct viosrp_capabilities *vcap; | |
994 | struct capabilities cap = { }; | |
995 | uint16_t len, req_len; | |
996 | uint64_t buffer; | |
997 | int rc; | |
998 | ||
81e70549 | 999 | vcap = &req_iu(req)->mad.capabilities; |
26573a0c ND |
1000 | req_len = len = be16_to_cpu(vcap->common.length); |
1001 | buffer = be64_to_cpu(vcap->buffer); | |
1002 | if (len > sizeof(cap)) { | |
1003 | fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n"); | |
1004 | ||
1005 | /* | |
1006 | * Just read and populate the structure that is known. | |
1007 | * Zero rest of the structure. | |
1008 | */ | |
1009 | len = sizeof(cap); | |
1010 | } | |
1011 | rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len); | |
1012 | if (rc) { | |
1013 | fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n"); | |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * Current implementation does not suppport any migration or | |
1018 | * reservation capabilities. Construct the response telling the | |
1019 | * guest not to use them. | |
1020 | */ | |
1021 | cap.flags = 0; | |
1022 | cap.migration.ecl = 0; | |
1023 | cap.reserve.type = 0; | |
1024 | cap.migration.common.server_support = 0; | |
1025 | cap.reserve.common.server_support = 0; | |
1026 | ||
1027 | rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len); | |
1028 | if (rc) { | |
1029 | fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n"); | |
1030 | } | |
1031 | if (req_len > len) { | |
1032 | /* | |
1033 | * Being paranoid and lets not worry about the error code | |
1034 | * here. Actual write of the cap is done above. | |
1035 | */ | |
1036 | spapr_vio_dma_set(&s->vdev, (buffer + len), 0, (req_len - len)); | |
1037 | } | |
1038 | vcap->common.status = rc ? cpu_to_be32(1) : 0; | |
1039 | return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT); | |
1040 | } | |
1041 | ||
6e270446 BH |
1042 | static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req) |
1043 | { | |
81e70549 | 1044 | union mad_iu *mad = &req_iu(req)->mad; |
f4ff3b7b AK |
1045 | bool request_handled = false; |
1046 | uint64_t retlen = 0; | |
6e270446 BH |
1047 | |
1048 | switch (be32_to_cpu(mad->empty_iu.common.type)) { | |
1049 | case VIOSRP_EMPTY_IU_TYPE: | |
1050 | fprintf(stderr, "Unsupported EMPTY MAD IU\n"); | |
f4ff3b7b | 1051 | retlen = sizeof(mad->empty_iu); |
6e270446 BH |
1052 | break; |
1053 | case VIOSRP_ERROR_LOG_TYPE: | |
1054 | fprintf(stderr, "Unsupported ERROR LOG MAD IU\n"); | |
f4ff3b7b | 1055 | retlen = sizeof(mad->error_log); |
6e270446 BH |
1056 | break; |
1057 | case VIOSRP_ADAPTER_INFO_TYPE: | |
1058 | vscsi_send_adapter_info(s, req); | |
f4ff3b7b | 1059 | request_handled = true; |
6e270446 BH |
1060 | break; |
1061 | case VIOSRP_HOST_CONFIG_TYPE: | |
f4ff3b7b | 1062 | retlen = sizeof(mad->host_config); |
6e270446 | 1063 | break; |
26573a0c ND |
1064 | case VIOSRP_CAPABILITIES_TYPE: |
1065 | vscsi_send_capabilities(s, req); | |
f4ff3b7b | 1066 | request_handled = true; |
26573a0c | 1067 | break; |
6e270446 BH |
1068 | default: |
1069 | fprintf(stderr, "VSCSI: Unknown MAD type %02x\n", | |
1070 | be32_to_cpu(mad->empty_iu.common.type)); | |
f4ff3b7b AK |
1071 | /* |
1072 | * PAPR+ says that "The length field is set to the length | |
1073 | * of the data structure(s) used in the command". | |
1074 | * As we did not recognize the request type, put zero there. | |
1075 | */ | |
1076 | retlen = 0; | |
1077 | } | |
1078 | ||
1079 | if (!request_handled) { | |
1080 | mad->empty_iu.common.status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED); | |
1081 | vscsi_send_iu(s, req, retlen, VIOSRP_MAD_FORMAT); | |
6e270446 BH |
1082 | } |
1083 | ||
1084 | return 1; | |
1085 | } | |
1086 | ||
1087 | static void vscsi_got_payload(VSCSIState *s, vscsi_crq *crq) | |
1088 | { | |
1089 | vscsi_req *req; | |
1090 | int done; | |
1091 | ||
1092 | req = vscsi_get_req(s); | |
1093 | if (req == NULL) { | |
1094 | fprintf(stderr, "VSCSI: Failed to get a request !\n"); | |
1095 | return; | |
1096 | } | |
1097 | ||
1098 | /* We only support a limited number of descriptors, we know | |
1099 | * the ibmvscsi driver uses up to 10 max, so it should fit | |
1100 | * in our 256 bytes IUs. If not we'll have to increase the size | |
1101 | * of the structure. | |
1102 | */ | |
0dc55698 | 1103 | if (crq->s.IU_length > SRP_MAX_IU_LEN) { |
6e270446 BH |
1104 | fprintf(stderr, "VSCSI: SRP IU too long (%d bytes) !\n", |
1105 | crq->s.IU_length); | |
a4d8e8da | 1106 | vscsi_put_req(req); |
6e270446 BH |
1107 | return; |
1108 | } | |
1109 | ||
1110 | /* XXX Handle failure differently ? */ | |
ff78b728 | 1111 | if (spapr_vio_dma_read(&s->vdev, crq->s.IU_data_ptr, &req->viosrp_iu_buf, |
6e270446 BH |
1112 | crq->s.IU_length)) { |
1113 | fprintf(stderr, "vscsi_got_payload: DMA read failure !\n"); | |
a4d8e8da BH |
1114 | vscsi_put_req(req); |
1115 | return; | |
6e270446 BH |
1116 | } |
1117 | memcpy(&req->crq, crq, sizeof(vscsi_crq)); | |
1118 | ||
1119 | if (crq->s.format == VIOSRP_MAD_FORMAT) { | |
1120 | done = vscsi_handle_mad_req(s, req); | |
1121 | } else { | |
1122 | done = vscsi_handle_srp_req(s, req); | |
1123 | } | |
1124 | ||
1125 | if (done) { | |
c5bf71a9 | 1126 | vscsi_put_req(req); |
6e270446 BH |
1127 | } |
1128 | } | |
1129 | ||
1130 | ||
ce2918cb | 1131 | static int vscsi_do_crq(struct SpaprVioDevice *dev, uint8_t *crq_data) |
6e270446 | 1132 | { |
fd506b4f | 1133 | VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev); |
6e270446 BH |
1134 | vscsi_crq crq; |
1135 | ||
1136 | memcpy(crq.raw, crq_data, 16); | |
1137 | crq.s.timeout = be16_to_cpu(crq.s.timeout); | |
1138 | crq.s.IU_length = be16_to_cpu(crq.s.IU_length); | |
1139 | crq.s.IU_data_ptr = be64_to_cpu(crq.s.IU_data_ptr); | |
1140 | ||
f19661c8 | 1141 | trace_spapr_vscsi_do_crq(crq.raw[0], crq.raw[1]); |
6e270446 BH |
1142 | |
1143 | switch (crq.s.valid) { | |
1144 | case 0xc0: /* Init command/response */ | |
1145 | ||
1146 | /* Respond to initialization request */ | |
1147 | if (crq.s.format == 0x01) { | |
1148 | memset(crq.raw, 0, 16); | |
1149 | crq.s.valid = 0xc0; | |
1150 | crq.s.format = 0x02; | |
1151 | spapr_vio_send_crq(dev, crq.raw); | |
1152 | } | |
1153 | ||
1154 | /* Note that in hotplug cases, we might get a 0x02 | |
1155 | * as a result of us emitting the init request | |
1156 | */ | |
1157 | ||
1158 | break; | |
1159 | case 0xff: /* Link event */ | |
1160 | ||
1161 | /* Not handled for now */ | |
1162 | ||
1163 | break; | |
1164 | case 0x80: /* Payloads */ | |
1165 | switch (crq.s.format) { | |
1166 | case VIOSRP_SRP_FORMAT: /* AKA VSCSI request */ | |
1167 | case VIOSRP_MAD_FORMAT: /* AKA VSCSI response */ | |
1168 | vscsi_got_payload(s, &crq); | |
1169 | break; | |
1170 | case VIOSRP_OS400_FORMAT: | |
1171 | case VIOSRP_AIX_FORMAT: | |
1172 | case VIOSRP_LINUX_FORMAT: | |
1173 | case VIOSRP_INLINE_FORMAT: | |
1174 | fprintf(stderr, "vscsi_do_srq: Unsupported payload format %02x\n", | |
1175 | crq.s.format); | |
1176 | break; | |
1177 | default: | |
1178 | fprintf(stderr, "vscsi_do_srq: Unknown payload format %02x\n", | |
1179 | crq.s.format); | |
1180 | } | |
1181 | break; | |
1182 | default: | |
1183 | fprintf(stderr, "vscsi_do_crq: unknown CRQ %02x %02x ...\n", | |
1184 | crq.raw[0], crq.raw[1]); | |
1185 | }; | |
1186 | ||
1187 | return 0; | |
1188 | } | |
1189 | ||
afd4030c PB |
1190 | static const struct SCSIBusInfo vscsi_scsi_info = { |
1191 | .tcq = true, | |
0d3545e7 PB |
1192 | .max_channel = 7, /* logical unit addressing format */ |
1193 | .max_target = 63, | |
7e0380b9 | 1194 | .max_lun = 31, |
afd4030c | 1195 | |
c6df7102 | 1196 | .transfer_data = vscsi_transfer_data, |
94d3f98a | 1197 | .complete = vscsi_command_complete, |
1168ec7d DG |
1198 | .cancel = vscsi_request_cancelled, |
1199 | .save_request = vscsi_save_request, | |
1200 | .load_request = vscsi_load_request, | |
cfdc1bb0 PB |
1201 | }; |
1202 | ||
ce2918cb | 1203 | static void spapr_vscsi_reset(SpaprVioDevice *dev) |
6e270446 | 1204 | { |
fd506b4f | 1205 | VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev); |
6e270446 BH |
1206 | int i; |
1207 | ||
6e270446 BH |
1208 | memset(s->reqs, 0, sizeof(s->reqs)); |
1209 | for (i = 0; i < VSCSI_REQ_LIMIT; i++) { | |
1210 | s->reqs[i].qtag = i; | |
1211 | } | |
3cabba60 DG |
1212 | } |
1213 | ||
ce2918cb | 1214 | static void spapr_vscsi_realize(SpaprVioDevice *dev, Error **errp) |
3cabba60 | 1215 | { |
fd506b4f | 1216 | VSCSIState *s = VIO_SPAPR_VSCSI_DEVICE(dev); |
6e270446 BH |
1217 | |
1218 | dev->crq.SendFunc = vscsi_do_crq; | |
1219 | ||
b1187b51 AF |
1220 | scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev), |
1221 | &vscsi_scsi_info, NULL); | |
6e270446 BH |
1222 | } |
1223 | ||
ce2918cb | 1224 | void spapr_vscsi_create(SpaprVioBus *bus) |
6e270446 BH |
1225 | { |
1226 | DeviceState *dev; | |
6e270446 BH |
1227 | |
1228 | dev = qdev_create(&bus->bus, "spapr-vscsi"); | |
6e270446 BH |
1229 | |
1230 | qdev_init_nofail(dev); | |
14545097 | 1231 | scsi_bus_legacy_handle_cmdline(&VIO_SPAPR_VSCSI_DEVICE(dev)->bus); |
6e270446 BH |
1232 | } |
1233 | ||
ce2918cb | 1234 | static int spapr_vscsi_devnode(SpaprVioDevice *dev, void *fdt, int node_off) |
6e270446 BH |
1235 | { |
1236 | int ret; | |
1237 | ||
1238 | ret = fdt_setprop_cell(fdt, node_off, "#address-cells", 2); | |
1239 | if (ret < 0) { | |
1240 | return ret; | |
1241 | } | |
1242 | ||
1243 | ret = fdt_setprop_cell(fdt, node_off, "#size-cells", 0); | |
1244 | if (ret < 0) { | |
1245 | return ret; | |
1246 | } | |
1247 | ||
1248 | return 0; | |
1249 | } | |
1250 | ||
3954d33a | 1251 | static Property spapr_vscsi_properties[] = { |
ad0ebb91 | 1252 | DEFINE_SPAPR_PROPERTIES(VSCSIState, vdev), |
3954d33a AL |
1253 | DEFINE_PROP_END_OF_LIST(), |
1254 | }; | |
1255 | ||
1168ec7d DG |
1256 | static const VMStateDescription vmstate_spapr_vscsi = { |
1257 | .name = "spapr_vscsi", | |
1258 | .version_id = 1, | |
1259 | .minimum_version_id = 1, | |
3aff6c2f | 1260 | .fields = (VMStateField[]) { |
1168ec7d DG |
1261 | VMSTATE_SPAPR_VIO(vdev, VSCSIState), |
1262 | /* VSCSI state */ | |
1263 | /* ???? */ | |
1264 | ||
1265 | VMSTATE_END_OF_LIST() | |
1266 | }, | |
1267 | }; | |
1268 | ||
3954d33a AL |
1269 | static void spapr_vscsi_class_init(ObjectClass *klass, void *data) |
1270 | { | |
39bffca2 | 1271 | DeviceClass *dc = DEVICE_CLASS(klass); |
ce2918cb | 1272 | SpaprVioDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); |
3954d33a | 1273 | |
28b07e73 | 1274 | k->realize = spapr_vscsi_realize; |
3cabba60 | 1275 | k->reset = spapr_vscsi_reset; |
3954d33a AL |
1276 | k->devnode = spapr_vscsi_devnode; |
1277 | k->dt_name = "v-scsi"; | |
1278 | k->dt_type = "vscsi"; | |
1279 | k->dt_compatible = "IBM,v-scsi"; | |
1280 | k->signal_mask = 0x00000001; | |
29fdedfe | 1281 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
4f67d30b | 1282 | device_class_set_props(dc, spapr_vscsi_properties); |
ad0ebb91 | 1283 | k->rtce_window_size = 0x10000000; |
1168ec7d | 1284 | dc->vmsd = &vmstate_spapr_vscsi; |
3954d33a AL |
1285 | } |
1286 | ||
8c43a6f0 | 1287 | static const TypeInfo spapr_vscsi_info = { |
fd506b4f | 1288 | .name = TYPE_VIO_SPAPR_VSCSI_DEVICE, |
39bffca2 AL |
1289 | .parent = TYPE_VIO_SPAPR_DEVICE, |
1290 | .instance_size = sizeof(VSCSIState), | |
1291 | .class_init = spapr_vscsi_class_init, | |
6e270446 BH |
1292 | }; |
1293 | ||
83f7d43a | 1294 | static void spapr_vscsi_register_types(void) |
6e270446 | 1295 | { |
39bffca2 | 1296 | type_register_static(&spapr_vscsi_info); |
6e270446 | 1297 | } |
83f7d43a AF |
1298 | |
1299 | type_init(spapr_vscsi_register_types) |