1886bdf9cSHuisong Li // SPDX-License-Identifier: GPL-2.0+
2886bdf9cSHuisong Li /*
3886bdf9cSHuisong Li * The Huawei Cache Coherence System (HCCS) is a multi-chip interconnection
4886bdf9cSHuisong Li * bus protocol.
5886bdf9cSHuisong Li *
6886bdf9cSHuisong Li * Copyright (c) 2023 Hisilicon Limited.
7886bdf9cSHuisong Li * Author: Huisong Li <lihuisong@huawei.com>
847f7a255SHuisong Li *
947f7a255SHuisong Li * HCCS driver for Kunpeng SoC provides the following features:
1047f7a255SHuisong Li * - Retrieve the following information about each port:
1147f7a255SHuisong Li * - port type
1247f7a255SHuisong Li * - lane mode
1347f7a255SHuisong Li * - enable
1447f7a255SHuisong Li * - current lane mode
1547f7a255SHuisong Li * - link finite state machine
1647f7a255SHuisong Li * - lane mask
1747f7a255SHuisong Li * - CRC error count
1847f7a255SHuisong Li *
1947f7a255SHuisong Li * - Retrieve the following information about all the ports on the chip or
2047f7a255SHuisong Li * the die:
2147f7a255SHuisong Li * - if all enabled ports are in linked
2247f7a255SHuisong Li * - if all linked ports are in full lane
2347f7a255SHuisong Li * - CRC error count sum
24886bdf9cSHuisong Li */
25886bdf9cSHuisong Li #include <linux/acpi.h>
26886bdf9cSHuisong Li #include <linux/iopoll.h>
27886bdf9cSHuisong Li #include <linux/platform_device.h>
2847f7a255SHuisong Li #include <linux/sysfs.h>
29886bdf9cSHuisong Li
30886bdf9cSHuisong Li #include <acpi/pcc.h>
31886bdf9cSHuisong Li
32886bdf9cSHuisong Li #include "kunpeng_hccs.h"
33886bdf9cSHuisong Li
34886bdf9cSHuisong Li /*
35886bdf9cSHuisong Li * Arbitrary retries in case the remote processor is slow to respond
36886bdf9cSHuisong Li * to PCC commands
37886bdf9cSHuisong Li */
38886bdf9cSHuisong Li #define HCCS_PCC_CMD_WAIT_RETRIES_NUM 500ULL
39886bdf9cSHuisong Li #define HCCS_POLL_STATUS_TIME_INTERVAL_US 3
40886bdf9cSHuisong Li
kobj_to_port_info(struct kobject * k)4147f7a255SHuisong Li static struct hccs_port_info *kobj_to_port_info(struct kobject *k)
4247f7a255SHuisong Li {
4347f7a255SHuisong Li return container_of(k, struct hccs_port_info, kobj);
4447f7a255SHuisong Li }
4547f7a255SHuisong Li
kobj_to_die_info(struct kobject * k)4647f7a255SHuisong Li static struct hccs_die_info *kobj_to_die_info(struct kobject *k)
4747f7a255SHuisong Li {
4847f7a255SHuisong Li return container_of(k, struct hccs_die_info, kobj);
4947f7a255SHuisong Li }
5047f7a255SHuisong Li
kobj_to_chip_info(struct kobject * k)5147f7a255SHuisong Li static struct hccs_chip_info *kobj_to_chip_info(struct kobject *k)
5247f7a255SHuisong Li {
5347f7a255SHuisong Li return container_of(k, struct hccs_chip_info, kobj);
5447f7a255SHuisong Li }
5547f7a255SHuisong Li
56886bdf9cSHuisong Li struct hccs_register_ctx {
57886bdf9cSHuisong Li struct device *dev;
58886bdf9cSHuisong Li u8 chan_id;
59886bdf9cSHuisong Li int err;
60886bdf9cSHuisong Li };
61886bdf9cSHuisong Li
hccs_get_register_cb(struct acpi_resource * ares,void * context)62886bdf9cSHuisong Li static acpi_status hccs_get_register_cb(struct acpi_resource *ares,
63886bdf9cSHuisong Li void *context)
64886bdf9cSHuisong Li {
65886bdf9cSHuisong Li struct acpi_resource_generic_register *reg;
66886bdf9cSHuisong Li struct hccs_register_ctx *ctx = context;
67886bdf9cSHuisong Li
68886bdf9cSHuisong Li if (ares->type != ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
69886bdf9cSHuisong Li return AE_OK;
70886bdf9cSHuisong Li
71886bdf9cSHuisong Li reg = &ares->data.generic_reg;
72886bdf9cSHuisong Li if (reg->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) {
73886bdf9cSHuisong Li dev_err(ctx->dev, "Bad register resource.\n");
74886bdf9cSHuisong Li ctx->err = -EINVAL;
75886bdf9cSHuisong Li return AE_ERROR;
76886bdf9cSHuisong Li }
77886bdf9cSHuisong Li ctx->chan_id = reg->access_size;
78886bdf9cSHuisong Li
79886bdf9cSHuisong Li return AE_OK;
80886bdf9cSHuisong Li }
81886bdf9cSHuisong Li
hccs_get_pcc_chan_id(struct hccs_dev * hdev)82886bdf9cSHuisong Li static int hccs_get_pcc_chan_id(struct hccs_dev *hdev)
83886bdf9cSHuisong Li {
84886bdf9cSHuisong Li acpi_handle handle = ACPI_HANDLE(hdev->dev);
85886bdf9cSHuisong Li struct hccs_register_ctx ctx = {0};
86886bdf9cSHuisong Li acpi_status status;
87886bdf9cSHuisong Li
88e1e720f3SHuisong Li if (!acpi_has_method(handle, METHOD_NAME__CRS)) {
89e1e720f3SHuisong Li dev_err(hdev->dev, "No _CRS method.\n");
90886bdf9cSHuisong Li return -ENODEV;
91e1e720f3SHuisong Li }
92886bdf9cSHuisong Li
93886bdf9cSHuisong Li ctx.dev = hdev->dev;
94886bdf9cSHuisong Li status = acpi_walk_resources(handle, METHOD_NAME__CRS,
95886bdf9cSHuisong Li hccs_get_register_cb, &ctx);
96886bdf9cSHuisong Li if (ACPI_FAILURE(status))
97886bdf9cSHuisong Li return ctx.err;
98886bdf9cSHuisong Li hdev->chan_id = ctx.chan_id;
99886bdf9cSHuisong Li
100886bdf9cSHuisong Li return 0;
101886bdf9cSHuisong Li }
102886bdf9cSHuisong Li
hccs_chan_tx_done(struct mbox_client * cl,void * msg,int ret)103886bdf9cSHuisong Li static void hccs_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
104886bdf9cSHuisong Li {
105886bdf9cSHuisong Li if (ret < 0)
106886bdf9cSHuisong Li pr_debug("TX did not complete: CMD sent:0x%x, ret:%d\n",
107886bdf9cSHuisong Li *(u8 *)msg, ret);
108886bdf9cSHuisong Li else
109886bdf9cSHuisong Li pr_debug("TX completed. CMD sent:0x%x, ret:%d\n",
110886bdf9cSHuisong Li *(u8 *)msg, ret);
111886bdf9cSHuisong Li }
112886bdf9cSHuisong Li
hccs_pcc_rx_callback(struct mbox_client * cl,void * mssg)113be2f78a8SHuisong Li static void hccs_pcc_rx_callback(struct mbox_client *cl, void *mssg)
114be2f78a8SHuisong Li {
115be2f78a8SHuisong Li struct hccs_mbox_client_info *cl_info =
116be2f78a8SHuisong Li container_of(cl, struct hccs_mbox_client_info, client);
117be2f78a8SHuisong Li
118be2f78a8SHuisong Li complete(&cl_info->done);
119be2f78a8SHuisong Li }
120be2f78a8SHuisong Li
hccs_unregister_pcc_channel(struct hccs_dev * hdev)121886bdf9cSHuisong Li static void hccs_unregister_pcc_channel(struct hccs_dev *hdev)
122886bdf9cSHuisong Li {
123886bdf9cSHuisong Li struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
124886bdf9cSHuisong Li
125886bdf9cSHuisong Li if (cl_info->pcc_comm_addr)
126886bdf9cSHuisong Li iounmap(cl_info->pcc_comm_addr);
127886bdf9cSHuisong Li pcc_mbox_free_channel(hdev->cl_info.pcc_chan);
128886bdf9cSHuisong Li }
129886bdf9cSHuisong Li
hccs_register_pcc_channel(struct hccs_dev * hdev)130886bdf9cSHuisong Li static int hccs_register_pcc_channel(struct hccs_dev *hdev)
131886bdf9cSHuisong Li {
132886bdf9cSHuisong Li struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
133886bdf9cSHuisong Li struct mbox_client *cl = &cl_info->client;
134886bdf9cSHuisong Li struct pcc_mbox_chan *pcc_chan;
135886bdf9cSHuisong Li struct device *dev = hdev->dev;
136886bdf9cSHuisong Li int rc;
137886bdf9cSHuisong Li
138886bdf9cSHuisong Li cl->dev = dev;
139886bdf9cSHuisong Li cl->tx_block = false;
140886bdf9cSHuisong Li cl->knows_txdone = true;
141886bdf9cSHuisong Li cl->tx_done = hccs_chan_tx_done;
142be2f78a8SHuisong Li cl->rx_callback = hdev->verspec_data->rx_callback;
143be2f78a8SHuisong Li init_completion(&cl_info->done);
144be2f78a8SHuisong Li
145886bdf9cSHuisong Li pcc_chan = pcc_mbox_request_channel(cl, hdev->chan_id);
146886bdf9cSHuisong Li if (IS_ERR(pcc_chan)) {
147886bdf9cSHuisong Li dev_err(dev, "PPC channel request failed.\n");
148886bdf9cSHuisong Li rc = -ENODEV;
149886bdf9cSHuisong Li goto out;
150886bdf9cSHuisong Li }
151886bdf9cSHuisong Li cl_info->pcc_chan = pcc_chan;
152886bdf9cSHuisong Li cl_info->mbox_chan = pcc_chan->mchan;
153886bdf9cSHuisong Li
154886bdf9cSHuisong Li /*
155886bdf9cSHuisong Li * pcc_chan->latency is just a nominal value. In reality the remote
156886bdf9cSHuisong Li * processor could be much slower to reply. So add an arbitrary amount
157886bdf9cSHuisong Li * of wait on top of nominal.
158886bdf9cSHuisong Li */
159886bdf9cSHuisong Li cl_info->deadline_us =
160886bdf9cSHuisong Li HCCS_PCC_CMD_WAIT_RETRIES_NUM * pcc_chan->latency;
161be2f78a8SHuisong Li if (!hdev->verspec_data->has_txdone_irq &&
162be2f78a8SHuisong Li cl_info->mbox_chan->mbox->txdone_irq) {
163886bdf9cSHuisong Li dev_err(dev, "PCC IRQ in PCCT is enabled.\n");
164886bdf9cSHuisong Li rc = -EINVAL;
165886bdf9cSHuisong Li goto err_mbx_channel_free;
166be2f78a8SHuisong Li } else if (hdev->verspec_data->has_txdone_irq &&
167be2f78a8SHuisong Li !cl_info->mbox_chan->mbox->txdone_irq) {
168be2f78a8SHuisong Li dev_err(dev, "PCC IRQ in PCCT isn't supported.\n");
169be2f78a8SHuisong Li rc = -EINVAL;
170be2f78a8SHuisong Li goto err_mbx_channel_free;
171886bdf9cSHuisong Li }
172886bdf9cSHuisong Li
173886bdf9cSHuisong Li if (pcc_chan->shmem_base_addr) {
1747d661283SHuisong Li cl_info->pcc_comm_addr = ioremap(pcc_chan->shmem_base_addr,
1757d661283SHuisong Li pcc_chan->shmem_size);
176886bdf9cSHuisong Li if (!cl_info->pcc_comm_addr) {
177734add1aSHuisong Li dev_err(dev, "Failed to ioremap PCC communication region for channel-%u.\n",
178886bdf9cSHuisong Li hdev->chan_id);
179886bdf9cSHuisong Li rc = -ENOMEM;
180886bdf9cSHuisong Li goto err_mbx_channel_free;
181886bdf9cSHuisong Li }
182886bdf9cSHuisong Li }
183886bdf9cSHuisong Li
184886bdf9cSHuisong Li return 0;
185886bdf9cSHuisong Li
186886bdf9cSHuisong Li err_mbx_channel_free:
187886bdf9cSHuisong Li pcc_mbox_free_channel(cl_info->pcc_chan);
188886bdf9cSHuisong Li out:
189886bdf9cSHuisong Li return rc;
190886bdf9cSHuisong Li }
191886bdf9cSHuisong Li
hccs_wait_cmd_complete_by_poll(struct hccs_dev * hdev)192be2f78a8SHuisong Li static int hccs_wait_cmd_complete_by_poll(struct hccs_dev *hdev)
193886bdf9cSHuisong Li {
194886bdf9cSHuisong Li struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
1957d661283SHuisong Li struct acpi_pcct_shared_memory __iomem *comm_base =
1967d661283SHuisong Li cl_info->pcc_comm_addr;
197886bdf9cSHuisong Li u16 status;
198886bdf9cSHuisong Li int ret;
199886bdf9cSHuisong Li
200886bdf9cSHuisong Li /*
201886bdf9cSHuisong Li * Poll PCC status register every 3us(delay_us) for maximum of
202886bdf9cSHuisong Li * deadline_us(timeout_us) until PCC command complete bit is set(cond)
203886bdf9cSHuisong Li */
204886bdf9cSHuisong Li ret = readw_poll_timeout(&comm_base->status, status,
205a46e42c0SSudeep Holla status & PCC_STATUS_CMD_COMPLETE,
206886bdf9cSHuisong Li HCCS_POLL_STATUS_TIME_INTERVAL_US,
207886bdf9cSHuisong Li cl_info->deadline_us);
208886bdf9cSHuisong Li if (unlikely(ret))
209886bdf9cSHuisong Li dev_err(hdev->dev, "poll PCC status failed, ret = %d.\n", ret);
210886bdf9cSHuisong Li
211886bdf9cSHuisong Li return ret;
212886bdf9cSHuisong Li }
213886bdf9cSHuisong Li
hccs_wait_cmd_complete_by_irq(struct hccs_dev * hdev)214be2f78a8SHuisong Li static int hccs_wait_cmd_complete_by_irq(struct hccs_dev *hdev)
215886bdf9cSHuisong Li {
216886bdf9cSHuisong Li struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
217886bdf9cSHuisong Li
218be2f78a8SHuisong Li if (!wait_for_completion_timeout(&cl_info->done,
219be2f78a8SHuisong Li usecs_to_jiffies(cl_info->deadline_us))) {
220be2f78a8SHuisong Li dev_err(hdev->dev, "PCC command executed timeout!\n");
221be2f78a8SHuisong Li return -ETIMEDOUT;
222be2f78a8SHuisong Li }
223be2f78a8SHuisong Li
224be2f78a8SHuisong Li return 0;
225be2f78a8SHuisong Li }
226be2f78a8SHuisong Li
hccs_fill_pcc_shared_mem_region(struct hccs_dev * hdev,u8 cmd,struct hccs_desc * desc,void __iomem * comm_space,u16 space_size)227be2f78a8SHuisong Li static inline void hccs_fill_pcc_shared_mem_region(struct hccs_dev *hdev,
228be2f78a8SHuisong Li u8 cmd,
229be2f78a8SHuisong Li struct hccs_desc *desc,
230be2f78a8SHuisong Li void __iomem *comm_space,
231be2f78a8SHuisong Li u16 space_size)
232be2f78a8SHuisong Li {
233be2f78a8SHuisong Li struct acpi_pcct_shared_memory tmp = {
234be2f78a8SHuisong Li .signature = PCC_SIGNATURE | hdev->chan_id,
235be2f78a8SHuisong Li .command = cmd,
236be2f78a8SHuisong Li .status = 0,
237be2f78a8SHuisong Li };
238be2f78a8SHuisong Li
239be2f78a8SHuisong Li memcpy_toio(hdev->cl_info.pcc_comm_addr, (void *)&tmp,
240886bdf9cSHuisong Li sizeof(struct acpi_pcct_shared_memory));
241886bdf9cSHuisong Li
242886bdf9cSHuisong Li /* Copy the message to the PCC comm space */
243be2f78a8SHuisong Li memcpy_toio(comm_space, (void *)desc, space_size);
244be2f78a8SHuisong Li }
245be2f78a8SHuisong Li
hccs_fill_ext_pcc_shared_mem_region(struct hccs_dev * hdev,u8 cmd,struct hccs_desc * desc,void __iomem * comm_space,u16 space_size)246be2f78a8SHuisong Li static inline void hccs_fill_ext_pcc_shared_mem_region(struct hccs_dev *hdev,
247be2f78a8SHuisong Li u8 cmd,
248be2f78a8SHuisong Li struct hccs_desc *desc,
249be2f78a8SHuisong Li void __iomem *comm_space,
250be2f78a8SHuisong Li u16 space_size)
251be2f78a8SHuisong Li {
252be2f78a8SHuisong Li struct acpi_pcct_ext_pcc_shared_memory tmp = {
253be2f78a8SHuisong Li .signature = PCC_SIGNATURE | hdev->chan_id,
254be2f78a8SHuisong Li .flags = PCC_CMD_COMPLETION_NOTIFY,
255be2f78a8SHuisong Li .length = HCCS_PCC_SHARE_MEM_BYTES,
256be2f78a8SHuisong Li .command = cmd,
257be2f78a8SHuisong Li };
258be2f78a8SHuisong Li
259be2f78a8SHuisong Li memcpy_toio(hdev->cl_info.pcc_comm_addr, (void *)&tmp,
260be2f78a8SHuisong Li sizeof(struct acpi_pcct_ext_pcc_shared_memory));
261be2f78a8SHuisong Li
262be2f78a8SHuisong Li /* Copy the message to the PCC comm space */
263be2f78a8SHuisong Li memcpy_toio(comm_space, (void *)desc, space_size);
264be2f78a8SHuisong Li }
265be2f78a8SHuisong Li
hccs_pcc_cmd_send(struct hccs_dev * hdev,u8 cmd,struct hccs_desc * desc)266be2f78a8SHuisong Li static int hccs_pcc_cmd_send(struct hccs_dev *hdev, u8 cmd,
267be2f78a8SHuisong Li struct hccs_desc *desc)
268be2f78a8SHuisong Li {
269be2f78a8SHuisong Li const struct hccs_verspecific_data *verspec_data = hdev->verspec_data;
270be2f78a8SHuisong Li struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
271be2f78a8SHuisong Li struct hccs_fw_inner_head *fw_inner_head;
272be2f78a8SHuisong Li void __iomem *comm_space;
273be2f78a8SHuisong Li u16 space_size;
274be2f78a8SHuisong Li int ret;
275be2f78a8SHuisong Li
276be2f78a8SHuisong Li comm_space = cl_info->pcc_comm_addr + verspec_data->shared_mem_size;
277be2f78a8SHuisong Li space_size = HCCS_PCC_SHARE_MEM_BYTES - verspec_data->shared_mem_size;
278be2f78a8SHuisong Li verspec_data->fill_pcc_shared_mem(hdev, cmd, desc,
279be2f78a8SHuisong Li comm_space, space_size);
280be2f78a8SHuisong Li if (verspec_data->has_txdone_irq)
281be2f78a8SHuisong Li reinit_completion(&cl_info->done);
282886bdf9cSHuisong Li
283886bdf9cSHuisong Li /* Ring doorbell */
284886bdf9cSHuisong Li ret = mbox_send_message(cl_info->mbox_chan, &cmd);
285886bdf9cSHuisong Li if (ret < 0) {
286886bdf9cSHuisong Li dev_err(hdev->dev, "Send PCC mbox message failed, ret = %d.\n",
287886bdf9cSHuisong Li ret);
288886bdf9cSHuisong Li goto end;
289886bdf9cSHuisong Li }
290886bdf9cSHuisong Li
291be2f78a8SHuisong Li ret = verspec_data->wait_cmd_complete(hdev);
292886bdf9cSHuisong Li if (ret)
293886bdf9cSHuisong Li goto end;
294886bdf9cSHuisong Li
295886bdf9cSHuisong Li /* Copy response data */
296be2f78a8SHuisong Li memcpy_fromio((void *)desc, comm_space, space_size);
297886bdf9cSHuisong Li fw_inner_head = &desc->rsp.fw_inner_head;
298886bdf9cSHuisong Li if (fw_inner_head->retStatus) {
299886bdf9cSHuisong Li dev_err(hdev->dev, "Execute PCC command failed, error code = %u.\n",
300886bdf9cSHuisong Li fw_inner_head->retStatus);
301886bdf9cSHuisong Li ret = -EIO;
302886bdf9cSHuisong Li }
303886bdf9cSHuisong Li
304886bdf9cSHuisong Li end:
305be2f78a8SHuisong Li if (verspec_data->has_txdone_irq)
306be2f78a8SHuisong Li mbox_chan_txdone(cl_info->mbox_chan, ret);
307be2f78a8SHuisong Li else
308886bdf9cSHuisong Li mbox_client_txdone(cl_info->mbox_chan, ret);
309886bdf9cSHuisong Li return ret;
310886bdf9cSHuisong Li }
311886bdf9cSHuisong Li
hccs_init_req_desc(struct hccs_desc * desc)312886bdf9cSHuisong Li static void hccs_init_req_desc(struct hccs_desc *desc)
313886bdf9cSHuisong Li {
314886bdf9cSHuisong Li struct hccs_req_desc *req = &desc->req;
315886bdf9cSHuisong Li
316886bdf9cSHuisong Li memset(desc, 0, sizeof(*desc));
317886bdf9cSHuisong Li req->req_head.module_code = HCCS_SERDES_MODULE_CODE;
318886bdf9cSHuisong Li }
319886bdf9cSHuisong Li
hccs_get_dev_caps(struct hccs_dev * hdev)320886bdf9cSHuisong Li static int hccs_get_dev_caps(struct hccs_dev *hdev)
321886bdf9cSHuisong Li {
322886bdf9cSHuisong Li struct hccs_desc desc;
323886bdf9cSHuisong Li int ret;
324886bdf9cSHuisong Li
325886bdf9cSHuisong Li hccs_init_req_desc(&desc);
326886bdf9cSHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DEV_CAP, &desc);
327886bdf9cSHuisong Li if (ret) {
328886bdf9cSHuisong Li dev_err(hdev->dev, "Get device capabilities failed, ret = %d.\n",
329886bdf9cSHuisong Li ret);
330886bdf9cSHuisong Li return ret;
331886bdf9cSHuisong Li }
332886bdf9cSHuisong Li memcpy(&hdev->caps, desc.rsp.data, sizeof(hdev->caps));
333886bdf9cSHuisong Li
334886bdf9cSHuisong Li return 0;
335886bdf9cSHuisong Li }
336886bdf9cSHuisong Li
hccs_query_chip_num_on_platform(struct hccs_dev * hdev)337886bdf9cSHuisong Li static int hccs_query_chip_num_on_platform(struct hccs_dev *hdev)
338886bdf9cSHuisong Li {
339886bdf9cSHuisong Li struct hccs_desc desc;
340886bdf9cSHuisong Li int ret;
341886bdf9cSHuisong Li
342886bdf9cSHuisong Li hccs_init_req_desc(&desc);
343886bdf9cSHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_CHIP_NUM, &desc);
344886bdf9cSHuisong Li if (ret) {
345886bdf9cSHuisong Li dev_err(hdev->dev, "query system chip number failed, ret = %d.\n",
346886bdf9cSHuisong Li ret);
347886bdf9cSHuisong Li return ret;
348886bdf9cSHuisong Li }
349886bdf9cSHuisong Li
350886bdf9cSHuisong Li hdev->chip_num = *((u8 *)&desc.rsp.data);
351886bdf9cSHuisong Li if (!hdev->chip_num) {
352886bdf9cSHuisong Li dev_err(hdev->dev, "chip num obtained from firmware is zero.\n");
353886bdf9cSHuisong Li return -EINVAL;
354886bdf9cSHuisong Li }
355886bdf9cSHuisong Li
356886bdf9cSHuisong Li return 0;
357886bdf9cSHuisong Li }
358886bdf9cSHuisong Li
hccs_get_chip_info(struct hccs_dev * hdev,struct hccs_chip_info * chip)359886bdf9cSHuisong Li static int hccs_get_chip_info(struct hccs_dev *hdev,
360886bdf9cSHuisong Li struct hccs_chip_info *chip)
361886bdf9cSHuisong Li {
362886bdf9cSHuisong Li struct hccs_die_num_req_param *req_param;
363886bdf9cSHuisong Li struct hccs_desc desc;
364886bdf9cSHuisong Li int ret;
365886bdf9cSHuisong Li
366886bdf9cSHuisong Li hccs_init_req_desc(&desc);
367886bdf9cSHuisong Li req_param = (struct hccs_die_num_req_param *)desc.req.data;
368886bdf9cSHuisong Li req_param->chip_id = chip->chip_id;
369886bdf9cSHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_NUM, &desc);
370886bdf9cSHuisong Li if (ret)
371886bdf9cSHuisong Li return ret;
372886bdf9cSHuisong Li
373886bdf9cSHuisong Li chip->die_num = *((u8 *)&desc.rsp.data);
374886bdf9cSHuisong Li
375886bdf9cSHuisong Li return 0;
376886bdf9cSHuisong Li }
377886bdf9cSHuisong Li
hccs_query_chip_info_on_platform(struct hccs_dev * hdev)378886bdf9cSHuisong Li static int hccs_query_chip_info_on_platform(struct hccs_dev *hdev)
379886bdf9cSHuisong Li {
380886bdf9cSHuisong Li struct hccs_chip_info *chip;
381886bdf9cSHuisong Li int ret;
382886bdf9cSHuisong Li u8 idx;
383886bdf9cSHuisong Li
384886bdf9cSHuisong Li ret = hccs_query_chip_num_on_platform(hdev);
385886bdf9cSHuisong Li if (ret) {
386886bdf9cSHuisong Li dev_err(hdev->dev, "query chip number on platform failed, ret = %d.\n",
387886bdf9cSHuisong Li ret);
388886bdf9cSHuisong Li return ret;
389886bdf9cSHuisong Li }
390886bdf9cSHuisong Li
391886bdf9cSHuisong Li hdev->chips = devm_kzalloc(hdev->dev,
392886bdf9cSHuisong Li hdev->chip_num * sizeof(struct hccs_chip_info),
393886bdf9cSHuisong Li GFP_KERNEL);
394886bdf9cSHuisong Li if (!hdev->chips) {
395886bdf9cSHuisong Li dev_err(hdev->dev, "allocate all chips memory failed.\n");
396886bdf9cSHuisong Li return -ENOMEM;
397886bdf9cSHuisong Li }
398886bdf9cSHuisong Li
399886bdf9cSHuisong Li for (idx = 0; idx < hdev->chip_num; idx++) {
400886bdf9cSHuisong Li chip = &hdev->chips[idx];
401886bdf9cSHuisong Li chip->chip_id = idx;
402886bdf9cSHuisong Li ret = hccs_get_chip_info(hdev, chip);
403886bdf9cSHuisong Li if (ret) {
404886bdf9cSHuisong Li dev_err(hdev->dev, "get chip%u info failed, ret = %d.\n",
405886bdf9cSHuisong Li idx, ret);
406886bdf9cSHuisong Li return ret;
407886bdf9cSHuisong Li }
408886bdf9cSHuisong Li chip->hdev = hdev;
409886bdf9cSHuisong Li }
410886bdf9cSHuisong Li
411886bdf9cSHuisong Li return 0;
412886bdf9cSHuisong Li }
413886bdf9cSHuisong Li
hccs_query_die_info_on_chip(struct hccs_dev * hdev,u8 chip_id,u8 die_idx,struct hccs_die_info * die)414886bdf9cSHuisong Li static int hccs_query_die_info_on_chip(struct hccs_dev *hdev, u8 chip_id,
415886bdf9cSHuisong Li u8 die_idx, struct hccs_die_info *die)
416886bdf9cSHuisong Li {
417886bdf9cSHuisong Li struct hccs_die_info_req_param *req_param;
418886bdf9cSHuisong Li struct hccs_die_info_rsp_data *rsp_data;
419886bdf9cSHuisong Li struct hccs_desc desc;
420886bdf9cSHuisong Li int ret;
421886bdf9cSHuisong Li
422886bdf9cSHuisong Li hccs_init_req_desc(&desc);
423886bdf9cSHuisong Li req_param = (struct hccs_die_info_req_param *)desc.req.data;
424886bdf9cSHuisong Li req_param->chip_id = chip_id;
425886bdf9cSHuisong Li req_param->die_idx = die_idx;
426886bdf9cSHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_INFO, &desc);
427886bdf9cSHuisong Li if (ret)
428886bdf9cSHuisong Li return ret;
429886bdf9cSHuisong Li
430886bdf9cSHuisong Li rsp_data = (struct hccs_die_info_rsp_data *)desc.rsp.data;
431886bdf9cSHuisong Li die->die_id = rsp_data->die_id;
432886bdf9cSHuisong Li die->port_num = rsp_data->port_num;
433886bdf9cSHuisong Li die->min_port_id = rsp_data->min_port_id;
434886bdf9cSHuisong Li die->max_port_id = rsp_data->max_port_id;
435886bdf9cSHuisong Li if (die->min_port_id > die->max_port_id) {
436886bdf9cSHuisong Li dev_err(hdev->dev, "min port id(%u) > max port id(%u) on die_idx(%u).\n",
437886bdf9cSHuisong Li die->min_port_id, die->max_port_id, die_idx);
438886bdf9cSHuisong Li return -EINVAL;
439886bdf9cSHuisong Li }
440886bdf9cSHuisong Li if (die->max_port_id > HCCS_DIE_MAX_PORT_ID) {
441886bdf9cSHuisong Li dev_err(hdev->dev, "max port id(%u) on die_idx(%u) is too big.\n",
442886bdf9cSHuisong Li die->max_port_id, die_idx);
443886bdf9cSHuisong Li return -EINVAL;
444886bdf9cSHuisong Li }
445886bdf9cSHuisong Li
446886bdf9cSHuisong Li return 0;
447886bdf9cSHuisong Li }
448886bdf9cSHuisong Li
hccs_query_all_die_info_on_platform(struct hccs_dev * hdev)449886bdf9cSHuisong Li static int hccs_query_all_die_info_on_platform(struct hccs_dev *hdev)
450886bdf9cSHuisong Li {
451886bdf9cSHuisong Li struct device *dev = hdev->dev;
452886bdf9cSHuisong Li struct hccs_chip_info *chip;
453886bdf9cSHuisong Li struct hccs_die_info *die;
454886bdf9cSHuisong Li u8 i, j;
455886bdf9cSHuisong Li int ret;
456886bdf9cSHuisong Li
457886bdf9cSHuisong Li for (i = 0; i < hdev->chip_num; i++) {
458886bdf9cSHuisong Li chip = &hdev->chips[i];
459886bdf9cSHuisong Li if (!chip->die_num)
460886bdf9cSHuisong Li continue;
461886bdf9cSHuisong Li
462886bdf9cSHuisong Li chip->dies = devm_kzalloc(hdev->dev,
463886bdf9cSHuisong Li chip->die_num * sizeof(struct hccs_die_info),
464886bdf9cSHuisong Li GFP_KERNEL);
465886bdf9cSHuisong Li if (!chip->dies) {
466886bdf9cSHuisong Li dev_err(dev, "allocate all dies memory on chip%u failed.\n",
467886bdf9cSHuisong Li i);
468886bdf9cSHuisong Li return -ENOMEM;
469886bdf9cSHuisong Li }
470886bdf9cSHuisong Li
471886bdf9cSHuisong Li for (j = 0; j < chip->die_num; j++) {
472886bdf9cSHuisong Li die = &chip->dies[j];
473886bdf9cSHuisong Li ret = hccs_query_die_info_on_chip(hdev, i, j, die);
474886bdf9cSHuisong Li if (ret) {
475886bdf9cSHuisong Li dev_err(dev, "get die idx (%u) info on chip%u failed, ret = %d.\n",
476886bdf9cSHuisong Li j, i, ret);
477886bdf9cSHuisong Li return ret;
478886bdf9cSHuisong Li }
479886bdf9cSHuisong Li die->chip = chip;
480886bdf9cSHuisong Li }
481886bdf9cSHuisong Li }
482886bdf9cSHuisong Li
483886bdf9cSHuisong Li return 0;
484886bdf9cSHuisong Li }
485886bdf9cSHuisong Li
hccs_get_bd_info(struct hccs_dev * hdev,u8 opcode,struct hccs_desc * desc,void * buf,size_t buf_len,struct hccs_rsp_head * rsp_head)486886bdf9cSHuisong Li static int hccs_get_bd_info(struct hccs_dev *hdev, u8 opcode,
487886bdf9cSHuisong Li struct hccs_desc *desc,
488886bdf9cSHuisong Li void *buf, size_t buf_len,
489886bdf9cSHuisong Li struct hccs_rsp_head *rsp_head)
490886bdf9cSHuisong Li {
491886bdf9cSHuisong Li struct hccs_rsp_head *head;
492886bdf9cSHuisong Li struct hccs_rsp_desc *rsp;
493886bdf9cSHuisong Li int ret;
494886bdf9cSHuisong Li
495886bdf9cSHuisong Li ret = hccs_pcc_cmd_send(hdev, opcode, desc);
496886bdf9cSHuisong Li if (ret)
497886bdf9cSHuisong Li return ret;
498886bdf9cSHuisong Li
499886bdf9cSHuisong Li rsp = &desc->rsp;
500886bdf9cSHuisong Li head = &rsp->rsp_head;
501886bdf9cSHuisong Li if (head->data_len > buf_len) {
502886bdf9cSHuisong Li dev_err(hdev->dev,
50337696fa7SArnd Bergmann "buffer overflow (buf_len = %zu, data_len = %u)!\n",
504886bdf9cSHuisong Li buf_len, head->data_len);
505886bdf9cSHuisong Li return -ENOMEM;
506886bdf9cSHuisong Li }
507886bdf9cSHuisong Li
508886bdf9cSHuisong Li memcpy(buf, rsp->data, head->data_len);
509886bdf9cSHuisong Li *rsp_head = *head;
510886bdf9cSHuisong Li
511886bdf9cSHuisong Li return 0;
512886bdf9cSHuisong Li }
513886bdf9cSHuisong Li
hccs_get_all_port_attr(struct hccs_dev * hdev,struct hccs_die_info * die,struct hccs_port_attr * attrs,u16 size)514886bdf9cSHuisong Li static int hccs_get_all_port_attr(struct hccs_dev *hdev,
515886bdf9cSHuisong Li struct hccs_die_info *die,
516886bdf9cSHuisong Li struct hccs_port_attr *attrs, u16 size)
517886bdf9cSHuisong Li {
518886bdf9cSHuisong Li struct hccs_die_comm_req_param *req_param;
519886bdf9cSHuisong Li struct hccs_req_head *req_head;
520886bdf9cSHuisong Li struct hccs_rsp_head rsp_head;
521886bdf9cSHuisong Li struct hccs_desc desc;
522886bdf9cSHuisong Li size_t left_buf_len;
523886bdf9cSHuisong Li u32 data_len = 0;
524886bdf9cSHuisong Li u8 start_id;
525886bdf9cSHuisong Li u8 *buf;
526886bdf9cSHuisong Li int ret;
527886bdf9cSHuisong Li
528886bdf9cSHuisong Li buf = (u8 *)attrs;
529886bdf9cSHuisong Li left_buf_len = sizeof(struct hccs_port_attr) * size;
530886bdf9cSHuisong Li start_id = die->min_port_id;
531886bdf9cSHuisong Li while (start_id <= die->max_port_id) {
532886bdf9cSHuisong Li hccs_init_req_desc(&desc);
533886bdf9cSHuisong Li req_head = &desc.req.req_head;
534886bdf9cSHuisong Li req_head->start_id = start_id;
535886bdf9cSHuisong Li req_param = (struct hccs_die_comm_req_param *)desc.req.data;
536886bdf9cSHuisong Li req_param->chip_id = die->chip->chip_id;
537886bdf9cSHuisong Li req_param->die_id = die->die_id;
538886bdf9cSHuisong Li
539886bdf9cSHuisong Li ret = hccs_get_bd_info(hdev, HCCS_GET_DIE_PORT_INFO, &desc,
540886bdf9cSHuisong Li buf + data_len, left_buf_len, &rsp_head);
541886bdf9cSHuisong Li if (ret) {
542886bdf9cSHuisong Li dev_err(hdev->dev,
543886bdf9cSHuisong Li "get the information of port%u on die%u failed, ret = %d.\n",
544886bdf9cSHuisong Li start_id, die->die_id, ret);
545886bdf9cSHuisong Li return ret;
546886bdf9cSHuisong Li }
547886bdf9cSHuisong Li
548886bdf9cSHuisong Li data_len += rsp_head.data_len;
549886bdf9cSHuisong Li left_buf_len -= rsp_head.data_len;
550886bdf9cSHuisong Li if (unlikely(rsp_head.next_id <= start_id)) {
551886bdf9cSHuisong Li dev_err(hdev->dev,
552886bdf9cSHuisong Li "next port id (%u) is not greater than last start id (%u) on die%u.\n",
553886bdf9cSHuisong Li rsp_head.next_id, start_id, die->die_id);
554886bdf9cSHuisong Li return -EINVAL;
555886bdf9cSHuisong Li }
556886bdf9cSHuisong Li start_id = rsp_head.next_id;
557886bdf9cSHuisong Li }
558886bdf9cSHuisong Li
559*192f97feSHuisong Li if (left_buf_len != 0) {
560*192f97feSHuisong Li dev_err(hdev->dev, "failed to get the expected port number(%u) attribute.\n",
561*192f97feSHuisong Li size);
562*192f97feSHuisong Li return -EINVAL;
563*192f97feSHuisong Li }
564*192f97feSHuisong Li
565886bdf9cSHuisong Li return 0;
566886bdf9cSHuisong Li }
567886bdf9cSHuisong Li
hccs_get_all_port_info_on_die(struct hccs_dev * hdev,struct hccs_die_info * die)568886bdf9cSHuisong Li static int hccs_get_all_port_info_on_die(struct hccs_dev *hdev,
569886bdf9cSHuisong Li struct hccs_die_info *die)
570886bdf9cSHuisong Li {
571886bdf9cSHuisong Li struct hccs_port_attr *attrs;
572886bdf9cSHuisong Li struct hccs_port_info *port;
573886bdf9cSHuisong Li int ret;
574886bdf9cSHuisong Li u8 i;
575886bdf9cSHuisong Li
576886bdf9cSHuisong Li attrs = kcalloc(die->port_num, sizeof(struct hccs_port_attr),
577886bdf9cSHuisong Li GFP_KERNEL);
578886bdf9cSHuisong Li if (!attrs)
579886bdf9cSHuisong Li return -ENOMEM;
580886bdf9cSHuisong Li
581886bdf9cSHuisong Li ret = hccs_get_all_port_attr(hdev, die, attrs, die->port_num);
582886bdf9cSHuisong Li if (ret)
583886bdf9cSHuisong Li goto out;
584886bdf9cSHuisong Li
585886bdf9cSHuisong Li for (i = 0; i < die->port_num; i++) {
586886bdf9cSHuisong Li port = &die->ports[i];
587886bdf9cSHuisong Li port->port_id = attrs[i].port_id;
588886bdf9cSHuisong Li port->port_type = attrs[i].port_type;
589886bdf9cSHuisong Li port->lane_mode = attrs[i].lane_mode;
590886bdf9cSHuisong Li port->enable = attrs[i].enable;
591886bdf9cSHuisong Li port->die = die;
592886bdf9cSHuisong Li }
593886bdf9cSHuisong Li
594886bdf9cSHuisong Li out:
595886bdf9cSHuisong Li kfree(attrs);
596886bdf9cSHuisong Li return ret;
597886bdf9cSHuisong Li }
598886bdf9cSHuisong Li
hccs_query_all_port_info_on_platform(struct hccs_dev * hdev)599886bdf9cSHuisong Li static int hccs_query_all_port_info_on_platform(struct hccs_dev *hdev)
600886bdf9cSHuisong Li {
601886bdf9cSHuisong Li struct device *dev = hdev->dev;
602886bdf9cSHuisong Li struct hccs_chip_info *chip;
603886bdf9cSHuisong Li struct hccs_die_info *die;
604886bdf9cSHuisong Li u8 i, j;
605886bdf9cSHuisong Li int ret;
606886bdf9cSHuisong Li
607886bdf9cSHuisong Li for (i = 0; i < hdev->chip_num; i++) {
608886bdf9cSHuisong Li chip = &hdev->chips[i];
609886bdf9cSHuisong Li for (j = 0; j < chip->die_num; j++) {
610886bdf9cSHuisong Li die = &chip->dies[j];
611886bdf9cSHuisong Li if (!die->port_num)
612886bdf9cSHuisong Li continue;
613886bdf9cSHuisong Li
614886bdf9cSHuisong Li die->ports = devm_kzalloc(dev,
615886bdf9cSHuisong Li die->port_num * sizeof(struct hccs_port_info),
616886bdf9cSHuisong Li GFP_KERNEL);
617886bdf9cSHuisong Li if (!die->ports) {
618886bdf9cSHuisong Li dev_err(dev, "allocate ports memory on chip%u/die%u failed.\n",
619886bdf9cSHuisong Li i, die->die_id);
620886bdf9cSHuisong Li return -ENOMEM;
621886bdf9cSHuisong Li }
622886bdf9cSHuisong Li
623886bdf9cSHuisong Li ret = hccs_get_all_port_info_on_die(hdev, die);
624886bdf9cSHuisong Li if (ret) {
625886bdf9cSHuisong Li dev_err(dev, "get all port info on chip%u/die%u failed, ret = %d.\n",
626886bdf9cSHuisong Li i, die->die_id, ret);
627886bdf9cSHuisong Li return ret;
628886bdf9cSHuisong Li }
629886bdf9cSHuisong Li }
630886bdf9cSHuisong Li }
631886bdf9cSHuisong Li
632886bdf9cSHuisong Li return 0;
633886bdf9cSHuisong Li }
634886bdf9cSHuisong Li
hccs_get_hw_info(struct hccs_dev * hdev)635886bdf9cSHuisong Li static int hccs_get_hw_info(struct hccs_dev *hdev)
636886bdf9cSHuisong Li {
637886bdf9cSHuisong Li int ret;
638886bdf9cSHuisong Li
639886bdf9cSHuisong Li ret = hccs_query_chip_info_on_platform(hdev);
640886bdf9cSHuisong Li if (ret) {
641886bdf9cSHuisong Li dev_err(hdev->dev, "query chip info on platform failed, ret = %d.\n",
642886bdf9cSHuisong Li ret);
643886bdf9cSHuisong Li return ret;
644886bdf9cSHuisong Li }
645886bdf9cSHuisong Li
646886bdf9cSHuisong Li ret = hccs_query_all_die_info_on_platform(hdev);
647886bdf9cSHuisong Li if (ret) {
648886bdf9cSHuisong Li dev_err(hdev->dev, "query all die info on platform failed, ret = %d.\n",
649886bdf9cSHuisong Li ret);
650886bdf9cSHuisong Li return ret;
651886bdf9cSHuisong Li }
652886bdf9cSHuisong Li
653886bdf9cSHuisong Li ret = hccs_query_all_port_info_on_platform(hdev);
654886bdf9cSHuisong Li if (ret) {
655886bdf9cSHuisong Li dev_err(hdev->dev, "query all port info on platform failed, ret = %d.\n",
656886bdf9cSHuisong Li ret);
657886bdf9cSHuisong Li return ret;
658886bdf9cSHuisong Li }
659886bdf9cSHuisong Li
660886bdf9cSHuisong Li return 0;
661886bdf9cSHuisong Li }
662886bdf9cSHuisong Li
hccs_query_port_link_status(struct hccs_dev * hdev,const struct hccs_port_info * port,struct hccs_link_status * link_status)66347f7a255SHuisong Li static int hccs_query_port_link_status(struct hccs_dev *hdev,
66447f7a255SHuisong Li const struct hccs_port_info *port,
66547f7a255SHuisong Li struct hccs_link_status *link_status)
66647f7a255SHuisong Li {
66747f7a255SHuisong Li const struct hccs_die_info *die = port->die;
66847f7a255SHuisong Li const struct hccs_chip_info *chip = die->chip;
66947f7a255SHuisong Li struct hccs_port_comm_req_param *req_param;
67047f7a255SHuisong Li struct hccs_desc desc;
67147f7a255SHuisong Li int ret;
67247f7a255SHuisong Li
67347f7a255SHuisong Li hccs_init_req_desc(&desc);
67447f7a255SHuisong Li req_param = (struct hccs_port_comm_req_param *)desc.req.data;
67547f7a255SHuisong Li req_param->chip_id = chip->chip_id;
67647f7a255SHuisong Li req_param->die_id = die->die_id;
67747f7a255SHuisong Li req_param->port_id = port->port_id;
67847f7a255SHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_LINK_STATUS, &desc);
67947f7a255SHuisong Li if (ret) {
68047f7a255SHuisong Li dev_err(hdev->dev,
68147f7a255SHuisong Li "get port link status info failed, ret = %d.\n", ret);
68247f7a255SHuisong Li return ret;
68347f7a255SHuisong Li }
68447f7a255SHuisong Li
68547f7a255SHuisong Li *link_status = *((struct hccs_link_status *)desc.rsp.data);
68647f7a255SHuisong Li
68747f7a255SHuisong Li return 0;
68847f7a255SHuisong Li }
68947f7a255SHuisong Li
hccs_query_port_crc_err_cnt(struct hccs_dev * hdev,const struct hccs_port_info * port,u64 * crc_err_cnt)69047f7a255SHuisong Li static int hccs_query_port_crc_err_cnt(struct hccs_dev *hdev,
69147f7a255SHuisong Li const struct hccs_port_info *port,
69247f7a255SHuisong Li u64 *crc_err_cnt)
69347f7a255SHuisong Li {
69447f7a255SHuisong Li const struct hccs_die_info *die = port->die;
69547f7a255SHuisong Li const struct hccs_chip_info *chip = die->chip;
69647f7a255SHuisong Li struct hccs_port_comm_req_param *req_param;
69747f7a255SHuisong Li struct hccs_desc desc;
69847f7a255SHuisong Li int ret;
69947f7a255SHuisong Li
70047f7a255SHuisong Li hccs_init_req_desc(&desc);
70147f7a255SHuisong Li req_param = (struct hccs_port_comm_req_param *)desc.req.data;
70247f7a255SHuisong Li req_param->chip_id = chip->chip_id;
70347f7a255SHuisong Li req_param->die_id = die->die_id;
70447f7a255SHuisong Li req_param->port_id = port->port_id;
70547f7a255SHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_CRC_ERR_CNT, &desc);
70647f7a255SHuisong Li if (ret) {
70747f7a255SHuisong Li dev_err(hdev->dev,
70847f7a255SHuisong Li "get port crc error count failed, ret = %d.\n", ret);
70947f7a255SHuisong Li return ret;
71047f7a255SHuisong Li }
71147f7a255SHuisong Li
71247f7a255SHuisong Li memcpy(crc_err_cnt, &desc.rsp.data, sizeof(u64));
71347f7a255SHuisong Li
71447f7a255SHuisong Li return 0;
71547f7a255SHuisong Li }
71647f7a255SHuisong Li
hccs_get_die_all_link_status(struct hccs_dev * hdev,const struct hccs_die_info * die,u8 * all_linked)71747f7a255SHuisong Li static int hccs_get_die_all_link_status(struct hccs_dev *hdev,
71847f7a255SHuisong Li const struct hccs_die_info *die,
71947f7a255SHuisong Li u8 *all_linked)
72047f7a255SHuisong Li {
72147f7a255SHuisong Li struct hccs_die_comm_req_param *req_param;
72247f7a255SHuisong Li struct hccs_desc desc;
72347f7a255SHuisong Li int ret;
72447f7a255SHuisong Li
72547f7a255SHuisong Li if (die->port_num == 0) {
72647f7a255SHuisong Li *all_linked = 1;
72747f7a255SHuisong Li return 0;
72847f7a255SHuisong Li }
72947f7a255SHuisong Li
73047f7a255SHuisong Li hccs_init_req_desc(&desc);
73147f7a255SHuisong Li req_param = (struct hccs_die_comm_req_param *)desc.req.data;
73247f7a255SHuisong Li req_param->chip_id = die->chip->chip_id;
73347f7a255SHuisong Li req_param->die_id = die->die_id;
73447f7a255SHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LINK_STA, &desc);
73547f7a255SHuisong Li if (ret) {
73647f7a255SHuisong Li dev_err(hdev->dev,
73747f7a255SHuisong Li "get link status of all ports failed on die%u, ret = %d.\n",
73847f7a255SHuisong Li die->die_id, ret);
73947f7a255SHuisong Li return ret;
74047f7a255SHuisong Li }
74147f7a255SHuisong Li
74247f7a255SHuisong Li *all_linked = *((u8 *)&desc.rsp.data);
74347f7a255SHuisong Li
74447f7a255SHuisong Li return 0;
74547f7a255SHuisong Li }
74647f7a255SHuisong Li
hccs_get_die_all_port_lane_status(struct hccs_dev * hdev,const struct hccs_die_info * die,u8 * full_lane)74747f7a255SHuisong Li static int hccs_get_die_all_port_lane_status(struct hccs_dev *hdev,
74847f7a255SHuisong Li const struct hccs_die_info *die,
74947f7a255SHuisong Li u8 *full_lane)
75047f7a255SHuisong Li {
75147f7a255SHuisong Li struct hccs_die_comm_req_param *req_param;
75247f7a255SHuisong Li struct hccs_desc desc;
75347f7a255SHuisong Li int ret;
75447f7a255SHuisong Li
75547f7a255SHuisong Li if (die->port_num == 0) {
75647f7a255SHuisong Li *full_lane = 1;
75747f7a255SHuisong Li return 0;
75847f7a255SHuisong Li }
75947f7a255SHuisong Li
76047f7a255SHuisong Li hccs_init_req_desc(&desc);
76147f7a255SHuisong Li req_param = (struct hccs_die_comm_req_param *)desc.req.data;
76247f7a255SHuisong Li req_param->chip_id = die->chip->chip_id;
76347f7a255SHuisong Li req_param->die_id = die->die_id;
76447f7a255SHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LANE_STA, &desc);
76547f7a255SHuisong Li if (ret) {
76647f7a255SHuisong Li dev_err(hdev->dev, "get lane status of all ports failed on die%u, ret = %d.\n",
76747f7a255SHuisong Li die->die_id, ret);
76847f7a255SHuisong Li return ret;
76947f7a255SHuisong Li }
77047f7a255SHuisong Li
77147f7a255SHuisong Li *full_lane = *((u8 *)&desc.rsp.data);
77247f7a255SHuisong Li
77347f7a255SHuisong Li return 0;
77447f7a255SHuisong Li }
77547f7a255SHuisong Li
hccs_get_die_total_crc_err_cnt(struct hccs_dev * hdev,const struct hccs_die_info * die,u64 * total_crc_err_cnt)77647f7a255SHuisong Li static int hccs_get_die_total_crc_err_cnt(struct hccs_dev *hdev,
77747f7a255SHuisong Li const struct hccs_die_info *die,
77847f7a255SHuisong Li u64 *total_crc_err_cnt)
77947f7a255SHuisong Li {
78047f7a255SHuisong Li struct hccs_die_comm_req_param *req_param;
78147f7a255SHuisong Li struct hccs_desc desc;
78247f7a255SHuisong Li int ret;
78347f7a255SHuisong Li
78447f7a255SHuisong Li if (die->port_num == 0) {
78547f7a255SHuisong Li *total_crc_err_cnt = 0;
78647f7a255SHuisong Li return 0;
78747f7a255SHuisong Li }
78847f7a255SHuisong Li
78947f7a255SHuisong Li hccs_init_req_desc(&desc);
79047f7a255SHuisong Li req_param = (struct hccs_die_comm_req_param *)desc.req.data;
79147f7a255SHuisong Li req_param->chip_id = die->chip->chip_id;
79247f7a255SHuisong Li req_param->die_id = die->die_id;
79347f7a255SHuisong Li ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_CRC_ERR_CNT, &desc);
79447f7a255SHuisong Li if (ret) {
79547f7a255SHuisong Li dev_err(hdev->dev, "get crc error count sum failed on die%u, ret = %d.\n",
79647f7a255SHuisong Li die->die_id, ret);
79747f7a255SHuisong Li return ret;
79847f7a255SHuisong Li }
79947f7a255SHuisong Li
80047f7a255SHuisong Li memcpy(total_crc_err_cnt, &desc.rsp.data, sizeof(u64));
80147f7a255SHuisong Li
80247f7a255SHuisong Li return 0;
80347f7a255SHuisong Li }
80447f7a255SHuisong Li
hccs_show(struct kobject * k,struct attribute * attr,char * buf)80547f7a255SHuisong Li static ssize_t hccs_show(struct kobject *k, struct attribute *attr, char *buf)
80647f7a255SHuisong Li {
80747f7a255SHuisong Li struct kobj_attribute *kobj_attr;
80847f7a255SHuisong Li
80947f7a255SHuisong Li kobj_attr = container_of(attr, struct kobj_attribute, attr);
81047f7a255SHuisong Li
81147f7a255SHuisong Li return kobj_attr->show(k, kobj_attr, buf);
81247f7a255SHuisong Li }
81347f7a255SHuisong Li
81447f7a255SHuisong Li static const struct sysfs_ops hccs_comm_ops = {
81547f7a255SHuisong Li .show = hccs_show,
81647f7a255SHuisong Li };
81747f7a255SHuisong Li
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)81847f7a255SHuisong Li static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
81947f7a255SHuisong Li char *buf)
82047f7a255SHuisong Li {
82147f7a255SHuisong Li const struct hccs_port_info *port = kobj_to_port_info(kobj);
82247f7a255SHuisong Li
82347f7a255SHuisong Li return sysfs_emit(buf, "HCCS-v%u\n", port->port_type);
82447f7a255SHuisong Li }
82547f7a255SHuisong Li static struct kobj_attribute hccs_type_attr = __ATTR_RO(type);
82647f7a255SHuisong Li
lane_mode_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)82747f7a255SHuisong Li static ssize_t lane_mode_show(struct kobject *kobj, struct kobj_attribute *attr,
82847f7a255SHuisong Li char *buf)
82947f7a255SHuisong Li {
83047f7a255SHuisong Li const struct hccs_port_info *port = kobj_to_port_info(kobj);
83147f7a255SHuisong Li
83247f7a255SHuisong Li return sysfs_emit(buf, "x%u\n", port->lane_mode);
83347f7a255SHuisong Li }
83447f7a255SHuisong Li static struct kobj_attribute lane_mode_attr = __ATTR_RO(lane_mode);
83547f7a255SHuisong Li
enable_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)83647f7a255SHuisong Li static ssize_t enable_show(struct kobject *kobj,
83747f7a255SHuisong Li struct kobj_attribute *attr, char *buf)
83847f7a255SHuisong Li {
83947f7a255SHuisong Li const struct hccs_port_info *port = kobj_to_port_info(kobj);
84047f7a255SHuisong Li
84147f7a255SHuisong Li return sysfs_emit(buf, "%u\n", port->enable);
84247f7a255SHuisong Li }
84347f7a255SHuisong Li static struct kobj_attribute port_enable_attr = __ATTR_RO(enable);
84447f7a255SHuisong Li
cur_lane_num_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)84547f7a255SHuisong Li static ssize_t cur_lane_num_show(struct kobject *kobj,
84647f7a255SHuisong Li struct kobj_attribute *attr, char *buf)
84747f7a255SHuisong Li {
84847f7a255SHuisong Li const struct hccs_port_info *port = kobj_to_port_info(kobj);
84947f7a255SHuisong Li struct hccs_dev *hdev = port->die->chip->hdev;
85047f7a255SHuisong Li struct hccs_link_status link_status = {0};
85147f7a255SHuisong Li int ret;
85247f7a255SHuisong Li
85347f7a255SHuisong Li mutex_lock(&hdev->lock);
85447f7a255SHuisong Li ret = hccs_query_port_link_status(hdev, port, &link_status);
85547f7a255SHuisong Li mutex_unlock(&hdev->lock);
85647f7a255SHuisong Li if (ret)
85747f7a255SHuisong Li return ret;
85847f7a255SHuisong Li
85947f7a255SHuisong Li return sysfs_emit(buf, "%u\n", link_status.lane_num);
86047f7a255SHuisong Li }
86147f7a255SHuisong Li static struct kobj_attribute cur_lane_num_attr = __ATTR_RO(cur_lane_num);
86247f7a255SHuisong Li
link_fsm_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)86347f7a255SHuisong Li static ssize_t link_fsm_show(struct kobject *kobj,
86447f7a255SHuisong Li struct kobj_attribute *attr, char *buf)
86547f7a255SHuisong Li {
86647f7a255SHuisong Li const struct hccs_port_info *port = kobj_to_port_info(kobj);
86747f7a255SHuisong Li struct hccs_dev *hdev = port->die->chip->hdev;
86847f7a255SHuisong Li struct hccs_link_status link_status = {0};
86947f7a255SHuisong Li const struct {
87047f7a255SHuisong Li u8 link_fsm;
87147f7a255SHuisong Li char *str;
87247f7a255SHuisong Li } link_fsm_map[] = {
87347f7a255SHuisong Li {HCCS_PORT_RESET, "reset"},
87447f7a255SHuisong Li {HCCS_PORT_SETUP, "setup"},
87547f7a255SHuisong Li {HCCS_PORT_CONFIG, "config"},
87647f7a255SHuisong Li {HCCS_PORT_READY, "link-up"},
87747f7a255SHuisong Li };
87847f7a255SHuisong Li const char *link_fsm_str = "unknown";
87947f7a255SHuisong Li size_t i;
88047f7a255SHuisong Li int ret;
88147f7a255SHuisong Li
88247f7a255SHuisong Li mutex_lock(&hdev->lock);
88347f7a255SHuisong Li ret = hccs_query_port_link_status(hdev, port, &link_status);
88447f7a255SHuisong Li mutex_unlock(&hdev->lock);
88547f7a255SHuisong Li if (ret)
88647f7a255SHuisong Li return ret;
88747f7a255SHuisong Li
88847f7a255SHuisong Li for (i = 0; i < ARRAY_SIZE(link_fsm_map); i++) {
88947f7a255SHuisong Li if (link_fsm_map[i].link_fsm == link_status.link_fsm) {
89047f7a255SHuisong Li link_fsm_str = link_fsm_map[i].str;
89147f7a255SHuisong Li break;
89247f7a255SHuisong Li }
89347f7a255SHuisong Li }
89447f7a255SHuisong Li
89547f7a255SHuisong Li return sysfs_emit(buf, "%s\n", link_fsm_str);
89647f7a255SHuisong Li }
89747f7a255SHuisong Li static struct kobj_attribute link_fsm_attr = __ATTR_RO(link_fsm);
89847f7a255SHuisong Li
lane_mask_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)89947f7a255SHuisong Li static ssize_t lane_mask_show(struct kobject *kobj,
90047f7a255SHuisong Li struct kobj_attribute *attr, char *buf)
90147f7a255SHuisong Li {
90247f7a255SHuisong Li const struct hccs_port_info *port = kobj_to_port_info(kobj);
90347f7a255SHuisong Li struct hccs_dev *hdev = port->die->chip->hdev;
90447f7a255SHuisong Li struct hccs_link_status link_status = {0};
90547f7a255SHuisong Li int ret;
90647f7a255SHuisong Li
90747f7a255SHuisong Li mutex_lock(&hdev->lock);
90847f7a255SHuisong Li ret = hccs_query_port_link_status(hdev, port, &link_status);
90947f7a255SHuisong Li mutex_unlock(&hdev->lock);
91047f7a255SHuisong Li if (ret)
91147f7a255SHuisong Li return ret;
91247f7a255SHuisong Li
91347f7a255SHuisong Li return sysfs_emit(buf, "0x%x\n", link_status.lane_mask);
91447f7a255SHuisong Li }
91547f7a255SHuisong Li static struct kobj_attribute lane_mask_attr = __ATTR_RO(lane_mask);
91647f7a255SHuisong Li
crc_err_cnt_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)91747f7a255SHuisong Li static ssize_t crc_err_cnt_show(struct kobject *kobj,
91847f7a255SHuisong Li struct kobj_attribute *attr, char *buf)
91947f7a255SHuisong Li {
92047f7a255SHuisong Li const struct hccs_port_info *port = kobj_to_port_info(kobj);
92147f7a255SHuisong Li struct hccs_dev *hdev = port->die->chip->hdev;
92247f7a255SHuisong Li u64 crc_err_cnt;
92347f7a255SHuisong Li int ret;
92447f7a255SHuisong Li
92547f7a255SHuisong Li mutex_lock(&hdev->lock);
92647f7a255SHuisong Li ret = hccs_query_port_crc_err_cnt(hdev, port, &crc_err_cnt);
92747f7a255SHuisong Li mutex_unlock(&hdev->lock);
92847f7a255SHuisong Li if (ret)
92947f7a255SHuisong Li return ret;
93047f7a255SHuisong Li
93147f7a255SHuisong Li return sysfs_emit(buf, "%llu\n", crc_err_cnt);
93247f7a255SHuisong Li }
93347f7a255SHuisong Li static struct kobj_attribute crc_err_cnt_attr = __ATTR_RO(crc_err_cnt);
93447f7a255SHuisong Li
93547f7a255SHuisong Li static struct attribute *hccs_port_default_attrs[] = {
93647f7a255SHuisong Li &hccs_type_attr.attr,
93747f7a255SHuisong Li &lane_mode_attr.attr,
93847f7a255SHuisong Li &port_enable_attr.attr,
93947f7a255SHuisong Li &cur_lane_num_attr.attr,
94047f7a255SHuisong Li &link_fsm_attr.attr,
94147f7a255SHuisong Li &lane_mask_attr.attr,
94247f7a255SHuisong Li &crc_err_cnt_attr.attr,
94347f7a255SHuisong Li NULL,
94447f7a255SHuisong Li };
94547f7a255SHuisong Li ATTRIBUTE_GROUPS(hccs_port_default);
94647f7a255SHuisong Li
94747f7a255SHuisong Li static const struct kobj_type hccs_port_type = {
94847f7a255SHuisong Li .sysfs_ops = &hccs_comm_ops,
94947f7a255SHuisong Li .default_groups = hccs_port_default_groups,
95047f7a255SHuisong Li };
95147f7a255SHuisong Li
all_linked_on_die_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)95247f7a255SHuisong Li static ssize_t all_linked_on_die_show(struct kobject *kobj,
95347f7a255SHuisong Li struct kobj_attribute *attr, char *buf)
95447f7a255SHuisong Li {
95547f7a255SHuisong Li const struct hccs_die_info *die = kobj_to_die_info(kobj);
95647f7a255SHuisong Li struct hccs_dev *hdev = die->chip->hdev;
95747f7a255SHuisong Li u8 all_linked;
95847f7a255SHuisong Li int ret;
95947f7a255SHuisong Li
96047f7a255SHuisong Li mutex_lock(&hdev->lock);
96147f7a255SHuisong Li ret = hccs_get_die_all_link_status(hdev, die, &all_linked);
96247f7a255SHuisong Li mutex_unlock(&hdev->lock);
96347f7a255SHuisong Li if (ret)
96447f7a255SHuisong Li return ret;
96547f7a255SHuisong Li
96647f7a255SHuisong Li return sysfs_emit(buf, "%u\n", all_linked);
96747f7a255SHuisong Li }
96847f7a255SHuisong Li static struct kobj_attribute all_linked_on_die_attr =
96947f7a255SHuisong Li __ATTR(all_linked, 0444, all_linked_on_die_show, NULL);
97047f7a255SHuisong Li
linked_full_lane_on_die_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)97147f7a255SHuisong Li static ssize_t linked_full_lane_on_die_show(struct kobject *kobj,
97247f7a255SHuisong Li struct kobj_attribute *attr,
97347f7a255SHuisong Li char *buf)
97447f7a255SHuisong Li {
97547f7a255SHuisong Li const struct hccs_die_info *die = kobj_to_die_info(kobj);
97647f7a255SHuisong Li struct hccs_dev *hdev = die->chip->hdev;
97747f7a255SHuisong Li u8 full_lane;
97847f7a255SHuisong Li int ret;
97947f7a255SHuisong Li
98047f7a255SHuisong Li mutex_lock(&hdev->lock);
98147f7a255SHuisong Li ret = hccs_get_die_all_port_lane_status(hdev, die, &full_lane);
98247f7a255SHuisong Li mutex_unlock(&hdev->lock);
98347f7a255SHuisong Li if (ret)
98447f7a255SHuisong Li return ret;
98547f7a255SHuisong Li
98647f7a255SHuisong Li return sysfs_emit(buf, "%u\n", full_lane);
98747f7a255SHuisong Li }
98847f7a255SHuisong Li static struct kobj_attribute linked_full_lane_on_die_attr =
98947f7a255SHuisong Li __ATTR(linked_full_lane, 0444, linked_full_lane_on_die_show, NULL);
99047f7a255SHuisong Li
crc_err_cnt_sum_on_die_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)99147f7a255SHuisong Li static ssize_t crc_err_cnt_sum_on_die_show(struct kobject *kobj,
99247f7a255SHuisong Li struct kobj_attribute *attr,
99347f7a255SHuisong Li char *buf)
99447f7a255SHuisong Li {
99547f7a255SHuisong Li const struct hccs_die_info *die = kobj_to_die_info(kobj);
99647f7a255SHuisong Li struct hccs_dev *hdev = die->chip->hdev;
99747f7a255SHuisong Li u64 total_crc_err_cnt;
99847f7a255SHuisong Li int ret;
99947f7a255SHuisong Li
100047f7a255SHuisong Li mutex_lock(&hdev->lock);
100147f7a255SHuisong Li ret = hccs_get_die_total_crc_err_cnt(hdev, die, &total_crc_err_cnt);
100247f7a255SHuisong Li mutex_unlock(&hdev->lock);
100347f7a255SHuisong Li if (ret)
100447f7a255SHuisong Li return ret;
100547f7a255SHuisong Li
100647f7a255SHuisong Li return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
100747f7a255SHuisong Li }
100847f7a255SHuisong Li static struct kobj_attribute crc_err_cnt_sum_on_die_attr =
100947f7a255SHuisong Li __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_die_show, NULL);
101047f7a255SHuisong Li
101147f7a255SHuisong Li static struct attribute *hccs_die_default_attrs[] = {
101247f7a255SHuisong Li &all_linked_on_die_attr.attr,
101347f7a255SHuisong Li &linked_full_lane_on_die_attr.attr,
101447f7a255SHuisong Li &crc_err_cnt_sum_on_die_attr.attr,
101547f7a255SHuisong Li NULL,
101647f7a255SHuisong Li };
101747f7a255SHuisong Li ATTRIBUTE_GROUPS(hccs_die_default);
101847f7a255SHuisong Li
101947f7a255SHuisong Li static const struct kobj_type hccs_die_type = {
102047f7a255SHuisong Li .sysfs_ops = &hccs_comm_ops,
102147f7a255SHuisong Li .default_groups = hccs_die_default_groups,
102247f7a255SHuisong Li };
102347f7a255SHuisong Li
all_linked_on_chip_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)102447f7a255SHuisong Li static ssize_t all_linked_on_chip_show(struct kobject *kobj,
102547f7a255SHuisong Li struct kobj_attribute *attr, char *buf)
102647f7a255SHuisong Li {
102747f7a255SHuisong Li const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
102847f7a255SHuisong Li struct hccs_dev *hdev = chip->hdev;
102947f7a255SHuisong Li const struct hccs_die_info *die;
103047f7a255SHuisong Li u8 all_linked = 1;
103147f7a255SHuisong Li u8 i, tmp;
103247f7a255SHuisong Li int ret;
103347f7a255SHuisong Li
103447f7a255SHuisong Li mutex_lock(&hdev->lock);
103547f7a255SHuisong Li for (i = 0; i < chip->die_num; i++) {
103647f7a255SHuisong Li die = &chip->dies[i];
103747f7a255SHuisong Li ret = hccs_get_die_all_link_status(hdev, die, &tmp);
103847f7a255SHuisong Li if (ret) {
103947f7a255SHuisong Li mutex_unlock(&hdev->lock);
104047f7a255SHuisong Li return ret;
104147f7a255SHuisong Li }
104247f7a255SHuisong Li if (tmp != all_linked) {
104347f7a255SHuisong Li all_linked = 0;
104447f7a255SHuisong Li break;
104547f7a255SHuisong Li }
104647f7a255SHuisong Li }
104747f7a255SHuisong Li mutex_unlock(&hdev->lock);
104847f7a255SHuisong Li
104947f7a255SHuisong Li return sysfs_emit(buf, "%u\n", all_linked);
105047f7a255SHuisong Li }
105147f7a255SHuisong Li static struct kobj_attribute all_linked_on_chip_attr =
105247f7a255SHuisong Li __ATTR(all_linked, 0444, all_linked_on_chip_show, NULL);
105347f7a255SHuisong Li
linked_full_lane_on_chip_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)105447f7a255SHuisong Li static ssize_t linked_full_lane_on_chip_show(struct kobject *kobj,
105547f7a255SHuisong Li struct kobj_attribute *attr,
105647f7a255SHuisong Li char *buf)
105747f7a255SHuisong Li {
105847f7a255SHuisong Li const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
105947f7a255SHuisong Li struct hccs_dev *hdev = chip->hdev;
106047f7a255SHuisong Li const struct hccs_die_info *die;
106147f7a255SHuisong Li u8 full_lane = 1;
106247f7a255SHuisong Li u8 i, tmp;
106347f7a255SHuisong Li int ret;
106447f7a255SHuisong Li
106547f7a255SHuisong Li mutex_lock(&hdev->lock);
106647f7a255SHuisong Li for (i = 0; i < chip->die_num; i++) {
106747f7a255SHuisong Li die = &chip->dies[i];
106847f7a255SHuisong Li ret = hccs_get_die_all_port_lane_status(hdev, die, &tmp);
106947f7a255SHuisong Li if (ret) {
107047f7a255SHuisong Li mutex_unlock(&hdev->lock);
107147f7a255SHuisong Li return ret;
107247f7a255SHuisong Li }
107347f7a255SHuisong Li if (tmp != full_lane) {
107447f7a255SHuisong Li full_lane = 0;
107547f7a255SHuisong Li break;
107647f7a255SHuisong Li }
107747f7a255SHuisong Li }
107847f7a255SHuisong Li mutex_unlock(&hdev->lock);
107947f7a255SHuisong Li
108047f7a255SHuisong Li return sysfs_emit(buf, "%u\n", full_lane);
108147f7a255SHuisong Li }
108247f7a255SHuisong Li static struct kobj_attribute linked_full_lane_on_chip_attr =
108347f7a255SHuisong Li __ATTR(linked_full_lane, 0444, linked_full_lane_on_chip_show, NULL);
108447f7a255SHuisong Li
crc_err_cnt_sum_on_chip_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)108547f7a255SHuisong Li static ssize_t crc_err_cnt_sum_on_chip_show(struct kobject *kobj,
108647f7a255SHuisong Li struct kobj_attribute *attr,
108747f7a255SHuisong Li char *buf)
108847f7a255SHuisong Li {
108947f7a255SHuisong Li const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
109047f7a255SHuisong Li u64 crc_err_cnt, total_crc_err_cnt = 0;
109147f7a255SHuisong Li struct hccs_dev *hdev = chip->hdev;
109247f7a255SHuisong Li const struct hccs_die_info *die;
109347f7a255SHuisong Li int ret;
109447f7a255SHuisong Li u16 i;
109547f7a255SHuisong Li
109647f7a255SHuisong Li mutex_lock(&hdev->lock);
109747f7a255SHuisong Li for (i = 0; i < chip->die_num; i++) {
109847f7a255SHuisong Li die = &chip->dies[i];
109947f7a255SHuisong Li ret = hccs_get_die_total_crc_err_cnt(hdev, die, &crc_err_cnt);
110047f7a255SHuisong Li if (ret) {
110147f7a255SHuisong Li mutex_unlock(&hdev->lock);
110247f7a255SHuisong Li return ret;
110347f7a255SHuisong Li }
110447f7a255SHuisong Li
110547f7a255SHuisong Li total_crc_err_cnt += crc_err_cnt;
110647f7a255SHuisong Li }
110747f7a255SHuisong Li mutex_unlock(&hdev->lock);
110847f7a255SHuisong Li
110947f7a255SHuisong Li return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
111047f7a255SHuisong Li }
111147f7a255SHuisong Li static struct kobj_attribute crc_err_cnt_sum_on_chip_attr =
111247f7a255SHuisong Li __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_chip_show, NULL);
111347f7a255SHuisong Li
111447f7a255SHuisong Li static struct attribute *hccs_chip_default_attrs[] = {
111547f7a255SHuisong Li &all_linked_on_chip_attr.attr,
111647f7a255SHuisong Li &linked_full_lane_on_chip_attr.attr,
111747f7a255SHuisong Li &crc_err_cnt_sum_on_chip_attr.attr,
111847f7a255SHuisong Li NULL,
111947f7a255SHuisong Li };
112047f7a255SHuisong Li ATTRIBUTE_GROUPS(hccs_chip_default);
112147f7a255SHuisong Li
112247f7a255SHuisong Li static const struct kobj_type hccs_chip_type = {
112347f7a255SHuisong Li .sysfs_ops = &hccs_comm_ops,
112447f7a255SHuisong Li .default_groups = hccs_chip_default_groups,
112547f7a255SHuisong Li };
112647f7a255SHuisong Li
hccs_remove_die_dir(struct hccs_die_info * die)112747f7a255SHuisong Li static void hccs_remove_die_dir(struct hccs_die_info *die)
112847f7a255SHuisong Li {
112947f7a255SHuisong Li struct hccs_port_info *port;
113047f7a255SHuisong Li u8 i;
113147f7a255SHuisong Li
113247f7a255SHuisong Li for (i = 0; i < die->port_num; i++) {
113347f7a255SHuisong Li port = &die->ports[i];
113447f7a255SHuisong Li if (port->dir_created)
113547f7a255SHuisong Li kobject_put(&port->kobj);
113647f7a255SHuisong Li }
113747f7a255SHuisong Li
113847f7a255SHuisong Li kobject_put(&die->kobj);
113947f7a255SHuisong Li }
114047f7a255SHuisong Li
hccs_remove_chip_dir(struct hccs_chip_info * chip)114147f7a255SHuisong Li static void hccs_remove_chip_dir(struct hccs_chip_info *chip)
114247f7a255SHuisong Li {
114347f7a255SHuisong Li struct hccs_die_info *die;
114447f7a255SHuisong Li u8 i;
114547f7a255SHuisong Li
114647f7a255SHuisong Li for (i = 0; i < chip->die_num; i++) {
114747f7a255SHuisong Li die = &chip->dies[i];
114847f7a255SHuisong Li if (die->dir_created)
114947f7a255SHuisong Li hccs_remove_die_dir(die);
115047f7a255SHuisong Li }
115147f7a255SHuisong Li
115247f7a255SHuisong Li kobject_put(&chip->kobj);
115347f7a255SHuisong Li }
115447f7a255SHuisong Li
hccs_remove_topo_dirs(struct hccs_dev * hdev)115547f7a255SHuisong Li static void hccs_remove_topo_dirs(struct hccs_dev *hdev)
115647f7a255SHuisong Li {
115747f7a255SHuisong Li u8 i;
115847f7a255SHuisong Li
115947f7a255SHuisong Li for (i = 0; i < hdev->chip_num; i++)
116047f7a255SHuisong Li hccs_remove_chip_dir(&hdev->chips[i]);
116147f7a255SHuisong Li }
116247f7a255SHuisong Li
hccs_create_hccs_dir(struct hccs_dev * hdev,struct hccs_die_info * die,struct hccs_port_info * port)116347f7a255SHuisong Li static int hccs_create_hccs_dir(struct hccs_dev *hdev,
116447f7a255SHuisong Li struct hccs_die_info *die,
116547f7a255SHuisong Li struct hccs_port_info *port)
116647f7a255SHuisong Li {
116747f7a255SHuisong Li int ret;
116847f7a255SHuisong Li
116947f7a255SHuisong Li ret = kobject_init_and_add(&port->kobj, &hccs_port_type,
1170734add1aSHuisong Li &die->kobj, "hccs%u", port->port_id);
117147f7a255SHuisong Li if (ret) {
117247f7a255SHuisong Li kobject_put(&port->kobj);
117347f7a255SHuisong Li return ret;
117447f7a255SHuisong Li }
117547f7a255SHuisong Li
117647f7a255SHuisong Li return 0;
117747f7a255SHuisong Li }
117847f7a255SHuisong Li
hccs_create_die_dir(struct hccs_dev * hdev,struct hccs_chip_info * chip,struct hccs_die_info * die)117947f7a255SHuisong Li static int hccs_create_die_dir(struct hccs_dev *hdev,
118047f7a255SHuisong Li struct hccs_chip_info *chip,
118147f7a255SHuisong Li struct hccs_die_info *die)
118247f7a255SHuisong Li {
118347f7a255SHuisong Li struct hccs_port_info *port;
118447f7a255SHuisong Li int ret;
118547f7a255SHuisong Li u16 i;
118647f7a255SHuisong Li
118747f7a255SHuisong Li ret = kobject_init_and_add(&die->kobj, &hccs_die_type,
1188734add1aSHuisong Li &chip->kobj, "die%u", die->die_id);
118947f7a255SHuisong Li if (ret) {
119047f7a255SHuisong Li kobject_put(&die->kobj);
119147f7a255SHuisong Li return ret;
119247f7a255SHuisong Li }
119347f7a255SHuisong Li
119447f7a255SHuisong Li for (i = 0; i < die->port_num; i++) {
119547f7a255SHuisong Li port = &die->ports[i];
119647f7a255SHuisong Li ret = hccs_create_hccs_dir(hdev, die, port);
119747f7a255SHuisong Li if (ret) {
1198734add1aSHuisong Li dev_err(hdev->dev, "create hccs%u dir failed.\n",
119947f7a255SHuisong Li port->port_id);
120047f7a255SHuisong Li goto err;
120147f7a255SHuisong Li }
120247f7a255SHuisong Li port->dir_created = true;
120347f7a255SHuisong Li }
120447f7a255SHuisong Li
120547f7a255SHuisong Li return 0;
120647f7a255SHuisong Li err:
120747f7a255SHuisong Li hccs_remove_die_dir(die);
120847f7a255SHuisong Li
120947f7a255SHuisong Li return ret;
121047f7a255SHuisong Li }
121147f7a255SHuisong Li
hccs_create_chip_dir(struct hccs_dev * hdev,struct hccs_chip_info * chip)121247f7a255SHuisong Li static int hccs_create_chip_dir(struct hccs_dev *hdev,
121347f7a255SHuisong Li struct hccs_chip_info *chip)
121447f7a255SHuisong Li {
121547f7a255SHuisong Li struct hccs_die_info *die;
121647f7a255SHuisong Li int ret;
121747f7a255SHuisong Li u16 id;
121847f7a255SHuisong Li
121947f7a255SHuisong Li ret = kobject_init_and_add(&chip->kobj, &hccs_chip_type,
1220734add1aSHuisong Li &hdev->dev->kobj, "chip%u", chip->chip_id);
122147f7a255SHuisong Li if (ret) {
122247f7a255SHuisong Li kobject_put(&chip->kobj);
122347f7a255SHuisong Li return ret;
122447f7a255SHuisong Li }
122547f7a255SHuisong Li
122647f7a255SHuisong Li for (id = 0; id < chip->die_num; id++) {
122747f7a255SHuisong Li die = &chip->dies[id];
122847f7a255SHuisong Li ret = hccs_create_die_dir(hdev, chip, die);
122947f7a255SHuisong Li if (ret)
123047f7a255SHuisong Li goto err;
123147f7a255SHuisong Li die->dir_created = true;
123247f7a255SHuisong Li }
123347f7a255SHuisong Li
123447f7a255SHuisong Li return 0;
123547f7a255SHuisong Li err:
123647f7a255SHuisong Li hccs_remove_chip_dir(chip);
123747f7a255SHuisong Li
123847f7a255SHuisong Li return ret;
123947f7a255SHuisong Li }
124047f7a255SHuisong Li
hccs_create_topo_dirs(struct hccs_dev * hdev)124147f7a255SHuisong Li static int hccs_create_topo_dirs(struct hccs_dev *hdev)
124247f7a255SHuisong Li {
124347f7a255SHuisong Li struct hccs_chip_info *chip;
124447f7a255SHuisong Li u8 id, k;
124547f7a255SHuisong Li int ret;
124647f7a255SHuisong Li
124747f7a255SHuisong Li for (id = 0; id < hdev->chip_num; id++) {
124847f7a255SHuisong Li chip = &hdev->chips[id];
124947f7a255SHuisong Li ret = hccs_create_chip_dir(hdev, chip);
125047f7a255SHuisong Li if (ret) {
1251734add1aSHuisong Li dev_err(hdev->dev, "init chip%u dir failed!\n", id);
125247f7a255SHuisong Li goto err;
125347f7a255SHuisong Li }
125447f7a255SHuisong Li }
125547f7a255SHuisong Li
125647f7a255SHuisong Li return 0;
125747f7a255SHuisong Li err:
125847f7a255SHuisong Li for (k = 0; k < id; k++)
125947f7a255SHuisong Li hccs_remove_chip_dir(&hdev->chips[k]);
126047f7a255SHuisong Li
126147f7a255SHuisong Li return ret;
126247f7a255SHuisong Li }
126347f7a255SHuisong Li
hccs_probe(struct platform_device * pdev)1264886bdf9cSHuisong Li static int hccs_probe(struct platform_device *pdev)
1265886bdf9cSHuisong Li {
1266886bdf9cSHuisong Li struct acpi_device *acpi_dev;
1267886bdf9cSHuisong Li struct hccs_dev *hdev;
1268886bdf9cSHuisong Li int rc;
1269886bdf9cSHuisong Li
1270886bdf9cSHuisong Li if (acpi_disabled) {
1271886bdf9cSHuisong Li dev_err(&pdev->dev, "acpi is disabled.\n");
1272886bdf9cSHuisong Li return -ENODEV;
1273886bdf9cSHuisong Li }
1274886bdf9cSHuisong Li acpi_dev = ACPI_COMPANION(&pdev->dev);
1275886bdf9cSHuisong Li if (!acpi_dev)
1276886bdf9cSHuisong Li return -ENODEV;
1277886bdf9cSHuisong Li
1278886bdf9cSHuisong Li hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
1279886bdf9cSHuisong Li if (!hdev)
1280886bdf9cSHuisong Li return -ENOMEM;
1281886bdf9cSHuisong Li hdev->acpi_dev = acpi_dev;
1282886bdf9cSHuisong Li hdev->dev = &pdev->dev;
1283886bdf9cSHuisong Li platform_set_drvdata(pdev, hdev);
1284886bdf9cSHuisong Li
1285be2f78a8SHuisong Li /*
1286be2f78a8SHuisong Li * Here would never be failure as the driver and device has been matched.
1287be2f78a8SHuisong Li */
1288be2f78a8SHuisong Li hdev->verspec_data = acpi_device_get_match_data(hdev->dev);
1289be2f78a8SHuisong Li
1290886bdf9cSHuisong Li mutex_init(&hdev->lock);
1291886bdf9cSHuisong Li rc = hccs_get_pcc_chan_id(hdev);
1292886bdf9cSHuisong Li if (rc)
1293886bdf9cSHuisong Li return rc;
1294886bdf9cSHuisong Li rc = hccs_register_pcc_channel(hdev);
1295886bdf9cSHuisong Li if (rc)
1296886bdf9cSHuisong Li return rc;
1297886bdf9cSHuisong Li
1298886bdf9cSHuisong Li rc = hccs_get_dev_caps(hdev);
1299886bdf9cSHuisong Li if (rc)
1300886bdf9cSHuisong Li goto unregister_pcc_chan;
1301886bdf9cSHuisong Li
1302886bdf9cSHuisong Li rc = hccs_get_hw_info(hdev);
1303886bdf9cSHuisong Li if (rc)
1304886bdf9cSHuisong Li goto unregister_pcc_chan;
1305886bdf9cSHuisong Li
130647f7a255SHuisong Li rc = hccs_create_topo_dirs(hdev);
130747f7a255SHuisong Li if (rc)
130847f7a255SHuisong Li goto unregister_pcc_chan;
130947f7a255SHuisong Li
1310886bdf9cSHuisong Li return 0;
1311886bdf9cSHuisong Li
1312886bdf9cSHuisong Li unregister_pcc_chan:
1313886bdf9cSHuisong Li hccs_unregister_pcc_channel(hdev);
1314886bdf9cSHuisong Li
1315886bdf9cSHuisong Li return rc;
1316886bdf9cSHuisong Li }
1317886bdf9cSHuisong Li
hccs_remove(struct platform_device * pdev)13183f40c999SUwe Kleine-König static void hccs_remove(struct platform_device *pdev)
1319886bdf9cSHuisong Li {
1320886bdf9cSHuisong Li struct hccs_dev *hdev = platform_get_drvdata(pdev);
1321886bdf9cSHuisong Li
132247f7a255SHuisong Li hccs_remove_topo_dirs(hdev);
1323886bdf9cSHuisong Li hccs_unregister_pcc_channel(hdev);
1324886bdf9cSHuisong Li }
1325886bdf9cSHuisong Li
1326be2f78a8SHuisong Li static const struct hccs_verspecific_data hisi04b1_verspec_data = {
1327be2f78a8SHuisong Li .rx_callback = NULL,
1328be2f78a8SHuisong Li .wait_cmd_complete = hccs_wait_cmd_complete_by_poll,
1329be2f78a8SHuisong Li .fill_pcc_shared_mem = hccs_fill_pcc_shared_mem_region,
1330be2f78a8SHuisong Li .shared_mem_size = sizeof(struct acpi_pcct_shared_memory),
1331be2f78a8SHuisong Li .has_txdone_irq = false,
1332be2f78a8SHuisong Li };
1333be2f78a8SHuisong Li
1334be2f78a8SHuisong Li static const struct hccs_verspecific_data hisi04b2_verspec_data = {
1335be2f78a8SHuisong Li .rx_callback = hccs_pcc_rx_callback,
1336be2f78a8SHuisong Li .wait_cmd_complete = hccs_wait_cmd_complete_by_irq,
1337be2f78a8SHuisong Li .fill_pcc_shared_mem = hccs_fill_ext_pcc_shared_mem_region,
1338be2f78a8SHuisong Li .shared_mem_size = sizeof(struct acpi_pcct_ext_pcc_shared_memory),
1339be2f78a8SHuisong Li .has_txdone_irq = true,
1340be2f78a8SHuisong Li };
1341be2f78a8SHuisong Li
1342886bdf9cSHuisong Li static const struct acpi_device_id hccs_acpi_match[] = {
1343be2f78a8SHuisong Li { "HISI04B1", (unsigned long)&hisi04b1_verspec_data},
1344be2f78a8SHuisong Li { "HISI04B2", (unsigned long)&hisi04b2_verspec_data},
1345be2f78a8SHuisong Li { }
1346886bdf9cSHuisong Li };
1347886bdf9cSHuisong Li MODULE_DEVICE_TABLE(acpi, hccs_acpi_match);
1348886bdf9cSHuisong Li
1349886bdf9cSHuisong Li static struct platform_driver hccs_driver = {
1350886bdf9cSHuisong Li .probe = hccs_probe,
13513f40c999SUwe Kleine-König .remove_new = hccs_remove,
1352886bdf9cSHuisong Li .driver = {
1353886bdf9cSHuisong Li .name = "kunpeng_hccs",
1354886bdf9cSHuisong Li .acpi_match_table = hccs_acpi_match,
1355886bdf9cSHuisong Li },
1356886bdf9cSHuisong Li };
1357886bdf9cSHuisong Li
1358886bdf9cSHuisong Li module_platform_driver(hccs_driver);
1359886bdf9cSHuisong Li
1360886bdf9cSHuisong Li MODULE_DESCRIPTION("Kunpeng SoC HCCS driver");
1361886bdf9cSHuisong Li MODULE_LICENSE("GPL");
1362886bdf9cSHuisong Li MODULE_AUTHOR("Huisong Li <lihuisong@huawei.com>");
1363