blob: 4e31fec46161f51c617ca92bc450cc8a71ff5f36 [file] [log] [blame]
Don Slutzf2645a82016-03-25 17:04:31 +01001// MPT Fusion boot support.
2//
3// Copyright (c) 2012 Verizon, Inc.
4// Copyright (C) 2016 Paolo Bonzini <pbonzini@redhat.com>
5//
6// This file may be distributed under the terms of the GNU LGPLv3 license.
7
8#include "biosvar.h" // GET_GLOBALFLAT
9#include "block.h" // struct drive_s
10#include "blockcmd.h" // scsi_drive_setup
11#include "config.h" // CONFIG_*
12#include "fw/paravirt.h" // runningOnQEMU
13#include "malloc.h" // free
14#include "output.h" // dprintf
15#include "pcidevice.h" // foreachpci
16#include "pci_ids.h" // PCI_DEVICE_ID
17#include "pci_regs.h" // PCI_VENDOR_ID
18#include "std/disk.h" // DISK_RET_SUCCESS
19#include "string.h" // memset
20#include "util.h" // usleep
21
22#define MPT_REG_DOORBELL 0x00
23#define MPT_REG_WRITE_SEQ 0x04
24#define MPT_REG_HOST_DIAG 0x08
25#define MPT_REG_TEST 0x0c
26#define MPT_REG_DIAG_DATA 0x10
27#define MPT_REG_DIAG_ADDR 0x14
28#define MPT_REG_ISTATUS 0x30
29#define MPT_REG_IMASK 0x34
30#define MPT_REG_REQ_Q 0x40
31#define MPT_REG_REP_Q 0x44
32
33#define MPT_DOORBELL_MSG_RESET 0x40
34#define MPT_DOORBELL_HANDSHAKE 0x42
35
36#define MPT_IMASK_DOORBELL 0x01
37#define MPT_IMASK_REPLY 0x08
38
39struct mpt_lun_s {
40 struct drive_s drive;
41 struct pci_device *pci;
42 u32 iobase;
43 u8 target;
44 u8 lun;
45};
46
47u8 reply_msg[4] __attribute((aligned(4))) VARLOW;
48
49#define MPT_MESSAGE_HDR_FUNCTION_SCSI_IO_REQUEST (0x00)
50#define MPT_MESSAGE_HDR_FUNCTION_IOC_INIT (0x02)
51
52static struct MptIOCInitRequest
53{
54 u8 WhoInit; /* Which system sent this init request. */
55 u8 Reserved1; /* Reserved */
56 u8 ChainOffset; /* Chain offset in the SG list. */
57 u8 Function; /* Function to execute. */
58 u8 Flags; /* Flags */
59 u8 MaxDevices; /* Max devices the driver can handle. */
60 u8 MaxBuses; /* Max buses the driver can handle. */
61 u8 MessageFlags; /* Message flags. */
62 u32 MessageContext; /* Message context ID. */
63 u16 ReplyFrameSize; /* Reply frame size. */
64 u16 Reserved2; /* Reserved */
65 u32 HostMfaHighAddr; /* Upper 32bit of the message frames. */
66 u32 SenseBufferHighAddr; /* Upper 32bit of the sense buffer. */
67} MptIOCInitRequest = {
68 .WhoInit = 2,
69 .Function = MPT_MESSAGE_HDR_FUNCTION_IOC_INIT,
70 .MaxDevices = 8,
71 .MaxBuses = 1,
72 .ReplyFrameSize = sizeof(reply_msg),
73 .HostMfaHighAddr = 0,
74 .SenseBufferHighAddr = 0
75};
76
77struct MptIOCInitReply {
78 u8 WhoInit; /* Which subsystem sent this init request. */
79 u8 Reserved1; /* Reserved */
80 u8 MessageLength; /* Message length */
81 u8 Function; /* Function. */
82 u8 Flags; /* Flags */
83 u8 MaxDevices; /* Maximum number of devices the driver can handle. */
84 u8 MaxBuses; /* Maximum number of busses the driver can handle. */
85 u8 MessageFlags; /* Message flags. */
86 u32 MessageContext; /* Message context ID */
87 u16 Reserved2; /* Reserved */
88 u16 IOCStatus; /* IO controller status. */
89 u32 IOCLogInfo; /* IO controller log information. */
90};
91
92typedef struct MptSCSIIORequest {
93 u8 TargetID; /* Target ID */
94 u8 Bus; /* Bus number */
95 u8 ChainOffset; /* Chain offset */
96 u8 Function; /* Function number. */
97 u8 CDBLength; /* CDB length. */
98 u8 SenseBufferLength; /* Sense buffer length. */
99 u8 Reserved; /* Reserved */
100 u8 MessageFlags; /* Message flags. */
101 u32 MessageContext; /* Message context ID. */
102 u8 LUN[8]; /* LUN */
103 u32 Control; /* Control values. */
104 u8 CDB[16]; /* The CDB. */
105 u32 DataLength; /* Data length. */
106 u32 SenseBufferLowAddr; /* Sense buffer low 32bit address. */
107} __attribute__((packed)) MptSCSIIORequest_t;
108
109#define MPT_POLL_TIMEOUT 60000
110
111typedef struct MptSGEntrySimple32 {
112 u32 FlagsLength;
113 u32 DataBufferAddressLow;
114} __attribute__((packed)) MptSGEntrySimple32_t;
115
116static int
117mpt_scsi_cmd(u32 iobase, struct disk_op_s *op,
118 u8 *cdb, u16 target, u16 lun, u16 blocksize)
119{
120 if (lun != 0)
121 return DISK_RET_ENOTREADY;
122
123 u32 end = timer_calc(MPT_POLL_TIMEOUT);
124
125 u8 sense_buf[18];
126 struct scsi_req {
127 MptSCSIIORequest_t scsi_io;
128 MptSGEntrySimple32_t sge;
129 } __attribute__((packed, aligned(4))) req = {
130 .scsi_io = {
131 .TargetID = target,
132 .Bus = 0,
133 .Function = MPT_MESSAGE_HDR_FUNCTION_SCSI_IO_REQUEST,
134 .CDBLength = 16,
135 .SenseBufferLength = 18,
136 .MessageContext = end & 0x7fffffff,
137 .DataLength = op->count * blocksize,
138 .SenseBufferLowAddr = (u32)MAKE_FLATPTR(GET_SEG(SS), &sense_buf[0]),
139 },
140 .sge = {
141 /* end of list, simple entry, end of buffer, last element */
142 .FlagsLength = (op->count * blocksize) | 0xD1000000,
143 .DataBufferAddressLow = (u32)op->buf_fl,
144 }
145 };
146
147 req.scsi_io.LUN[1] = lun;
148 memcpy(req.scsi_io.CDB, cdb, 16);
149 if (blocksize) {
150 if (scsi_is_read(op)) {
151 req.scsi_io.Control = 2 << 24;
152 } else {
153 req.scsi_io.Control = 1 << 24;
154 req.sge.FlagsLength |= 0x04000000;
155 }
156 }
157
158 outl((u32)MAKE_FLATPTR(GET_SEG(SS), &req), iobase + MPT_REG_REQ_Q);
159
160 for (;;) {
161 if (timer_check(end)) {
162 return DISK_RET_ETIMEOUT;
163 }
164
165 u32 istatus = inl(iobase + MPT_REG_ISTATUS);
166 if (istatus & MPT_IMASK_REPLY) {
167 u32 resp = inl(iobase + MPT_REG_REP_Q);
168 /* another read to turn interrupt off */
169 inl(iobase + MPT_REG_REP_Q);
170 if (resp == req.scsi_io.MessageContext) {
171 return DISK_RET_SUCCESS;
172 } else if (resp & 0x80000000) {
173 outl((u32)&reply_msg[0], iobase + MPT_REG_REP_Q);
174 return DISK_RET_EBADTRACK;
175 }
176 }
177 usleep(50);
178 }
179}
180
181int
182mpt_scsi_process_op(struct disk_op_s *op)
183{
184 if (!CONFIG_MPT_SCSI)
185 return DISK_RET_EBADTRACK;
186
187 u8 cdbcmd[16];
188 int blocksize = scsi_fill_cmd(op, cdbcmd, sizeof(cdbcmd));
189 if (blocksize < 0)
190 return default_process_op(op);
191
192 struct mpt_lun_s *llun_gf =
193 container_of(op->drive_gf, struct mpt_lun_s, drive);
194 u16 target = GET_GLOBALFLAT(llun_gf->target);
195 u16 lun = GET_GLOBALFLAT(llun_gf->lun);
196 u32 iobase = GET_GLOBALFLAT(llun_gf->iobase);
197 return mpt_scsi_cmd(iobase, op, cdbcmd, target, lun, blocksize);
198}
199
200static int
201mpt_scsi_add_lun(struct pci_device *pci, u32 iobase, u8 target, u8 lun)
202{
203 struct mpt_lun_s *llun = malloc_fseg(sizeof(*llun));
204 if (!llun) {
205 warn_noalloc();
206 return -1;
207 }
208 memset(llun, 0, sizeof(*llun));
209 llun->drive.type = DTYPE_MPT_SCSI;
210 llun->drive.cntl_id = pci->bdf;
211 llun->pci = pci;
212 llun->target = target;
213 llun->lun = lun;
214 llun->iobase = iobase;
215
216 char *name = znprintf(MAXDESCSIZE, "mpt %pP %d:%d", pci, target, lun);
217 int prio = bootprio_find_scsi_device(pci, target, lun);
218 int ret = scsi_drive_setup(&llun->drive, name, prio);
219 free(name);
220 if (ret) {
221 goto fail;
222 }
223 return 0;
224
225fail:
226 free(llun);
227 return -1;
228}
229
230static void
231mpt_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target)
232{
233 /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */
234 mpt_scsi_add_lun(pci, iobase, target, 0);
235}
236
237static inline void
238mpt_out_doorbell(u8 func, u8 arg, u16 iobase)
239{
240 outl((func << 24) | (arg << 16), iobase + MPT_REG_DOORBELL);
241}
242
243static void
244init_mpt_scsi(struct pci_device *pci, const char *dev_name)
245{
246 u16 *msg_in_p;
247 u32 iobase = pci_enable_iobar(pci, PCI_BASE_ADDRESS_0);
248 if (!iobase)
249 return;
250 struct MptIOCInitReply MptIOCInitReply;
251 pci_enable_busmaster(pci);
252
253 dprintf(1, "found %s at %pP, io @ %x\n", dev_name, pci, iobase);
254
255 // reset
256 mpt_out_doorbell(MPT_DOORBELL_MSG_RESET, 0, iobase);
257 outl(MPT_IMASK_DOORBELL|MPT_IMASK_REPLY , iobase + MPT_REG_IMASK);
258 outl(0, iobase + MPT_REG_ISTATUS);
259
260 // send IOC Init message through the doorbell
261 mpt_out_doorbell(MPT_DOORBELL_HANDSHAKE,
262 sizeof(MptIOCInitRequest)/sizeof(u32),
263 iobase);
264
265 outsl(iobase + MPT_REG_DOORBELL,
266 (u32 *)&MptIOCInitRequest,
267 sizeof(MptIOCInitRequest)/sizeof(u32));
268
269 // Read the reply 16 bits at a time. Cannot use insl
270 // because the port is 32 bits wide.
271 msg_in_p = (u16 *)&MptIOCInitReply;
272 while(msg_in_p != (u16 *)(&MptIOCInitReply + 1))
273 *msg_in_p++ = (u16)inl(iobase + MPT_REG_DOORBELL);
274
275 // Eat doorbell interrupt
276 outl(0, iobase + MPT_REG_ISTATUS);
277
278 // Post reply message used for SCSI errors
279 outl((u32)&reply_msg[0], iobase + MPT_REG_REP_Q);
280
281 for (int i = 0; i < 7; i++)
282 mpt_scsi_scan_target(pci, iobase, i);
283}
284
285void
286mpt_scsi_setup(void)
287{
288 ASSERT32FLAT();
289 if (!CONFIG_MPT_SCSI || !runningOnQEMU()) {
290 return;
291 }
292
293 dprintf(3, "init MPT\n");
294
295 struct pci_device *pci;
296 foreachpci(pci) {
297 if (pci->vendor == PCI_VENDOR_ID_LSI_LOGIC) {
298 if (pci->device == PCI_DEVICE_ID_LSI_53C1030) {
299 init_mpt_scsi(pci, "lsi53c1030");
300 }
301 if (pci->device == PCI_DEVICE_ID_LSI_SAS1068) {
302 init_mpt_scsi(pci, "sas1068");
303 }
304 if (pci->device == PCI_DEVICE_ID_LSI_SAS1068E) {
305 init_mpt_scsi(pci, "sas1068e");
306 }
307 }
308 }
309}