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