probe_roms.c 6.3 KB

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