blob: 086a0328a1551349cba066a3d6b20365583491e9 [file] [log] [blame]
Paolo Bonzini7a39e722012-08-06 13:15:06 +02001// AMD PCscsi boot support.
2//
3// Copyright (C) 2012 Red Hat Inc.
4//
5// Authors:
6// Paolo Bonzini <pbonzini@redhat.com>
7//
8// based on lsi-scsi.c which is written by:
9// Gerd Hoffman <kraxel@redhat.com>
10//
11// This file may be distributed under the terms of the GNU LGPLv3 license.
12
Kevin O'Connor1902c942013-10-26 11:48:06 -040013#include "biosvar.h" // GET_GLOBALFLAT
Kevin O'Connor135f3f62013-09-14 23:57:26 -040014#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "blockcmd.h" // scsi_drive_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "config.h" // CONFIG_*
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040017#include "fw/paravirt.h" // runningOnQEMU
18#include "malloc.h" // free
19#include "output.h" // dprintf
Kevin O'Connor4d8510c2016-02-03 01:28:20 -050020#include "pcidevice.h" // foreachpci
Paolo Bonzini7a39e722012-08-06 13:15:06 +020021#include "pci_ids.h" // PCI_DEVICE_ID
22#include "pci_regs.h" // PCI_VENDOR_ID
Kevin O'Connor135f3f62013-09-14 23:57:26 -040023#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040024#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040025#include "util.h" // usleep
Paolo Bonzini7a39e722012-08-06 13:15:06 +020026
27#define ESP_TCLO 0x00
28#define ESP_TCMID 0x04
29#define ESP_FIFO 0x08
30#define ESP_CMD 0x0c
31#define ESP_WBUSID 0x10
32#define ESP_TCHI 0x38
33
34#define ESP_RSTAT 0x10
35#define ESP_RINTR 0x14
36#define ESP_RFLAGS 0x1c
37
38#define ESP_DMA_CMD 0x40
39#define ESP_DMA_STC 0x44
40#define ESP_DMA_SPA 0x48
41#define ESP_DMA_WBC 0x4c
42#define ESP_DMA_WAC 0x50
43#define ESP_DMA_STAT 0x54
44#define ESP_DMA_SMDLA 0x58
45#define ESP_DMA_WMAC 0x58c
46
47#define ESP_CMD_DMA 0x80
48#define ESP_CMD_RESET 0x02
49#define ESP_CMD_TI 0x10
50#define ESP_CMD_ICCS 0x11
51#define ESP_CMD_SELATN 0x42
52
53#define ESP_STAT_DI 0x01
54#define ESP_STAT_CD 0x02
55#define ESP_STAT_MSG 0x04
56#define ESP_STAT_TC 0x10
57
58#define ESP_INTR_DC 0x20
59
60struct esp_lun_s {
61 struct drive_s drive;
62 struct pci_device *pci;
63 u32 iobase;
64 u8 target;
65 u8 lun;
66};
67
68static void
69esp_scsi_dma(u32 iobase, u32 buf, u32 len, int read)
70{
71 outb(len & 0xff, iobase + ESP_TCLO);
72 outb((len >> 8) & 0xff, iobase + ESP_TCMID);
73 outb((len >> 16) & 0xff, iobase + ESP_TCHI);
74 outl(buf, iobase + ESP_DMA_SPA);
75 outl(len, iobase + ESP_DMA_STC);
76 outb(read ? 0x83 : 0x03, iobase + ESP_DMA_CMD);
77}
78
Kevin O'Connore404cad2015-07-07 11:57:50 -040079int
80esp_scsi_process_op(struct disk_op_s *op)
Paolo Bonzini7a39e722012-08-06 13:15:06 +020081{
Kevin O'Connore404cad2015-07-07 11:57:50 -040082 if (!CONFIG_ESP_SCSI)
83 return DISK_RET_EBADTRACK;
84 struct esp_lun_s *llun_gf =
85 container_of(op->drive_gf, struct esp_lun_s, drive);
86 u16 target = GET_GLOBALFLAT(llun_gf->target);
87 u16 lun = GET_GLOBALFLAT(llun_gf->lun);
88 u8 cdbcmd[16];
89 int blocksize = scsi_fill_cmd(op, cdbcmd, sizeof(cdbcmd));
90 if (blocksize < 0)
91 return default_process_op(op);
Kevin O'Connor1902c942013-10-26 11:48:06 -040092 u32 iobase = GET_GLOBALFLAT(llun_gf->iobase);
Paolo Bonzini7a39e722012-08-06 13:15:06 +020093 int i, state;
94 u8 status;
95
96 outb(target, iobase + ESP_WBUSID);
97
98 /*
99 * We need to pass the LUN at the beginning of the command, and the FIFO
100 * is only 16 bytes, so we cannot support 16-byte CDBs. The alternative
101 * would be to use DMA for the 17-byte command too, which is quite
102 * overkill.
103 */
104 outb(lun, iobase + ESP_FIFO);
105 cdbcmd[1] &= 0x1f;
106 cdbcmd[1] |= lun << 5;
107 for (i = 0; i < 12; i++)
108 outb(cdbcmd[i], iobase + ESP_FIFO);
109 outb(ESP_CMD_SELATN, iobase + ESP_CMD);
110
111 for (state = 0;;) {
112 u8 stat = inb(iobase + ESP_RSTAT);
113
114 /* Detect disconnected device. */
115 if (state == 0 && (inb(iobase + ESP_RINTR) & ESP_INTR_DC)) {
116 return DISK_RET_ENOTREADY;
117 }
118
119 /* HBA reads command, clears CD, sets TC -> do DMA if needed. */
120 if (state == 0 && (stat & ESP_STAT_TC)) {
121 state++;
122 if (op->count && blocksize) {
123 /* Data phase. */
124 u32 count = (u32)op->count * blocksize;
Kevin O'Connor5dcd1ee2015-07-07 14:43:01 -0400125 esp_scsi_dma(iobase, (u32)op->buf_fl, count, scsi_is_read(op));
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200126 outb(ESP_CMD_TI | ESP_CMD_DMA, iobase + ESP_CMD);
127 continue;
128 }
129 }
130
Kevin O'Connor028f3482014-04-11 12:08:33 -0400131 /* At end of DMA TC is set again -> complete command. */
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200132 if (state == 1 && (stat & ESP_STAT_TC)) {
133 state++;
134 outb(ESP_CMD_ICCS, iobase + ESP_CMD);
135 continue;
136 }
137
Kevin O'Connor028f3482014-04-11 12:08:33 -0400138 /* Finally read data from the message in phase. */
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200139 if (state == 2 && (stat & ESP_STAT_MSG)) {
140 state++;
141 status = inb(iobase + ESP_FIFO);
142 inb(iobase + ESP_FIFO);
143 break;
144 }
145 usleep(5);
146 }
147
148 if (status == 0) {
149 return DISK_RET_SUCCESS;
150 }
151
152 return DISK_RET_EBADTRACK;
153}
154
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200155static int
156esp_scsi_add_lun(struct pci_device *pci, u32 iobase, u8 target, u8 lun)
157{
158 struct esp_lun_s *llun = malloc_fseg(sizeof(*llun));
159 if (!llun) {
160 warn_noalloc();
161 return -1;
162 }
163 memset(llun, 0, sizeof(*llun));
164 llun->drive.type = DTYPE_ESP_SCSI;
165 llun->drive.cntl_id = pci->bdf;
166 llun->pci = pci;
167 llun->target = target;
168 llun->lun = lun;
169 llun->iobase = iobase;
170
Kevin O'Connor7b673002016-02-03 03:03:15 -0500171 char *name = znprintf(16, "esp %pP %d:%d", pci, target, lun);
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200172 int prio = bootprio_find_scsi_device(pci, target, lun);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500173 int ret = scsi_drive_setup(&llun->drive, name, prio);
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200174 free(name);
175 if (ret)
176 goto fail;
177 return 0;
178
179fail:
180 free(llun);
181 return -1;
182}
183
184static void
185esp_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target)
186{
187 esp_scsi_add_lun(pci, iobase, target, 0);
188}
189
190static void
191init_esp_scsi(struct pci_device *pci)
192{
Kevin O'Connorf51388d2016-02-02 22:17:01 -0500193 u32 iobase = pci_enable_iobar(pci, PCI_BASE_ADDRESS_0);
194 if (!iobase)
195 return;
196 pci_enable_busmaster(pci);
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200197
Kevin O'Connor7b673002016-02-03 03:03:15 -0500198 dprintf(1, "found esp at %pP, io @ %x\n", pci, iobase);
Paolo Bonzini68513ab2012-11-20 18:33:41 +0100199
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200200 // reset
201 outb(ESP_CMD_RESET, iobase + ESP_CMD);
202
203 int i;
204 for (i = 0; i <= 7; i++)
205 esp_scsi_scan_target(pci, iobase, i);
206
207 return;
208}
209
210void
211esp_scsi_setup(void)
212{
213 ASSERT32FLAT();
Kevin O'Connor897fb112013-02-07 23:32:48 -0500214 if (!CONFIG_ESP_SCSI || !runningOnQEMU())
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200215 return;
216
217 dprintf(3, "init esp\n");
218
219 struct pci_device *pci;
220 foreachpci(pci) {
221 if (pci->vendor != PCI_VENDOR_ID_AMD
222 || pci->device != PCI_DEVICE_ID_AMD_SCSI)
223 continue;
224 init_esp_scsi(pci);
225 }
226}