|
45 | 45 | #include <linux/etherdevice.h> |
46 | 46 | #include <linux/vmalloc.h> |
47 | 47 | #include <linux/crash_dump.h> |
| 48 | +#include <linux/crc32.h> |
48 | 49 | #include <linux/qed/qed_if.h> |
49 | 50 | #include <linux/qed/qed_ll2_if.h> |
50 | 51 |
|
@@ -1553,6 +1554,342 @@ static int qed_drain(struct qed_dev *cdev) |
1553 | 1554 | return 0; |
1554 | 1555 | } |
1555 | 1556 |
|
| 1557 | +static u32 qed_nvm_flash_image_access_crc(struct qed_dev *cdev, |
| 1558 | + struct qed_nvm_image_att *nvm_image, |
| 1559 | + u32 *crc) |
| 1560 | +{ |
| 1561 | + u8 *buf = NULL; |
| 1562 | + int rc, j; |
| 1563 | + u32 val; |
| 1564 | + |
| 1565 | + /* Allocate a buffer for holding the nvram image */ |
| 1566 | + buf = kzalloc(nvm_image->length, GFP_KERNEL); |
| 1567 | + if (!buf) |
| 1568 | + return -ENOMEM; |
| 1569 | + |
| 1570 | + /* Read image into buffer */ |
| 1571 | + rc = qed_mcp_nvm_read(cdev, nvm_image->start_addr, |
| 1572 | + buf, nvm_image->length); |
| 1573 | + if (rc) { |
| 1574 | + DP_ERR(cdev, "Failed reading image from nvm\n"); |
| 1575 | + goto out; |
| 1576 | + } |
| 1577 | + |
| 1578 | + /* Convert the buffer into big-endian format (excluding the |
| 1579 | + * closing 4 bytes of CRC). |
| 1580 | + */ |
| 1581 | + for (j = 0; j < nvm_image->length - 4; j += 4) { |
| 1582 | + val = cpu_to_be32(*(u32 *)&buf[j]); |
| 1583 | + *(u32 *)&buf[j] = val; |
| 1584 | + } |
| 1585 | + |
| 1586 | + /* Calc CRC for the "actual" image buffer, i.e. not including |
| 1587 | + * the last 4 CRC bytes. |
| 1588 | + */ |
| 1589 | + *crc = (~cpu_to_be32(crc32(0xffffffff, buf, nvm_image->length - 4))); |
| 1590 | + |
| 1591 | +out: |
| 1592 | + kfree(buf); |
| 1593 | + |
| 1594 | + return rc; |
| 1595 | +} |
| 1596 | + |
| 1597 | +/* Binary file format - |
| 1598 | + * /----------------------------------------------------------------------\ |
| 1599 | + * 0B | 0x4 [command index] | |
| 1600 | + * 4B | image_type | Options | Number of register settings | |
| 1601 | + * 8B | Value | |
| 1602 | + * 12B | Mask | |
| 1603 | + * 16B | Offset | |
| 1604 | + * \----------------------------------------------------------------------/ |
| 1605 | + * There can be several Value-Mask-Offset sets as specified by 'Number of...'. |
| 1606 | + * Options - 0'b - Calculate & Update CRC for image |
| 1607 | + */ |
| 1608 | +static int qed_nvm_flash_image_access(struct qed_dev *cdev, const u8 **data, |
| 1609 | + bool *check_resp) |
| 1610 | +{ |
| 1611 | + struct qed_nvm_image_att nvm_image; |
| 1612 | + struct qed_hwfn *p_hwfn; |
| 1613 | + bool is_crc = false; |
| 1614 | + u32 image_type; |
| 1615 | + int rc = 0, i; |
| 1616 | + u16 len; |
| 1617 | + |
| 1618 | + *data += 4; |
| 1619 | + image_type = **data; |
| 1620 | + p_hwfn = QED_LEADING_HWFN(cdev); |
| 1621 | + for (i = 0; i < p_hwfn->nvm_info.num_images; i++) |
| 1622 | + if (image_type == p_hwfn->nvm_info.image_att[i].image_type) |
| 1623 | + break; |
| 1624 | + if (i == p_hwfn->nvm_info.num_images) { |
| 1625 | + DP_ERR(cdev, "Failed to find nvram image of type %08x\n", |
| 1626 | + image_type); |
| 1627 | + return -ENOENT; |
| 1628 | + } |
| 1629 | + |
| 1630 | + nvm_image.start_addr = p_hwfn->nvm_info.image_att[i].nvm_start_addr; |
| 1631 | + nvm_image.length = p_hwfn->nvm_info.image_att[i].len; |
| 1632 | + |
| 1633 | + DP_VERBOSE(cdev, NETIF_MSG_DRV, |
| 1634 | + "Read image %02x; type = %08x; NVM [%08x,...,%08x]\n", |
| 1635 | + **data, image_type, nvm_image.start_addr, |
| 1636 | + nvm_image.start_addr + nvm_image.length - 1); |
| 1637 | + (*data)++; |
| 1638 | + is_crc = !!(**data & BIT(0)); |
| 1639 | + (*data)++; |
| 1640 | + len = *((u16 *)*data); |
| 1641 | + *data += 2; |
| 1642 | + if (is_crc) { |
| 1643 | + u32 crc = 0; |
| 1644 | + |
| 1645 | + rc = qed_nvm_flash_image_access_crc(cdev, &nvm_image, &crc); |
| 1646 | + if (rc) { |
| 1647 | + DP_ERR(cdev, "Failed calculating CRC, rc = %d\n", rc); |
| 1648 | + goto exit; |
| 1649 | + } |
| 1650 | + |
| 1651 | + rc = qed_mcp_nvm_write(cdev, QED_NVM_WRITE_NVRAM, |
| 1652 | + (nvm_image.start_addr + |
| 1653 | + nvm_image.length - 4), (u8 *)&crc, 4); |
| 1654 | + if (rc) |
| 1655 | + DP_ERR(cdev, "Failed writing to %08x, rc = %d\n", |
| 1656 | + nvm_image.start_addr + nvm_image.length - 4, rc); |
| 1657 | + goto exit; |
| 1658 | + } |
| 1659 | + |
| 1660 | + /* Iterate over the values for setting */ |
| 1661 | + while (len) { |
| 1662 | + u32 offset, mask, value, cur_value; |
| 1663 | + u8 buf[4]; |
| 1664 | + |
| 1665 | + value = *((u32 *)*data); |
| 1666 | + *data += 4; |
| 1667 | + mask = *((u32 *)*data); |
| 1668 | + *data += 4; |
| 1669 | + offset = *((u32 *)*data); |
| 1670 | + *data += 4; |
| 1671 | + |
| 1672 | + rc = qed_mcp_nvm_read(cdev, nvm_image.start_addr + offset, buf, |
| 1673 | + 4); |
| 1674 | + if (rc) { |
| 1675 | + DP_ERR(cdev, "Failed reading from %08x\n", |
| 1676 | + nvm_image.start_addr + offset); |
| 1677 | + goto exit; |
| 1678 | + } |
| 1679 | + |
| 1680 | + cur_value = le32_to_cpu(*((__le32 *)buf)); |
| 1681 | + DP_VERBOSE(cdev, NETIF_MSG_DRV, |
| 1682 | + "NVM %08x: %08x -> %08x [Value %08x Mask %08x]\n", |
| 1683 | + nvm_image.start_addr + offset, cur_value, |
| 1684 | + (cur_value & ~mask) | (value & mask), value, mask); |
| 1685 | + value = (value & mask) | (cur_value & ~mask); |
| 1686 | + rc = qed_mcp_nvm_write(cdev, QED_NVM_WRITE_NVRAM, |
| 1687 | + nvm_image.start_addr + offset, |
| 1688 | + (u8 *)&value, 4); |
| 1689 | + if (rc) { |
| 1690 | + DP_ERR(cdev, "Failed writing to %08x\n", |
| 1691 | + nvm_image.start_addr + offset); |
| 1692 | + goto exit; |
| 1693 | + } |
| 1694 | + |
| 1695 | + len--; |
| 1696 | + } |
| 1697 | +exit: |
| 1698 | + return rc; |
| 1699 | +} |
| 1700 | + |
| 1701 | +/* Binary file format - |
| 1702 | + * /----------------------------------------------------------------------\ |
| 1703 | + * 0B | 0x3 [command index] | |
| 1704 | + * 4B | b'0: check_response? | b'1-31 reserved | |
| 1705 | + * 8B | File-type | reserved | |
| 1706 | + * \----------------------------------------------------------------------/ |
| 1707 | + * Start a new file of the provided type |
| 1708 | + */ |
| 1709 | +static int qed_nvm_flash_image_file_start(struct qed_dev *cdev, |
| 1710 | + const u8 **data, bool *check_resp) |
| 1711 | +{ |
| 1712 | + int rc; |
| 1713 | + |
| 1714 | + *data += 4; |
| 1715 | + *check_resp = !!(**data & BIT(0)); |
| 1716 | + *data += 4; |
| 1717 | + |
| 1718 | + DP_VERBOSE(cdev, NETIF_MSG_DRV, |
| 1719 | + "About to start a new file of type %02x\n", **data); |
| 1720 | + rc = qed_mcp_nvm_put_file_begin(cdev, **data); |
| 1721 | + *data += 4; |
| 1722 | + |
| 1723 | + return rc; |
| 1724 | +} |
| 1725 | + |
| 1726 | +/* Binary file format - |
| 1727 | + * /----------------------------------------------------------------------\ |
| 1728 | + * 0B | 0x2 [command index] | |
| 1729 | + * 4B | Length in bytes | |
| 1730 | + * 8B | b'0: check_response? | b'1-31 reserved | |
| 1731 | + * 12B | Offset in bytes | |
| 1732 | + * 16B | Data ... | |
| 1733 | + * \----------------------------------------------------------------------/ |
| 1734 | + * Write data as part of a file that was previously started. Data should be |
| 1735 | + * of length equal to that provided in the message |
| 1736 | + */ |
| 1737 | +static int qed_nvm_flash_image_file_data(struct qed_dev *cdev, |
| 1738 | + const u8 **data, bool *check_resp) |
| 1739 | +{ |
| 1740 | + u32 offset, len; |
| 1741 | + int rc; |
| 1742 | + |
| 1743 | + *data += 4; |
| 1744 | + len = *((u32 *)(*data)); |
| 1745 | + *data += 4; |
| 1746 | + *check_resp = !!(**data & BIT(0)); |
| 1747 | + *data += 4; |
| 1748 | + offset = *((u32 *)(*data)); |
| 1749 | + *data += 4; |
| 1750 | + |
| 1751 | + DP_VERBOSE(cdev, NETIF_MSG_DRV, |
| 1752 | + "About to write File-data: %08x bytes to offset %08x\n", |
| 1753 | + len, offset); |
| 1754 | + |
| 1755 | + rc = qed_mcp_nvm_write(cdev, QED_PUT_FILE_DATA, offset, |
| 1756 | + (char *)(*data), len); |
| 1757 | + *data += len; |
| 1758 | + |
| 1759 | + return rc; |
| 1760 | +} |
| 1761 | + |
| 1762 | +/* Binary file format [General header] - |
| 1763 | + * /----------------------------------------------------------------------\ |
| 1764 | + * 0B | QED_NVM_SIGNATURE | |
| 1765 | + * 4B | Length in bytes | |
| 1766 | + * 8B | Highest command in this batchfile | Reserved | |
| 1767 | + * \----------------------------------------------------------------------/ |
| 1768 | + */ |
| 1769 | +static int qed_nvm_flash_image_validate(struct qed_dev *cdev, |
| 1770 | + const struct firmware *image, |
| 1771 | + const u8 **data) |
| 1772 | +{ |
| 1773 | + u32 signature, len; |
| 1774 | + |
| 1775 | + /* Check minimum size */ |
| 1776 | + if (image->size < 12) { |
| 1777 | + DP_ERR(cdev, "Image is too short [%08x]\n", (u32)image->size); |
| 1778 | + return -EINVAL; |
| 1779 | + } |
| 1780 | + |
| 1781 | + /* Check signature */ |
| 1782 | + signature = *((u32 *)(*data)); |
| 1783 | + if (signature != QED_NVM_SIGNATURE) { |
| 1784 | + DP_ERR(cdev, "Wrong signature '%08x'\n", signature); |
| 1785 | + return -EINVAL; |
| 1786 | + } |
| 1787 | + |
| 1788 | + *data += 4; |
| 1789 | + /* Validate internal size equals the image-size */ |
| 1790 | + len = *((u32 *)(*data)); |
| 1791 | + if (len != image->size) { |
| 1792 | + DP_ERR(cdev, "Size mismatch: internal = %08x image = %08x\n", |
| 1793 | + len, (u32)image->size); |
| 1794 | + return -EINVAL; |
| 1795 | + } |
| 1796 | + |
| 1797 | + *data += 4; |
| 1798 | + /* Make sure driver familiar with all commands necessary for this */ |
| 1799 | + if (*((u16 *)(*data)) >= QED_NVM_FLASH_CMD_NVM_MAX) { |
| 1800 | + DP_ERR(cdev, "File contains unsupported commands [Need %04x]\n", |
| 1801 | + *((u16 *)(*data))); |
| 1802 | + return -EINVAL; |
| 1803 | + } |
| 1804 | + |
| 1805 | + *data += 4; |
| 1806 | + |
| 1807 | + return 0; |
| 1808 | +} |
| 1809 | + |
| 1810 | +static int qed_nvm_flash(struct qed_dev *cdev, const char *name) |
| 1811 | +{ |
| 1812 | + const struct firmware *image; |
| 1813 | + const u8 *data, *data_end; |
| 1814 | + u32 cmd_type; |
| 1815 | + int rc; |
| 1816 | + |
| 1817 | + rc = request_firmware(&image, name, &cdev->pdev->dev); |
| 1818 | + if (rc) { |
| 1819 | + DP_ERR(cdev, "Failed to find '%s'\n", name); |
| 1820 | + return rc; |
| 1821 | + } |
| 1822 | + |
| 1823 | + DP_VERBOSE(cdev, NETIF_MSG_DRV, |
| 1824 | + "Flashing '%s' - firmware's data at %p, size is %08x\n", |
| 1825 | + name, image->data, (u32)image->size); |
| 1826 | + data = image->data; |
| 1827 | + data_end = data + image->size; |
| 1828 | + |
| 1829 | + rc = qed_nvm_flash_image_validate(cdev, image, &data); |
| 1830 | + if (rc) |
| 1831 | + goto exit; |
| 1832 | + |
| 1833 | + while (data < data_end) { |
| 1834 | + bool check_resp = false; |
| 1835 | + |
| 1836 | + /* Parse the actual command */ |
| 1837 | + cmd_type = *((u32 *)data); |
| 1838 | + switch (cmd_type) { |
| 1839 | + case QED_NVM_FLASH_CMD_FILE_DATA: |
| 1840 | + rc = qed_nvm_flash_image_file_data(cdev, &data, |
| 1841 | + &check_resp); |
| 1842 | + break; |
| 1843 | + case QED_NVM_FLASH_CMD_FILE_START: |
| 1844 | + rc = qed_nvm_flash_image_file_start(cdev, &data, |
| 1845 | + &check_resp); |
| 1846 | + break; |
| 1847 | + case QED_NVM_FLASH_CMD_NVM_CHANGE: |
| 1848 | + rc = qed_nvm_flash_image_access(cdev, &data, |
| 1849 | + &check_resp); |
| 1850 | + break; |
| 1851 | + default: |
| 1852 | + DP_ERR(cdev, "Unknown command %08x\n", cmd_type); |
| 1853 | + rc = -EINVAL; |
| 1854 | + goto exit; |
| 1855 | + } |
| 1856 | + |
| 1857 | + if (rc) { |
| 1858 | + DP_ERR(cdev, "Command %08x failed\n", cmd_type); |
| 1859 | + goto exit; |
| 1860 | + } |
| 1861 | + |
| 1862 | + /* Check response if needed */ |
| 1863 | + if (check_resp) { |
| 1864 | + u32 mcp_response = 0; |
| 1865 | + |
| 1866 | + if (qed_mcp_nvm_resp(cdev, (u8 *)&mcp_response)) { |
| 1867 | + DP_ERR(cdev, "Failed getting MCP response\n"); |
| 1868 | + rc = -EINVAL; |
| 1869 | + goto exit; |
| 1870 | + } |
| 1871 | + |
| 1872 | + switch (mcp_response & FW_MSG_CODE_MASK) { |
| 1873 | + case FW_MSG_CODE_OK: |
| 1874 | + case FW_MSG_CODE_NVM_OK: |
| 1875 | + case FW_MSG_CODE_NVM_PUT_FILE_FINISH_OK: |
| 1876 | + case FW_MSG_CODE_PHY_OK: |
| 1877 | + break; |
| 1878 | + default: |
| 1879 | + DP_ERR(cdev, "MFW returns error: %08x\n", |
| 1880 | + mcp_response); |
| 1881 | + rc = -EINVAL; |
| 1882 | + goto exit; |
| 1883 | + } |
| 1884 | + } |
| 1885 | + } |
| 1886 | + |
| 1887 | +exit: |
| 1888 | + release_firmware(image); |
| 1889 | + |
| 1890 | + return rc; |
| 1891 | +} |
| 1892 | + |
1556 | 1893 | static int qed_nvm_get_image(struct qed_dev *cdev, enum qed_nvm_images type, |
1557 | 1894 | u8 *buf, u16 len) |
1558 | 1895 | { |
@@ -1719,6 +2056,7 @@ const struct qed_common_ops qed_common_ops_pass = { |
1719 | 2056 | .dbg_all_data_size = &qed_dbg_all_data_size, |
1720 | 2057 | .chain_alloc = &qed_chain_alloc, |
1721 | 2058 | .chain_free = &qed_chain_free, |
| 2059 | + .nvm_flash = &qed_nvm_flash, |
1722 | 2060 | .nvm_get_image = &qed_nvm_get_image, |
1723 | 2061 | .set_coalesce = &qed_set_coalesce, |
1724 | 2062 | .set_led = &qed_set_led, |
|
0 commit comments