Skip to content

Commit 670e4e8

Browse files
davejiangdjbw
authored andcommitted
cxl: Add checksum verification to CDAT from CXL
A CDAT table is available from a CXL device. The table is read by the driver and cached in software. With the CXL subsystem needing to parse the CDAT table, the checksum should be verified. Add checksum verification after the CDAT table is read from device. Reviewed-by: Ira Weiny <ira.weiny@intel.com> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net> Signed-off-by: Dave Jiang <dave.jiang@intel.com> Link: https://lore.kernel.org/r/169713682277.2205276.2687265961314933628.stgit@djiang5-mobl3 Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent 529c0a4 commit 670e4e8

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

drivers/cxl/core/pci.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,16 @@ static int cxl_cdat_read_table(struct device *dev,
595595
return 0;
596596
}
597597

598+
static unsigned char cdat_checksum(void *buf, size_t size)
599+
{
600+
unsigned char sum, *data = buf;
601+
size_t i;
602+
603+
for (sum = 0, i = 0; i < size; i++)
604+
sum += data[i];
605+
return sum;
606+
}
607+
598608
/**
599609
* read_cdat_data - Read the CDAT data on this port
600610
* @port: Port to read data from
@@ -634,15 +644,21 @@ void read_cdat_data(struct cxl_port *port)
634644
return;
635645

636646
rc = cxl_cdat_read_table(dev, cdat_doe, cdat_table, &cdat_length);
637-
if (rc) {
638-
/* Don't leave table data allocated on error */
639-
devm_kfree(dev, cdat_table);
640-
dev_err(dev, "CDAT data read error\n");
641-
return;
642-
}
647+
if (rc)
648+
goto err;
643649

644-
port->cdat.table = cdat_table + sizeof(__le32);
650+
cdat_table = cdat_table + sizeof(__le32);
651+
if (cdat_checksum(cdat_table, cdat_length))
652+
goto err;
653+
654+
port->cdat.table = cdat_table;
645655
port->cdat.length = cdat_length;
656+
return;
657+
658+
err:
659+
/* Don't leave table data allocated on error */
660+
devm_kfree(dev, cdat_table);
661+
dev_err(dev, "Failed to read/validate CDAT.\n");
646662
}
647663
EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
648664

0 commit comments

Comments
 (0)