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