Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 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> |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 30 | #include <linux/efi.h> |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 31 | #include <asm/probe_roms.h> |
| 32 | |
| 33 | #include "isci.h" |
| 34 | #include "task.h" |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 35 | #include "probe_roms.h" |
| 36 | |
James Bottomley | a5ec7f86 | 2011-07-03 14:14:45 -0500 | [diff] [blame] | 37 | static efi_char16_t isci_efivar_name[] = { |
| 38 | 'R', 's', 't', 'S', 'c', 'u', 'O' |
| 39 | }; |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 40 | |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 41 | struct 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 Williams | 3b67c1f | 2011-03-08 09:53:51 -0800 | [diff] [blame] | 46 | int j; |
| 47 | char oem_sig[4]; |
| 48 | struct isci_oem_hdr oem_hdr; |
| 49 | u8 *tmp, sum; |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 50 | |
| 51 | if (!oprom) |
| 52 | return NULL; |
| 53 | |
| 54 | len = pci_biosrom_size(pdev); |
| 55 | rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL); |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 56 | if (!rom) { |
| 57 | dev_warn(&pdev->dev, |
| 58 | "Unable to allocate memory for orom\n"); |
| 59 | return NULL; |
| 60 | } |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 61 | |
Dan Williams | 3b67c1f | 2011-03-08 09:53:51 -0800 | [diff] [blame] | 62 | for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) { |
| 63 | memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE); |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 64 | |
Dan Williams | 3b67c1f | 2011-03-08 09:53:51 -0800 | [diff] [blame] | 65 | /* 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 Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (i >= len) { |
| 106 | dev_err(&pdev->dev, "oprom parse error\n"); |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 107 | rom = NULL; |
| 108 | } |
| 109 | pci_unmap_biosrom(oprom); |
| 110 | |
| 111 | return rom; |
| 112 | } |
| 113 | |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 114 | struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw) |
| 115 | { |
| 116 | struct isci_orom *orom = NULL, *data; |
Adam Gruchala | dbb0743 | 2011-06-01 22:31:03 +0000 | [diff] [blame] | 117 | int i, j; |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 118 | |
| 119 | if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0) |
| 120 | return NULL; |
| 121 | |
| 122 | if (fw->size < sizeof(*orom)) |
| 123 | goto out; |
| 124 | |
| 125 | data = (struct isci_orom *)fw->data; |
| 126 | |
| 127 | if (strncmp(ISCI_ROM_SIG, data->hdr.signature, |
| 128 | strlen(ISCI_ROM_SIG)) != 0) |
| 129 | goto out; |
| 130 | |
| 131 | orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL); |
| 132 | if (!orom) |
| 133 | goto out; |
| 134 | |
| 135 | memcpy(orom, fw->data, fw->size); |
| 136 | |
Jeff Skirvin | afd13a1 | 2012-01-04 01:32:39 -0800 | [diff] [blame] | 137 | if (is_c0(pdev) || is_c1(pdev)) |
Dan Williams | dc00c8b | 2011-07-01 11:41:21 -0700 | [diff] [blame] | 138 | goto out; |
| 139 | |
Adam Gruchala | dbb0743 | 2011-06-01 22:31:03 +0000 | [diff] [blame] | 140 | /* |
| 141 | * deprecated: override default amp_control for pre-preproduction |
| 142 | * silicon revisions |
| 143 | */ |
Adam Gruchala | dbb0743 | 2011-06-01 22:31:03 +0000 | [diff] [blame] | 144 | for (i = 0; i < ARRAY_SIZE(orom->ctrl); i++) |
| 145 | for (j = 0; j < ARRAY_SIZE(orom->ctrl[i].phys); j++) { |
| 146 | orom->ctrl[i].phys[j].afe_tx_amp_control0 = 0xe7c03; |
| 147 | orom->ctrl[i].phys[j].afe_tx_amp_control1 = 0xe7c03; |
| 148 | orom->ctrl[i].phys[j].afe_tx_amp_control2 = 0xe7c03; |
| 149 | orom->ctrl[i].phys[j].afe_tx_amp_control3 = 0xe7c03; |
| 150 | } |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 151 | out: |
| 152 | release_firmware(fw); |
| 153 | |
| 154 | return orom; |
| 155 | } |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 156 | |
| 157 | static struct efi *get_efi(void) |
| 158 | { |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 159 | #ifdef CONFIG_EFI |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 160 | return &efi; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 161 | #else |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 162 | return NULL; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 163 | #endif |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | struct isci_orom *isci_get_efi_var(struct pci_dev *pdev) |
| 167 | { |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 168 | efi_status_t status; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 169 | struct isci_orom *rom; |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 170 | struct isci_oem_hdr *oem_hdr; |
| 171 | u8 *tmp, sum; |
| 172 | int j; |
James Bottomley | a5ec7f86 | 2011-07-03 14:14:45 -0500 | [diff] [blame] | 173 | unsigned long data_len; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 174 | u8 *efi_data; |
| 175 | u32 efi_attrib = 0; |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 176 | |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 177 | data_len = 1024; |
| 178 | efi_data = devm_kzalloc(&pdev->dev, data_len, GFP_KERNEL); |
| 179 | if (!efi_data) { |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 180 | dev_warn(&pdev->dev, |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 181 | "Unable to allocate memory for EFI data\n"); |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 185 | rom = (struct isci_orom *)(efi_data + sizeof(struct isci_oem_hdr)); |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 186 | |
| 187 | if (get_efi()) |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 188 | status = get_efi()->get_variable(isci_efivar_name, |
| 189 | &ISCI_EFI_VENDOR_GUID, |
| 190 | &efi_attrib, |
| 191 | &data_len, |
| 192 | efi_data); |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 193 | else |
| 194 | status = EFI_NOT_FOUND; |
| 195 | |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 196 | if (status != EFI_SUCCESS) { |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 197 | dev_warn(&pdev->dev, |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 198 | "Unable to obtain EFI var data for OEM parms\n"); |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 201 | |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 202 | oem_hdr = (struct isci_oem_hdr *)efi_data; |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 203 | |
| 204 | if (memcmp(oem_hdr->sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) != 0) { |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 205 | dev_warn(&pdev->dev, |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 206 | "Invalid OEM header signature\n"); |
| 207 | return NULL; |
| 208 | } |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 209 | |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 210 | /* calculate checksum */ |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 211 | tmp = (u8 *)efi_data; |
| 212 | for (j = 0, sum = 0; j < (sizeof(*oem_hdr) + sizeof(*rom)); j++, tmp++) |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 213 | sum += *tmp; |
| 214 | |
| 215 | if (sum != 0) { |
| 216 | dev_warn(&pdev->dev, |
| 217 | "OEM table checksum failed\n"); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 221 | if (memcmp(rom->hdr.signature, |
| 222 | ISCI_ROM_SIG, |
| 223 | ISCI_ROM_SIG_SIZE) != 0) { |
| 224 | dev_warn(&pdev->dev, |
| 225 | "Invalid OEM table signature\n"); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | return rom; |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 230 | } |