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