probe_roms.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "scic_remote_device.h"
  34. #include "sci_environment.h"
  35. #include "probe_roms.h"
  36. struct efi_variable {
  37. efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
  38. efi_guid_t VendorGuid;
  39. unsigned long DataSize;
  40. __u8 Data[1024];
  41. efi_status_t Status;
  42. __u32 Attributes;
  43. } __attribute__((packed));
  44. struct isci_orom *isci_request_oprom(struct pci_dev *pdev)
  45. {
  46. void __iomem *oprom = pci_map_biosrom(pdev);
  47. struct isci_orom *rom = NULL;
  48. size_t len, i;
  49. if (!oprom)
  50. return NULL;
  51. len = pci_biosrom_size(pdev);
  52. rom = devm_kzalloc(&pdev->dev, sizeof(*rom), GFP_KERNEL);
  53. for (i = 0; i < len && rom; i += ISCI_ROM_SIG_SIZE) {
  54. memcpy_fromio(rom->hdr.signature, oprom + i, ISCI_ROM_SIG_SIZE);
  55. if (memcmp(rom->hdr.signature, ISCI_ROM_SIG,
  56. ISCI_ROM_SIG_SIZE) == 0) {
  57. size_t copy_len = min(len - i, sizeof(*rom));
  58. memcpy_fromio(rom, oprom + i, copy_len);
  59. break;
  60. }
  61. }
  62. if (i >= len) {
  63. dev_err(&pdev->dev, "oprom parse error\n");
  64. devm_kfree(&pdev->dev, rom);
  65. rom = NULL;
  66. }
  67. pci_unmap_biosrom(oprom);
  68. return rom;
  69. }
  70. /**
  71. * isci_parse_oem_parameters() - This method will take OEM parameters
  72. * from the module init parameters and copy them to oem_params. This will
  73. * only copy values that are not set to the module parameter default values
  74. * @oem_parameters: This parameter specifies the controller default OEM
  75. * parameters. It is expected that this has been initialized to the default
  76. * parameters for the controller
  77. *
  78. *
  79. */
  80. enum sci_status isci_parse_oem_parameters(union scic_oem_parameters *oem_params,
  81. struct isci_orom *orom, int scu_index)
  82. {
  83. /* check for valid inputs */
  84. if (scu_index < 0 || scu_index > SCI_MAX_CONTROLLERS ||
  85. scu_index > orom->hdr.num_elements || !oem_params)
  86. return -EINVAL;
  87. memcpy(oem_params,
  88. &orom->ctrl[scu_index],
  89. sizeof(struct scic_sds_oem_params));
  90. return 0;
  91. }
  92. struct isci_orom *isci_request_firmware(struct pci_dev *pdev, const struct firmware *fw)
  93. {
  94. struct isci_orom *orom = NULL, *data;
  95. if (request_firmware(&fw, ISCI_FW_NAME, &pdev->dev) != 0)
  96. return NULL;
  97. if (fw->size < sizeof(*orom))
  98. goto out;
  99. data = (struct isci_orom *)fw->data;
  100. if (strncmp(ISCI_ROM_SIG, data->hdr.signature,
  101. strlen(ISCI_ROM_SIG)) != 0)
  102. goto out;
  103. orom = devm_kzalloc(&pdev->dev, fw->size, GFP_KERNEL);
  104. if (!orom)
  105. goto out;
  106. memcpy(orom, fw->data, fw->size);
  107. out:
  108. release_firmware(fw);
  109. return orom;
  110. }
  111. static struct efi *get_efi(void)
  112. {
  113. #ifdef CONFIG_EFI
  114. return &efi;
  115. #else
  116. return NULL;
  117. #endif
  118. }
  119. struct isci_orom *isci_get_efi_var(struct pci_dev *pdev)
  120. {
  121. struct efi_variable *evar;
  122. efi_status_t status;
  123. struct isci_orom *orom = NULL;
  124. evar = devm_kzalloc(&pdev->dev,
  125. sizeof(struct efi_variable),
  126. GFP_KERNEL);
  127. if (!evar) {
  128. dev_warn(&pdev->dev,
  129. "Unable to allocate memory for EFI var\n");
  130. return NULL;
  131. }
  132. evar->DataSize = 1024;
  133. evar->VendorGuid = ISCI_EFI_VENDOR_GUID;
  134. evar->Attributes = ISCI_EFI_ATTRIBUTES;
  135. if (get_efi())
  136. status = get_efi()->get_variable(evar->VariableName,
  137. &evar->VendorGuid,
  138. &evar->Attributes,
  139. &evar->DataSize,
  140. evar->Data);
  141. else
  142. status = EFI_NOT_FOUND;
  143. if (status == EFI_SUCCESS)
  144. orom = (struct isci_orom *)evar->Data;
  145. else
  146. dev_warn(&pdev->dev,
  147. "Unable to obtain EFI variable for OEM parms\n");
  148. if (orom && memcmp(orom->hdr.signature, ISCI_ROM_SIG,
  149. strlen(ISCI_ROM_SIG)) != 0)
  150. dev_warn(&pdev->dev,
  151. "Verifying OROM signature failed\n");
  152. if (!orom)
  153. devm_kfree(&pdev->dev, evar);
  154. return orom;
  155. }