1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
3
4 #include <linux/pldmfw.h>
5 #include <linux/vmalloc.h>
6
7 #include "core.h"
8
9 /* The worst case wait for the install activity is about 25 minutes when
10 * installing a new CPLD, which is very seldom. Normal is about 30-35
11 * seconds. Since the driver can't tell if a CPLD update will happen we
12 * set the timeout for the ugly case.
13 */
14 #define PDSC_FW_INSTALL_TIMEOUT (25 * 60)
15 #define PDSC_FW_SELECT_TIMEOUT 30
16
17 /* Number of periodic log updates during fw file download */
18 #define PDSC_FW_INTERVAL_FRACTION 32
19
20 #define PDSC_FW_COMPONENT_PREFIX "fw."
21 #define PDSC_FW_COMPONENT_FULL_NAME_BUFLEN \
22 (sizeof(PDSC_FW_COMPONENT_PREFIX) + PDS_CORE_FW_COMPONENT_NAME_BUFLEN)
23
24 /* Driver-defined component type to name mapping.
25 * PDS_CORE_FW_TYPE_MAIN is NULL - handled specially as "fw" without prefix.
26 */
27 static const char * const pdsc_fw_type_names[] = {
28 [PDS_CORE_FW_TYPE_MAIN] = NULL,
29 [PDS_CORE_FW_TYPE_BOOT] = "bootloader",
30 [PDS_CORE_FW_TYPE_CPLD] = "cpld",
31 [PDS_CORE_FW_TYPE_SECURE] = "secure",
32 [PDS_CORE_FW_TYPE_FPGA] = "fpga",
33 [PDS_CORE_FW_TYPE_SUC_MAIN] = "suc",
34 [PDS_CORE_FW_TYPE_SUC_BOOT] = "suc.bootloader",
35 [PDS_CORE_FW_TYPE_UBOOT] = "uboot",
36 };
37
pdsc_fw_type_to_name(u8 type)38 const char *pdsc_fw_type_to_name(u8 type)
39 {
40 if (type < ARRAY_SIZE(pdsc_fw_type_names) && pdsc_fw_type_names[type])
41 return pdsc_fw_type_names[type];
42 return NULL;
43 }
44
pdsc_fw_components_invalidate(struct pdsc * pdsc)45 void pdsc_fw_components_invalidate(struct pdsc *pdsc)
46 {
47 /* Pairs with READ_ONCE in pdsc_dl_component_info_get() */
48 WRITE_ONCE(pdsc->fw_components.num_components, 0);
49 }
50
pdsc_name_to_fw_type(const char * name)51 static u8 pdsc_name_to_fw_type(const char *name)
52 {
53 size_t prefix_len;
54 int i;
55
56 /* "fw" without suffix maps to main firmware */
57 if (!strcmp(name, "fw"))
58 return PDS_CORE_FW_TYPE_MAIN;
59
60 prefix_len = str_has_prefix(name, PDSC_FW_COMPONENT_PREFIX);
61 if (prefix_len)
62 name += prefix_len;
63
64 for (i = 1; i < ARRAY_SIZE(pdsc_fw_type_names); i++) {
65 if (pdsc_fw_type_names[i] &&
66 !strcmp(name, pdsc_fw_type_names[i]))
67 return i;
68 }
69 return 0;
70 }
71
pdsc_devcmd_fw_download_locked(struct pdsc * pdsc,u64 addr,u32 offset,u32 length)72 static int pdsc_devcmd_fw_download_locked(struct pdsc *pdsc, u64 addr,
73 u32 offset, u32 length)
74 {
75 union pds_core_dev_cmd cmd = {
76 .fw_download.opcode = PDS_CORE_CMD_FW_DOWNLOAD,
77 .fw_download.offset = cpu_to_le32(offset),
78 .fw_download.addr = cpu_to_le64(addr),
79 .fw_download.length = cpu_to_le32(length),
80 };
81 union pds_core_dev_comp comp = {};
82
83 return pdsc_devcmd_locked(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
84 }
85
pdsc_devcmd_fw_install(struct pdsc * pdsc)86 static int pdsc_devcmd_fw_install(struct pdsc *pdsc)
87 {
88 union pds_core_dev_cmd cmd = {
89 .fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
90 .fw_control.oper = PDS_CORE_FW_INSTALL_ASYNC
91 };
92 union pds_core_dev_comp comp;
93 int err;
94
95 err = pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
96 if (err < 0)
97 return err;
98
99 return comp.fw_control.slot;
100 }
101
pdsc_devcmd_fw_activate(struct pdsc * pdsc,enum pds_core_fw_slot slot)102 static int pdsc_devcmd_fw_activate(struct pdsc *pdsc,
103 enum pds_core_fw_slot slot)
104 {
105 union pds_core_dev_cmd cmd = {
106 .fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
107 .fw_control.oper = PDS_CORE_FW_ACTIVATE_ASYNC,
108 .fw_control.slot = slot
109 };
110 union pds_core_dev_comp comp;
111
112 return pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
113 }
114
pdsc_fw_status_long_wait(struct pdsc * pdsc,const char * label,unsigned long timeout,u8 fw_cmd,struct netlink_ext_ack * extack)115 static int pdsc_fw_status_long_wait(struct pdsc *pdsc,
116 const char *label,
117 unsigned long timeout,
118 u8 fw_cmd,
119 struct netlink_ext_ack *extack)
120 {
121 union pds_core_dev_cmd cmd = {
122 .fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
123 .fw_control.oper = fw_cmd,
124 };
125 union pds_core_dev_comp comp;
126 unsigned long start_time;
127 unsigned long end_time;
128 int err;
129
130 /* Ping on the status of the long running async install
131 * command. We get EAGAIN while the command is still
132 * running, else we get the final command status.
133 */
134 start_time = jiffies;
135 end_time = start_time + (timeout * HZ);
136 do {
137 err = pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
138 msleep(20);
139 } while (time_before(jiffies, end_time) &&
140 (err == -EAGAIN || err == -ETIMEDOUT));
141
142 if (err == -EAGAIN || err == -ETIMEDOUT) {
143 NL_SET_ERR_MSG_MOD(extack, "Firmware wait timed out");
144 dev_err(pdsc->dev, "DEV_CMD firmware wait %s timed out\n",
145 label);
146 } else if (err) {
147 NL_SET_ERR_MSG_MOD(extack, "Firmware wait failed");
148 }
149
150 return err;
151 }
152
153 static int
pdsc_legacy_firmware_update(struct pdsc * pdsc,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)154 pdsc_legacy_firmware_update(struct pdsc *pdsc,
155 struct devlink_flash_update_params *params,
156 struct netlink_ext_ack *extack)
157 {
158 const struct firmware *fw = params->fw;
159 u32 buf_sz, copy_sz, offset;
160 struct devlink *dl;
161 int next_interval;
162 u64 data_addr;
163 int err = 0;
164 int fw_slot;
165
166 if (params->component) {
167 NL_SET_ERR_MSG_MOD(extack,
168 "Component update not supported by this device");
169 return -EOPNOTSUPP;
170 }
171
172 dev_info(pdsc->dev, "Installing firmware\n");
173
174 if (!pdsc->cmd_regs)
175 return -ENXIO;
176
177 dl = priv_to_devlink(pdsc);
178 devlink_flash_update_status_notify(dl, "Preparing to flash",
179 NULL, 0, 0);
180
181 buf_sz = sizeof(pdsc->cmd_regs->data);
182
183 dev_dbg(pdsc->dev,
184 "downloading firmware - size %d part_sz %d nparts %lu\n",
185 (int)fw->size, buf_sz, DIV_ROUND_UP(fw->size, buf_sz));
186
187 offset = 0;
188 next_interval = 0;
189 data_addr = offsetof(struct pds_core_dev_cmd_regs, data);
190 while (offset < fw->size) {
191 if (offset >= next_interval) {
192 devlink_flash_update_status_notify(dl, "Downloading",
193 NULL, offset,
194 fw->size);
195 next_interval = offset +
196 (fw->size / PDSC_FW_INTERVAL_FRACTION);
197 }
198
199 copy_sz = min_t(unsigned int, buf_sz, fw->size - offset);
200 mutex_lock(&pdsc->devcmd_lock);
201 memcpy_toio(&pdsc->cmd_regs->data, fw->data + offset, copy_sz);
202 err = pdsc_devcmd_fw_download_locked(pdsc, data_addr,
203 offset, copy_sz);
204 mutex_unlock(&pdsc->devcmd_lock);
205 if (err) {
206 dev_err(pdsc->dev,
207 "download failed offset 0x%x addr 0x%llx len 0x%x: %pe\n",
208 offset, data_addr, copy_sz, ERR_PTR(err));
209 NL_SET_ERR_MSG_MOD(extack, "Segment download failed");
210 goto err_out;
211 }
212 offset += copy_sz;
213 }
214 devlink_flash_update_status_notify(dl, "Downloading", NULL,
215 fw->size, fw->size);
216
217 devlink_flash_update_timeout_notify(dl, "Installing", NULL,
218 PDSC_FW_INSTALL_TIMEOUT);
219
220 fw_slot = pdsc_devcmd_fw_install(pdsc);
221 if (fw_slot < 0) {
222 err = fw_slot;
223 dev_err(pdsc->dev, "install failed: %pe\n", ERR_PTR(err));
224 NL_SET_ERR_MSG_MOD(extack, "Failed to start firmware install");
225 goto err_out;
226 }
227
228 err = pdsc_fw_status_long_wait(pdsc, "Installing",
229 PDSC_FW_INSTALL_TIMEOUT,
230 PDS_CORE_FW_INSTALL_STATUS,
231 extack);
232 if (err)
233 goto err_out;
234
235 devlink_flash_update_timeout_notify(dl, "Selecting", NULL,
236 PDSC_FW_SELECT_TIMEOUT);
237
238 err = pdsc_devcmd_fw_activate(pdsc, fw_slot);
239 if (err) {
240 NL_SET_ERR_MSG_MOD(extack, "Failed to start firmware select");
241 goto err_out;
242 }
243
244 err = pdsc_fw_status_long_wait(pdsc, "Selecting",
245 PDSC_FW_SELECT_TIMEOUT,
246 PDS_CORE_FW_ACTIVATE_STATUS,
247 extack);
248 if (err)
249 goto err_out;
250
251 dev_info(pdsc->dev, "Firmware update completed, slot %d\n", fw_slot);
252
253 err_out:
254 if (err)
255 devlink_flash_update_status_notify(dl, "Flash failed",
256 NULL, 0, 0);
257 else
258 devlink_flash_update_status_notify(dl, "Flash done",
259 NULL, 0, 0);
260 return err;
261 }
262
263 struct pdsc_component_priv {
264 u16 component_id;
265 bool skip;
266 struct list_head list_entry;
267 };
268
269 struct pds_core_fwu_priv {
270 struct pldmfw context;
271 struct devlink_flash_update_params *params;
272 struct netlink_ext_ack *extack;
273 struct pdsc *pdsc;
274 struct list_head components;
275 bool component_found;
276 };
277
pdsc_free_fwu_priv(struct pds_core_fwu_priv * priv)278 static void pdsc_free_fwu_priv(struct pds_core_fwu_priv *priv)
279 {
280 struct pdsc_component_priv *component_priv, *tmp;
281
282 list_for_each_entry_safe(component_priv, tmp, &priv->components,
283 list_entry) {
284 list_del(&component_priv->list_entry);
285 kfree(component_priv);
286 }
287 }
288
pdsc_devcmd_match_record_desc(struct pdsc * pdsc,u16 desc_type,u16 desc_size,const u8 * desc_data,u8 * match)289 static int pdsc_devcmd_match_record_desc(struct pdsc *pdsc, u16 desc_type,
290 u16 desc_size, const u8 *desc_data,
291 u8 *match)
292 {
293 union pds_core_dev_cmd cmd = {
294 .match_record_desc.opcode = PDS_CORE_CMD_MATCH_RECORD_DESC,
295 .match_record_desc.ver = 1,
296 .match_record_desc.type = cpu_to_le16(desc_type),
297 .match_record_desc.size = cpu_to_le16(desc_size),
298 };
299 union pds_core_dev_comp comp = {};
300 int err;
301
302 err = pdsc_devcmd_with_data(pdsc, &cmd, desc_data, desc_size,
303 &comp, pdsc->devcmd_timeout);
304 *match = comp.match_record_desc.match;
305
306 return err;
307 }
308
pdsc_match_record_descs(struct pldmfw * context,struct pldmfw_record * record)309 static bool pdsc_match_record_descs(struct pldmfw *context,
310 struct pldmfw_record *record)
311 {
312 struct pds_core_fwu_priv *priv =
313 container_of(context, struct pds_core_fwu_priv, context);
314 struct pdsc *pdsc = priv->pdsc;
315 struct pldmfw_desc_tlv *desc;
316
317 if (!pldmfw_op_pci_match_record(context, record))
318 return false;
319
320 list_for_each_entry(desc, &record->descs, entry) {
321 u8 match;
322 int err;
323
324 switch (desc->type) {
325 /* skip types checked in pldmfw_op_pci_match_record */
326 case PLDM_DESC_ID_PCI_VENDOR_ID:
327 case PLDM_DESC_ID_PCI_DEVICE_ID:
328 case PLDM_DESC_ID_PCI_SUBVENDOR_ID:
329 case PLDM_DESC_ID_PCI_SUBDEV_ID:
330 continue;
331 }
332
333 if (!desc->size)
334 return false;
335
336 err = pdsc_devcmd_match_record_desc(pdsc, desc->type,
337 desc->size, desc->data,
338 &match);
339 if (err) {
340 dev_err(pdsc->dev,
341 "match_record_desc failed type: 0x%04x size: %u, err %d\n",
342 desc->type, desc->size, err);
343 return false;
344 }
345 /* all record descriptors must match */
346 if (!match)
347 return false;
348 }
349
350 return true;
351 }
352
pdsc_devcmd_send_package_data(struct pdsc * pdsc,u64 addr,u16 length,u16 offset,u16 total_len)353 static int pdsc_devcmd_send_package_data(struct pdsc *pdsc, u64 addr,
354 u16 length, u16 offset, u16 total_len)
355 {
356 union pds_core_dev_cmd cmd = {
357 .send_pkg_data.opcode = PDS_CORE_CMD_SEND_PKG_DATA,
358 .send_pkg_data.ver = 1,
359 .send_pkg_data.data_pa = cpu_to_le64(addr),
360 .send_pkg_data.data_len = cpu_to_le16(length),
361 .send_pkg_data.offset = cpu_to_le16(offset),
362 .send_pkg_data.total_len = cpu_to_le16(total_len),
363 };
364 union pds_core_dev_comp comp = {};
365
366 return pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
367 }
368
pdsc_send_package_data(struct pldmfw * context,const u8 * data,u16 length)369 static int pdsc_send_package_data(struct pldmfw *context, const u8 *data,
370 u16 length)
371 {
372 struct pds_core_fwu_priv *priv =
373 container_of(context, struct pds_core_fwu_priv, context);
374 struct pdsc_deferred_dma *deferred;
375 struct device *dev = context->dev;
376 struct pdsc *pdsc = priv->pdsc;
377 dma_addr_t dma_addr;
378 u8 *package_data;
379 u32 offset;
380 int err;
381
382 if (!length)
383 return 0;
384
385 deferred = kmalloc_obj(*deferred, GFP_KERNEL);
386 if (!deferred)
387 return -ENOMEM;
388
389 package_data = kmemdup(data, length, GFP_KERNEL);
390 if (!package_data) {
391 kfree(deferred);
392 return -ENOMEM;
393 }
394
395 dma_addr = dma_map_single(dev, package_data, length, DMA_TO_DEVICE);
396 if (dma_mapping_error(dev, dma_addr)) {
397 dev_err(dev, "Failed to dma_map package_data length 0x%x\n",
398 length);
399 kfree(package_data);
400 kfree(deferred);
401 return -ENOMEM;
402 }
403
404 for (offset = 0; offset < length; offset += PDS_PAGE_SIZE) {
405 u32 copy_sz;
406
407 copy_sz = min_t(unsigned int, PDS_PAGE_SIZE, length - offset);
408 err = pdsc_devcmd_send_package_data(pdsc, dma_addr + offset,
409 copy_sz, offset, length);
410 if (err) {
411 NL_SET_ERR_MSG_MOD(priv->extack,
412 "Failed to send package data");
413 break;
414 }
415 }
416
417 if (err == -ETIMEDOUT || err == -EAGAIN) {
418 pdsc_deferred_dma_add(pdsc, deferred, dma_addr,
419 package_data, length, DMA_TO_DEVICE);
420 return err;
421 }
422
423 kfree(deferred);
424 dma_unmap_single(dev, dma_addr, length, DMA_TO_DEVICE);
425 kfree(package_data);
426 return err;
427 }
428
pdsc_component_type_exists(struct pdsc * pdsc,u8 type)429 static bool pdsc_component_type_exists(struct pdsc *pdsc, u8 type)
430 {
431 int i;
432
433 for (i = 0; i < pdsc->fw_components.num_components; i++) {
434 if (pdsc->fw_components.info[i].component_type == type)
435 return true;
436 }
437 return false;
438 }
439
pdsc_component_id_matches_type(struct pdsc * pdsc,u8 component_id,u8 type)440 static bool pdsc_component_id_matches_type(struct pdsc *pdsc,
441 u8 component_id, u8 type)
442 {
443 int i;
444
445 for (i = 0; i < pdsc->fw_components.num_components; i++) {
446 struct pds_core_fw_component_info *info =
447 &pdsc->fw_components.info[i];
448
449 if (info->identifier == component_id &&
450 info->component_type == type)
451 return true;
452 }
453 return false;
454 }
455
pdsc_get_component_type_by_id(struct pdsc * pdsc,u16 component_id)456 static u8 pdsc_get_component_type_by_id(struct pdsc *pdsc, u16 component_id)
457 {
458 int i;
459
460 for (i = 0; i < pdsc->fw_components.num_components; i++) {
461 struct pds_core_fw_component_info *info =
462 &pdsc->fw_components.info[i];
463
464 if (info->identifier == component_id)
465 return info->component_type;
466 }
467 return 0;
468 }
469
pdsc_skip_component(struct pds_core_fwu_priv * priv,u16 component_id)470 static bool pdsc_skip_component(struct pds_core_fwu_priv *priv,
471 u16 component_id)
472 {
473 struct pdsc_component_priv *component_priv;
474
475 list_for_each_entry(component_priv, &priv->components, list_entry) {
476 if (component_priv->component_id == component_id)
477 return component_priv->skip;
478 }
479
480 return false;
481 }
482
pdsc_send_component_table(struct pldmfw * context,struct pldmfw_component * component,u8 transfer_flag)483 static int pdsc_send_component_table(struct pldmfw *context,
484 struct pldmfw_component *component,
485 u8 transfer_flag)
486 {
487 struct pds_core_fwu_priv *priv =
488 container_of(context, struct pds_core_fwu_priv, context);
489 struct pds_core_component_tbl *component_tbl;
490 struct pdsc_component_priv *component_priv;
491 struct device *dev = context->dev;
492 union pds_core_dev_comp comp = {};
493 union pds_core_dev_cmd cmd = {};
494 struct pdsc *pdsc = priv->pdsc;
495 bool skip_component = false;
496 u8 requested_type = 0;
497 u16 buf_sz, tbl_sz;
498 int err = 0;
499
500 dev_dbg(dev,
501 "component name %s classification %u id %u activation_method %u ver_len %d ver_str %.*s index %u size %u transfer_flag 0x%02x\n",
502 priv->params->component, component->classification,
503 component->identifier, component->activation_method,
504 component->version_len, component->version_len,
505 component->version_string, component->index,
506 component->component_size, transfer_flag);
507
508 component_priv = kzalloc_obj(*component_priv, GFP_KERNEL);
509 if (!component_priv)
510 return -ENOMEM;
511
512 if (priv->params->component) {
513 requested_type = pdsc_name_to_fw_type(priv->params->component);
514 if (component->identifier > U8_MAX ||
515 !pdsc_component_id_matches_type(pdsc,
516 component->identifier,
517 requested_type)) {
518 skip_component = true;
519 goto add_component_priv;
520 }
521 priv->component_found = true;
522 }
523
524 buf_sz = sizeof(pdsc->cmd_regs->data);
525 tbl_sz = struct_size(component_tbl, version_str,
526 component->version_len);
527 if (tbl_sz > buf_sz) {
528 dev_err(dev, "component_tbl size %d too big, max size: %d\n",
529 tbl_sz, buf_sz);
530 err = -ENOSPC;
531 goto free_component_priv;
532 }
533 component_tbl = kzalloc(tbl_sz, GFP_KERNEL);
534 if (!component_tbl) {
535 err = -ENOMEM;
536 goto free_component_priv;
537 }
538
539 component_tbl->comparison_stamp =
540 cpu_to_le32(component->comparison_stamp);
541 component_tbl->classification = cpu_to_le16(component->classification);
542 component_tbl->identifier = cpu_to_le16(component->identifier);
543 component_tbl->transfer_flag = transfer_flag;
544 component_tbl->version_str_type = component->version_type;
545 component_tbl->version_str_len = component->version_len;
546 memcpy(component_tbl->version_str, component->version_string,
547 component->version_len);
548
549 cmd.send_component_tbl.opcode = PDS_CORE_CMD_SEND_COMPONENT_TBL;
550 cmd.send_component_tbl.ver = 1;
551 cmd.send_component_tbl.slot_id = PDS_CORE_FW_SLOT_INVALID;
552
553 err = pdsc_devcmd_with_data(pdsc, &cmd, component_tbl, tbl_sz,
554 &comp, pdsc->devcmd_timeout);
555 kfree(component_tbl);
556 if (err) {
557 dev_err(dev, "Failed sending component table: %pe\n",
558 ERR_PTR(err));
559 goto free_component_priv;
560 }
561
562 skip_component = comp.send_component_tbl.response == 1;
563
564 add_component_priv:
565 component_priv->skip = skip_component;
566 component_priv->component_id = component->identifier;
567 list_add(&component_priv->list_entry, &priv->components);
568
569 return 0;
570
571 free_component_priv:
572 kfree(component_priv);
573 return err;
574 }
575
pdsc_get_component_info(struct pdsc * pdsc)576 int pdsc_get_component_info(struct pdsc *pdsc)
577 {
578 union pds_core_dev_cmd cmd = {
579 .get_component_info.opcode = PDS_CORE_CMD_GET_COMPONENT_INFO,
580 .get_component_info.ver = 1,
581 };
582 struct pds_core_component_list_info *list_info;
583 struct pdsc_deferred_dma *deferred;
584 union pds_core_dev_comp comp = {};
585 dma_addr_t dma_addr;
586 u8 num_components;
587 int err, i;
588
589 deferred = kmalloc_obj(*deferred);
590 if (!deferred)
591 return -ENOMEM;
592
593 list_info = kzalloc(PDS_PAGE_SIZE, GFP_KERNEL);
594 if (!list_info) {
595 kfree(deferred);
596 return -ENOMEM;
597 }
598
599 dma_addr = dma_map_single(pdsc->dev, list_info, PDS_PAGE_SIZE,
600 DMA_FROM_DEVICE);
601 if (dma_mapping_error(pdsc->dev, dma_addr)) {
602 dev_err(pdsc->dev,
603 "Failed to dma_map component_list_info length %d\n",
604 PDS_PAGE_SIZE);
605 kfree(list_info);
606 kfree(deferred);
607 return -ENOMEM;
608 }
609
610 cmd.get_component_info.data_len = cpu_to_le16(PDS_PAGE_SIZE);
611 cmd.get_component_info.data_pa = cpu_to_le64(dma_addr);
612
613 err = pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout * 2);
614 if (err == -ETIMEDOUT || err == -EAGAIN) {
615 pdsc_deferred_dma_add(pdsc, deferred, dma_addr, list_info,
616 PDS_PAGE_SIZE, DMA_FROM_DEVICE);
617 return err;
618 }
619
620 kfree(deferred);
621 dma_unmap_single(pdsc->dev, dma_addr, PDS_PAGE_SIZE, DMA_FROM_DEVICE);
622 if (err)
623 goto out;
624
625 if (comp.get_component_info.ver == 0) {
626 /* Don't support backward compatibility as version 0 has
627 * alignment issues, so give a hint to users to update
628 * their firmware
629 */
630 dev_warn_once(pdsc->dev,
631 "Incompatible get_component_info version %u reported by firmware\n",
632 comp.get_component_info.ver);
633 err = 0;
634 goto out;
635 }
636
637 num_components = list_info->num_components;
638 if (num_components > PDS_CORE_FW_COMPONENT_LIST_LEN) {
639 err = -ENOMEM;
640 goto out;
641 }
642
643 pdsc->fw_components.num_components = num_components;
644 for (i = 0; i < num_components; i++) {
645 struct pds_core_fw_component_info *info =
646 &pdsc->fw_components.info[i];
647
648 memcpy(info, &list_info->info[i], sizeof(*info));
649 info->version[PDS_CORE_FW_COMPONENT_VER_BUFLEN - 1] = 0;
650 info->name[PDS_CORE_FW_COMPONENT_NAME_BUFLEN - 1] = 0;
651 }
652
653 out:
654 kfree(list_info);
655 return err;
656 }
657
pdsc_devcmd_send_component(struct pdsc * pdsc,struct pds_core_flash_component * info,u16 info_sz,dma_addr_t addr,u32 length,u32 offset,u16 slot_id,union pds_core_dev_comp * comp)658 static int pdsc_devcmd_send_component(struct pdsc *pdsc,
659 struct pds_core_flash_component *info,
660 u16 info_sz, dma_addr_t addr, u32 length,
661 u32 offset, u16 slot_id,
662 union pds_core_dev_comp *comp)
663 {
664 union pds_core_dev_cmd cmd = {
665 .send_component.opcode = PDS_CORE_CMD_SEND_COMPONENT,
666 .send_component.ver = 1,
667 .send_component.operation = PDS_CORE_SEND_COMPONENT_START,
668 .send_component.data_pa = cpu_to_le64(addr),
669 .send_component.data_len = cpu_to_le32(length),
670 .send_component.offset = cpu_to_le32(offset),
671 .send_component.slot_id = slot_id,
672 };
673 unsigned long timeout = 300 * HZ;
674 unsigned long start_time;
675 unsigned long end_time;
676 int err;
677
678 start_time = jiffies;
679 end_time = start_time + timeout;
680 do {
681 /* prevent noisy/benign devcmd failures */
682 err = pdsc_devcmd_with_data_nomsg(pdsc, &cmd, info, info_sz,
683 comp, 60);
684 if (err != -EAGAIN)
685 break;
686
687 /* if required, subsequent commands check status of
688 * PDS_CORE_CMD_SEND_COMPONENT command, which returns
689 * EAGAIN while the command is still running,
690 * else we get the final command status.
691 */
692 cmd.send_component.operation = PDS_CORE_SEND_COMPONENT_STATUS;
693 msleep(20);
694 } while (time_before(jiffies, end_time));
695
696 if (err == -EAGAIN || err == -ETIMEDOUT)
697 dev_err(pdsc->dev, "PDS_CORE_CMD_SEND_COMPONENT timed out\n");
698
699 return err;
700 }
701
pdsc_flash_component_chunk(struct pdsc * pdsc,struct device * dev,struct pds_core_flash_component * info,u16 info_sz,const u8 * data,u16 copy_sz,u32 offset,u8 slot_id,union pds_core_dev_comp * comp)702 static int pdsc_flash_component_chunk(struct pdsc *pdsc, struct device *dev,
703 struct pds_core_flash_component *info,
704 u16 info_sz, const u8 *data, u16 copy_sz,
705 u32 offset, u8 slot_id,
706 union pds_core_dev_comp *comp)
707 {
708 struct pdsc_deferred_dma *deferred;
709 dma_addr_t dma_addr;
710 u8 *component_data;
711 int err;
712
713 deferred = kmalloc_obj(*deferred, GFP_KERNEL);
714 if (!deferred)
715 return -ENOMEM;
716
717 component_data = kmemdup(data, copy_sz, GFP_KERNEL);
718 if (!component_data) {
719 kfree(deferred);
720 return -ENOMEM;
721 }
722
723 dma_addr = dma_map_single(dev, component_data, copy_sz, DMA_TO_DEVICE);
724 if (dma_mapping_error(dev, dma_addr)) {
725 dev_err(dev,
726 "Failed to dma_map component_data at offset 0x%x copy_sz 0x%x\n",
727 offset, copy_sz);
728 kfree(component_data);
729 kfree(deferred);
730 return -ENOMEM;
731 }
732
733 err = pdsc_devcmd_send_component(pdsc, info, info_sz, dma_addr,
734 copy_sz, offset, slot_id, comp);
735 if (err == -ETIMEDOUT || err == -EAGAIN) {
736 pdsc_deferred_dma_add(pdsc, deferred, dma_addr,
737 component_data, copy_sz, DMA_TO_DEVICE);
738 return err;
739 }
740
741 kfree(deferred);
742 dma_unmap_single(dev, dma_addr, copy_sz, DMA_TO_DEVICE);
743 kfree(component_data);
744
745 return err;
746 }
747
pdsc_flash_component(struct pldmfw * context,struct pldmfw_component * component)748 static int pdsc_flash_component(struct pldmfw *context,
749 struct pldmfw_component *component)
750 {
751 char component_name_buf[PDSC_FW_COMPONENT_FULL_NAME_BUFLEN];
752 struct pds_core_fwu_priv *priv =
753 container_of(context, struct pds_core_fwu_priv, context);
754 struct pds_core_flash_component *component_info;
755 const char *component_name = NULL;
756 struct device *dev = context->dev;
757 struct pdsc *pdsc = priv->pdsc;
758 u16 buf_sz, info_sz;
759 struct devlink *dl;
760 u8 component_type;
761 u32 total_len;
762 u32 offset;
763 int err;
764
765 component_type = pdsc_get_component_type_by_id(pdsc,
766 component->identifier);
767 if (component_type) {
768 const char *type_name = pdsc_fw_type_to_name(component_type);
769
770 if (component_type == PDS_CORE_FW_TYPE_MAIN) {
771 component_name = "fw";
772 } else if (type_name) {
773 snprintf(component_name_buf, sizeof(component_name_buf),
774 "%s%s", PDSC_FW_COMPONENT_PREFIX, type_name);
775 component_name = component_name_buf;
776 }
777 }
778
779 dl = priv_to_devlink(pdsc);
780
781 if (pdsc_skip_component(priv, component->identifier)) {
782 devlink_flash_update_status_notify(dl, "Skipped",
783 component_name, 0, 0);
784 return 0;
785 }
786
787 total_len = component->component_size;
788 dev_dbg(dev,
789 "component name %s class %u id %u act_meth %u ver_str %.*s index %u size %u\n",
790 component_name ?: "(unknown)", component->classification,
791 component->identifier, component->activation_method,
792 component->version_len, component->version_string,
793 component->index, component->component_size);
794
795 buf_sz = sizeof(pdsc->cmd_regs->data);
796 info_sz = struct_size(component_info, version_str,
797 component->version_len);
798 if (info_sz > buf_sz) {
799 dev_err(dev, "component_info size %d too big, max size: %d\n",
800 info_sz, buf_sz);
801 return -ENOSPC;
802 }
803 component_info = vzalloc(info_sz);
804 if (!component_info)
805 return -ENOMEM;
806
807 component_info->comparison_stamp =
808 cpu_to_le32(component->comparison_stamp);
809 component_info->image_size = cpu_to_le32(total_len);
810 component_info->classification = cpu_to_le16(component->classification);
811 component_info->identifier = cpu_to_le16(component->identifier);
812 component_info->options = cpu_to_le16(component->options);
813 component_info->version_str_type = component->version_type;
814 component_info->version_str_len = component->version_len;
815 memcpy(component_info->version_str, component->version_string,
816 component->version_len);
817
818 offset = 0;
819 while (offset < total_len) {
820 union pds_core_dev_comp comp = {};
821 u16 copy_sz;
822
823 copy_sz = min_t(unsigned int, PDS_PAGE_SIZE,
824 total_len - offset);
825
826 err = pdsc_flash_component_chunk(pdsc, dev, component_info,
827 info_sz,
828 component->component_data +
829 offset, copy_sz, offset,
830 PDS_CORE_FW_SLOT_INVALID,
831 &comp);
832 if (err &&
833 comp.send_component.compat_response &&
834 (comp.send_component.compat_response_code ==
835 PDS_CORE_COMPONENT_STAMP_IDENTICAL ||
836 comp.send_component.compat_response_code ==
837 PDS_CORE_COMPONENT_STAMP_LOWER)) {
838 err = 0;
839 devlink_flash_update_status_notify(dl, "Skipped",
840 component_name,
841 0, 0);
842 goto skip_component;
843 }
844
845 if (err) {
846 NL_SET_ERR_MSG_MOD(priv->extack,
847 "Failed to flash component");
848 goto err_out;
849 }
850
851 offset += copy_sz;
852 devlink_flash_update_status_notify(dl,
853 "Erasing/Flashing",
854 component_name, offset,
855 total_len);
856 }
857
858 vfree(component_info);
859 return 0;
860
861 err_out:
862 devlink_flash_update_status_notify(dl,
863 "Erasing/Flashing Component Failed",
864 component_name, 0, 0);
865 skip_component:
866 vfree(component_info);
867 return err;
868 }
869
pdsc_devcmd_finalize_update(struct pdsc * pdsc)870 static int pdsc_devcmd_finalize_update(struct pdsc *pdsc)
871 {
872 union pds_core_dev_cmd cmd = {
873 .finalize_update.opcode = PDS_CORE_CMD_FINALIZE_UPDATE,
874 .finalize_update.ver = 1,
875 };
876 union pds_core_dev_comp comp = {};
877
878 return pdsc_devcmd(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
879 }
880
pdsc_finalize_update(struct pldmfw * context)881 static int pdsc_finalize_update(struct pldmfw *context)
882 {
883 struct pds_core_fwu_priv *priv =
884 container_of(context, struct pds_core_fwu_priv, context);
885 const char *component_name = priv->params->component;
886 unsigned long start_time, end_time;
887 struct device *dev = context->dev;
888 struct pdsc *pdsc = priv->pdsc;
889 struct devlink *dl;
890 int err;
891
892 dl = priv_to_devlink(pdsc);
893
894 start_time = jiffies;
895 end_time = start_time + (PDSC_FW_INSTALL_TIMEOUT * HZ);
896 do {
897 err = pdsc_devcmd_finalize_update(pdsc);
898 if (err != -EAGAIN)
899 break;
900
901 dev_dbg(dev, "retrying finalize_update: %pe\n", ERR_PTR(err));
902 msleep(20);
903 } while (time_before(jiffies, end_time) && err == -EAGAIN);
904
905 if (err) {
906 devlink_flash_update_status_notify(dl, "Finalize Update Failed",
907 component_name, 0, 0);
908 NL_SET_ERR_MSG_MOD(priv->extack, "Finalize update failed");
909 return err;
910 }
911
912 devlink_flash_update_status_notify(dl, "Finalized Update",
913 component_name, 0, 0);
914 return 0;
915 }
916
917 static const struct pldmfw_ops pdsc_pldmfw_ops = {
918 .match_record = pdsc_match_record_descs,
919 .send_package_data = pdsc_send_package_data,
920 .send_component_table = pdsc_send_component_table,
921 .flash_component = pdsc_flash_component,
922 .finalize_update = pdsc_finalize_update
923 };
924
pdsc_pldm_firmware_update(struct pdsc * pdsc,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack,const struct firmware * fw)925 static int pdsc_pldm_firmware_update(struct pdsc *pdsc,
926 struct devlink_flash_update_params *params,
927 struct netlink_ext_ack *extack,
928 const struct firmware *fw)
929 {
930 struct pds_core_fwu_priv priv = {};
931 int err;
932
933 if (!pdsc->fw_components.num_components) {
934 err = pdsc_get_component_info(pdsc);
935 if (err) {
936 NL_SET_ERR_MSG_MOD(extack,
937 "Failed to get component info");
938 return err;
939 }
940 }
941
942 if (params->component) {
943 u8 type = pdsc_name_to_fw_type(params->component);
944
945 if (!type || !pdsc_component_type_exists(pdsc, type)) {
946 NL_SET_ERR_MSG_MOD(extack, "Unknown component name");
947 return -ENOENT;
948 }
949 }
950
951 INIT_LIST_HEAD(&priv.components);
952 priv.context.ops = &pdsc_pldmfw_ops;
953 priv.context.dev = pdsc->dev;
954 priv.params = params;
955 priv.extack = extack;
956 priv.pdsc = pdsc;
957
958 err = pldmfw_flash_image(&priv.context, fw);
959 if (!err && params->component && !priv.component_found) {
960 NL_SET_ERR_MSG_MOD(extack,
961 "Requested component not present in firmware package");
962 err = -ENOENT;
963 }
964 pdsc_free_fwu_priv(&priv);
965
966 return err;
967 }
968
pdsc_firmware_update(struct pdsc * pdsc,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)969 int pdsc_firmware_update(struct pdsc *pdsc,
970 struct devlink_flash_update_params *params,
971 struct netlink_ext_ack *extack)
972 {
973 int err;
974
975 if (pdsc->dev_ident.version >= PDS_CORE_IDENTITY_VERSION_2 &&
976 pdsc->dev_ident.capabilities &
977 cpu_to_le64(PDS_CORE_DEV_CAP_PLDM_FW_UPDATE))
978 err = pdsc_pldm_firmware_update(pdsc, params, extack,
979 params->fw);
980 else
981 err = pdsc_legacy_firmware_update(pdsc, params, extack);
982
983 /* Invalidate cached component info so next info_get refreshes */
984 pdsc_fw_components_invalidate(pdsc);
985
986 return err;
987 }
988