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 | |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 37 | static efi_char16_t isci_efivar_name[] = |
| 38 | {'R', 's', 't', 'S', 'c', 'u', 'O'}; |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 39 | |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 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; |
Dan Williams | 3b67c1f | 2011-03-08 09:53:51 -0800 | [diff] [blame] | 45 | int j; |
| 46 | char oem_sig[4]; |
| 47 | struct isci_oem_hdr oem_hdr; |
| 48 | u8 *tmp, sum; |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 49 | |
| 50 | if (!oprom) |
| 51 | return NULL; |
| 52 | |
| 53 | len = pci_biosrom_size(pdev); |
| 54 | rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL); |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 55 | if (!rom) { |
| 56 | dev_warn(&pdev->dev, |
| 57 | "Unable to allocate memory for orom\n"); |
| 58 | return NULL; |
| 59 | } |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 60 | |
Dan Williams | 3b67c1f | 2011-03-08 09:53:51 -0800 | [diff] [blame] | 61 | for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) { |
| 62 | memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE); |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 63 | |
Dan Williams | 3b67c1f | 2011-03-08 09:53:51 -0800 | [diff] [blame] | 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"); |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 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 | |
Dan Williams | 89a7301 | 2011-06-30 19:14:33 -0700 | [diff] [blame^] | 114 | enum sci_status isci_parse_oem_parameters(struct sci_oem_params *oem, |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 115 | struct isci_orom *orom, int scu_index) |
| 116 | { |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 117 | /* check for valid inputs */ |
Maciej Patelczyk | 7cafbf1 | 2011-06-21 22:03:13 +0000 | [diff] [blame] | 118 | if (scu_index < 0 || scu_index >= SCI_MAX_CONTROLLERS || |
Dan Williams | 89a7301 | 2011-06-30 19:14:33 -0700 | [diff] [blame^] | 119 | scu_index > orom->hdr.num_elements || !oem) |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | |
Dan Williams | 89a7301 | 2011-06-30 19:14:33 -0700 | [diff] [blame^] | 122 | *oem = orom->ctrl[scu_index]; |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw) |
| 127 | { |
| 128 | struct isci_orom *orom = NULL, *data; |
Adam Gruchala | dbb0743 | 2011-06-01 22:31:03 +0000 | [diff] [blame] | 129 | int i, j; |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 130 | |
| 131 | if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0) |
| 132 | return NULL; |
| 133 | |
| 134 | if (fw->size < sizeof(*orom)) |
| 135 | goto out; |
| 136 | |
| 137 | data = (struct isci_orom *)fw->data; |
| 138 | |
| 139 | if (strncmp(ISCI_ROM_SIG, data->hdr.signature, |
| 140 | strlen(ISCI_ROM_SIG)) != 0) |
| 141 | goto out; |
| 142 | |
| 143 | orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL); |
| 144 | if (!orom) |
| 145 | goto out; |
| 146 | |
| 147 | memcpy(orom, fw->data, fw->size); |
| 148 | |
Adam Gruchala | dbb0743 | 2011-06-01 22:31:03 +0000 | [diff] [blame] | 149 | /* |
| 150 | * deprecated: override default amp_control for pre-preproduction |
| 151 | * silicon revisions |
| 152 | */ |
| 153 | if (isci_si_rev <= ISCI_SI_REVB0) |
| 154 | goto out; |
| 155 | |
| 156 | for (i = 0; i < ARRAY_SIZE(orom->ctrl); i++) |
| 157 | for (j = 0; j < ARRAY_SIZE(orom->ctrl[i].phys); j++) { |
| 158 | orom->ctrl[i].phys[j].afe_tx_amp_control0 = 0xe7c03; |
| 159 | orom->ctrl[i].phys[j].afe_tx_amp_control1 = 0xe7c03; |
| 160 | orom->ctrl[i].phys[j].afe_tx_amp_control2 = 0xe7c03; |
| 161 | orom->ctrl[i].phys[j].afe_tx_amp_control3 = 0xe7c03; |
| 162 | } |
Dan Williams | d044af1 | 2011-03-08 09:52:49 -0800 | [diff] [blame] | 163 | out: |
| 164 | release_firmware(fw); |
| 165 | |
| 166 | return orom; |
| 167 | } |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 168 | |
| 169 | static struct efi *get_efi(void) |
| 170 | { |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 171 | #ifdef CONFIG_EFI |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 172 | return &efi; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 173 | #else |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 174 | return NULL; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 175 | #endif |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | struct isci_orom *isci_get_efi_var(struct pci_dev *pdev) |
| 179 | { |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 180 | efi_status_t status; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 181 | struct isci_orom *rom; |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 182 | struct isci_oem_hdr *oem_hdr; |
| 183 | u8 *tmp, sum; |
| 184 | int j; |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 185 | ssize_t data_len; |
| 186 | u8 *efi_data; |
| 187 | u32 efi_attrib = 0; |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 188 | |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 189 | data_len = 1024; |
| 190 | efi_data = devm_kzalloc(&pdev->dev, data_len, GFP_KERNEL); |
| 191 | if (!efi_data) { |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 192 | dev_warn(&pdev->dev, |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 193 | "Unable to allocate memory for EFI data\n"); |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 194 | return NULL; |
| 195 | } |
| 196 | |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 197 | rom = (struct isci_orom *)(efi_data + sizeof(struct isci_oem_hdr)); |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 198 | |
| 199 | if (get_efi()) |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 200 | status = get_efi()->get_variable(isci_efivar_name, |
| 201 | &ISCI_EFI_VENDOR_GUID, |
| 202 | &efi_attrib, |
| 203 | &data_len, |
| 204 | efi_data); |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 205 | else |
| 206 | status = EFI_NOT_FOUND; |
| 207 | |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 208 | if (status != EFI_SUCCESS) { |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 209 | dev_warn(&pdev->dev, |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 210 | "Unable to obtain EFI var data for OEM parms\n"); |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 211 | return NULL; |
| 212 | } |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 213 | |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 214 | oem_hdr = (struct isci_oem_hdr *)efi_data; |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 215 | |
| 216 | 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] | 217 | dev_warn(&pdev->dev, |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 218 | "Invalid OEM header signature\n"); |
| 219 | return NULL; |
| 220 | } |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 221 | |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 222 | /* calculate checksum */ |
Dave Jiang | bf482c6 | 2011-05-25 05:04:35 +0000 | [diff] [blame] | 223 | tmp = (u8 *)efi_data; |
| 224 | 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] | 225 | sum += *tmp; |
| 226 | |
| 227 | if (sum != 0) { |
| 228 | dev_warn(&pdev->dev, |
| 229 | "OEM table checksum failed\n"); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
Dave Jiang | 2e8320f | 2011-03-11 14:04:43 -0800 | [diff] [blame] | 233 | if (memcmp(rom->hdr.signature, |
| 234 | ISCI_ROM_SIG, |
| 235 | ISCI_ROM_SIG_SIZE) != 0) { |
| 236 | dev_warn(&pdev->dev, |
| 237 | "Invalid OEM table signature\n"); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | return rom; |
Dave Jiang | 8db37aa | 2011-02-23 00:02:24 -0800 | [diff] [blame] | 242 | } |