152857e68SIwona Winiarska // SPDX-License-Identifier: GPL-2.0-only
252857e68SIwona Winiarska // Copyright (c) 2021 Intel Corporation
352857e68SIwona Winiarska
46b8145b0SIwona Winiarska #include <linux/bug.h>
552857e68SIwona Winiarska #include <linux/export.h>
6*93e1821cSIwona Winiarska #include <linux/pci.h>
752857e68SIwona Winiarska #include <linux/peci.h>
852857e68SIwona Winiarska #include <linux/slab.h>
952857e68SIwona Winiarska #include <linux/types.h>
1052857e68SIwona Winiarska
116b8145b0SIwona Winiarska #include <asm/unaligned.h>
126b8145b0SIwona Winiarska
1352857e68SIwona Winiarska #include "internal.h"
1452857e68SIwona Winiarska
156b8145b0SIwona Winiarska #define PECI_GET_DIB_CMD 0xf7
166b8145b0SIwona Winiarska #define PECI_GET_DIB_WR_LEN 1
176b8145b0SIwona Winiarska #define PECI_GET_DIB_RD_LEN 8
186b8145b0SIwona Winiarska
19*93e1821cSIwona Winiarska #define PECI_GET_TEMP_CMD 0x01
20*93e1821cSIwona Winiarska #define PECI_GET_TEMP_WR_LEN 1
21*93e1821cSIwona Winiarska #define PECI_GET_TEMP_RD_LEN 2
22*93e1821cSIwona Winiarska
236b8145b0SIwona Winiarska #define PECI_RDPKGCFG_CMD 0xa1
246b8145b0SIwona Winiarska #define PECI_RDPKGCFG_WR_LEN 5
256b8145b0SIwona Winiarska #define PECI_RDPKGCFG_RD_LEN_BASE 1
266b8145b0SIwona Winiarska #define PECI_WRPKGCFG_CMD 0xa5
276b8145b0SIwona Winiarska #define PECI_WRPKGCFG_WR_LEN_BASE 6
286b8145b0SIwona Winiarska #define PECI_WRPKGCFG_RD_LEN 1
296b8145b0SIwona Winiarska
30*93e1821cSIwona Winiarska #define PECI_RDIAMSR_CMD 0xb1
31*93e1821cSIwona Winiarska #define PECI_RDIAMSR_WR_LEN 5
32*93e1821cSIwona Winiarska #define PECI_RDIAMSR_RD_LEN 9
33*93e1821cSIwona Winiarska #define PECI_WRIAMSR_CMD 0xb5
34*93e1821cSIwona Winiarska #define PECI_RDIAMSREX_CMD 0xd1
35*93e1821cSIwona Winiarska #define PECI_RDIAMSREX_WR_LEN 6
36*93e1821cSIwona Winiarska #define PECI_RDIAMSREX_RD_LEN 9
37*93e1821cSIwona Winiarska
38*93e1821cSIwona Winiarska #define PECI_RDPCICFG_CMD 0x61
39*93e1821cSIwona Winiarska #define PECI_RDPCICFG_WR_LEN 6
40*93e1821cSIwona Winiarska #define PECI_RDPCICFG_RD_LEN 5
41*93e1821cSIwona Winiarska #define PECI_RDPCICFG_RD_LEN_MAX 24
42*93e1821cSIwona Winiarska #define PECI_WRPCICFG_CMD 0x65
43*93e1821cSIwona Winiarska
44*93e1821cSIwona Winiarska #define PECI_RDPCICFGLOCAL_CMD 0xe1
45*93e1821cSIwona Winiarska #define PECI_RDPCICFGLOCAL_WR_LEN 5
46*93e1821cSIwona Winiarska #define PECI_RDPCICFGLOCAL_RD_LEN_BASE 1
47*93e1821cSIwona Winiarska #define PECI_WRPCICFGLOCAL_CMD 0xe5
48*93e1821cSIwona Winiarska #define PECI_WRPCICFGLOCAL_WR_LEN_BASE 6
49*93e1821cSIwona Winiarska #define PECI_WRPCICFGLOCAL_RD_LEN 1
50*93e1821cSIwona Winiarska
51*93e1821cSIwona Winiarska #define PECI_ENDPTCFG_TYPE_LOCAL_PCI 0x03
52*93e1821cSIwona Winiarska #define PECI_ENDPTCFG_TYPE_PCI 0x04
53*93e1821cSIwona Winiarska #define PECI_ENDPTCFG_TYPE_MMIO 0x05
54*93e1821cSIwona Winiarska #define PECI_ENDPTCFG_ADDR_TYPE_PCI 0x04
55*93e1821cSIwona Winiarska #define PECI_ENDPTCFG_ADDR_TYPE_MMIO_D 0x05
56*93e1821cSIwona Winiarska #define PECI_ENDPTCFG_ADDR_TYPE_MMIO_Q 0x06
57*93e1821cSIwona Winiarska #define PECI_RDENDPTCFG_CMD 0xc1
58*93e1821cSIwona Winiarska #define PECI_RDENDPTCFG_PCI_WR_LEN 12
59*93e1821cSIwona Winiarska #define PECI_RDENDPTCFG_MMIO_WR_LEN_BASE 10
60*93e1821cSIwona Winiarska #define PECI_RDENDPTCFG_MMIO_D_WR_LEN 14
61*93e1821cSIwona Winiarska #define PECI_RDENDPTCFG_MMIO_Q_WR_LEN 18
62*93e1821cSIwona Winiarska #define PECI_RDENDPTCFG_RD_LEN_BASE 1
63*93e1821cSIwona Winiarska #define PECI_WRENDPTCFG_CMD 0xc5
64*93e1821cSIwona Winiarska #define PECI_WRENDPTCFG_PCI_WR_LEN_BASE 13
65*93e1821cSIwona Winiarska #define PECI_WRENDPTCFG_MMIO_D_WR_LEN_BASE 15
66*93e1821cSIwona Winiarska #define PECI_WRENDPTCFG_MMIO_Q_WR_LEN_BASE 19
67*93e1821cSIwona Winiarska #define PECI_WRENDPTCFG_RD_LEN 1
68*93e1821cSIwona Winiarska
696b8145b0SIwona Winiarska /* Device Specific Completion Code (CC) Definition */
706b8145b0SIwona Winiarska #define PECI_CC_SUCCESS 0x40
716b8145b0SIwona Winiarska #define PECI_CC_NEED_RETRY 0x80
726b8145b0SIwona Winiarska #define PECI_CC_OUT_OF_RESOURCE 0x81
736b8145b0SIwona Winiarska #define PECI_CC_UNAVAIL_RESOURCE 0x82
746b8145b0SIwona Winiarska #define PECI_CC_INVALID_REQ 0x90
756b8145b0SIwona Winiarska #define PECI_CC_MCA_ERROR 0x91
766b8145b0SIwona Winiarska #define PECI_CC_CATASTROPHIC_MCA_ERROR 0x93
776b8145b0SIwona Winiarska #define PECI_CC_FATAL_MCA_ERROR 0x94
786b8145b0SIwona Winiarska #define PECI_CC_PARITY_ERR_GPSB_OR_PMSB 0x98
796b8145b0SIwona Winiarska #define PECI_CC_PARITY_ERR_GPSB_OR_PMSB_IERR 0x9B
806b8145b0SIwona Winiarska #define PECI_CC_PARITY_ERR_GPSB_OR_PMSB_MCA 0x9C
816b8145b0SIwona Winiarska
826b8145b0SIwona Winiarska #define PECI_RETRY_BIT BIT(0)
836b8145b0SIwona Winiarska
846b8145b0SIwona Winiarska #define PECI_RETRY_TIMEOUT msecs_to_jiffies(700)
856b8145b0SIwona Winiarska #define PECI_RETRY_INTERVAL_MIN msecs_to_jiffies(1)
866b8145b0SIwona Winiarska #define PECI_RETRY_INTERVAL_MAX msecs_to_jiffies(128)
876b8145b0SIwona Winiarska
peci_request_data_cc(struct peci_request * req)886b8145b0SIwona Winiarska static u8 peci_request_data_cc(struct peci_request *req)
896b8145b0SIwona Winiarska {
906b8145b0SIwona Winiarska return req->rx.buf[0];
916b8145b0SIwona Winiarska }
926b8145b0SIwona Winiarska
936b8145b0SIwona Winiarska /**
946b8145b0SIwona Winiarska * peci_request_status() - return -errno based on PECI completion code
956b8145b0SIwona Winiarska * @req: the PECI request that contains response data with completion code
966b8145b0SIwona Winiarska *
976b8145b0SIwona Winiarska * It can't be used for Ping(), GetDIB() and GetTemp() - for those commands we
986b8145b0SIwona Winiarska * don't expect completion code in the response.
996b8145b0SIwona Winiarska *
1006b8145b0SIwona Winiarska * Return: -errno
1016b8145b0SIwona Winiarska */
peci_request_status(struct peci_request * req)1026b8145b0SIwona Winiarska int peci_request_status(struct peci_request *req)
1036b8145b0SIwona Winiarska {
1046b8145b0SIwona Winiarska u8 cc = peci_request_data_cc(req);
1056b8145b0SIwona Winiarska
1066b8145b0SIwona Winiarska if (cc != PECI_CC_SUCCESS)
1076b8145b0SIwona Winiarska dev_dbg(&req->device->dev, "ret: %#02x\n", cc);
1086b8145b0SIwona Winiarska
1096b8145b0SIwona Winiarska switch (cc) {
1106b8145b0SIwona Winiarska case PECI_CC_SUCCESS:
1116b8145b0SIwona Winiarska return 0;
1126b8145b0SIwona Winiarska case PECI_CC_NEED_RETRY:
1136b8145b0SIwona Winiarska case PECI_CC_OUT_OF_RESOURCE:
1146b8145b0SIwona Winiarska case PECI_CC_UNAVAIL_RESOURCE:
1156b8145b0SIwona Winiarska return -EAGAIN;
1166b8145b0SIwona Winiarska case PECI_CC_INVALID_REQ:
1176b8145b0SIwona Winiarska return -EINVAL;
1186b8145b0SIwona Winiarska case PECI_CC_MCA_ERROR:
1196b8145b0SIwona Winiarska case PECI_CC_CATASTROPHIC_MCA_ERROR:
1206b8145b0SIwona Winiarska case PECI_CC_FATAL_MCA_ERROR:
1216b8145b0SIwona Winiarska case PECI_CC_PARITY_ERR_GPSB_OR_PMSB:
1226b8145b0SIwona Winiarska case PECI_CC_PARITY_ERR_GPSB_OR_PMSB_IERR:
1236b8145b0SIwona Winiarska case PECI_CC_PARITY_ERR_GPSB_OR_PMSB_MCA:
1246b8145b0SIwona Winiarska return -EIO;
1256b8145b0SIwona Winiarska }
1266b8145b0SIwona Winiarska
1276b8145b0SIwona Winiarska WARN_ONCE(1, "Unknown PECI completion code: %#02x\n", cc);
1286b8145b0SIwona Winiarska
1296b8145b0SIwona Winiarska return -EIO;
1306b8145b0SIwona Winiarska }
1316b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_status, PECI);
1326b8145b0SIwona Winiarska
peci_request_xfer(struct peci_request * req)1336b8145b0SIwona Winiarska static int peci_request_xfer(struct peci_request *req)
1346b8145b0SIwona Winiarska {
1356b8145b0SIwona Winiarska struct peci_device *device = req->device;
1366b8145b0SIwona Winiarska struct peci_controller *controller = to_peci_controller(device->dev.parent);
1376b8145b0SIwona Winiarska int ret;
1386b8145b0SIwona Winiarska
1396b8145b0SIwona Winiarska mutex_lock(&controller->bus_lock);
1406b8145b0SIwona Winiarska ret = controller->ops->xfer(controller, device->addr, req);
1416b8145b0SIwona Winiarska mutex_unlock(&controller->bus_lock);
1426b8145b0SIwona Winiarska
1436b8145b0SIwona Winiarska return ret;
1446b8145b0SIwona Winiarska }
1456b8145b0SIwona Winiarska
peci_request_xfer_retry(struct peci_request * req)1466b8145b0SIwona Winiarska static int peci_request_xfer_retry(struct peci_request *req)
1476b8145b0SIwona Winiarska {
1486b8145b0SIwona Winiarska long wait_interval = PECI_RETRY_INTERVAL_MIN;
1496b8145b0SIwona Winiarska struct peci_device *device = req->device;
1506b8145b0SIwona Winiarska struct peci_controller *controller = to_peci_controller(device->dev.parent);
1516b8145b0SIwona Winiarska unsigned long start = jiffies;
1526b8145b0SIwona Winiarska int ret;
1536b8145b0SIwona Winiarska
1546b8145b0SIwona Winiarska /* Don't try to use it for ping */
1556b8145b0SIwona Winiarska if (WARN_ON(req->tx.len == 0))
1566b8145b0SIwona Winiarska return 0;
1576b8145b0SIwona Winiarska
1586b8145b0SIwona Winiarska do {
1596b8145b0SIwona Winiarska ret = peci_request_xfer(req);
1606b8145b0SIwona Winiarska if (ret) {
1616b8145b0SIwona Winiarska dev_dbg(&controller->dev, "xfer error: %d\n", ret);
1626b8145b0SIwona Winiarska return ret;
1636b8145b0SIwona Winiarska }
1646b8145b0SIwona Winiarska
1656b8145b0SIwona Winiarska if (peci_request_status(req) != -EAGAIN)
1666b8145b0SIwona Winiarska return 0;
1676b8145b0SIwona Winiarska
1686b8145b0SIwona Winiarska /* Set the retry bit to indicate a retry attempt */
1696b8145b0SIwona Winiarska req->tx.buf[1] |= PECI_RETRY_BIT;
1706b8145b0SIwona Winiarska
1716b8145b0SIwona Winiarska if (schedule_timeout_interruptible(wait_interval))
1726b8145b0SIwona Winiarska return -ERESTARTSYS;
1736b8145b0SIwona Winiarska
1746b8145b0SIwona Winiarska wait_interval = min_t(long, wait_interval * 2, PECI_RETRY_INTERVAL_MAX);
1756b8145b0SIwona Winiarska } while (time_before(jiffies, start + PECI_RETRY_TIMEOUT));
1766b8145b0SIwona Winiarska
1776b8145b0SIwona Winiarska dev_dbg(&controller->dev, "request timed out\n");
1786b8145b0SIwona Winiarska
1796b8145b0SIwona Winiarska return -ETIMEDOUT;
1806b8145b0SIwona Winiarska }
1816b8145b0SIwona Winiarska
18252857e68SIwona Winiarska /**
18352857e68SIwona Winiarska * peci_request_alloc() - allocate &struct peci_requests
18452857e68SIwona Winiarska * @device: PECI device to which request is going to be sent
18552857e68SIwona Winiarska * @tx_len: TX length
18652857e68SIwona Winiarska * @rx_len: RX length
18752857e68SIwona Winiarska *
18852857e68SIwona Winiarska * Return: A pointer to a newly allocated &struct peci_request on success or NULL otherwise.
18952857e68SIwona Winiarska */
peci_request_alloc(struct peci_device * device,u8 tx_len,u8 rx_len)19052857e68SIwona Winiarska struct peci_request *peci_request_alloc(struct peci_device *device, u8 tx_len, u8 rx_len)
19152857e68SIwona Winiarska {
19252857e68SIwona Winiarska struct peci_request *req;
19352857e68SIwona Winiarska
19452857e68SIwona Winiarska /*
19552857e68SIwona Winiarska * TX and RX buffers are fixed length members of peci_request, this is
19652857e68SIwona Winiarska * just a warn for developers to make sure to expand the buffers (or
19752857e68SIwona Winiarska * change the allocation method) if we go over the current limit.
19852857e68SIwona Winiarska */
19952857e68SIwona Winiarska if (WARN_ON_ONCE(tx_len > PECI_REQUEST_MAX_BUF_SIZE || rx_len > PECI_REQUEST_MAX_BUF_SIZE))
20052857e68SIwona Winiarska return NULL;
20152857e68SIwona Winiarska /*
20252857e68SIwona Winiarska * PECI controllers that we are using now don't support DMA, this
20352857e68SIwona Winiarska * should be converted to DMA API once support for controllers that do
20452857e68SIwona Winiarska * allow it is added to avoid an extra copy.
20552857e68SIwona Winiarska */
20652857e68SIwona Winiarska req = kzalloc(sizeof(*req), GFP_KERNEL);
20752857e68SIwona Winiarska if (!req)
20852857e68SIwona Winiarska return NULL;
20952857e68SIwona Winiarska
21052857e68SIwona Winiarska req->device = device;
21152857e68SIwona Winiarska req->tx.len = tx_len;
21252857e68SIwona Winiarska req->rx.len = rx_len;
21352857e68SIwona Winiarska
21452857e68SIwona Winiarska return req;
21552857e68SIwona Winiarska }
21652857e68SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_alloc, PECI);
21752857e68SIwona Winiarska
21852857e68SIwona Winiarska /**
21952857e68SIwona Winiarska * peci_request_free() - free peci_request
22052857e68SIwona Winiarska * @req: the PECI request to be freed
22152857e68SIwona Winiarska */
peci_request_free(struct peci_request * req)22252857e68SIwona Winiarska void peci_request_free(struct peci_request *req)
22352857e68SIwona Winiarska {
22452857e68SIwona Winiarska kfree(req);
22552857e68SIwona Winiarska }
22652857e68SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_free, PECI);
2276b8145b0SIwona Winiarska
peci_xfer_get_dib(struct peci_device * device)2286b8145b0SIwona Winiarska struct peci_request *peci_xfer_get_dib(struct peci_device *device)
2296b8145b0SIwona Winiarska {
2306b8145b0SIwona Winiarska struct peci_request *req;
2316b8145b0SIwona Winiarska int ret;
2326b8145b0SIwona Winiarska
2336b8145b0SIwona Winiarska req = peci_request_alloc(device, PECI_GET_DIB_WR_LEN, PECI_GET_DIB_RD_LEN);
2346b8145b0SIwona Winiarska if (!req)
2356b8145b0SIwona Winiarska return ERR_PTR(-ENOMEM);
2366b8145b0SIwona Winiarska
2376b8145b0SIwona Winiarska req->tx.buf[0] = PECI_GET_DIB_CMD;
2386b8145b0SIwona Winiarska
2396b8145b0SIwona Winiarska ret = peci_request_xfer(req);
2406b8145b0SIwona Winiarska if (ret) {
2416b8145b0SIwona Winiarska peci_request_free(req);
2426b8145b0SIwona Winiarska return ERR_PTR(ret);
2436b8145b0SIwona Winiarska }
2446b8145b0SIwona Winiarska
2456b8145b0SIwona Winiarska return req;
2466b8145b0SIwona Winiarska }
2476b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_xfer_get_dib, PECI);
2486b8145b0SIwona Winiarska
peci_xfer_get_temp(struct peci_device * device)249*93e1821cSIwona Winiarska struct peci_request *peci_xfer_get_temp(struct peci_device *device)
250*93e1821cSIwona Winiarska {
251*93e1821cSIwona Winiarska struct peci_request *req;
252*93e1821cSIwona Winiarska int ret;
253*93e1821cSIwona Winiarska
254*93e1821cSIwona Winiarska req = peci_request_alloc(device, PECI_GET_TEMP_WR_LEN, PECI_GET_TEMP_RD_LEN);
255*93e1821cSIwona Winiarska if (!req)
256*93e1821cSIwona Winiarska return ERR_PTR(-ENOMEM);
257*93e1821cSIwona Winiarska
258*93e1821cSIwona Winiarska req->tx.buf[0] = PECI_GET_TEMP_CMD;
259*93e1821cSIwona Winiarska
260*93e1821cSIwona Winiarska ret = peci_request_xfer(req);
261*93e1821cSIwona Winiarska if (ret) {
262*93e1821cSIwona Winiarska peci_request_free(req);
263*93e1821cSIwona Winiarska return ERR_PTR(ret);
264*93e1821cSIwona Winiarska }
265*93e1821cSIwona Winiarska
266*93e1821cSIwona Winiarska return req;
267*93e1821cSIwona Winiarska }
268*93e1821cSIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_xfer_get_temp, PECI);
269*93e1821cSIwona Winiarska
2706b8145b0SIwona Winiarska static struct peci_request *
__pkg_cfg_read(struct peci_device * device,u8 index,u16 param,u8 len)2716b8145b0SIwona Winiarska __pkg_cfg_read(struct peci_device *device, u8 index, u16 param, u8 len)
2726b8145b0SIwona Winiarska {
2736b8145b0SIwona Winiarska struct peci_request *req;
2746b8145b0SIwona Winiarska int ret;
2756b8145b0SIwona Winiarska
2766b8145b0SIwona Winiarska req = peci_request_alloc(device, PECI_RDPKGCFG_WR_LEN, PECI_RDPKGCFG_RD_LEN_BASE + len);
2776b8145b0SIwona Winiarska if (!req)
2786b8145b0SIwona Winiarska return ERR_PTR(-ENOMEM);
2796b8145b0SIwona Winiarska
2806b8145b0SIwona Winiarska req->tx.buf[0] = PECI_RDPKGCFG_CMD;
2816b8145b0SIwona Winiarska req->tx.buf[1] = 0;
2826b8145b0SIwona Winiarska req->tx.buf[2] = index;
2836b8145b0SIwona Winiarska put_unaligned_le16(param, &req->tx.buf[3]);
2846b8145b0SIwona Winiarska
2856b8145b0SIwona Winiarska ret = peci_request_xfer_retry(req);
2866b8145b0SIwona Winiarska if (ret) {
2876b8145b0SIwona Winiarska peci_request_free(req);
2886b8145b0SIwona Winiarska return ERR_PTR(ret);
2896b8145b0SIwona Winiarska }
2906b8145b0SIwona Winiarska
2916b8145b0SIwona Winiarska return req;
2926b8145b0SIwona Winiarska }
2936b8145b0SIwona Winiarska
__get_pci_addr(u8 bus,u8 dev,u8 func,u16 reg)294*93e1821cSIwona Winiarska static u32 __get_pci_addr(u8 bus, u8 dev, u8 func, u16 reg)
295*93e1821cSIwona Winiarska {
296*93e1821cSIwona Winiarska return reg | PCI_DEVID(bus, PCI_DEVFN(dev, func)) << 12;
297*93e1821cSIwona Winiarska }
298*93e1821cSIwona Winiarska
299*93e1821cSIwona Winiarska static struct peci_request *
__pci_cfg_local_read(struct peci_device * device,u8 bus,u8 dev,u8 func,u16 reg,u8 len)300*93e1821cSIwona Winiarska __pci_cfg_local_read(struct peci_device *device, u8 bus, u8 dev, u8 func, u16 reg, u8 len)
301*93e1821cSIwona Winiarska {
302*93e1821cSIwona Winiarska struct peci_request *req;
303*93e1821cSIwona Winiarska u32 pci_addr;
304*93e1821cSIwona Winiarska int ret;
305*93e1821cSIwona Winiarska
306*93e1821cSIwona Winiarska req = peci_request_alloc(device, PECI_RDPCICFGLOCAL_WR_LEN,
307*93e1821cSIwona Winiarska PECI_RDPCICFGLOCAL_RD_LEN_BASE + len);
308*93e1821cSIwona Winiarska if (!req)
309*93e1821cSIwona Winiarska return ERR_PTR(-ENOMEM);
310*93e1821cSIwona Winiarska
311*93e1821cSIwona Winiarska pci_addr = __get_pci_addr(bus, dev, func, reg);
312*93e1821cSIwona Winiarska
313*93e1821cSIwona Winiarska req->tx.buf[0] = PECI_RDPCICFGLOCAL_CMD;
314*93e1821cSIwona Winiarska req->tx.buf[1] = 0;
315*93e1821cSIwona Winiarska put_unaligned_le24(pci_addr, &req->tx.buf[2]);
316*93e1821cSIwona Winiarska
317*93e1821cSIwona Winiarska ret = peci_request_xfer_retry(req);
318*93e1821cSIwona Winiarska if (ret) {
319*93e1821cSIwona Winiarska peci_request_free(req);
320*93e1821cSIwona Winiarska return ERR_PTR(ret);
321*93e1821cSIwona Winiarska }
322*93e1821cSIwona Winiarska
323*93e1821cSIwona Winiarska return req;
324*93e1821cSIwona Winiarska }
325*93e1821cSIwona Winiarska
326*93e1821cSIwona Winiarska static struct peci_request *
__ep_pci_cfg_read(struct peci_device * device,u8 msg_type,u8 seg,u8 bus,u8 dev,u8 func,u16 reg,u8 len)327*93e1821cSIwona Winiarska __ep_pci_cfg_read(struct peci_device *device, u8 msg_type, u8 seg,
328*93e1821cSIwona Winiarska u8 bus, u8 dev, u8 func, u16 reg, u8 len)
329*93e1821cSIwona Winiarska {
330*93e1821cSIwona Winiarska struct peci_request *req;
331*93e1821cSIwona Winiarska u32 pci_addr;
332*93e1821cSIwona Winiarska int ret;
333*93e1821cSIwona Winiarska
334*93e1821cSIwona Winiarska req = peci_request_alloc(device, PECI_RDENDPTCFG_PCI_WR_LEN,
335*93e1821cSIwona Winiarska PECI_RDENDPTCFG_RD_LEN_BASE + len);
336*93e1821cSIwona Winiarska if (!req)
337*93e1821cSIwona Winiarska return ERR_PTR(-ENOMEM);
338*93e1821cSIwona Winiarska
339*93e1821cSIwona Winiarska pci_addr = __get_pci_addr(bus, dev, func, reg);
340*93e1821cSIwona Winiarska
341*93e1821cSIwona Winiarska req->tx.buf[0] = PECI_RDENDPTCFG_CMD;
342*93e1821cSIwona Winiarska req->tx.buf[1] = 0;
343*93e1821cSIwona Winiarska req->tx.buf[2] = msg_type;
344*93e1821cSIwona Winiarska req->tx.buf[3] = 0;
345*93e1821cSIwona Winiarska req->tx.buf[4] = 0;
346*93e1821cSIwona Winiarska req->tx.buf[5] = 0;
347*93e1821cSIwona Winiarska req->tx.buf[6] = PECI_ENDPTCFG_ADDR_TYPE_PCI;
348*93e1821cSIwona Winiarska req->tx.buf[7] = seg; /* PCI Segment */
349*93e1821cSIwona Winiarska put_unaligned_le32(pci_addr, &req->tx.buf[8]);
350*93e1821cSIwona Winiarska
351*93e1821cSIwona Winiarska ret = peci_request_xfer_retry(req);
352*93e1821cSIwona Winiarska if (ret) {
353*93e1821cSIwona Winiarska peci_request_free(req);
354*93e1821cSIwona Winiarska return ERR_PTR(ret);
355*93e1821cSIwona Winiarska }
356*93e1821cSIwona Winiarska
357*93e1821cSIwona Winiarska return req;
358*93e1821cSIwona Winiarska }
359*93e1821cSIwona Winiarska
360*93e1821cSIwona Winiarska static struct peci_request *
__ep_mmio_read(struct peci_device * device,u8 bar,u8 addr_type,u8 seg,u8 bus,u8 dev,u8 func,u64 offset,u8 tx_len,u8 len)361*93e1821cSIwona Winiarska __ep_mmio_read(struct peci_device *device, u8 bar, u8 addr_type, u8 seg,
362*93e1821cSIwona Winiarska u8 bus, u8 dev, u8 func, u64 offset, u8 tx_len, u8 len)
363*93e1821cSIwona Winiarska {
364*93e1821cSIwona Winiarska struct peci_request *req;
365*93e1821cSIwona Winiarska int ret;
366*93e1821cSIwona Winiarska
367*93e1821cSIwona Winiarska req = peci_request_alloc(device, tx_len, PECI_RDENDPTCFG_RD_LEN_BASE + len);
368*93e1821cSIwona Winiarska if (!req)
369*93e1821cSIwona Winiarska return ERR_PTR(-ENOMEM);
370*93e1821cSIwona Winiarska
371*93e1821cSIwona Winiarska req->tx.buf[0] = PECI_RDENDPTCFG_CMD;
372*93e1821cSIwona Winiarska req->tx.buf[1] = 0;
373*93e1821cSIwona Winiarska req->tx.buf[2] = PECI_ENDPTCFG_TYPE_MMIO;
374*93e1821cSIwona Winiarska req->tx.buf[3] = 0; /* Endpoint ID */
375*93e1821cSIwona Winiarska req->tx.buf[4] = 0; /* Reserved */
376*93e1821cSIwona Winiarska req->tx.buf[5] = bar;
377*93e1821cSIwona Winiarska req->tx.buf[6] = addr_type;
378*93e1821cSIwona Winiarska req->tx.buf[7] = seg; /* PCI Segment */
379*93e1821cSIwona Winiarska req->tx.buf[8] = PCI_DEVFN(dev, func);
380*93e1821cSIwona Winiarska req->tx.buf[9] = bus; /* PCI Bus */
381*93e1821cSIwona Winiarska
382*93e1821cSIwona Winiarska if (addr_type == PECI_ENDPTCFG_ADDR_TYPE_MMIO_D)
383*93e1821cSIwona Winiarska put_unaligned_le32(offset, &req->tx.buf[10]);
384*93e1821cSIwona Winiarska else
385*93e1821cSIwona Winiarska put_unaligned_le64(offset, &req->tx.buf[10]);
386*93e1821cSIwona Winiarska
387*93e1821cSIwona Winiarska ret = peci_request_xfer_retry(req);
388*93e1821cSIwona Winiarska if (ret) {
389*93e1821cSIwona Winiarska peci_request_free(req);
390*93e1821cSIwona Winiarska return ERR_PTR(ret);
391*93e1821cSIwona Winiarska }
392*93e1821cSIwona Winiarska
393*93e1821cSIwona Winiarska return req;
394*93e1821cSIwona Winiarska }
395*93e1821cSIwona Winiarska
peci_request_data_readb(struct peci_request * req)3966b8145b0SIwona Winiarska u8 peci_request_data_readb(struct peci_request *req)
3976b8145b0SIwona Winiarska {
3986b8145b0SIwona Winiarska return req->rx.buf[1];
3996b8145b0SIwona Winiarska }
4006b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_data_readb, PECI);
4016b8145b0SIwona Winiarska
peci_request_data_readw(struct peci_request * req)4026b8145b0SIwona Winiarska u16 peci_request_data_readw(struct peci_request *req)
4036b8145b0SIwona Winiarska {
4046b8145b0SIwona Winiarska return get_unaligned_le16(&req->rx.buf[1]);
4056b8145b0SIwona Winiarska }
4066b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_data_readw, PECI);
4076b8145b0SIwona Winiarska
peci_request_data_readl(struct peci_request * req)4086b8145b0SIwona Winiarska u32 peci_request_data_readl(struct peci_request *req)
4096b8145b0SIwona Winiarska {
4106b8145b0SIwona Winiarska return get_unaligned_le32(&req->rx.buf[1]);
4116b8145b0SIwona Winiarska }
4126b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_data_readl, PECI);
4136b8145b0SIwona Winiarska
peci_request_data_readq(struct peci_request * req)4146b8145b0SIwona Winiarska u64 peci_request_data_readq(struct peci_request *req)
4156b8145b0SIwona Winiarska {
4166b8145b0SIwona Winiarska return get_unaligned_le64(&req->rx.buf[1]);
4176b8145b0SIwona Winiarska }
4186b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_data_readq, PECI);
4196b8145b0SIwona Winiarska
peci_request_dib_read(struct peci_request * req)4206b8145b0SIwona Winiarska u64 peci_request_dib_read(struct peci_request *req)
4216b8145b0SIwona Winiarska {
4226b8145b0SIwona Winiarska return get_unaligned_le64(&req->rx.buf[0]);
4236b8145b0SIwona Winiarska }
4246b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_dib_read, PECI);
4256b8145b0SIwona Winiarska
peci_request_temp_read(struct peci_request * req)426*93e1821cSIwona Winiarska s16 peci_request_temp_read(struct peci_request *req)
427*93e1821cSIwona Winiarska {
428*93e1821cSIwona Winiarska return get_unaligned_le16(&req->rx.buf[0]);
429*93e1821cSIwona Winiarska }
430*93e1821cSIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_request_temp_read, PECI);
431*93e1821cSIwona Winiarska
4326b8145b0SIwona Winiarska #define __read_pkg_config(x, type) \
4336b8145b0SIwona Winiarska struct peci_request *peci_xfer_pkg_cfg_##x(struct peci_device *device, u8 index, u16 param) \
4346b8145b0SIwona Winiarska { \
4356b8145b0SIwona Winiarska return __pkg_cfg_read(device, index, param, sizeof(type)); \
4366b8145b0SIwona Winiarska } \
4376b8145b0SIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_xfer_pkg_cfg_##x, PECI)
4386b8145b0SIwona Winiarska
4396b8145b0SIwona Winiarska __read_pkg_config(readb, u8);
4406b8145b0SIwona Winiarska __read_pkg_config(readw, u16);
4416b8145b0SIwona Winiarska __read_pkg_config(readl, u32);
4426b8145b0SIwona Winiarska __read_pkg_config(readq, u64);
443*93e1821cSIwona Winiarska
444*93e1821cSIwona Winiarska #define __read_pci_config_local(x, type) \
445*93e1821cSIwona Winiarska struct peci_request * \
446*93e1821cSIwona Winiarska peci_xfer_pci_cfg_local_##x(struct peci_device *device, u8 bus, u8 dev, u8 func, u16 reg) \
447*93e1821cSIwona Winiarska { \
448*93e1821cSIwona Winiarska return __pci_cfg_local_read(device, bus, dev, func, reg, sizeof(type)); \
449*93e1821cSIwona Winiarska } \
450*93e1821cSIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_xfer_pci_cfg_local_##x, PECI)
451*93e1821cSIwona Winiarska
452*93e1821cSIwona Winiarska __read_pci_config_local(readb, u8);
453*93e1821cSIwona Winiarska __read_pci_config_local(readw, u16);
454*93e1821cSIwona Winiarska __read_pci_config_local(readl, u32);
455*93e1821cSIwona Winiarska
456*93e1821cSIwona Winiarska #define __read_ep_pci_config(x, msg_type, type) \
457*93e1821cSIwona Winiarska struct peci_request * \
458*93e1821cSIwona Winiarska peci_xfer_ep_pci_cfg_##x(struct peci_device *device, u8 seg, u8 bus, u8 dev, u8 func, u16 reg) \
459*93e1821cSIwona Winiarska { \
460*93e1821cSIwona Winiarska return __ep_pci_cfg_read(device, msg_type, seg, bus, dev, func, reg, sizeof(type)); \
461*93e1821cSIwona Winiarska } \
462*93e1821cSIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_xfer_ep_pci_cfg_##x, PECI)
463*93e1821cSIwona Winiarska
464*93e1821cSIwona Winiarska __read_ep_pci_config(local_readb, PECI_ENDPTCFG_TYPE_LOCAL_PCI, u8);
465*93e1821cSIwona Winiarska __read_ep_pci_config(local_readw, PECI_ENDPTCFG_TYPE_LOCAL_PCI, u16);
466*93e1821cSIwona Winiarska __read_ep_pci_config(local_readl, PECI_ENDPTCFG_TYPE_LOCAL_PCI, u32);
467*93e1821cSIwona Winiarska __read_ep_pci_config(readb, PECI_ENDPTCFG_TYPE_PCI, u8);
468*93e1821cSIwona Winiarska __read_ep_pci_config(readw, PECI_ENDPTCFG_TYPE_PCI, u16);
469*93e1821cSIwona Winiarska __read_ep_pci_config(readl, PECI_ENDPTCFG_TYPE_PCI, u32);
470*93e1821cSIwona Winiarska
471*93e1821cSIwona Winiarska #define __read_ep_mmio(x, y, addr_type, type1, type2) \
472*93e1821cSIwona Winiarska struct peci_request *peci_xfer_ep_mmio##y##_##x(struct peci_device *device, u8 bar, u8 seg, \
473*93e1821cSIwona Winiarska u8 bus, u8 dev, u8 func, u64 offset) \
474*93e1821cSIwona Winiarska { \
475*93e1821cSIwona Winiarska return __ep_mmio_read(device, bar, addr_type, seg, bus, dev, func, \
476*93e1821cSIwona Winiarska offset, PECI_RDENDPTCFG_MMIO_WR_LEN_BASE + sizeof(type1), \
477*93e1821cSIwona Winiarska sizeof(type2)); \
478*93e1821cSIwona Winiarska } \
479*93e1821cSIwona Winiarska EXPORT_SYMBOL_NS_GPL(peci_xfer_ep_mmio##y##_##x, PECI)
480*93e1821cSIwona Winiarska
481*93e1821cSIwona Winiarska __read_ep_mmio(readl, 32, PECI_ENDPTCFG_ADDR_TYPE_MMIO_D, u32, u32);
482*93e1821cSIwona Winiarska __read_ep_mmio(readl, 64, PECI_ENDPTCFG_ADDR_TYPE_MMIO_Q, u64, u32);
483