1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
6 /* The worst case wait for the install activity is about 25 minutes when
7 * installing a new CPLD, which is very seldom. Normal is about 30-35
8 * seconds. Since the driver can't tell if a CPLD update will happen we
9 * set the timeout for the ugly case.
11 #define PDSC_FW_INSTALL_TIMEOUT (25 * 60)
12 #define PDSC_FW_SELECT_TIMEOUT 30
14 /* Number of periodic log updates during fw file download */
15 #define PDSC_FW_INTERVAL_FRACTION 32
17 static int pdsc_devcmd_fw_download_locked(struct pdsc *pdsc, u64 addr,
18 u32 offset, u32 length)
20 union pds_core_dev_cmd cmd = {
21 .fw_download.opcode = PDS_CORE_CMD_FW_DOWNLOAD,
22 .fw_download.offset = cpu_to_le32(offset),
23 .fw_download.addr = cpu_to_le64(addr),
24 .fw_download.length = cpu_to_le32(length),
26 union pds_core_dev_comp comp;
28 return pdsc_devcmd_locked(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
31 static int pdsc_devcmd_fw_install(struct pdsc *pdsc)
33 union pds_core_dev_cmd cmd = {
34 .fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
35 .fw_control.oper = PDS_CORE_FW_INSTALL_ASYNC
37 union pds_core_dev_comp comp;
40 err = pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
44 return comp.fw_control.slot;
47 static int pdsc_devcmd_fw_activate(struct pdsc *pdsc,
48 enum pds_core_fw_slot slot)
50 union pds_core_dev_cmd cmd = {
51 .fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
52 .fw_control.oper = PDS_CORE_FW_ACTIVATE_ASYNC,
53 .fw_control.slot = slot
55 union pds_core_dev_comp comp;
57 return pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
60 static int pdsc_fw_status_long_wait(struct pdsc *pdsc,
62 unsigned long timeout,
64 struct netlink_ext_ack *extack)
66 union pds_core_dev_cmd cmd = {
67 .fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
68 .fw_control.oper = fw_cmd,
70 union pds_core_dev_comp comp;
71 unsigned long start_time;
72 unsigned long end_time;
75 /* Ping on the status of the long running async install
76 * command. We get EAGAIN while the command is still
77 * running, else we get the final command status.
80 end_time = start_time + (timeout * HZ);
82 err = pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
84 } while (time_before(jiffies, end_time) &&
85 (err == -EAGAIN || err == -ETIMEDOUT));
87 if (err == -EAGAIN || err == -ETIMEDOUT) {
88 NL_SET_ERR_MSG_MOD(extack, "Firmware wait timed out");
89 dev_err(pdsc->dev, "DEV_CMD firmware wait %s timed out\n",
92 NL_SET_ERR_MSG_MOD(extack, "Firmware wait failed");
98 int pdsc_firmware_update(struct pdsc *pdsc, const struct firmware *fw,
99 struct netlink_ext_ack *extack)
101 u32 buf_sz, copy_sz, offset;
108 dev_info(pdsc->dev, "Installing firmware\n");
113 dl = priv_to_devlink(pdsc);
114 devlink_flash_update_status_notify(dl, "Preparing to flash",
117 buf_sz = sizeof(pdsc->cmd_regs->data);
120 "downloading firmware - size %d part_sz %d nparts %lu\n",
121 (int)fw->size, buf_sz, DIV_ROUND_UP(fw->size, buf_sz));
125 data_addr = offsetof(struct pds_core_dev_cmd_regs, data);
126 while (offset < fw->size) {
127 if (offset >= next_interval) {
128 devlink_flash_update_status_notify(dl, "Downloading",
131 next_interval = offset +
132 (fw->size / PDSC_FW_INTERVAL_FRACTION);
135 copy_sz = min_t(unsigned int, buf_sz, fw->size - offset);
136 mutex_lock(&pdsc->devcmd_lock);
137 memcpy_toio(&pdsc->cmd_regs->data, fw->data + offset, copy_sz);
138 err = pdsc_devcmd_fw_download_locked(pdsc, data_addr,
140 mutex_unlock(&pdsc->devcmd_lock);
143 "download failed offset 0x%x addr 0x%llx len 0x%x: %pe\n",
144 offset, data_addr, copy_sz, ERR_PTR(err));
145 NL_SET_ERR_MSG_MOD(extack, "Segment download failed");
150 devlink_flash_update_status_notify(dl, "Downloading", NULL,
153 devlink_flash_update_timeout_notify(dl, "Installing", NULL,
154 PDSC_FW_INSTALL_TIMEOUT);
156 fw_slot = pdsc_devcmd_fw_install(pdsc);
159 dev_err(pdsc->dev, "install failed: %pe\n", ERR_PTR(err));
160 NL_SET_ERR_MSG_MOD(extack, "Failed to start firmware install");
164 err = pdsc_fw_status_long_wait(pdsc, "Installing",
165 PDSC_FW_INSTALL_TIMEOUT,
166 PDS_CORE_FW_INSTALL_STATUS,
171 devlink_flash_update_timeout_notify(dl, "Selecting", NULL,
172 PDSC_FW_SELECT_TIMEOUT);
174 err = pdsc_devcmd_fw_activate(pdsc, fw_slot);
176 NL_SET_ERR_MSG_MOD(extack, "Failed to start firmware select");
180 err = pdsc_fw_status_long_wait(pdsc, "Selecting",
181 PDSC_FW_SELECT_TIMEOUT,
182 PDS_CORE_FW_ACTIVATE_STATUS,
187 dev_info(pdsc->dev, "Firmware update completed, slot %d\n", fw_slot);
191 devlink_flash_update_status_notify(dl, "Flash failed",
194 devlink_flash_update_status_notify(dl, "Flash done",