linux-user: Use target_restore_altstack in all sigreturn
[qemu/ar7.git] / hw / scsi / esp.c
blobb668acef82df4f8c03a39d98ffb88404a5c79dc9
1 /*
2 * QEMU ESP/NCR53C9x emulation
4 * Copyright (c) 2005-2006 Fabrice Bellard
5 * Copyright (c) 2012 Herve Poussineau
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
28 #include "migration/vmstate.h"
29 #include "hw/irq.h"
30 #include "hw/scsi/esp.h"
31 #include "trace.h"
32 #include "qemu/log.h"
33 #include "qemu/module.h"
36 * On Sparc32, this is the ESP (NCR53C90) part of chip STP2000 (Master I/O),
37 * also produced as NCR89C100. See
38 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
39 * and
40 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR53C9X.txt
42 * On Macintosh Quadra it is a NCR53C96.
45 static void esp_raise_irq(ESPState *s)
47 if (!(s->rregs[ESP_RSTAT] & STAT_INT)) {
48 s->rregs[ESP_RSTAT] |= STAT_INT;
49 qemu_irq_raise(s->irq);
50 trace_esp_raise_irq();
54 static void esp_lower_irq(ESPState *s)
56 if (s->rregs[ESP_RSTAT] & STAT_INT) {
57 s->rregs[ESP_RSTAT] &= ~STAT_INT;
58 qemu_irq_lower(s->irq);
59 trace_esp_lower_irq();
63 static void esp_raise_drq(ESPState *s)
65 qemu_irq_raise(s->irq_data);
66 trace_esp_raise_drq();
69 static void esp_lower_drq(ESPState *s)
71 qemu_irq_lower(s->irq_data);
72 trace_esp_lower_drq();
75 void esp_dma_enable(ESPState *s, int irq, int level)
77 if (level) {
78 s->dma_enabled = 1;
79 trace_esp_dma_enable();
80 if (s->dma_cb) {
81 s->dma_cb(s);
82 s->dma_cb = NULL;
84 } else {
85 trace_esp_dma_disable();
86 s->dma_enabled = 0;
90 void esp_request_cancelled(SCSIRequest *req)
92 ESPState *s = req->hba_private;
94 if (req == s->current_req) {
95 scsi_req_unref(s->current_req);
96 s->current_req = NULL;
97 s->current_dev = NULL;
98 s->async_len = 0;
102 static void esp_fifo_push(Fifo8 *fifo, uint8_t val)
104 if (fifo8_num_used(fifo) == fifo->capacity) {
105 trace_esp_error_fifo_overrun();
106 return;
109 fifo8_push(fifo, val);
112 static uint8_t esp_fifo_pop(Fifo8 *fifo)
114 if (fifo8_is_empty(fifo)) {
115 return 0;
118 return fifo8_pop(fifo);
121 static uint32_t esp_fifo_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen)
123 const uint8_t *buf;
124 uint32_t n;
126 if (maxlen == 0) {
127 return 0;
130 buf = fifo8_pop_buf(fifo, maxlen, &n);
131 if (dest) {
132 memcpy(dest, buf, n);
135 return n;
138 static uint32_t esp_get_tc(ESPState *s)
140 uint32_t dmalen;
142 dmalen = s->rregs[ESP_TCLO];
143 dmalen |= s->rregs[ESP_TCMID] << 8;
144 dmalen |= s->rregs[ESP_TCHI] << 16;
146 return dmalen;
149 static void esp_set_tc(ESPState *s, uint32_t dmalen)
151 s->rregs[ESP_TCLO] = dmalen;
152 s->rregs[ESP_TCMID] = dmalen >> 8;
153 s->rregs[ESP_TCHI] = dmalen >> 16;
156 static uint32_t esp_get_stc(ESPState *s)
158 uint32_t dmalen;
160 dmalen = s->wregs[ESP_TCLO];
161 dmalen |= s->wregs[ESP_TCMID] << 8;
162 dmalen |= s->wregs[ESP_TCHI] << 16;
164 return dmalen;
167 static uint8_t esp_pdma_read(ESPState *s)
169 uint8_t val;
171 if (s->do_cmd) {
172 val = esp_fifo_pop(&s->cmdfifo);
173 } else {
174 val = esp_fifo_pop(&s->fifo);
177 return val;
180 static void esp_pdma_write(ESPState *s, uint8_t val)
182 uint32_t dmalen = esp_get_tc(s);
184 if (dmalen == 0) {
185 return;
188 if (s->do_cmd) {
189 esp_fifo_push(&s->cmdfifo, val);
190 } else {
191 esp_fifo_push(&s->fifo, val);
194 dmalen--;
195 esp_set_tc(s, dmalen);
198 static int esp_select(ESPState *s)
200 int target;
202 target = s->wregs[ESP_WBUSID] & BUSID_DID;
204 s->ti_size = 0;
205 fifo8_reset(&s->fifo);
207 if (s->current_req) {
208 /* Started a new command before the old one finished. Cancel it. */
209 scsi_req_cancel(s->current_req);
212 s->current_dev = scsi_device_find(&s->bus, 0, target, 0);
213 if (!s->current_dev) {
214 /* No such drive */
215 s->rregs[ESP_RSTAT] = 0;
216 s->rregs[ESP_RINTR] |= INTR_DC;
217 s->rregs[ESP_RSEQ] = SEQ_0;
218 esp_raise_irq(s);
219 return -1;
223 * Note that we deliberately don't raise the IRQ here: this will be done
224 * either in do_busid_cmd() for DATA OUT transfers or by the deferred
225 * IRQ mechanism in esp_transfer_data() for DATA IN transfers
227 s->rregs[ESP_RINTR] |= INTR_FC;
228 s->rregs[ESP_RSEQ] = SEQ_CD;
229 return 0;
232 static uint32_t get_cmd(ESPState *s, uint32_t maxlen)
234 uint8_t buf[ESP_CMDFIFO_SZ];
235 uint32_t dmalen, n;
236 int target;
238 target = s->wregs[ESP_WBUSID] & BUSID_DID;
239 if (s->dma) {
240 dmalen = MIN(esp_get_tc(s), maxlen);
241 if (dmalen == 0) {
242 return 0;
244 if (s->dma_memory_read) {
245 s->dma_memory_read(s->dma_opaque, buf, dmalen);
246 dmalen = MIN(fifo8_num_free(&s->cmdfifo), dmalen);
247 fifo8_push_all(&s->cmdfifo, buf, dmalen);
248 } else {
249 if (esp_select(s) < 0) {
250 fifo8_reset(&s->cmdfifo);
251 return -1;
253 esp_raise_drq(s);
254 fifo8_reset(&s->cmdfifo);
255 return 0;
257 } else {
258 dmalen = MIN(fifo8_num_used(&s->fifo), maxlen);
259 if (dmalen == 0) {
260 return 0;
262 n = esp_fifo_pop_buf(&s->fifo, buf, dmalen);
263 if (n >= 3) {
264 buf[0] = buf[2] >> 5;
266 n = MIN(fifo8_num_free(&s->cmdfifo), n);
267 fifo8_push_all(&s->cmdfifo, buf, n);
269 trace_esp_get_cmd(dmalen, target);
271 if (esp_select(s) < 0) {
272 fifo8_reset(&s->cmdfifo);
273 return -1;
275 return dmalen;
278 static void do_busid_cmd(ESPState *s, uint8_t busid)
280 uint32_t cmdlen;
281 int32_t datalen;
282 int lun;
283 SCSIDevice *current_lun;
284 uint8_t buf[ESP_CMDFIFO_SZ];
286 trace_esp_do_busid_cmd(busid);
287 lun = busid & 7;
288 cmdlen = fifo8_num_used(&s->cmdfifo);
289 if (!cmdlen || !s->current_dev) {
290 return;
292 esp_fifo_pop_buf(&s->cmdfifo, buf, cmdlen);
294 current_lun = scsi_device_find(&s->bus, 0, s->current_dev->id, lun);
295 s->current_req = scsi_req_new(current_lun, 0, lun, buf, s);
296 datalen = scsi_req_enqueue(s->current_req);
297 s->ti_size = datalen;
298 fifo8_reset(&s->cmdfifo);
299 if (datalen != 0) {
300 s->rregs[ESP_RSTAT] = STAT_TC;
301 s->rregs[ESP_RSEQ] = SEQ_CD;
302 s->ti_cmd = 0;
303 esp_set_tc(s, 0);
304 if (datalen > 0) {
306 * Switch to DATA IN phase but wait until initial data xfer is
307 * complete before raising the command completion interrupt
309 s->data_in_ready = false;
310 s->rregs[ESP_RSTAT] |= STAT_DI;
311 } else {
312 s->rregs[ESP_RSTAT] |= STAT_DO;
313 s->rregs[ESP_RINTR] |= INTR_BS | INTR_FC;
314 esp_raise_irq(s);
315 esp_lower_drq(s);
317 scsi_req_continue(s->current_req);
318 return;
322 static void do_cmd(ESPState *s)
324 uint8_t busid = esp_fifo_pop(&s->cmdfifo);
325 int len;
327 s->cmdfifo_cdb_offset--;
329 /* Ignore extended messages for now */
330 if (s->cmdfifo_cdb_offset) {
331 len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo));
332 esp_fifo_pop_buf(&s->cmdfifo, NULL, len);
333 s->cmdfifo_cdb_offset = 0;
336 do_busid_cmd(s, busid);
339 static void satn_pdma_cb(ESPState *s)
341 s->do_cmd = 0;
342 if (!fifo8_is_empty(&s->cmdfifo)) {
343 s->cmdfifo_cdb_offset = 1;
344 do_cmd(s);
348 static void handle_satn(ESPState *s)
350 int32_t cmdlen;
352 if (s->dma && !s->dma_enabled) {
353 s->dma_cb = handle_satn;
354 return;
356 s->pdma_cb = satn_pdma_cb;
357 cmdlen = get_cmd(s, ESP_CMDFIFO_SZ);
358 if (cmdlen > 0) {
359 s->cmdfifo_cdb_offset = 1;
360 s->do_cmd = 0;
361 do_cmd(s);
362 } else if (cmdlen == 0) {
363 s->do_cmd = 1;
364 /* Target present, but no cmd yet - switch to command phase */
365 s->rregs[ESP_RSEQ] = SEQ_CD;
366 s->rregs[ESP_RSTAT] = STAT_CD;
370 static void s_without_satn_pdma_cb(ESPState *s)
372 uint32_t len;
374 s->do_cmd = 0;
375 len = fifo8_num_used(&s->cmdfifo);
376 if (len) {
377 s->cmdfifo_cdb_offset = 0;
378 do_busid_cmd(s, 0);
382 static void handle_s_without_atn(ESPState *s)
384 int32_t cmdlen;
386 if (s->dma && !s->dma_enabled) {
387 s->dma_cb = handle_s_without_atn;
388 return;
390 s->pdma_cb = s_without_satn_pdma_cb;
391 cmdlen = get_cmd(s, ESP_CMDFIFO_SZ);
392 if (cmdlen > 0) {
393 s->cmdfifo_cdb_offset = 0;
394 s->do_cmd = 0;
395 do_busid_cmd(s, 0);
396 } else if (cmdlen == 0) {
397 s->do_cmd = 1;
398 /* Target present, but no cmd yet - switch to command phase */
399 s->rregs[ESP_RSEQ] = SEQ_CD;
400 s->rregs[ESP_RSTAT] = STAT_CD;
404 static void satn_stop_pdma_cb(ESPState *s)
406 s->do_cmd = 0;
407 if (!fifo8_is_empty(&s->cmdfifo)) {
408 trace_esp_handle_satn_stop(fifo8_num_used(&s->cmdfifo));
409 s->do_cmd = 1;
410 s->cmdfifo_cdb_offset = 1;
411 s->rregs[ESP_RSTAT] = STAT_TC | STAT_CD;
412 s->rregs[ESP_RINTR] |= INTR_BS | INTR_FC;
413 s->rregs[ESP_RSEQ] = SEQ_CD;
414 esp_raise_irq(s);
418 static void handle_satn_stop(ESPState *s)
420 int32_t cmdlen;
422 if (s->dma && !s->dma_enabled) {
423 s->dma_cb = handle_satn_stop;
424 return;
426 s->pdma_cb = satn_stop_pdma_cb;
427 cmdlen = get_cmd(s, 1);
428 if (cmdlen > 0) {
429 trace_esp_handle_satn_stop(fifo8_num_used(&s->cmdfifo));
430 s->do_cmd = 1;
431 s->cmdfifo_cdb_offset = 1;
432 s->rregs[ESP_RSTAT] = STAT_MO;
433 s->rregs[ESP_RINTR] |= INTR_BS | INTR_FC;
434 s->rregs[ESP_RSEQ] = SEQ_MO;
435 esp_raise_irq(s);
436 } else if (cmdlen == 0) {
437 s->do_cmd = 1;
438 /* Target present, switch to message out phase */
439 s->rregs[ESP_RSEQ] = SEQ_MO;
440 s->rregs[ESP_RSTAT] = STAT_MO;
444 static void write_response_pdma_cb(ESPState *s)
446 s->rregs[ESP_RSTAT] = STAT_TC | STAT_ST;
447 s->rregs[ESP_RINTR] |= INTR_BS | INTR_FC;
448 s->rregs[ESP_RSEQ] = SEQ_CD;
449 esp_raise_irq(s);
452 static void write_response(ESPState *s)
454 uint8_t buf[2];
456 trace_esp_write_response(s->status);
458 buf[0] = s->status;
459 buf[1] = 0;
461 if (s->dma) {
462 if (s->dma_memory_write) {
463 s->dma_memory_write(s->dma_opaque, buf, 2);
464 s->rregs[ESP_RSTAT] = STAT_TC | STAT_ST;
465 s->rregs[ESP_RINTR] |= INTR_BS | INTR_FC;
466 s->rregs[ESP_RSEQ] = SEQ_CD;
467 } else {
468 s->pdma_cb = write_response_pdma_cb;
469 esp_raise_drq(s);
470 return;
472 } else {
473 fifo8_reset(&s->fifo);
474 fifo8_push_all(&s->fifo, buf, 2);
475 s->rregs[ESP_RFLAGS] = 2;
477 esp_raise_irq(s);
480 static void esp_dma_done(ESPState *s)
482 s->rregs[ESP_RSTAT] |= STAT_TC;
483 s->rregs[ESP_RINTR] |= INTR_BS;
484 s->rregs[ESP_RSEQ] = 0;
485 s->rregs[ESP_RFLAGS] = 0;
486 esp_set_tc(s, 0);
487 esp_raise_irq(s);
490 static void do_dma_pdma_cb(ESPState *s)
492 int to_device = ((s->rregs[ESP_RSTAT] & 7) == STAT_DO);
493 int len;
494 uint32_t n;
496 if (s->do_cmd) {
497 s->ti_size = 0;
498 s->do_cmd = 0;
499 do_cmd(s);
500 esp_lower_drq(s);
501 return;
504 if (!s->current_req) {
505 return;
508 if (to_device) {
509 /* Copy FIFO data to device */
510 len = MIN(s->async_len, ESP_FIFO_SZ);
511 len = MIN(len, fifo8_num_used(&s->fifo));
512 n = esp_fifo_pop_buf(&s->fifo, s->async_buf, len);
513 s->async_buf += n;
514 s->async_len -= n;
515 s->ti_size += n;
517 if (n < len) {
518 /* Unaligned accesses can cause FIFO wraparound */
519 len = len - n;
520 n = esp_fifo_pop_buf(&s->fifo, s->async_buf, len);
521 s->async_buf += n;
522 s->async_len -= n;
523 s->ti_size += n;
526 if (s->async_len == 0) {
527 scsi_req_continue(s->current_req);
528 return;
531 if (esp_get_tc(s) == 0) {
532 esp_lower_drq(s);
533 esp_dma_done(s);
536 return;
537 } else {
538 if (s->async_len == 0) {
539 /* Defer until the scsi layer has completed */
540 scsi_req_continue(s->current_req);
541 s->data_in_ready = false;
542 return;
545 if (esp_get_tc(s) != 0) {
546 /* Copy device data to FIFO */
547 len = MIN(s->async_len, esp_get_tc(s));
548 len = MIN(len, fifo8_num_free(&s->fifo));
549 fifo8_push_all(&s->fifo, s->async_buf, len);
550 s->async_buf += len;
551 s->async_len -= len;
552 s->ti_size -= len;
553 esp_set_tc(s, esp_get_tc(s) - len);
555 if (esp_get_tc(s) == 0) {
556 /* Indicate transfer to FIFO is complete */
557 s->rregs[ESP_RSTAT] |= STAT_TC;
559 return;
562 /* Partially filled a scsi buffer. Complete immediately. */
563 esp_lower_drq(s);
564 esp_dma_done(s);
568 static void esp_do_dma(ESPState *s)
570 uint32_t len, cmdlen;
571 int to_device = ((s->rregs[ESP_RSTAT] & 7) == STAT_DO);
572 uint8_t buf[ESP_CMDFIFO_SZ];
574 len = esp_get_tc(s);
575 if (s->do_cmd) {
577 * handle_ti_cmd() case: esp_do_dma() is called only from
578 * handle_ti_cmd() with do_cmd != NULL (see the assert())
580 cmdlen = fifo8_num_used(&s->cmdfifo);
581 trace_esp_do_dma(cmdlen, len);
582 if (s->dma_memory_read) {
583 len = MIN(len, fifo8_num_free(&s->cmdfifo));
584 s->dma_memory_read(s->dma_opaque, buf, len);
585 fifo8_push_all(&s->cmdfifo, buf, len);
586 } else {
587 s->pdma_cb = do_dma_pdma_cb;
588 esp_raise_drq(s);
589 return;
591 trace_esp_handle_ti_cmd(cmdlen);
592 s->ti_size = 0;
593 if ((s->rregs[ESP_RSTAT] & 7) == STAT_CD) {
594 /* No command received */
595 if (s->cmdfifo_cdb_offset == fifo8_num_used(&s->cmdfifo)) {
596 return;
599 /* Command has been received */
600 s->do_cmd = 0;
601 do_cmd(s);
602 } else {
604 * Extra message out bytes received: update cmdfifo_cdb_offset
605 * and then switch to commmand phase
607 s->cmdfifo_cdb_offset = fifo8_num_used(&s->cmdfifo);
608 s->rregs[ESP_RSTAT] = STAT_TC | STAT_CD;
609 s->rregs[ESP_RSEQ] = SEQ_CD;
610 s->rregs[ESP_RINTR] |= INTR_BS;
611 esp_raise_irq(s);
613 return;
615 if (!s->current_req) {
616 return;
618 if (s->async_len == 0) {
619 /* Defer until data is available. */
620 return;
622 if (len > s->async_len) {
623 len = s->async_len;
625 if (to_device) {
626 if (s->dma_memory_read) {
627 s->dma_memory_read(s->dma_opaque, s->async_buf, len);
628 } else {
629 s->pdma_cb = do_dma_pdma_cb;
630 esp_raise_drq(s);
631 return;
633 } else {
634 if (s->dma_memory_write) {
635 s->dma_memory_write(s->dma_opaque, s->async_buf, len);
636 } else {
637 /* Adjust TC for any leftover data in the FIFO */
638 if (!fifo8_is_empty(&s->fifo)) {
639 esp_set_tc(s, esp_get_tc(s) - fifo8_num_used(&s->fifo));
642 /* Copy device data to FIFO */
643 len = MIN(len, fifo8_num_free(&s->fifo));
644 fifo8_push_all(&s->fifo, s->async_buf, len);
645 s->async_buf += len;
646 s->async_len -= len;
647 s->ti_size -= len;
650 * MacOS toolbox uses a TI length of 16 bytes for all commands, so
651 * commands shorter than this must be padded accordingly
653 if (len < esp_get_tc(s) && esp_get_tc(s) <= ESP_FIFO_SZ) {
654 while (fifo8_num_used(&s->fifo) < ESP_FIFO_SZ) {
655 esp_fifo_push(&s->fifo, 0);
656 len++;
660 esp_set_tc(s, esp_get_tc(s) - len);
661 s->pdma_cb = do_dma_pdma_cb;
662 esp_raise_drq(s);
664 /* Indicate transfer to FIFO is complete */
665 s->rregs[ESP_RSTAT] |= STAT_TC;
666 return;
669 esp_set_tc(s, esp_get_tc(s) - len);
670 s->async_buf += len;
671 s->async_len -= len;
672 if (to_device) {
673 s->ti_size += len;
674 } else {
675 s->ti_size -= len;
677 if (s->async_len == 0) {
678 scsi_req_continue(s->current_req);
680 * If there is still data to be read from the device then
681 * complete the DMA operation immediately. Otherwise defer
682 * until the scsi layer has completed.
684 if (to_device || esp_get_tc(s) != 0 || s->ti_size == 0) {
685 return;
689 /* Partially filled a scsi buffer. Complete immediately. */
690 esp_dma_done(s);
691 esp_lower_drq(s);
694 static void esp_do_nodma(ESPState *s)
696 int to_device = ((s->rregs[ESP_RSTAT] & 7) == STAT_DO);
697 uint32_t cmdlen;
698 int len;
700 if (s->do_cmd) {
701 cmdlen = fifo8_num_used(&s->cmdfifo);
702 trace_esp_handle_ti_cmd(cmdlen);
703 s->ti_size = 0;
704 if ((s->rregs[ESP_RSTAT] & 7) == STAT_CD) {
705 /* No command received */
706 if (s->cmdfifo_cdb_offset == fifo8_num_used(&s->cmdfifo)) {
707 return;
710 /* Command has been received */
711 s->do_cmd = 0;
712 do_cmd(s);
713 } else {
715 * Extra message out bytes received: update cmdfifo_cdb_offset
716 * and then switch to commmand phase
718 s->cmdfifo_cdb_offset = fifo8_num_used(&s->cmdfifo);
719 s->rregs[ESP_RSTAT] = STAT_TC | STAT_CD;
720 s->rregs[ESP_RSEQ] = SEQ_CD;
721 s->rregs[ESP_RINTR] |= INTR_BS;
722 esp_raise_irq(s);
724 return;
727 if (!s->current_req) {
728 return;
731 if (s->async_len == 0) {
732 /* Defer until data is available. */
733 return;
736 if (to_device) {
737 len = MIN(fifo8_num_used(&s->fifo), ESP_FIFO_SZ);
738 esp_fifo_pop_buf(&s->fifo, s->async_buf, len);
739 s->async_buf += len;
740 s->async_len -= len;
741 s->ti_size += len;
742 } else {
743 len = MIN(s->ti_size, s->async_len);
744 len = MIN(len, fifo8_num_free(&s->fifo));
745 fifo8_push_all(&s->fifo, s->async_buf, len);
746 s->async_buf += len;
747 s->async_len -= len;
748 s->ti_size -= len;
751 if (s->async_len == 0) {
752 scsi_req_continue(s->current_req);
754 if (to_device || s->ti_size == 0) {
755 return;
759 s->rregs[ESP_RINTR] |= INTR_BS;
760 esp_raise_irq(s);
763 void esp_command_complete(SCSIRequest *req, size_t resid)
765 ESPState *s = req->hba_private;
767 trace_esp_command_complete();
768 if (s->ti_size != 0) {
769 trace_esp_command_complete_unexpected();
771 s->ti_size = 0;
772 s->async_len = 0;
773 if (req->status) {
774 trace_esp_command_complete_fail();
776 s->status = req->status;
777 s->rregs[ESP_RSTAT] = STAT_ST;
778 esp_dma_done(s);
779 esp_lower_drq(s);
780 if (s->current_req) {
781 scsi_req_unref(s->current_req);
782 s->current_req = NULL;
783 s->current_dev = NULL;
787 void esp_transfer_data(SCSIRequest *req, uint32_t len)
789 ESPState *s = req->hba_private;
790 int to_device = ((s->rregs[ESP_RSTAT] & 7) == STAT_DO);
791 uint32_t dmalen = esp_get_tc(s);
793 assert(!s->do_cmd);
794 trace_esp_transfer_data(dmalen, s->ti_size);
795 s->async_len = len;
796 s->async_buf = scsi_req_get_buf(req);
798 if (!to_device && !s->data_in_ready) {
800 * Initial incoming data xfer is complete so raise command
801 * completion interrupt
803 s->data_in_ready = true;
804 s->rregs[ESP_RSTAT] |= STAT_TC;
805 s->rregs[ESP_RINTR] |= INTR_BS;
806 esp_raise_irq(s);
809 * If data is ready to transfer and the TI command has already
810 * been executed, start DMA immediately. Otherwise DMA will start
811 * when host sends the TI command
813 if (s->ti_size && (s->rregs[ESP_CMD] == (CMD_TI | CMD_DMA))) {
814 esp_do_dma(s);
816 return;
819 if (s->ti_cmd == 0) {
821 * Always perform the initial transfer upon reception of the next TI
822 * command to ensure the DMA/non-DMA status of the command is correct.
823 * It is not possible to use s->dma directly in the section below as
824 * some OSs send non-DMA NOP commands after a DMA transfer. Hence if the
825 * async data transfer is delayed then s->dma is set incorrectly.
827 return;
830 if (s->ti_cmd & CMD_DMA) {
831 if (dmalen) {
832 esp_do_dma(s);
833 } else if (s->ti_size <= 0) {
835 * If this was the last part of a DMA transfer then the
836 * completion interrupt is deferred to here.
838 esp_dma_done(s);
839 esp_lower_drq(s);
841 } else {
842 esp_do_nodma(s);
846 static void handle_ti(ESPState *s)
848 uint32_t dmalen;
850 if (s->dma && !s->dma_enabled) {
851 s->dma_cb = handle_ti;
852 return;
855 s->ti_cmd = s->rregs[ESP_CMD];
856 if (s->dma) {
857 dmalen = esp_get_tc(s);
858 trace_esp_handle_ti(dmalen);
859 s->rregs[ESP_RSTAT] &= ~STAT_TC;
860 esp_do_dma(s);
861 } else {
862 trace_esp_handle_ti(s->ti_size);
863 esp_do_nodma(s);
867 void esp_hard_reset(ESPState *s)
869 memset(s->rregs, 0, ESP_REGS);
870 memset(s->wregs, 0, ESP_REGS);
871 s->tchi_written = 0;
872 s->ti_size = 0;
873 fifo8_reset(&s->fifo);
874 fifo8_reset(&s->cmdfifo);
875 s->dma = 0;
876 s->do_cmd = 0;
877 s->dma_cb = NULL;
879 s->rregs[ESP_CFG1] = 7;
882 static void esp_soft_reset(ESPState *s)
884 qemu_irq_lower(s->irq);
885 qemu_irq_lower(s->irq_data);
886 esp_hard_reset(s);
889 static void parent_esp_reset(ESPState *s, int irq, int level)
891 if (level) {
892 esp_soft_reset(s);
896 uint64_t esp_reg_read(ESPState *s, uint32_t saddr)
898 uint32_t val;
900 switch (saddr) {
901 case ESP_FIFO:
902 if (s->dma_memory_read && s->dma_memory_write &&
903 (s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
904 /* Data out. */
905 qemu_log_mask(LOG_UNIMP, "esp: PIO data read not implemented\n");
906 s->rregs[ESP_FIFO] = 0;
907 } else {
908 s->rregs[ESP_FIFO] = esp_fifo_pop(&s->fifo);
910 val = s->rregs[ESP_FIFO];
911 break;
912 case ESP_RINTR:
914 * Clear sequence step, interrupt register and all status bits
915 * except TC
917 val = s->rregs[ESP_RINTR];
918 s->rregs[ESP_RINTR] = 0;
919 s->rregs[ESP_RSTAT] &= ~STAT_TC;
920 s->rregs[ESP_RSEQ] = SEQ_0;
921 esp_lower_irq(s);
922 break;
923 case ESP_TCHI:
924 /* Return the unique id if the value has never been written */
925 if (!s->tchi_written) {
926 val = s->chip_id;
927 } else {
928 val = s->rregs[saddr];
930 break;
931 case ESP_RFLAGS:
932 /* Bottom 5 bits indicate number of bytes in FIFO */
933 val = fifo8_num_used(&s->fifo);
934 break;
935 default:
936 val = s->rregs[saddr];
937 break;
940 trace_esp_mem_readb(saddr, val);
941 return val;
944 void esp_reg_write(ESPState *s, uint32_t saddr, uint64_t val)
946 trace_esp_mem_writeb(saddr, s->wregs[saddr], val);
947 switch (saddr) {
948 case ESP_TCHI:
949 s->tchi_written = true;
950 /* fall through */
951 case ESP_TCLO:
952 case ESP_TCMID:
953 s->rregs[ESP_RSTAT] &= ~STAT_TC;
954 break;
955 case ESP_FIFO:
956 if (s->do_cmd) {
957 esp_fifo_push(&s->cmdfifo, val);
958 } else {
959 esp_fifo_push(&s->fifo, val);
962 /* Non-DMA transfers raise an interrupt after every byte */
963 if (s->rregs[ESP_CMD] == CMD_TI) {
964 s->rregs[ESP_RINTR] |= INTR_FC | INTR_BS;
965 esp_raise_irq(s);
967 break;
968 case ESP_CMD:
969 s->rregs[saddr] = val;
970 if (val & CMD_DMA) {
971 s->dma = 1;
972 /* Reload DMA counter. */
973 if (esp_get_stc(s) == 0) {
974 esp_set_tc(s, 0x10000);
975 } else {
976 esp_set_tc(s, esp_get_stc(s));
978 } else {
979 s->dma = 0;
981 switch (val & CMD_CMD) {
982 case CMD_NOP:
983 trace_esp_mem_writeb_cmd_nop(val);
984 break;
985 case CMD_FLUSH:
986 trace_esp_mem_writeb_cmd_flush(val);
987 fifo8_reset(&s->fifo);
988 break;
989 case CMD_RESET:
990 trace_esp_mem_writeb_cmd_reset(val);
991 esp_soft_reset(s);
992 break;
993 case CMD_BUSRESET:
994 trace_esp_mem_writeb_cmd_bus_reset(val);
995 if (!(s->wregs[ESP_CFG1] & CFG1_RESREPT)) {
996 s->rregs[ESP_RINTR] |= INTR_RST;
997 esp_raise_irq(s);
999 break;
1000 case CMD_TI:
1001 trace_esp_mem_writeb_cmd_ti(val);
1002 handle_ti(s);
1003 break;
1004 case CMD_ICCS:
1005 trace_esp_mem_writeb_cmd_iccs(val);
1006 write_response(s);
1007 s->rregs[ESP_RINTR] |= INTR_FC;
1008 s->rregs[ESP_RSTAT] |= STAT_MI;
1009 break;
1010 case CMD_MSGACC:
1011 trace_esp_mem_writeb_cmd_msgacc(val);
1012 s->rregs[ESP_RINTR] |= INTR_DC;
1013 s->rregs[ESP_RSEQ] = 0;
1014 s->rregs[ESP_RFLAGS] = 0;
1015 esp_raise_irq(s);
1016 break;
1017 case CMD_PAD:
1018 trace_esp_mem_writeb_cmd_pad(val);
1019 s->rregs[ESP_RSTAT] = STAT_TC;
1020 s->rregs[ESP_RINTR] |= INTR_FC;
1021 s->rregs[ESP_RSEQ] = 0;
1022 break;
1023 case CMD_SATN:
1024 trace_esp_mem_writeb_cmd_satn(val);
1025 break;
1026 case CMD_RSTATN:
1027 trace_esp_mem_writeb_cmd_rstatn(val);
1028 break;
1029 case CMD_SEL:
1030 trace_esp_mem_writeb_cmd_sel(val);
1031 handle_s_without_atn(s);
1032 break;
1033 case CMD_SELATN:
1034 trace_esp_mem_writeb_cmd_selatn(val);
1035 handle_satn(s);
1036 break;
1037 case CMD_SELATNS:
1038 trace_esp_mem_writeb_cmd_selatns(val);
1039 handle_satn_stop(s);
1040 break;
1041 case CMD_ENSEL:
1042 trace_esp_mem_writeb_cmd_ensel(val);
1043 s->rregs[ESP_RINTR] = 0;
1044 break;
1045 case CMD_DISSEL:
1046 trace_esp_mem_writeb_cmd_dissel(val);
1047 s->rregs[ESP_RINTR] = 0;
1048 esp_raise_irq(s);
1049 break;
1050 default:
1051 trace_esp_error_unhandled_command(val);
1052 break;
1054 break;
1055 case ESP_WBUSID ... ESP_WSYNO:
1056 break;
1057 case ESP_CFG1:
1058 case ESP_CFG2: case ESP_CFG3:
1059 case ESP_RES3: case ESP_RES4:
1060 s->rregs[saddr] = val;
1061 break;
1062 case ESP_WCCF ... ESP_WTEST:
1063 break;
1064 default:
1065 trace_esp_error_invalid_write(val, saddr);
1066 return;
1068 s->wregs[saddr] = val;
1071 static bool esp_mem_accepts(void *opaque, hwaddr addr,
1072 unsigned size, bool is_write,
1073 MemTxAttrs attrs)
1075 return (size == 1) || (is_write && size == 4);
1078 static bool esp_is_before_version_5(void *opaque, int version_id)
1080 ESPState *s = ESP(opaque);
1082 version_id = MIN(version_id, s->mig_version_id);
1083 return version_id < 5;
1086 static bool esp_is_version_5(void *opaque, int version_id)
1088 ESPState *s = ESP(opaque);
1090 version_id = MIN(version_id, s->mig_version_id);
1091 return version_id == 5;
1094 int esp_pre_save(void *opaque)
1096 ESPState *s = ESP(object_resolve_path_component(
1097 OBJECT(opaque), "esp"));
1099 s->mig_version_id = vmstate_esp.version_id;
1100 return 0;
1103 static int esp_post_load(void *opaque, int version_id)
1105 ESPState *s = ESP(opaque);
1106 int len, i;
1108 version_id = MIN(version_id, s->mig_version_id);
1110 if (version_id < 5) {
1111 esp_set_tc(s, s->mig_dma_left);
1113 /* Migrate ti_buf to fifo */
1114 len = s->mig_ti_wptr - s->mig_ti_rptr;
1115 for (i = 0; i < len; i++) {
1116 fifo8_push(&s->fifo, s->mig_ti_buf[i]);
1119 /* Migrate cmdbuf to cmdfifo */
1120 for (i = 0; i < s->mig_cmdlen; i++) {
1121 fifo8_push(&s->cmdfifo, s->mig_cmdbuf[i]);
1125 s->mig_version_id = vmstate_esp.version_id;
1126 return 0;
1129 const VMStateDescription vmstate_esp = {
1130 .name = "esp",
1131 .version_id = 5,
1132 .minimum_version_id = 3,
1133 .post_load = esp_post_load,
1134 .fields = (VMStateField[]) {
1135 VMSTATE_BUFFER(rregs, ESPState),
1136 VMSTATE_BUFFER(wregs, ESPState),
1137 VMSTATE_INT32(ti_size, ESPState),
1138 VMSTATE_UINT32_TEST(mig_ti_rptr, ESPState, esp_is_before_version_5),
1139 VMSTATE_UINT32_TEST(mig_ti_wptr, ESPState, esp_is_before_version_5),
1140 VMSTATE_BUFFER_TEST(mig_ti_buf, ESPState, esp_is_before_version_5),
1141 VMSTATE_UINT32(status, ESPState),
1142 VMSTATE_UINT32_TEST(mig_deferred_status, ESPState,
1143 esp_is_before_version_5),
1144 VMSTATE_BOOL_TEST(mig_deferred_complete, ESPState,
1145 esp_is_before_version_5),
1146 VMSTATE_UINT32(dma, ESPState),
1147 VMSTATE_STATIC_BUFFER(mig_cmdbuf, ESPState, 0,
1148 esp_is_before_version_5, 0, 16),
1149 VMSTATE_STATIC_BUFFER(mig_cmdbuf, ESPState, 4,
1150 esp_is_before_version_5, 16,
1151 sizeof(typeof_field(ESPState, mig_cmdbuf))),
1152 VMSTATE_UINT32_TEST(mig_cmdlen, ESPState, esp_is_before_version_5),
1153 VMSTATE_UINT32(do_cmd, ESPState),
1154 VMSTATE_UINT32_TEST(mig_dma_left, ESPState, esp_is_before_version_5),
1155 VMSTATE_BOOL_TEST(data_in_ready, ESPState, esp_is_version_5),
1156 VMSTATE_UINT8_TEST(cmdfifo_cdb_offset, ESPState, esp_is_version_5),
1157 VMSTATE_FIFO8_TEST(fifo, ESPState, esp_is_version_5),
1158 VMSTATE_FIFO8_TEST(cmdfifo, ESPState, esp_is_version_5),
1159 VMSTATE_UINT8_TEST(ti_cmd, ESPState, esp_is_version_5),
1160 VMSTATE_END_OF_LIST()
1164 static void sysbus_esp_mem_write(void *opaque, hwaddr addr,
1165 uint64_t val, unsigned int size)
1167 SysBusESPState *sysbus = opaque;
1168 ESPState *s = ESP(&sysbus->esp);
1169 uint32_t saddr;
1171 saddr = addr >> sysbus->it_shift;
1172 esp_reg_write(s, saddr, val);
1175 static uint64_t sysbus_esp_mem_read(void *opaque, hwaddr addr,
1176 unsigned int size)
1178 SysBusESPState *sysbus = opaque;
1179 ESPState *s = ESP(&sysbus->esp);
1180 uint32_t saddr;
1182 saddr = addr >> sysbus->it_shift;
1183 return esp_reg_read(s, saddr);
1186 static const MemoryRegionOps sysbus_esp_mem_ops = {
1187 .read = sysbus_esp_mem_read,
1188 .write = sysbus_esp_mem_write,
1189 .endianness = DEVICE_NATIVE_ENDIAN,
1190 .valid.accepts = esp_mem_accepts,
1193 static void sysbus_esp_pdma_write(void *opaque, hwaddr addr,
1194 uint64_t val, unsigned int size)
1196 SysBusESPState *sysbus = opaque;
1197 ESPState *s = ESP(&sysbus->esp);
1198 uint32_t dmalen;
1200 trace_esp_pdma_write(size);
1202 switch (size) {
1203 case 1:
1204 esp_pdma_write(s, val);
1205 break;
1206 case 2:
1207 esp_pdma_write(s, val >> 8);
1208 esp_pdma_write(s, val);
1209 break;
1211 dmalen = esp_get_tc(s);
1212 if (dmalen == 0 || fifo8_num_free(&s->fifo) < 2) {
1213 s->pdma_cb(s);
1217 static uint64_t sysbus_esp_pdma_read(void *opaque, hwaddr addr,
1218 unsigned int size)
1220 SysBusESPState *sysbus = opaque;
1221 ESPState *s = ESP(&sysbus->esp);
1222 uint64_t val = 0;
1224 trace_esp_pdma_read(size);
1226 switch (size) {
1227 case 1:
1228 val = esp_pdma_read(s);
1229 break;
1230 case 2:
1231 val = esp_pdma_read(s);
1232 val = (val << 8) | esp_pdma_read(s);
1233 break;
1235 if (fifo8_num_used(&s->fifo) < 2) {
1236 s->pdma_cb(s);
1238 return val;
1241 static const MemoryRegionOps sysbus_esp_pdma_ops = {
1242 .read = sysbus_esp_pdma_read,
1243 .write = sysbus_esp_pdma_write,
1244 .endianness = DEVICE_NATIVE_ENDIAN,
1245 .valid.min_access_size = 1,
1246 .valid.max_access_size = 4,
1247 .impl.min_access_size = 1,
1248 .impl.max_access_size = 2,
1251 static const struct SCSIBusInfo esp_scsi_info = {
1252 .tcq = false,
1253 .max_target = ESP_MAX_DEVS,
1254 .max_lun = 7,
1256 .transfer_data = esp_transfer_data,
1257 .complete = esp_command_complete,
1258 .cancel = esp_request_cancelled
1261 static void sysbus_esp_gpio_demux(void *opaque, int irq, int level)
1263 SysBusESPState *sysbus = SYSBUS_ESP(opaque);
1264 ESPState *s = ESP(&sysbus->esp);
1266 switch (irq) {
1267 case 0:
1268 parent_esp_reset(s, irq, level);
1269 break;
1270 case 1:
1271 esp_dma_enable(opaque, irq, level);
1272 break;
1276 static void sysbus_esp_realize(DeviceState *dev, Error **errp)
1278 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1279 SysBusESPState *sysbus = SYSBUS_ESP(dev);
1280 ESPState *s = ESP(&sysbus->esp);
1282 if (!qdev_realize(DEVICE(s), NULL, errp)) {
1283 return;
1286 sysbus_init_irq(sbd, &s->irq);
1287 sysbus_init_irq(sbd, &s->irq_data);
1288 assert(sysbus->it_shift != -1);
1290 s->chip_id = TCHI_FAS100A;
1291 memory_region_init_io(&sysbus->iomem, OBJECT(sysbus), &sysbus_esp_mem_ops,
1292 sysbus, "esp-regs", ESP_REGS << sysbus->it_shift);
1293 sysbus_init_mmio(sbd, &sysbus->iomem);
1294 memory_region_init_io(&sysbus->pdma, OBJECT(sysbus), &sysbus_esp_pdma_ops,
1295 sysbus, "esp-pdma", 4);
1296 sysbus_init_mmio(sbd, &sysbus->pdma);
1298 qdev_init_gpio_in(dev, sysbus_esp_gpio_demux, 2);
1300 scsi_bus_new(&s->bus, sizeof(s->bus), dev, &esp_scsi_info, NULL);
1303 static void sysbus_esp_hard_reset(DeviceState *dev)
1305 SysBusESPState *sysbus = SYSBUS_ESP(dev);
1306 ESPState *s = ESP(&sysbus->esp);
1308 esp_hard_reset(s);
1311 static void sysbus_esp_init(Object *obj)
1313 SysBusESPState *sysbus = SYSBUS_ESP(obj);
1315 object_initialize_child(obj, "esp", &sysbus->esp, TYPE_ESP);
1318 static const VMStateDescription vmstate_sysbus_esp_scsi = {
1319 .name = "sysbusespscsi",
1320 .version_id = 2,
1321 .minimum_version_id = 1,
1322 .pre_save = esp_pre_save,
1323 .fields = (VMStateField[]) {
1324 VMSTATE_UINT8_V(esp.mig_version_id, SysBusESPState, 2),
1325 VMSTATE_STRUCT(esp, SysBusESPState, 0, vmstate_esp, ESPState),
1326 VMSTATE_END_OF_LIST()
1330 static void sysbus_esp_class_init(ObjectClass *klass, void *data)
1332 DeviceClass *dc = DEVICE_CLASS(klass);
1334 dc->realize = sysbus_esp_realize;
1335 dc->reset = sysbus_esp_hard_reset;
1336 dc->vmsd = &vmstate_sysbus_esp_scsi;
1337 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1340 static const TypeInfo sysbus_esp_info = {
1341 .name = TYPE_SYSBUS_ESP,
1342 .parent = TYPE_SYS_BUS_DEVICE,
1343 .instance_init = sysbus_esp_init,
1344 .instance_size = sizeof(SysBusESPState),
1345 .class_init = sysbus_esp_class_init,
1348 static void esp_finalize(Object *obj)
1350 ESPState *s = ESP(obj);
1352 fifo8_destroy(&s->fifo);
1353 fifo8_destroy(&s->cmdfifo);
1356 static void esp_init(Object *obj)
1358 ESPState *s = ESP(obj);
1360 fifo8_create(&s->fifo, ESP_FIFO_SZ);
1361 fifo8_create(&s->cmdfifo, ESP_CMDFIFO_SZ);
1364 static void esp_class_init(ObjectClass *klass, void *data)
1366 DeviceClass *dc = DEVICE_CLASS(klass);
1368 /* internal device for sysbusesp/pciespscsi, not user-creatable */
1369 dc->user_creatable = false;
1370 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1373 static const TypeInfo esp_info = {
1374 .name = TYPE_ESP,
1375 .parent = TYPE_DEVICE,
1376 .instance_init = esp_init,
1377 .instance_finalize = esp_finalize,
1378 .instance_size = sizeof(ESPState),
1379 .class_init = esp_class_init,
1382 static void esp_register_types(void)
1384 type_register_static(&sysbus_esp_info);
1385 type_register_static(&esp_info);
1388 type_init(esp_register_types)