probe_roms.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. struct efi_variable {
  34. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  35. efi_guid_t VendorGuid;
  36. unsigned long DataSize;
  37. __u8 Data[1024];
  38. efi_status_t Status;
  39. __u32 Attributes;
  40. } __attribute__((packed));
  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;
  46. int j;
  47. char oem_sig[4];
  48. struct isci_oem_hdr oem_hdr;
  49. u8 *tmp, sum;
  50. if (!oprom)
  51. return NULL;
  52. len = pci_biosrom_size(pdev);
  53. rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
  54. if (!rom) {
  55. dev_warn(&pdev->dev,
  56. "Unable to allocate memory for orom\n");
  57. return NULL;
  58. }
  59. for (i = 0; i < len && rom; i += ISCI_OEM_SIG_SIZE) {
  60. memcpy_fromio(oem_sig, oprom + i, ISCI_OEM_SIG_SIZE);
  61. /* we think we found the OEM table */
  62. if (memcmp(oem_sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) == 0) {
  63. size_t copy_len;
  64. memcpy_fromio(&oem_hdr, oprom + i, sizeof(oem_hdr));
  65. copy_len = min(oem_hdr.len - sizeof(oem_hdr),
  66. sizeof(*rom));
  67. memcpy_fromio(rom,
  68. oprom + i + sizeof(oem_hdr),
  69. copy_len);
  70. /* calculate checksum */
  71. tmp = (u8 *)&oem_hdr;
  72. for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
  73. sum += *tmp;
  74. tmp = (u8 *)rom;
  75. for (j = 0; j < sizeof(*rom); j++, tmp++)
  76. sum += *tmp;
  77. if (sum != 0) {
  78. dev_warn(&pdev->dev,
  79. "OEM table checksum failed\n");
  80. continue;
  81. }
  82. /* keep going if that's not the oem param table */
  83. if (memcmp(rom->hdr.signature,
  84. ISCI_ROM_SIG,
  85. ISCI_ROM_SIG_SIZE) != 0)
  86. continue;
  87. dev_info(&pdev->dev,
  88. "OEM parameter table found in OROM\n");
  89. break;
  90. }
  91. }
  92. if (i >= len) {
  93. dev_err(&pdev->dev, "oprom parse error\n");
  94. devm_kfree(&pdev->dev, rom);
  95. rom = NULL;
  96. }
  97. pci_unmap_biosrom(oprom);
  98. return rom;
  99. }
  100. /**
  101. * isci_parse_oem_parameters() - This method will take OEM parameters
  102. * from the module init parameters and copy them to oem_params. This will
  103. * only copy values that are not set to the module parameter default values
  104. * @oem_parameters: This parameter specifies the controller default OEM
  105. * parameters. It is expected that this has been initialized to the default
  106. * parameters for the controller
  107. *
  108. *
  109. */
  110. enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
  111. struct isci_orom *orom, int scu_index)
  112. {
  113. /* check for valid inputs */
  114. if (scu_index < 0 || scu_index > SCI_MAX_CONTROLLERS ||
  115. scu_index > orom->hdr.num_elements || !oem_params)
  116. return -EINVAL;
  117. oem_params->sds1 = orom->ctrl[scu_index];
  118. return 0;
  119. }
  120. struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
  121. {
  122. struct isci_orom *orom = NULL, *data;
  123. if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
  124. return NULL;
  125. if (fw->size < sizeof(*orom))
  126. goto out;
  127. data = (struct isci_orom *)fw->data;
  128. if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
  129. strlen(ISCI_ROM_SIG)) != 0)
  130. goto out;
  131. orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
  132. if (!orom)
  133. goto out;
  134. memcpy(orom, fw->data, fw->size);
  135. out:
  136. release_firmware(fw);
  137. return orom;
  138. }
  139. static struct efi *get_efi(void)
  140. {
  141. #ifdef CONFIG_EFI
  142. return &efi;
  143. #else
  144. return NULL;
  145. #endif
  146. }
  147. struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
  148. {
  149. struct efi_variable *evar;
  150. efi_status_t status;
  151. struct isci_orom *rom = NULL;
  152. struct isci_oem_hdr *oem_hdr;
  153. u8 *tmp, sum;
  154. int j;
  155. size_t copy_len;
  156. evar = devm_kzalloc(&pdev->dev,
  157. sizeof(struct efi_variable),
  158. GFP_KERNEL);
  159. if (!evar) {
  160. dev_warn(&pdev->dev,
  161. "Unable to allocate memory for EFI var\n");
  162. return NULL;
  163. }
  164. rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
  165. if (!rom) {
  166. dev_warn(&pdev->dev,
  167. "Unable to allocate memory for orom\n");
  168. return NULL;
  169. }
  170. for (j = 0; j < strlen(ISCI_EFI_VAR_NAME) + 1; j++)
  171. evar->VariableName[j] = ISCI_EFI_VAR_NAME[j];
  172. evar->DataSize = 1024;
  173. evar->VendorGuid = ISCI_EFI_VENDOR_GUID;
  174. evar->Attributes = ISCI_EFI_ATTRIBUTES;
  175. if (get_efi())
  176. status = get_efi()->get_variable(evar->VariableName,
  177. &evar->VendorGuid,
  178. &evar->Attributes,
  179. &evar->DataSize,
  180. evar->Data);
  181. else
  182. status = EFI_NOT_FOUND;
  183. if (status != EFI_SUCCESS) {
  184. dev_warn(&pdev->dev,
  185. "Unable to obtain EFI variable for OEM parms\n");
  186. return NULL;
  187. }
  188. oem_hdr = (struct isci_oem_hdr *)evar->Data;
  189. if (memcmp(oem_hdr->sig, ISCI_OEM_SIG, ISCI_OEM_SIG_SIZE) != 0) {
  190. dev_warn(&pdev->dev,
  191. "Invalid OEM header signature\n");
  192. return NULL;
  193. }
  194. /* calculate checksum */
  195. tmp = (u8 *)oem_hdr;
  196. for (j = 0, sum = 0; j < sizeof(oem_hdr); j++, tmp++)
  197. sum += *tmp;
  198. tmp = (u8 *)rom;
  199. for (j = 0; j < sizeof(*rom); j++, tmp++)
  200. sum += *tmp;
  201. if (sum != 0) {
  202. dev_warn(&pdev->dev,
  203. "OEM table checksum failed\n");
  204. return NULL;
  205. }
  206. copy_len = min_t(u16, evar->DataSize,
  207. min_t(u16, oem_hdr->len - sizeof(*oem_hdr), sizeof(*rom)));
  208. memcpy(rom, (char *)evar->Data + sizeof(*oem_hdr), copy_len);
  209. if (memcmp(rom->hdr.signature,
  210. ISCI_ROM_SIG,
  211. ISCI_ROM_SIG_SIZE) != 0) {
  212. dev_warn(&pdev->dev,
  213. "Invalid OEM table signature\n");
  214. return NULL;
  215. }
  216. return rom;
  217. }