blob: 1faede6aec6f8dede373689430375764726133f4 [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{
Don Slutzf2645a82016-03-25 17:04:31 +0100121 u32 end = timer_calc(MPT_POLL_TIMEOUT);
122
123 u8 sense_buf[18];
124 struct scsi_req {
125 MptSCSIIORequest_t scsi_io;
126 MptSGEntrySimple32_t sge;
127 } __attribute__((packed, aligned(4))) req = {
128 .scsi_io = {
129 .TargetID = target,
130 .Bus = 0,
131 .Function = MPT_MESSAGE_HDR_FUNCTION_SCSI_IO_REQUEST,
132 .CDBLength = 16,
133 .SenseBufferLength = 18,
134 .MessageContext = end & 0x7fffffff,
135 .DataLength = op->count * blocksize,
136 .SenseBufferLowAddr = (u32)MAKE_FLATPTR(GET_SEG(SS), &sense_buf[0]),
137 },
138 .sge = {
139 /* end of list, simple entry, end of buffer, last element */
140 .FlagsLength = (op->count * blocksize) | 0xD1000000,
141 .DataBufferAddressLow = (u32)op->buf_fl,
142 }
143 };
144
145 req.scsi_io.LUN[1] = lun;
146 memcpy(req.scsi_io.CDB, cdb, 16);
147 if (blocksize) {
148 if (scsi_is_read(op)) {
149 req.scsi_io.Control = 2 << 24;
150 } else {
151 req.scsi_io.Control = 1 << 24;
152 req.sge.FlagsLength |= 0x04000000;
153 }
154 }
155
156 outl((u32)MAKE_FLATPTR(GET_SEG(SS), &req), iobase + MPT_REG_REQ_Q);
157
158 for (;;) {
159 if (timer_check(end)) {
160 return DISK_RET_ETIMEOUT;
161 }
162
163 u32 istatus = inl(iobase + MPT_REG_ISTATUS);
164 if (istatus & MPT_IMASK_REPLY) {
165 u32 resp = inl(iobase + MPT_REG_REP_Q);
166 /* another read to turn interrupt off */
167 inl(iobase + MPT_REG_REP_Q);
168 if (resp == req.scsi_io.MessageContext) {
169 return DISK_RET_SUCCESS;
170 } else if (resp & 0x80000000) {
171 outl((u32)&reply_msg[0], iobase + MPT_REG_REP_Q);
172 return DISK_RET_EBADTRACK;
173 }
174 }
175 usleep(50);
176 }
177}
178
179int
180mpt_scsi_process_op(struct disk_op_s *op)
181{
182 if (!CONFIG_MPT_SCSI)
183 return DISK_RET_EBADTRACK;
184
185 u8 cdbcmd[16];
186 int blocksize = scsi_fill_cmd(op, cdbcmd, sizeof(cdbcmd));
187 if (blocksize < 0)
188 return default_process_op(op);
189
190 struct mpt_lun_s *llun_gf =
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400191 container_of(op->drive_fl, struct mpt_lun_s, drive);
Don Slutzf2645a82016-03-25 17:04:31 +0100192 u16 target = GET_GLOBALFLAT(llun_gf->target);
193 u16 lun = GET_GLOBALFLAT(llun_gf->lun);
194 u32 iobase = GET_GLOBALFLAT(llun_gf->iobase);
195 return mpt_scsi_cmd(iobase, op, cdbcmd, target, lun, blocksize);
196}
197
Roman Kagancf480dd2017-04-26 17:18:07 +0300198static void
199mpt_scsi_init_lun(struct mpt_lun_s *llun, struct pci_device *pci,
200 u32 iobase, u8 target, u8 lun)
Don Slutzf2645a82016-03-25 17:04:31 +0100201{
Don Slutzf2645a82016-03-25 17:04:31 +0100202 memset(llun, 0, sizeof(*llun));
203 llun->drive.type = DTYPE_MPT_SCSI;
204 llun->drive.cntl_id = pci->bdf;
205 llun->pci = pci;
206 llun->target = target;
207 llun->lun = lun;
208 llun->iobase = iobase;
Roman Kagancf480dd2017-04-26 17:18:07 +0300209}
Don Slutzf2645a82016-03-25 17:04:31 +0100210
Roman Kagancf480dd2017-04-26 17:18:07 +0300211static int
212mpt_scsi_add_lun(u32 lun, struct drive_s *tmpl_drv)
213{
214 struct mpt_lun_s *tmpl_llun =
215 container_of(tmpl_drv, struct mpt_lun_s, drive);
216 struct mpt_lun_s *llun = malloc_fseg(sizeof(*llun));
217 if (!llun) {
218 warn_noalloc();
219 return -1;
220 }
221 mpt_scsi_init_lun(llun, tmpl_llun->pci, tmpl_llun->iobase,
222 tmpl_llun->target, lun);
223
224 char *name = znprintf(MAXDESCSIZE, "mpt %pP %d:%d",
225 llun->pci, llun->target, llun->lun);
226 int prio = bootprio_find_scsi_device(llun->pci, llun->target, llun->lun);
Don Slutzf2645a82016-03-25 17:04:31 +0100227 int ret = scsi_drive_setup(&llun->drive, name, prio);
228 free(name);
229 if (ret) {
230 goto fail;
231 }
232 return 0;
233
234fail:
235 free(llun);
236 return -1;
237}
238
239static void
240mpt_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target)
241{
Roman Kagancf480dd2017-04-26 17:18:07 +0300242 struct mpt_lun_s llun0;
243
244 mpt_scsi_init_lun(&llun0, pci, iobase, target, 0);
245
246 if (scsi_rep_luns_scan(&llun0.drive, mpt_scsi_add_lun) < 0)
247 scsi_sequential_scan(&llun0.drive, 8, mpt_scsi_add_lun);
Don Slutzf2645a82016-03-25 17:04:31 +0100248}
249
250static inline void
251mpt_out_doorbell(u8 func, u8 arg, u16 iobase)
252{
253 outl((func << 24) | (arg << 16), iobase + MPT_REG_DOORBELL);
254}
255
256static void
Kevin O'Connor79bafa12016-04-05 13:04:07 -0400257init_mpt_scsi(void *data)
Don Slutzf2645a82016-03-25 17:04:31 +0100258{
Kevin O'Connor79bafa12016-04-05 13:04:07 -0400259 struct pci_device *pci = data;
Don Slutzf2645a82016-03-25 17:04:31 +0100260 u16 *msg_in_p;
261 u32 iobase = pci_enable_iobar(pci, PCI_BASE_ADDRESS_0);
262 if (!iobase)
263 return;
264 struct MptIOCInitReply MptIOCInitReply;
265 pci_enable_busmaster(pci);
266
Kevin O'Connor79bafa12016-04-05 13:04:07 -0400267 dprintf(1, "found mpt-scsi(%04x) at %pP, io @ %x\n"
268 , pci->device, pci, iobase);
Don Slutzf2645a82016-03-25 17:04:31 +0100269
270 // reset
271 mpt_out_doorbell(MPT_DOORBELL_MSG_RESET, 0, iobase);
272 outl(MPT_IMASK_DOORBELL|MPT_IMASK_REPLY , iobase + MPT_REG_IMASK);
273 outl(0, iobase + MPT_REG_ISTATUS);
274
275 // send IOC Init message through the doorbell
276 mpt_out_doorbell(MPT_DOORBELL_HANDSHAKE,
Kevin O'Connor3fdabae2017-05-02 20:02:01 -0400277 sizeof(MptIOCInitRequest)/sizeof(u32),
278 iobase);
Don Slutzf2645a82016-03-25 17:04:31 +0100279
280 outsl(iobase + MPT_REG_DOORBELL,
Kevin O'Connor3fdabae2017-05-02 20:02:01 -0400281 (u32 *)&MptIOCInitRequest,
282 sizeof(MptIOCInitRequest)/sizeof(u32));
Don Slutzf2645a82016-03-25 17:04:31 +0100283
284 // Read the reply 16 bits at a time. Cannot use insl
285 // because the port is 32 bits wide.
286 msg_in_p = (u16 *)&MptIOCInitReply;
287 while(msg_in_p != (u16 *)(&MptIOCInitReply + 1))
288 *msg_in_p++ = (u16)inl(iobase + MPT_REG_DOORBELL);
289
290 // Eat doorbell interrupt
291 outl(0, iobase + MPT_REG_ISTATUS);
292
293 // Post reply message used for SCSI errors
294 outl((u32)&reply_msg[0], iobase + MPT_REG_REP_Q);
295
Kevin O'Connor6b76e692016-03-30 21:38:56 -0400296 int i;
297 for (i = 0; i < 7; i++)
Don Slutzf2645a82016-03-25 17:04:31 +0100298 mpt_scsi_scan_target(pci, iobase, i);
299}
300
301void
302mpt_scsi_setup(void)
303{
304 ASSERT32FLAT();
305 if (!CONFIG_MPT_SCSI || !runningOnQEMU()) {
306 return;
307 }
308
309 dprintf(3, "init MPT\n");
310
311 struct pci_device *pci;
312 foreachpci(pci) {
Kevin O'Connor79bafa12016-04-05 13:04:07 -0400313 if (pci->vendor == PCI_VENDOR_ID_LSI_LOGIC
314 && (pci->device == PCI_DEVICE_ID_LSI_53C1030
315 || pci->device == PCI_DEVICE_ID_LSI_SAS1068
316 || pci->device == PCI_DEVICE_ID_LSI_SAS1068E))
317 run_thread(init_mpt_scsi, pci);
Don Slutzf2645a82016-03-25 17:04:31 +0100318 }
319}