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