isci: Retrieve the EFI variable for OEM parameter
[cascardo/linux.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 static efi_char16_t isci_efivar_name[] =
38                         {'R', 's', 't', 'S', 'c', 'u', 'O'};
39
40 struct isci_orom *isci_request_oprom(struct pci_dev *pdev)
41 {
42         void __iomem *oprom = pci_map_biosrom(pdev);
43         struct isci_orom *rom = NULL;
44         size_t len, i;
45         int j;
46         char oem_sig[4];
47         struct isci_oem_hdr oem_hdr;
48         u8 *tmp, sum;
49
50         if (!oprom)
51                 return NULL;
52
53         len = pci_biosrom_size(pdev);
54         rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
55         if (!rom) {
56                 dev_warn(&pdev->dev,
57                          "Unable to allocate memory for orom\n");
58                 return NULL;
59         }
60
61         for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) {
62                 memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE);
63
64                 /* we think we found the OEM table */
65                 if (memcmp(oem_sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) == 0) {
66                         size_t copy_len;
67
68                         memcpy_fromio(&oem_hdr, oprom + i, sizeof(oem_hdr));
69
70                         copy_len = min(oem_hdr.len - sizeof(oem_hdr),
71                                        sizeof(*rom));
72
73                         memcpy_fromio(rom,
74                                       oprom + i + sizeof(oem_hdr),
75                                       copy_len);
76
77                         /* calculate checksum */
78                         tmp = (u8 *)&oem_hdr;
79                         for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
80                                 sum += *tmp;
81
82                         tmp = (u8 *)rom;
83                         for (j = 0; j < sizeof(*rom); j++, tmp++)
84                                 sum += *tmp;
85
86                         if (sum != 0) {
87                                 dev_warn(&pdev->dev,
88                                          "OEM table checksum failed\n");
89                                 continue;
90                         }
91
92                         /* keep going if that's not the oem param table */
93                         if (memcmp(rom->hdr.signature,
94                                    ISCI_ROM_SIG,
95                                    ISCI_ROM_SIG_SIZE) != 0)
96                                 continue;
97
98                         dev_info(&pdev->dev,
99                                  "OEM parameter table found in OROM\n");
100                         break;
101                 }
102         }
103
104         if (i >= len) {
105                 dev_err(&pdev->dev, "oprom parse error\n");
106                 devm_kfree(&pdev->dev, rom);
107                 rom = NULL;
108         }
109         pci_unmap_biosrom(oprom);
110
111         return rom;
112 }
113
114 /**
115  * isci_parse_oem_parameters() - This method will take OEM parameters
116  *    from the module init parameters and copy them to oem_params. This will
117  *    only copy values that are not set to the module parameter default values
118  * @oem_parameters: This parameter specifies the controller default OEM
119  *    parameters. It is expected that this has been initialized to the default
120  *    parameters for the controller
121  *
122  *
123  */
124 enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
125                                           struct isci_orom *orom, int scu_index)
126 {
127         /* check for valid inputs */
128         if (scu_index < 0 || scu_index > SCI_MAX_CONTROLLERS ||
129             scu_index > orom->hdr.num_elements || !oem_params)
130                 return -EINVAL;
131
132         oem_params->sds1 = orom->ctrl[scu_index];
133         return 0;
134 }
135
136 struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
137 {
138         struct isci_orom *orom = NULL, *data;
139
140         if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
141                 return NULL;
142
143         if (fw->size < sizeof(*orom))
144                 goto out;
145
146         data = (struct isci_orom *)fw->data;
147
148         if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
149                     strlen(ISCI_ROM_SIG)) != 0)
150                 goto out;
151
152         orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
153         if (!orom)
154                 goto out;
155
156         memcpy(orom, fw->data, fw->size);
157
158  out:
159         release_firmware(fw);
160
161         return orom;
162 }
163
164 static struct efi *get_efi(void)
165 {
166 #ifdef CONFIG_EFI
167         return &efi;
168 #else
169         return NULL;
170 #endif
171 }
172
173 struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
174 {
175         efi_status_t status;
176         struct isci_orom *rom;
177         struct isci_oem_hdr *oem_hdr;
178         u8 *tmp, sum;
179         int j;
180         ssize_t data_len;
181         u8 *efi_data;
182         u32 efi_attrib = 0;
183
184         data_len = 1024;
185         efi_data = devm_kzalloc(&pdev->dev, data_len, GFP_KERNEL);
186         if (!efi_data) {
187                 dev_warn(&pdev->dev,
188                          "Unable to allocate memory for EFI data\n");
189                 return NULL;
190         }
191
192         rom = (struct isci_orom *)(efi_data + sizeof(struct isci_oem_hdr));
193
194         if (get_efi())
195                 status = get_efi()->get_variable(isci_efivar_name,
196                                                  &ISCI_EFI_VENDOR_GUID,
197                                                  &efi_attrib,
198                                                  &data_len,
199                                                  efi_data);
200         else
201                 status = EFI_NOT_FOUND;
202
203         if (status != EFI_SUCCESS) {
204                 dev_warn(&pdev->dev,
205                          "Unable to obtain EFI var data for OEM parms\n");
206                 return NULL;
207         }
208
209         oem_hdr = (struct isci_oem_hdr *)efi_data;
210
211         if (memcmp(oem_hdr->sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) != 0) {
212                 dev_warn(&pdev->dev,
213                          "Invalid OEM header signature\n");
214                 return NULL;
215         }
216
217         /* calculate checksum */
218         tmp = (u8 *)efi_data;
219         for (j = 0, sum = 0; j < (sizeof(*oem_hdr) + sizeof(*rom)); j++, tmp++)
220                 sum += *tmp;
221
222         if (sum != 0) {
223                 dev_warn(&pdev->dev,
224                          "OEM table checksum failed\n");
225                 return NULL;
226         }
227
228         if (memcmp(rom->hdr.signature,
229                    ISCI_ROM_SIG,
230                    ISCI_ROM_SIG_SIZE) != 0) {
231                 dev_warn(&pdev->dev,
232                          "Invalid OEM table signature\n");
233                 return NULL;
234         }
235
236         return rom;
237 }