blob: bc52a617407076c19e9f4fb369de74c8cc2cd512 [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"
Dan Williamsd044af12011-03-08 09:52:49 -080035#include "probe_roms.h"
36
Dave Jiangbf482c62011-05-25 05:04:35 +000037static efi_char16_t isci_efivar_name[] =
38 {'R', 's', 't', 'S', 'c', 'u', 'O'};
Dave Jiang8db37aa2011-02-23 00:02:24 -080039
Dan Williamsd044af12011-03-08 09:52:49 -080040struct 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;
Dan Williams3b67c1f2011-03-08 09:53:51 -080045 int j;
46 char oem_sig[4];
47 struct isci_oem_hdr oem_hdr;
48 u8 *tmp, sum;
Dan Williamsd044af12011-03-08 09:52:49 -080049
50 if (!oprom)
51 return NULL;
52
53 len = pci_biosrom_size(pdev);
54 rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
Dave Jiang2e8320f2011-03-11 14:04:43 -080055 if (!rom) {
56 dev_warn(&pdev->dev,
57 "Unable to allocate memory for orom\n");
58 return NULL;
59 }
Dan Williamsd044af12011-03-08 09:52:49 -080060
Dan Williams3b67c1f2011-03-08 09:53:51 -080061 for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) {
62 memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE);
Dan Williamsd044af12011-03-08 09:52:49 -080063
Dan Williams3b67c1f2011-03-08 09:53:51 -080064 /* 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");
Dan Williamsd044af12011-03-08 09:52:49 -0800100 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 */
124enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
125 struct isci_orom *orom, int scu_index)
126{
Dan Williamsd044af12011-03-08 09:52:49 -0800127 /* check for valid inputs */
Dave Jiang02839a8b52011-02-24 17:45:57 -0800128 if (scu_index < 0 || scu_index > SCI_MAX_CONTROLLERS ||
129 scu_index > orom->hdr.num_elements || !oem_params)
Dan Williamsd044af12011-03-08 09:52:49 -0800130 return -EINVAL;
131
Dan Williams4711ba12011-03-11 10:43:57 -0800132 oem_params->sds1 = orom->ctrl[scu_index];
Dan Williamsd044af12011-03-08 09:52:49 -0800133 return 0;
134}
135
136struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
137{
138 struct isci_orom *orom = NULL, *data;
Adam Gruchaladbb07432011-06-01 22:31:03 +0000139 int i, j;
Dan Williamsd044af12011-03-08 09:52:49 -0800140
141 if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
142 return NULL;
143
144 if (fw->size < sizeof(*orom))
145 goto out;
146
147 data = (struct isci_orom *)fw->data;
148
149 if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
150 strlen(ISCI_ROM_SIG)) != 0)
151 goto out;
152
153 orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
154 if (!orom)
155 goto out;
156
157 memcpy(orom, fw->data, fw->size);
158
Adam Gruchaladbb07432011-06-01 22:31:03 +0000159 /*
160 * deprecated: override default amp_control for pre-preproduction
161 * silicon revisions
162 */
163 if (isci_si_rev <= ISCI_SI_REVB0)
164 goto out;
165
166 for (i = 0; i < ARRAY_SIZE(orom->ctrl); i++)
167 for (j = 0; j < ARRAY_SIZE(orom->ctrl[i].phys); j++) {
168 orom->ctrl[i].phys[j].afe_tx_amp_control0 = 0xe7c03;
169 orom->ctrl[i].phys[j].afe_tx_amp_control1 = 0xe7c03;
170 orom->ctrl[i].phys[j].afe_tx_amp_control2 = 0xe7c03;
171 orom->ctrl[i].phys[j].afe_tx_amp_control3 = 0xe7c03;
172 }
Dan Williamsd044af12011-03-08 09:52:49 -0800173 out:
174 release_firmware(fw);
175
176 return orom;
177}
Dave Jiang8db37aa2011-02-23 00:02:24 -0800178
179static struct efi *get_efi(void)
180{
Dave Jiangbf482c62011-05-25 05:04:35 +0000181#ifdef CONFIG_EFI
Dave Jiang8db37aa2011-02-23 00:02:24 -0800182 return &efi;
Dave Jiangbf482c62011-05-25 05:04:35 +0000183#else
Dave Jiang8db37aa2011-02-23 00:02:24 -0800184 return NULL;
Dave Jiangbf482c62011-05-25 05:04:35 +0000185#endif
Dave Jiang8db37aa2011-02-23 00:02:24 -0800186}
187
188struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
189{
Dave Jiang8db37aa2011-02-23 00:02:24 -0800190 efi_status_t status;
Dave Jiangbf482c62011-05-25 05:04:35 +0000191 struct isci_orom *rom;
Dave Jiang2e8320f2011-03-11 14:04:43 -0800192 struct isci_oem_hdr *oem_hdr;
193 u8 *tmp, sum;
194 int j;
Dave Jiangbf482c62011-05-25 05:04:35 +0000195 ssize_t data_len;
196 u8 *efi_data;
197 u32 efi_attrib = 0;
Dave Jiang8db37aa2011-02-23 00:02:24 -0800198
Dave Jiangbf482c62011-05-25 05:04:35 +0000199 data_len = 1024;
200 efi_data = devm_kzalloc(&pdev->dev, data_len, GFP_KERNEL);
201 if (!efi_data) {
Dave Jiang8db37aa2011-02-23 00:02:24 -0800202 dev_warn(&pdev->dev,
Dave Jiangbf482c62011-05-25 05:04:35 +0000203 "Unable to allocate memory for EFI data\n");
Dave Jiang8db37aa2011-02-23 00:02:24 -0800204 return NULL;
205 }
206
Dave Jiangbf482c62011-05-25 05:04:35 +0000207 rom = (struct isci_orom *)(efi_data + sizeof(struct isci_oem_hdr));
Dave Jiang8db37aa2011-02-23 00:02:24 -0800208
209 if (get_efi())
Dave Jiangbf482c62011-05-25 05:04:35 +0000210 status = get_efi()->get_variable(isci_efivar_name,
211 &ISCI_EFI_VENDOR_GUID,
212 &efi_attrib,
213 &data_len,
214 efi_data);
Dave Jiang8db37aa2011-02-23 00:02:24 -0800215 else
216 status = EFI_NOT_FOUND;
217
Dave Jiang2e8320f2011-03-11 14:04:43 -0800218 if (status != EFI_SUCCESS) {
Dave Jiang8db37aa2011-02-23 00:02:24 -0800219 dev_warn(&pdev->dev,
Dave Jiangbf482c62011-05-25 05:04:35 +0000220 "Unable to obtain EFI var data for OEM parms\n");
Dave Jiang2e8320f2011-03-11 14:04:43 -0800221 return NULL;
222 }
Dave Jiang8db37aa2011-02-23 00:02:24 -0800223
Dave Jiangbf482c62011-05-25 05:04:35 +0000224 oem_hdr = (struct isci_oem_hdr *)efi_data;
Dave Jiang2e8320f2011-03-11 14:04:43 -0800225
226 if (memcmp(oem_hdr->sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) != 0) {
Dave Jiang8db37aa2011-02-23 00:02:24 -0800227 dev_warn(&pdev->dev,
Dave Jiang2e8320f2011-03-11 14:04:43 -0800228 "Invalid OEM header signature\n");
229 return NULL;
230 }
Dave Jiang8db37aa2011-02-23 00:02:24 -0800231
Dave Jiang2e8320f2011-03-11 14:04:43 -0800232 /* calculate checksum */
Dave Jiangbf482c62011-05-25 05:04:35 +0000233 tmp = (u8 *)efi_data;
234 for (j = 0, sum = 0; j < (sizeof(*oem_hdr) + sizeof(*rom)); j++, tmp++)
Dave Jiang2e8320f2011-03-11 14:04:43 -0800235 sum += *tmp;
236
237 if (sum != 0) {
238 dev_warn(&pdev->dev,
239 "OEM table checksum failed\n");
240 return NULL;
241 }
242
Dave Jiang2e8320f2011-03-11 14:04:43 -0800243 if (memcmp(rom->hdr.signature,
244 ISCI_ROM_SIG,
245 ISCI_ROM_SIG_SIZE) != 0) {
246 dev_warn(&pdev->dev,
247 "Invalid OEM table signature\n");
248 return NULL;
249 }
250
251 return rom;
Dave Jiang8db37aa2011-02-23 00:02:24 -0800252}