ionic_fw.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2020 Pensando Systems, Inc */
  3. #include <linux/kernel.h>
  4. #include <linux/types.h>
  5. #include <linux/errno.h>
  6. #include <linux/firmware.h>
  7. #include "ionic.h"
  8. #include "ionic_dev.h"
  9. #include "ionic_lif.h"
  10. #include "ionic_devlink.h"
  11. /* The worst case wait for the install activity is about 25 minutes when
  12. * installing a new CPLD, which is very seldom. Normal is about 30-35
  13. * seconds. Since the driver can't tell if a CPLD update will happen we
  14. * set the timeout for the ugly case.
  15. */
  16. #define IONIC_FW_INSTALL_TIMEOUT (25 * 60)
  17. #define IONIC_FW_SELECT_TIMEOUT 30
  18. /* Number of periodic log updates during fw file download */
  19. #define IONIC_FW_INTERVAL_FRACTION 32
  20. static void ionic_dev_cmd_firmware_download(struct ionic_dev *idev, u64 addr,
  21. u32 offset, u32 length)
  22. {
  23. union ionic_dev_cmd cmd = {
  24. .fw_download.opcode = IONIC_CMD_FW_DOWNLOAD,
  25. .fw_download.offset = cpu_to_le32(offset),
  26. .fw_download.addr = cpu_to_le64(addr),
  27. .fw_download.length = cpu_to_le32(length),
  28. };
  29. ionic_dev_cmd_go(idev, &cmd);
  30. }
  31. static void ionic_dev_cmd_firmware_install(struct ionic_dev *idev)
  32. {
  33. union ionic_dev_cmd cmd = {
  34. .fw_control.opcode = IONIC_CMD_FW_CONTROL,
  35. .fw_control.oper = IONIC_FW_INSTALL_ASYNC
  36. };
  37. ionic_dev_cmd_go(idev, &cmd);
  38. }
  39. static void ionic_dev_cmd_firmware_activate(struct ionic_dev *idev, u8 slot)
  40. {
  41. union ionic_dev_cmd cmd = {
  42. .fw_control.opcode = IONIC_CMD_FW_CONTROL,
  43. .fw_control.oper = IONIC_FW_ACTIVATE_ASYNC,
  44. .fw_control.slot = slot
  45. };
  46. ionic_dev_cmd_go(idev, &cmd);
  47. }
  48. static int ionic_fw_status_long_wait(struct ionic *ionic,
  49. const char *label,
  50. unsigned long timeout,
  51. u8 fw_cmd,
  52. struct netlink_ext_ack *extack)
  53. {
  54. union ionic_dev_cmd cmd = {
  55. .fw_control.opcode = IONIC_CMD_FW_CONTROL,
  56. .fw_control.oper = fw_cmd,
  57. };
  58. unsigned long start_time;
  59. unsigned long end_time;
  60. int err;
  61. start_time = jiffies;
  62. end_time = start_time + (timeout * HZ);
  63. do {
  64. mutex_lock(&ionic->dev_cmd_lock);
  65. ionic_dev_cmd_go(&ionic->idev, &cmd);
  66. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  67. mutex_unlock(&ionic->dev_cmd_lock);
  68. msleep(20);
  69. } while (time_before(jiffies, end_time) && (err == -EAGAIN || err == -ETIMEDOUT));
  70. if (err == -EAGAIN || err == -ETIMEDOUT) {
  71. NL_SET_ERR_MSG_MOD(extack, "Firmware wait timed out");
  72. dev_err(ionic->dev, "DEV_CMD firmware wait %s timed out\n", label);
  73. } else if (err) {
  74. NL_SET_ERR_MSG_MOD(extack, "Firmware wait failed");
  75. }
  76. return err;
  77. }
  78. int ionic_firmware_update(struct ionic_lif *lif, const struct firmware *fw,
  79. struct netlink_ext_ack *extack)
  80. {
  81. struct ionic_dev *idev = &lif->ionic->idev;
  82. struct net_device *netdev = lif->netdev;
  83. struct ionic *ionic = lif->ionic;
  84. union ionic_dev_cmd_comp comp;
  85. u32 buf_sz, copy_sz, offset;
  86. struct devlink *dl;
  87. int next_interval;
  88. int err = 0;
  89. u8 fw_slot;
  90. netdev_info(netdev, "Installing firmware\n");
  91. dl = priv_to_devlink(ionic);
  92. devlink_flash_update_status_notify(dl, "Preparing to flash", NULL, 0, 0);
  93. if (!idev->dev_cmd_regs) {
  94. err = -ENXIO;
  95. goto err_out;
  96. }
  97. buf_sz = sizeof(idev->dev_cmd_regs->data);
  98. netdev_dbg(netdev,
  99. "downloading firmware - size %d part_sz %d nparts %lu\n",
  100. (int)fw->size, buf_sz, DIV_ROUND_UP(fw->size, buf_sz));
  101. offset = 0;
  102. next_interval = 0;
  103. while (offset < fw->size) {
  104. if (offset >= next_interval) {
  105. devlink_flash_update_status_notify(dl, "Downloading", NULL,
  106. offset, fw->size);
  107. next_interval = offset + (fw->size / IONIC_FW_INTERVAL_FRACTION);
  108. }
  109. copy_sz = min_t(unsigned int, buf_sz, fw->size - offset);
  110. mutex_lock(&ionic->dev_cmd_lock);
  111. memcpy_toio(&idev->dev_cmd_regs->data, fw->data + offset, copy_sz);
  112. ionic_dev_cmd_firmware_download(idev,
  113. offsetof(union ionic_dev_cmd_regs, data),
  114. offset, copy_sz);
  115. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  116. mutex_unlock(&ionic->dev_cmd_lock);
  117. if (err) {
  118. netdev_err(netdev,
  119. "download failed offset 0x%x addr 0x%lx len 0x%x\n",
  120. offset, offsetof(union ionic_dev_cmd_regs, data),
  121. copy_sz);
  122. NL_SET_ERR_MSG_MOD(extack, "Segment download failed");
  123. goto err_out;
  124. }
  125. offset += copy_sz;
  126. }
  127. devlink_flash_update_status_notify(dl, "Downloading", NULL,
  128. fw->size, fw->size);
  129. devlink_flash_update_timeout_notify(dl, "Installing", NULL,
  130. IONIC_FW_INSTALL_TIMEOUT);
  131. mutex_lock(&ionic->dev_cmd_lock);
  132. ionic_dev_cmd_firmware_install(idev);
  133. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  134. ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
  135. fw_slot = comp.fw_control.slot;
  136. mutex_unlock(&ionic->dev_cmd_lock);
  137. if (err) {
  138. NL_SET_ERR_MSG_MOD(extack, "Failed to start firmware install");
  139. goto err_out;
  140. }
  141. err = ionic_fw_status_long_wait(ionic, "Installing",
  142. IONIC_FW_INSTALL_TIMEOUT,
  143. IONIC_FW_INSTALL_STATUS,
  144. extack);
  145. if (err)
  146. goto err_out;
  147. devlink_flash_update_timeout_notify(dl, "Selecting", NULL,
  148. IONIC_FW_SELECT_TIMEOUT);
  149. mutex_lock(&ionic->dev_cmd_lock);
  150. ionic_dev_cmd_firmware_activate(idev, fw_slot);
  151. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  152. mutex_unlock(&ionic->dev_cmd_lock);
  153. if (err) {
  154. NL_SET_ERR_MSG_MOD(extack, "Failed to start firmware select");
  155. goto err_out;
  156. }
  157. err = ionic_fw_status_long_wait(ionic, "Selecting",
  158. IONIC_FW_SELECT_TIMEOUT,
  159. IONIC_FW_ACTIVATE_STATUS,
  160. extack);
  161. if (err)
  162. goto err_out;
  163. netdev_info(netdev, "Firmware update completed\n");
  164. err_out:
  165. if (err)
  166. devlink_flash_update_status_notify(dl, "Flash failed", NULL, 0, 0);
  167. else
  168. devlink_flash_update_status_notify(dl, "Flash done", NULL, 0, 0);
  169. return err;
  170. }