blob: 49c74ffd17fe2db6acf63df12e17390545de3641 [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
13#include "util.h" // dprintf
14#include "pci.h" // foreachpci
15#include "config.h" // CONFIG_*
16#include "biosvar.h" // GET_GLOBAL
17#include "pci_ids.h" // PCI_DEVICE_ID
18#include "pci_regs.h" // PCI_VENDOR_ID
19#include "boot.h" // bootprio_find_scsi_device
Kevin O'Connord83c87b2013-01-21 01:14:12 -050020#include "blockcmd.h" // scsi_drive_setup
Kevin O'Connorccee6e82013-09-02 21:25:21 -040021#include "fw/paravirt.h" // runningOnQEMU
Paolo Bonzini7a39e722012-08-06 13:15:06 +020022#include "disk.h"
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040023#include "string.h" // memset
Paolo Bonzini7a39e722012-08-06 13:15:06 +020024
25#define ESP_TCLO 0x00
26#define ESP_TCMID 0x04
27#define ESP_FIFO 0x08
28#define ESP_CMD 0x0c
29#define ESP_WBUSID 0x10
30#define ESP_TCHI 0x38
31
32#define ESP_RSTAT 0x10
33#define ESP_RINTR 0x14
34#define ESP_RFLAGS 0x1c
35
36#define ESP_DMA_CMD 0x40
37#define ESP_DMA_STC 0x44
38#define ESP_DMA_SPA 0x48
39#define ESP_DMA_WBC 0x4c
40#define ESP_DMA_WAC 0x50
41#define ESP_DMA_STAT 0x54
42#define ESP_DMA_SMDLA 0x58
43#define ESP_DMA_WMAC 0x58c
44
45#define ESP_CMD_DMA 0x80
46#define ESP_CMD_RESET 0x02
47#define ESP_CMD_TI 0x10
48#define ESP_CMD_ICCS 0x11
49#define ESP_CMD_SELATN 0x42
50
51#define ESP_STAT_DI 0x01
52#define ESP_STAT_CD 0x02
53#define ESP_STAT_MSG 0x04
54#define ESP_STAT_TC 0x10
55
56#define ESP_INTR_DC 0x20
57
58struct esp_lun_s {
59 struct drive_s drive;
60 struct pci_device *pci;
61 u32 iobase;
62 u8 target;
63 u8 lun;
64};
65
66static void
67esp_scsi_dma(u32 iobase, u32 buf, u32 len, int read)
68{
69 outb(len & 0xff, iobase + ESP_TCLO);
70 outb((len >> 8) & 0xff, iobase + ESP_TCMID);
71 outb((len >> 16) & 0xff, iobase + ESP_TCHI);
72 outl(buf, iobase + ESP_DMA_SPA);
73 outl(len, iobase + ESP_DMA_STC);
74 outb(read ? 0x83 : 0x03, iobase + ESP_DMA_CMD);
75}
76
77static int
78esp_scsi_cmd(struct esp_lun_s *llun, struct disk_op_s *op,
79 u8 *cdbcmd, u16 target, u16 lun, u16 blocksize)
80{
81 u32 iobase = GET_GLOBAL(llun->iobase);
82 int i, state;
83 u8 status;
84
85 outb(target, iobase + ESP_WBUSID);
86
87 /*
88 * We need to pass the LUN at the beginning of the command, and the FIFO
89 * is only 16 bytes, so we cannot support 16-byte CDBs. The alternative
90 * would be to use DMA for the 17-byte command too, which is quite
91 * overkill.
92 */
93 outb(lun, iobase + ESP_FIFO);
94 cdbcmd[1] &= 0x1f;
95 cdbcmd[1] |= lun << 5;
96 for (i = 0; i < 12; i++)
97 outb(cdbcmd[i], iobase + ESP_FIFO);
98 outb(ESP_CMD_SELATN, iobase + ESP_CMD);
99
100 for (state = 0;;) {
101 u8 stat = inb(iobase + ESP_RSTAT);
102
103 /* Detect disconnected device. */
104 if (state == 0 && (inb(iobase + ESP_RINTR) & ESP_INTR_DC)) {
105 return DISK_RET_ENOTREADY;
106 }
107
108 /* HBA reads command, clears CD, sets TC -> do DMA if needed. */
109 if (state == 0 && (stat & ESP_STAT_TC)) {
110 state++;
111 if (op->count && blocksize) {
112 /* Data phase. */
113 u32 count = (u32)op->count * blocksize;
114 esp_scsi_dma(iobase, (u32)op->buf_fl, count,
115 cdb_is_read(cdbcmd, blocksize));
116 outb(ESP_CMD_TI | ESP_CMD_DMA, iobase + ESP_CMD);
117 continue;
118 }
119 }
120
121 /* At end of DMA TC is set again -> complete command. */
122 if (state == 1 && (stat & ESP_STAT_TC)) {
123 state++;
124 outb(ESP_CMD_ICCS, iobase + ESP_CMD);
125 continue;
126 }
127
128 /* Finally read data from the message in phase. */
129 if (state == 2 && (stat & ESP_STAT_MSG)) {
130 state++;
131 status = inb(iobase + ESP_FIFO);
132 inb(iobase + ESP_FIFO);
133 break;
134 }
135 usleep(5);
136 }
137
138 if (status == 0) {
139 return DISK_RET_SUCCESS;
140 }
141
142 return DISK_RET_EBADTRACK;
143}
144
145int
146esp_scsi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
147{
148 if (!CONFIG_ESP_SCSI)
149 return DISK_RET_EBADTRACK;
150
151 struct esp_lun_s *llun =
152 container_of(op->drive_g, struct esp_lun_s, drive);
153
154 return esp_scsi_cmd(llun, op, cdbcmd,
155 GET_GLOBAL(llun->target), GET_GLOBAL(llun->lun),
156 blocksize);
157}
158
159static int
160esp_scsi_add_lun(struct pci_device *pci, u32 iobase, u8 target, u8 lun)
161{
162 struct esp_lun_s *llun = malloc_fseg(sizeof(*llun));
163 if (!llun) {
164 warn_noalloc();
165 return -1;
166 }
167 memset(llun, 0, sizeof(*llun));
168 llun->drive.type = DTYPE_ESP_SCSI;
169 llun->drive.cntl_id = pci->bdf;
170 llun->pci = pci;
171 llun->target = target;
172 llun->lun = lun;
173 llun->iobase = iobase;
174
175 char *name = znprintf(16, "esp %02x:%02x.%x %d:%d",
176 pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf),
177 pci_bdf_to_fn(pci->bdf), target, lun);
178 int prio = bootprio_find_scsi_device(pci, target, lun);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500179 int ret = scsi_drive_setup(&llun->drive, name, prio);
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200180 free(name);
181 if (ret)
182 goto fail;
183 return 0;
184
185fail:
186 free(llun);
187 return -1;
188}
189
190static void
191esp_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target)
192{
193 esp_scsi_add_lun(pci, iobase, target, 0);
194}
195
196static void
197init_esp_scsi(struct pci_device *pci)
198{
199 u16 bdf = pci->bdf;
200 u32 iobase = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0)
201 & PCI_BASE_ADDRESS_IO_MASK;
202
203 dprintf(1, "found esp at %02x:%02x.%x, io @ %x\n",
204 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
205 pci_bdf_to_fn(bdf), iobase);
206
Paolo Bonzini68513ab2012-11-20 18:33:41 +0100207 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
208
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200209 // reset
210 outb(ESP_CMD_RESET, iobase + ESP_CMD);
211
212 int i;
213 for (i = 0; i <= 7; i++)
214 esp_scsi_scan_target(pci, iobase, i);
215
216 return;
217}
218
219void
220esp_scsi_setup(void)
221{
222 ASSERT32FLAT();
Kevin O'Connor897fb112013-02-07 23:32:48 -0500223 if (!CONFIG_ESP_SCSI || !runningOnQEMU())
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200224 return;
225
226 dprintf(3, "init esp\n");
227
228 struct pci_device *pci;
229 foreachpci(pci) {
230 if (pci->vendor != PCI_VENDOR_ID_AMD
231 || pci->device != PCI_DEVICE_ID_AMD_SCSI)
232 continue;
233 init_esp_scsi(pci);
234 }
235}