probe_roms.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /* probe_roms - scan for oem parameters */
  25. #include <linux/kernel.h>
  26. #include <linux/firmware.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/efi.h>
  29. #include <asm/probe_roms.h>
  30. #include "isci.h"
  31. #include "task.h"
  32. #include "probe_roms.h"
  33. static efi_char16_t isci_efivar_name[] = {
  34. 'R', 's', 't', 'S', 'c', 'u', 'O'
  35. };
  36. struct isci_orom *isci_request_oprom(struct pci_dev *pdev)
  37. {
  38. void __iomem *oprom = pci_map_biosrom(pdev);
  39. struct isci_orom *rom = NULL;
  40. size_t len, i;
  41. int j;
  42. char oem_sig[4];
  43. struct isci_oem_hdr oem_hdr;
  44. u8 *tmp, sum;
  45. if (!oprom)
  46. return NULL;
  47. len = pci_biosrom_size(pdev);
  48. rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
  49. if (!rom) {
  50. pci_unmap_biosrom(oprom);
  51. dev_warn(&pdev->dev,
  52. "Unable to allocate memory for orom\n");
  53. return NULL;
  54. }
  55. for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) {
  56. memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE);
  57. /* we think we found the OEM table */
  58. if (memcmp(oem_sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) == 0) {
  59. size_t copy_len;
  60. memcpy_fromio(&oem_hdr, oprom + i, sizeof(oem_hdr));
  61. copy_len = min(oem_hdr.len - sizeof(oem_hdr),
  62. sizeof(*rom));
  63. memcpy_fromio(rom,
  64. oprom + i + sizeof(oem_hdr),
  65. copy_len);
  66. /* calculate checksum */
  67. tmp = (u8 *)&oem_hdr;
  68. for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
  69. sum += *tmp;
  70. tmp = (u8 *)rom;
  71. for (j = 0; j < sizeof(*rom); j++, tmp++)
  72. sum += *tmp;
  73. if (sum != 0) {
  74. dev_warn(&pdev->dev,
  75. "OEM table checksum failed\n");
  76. continue;
  77. }
  78. /* keep going if that's not the oem param table */
  79. if (memcmp(rom->hdr.signature,
  80. ISCI_ROM_SIG,
  81. ISCI_ROM_SIG_SIZE) != 0)
  82. continue;
  83. dev_info(&pdev->dev,
  84. "OEM parameter table found in OROM\n");
  85. break;
  86. }
  87. }
  88. if (i >= len) {
  89. dev_err(&pdev->dev, "oprom parse error\n");
  90. rom = NULL;
  91. }
  92. pci_unmap_biosrom(oprom);
  93. return rom;
  94. }
  95. struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
  96. {
  97. struct isci_orom *orom = NULL, *data;
  98. int i, j;
  99. if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
  100. return NULL;
  101. if (fw->size < sizeof(*orom))
  102. goto out;
  103. data = (struct isci_orom *)fw->data;
  104. if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
  105. strlen(ISCI_ROM_SIG)) != 0)
  106. goto out;
  107. orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
  108. if (!orom)
  109. goto out;
  110. memcpy(orom, fw->data, fw->size);
  111. if (is_c0(pdev) || is_c1(pdev))
  112. goto out;
  113. /*
  114. * deprecated: override default amp_control for pre-preproduction
  115. * silicon revisions
  116. */
  117. for (i = 0; i < ARRAY_SIZE(orom->ctrl); i++)
  118. for (j = 0; j < ARRAY_SIZE(orom->ctrl[i].phys); j++) {
  119. orom->ctrl[i].phys[j].afe_tx_amp_control0 = 0xe7c03;
  120. orom->ctrl[i].phys[j].afe_tx_amp_control1 = 0xe7c03;
  121. orom->ctrl[i].phys[j].afe_tx_amp_control2 = 0xe7c03;
  122. orom->ctrl[i].phys[j].afe_tx_amp_control3 = 0xe7c03;
  123. }
  124. out:
  125. release_firmware(fw);
  126. return orom;
  127. }
  128. static struct efi *get_efi(void)
  129. {
  130. #ifdef CONFIG_EFI
  131. return &efi;
  132. #else
  133. return NULL;
  134. #endif
  135. }
  136. struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
  137. {
  138. efi_status_t status;
  139. struct isci_orom *rom;
  140. struct isci_oem_hdr *oem_hdr;
  141. u8 *tmp, sum;
  142. int j;
  143. unsigned long data_len;
  144. u8 *efi_data;
  145. u32 efi_attrib = 0;
  146. data_len = 1024;
  147. efi_data = devm_kzalloc(&pdev->dev, data_len, GFP_KERNEL);
  148. if (!efi_data) {
  149. dev_warn(&pdev->dev,
  150. "Unable to allocate memory for EFI data\n");
  151. return NULL;
  152. }
  153. rom = (struct isci_orom *)(efi_data + sizeof(struct isci_oem_hdr));
  154. if (get_efi())
  155. status = get_efi()->get_variable(isci_efivar_name,
  156. &ISCI_EFI_VENDOR_GUID,
  157. &efi_attrib,
  158. &data_len,
  159. efi_data);
  160. else
  161. status = EFI_NOT_FOUND;
  162. if (status != EFI_SUCCESS) {
  163. dev_warn(&pdev->dev,
  164. "Unable to obtain EFI var data for OEM parms\n");
  165. return NULL;
  166. }
  167. oem_hdr = (struct isci_oem_hdr *)efi_data;
  168. if (memcmp(oem_hdr->sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) != 0) {
  169. dev_warn(&pdev->dev,
  170. "Invalid OEM header signature\n");
  171. return NULL;
  172. }
  173. /* calculate checksum */
  174. tmp = (u8 *)efi_data;
  175. for (j = 0, sum = 0; j < (sizeof(*oem_hdr) + sizeof(*rom)); j++, tmp++)
  176. sum += *tmp;
  177. if (sum != 0) {
  178. dev_warn(&pdev->dev,
  179. "OEM table checksum failed\n");
  180. return NULL;
  181. }
  182. if (memcmp(rom->hdr.signature,
  183. ISCI_ROM_SIG,
  184. ISCI_ROM_SIG_SIZE) != 0) {
  185. dev_warn(&pdev->dev,
  186. "Invalid OEM table signature\n");
  187. return NULL;
  188. }
  189. return rom;
  190. }