probe_roms.c 6.4 KB

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