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