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