isci: move core/controller to host
[pandora-kernel.git] / drivers / scsi / isci / probe_roms.c
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  */
24
25 /* probe_roms - scan for oem parameters */
26
27 #include <linux/kernel.h>
28 #include <linux/firmware.h>
29 #include <linux/uaccess.h>
30 #include <linux/efi.h>
31 #include <asm/probe_roms.h>
32
33 #include "isci.h"
34 #include "task.h"
35 #include "probe_roms.h"
36
37 struct efi_variable {
38         efi_char16_t  VariableName[1024/sizeof(efi_char16_t)];
39         efi_guid_t    VendorGuid;
40         unsigned long DataSize;
41         __u8          Data[1024];
42         efi_status_t  Status;
43         __u32         Attributes;
44 } __attribute__((packed));
45
46 struct isci_orom *isci_request_oprom(struct pci_dev *pdev)
47 {
48         void __iomem *oprom = pci_map_biosrom(pdev);
49         struct isci_orom *rom = NULL;
50         size_t len, i;
51         int j;
52         char oem_sig[4];
53         struct isci_oem_hdr oem_hdr;
54         u8 *tmp, sum;
55
56         if (!oprom)
57                 return NULL;
58
59         len = pci_biosrom_size(pdev);
60         rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
61         if (!rom) {
62                 dev_warn(&pdev->dev,
63                          "Unable to allocate memory for orom\n");
64                 return NULL;
65         }
66
67         for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) {
68                 memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE);
69
70                 /* we think we found the OEM table */
71                 if (memcmp(oem_sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) == 0) {
72                         size_t copy_len;
73
74                         memcpy_fromio(&oem_hdr, oprom + i, sizeof(oem_hdr));
75
76                         copy_len = min(oem_hdr.len - sizeof(oem_hdr),
77                                        sizeof(*rom));
78
79                         memcpy_fromio(rom,
80                                       oprom + i + sizeof(oem_hdr),
81                                       copy_len);
82
83                         /* calculate checksum */
84                         tmp = (u8 *)&oem_hdr;
85                         for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
86                                 sum += *tmp;
87
88                         tmp = (u8 *)rom;
89                         for (j = 0; j < sizeof(*rom); j++, tmp++)
90                                 sum += *tmp;
91
92                         if (sum != 0) {
93                                 dev_warn(&pdev->dev,
94                                          "OEM table checksum failed\n");
95                                 continue;
96                         }
97
98                         /* keep going if that's not the oem param table */
99                         if (memcmp(rom->hdr.signature,
100                                    ISCI_ROM_SIG,
101                                    ISCI_ROM_SIG_SIZE) != 0)
102                                 continue;
103
104                         dev_info(&pdev->dev,
105                                  "OEM parameter table found in OROM\n");
106                         break;
107                 }
108         }
109
110         if (i >= len) {
111                 dev_err(&pdev->dev, "oprom parse error\n");
112                 devm_kfree(&pdev->dev, rom);
113                 rom = NULL;
114         }
115         pci_unmap_biosrom(oprom);
116
117         return rom;
118 }
119
120 /**
121  * isci_parse_oem_parameters() - This method will take OEM parameters
122  *    from the module init parameters and copy them to oem_params. This will
123  *    only copy values that are not set to the module parameter default values
124  * @oem_parameters: This parameter specifies the controller default OEM
125  *    parameters. It is expected that this has been initialized to the default
126  *    parameters for the controller
127  *
128  *
129  */
130 enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
131                                           struct isci_orom *orom, int scu_index)
132 {
133         /* check for valid inputs */
134         if (scu_index < 0 || scu_index > SCI_MAX_CONTROLLERS ||
135             scu_index > orom->hdr.num_elements || !oem_params)
136                 return -EINVAL;
137
138         oem_params->sds1 = orom->ctrl[scu_index];
139         return 0;
140 }
141
142 struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
143 {
144         struct isci_orom *orom = NULL, *data;
145
146         if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
147                 return NULL;
148
149         if (fw->size < sizeof(*orom))
150                 goto out;
151
152         data = (struct isci_orom *)fw->data;
153
154         if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
155                     strlen(ISCI_ROM_SIG)) != 0)
156                 goto out;
157
158         orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
159         if (!orom)
160                 goto out;
161
162         memcpy(orom, fw->data, fw->size);
163
164  out:
165         release_firmware(fw);
166
167         return orom;
168 }
169
170 static struct efi *get_efi(void)
171 {
172         #ifdef CONFIG_EFI
173         return &efi;
174         #else
175         return NULL;
176         #endif
177 }
178
179 struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
180 {
181         struct efi_variable *evar;
182         efi_status_t status;
183         struct isci_orom *rom = NULL;
184         struct isci_oem_hdr *oem_hdr;
185         u8 *tmp, sum;
186         int j;
187         size_t copy_len;
188
189         evar = devm_kzalloc(&pdev->dev,
190                             sizeof(struct efi_variable),
191                             GFP_KERNEL);
192         if (!evar) {
193                 dev_warn(&pdev->dev,
194                          "Unable to allocate memory for EFI var\n");
195                 return NULL;
196         }
197
198         rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
199         if (!rom) {
200                 dev_warn(&pdev->dev,
201                          "Unable to allocate memory for orom\n");
202                 return NULL;
203         }
204
205         for (j = 0; j < strlen(ISCI_EFI_VAR_NAME) + 1; j++)
206                 evar->VariableName[j] = ISCI_EFI_VAR_NAME[j];
207
208         evar->DataSize = 1024;
209         evar->VendorGuid = ISCI_EFI_VENDOR_GUID;
210         evar->Attributes = ISCI_EFI_ATTRIBUTES;
211
212         if (get_efi())
213                 status = get_efi()->get_variable(evar->VariableName,
214                                                  &evar->VendorGuid,
215                                                  &evar->Attributes,
216                                                  &evar->DataSize,
217                                                  evar->Data);
218         else
219                 status = EFI_NOT_FOUND;
220
221         if (status != EFI_SUCCESS) {
222                 dev_warn(&pdev->dev,
223                          "Unable to obtain EFI variable for OEM parms\n");
224                 return NULL;
225         }
226
227         oem_hdr = (struct isci_oem_hdr *)evar->Data;
228
229         if (memcmp(oem_hdr->sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) != 0) {
230                 dev_warn(&pdev->dev,
231                          "Invalid OEM header signature\n");
232                 return NULL;
233         }
234
235         /* calculate checksum */
236         tmp = (u8 *)oem_hdr;
237         for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
238                 sum += *tmp;
239
240         tmp = (u8 *)rom;
241         for (j = 0; j < sizeof(*rom); j++, tmp++)
242                 sum += *tmp;
243
244         if (sum != 0) {
245                 dev_warn(&pdev->dev,
246                          "OEM table checksum failed\n");
247                 return NULL;
248         }
249
250         copy_len = min_t(u16, evar->DataSize,
251                          min_t(u16, oem_hdr->len - sizeof(*oem_hdr), sizeof(*rom)));
252
253         memcpy(rom, (char *)evar->Data + sizeof(*oem_hdr), copy_len);
254
255         if (memcmp(rom->hdr.signature,
256                    ISCI_ROM_SIG,
257                    ISCI_ROM_SIG_SIZE) != 0) {
258                 dev_warn(&pdev->dev,
259                          "Invalid OEM table signature\n");
260                 return NULL;
261         }
262
263         return rom;
264 }