xref: /openbmc/qemu/hw/scsi/mptendian.c (revision e7bbc9b1)
1 /*
2  * QEMU LSI SAS1068 Host Bus Adapter emulation
3  * Endianness conversion for MPI data structures
4  *
5  * Copyright (c) 2016 Red Hat, Inc.
6  *
7  * Authors: Paolo Bonzini <pbonzini@redhat.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "qemu/osdep.h"
24 #include "hw/hw.h"
25 #include "hw/pci/pci.h"
26 #include "sysemu/dma.h"
27 #include "hw/pci/msi.h"
28 #include "qemu/iov.h"
29 #include "hw/scsi/scsi.h"
30 #include "scsi/constants.h"
31 #include "trace.h"
32 
33 #include "mptsas.h"
34 #include "mpi.h"
35 
36 static void mptsas_fix_sgentry_endianness(MPISGEntry *sge)
37 {
38     sge->FlagsLength = le32_to_cpu(sge->FlagsLength);
39     if (sge->FlagsLength & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
40         sge->u.Address64 = le64_to_cpu(sge->u.Address64);
41     } else {
42         sge->u.Address32 = le32_to_cpu(sge->u.Address32);
43     }
44 }
45 
46 static void mptsas_fix_sgentry_endianness_reply(MPISGEntry *sge)
47 {
48     if (sge->FlagsLength & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
49         sge->u.Address64 = cpu_to_le64(sge->u.Address64);
50     } else {
51         sge->u.Address32 = cpu_to_le32(sge->u.Address32);
52     }
53     sge->FlagsLength = cpu_to_le32(sge->FlagsLength);
54 }
55 
56 void mptsas_fix_scsi_io_endianness(MPIMsgSCSIIORequest *req)
57 {
58     req->MsgContext = le32_to_cpu(req->MsgContext);
59     req->Control = le32_to_cpu(req->Control);
60     req->DataLength = le32_to_cpu(req->DataLength);
61     req->SenseBufferLowAddr = le32_to_cpu(req->SenseBufferLowAddr);
62 }
63 
64 void mptsas_fix_scsi_io_reply_endianness(MPIMsgSCSIIOReply *reply)
65 {
66     reply->MsgContext = cpu_to_le32(reply->MsgContext);
67     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
68     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
69     reply->TransferCount = cpu_to_le32(reply->TransferCount);
70     reply->SenseCount = cpu_to_le32(reply->SenseCount);
71     reply->ResponseInfo = cpu_to_le32(reply->ResponseInfo);
72     reply->TaskTag = cpu_to_le16(reply->TaskTag);
73 }
74 
75 void mptsas_fix_scsi_task_mgmt_endianness(MPIMsgSCSITaskMgmt *req)
76 {
77     req->MsgContext = le32_to_cpu(req->MsgContext);
78     req->TaskMsgContext = le32_to_cpu(req->TaskMsgContext);
79 }
80 
81 void mptsas_fix_scsi_task_mgmt_reply_endianness(MPIMsgSCSITaskMgmtReply *reply)
82 {
83     reply->MsgContext = cpu_to_le32(reply->MsgContext);
84     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
85     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
86     reply->TerminationCount = cpu_to_le32(reply->TerminationCount);
87 }
88 
89 void mptsas_fix_ioc_init_endianness(MPIMsgIOCInit *req)
90 {
91     req->MsgContext = le32_to_cpu(req->MsgContext);
92     req->ReplyFrameSize = le16_to_cpu(req->ReplyFrameSize);
93     req->HostMfaHighAddr = le32_to_cpu(req->HostMfaHighAddr);
94     req->SenseBufferHighAddr = le32_to_cpu(req->SenseBufferHighAddr);
95     req->ReplyFifoHostSignalingAddr =
96         le32_to_cpu(req->ReplyFifoHostSignalingAddr);
97     mptsas_fix_sgentry_endianness(&req->HostPageBufferSGE);
98     req->MsgVersion = le16_to_cpu(req->MsgVersion);
99     req->HeaderVersion = le16_to_cpu(req->HeaderVersion);
100 }
101 
102 void mptsas_fix_ioc_init_reply_endianness(MPIMsgIOCInitReply *reply)
103 {
104     reply->MsgContext = cpu_to_le32(reply->MsgContext);
105     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
106     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
107 }
108 
109 void mptsas_fix_ioc_facts_endianness(MPIMsgIOCFacts *req)
110 {
111     req->MsgContext = le32_to_cpu(req->MsgContext);
112 }
113 
114 void mptsas_fix_ioc_facts_reply_endianness(MPIMsgIOCFactsReply *reply)
115 {
116     reply->MsgVersion = cpu_to_le16(reply->MsgVersion);
117     reply->HeaderVersion = cpu_to_le16(reply->HeaderVersion);
118     reply->MsgContext = cpu_to_le32(reply->MsgContext);
119     reply->IOCExceptions = cpu_to_le16(reply->IOCExceptions);
120     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
121     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
122     reply->ReplyQueueDepth = cpu_to_le16(reply->ReplyQueueDepth);
123     reply->RequestFrameSize = cpu_to_le16(reply->RequestFrameSize);
124     reply->ProductID = cpu_to_le16(reply->ProductID);
125     reply->CurrentHostMfaHighAddr = cpu_to_le32(reply->CurrentHostMfaHighAddr);
126     reply->GlobalCredits = cpu_to_le16(reply->GlobalCredits);
127     reply->CurrentSenseBufferHighAddr =
128         cpu_to_le32(reply->CurrentSenseBufferHighAddr);
129     reply->CurReplyFrameSize = cpu_to_le16(reply->CurReplyFrameSize);
130     reply->FWImageSize = cpu_to_le32(reply->FWImageSize);
131     reply->IOCCapabilities = cpu_to_le32(reply->IOCCapabilities);
132     reply->HighPriorityQueueDepth = cpu_to_le16(reply->HighPriorityQueueDepth);
133     mptsas_fix_sgentry_endianness_reply(&reply->HostPageBufferSGE);
134     reply->ReplyFifoHostSignalingAddr =
135         cpu_to_le32(reply->ReplyFifoHostSignalingAddr);
136 }
137 
138 void mptsas_fix_config_endianness(MPIMsgConfig *req)
139 {
140     req->ExtPageLength = le16_to_cpu(req->ExtPageLength);
141     req->MsgContext = le32_to_cpu(req->MsgContext);
142     req->PageAddress = le32_to_cpu(req->PageAddress);
143     mptsas_fix_sgentry_endianness(&req->PageBufferSGE);
144 }
145 
146 void mptsas_fix_config_reply_endianness(MPIMsgConfigReply *reply)
147 {
148     reply->ExtPageLength = cpu_to_le16(reply->ExtPageLength);
149     reply->MsgContext = cpu_to_le32(reply->MsgContext);
150     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
151     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
152 }
153 
154 void mptsas_fix_port_facts_endianness(MPIMsgPortFacts *req)
155 {
156     req->MsgContext = le32_to_cpu(req->MsgContext);
157 }
158 
159 void mptsas_fix_port_facts_reply_endianness(MPIMsgPortFactsReply *reply)
160 {
161     reply->MsgContext = cpu_to_le32(reply->MsgContext);
162     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
163     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
164     reply->MaxDevices = cpu_to_le16(reply->MaxDevices);
165     reply->PortSCSIID = cpu_to_le16(reply->PortSCSIID);
166     reply->ProtocolFlags = cpu_to_le16(reply->ProtocolFlags);
167     reply->MaxPostedCmdBuffers = cpu_to_le16(reply->MaxPostedCmdBuffers);
168     reply->MaxPersistentIDs = cpu_to_le16(reply->MaxPersistentIDs);
169     reply->MaxLanBuckets = cpu_to_le16(reply->MaxLanBuckets);
170 }
171 
172 void mptsas_fix_port_enable_endianness(MPIMsgPortEnable *req)
173 {
174     req->MsgContext = le32_to_cpu(req->MsgContext);
175 }
176 
177 void mptsas_fix_port_enable_reply_endianness(MPIMsgPortEnableReply *reply)
178 {
179     reply->MsgContext = cpu_to_le32(reply->MsgContext);
180     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
181     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
182 }
183 
184 void mptsas_fix_event_notification_endianness(MPIMsgEventNotify *req)
185 {
186     req->MsgContext = le32_to_cpu(req->MsgContext);
187 }
188 
189 void mptsas_fix_event_notification_reply_endianness(MPIMsgEventNotifyReply *reply)
190 {
191     int length = reply->EventDataLength;
192     int i;
193 
194     reply->EventDataLength = cpu_to_le16(reply->EventDataLength);
195     reply->MsgContext = cpu_to_le32(reply->MsgContext);
196     reply->IOCStatus = cpu_to_le16(reply->IOCStatus);
197     reply->IOCLogInfo = cpu_to_le32(reply->IOCLogInfo);
198     reply->Event = cpu_to_le32(reply->Event);
199     reply->EventContext = cpu_to_le32(reply->EventContext);
200 
201     /* Really depends on the event kind.  This will do for now.  */
202     for (i = 0; i < length; i++) {
203         reply->Data[i] = cpu_to_le32(reply->Data[i]);
204     }
205 }
206 
207