blob: b70b3a5a676bf360cb9f644636a4fe2de158d6c8 [file] [log] [blame]
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +02001// (qemu-emulated) lsi53c895a boot support.
2//
3// Copyright (C) 2012 Red Hat Inc.
4//
5// Authors:
6// Gerd Hoffmann <kraxel@redhat.com>
7//
8// based on virtio-scsi.c which is written by:
9// Paolo Bonzini <pbonzini@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_VIRTIO_BLK
18#include "pci_regs.h" // PCI_VENDOR_ID
19#include "boot.h" // bootprio_find_scsi_device
20#include "blockcmd.h" // scsi_init_drive
21#include "disk.h"
22
23#define LSI_REG_DSTAT 0x0c
24#define LSI_REG_ISTAT0 0x14
25#define LSI_REG_DSP0 0x2c
26#define LSI_REG_DSP1 0x2d
27#define LSI_REG_DSP2 0x2e
28#define LSI_REG_DSP3 0x2f
29#define LSI_REG_SIST0 0x42
30#define LSI_REG_SIST1 0x43
31
32#define LSI_ISTAT0_DIP 0x01
33#define LSI_ISTAT0_SIP 0x02
34#define LSI_ISTAT0_INTF 0x04
35#define LSI_ISTAT0_CON 0x08
36#define LSI_ISTAT0_SEM 0x10
37#define LSI_ISTAT0_SIGP 0x20
38#define LSI_ISTAT0_SRST 0x40
39#define LSI_ISTAT0_ABRT 0x80
40
41struct lsi_lun_s {
42 struct drive_s drive;
43 struct pci_device *pci;
44 u32 iobase;
45 u8 target;
46 u8 lun;
47};
48
49static int
50lsi_scsi_cmd(struct lsi_lun_s *llun, struct disk_op_s *op,
51 void *cdbcmd, u16 target, u16 lun, u16 blocksize)
52{
53 u32 iobase = GET_GLOBAL(llun->iobase);
54 u32 dma = ((cdb_is_read(cdbcmd, blocksize) ? 0x01000000 : 0x00000000) |
55 (op->count * blocksize));
56 u8 msgout[] = {
57 0x80 | lun, // select lun
58 0x08,
59 };
60 u8 status = 0xff;
61 u8 msgin_tmp[2];
62 u8 msgin = 0xff;
63
64 u32 script[] = {
65 /* select target, send scsi command */
66 0x40000000 | target << 16, // select target
67 0x00000000,
68 0x06000001, // msgout
69 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgout),
70 0x02000010, // scsi command
71 (u32)MAKE_FLATPTR(GET_SEG(SS), cdbcmd),
72
73 /* handle disconnect */
74 0x87820000, // phase == msgin ?
75 0x00000018,
76 0x07000002, // msgin
77 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp),
78 0x50000000, // re-select
79 0x00000000,
80 0x07000002, // msgin
81 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp),
82
83 /* dma data, get status, raise irq */
84 dma, // dma data
85 (u32)op->buf_fl,
86 0x03000001, // status
87 (u32)MAKE_FLATPTR(GET_SEG(SS), &status),
88 0x07000001, // msgin
89 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin),
90 0x98080000, // dma irq
91 0x00000000,
92 };
93 u32 dsp = (u32)MAKE_FLATPTR(GET_SEG(SS), &script);
94
95 outb(dsp & 0xff, iobase + LSI_REG_DSP0);
96 outb((dsp >> 8) & 0xff, iobase + LSI_REG_DSP1);
97 outb((dsp >> 16) & 0xff, iobase + LSI_REG_DSP2);
98 outb((dsp >> 24) & 0xff, iobase + LSI_REG_DSP3);
99
100 for (;;) {
101 u8 dstat = inb(iobase + LSI_REG_DSTAT);
102 u8 sist0 = inb(iobase + LSI_REG_SIST0);
103 u8 sist1 = inb(iobase + LSI_REG_SIST1);
104 if (sist0 || sist1) {
105 goto fail;
106 }
107 if (dstat & 0x04) {
108 break;
109 }
110 usleep(5);
111 }
112
113 if (msgin == 0 && status == 0) {
114 return DISK_RET_SUCCESS;
115 }
116
117fail:
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200118 return DISK_RET_EBADTRACK;
119}
120
121int
122lsi_scsi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
123{
124 if (!CONFIG_LSI_SCSI)
125 return DISK_RET_EBADTRACK;
126
127 struct lsi_lun_s *llun =
128 container_of(op->drive_g, struct lsi_lun_s, drive);
129
130 return lsi_scsi_cmd(llun, op, cdbcmd,
131 GET_GLOBAL(llun->target), GET_GLOBAL(llun->lun),
132 blocksize);
133}
134
135static int
136lsi_scsi_add_lun(struct pci_device *pci, u32 iobase, u8 target, u8 lun)
137{
138 struct lsi_lun_s *llun = malloc_fseg(sizeof(*llun));
139 if (!llun) {
140 warn_noalloc();
141 return -1;
142 }
143 memset(llun, 0, sizeof(*llun));
144 llun->drive.type = DTYPE_LSI_SCSI;
145 llun->drive.cntl_id = pci->bdf;
146 llun->pci = pci;
147 llun->target = target;
148 llun->lun = lun;
149 llun->iobase = iobase;
150
151 char *name = znprintf(16, "lsi %02x:%02x.%x %d:%d",
152 pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf),
153 pci_bdf_to_fn(pci->bdf), target, lun);
154 int prio = bootprio_find_scsi_device(pci, target, lun);
155 int ret = scsi_init_drive(&llun->drive, name, prio);
156 free(name);
157 if (ret)
158 goto fail;
159 return 0;
160
161fail:
162 free(llun);
163 return -1;
164}
165
166static void
167lsi_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target)
168{
169 /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */
170 lsi_scsi_add_lun(pci, iobase, target, 0);
171}
172
173static void
174init_lsi_scsi(struct pci_device *pci)
175{
176 u16 bdf = pci->bdf;
177 u32 iobase = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0)
178 & PCI_BASE_ADDRESS_IO_MASK;
179
180 dprintf(1, "found lsi53c895a at %02x:%02x.%x, io @ %x\n",
181 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
182 pci_bdf_to_fn(bdf), iobase);
183
184 // reset
185 outb(LSI_ISTAT0_SRST, iobase + LSI_REG_ISTAT0);
186
187 int i;
188 for (i = 0; i < 7; i++)
189 lsi_scsi_scan_target(pci, iobase, i);
190
191 return;
192}
193
194void
195lsi_scsi_setup(void)
196{
197 ASSERT32FLAT();
198 if (!CONFIG_LSI_SCSI)
199 return;
200
201 dprintf(3, "init lsi53c895a\n");
202
203 struct pci_device *pci;
204 foreachpci(pci) {
205 if (pci->vendor != PCI_VENDOR_ID_LSI_LOGIC
206 || pci->device != PCI_DEVICE_ID_LSI_53C895A)
207 continue;
208 init_lsi_scsi(pci);
209 }
210}