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