blob: b5f4341de2434ead30763b4228e49ff5ee2edc67 [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
James Bottomleya5ec7f862011-07-03 14:14:45 -050037static efi_char16_t isci_efivar_name[] = {
38 'R', 's', 't', 'S', 'c', 'u', 'O'
39};
Dave Jiang8db37aa2011-02-23 00:02:24 -080040
Dan Williamsd044af12011-03-08 09:52:49 -080041struct isci_orom *isci_request_oprom(struct pci_dev *pdev)
42{
43 void __iomem *oprom = pci_map_biosrom(pdev);
44 struct isci_orom *rom = NULL;
45 size_t len, i;
Dan Williams3b67c1f2011-03-08 09:53:51 -080046 int j;
47 char oem_sig[4];
48 struct isci_oem_hdr oem_hdr;
49 u8 *tmp, sum;
Dan Williamsd044af12011-03-08 09:52:49 -080050
51 if (!oprom)
52 return NULL;
53
54 len = pci_biosrom_size(pdev);
55 rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
Dave Jiang2e8320f2011-03-11 14:04:43 -080056 if (!rom) {
57 dev_warn(&pdev->dev,
58 "Unable to allocate memory for orom\n");
59 return NULL;
60 }
Dan Williamsd044af12011-03-08 09:52:49 -080061
Dan Williams3b67c1f2011-03-08 09:53:51 -080062 for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) {
63 memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE);
Dan Williamsd044af12011-03-08 09:52:49 -080064
Dan Williams3b67c1f2011-03-08 09:53:51 -080065 /* we think we found the OEM table */
66 if (memcmp(oem_sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) == 0) {
67 size_t copy_len;
68
69 memcpy_fromio(&oem_hdr, oprom + i, sizeof(oem_hdr));
70
71 copy_len = min(oem_hdr.len - sizeof(oem_hdr),
72 sizeof(*rom));
73
74 memcpy_fromio(rom,
75 oprom + i + sizeof(oem_hdr),
76 copy_len);
77
78 /* calculate checksum */
79 tmp = (u8 *)&oem_hdr;
80 for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
81 sum += *tmp;
82
83 tmp = (u8 *)rom;
84 for (j = 0; j < sizeof(*rom); j++, tmp++)
85 sum += *tmp;
86
87 if (sum != 0) {
88 dev_warn(&pdev->dev,
89 "OEM table checksum failed\n");
90 continue;
91 }
92
93 /* keep going if that's not the oem param table */
94 if (memcmp(rom->hdr.signature,
95 ISCI_ROM_SIG,
96 ISCI_ROM_SIG_SIZE) != 0)
97 continue;
98
99 dev_info(&pdev->dev,
100 "OEM parameter table found in OROM\n");
Dan Williamsd044af12011-03-08 09:52:49 -0800101 break;
102 }
103 }
104
105 if (i >= len) {
106 dev_err(&pdev->dev, "oprom parse error\n");
107 devm_kfree(&pdev->dev, rom);
108 rom = NULL;
109 }
110 pci_unmap_biosrom(oprom);
111
112 return rom;
113}
114
Dan Williams89a73012011-06-30 19:14:33 -0700115enum sci_status isci_parse_oem_parameters(struct sci_oem_params *oem,
Dan Williamsd044af12011-03-08 09:52:49 -0800116 struct isci_orom *orom, int scu_index)
117{
Dan Williamsd044af12011-03-08 09:52:49 -0800118 /* check for valid inputs */
Maciej Patelczyk7cafbf12011-06-21 22:03:13 +0000119 if (scu_index < 0 || scu_index >= SCI_MAX_CONTROLLERS ||
Dan Williams89a73012011-06-30 19:14:33 -0700120 scu_index > orom->hdr.num_elements || !oem)
Dan Williamsd044af12011-03-08 09:52:49 -0800121 return -EINVAL;
122
Dan Williams89a73012011-06-30 19:14:33 -0700123 *oem = orom->ctrl[scu_index];
Dan Williamsd044af12011-03-08 09:52:49 -0800124 return 0;
125}
126
127struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
128{
129 struct isci_orom *orom = NULL, *data;
Adam Gruchaladbb07432011-06-01 22:31:03 +0000130 int i, j;
Dan Williamsd044af12011-03-08 09:52:49 -0800131
132 if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
133 return NULL;
134
135 if (fw->size < sizeof(*orom))
136 goto out;
137
138 data = (struct isci_orom *)fw->data;
139
140 if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
141 strlen(ISCI_ROM_SIG)) != 0)
142 goto out;
143
144 orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
145 if (!orom)
146 goto out;
147
148 memcpy(orom, fw->data, fw->size);
149
Dan Williamsdc00c8b2011-07-01 11:41:21 -0700150 if (is_c0(pdev))
151 goto out;
152
Adam Gruchaladbb07432011-06-01 22:31:03 +0000153 /*
154 * deprecated: override default amp_control for pre-preproduction
155 * silicon revisions
156 */
Adam Gruchaladbb07432011-06-01 22:31:03 +0000157 for (i = 0; i < ARRAY_SIZE(orom->ctrl); i++)
158 for (j = 0; j < ARRAY_SIZE(orom->ctrl[i].phys); j++) {
159 orom->ctrl[i].phys[j].afe_tx_amp_control0 = 0xe7c03;
160 orom->ctrl[i].phys[j].afe_tx_amp_control1 = 0xe7c03;
161 orom->ctrl[i].phys[j].afe_tx_amp_control2 = 0xe7c03;
162 orom->ctrl[i].phys[j].afe_tx_amp_control3 = 0xe7c03;
163 }
Dan Williamsd044af12011-03-08 09:52:49 -0800164 out:
165 release_firmware(fw);
166
167 return orom;
168}
Dave Jiang8db37aa2011-02-23 00:02:24 -0800169
170static struct efi *get_efi(void)
171{
Dave Jiangbf482c62011-05-25 05:04:35 +0000172#ifdef CONFIG_EFI
Dave Jiang8db37aa2011-02-23 00:02:24 -0800173 return &efi;
Dave Jiangbf482c62011-05-25 05:04:35 +0000174#else
Dave Jiang8db37aa2011-02-23 00:02:24 -0800175 return NULL;
Dave Jiangbf482c62011-05-25 05:04:35 +0000176#endif
Dave Jiang8db37aa2011-02-23 00:02:24 -0800177}
178
179struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
180{
Dave Jiang8db37aa2011-02-23 00:02:24 -0800181 efi_status_t status;
Dave Jiangbf482c62011-05-25 05:04:35 +0000182 struct isci_orom *rom;
Dave Jiang2e8320f2011-03-11 14:04:43 -0800183 struct isci_oem_hdr *oem_hdr;
184 u8 *tmp, sum;
185 int j;
James Bottomleya5ec7f862011-07-03 14:14:45 -0500186 unsigned long data_len;
Dave Jiangbf482c62011-05-25 05:04:35 +0000187 u8 *efi_data;
188 u32 efi_attrib = 0;
Dave Jiang8db37aa2011-02-23 00:02:24 -0800189
Dave Jiangbf482c62011-05-25 05:04:35 +0000190 data_len = 1024;
191 efi_data = devm_kzalloc(&pdev->dev, data_len, GFP_KERNEL);
192 if (!efi_data) {
Dave Jiang8db37aa2011-02-23 00:02:24 -0800193 dev_warn(&pdev->dev,
Dave Jiangbf482c62011-05-25 05:04:35 +0000194 "Unable to allocate memory for EFI data\n");
Dave Jiang8db37aa2011-02-23 00:02:24 -0800195 return NULL;
196 }
197
Dave Jiangbf482c62011-05-25 05:04:35 +0000198 rom = (struct isci_orom *)(efi_data + sizeof(struct isci_oem_hdr));
Dave Jiang8db37aa2011-02-23 00:02:24 -0800199
200 if (get_efi())
Dave Jiangbf482c62011-05-25 05:04:35 +0000201 status = get_efi()->get_variable(isci_efivar_name,
202 &ISCI_EFI_VENDOR_GUID,
203 &efi_attrib,
204 &data_len,
205 efi_data);
Dave Jiang8db37aa2011-02-23 00:02:24 -0800206 else
207 status = EFI_NOT_FOUND;
208
Dave Jiang2e8320f2011-03-11 14:04:43 -0800209 if (status != EFI_SUCCESS) {
Dave Jiang8db37aa2011-02-23 00:02:24 -0800210 dev_warn(&pdev->dev,
Dave Jiangbf482c62011-05-25 05:04:35 +0000211 "Unable to obtain EFI var data for OEM parms\n");
Dave Jiang2e8320f2011-03-11 14:04:43 -0800212 return NULL;
213 }
Dave Jiang8db37aa2011-02-23 00:02:24 -0800214
Dave Jiangbf482c62011-05-25 05:04:35 +0000215 oem_hdr = (struct isci_oem_hdr *)efi_data;
Dave Jiang2e8320f2011-03-11 14:04:43 -0800216
217 if (memcmp(oem_hdr->sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) != 0) {
Dave Jiang8db37aa2011-02-23 00:02:24 -0800218 dev_warn(&pdev->dev,
Dave Jiang2e8320f2011-03-11 14:04:43 -0800219 "Invalid OEM header signature\n");
220 return NULL;
221 }
Dave Jiang8db37aa2011-02-23 00:02:24 -0800222
Dave Jiang2e8320f2011-03-11 14:04:43 -0800223 /* calculate checksum */
Dave Jiangbf482c62011-05-25 05:04:35 +0000224 tmp = (u8 *)efi_data;
225 for (j = 0, sum = 0; j < (sizeof(*oem_hdr) + sizeof(*rom)); j++, tmp++)
Dave Jiang2e8320f2011-03-11 14:04:43 -0800226 sum += *tmp;
227
228 if (sum != 0) {
229 dev_warn(&pdev->dev,
230 "OEM table checksum failed\n");
231 return NULL;
232 }
233
Dave Jiang2e8320f2011-03-11 14:04:43 -0800234 if (memcmp(rom->hdr.signature,
235 ISCI_ROM_SIG,
236 ISCI_ROM_SIG_SIZE) != 0) {
237 dev_warn(&pdev->dev,
238 "Invalid OEM table signature\n");
239 return NULL;
240 }
241
242 return rom;
Dave Jiang8db37aa2011-02-23 00:02:24 -0800243}