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