blob: 64e9a808c814758f2d917c7d943c10c88a55633f [file] [log] [blame]
Dan Williamsd044af12011-03-08 09:52:49 -08001/*
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>
Dave Jiang8db37aa2011-02-23 00:02:24 -080030#include <linux/efi.h>
Dan Williamsd044af12011-03-08 09:52:49 -080031#include <asm/probe_roms.h>
32
33#include "isci.h"
34#include "task.h"
35#include "sci_controller_constants.h"
36#include "scic_remote_device.h"
37#include "sci_environment.h"
38#include "probe_roms.h"
39
Dave Jiang8db37aa2011-02-23 00:02:24 -080040struct efi_variable {
41 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
42 efi_guid_t VendorGuid;
43 unsigned long DataSize;
44 __u8 Data[1024];
45 efi_status_t Status;
46 __u32 Attributes;
47} __attribute__((packed));
48
Dan Williamsd044af12011-03-08 09:52:49 -080049struct isci_orom *isci_request_oprom(struct pci_dev *pdev)
50{
51 void __iomem *oprom = pci_map_biosrom(pdev);
52 struct isci_orom *rom = NULL;
53 size_t len, i;
Dan Williams3b67c1f2011-03-08 09:53:51 -080054 int j;
55 char oem_sig[4];
56 struct isci_oem_hdr oem_hdr;
57 u8 *tmp, sum;
Dan Williamsd044af12011-03-08 09:52:49 -080058
59 if (!oprom)
60 return NULL;
61
62 len = pci_biosrom_size(pdev);
63 rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
64
Dan Williams3b67c1f2011-03-08 09:53:51 -080065 for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) {
66 memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE);
Dan Williamsd044af12011-03-08 09:52:49 -080067
Dan Williams3b67c1f2011-03-08 09:53:51 -080068 /* we think we found the OEM table */
69 if (memcmp(oem_sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) == 0) {
70 size_t copy_len;
71
72 memcpy_fromio(&oem_hdr, oprom + i, sizeof(oem_hdr));
73
74 copy_len = min(oem_hdr.len - sizeof(oem_hdr),
75 sizeof(*rom));
76
77 memcpy_fromio(rom,
78 oprom + i + sizeof(oem_hdr),
79 copy_len);
80
81 /* calculate checksum */
82 tmp = (u8 *)&oem_hdr;
83 for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
84 sum += *tmp;
85
86 tmp = (u8 *)rom;
87 for (j = 0; j < sizeof(*rom); j++, tmp++)
88 sum += *tmp;
89
90 if (sum != 0) {
91 dev_warn(&pdev->dev,
92 "OEM table checksum failed\n");
93 continue;
94 }
95
96 /* keep going if that's not the oem param table */
97 if (memcmp(rom->hdr.signature,
98 ISCI_ROM_SIG,
99 ISCI_ROM_SIG_SIZE) != 0)
100 continue;
101
102 dev_info(&pdev->dev,
103 "OEM parameter table found in OROM\n");
Dan Williamsd044af12011-03-08 09:52:49 -0800104 break;
105 }
106 }
107
108 if (i >= len) {
109 dev_err(&pdev->dev, "oprom parse error\n");
110 devm_kfree(&pdev->dev, rom);
111 rom = NULL;
112 }
113 pci_unmap_biosrom(oprom);
114
115 return rom;
116}
117
118/**
119 * isci_parse_oem_parameters() - This method will take OEM parameters
120 * from the module init parameters and copy them to oem_params. This will
121 * only copy values that are not set to the module parameter default values
122 * @oem_parameters: This parameter specifies the controller default OEM
123 * parameters. It is expected that this has been initialized to the default
124 * parameters for the controller
125 *
126 *
127 */
128enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
129 struct isci_orom *orom, int scu_index)
130{
Dan Williamsd044af12011-03-08 09:52:49 -0800131 /* check for valid inputs */
Dave Jiang02839a8b52011-02-24 17:45:57 -0800132 if (scu_index < 0 || scu_index > SCI_MAX_CONTROLLERS ||
133 scu_index > orom->hdr.num_elements || !oem_params)
Dan Williamsd044af12011-03-08 09:52:49 -0800134 return -EINVAL;
135
Dave Jiang02839a8b52011-02-24 17:45:57 -0800136 memcpy(oem_params,
137 &orom->ctrl[scu_index],
138 sizeof(struct scic_sds_oem_params));
Dan Williamsd044af12011-03-08 09:52:49 -0800139
140 return 0;
141}
142
143struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
144{
145 struct isci_orom *orom = NULL, *data;
146
147 if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
148 return NULL;
149
150 if (fw->size < sizeof(*orom))
151 goto out;
152
153 data = (struct isci_orom *)fw->data;
154
155 if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
156 strlen(ISCI_ROM_SIG)) != 0)
157 goto out;
158
159 orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
160 if (!orom)
161 goto out;
162
163 memcpy(orom, fw->data, fw->size);
164
165 out:
166 release_firmware(fw);
167
168 return orom;
169}
Dave Jiang8db37aa2011-02-23 00:02:24 -0800170
171static struct efi *get_efi(void)
172{
173 #ifdef CONFIG_EFI
174 return &efi;
175 #else
176 return NULL;
177 #endif
178}
179
180struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
181{
182 struct efi_variable *evar;
183 efi_status_t status;
184 struct isci_orom *orom = NULL;
185
186 evar = devm_kzalloc(&pdev->dev,
187 sizeof(struct efi_variable),
188 GFP_KERNEL);
189 if (!evar) {
190 dev_warn(&pdev->dev,
191 "Unable to allocate memory for EFI var\n");
192 return NULL;
193 }
194
195 evar->DataSize = 1024;
196 evar->VendorGuid = ISCI_EFI_VENDOR_GUID;
197 evar->Attributes = ISCI_EFI_ATTRIBUTES;
198
199 if (get_efi())
200 status = get_efi()->get_variable(evar->VariableName,
201 &evar->VendorGuid,
202 &evar->Attributes,
203 &evar->DataSize,
204 evar->Data);
205 else
206 status = EFI_NOT_FOUND;
207
208 if (status == EFI_SUCCESS)
209 orom = (struct isci_orom *)evar->Data;
210 else
211 dev_warn(&pdev->dev,
212 "Unable to obtain EFI variable for OEM parms\n");
213
214 if (orom && memcmp(orom->hdr.signature, ISCI_ROM_SIG,
215 strlen(ISCI_ROM_SIG)) != 0)
216 dev_warn(&pdev->dev,
217 "Verifying OROM signature failed\n");
218
219 if (!orom)
220 devm_kfree(&pdev->dev, evar);
221
222 return orom;
223}