soc: kunpeng_hccs: fix size_t format string
[platform/kernel/linux-starfive.git] / drivers / soc / hisilicon / kunpeng_hccs.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * The Huawei Cache Coherence System (HCCS) is a multi-chip interconnection
4  * bus protocol.
5  *
6  * Copyright (c) 2023 Hisilicon Limited.
7  * Author: Huisong Li <lihuisong@huawei.com>
8  *
9  * HCCS driver for Kunpeng SoC provides the following features:
10  * - Retrieve the following information about each port:
11  *    - port type
12  *    - lane mode
13  *    - enable
14  *    - current lane mode
15  *    - link finite state machine
16  *    - lane mask
17  *    - CRC error count
18  *
19  * - Retrieve the following information about all the ports on the chip or
20  *   the die:
21  *    - if all enabled ports are in linked
22  *    - if all linked ports are in full lane
23  *    - CRC error count sum
24  */
25 #include <linux/acpi.h>
26 #include <linux/iopoll.h>
27 #include <linux/platform_device.h>
28 #include <linux/sysfs.h>
29
30 #include <acpi/pcc.h>
31
32 #include "kunpeng_hccs.h"
33
34 /* PCC defines */
35 #define HCCS_PCC_SIGNATURE_MASK         0x50434300
36 #define HCCS_PCC_STATUS_CMD_COMPLETE    BIT(0)
37
38 /*
39  * Arbitrary retries in case the remote processor is slow to respond
40  * to PCC commands
41  */
42 #define HCCS_PCC_CMD_WAIT_RETRIES_NUM           500ULL
43 #define HCCS_POLL_STATUS_TIME_INTERVAL_US       3
44
45 static struct hccs_port_info *kobj_to_port_info(struct kobject *k)
46 {
47         return container_of(k, struct hccs_port_info, kobj);
48 }
49
50 static struct hccs_die_info *kobj_to_die_info(struct kobject *k)
51 {
52         return container_of(k, struct hccs_die_info, kobj);
53 }
54
55 static struct hccs_chip_info *kobj_to_chip_info(struct kobject *k)
56 {
57         return container_of(k, struct hccs_chip_info, kobj);
58 }
59
60 struct hccs_register_ctx {
61         struct device *dev;
62         u8 chan_id;
63         int err;
64 };
65
66 static acpi_status hccs_get_register_cb(struct acpi_resource *ares,
67                                         void *context)
68 {
69         struct acpi_resource_generic_register *reg;
70         struct hccs_register_ctx *ctx = context;
71
72         if (ares->type != ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
73                 return AE_OK;
74
75         reg = &ares->data.generic_reg;
76         if (reg->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) {
77                 dev_err(ctx->dev, "Bad register resource.\n");
78                 ctx->err = -EINVAL;
79                 return AE_ERROR;
80         }
81         ctx->chan_id = reg->access_size;
82
83         return AE_OK;
84 }
85
86 static int hccs_get_pcc_chan_id(struct hccs_dev *hdev)
87 {
88         acpi_handle handle = ACPI_HANDLE(hdev->dev);
89         struct hccs_register_ctx ctx = {0};
90         acpi_status status;
91
92         if (!acpi_has_method(handle, METHOD_NAME__CRS))
93                 return -ENODEV;
94
95         ctx.dev = hdev->dev;
96         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
97                                      hccs_get_register_cb, &ctx);
98         if (ACPI_FAILURE(status))
99                 return ctx.err;
100         hdev->chan_id = ctx.chan_id;
101
102         return 0;
103 }
104
105 static void hccs_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
106 {
107         if (ret < 0)
108                 pr_debug("TX did not complete: CMD sent:0x%x, ret:%d\n",
109                          *(u8 *)msg, ret);
110         else
111                 pr_debug("TX completed. CMD sent:0x%x, ret:%d\n",
112                          *(u8 *)msg, ret);
113 }
114
115 static void hccs_unregister_pcc_channel(struct hccs_dev *hdev)
116 {
117         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
118
119         if (cl_info->pcc_comm_addr)
120                 iounmap(cl_info->pcc_comm_addr);
121         pcc_mbox_free_channel(hdev->cl_info.pcc_chan);
122 }
123
124 static int hccs_register_pcc_channel(struct hccs_dev *hdev)
125 {
126         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
127         struct mbox_client *cl = &cl_info->client;
128         struct pcc_mbox_chan *pcc_chan;
129         struct device *dev = hdev->dev;
130         int rc;
131
132         cl->dev = dev;
133         cl->tx_block = false;
134         cl->knows_txdone = true;
135         cl->tx_done = hccs_chan_tx_done;
136         pcc_chan = pcc_mbox_request_channel(cl, hdev->chan_id);
137         if (IS_ERR(pcc_chan)) {
138                 dev_err(dev, "PPC channel request failed.\n");
139                 rc = -ENODEV;
140                 goto out;
141         }
142         cl_info->pcc_chan = pcc_chan;
143         cl_info->mbox_chan = pcc_chan->mchan;
144
145         /*
146          * pcc_chan->latency is just a nominal value. In reality the remote
147          * processor could be much slower to reply. So add an arbitrary amount
148          * of wait on top of nominal.
149          */
150         cl_info->deadline_us =
151                         HCCS_PCC_CMD_WAIT_RETRIES_NUM * pcc_chan->latency;
152         if (cl_info->mbox_chan->mbox->txdone_irq) {
153                 dev_err(dev, "PCC IRQ in PCCT is enabled.\n");
154                 rc = -EINVAL;
155                 goto err_mbx_channel_free;
156         }
157
158         if (pcc_chan->shmem_base_addr) {
159                 cl_info->pcc_comm_addr = (void __force *)ioremap(
160                         pcc_chan->shmem_base_addr, pcc_chan->shmem_size);
161                 if (!cl_info->pcc_comm_addr) {
162                         dev_err(dev, "Failed to ioremap PCC communication region for channel-%d.\n",
163                                 hdev->chan_id);
164                         rc = -ENOMEM;
165                         goto err_mbx_channel_free;
166                 }
167         }
168
169         return 0;
170
171 err_mbx_channel_free:
172         pcc_mbox_free_channel(cl_info->pcc_chan);
173 out:
174         return rc;
175 }
176
177 static int hccs_check_chan_cmd_complete(struct hccs_dev *hdev)
178 {
179         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
180         struct acpi_pcct_shared_memory *comm_base = cl_info->pcc_comm_addr;
181         u16 status;
182         int ret;
183
184         /*
185          * Poll PCC status register every 3us(delay_us) for maximum of
186          * deadline_us(timeout_us) until PCC command complete bit is set(cond)
187          */
188         ret = readw_poll_timeout(&comm_base->status, status,
189                                  status & HCCS_PCC_STATUS_CMD_COMPLETE,
190                                  HCCS_POLL_STATUS_TIME_INTERVAL_US,
191                                  cl_info->deadline_us);
192         if (unlikely(ret))
193                 dev_err(hdev->dev, "poll PCC status failed, ret = %d.\n", ret);
194
195         return ret;
196 }
197
198 static int hccs_pcc_cmd_send(struct hccs_dev *hdev, u8 cmd,
199                              struct hccs_desc *desc)
200 {
201         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
202         struct acpi_pcct_shared_memory *comm_base = cl_info->pcc_comm_addr;
203         void *comm_space = (void *)(comm_base + 1);
204         struct hccs_fw_inner_head *fw_inner_head;
205         struct acpi_pcct_shared_memory tmp = {0};
206         u16 comm_space_size;
207         int ret;
208
209         /* Write signature for this subspace */
210         tmp.signature = HCCS_PCC_SIGNATURE_MASK | hdev->chan_id;
211         /* Write to the shared command region */
212         tmp.command = cmd;
213         /* Clear cmd complete bit */
214         tmp.status = 0;
215         memcpy_toio(comm_base, (void *)&tmp,
216                         sizeof(struct acpi_pcct_shared_memory));
217
218         /* Copy the message to the PCC comm space */
219         comm_space_size = HCCS_PCC_SHARE_MEM_BYTES -
220                                 sizeof(struct acpi_pcct_shared_memory);
221         memcpy_toio(comm_space, (void *)desc, comm_space_size);
222
223         /* Ring doorbell */
224         ret = mbox_send_message(cl_info->mbox_chan, &cmd);
225         if (ret < 0) {
226                 dev_err(hdev->dev, "Send PCC mbox message failed, ret = %d.\n",
227                         ret);
228                 goto end;
229         }
230
231         /* Wait for completion */
232         ret = hccs_check_chan_cmd_complete(hdev);
233         if (ret)
234                 goto end;
235
236         /* Copy response data */
237         memcpy_fromio((void *)desc, comm_space, comm_space_size);
238         fw_inner_head = &desc->rsp.fw_inner_head;
239         if (fw_inner_head->retStatus) {
240                 dev_err(hdev->dev, "Execute PCC command failed, error code = %u.\n",
241                         fw_inner_head->retStatus);
242                 ret = -EIO;
243         }
244
245 end:
246         mbox_client_txdone(cl_info->mbox_chan, ret);
247         return ret;
248 }
249
250 static void hccs_init_req_desc(struct hccs_desc *desc)
251 {
252         struct hccs_req_desc *req = &desc->req;
253
254         memset(desc, 0, sizeof(*desc));
255         req->req_head.module_code = HCCS_SERDES_MODULE_CODE;
256 }
257
258 static int hccs_get_dev_caps(struct hccs_dev *hdev)
259 {
260         struct hccs_desc desc;
261         int ret;
262
263         hccs_init_req_desc(&desc);
264         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DEV_CAP, &desc);
265         if (ret) {
266                 dev_err(hdev->dev, "Get device capabilities failed, ret = %d.\n",
267                         ret);
268                 return ret;
269         }
270         memcpy(&hdev->caps, desc.rsp.data, sizeof(hdev->caps));
271
272         return 0;
273 }
274
275 static int hccs_query_chip_num_on_platform(struct hccs_dev *hdev)
276 {
277         struct hccs_desc desc;
278         int ret;
279
280         hccs_init_req_desc(&desc);
281         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_CHIP_NUM, &desc);
282         if (ret) {
283                 dev_err(hdev->dev, "query system chip number failed, ret = %d.\n",
284                         ret);
285                 return ret;
286         }
287
288         hdev->chip_num = *((u8 *)&desc.rsp.data);
289         if (!hdev->chip_num) {
290                 dev_err(hdev->dev, "chip num obtained from firmware is zero.\n");
291                 return -EINVAL;
292         }
293
294         return 0;
295 }
296
297 static int hccs_get_chip_info(struct hccs_dev *hdev,
298                               struct hccs_chip_info *chip)
299 {
300         struct hccs_die_num_req_param *req_param;
301         struct hccs_desc desc;
302         int ret;
303
304         hccs_init_req_desc(&desc);
305         req_param = (struct hccs_die_num_req_param *)desc.req.data;
306         req_param->chip_id = chip->chip_id;
307         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_NUM, &desc);
308         if (ret)
309                 return ret;
310
311         chip->die_num = *((u8 *)&desc.rsp.data);
312
313         return 0;
314 }
315
316 static int hccs_query_chip_info_on_platform(struct hccs_dev *hdev)
317 {
318         struct hccs_chip_info *chip;
319         int ret;
320         u8 idx;
321
322         ret = hccs_query_chip_num_on_platform(hdev);
323         if (ret) {
324                 dev_err(hdev->dev, "query chip number on platform failed, ret = %d.\n",
325                         ret);
326                 return ret;
327         }
328
329         hdev->chips = devm_kzalloc(hdev->dev,
330                                 hdev->chip_num * sizeof(struct hccs_chip_info),
331                                 GFP_KERNEL);
332         if (!hdev->chips) {
333                 dev_err(hdev->dev, "allocate all chips memory failed.\n");
334                 return -ENOMEM;
335         }
336
337         for (idx = 0; idx < hdev->chip_num; idx++) {
338                 chip = &hdev->chips[idx];
339                 chip->chip_id = idx;
340                 ret = hccs_get_chip_info(hdev, chip);
341                 if (ret) {
342                         dev_err(hdev->dev, "get chip%u info failed, ret = %d.\n",
343                                 idx, ret);
344                         return ret;
345                 }
346                 chip->hdev = hdev;
347         }
348
349         return 0;
350 }
351
352 static int hccs_query_die_info_on_chip(struct hccs_dev *hdev, u8 chip_id,
353                                        u8 die_idx, struct hccs_die_info *die)
354 {
355         struct hccs_die_info_req_param *req_param;
356         struct hccs_die_info_rsp_data *rsp_data;
357         struct hccs_desc desc;
358         int ret;
359
360         hccs_init_req_desc(&desc);
361         req_param = (struct hccs_die_info_req_param *)desc.req.data;
362         req_param->chip_id = chip_id;
363         req_param->die_idx = die_idx;
364         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_INFO, &desc);
365         if (ret)
366                 return ret;
367
368         rsp_data = (struct hccs_die_info_rsp_data *)desc.rsp.data;
369         die->die_id = rsp_data->die_id;
370         die->port_num = rsp_data->port_num;
371         die->min_port_id = rsp_data->min_port_id;
372         die->max_port_id = rsp_data->max_port_id;
373         if (die->min_port_id > die->max_port_id) {
374                 dev_err(hdev->dev, "min port id(%u) > max port id(%u) on die_idx(%u).\n",
375                         die->min_port_id, die->max_port_id, die_idx);
376                 return -EINVAL;
377         }
378         if (die->max_port_id > HCCS_DIE_MAX_PORT_ID) {
379                 dev_err(hdev->dev, "max port id(%u) on die_idx(%u) is too big.\n",
380                         die->max_port_id, die_idx);
381                 return -EINVAL;
382         }
383
384         return 0;
385 }
386
387 static int hccs_query_all_die_info_on_platform(struct hccs_dev *hdev)
388 {
389         struct device *dev = hdev->dev;
390         struct hccs_chip_info *chip;
391         struct hccs_die_info *die;
392         u8 i, j;
393         int ret;
394
395         for (i = 0; i < hdev->chip_num; i++) {
396                 chip = &hdev->chips[i];
397                 if (!chip->die_num)
398                         continue;
399
400                 chip->dies = devm_kzalloc(hdev->dev,
401                                 chip->die_num * sizeof(struct hccs_die_info),
402                                 GFP_KERNEL);
403                 if (!chip->dies) {
404                         dev_err(dev, "allocate all dies memory on chip%u failed.\n",
405                                 i);
406                         return -ENOMEM;
407                 }
408
409                 for (j = 0; j < chip->die_num; j++) {
410                         die = &chip->dies[j];
411                         ret = hccs_query_die_info_on_chip(hdev, i, j, die);
412                         if (ret) {
413                                 dev_err(dev, "get die idx (%u) info on chip%u failed, ret = %d.\n",
414                                         j, i, ret);
415                                 return ret;
416                         }
417                         die->chip = chip;
418                 }
419         }
420
421         return 0;
422 }
423
424 static int hccs_get_bd_info(struct hccs_dev *hdev, u8 opcode,
425                             struct hccs_desc *desc,
426                             void *buf, size_t buf_len,
427                             struct hccs_rsp_head *rsp_head)
428 {
429         struct hccs_rsp_head *head;
430         struct hccs_rsp_desc *rsp;
431         int ret;
432
433         ret = hccs_pcc_cmd_send(hdev, opcode, desc);
434         if (ret)
435                 return ret;
436
437         rsp = &desc->rsp;
438         head = &rsp->rsp_head;
439         if (head->data_len > buf_len) {
440                 dev_err(hdev->dev,
441                         "buffer overflow (buf_len = %zu, data_len = %u)!\n",
442                         buf_len, head->data_len);
443                 return -ENOMEM;
444         }
445
446         memcpy(buf, rsp->data, head->data_len);
447         *rsp_head = *head;
448
449         return 0;
450 }
451
452 static int hccs_get_all_port_attr(struct hccs_dev *hdev,
453                                   struct hccs_die_info *die,
454                                   struct hccs_port_attr *attrs, u16 size)
455 {
456         struct hccs_die_comm_req_param *req_param;
457         struct hccs_req_head *req_head;
458         struct hccs_rsp_head rsp_head;
459         struct hccs_desc desc;
460         size_t left_buf_len;
461         u32 data_len = 0;
462         u8 start_id;
463         u8 *buf;
464         int ret;
465
466         buf = (u8 *)attrs;
467         left_buf_len = sizeof(struct hccs_port_attr) * size;
468         start_id = die->min_port_id;
469         while (start_id <= die->max_port_id) {
470                 hccs_init_req_desc(&desc);
471                 req_head = &desc.req.req_head;
472                 req_head->start_id = start_id;
473                 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
474                 req_param->chip_id = die->chip->chip_id;
475                 req_param->die_id = die->die_id;
476
477                 ret = hccs_get_bd_info(hdev, HCCS_GET_DIE_PORT_INFO, &desc,
478                                        buf + data_len, left_buf_len, &rsp_head);
479                 if (ret) {
480                         dev_err(hdev->dev,
481                                 "get the information of port%u on die%u failed, ret = %d.\n",
482                                 start_id, die->die_id, ret);
483                         return ret;
484                 }
485
486                 data_len += rsp_head.data_len;
487                 left_buf_len -= rsp_head.data_len;
488                 if (unlikely(rsp_head.next_id <= start_id)) {
489                         dev_err(hdev->dev,
490                                 "next port id (%u) is not greater than last start id (%u) on die%u.\n",
491                                 rsp_head.next_id, start_id, die->die_id);
492                         return -EINVAL;
493                 }
494                 start_id = rsp_head.next_id;
495         }
496
497         return 0;
498 }
499
500 static int hccs_get_all_port_info_on_die(struct hccs_dev *hdev,
501                                          struct hccs_die_info *die)
502 {
503         struct hccs_port_attr *attrs;
504         struct hccs_port_info *port;
505         int ret;
506         u8 i;
507
508         attrs = kcalloc(die->port_num, sizeof(struct hccs_port_attr),
509                         GFP_KERNEL);
510         if (!attrs)
511                 return -ENOMEM;
512
513         ret = hccs_get_all_port_attr(hdev, die, attrs, die->port_num);
514         if (ret)
515                 goto out;
516
517         for (i = 0; i < die->port_num; i++) {
518                 port = &die->ports[i];
519                 port->port_id = attrs[i].port_id;
520                 port->port_type = attrs[i].port_type;
521                 port->lane_mode = attrs[i].lane_mode;
522                 port->enable = attrs[i].enable;
523                 port->die = die;
524         }
525
526 out:
527         kfree(attrs);
528         return ret;
529 }
530
531 static int hccs_query_all_port_info_on_platform(struct hccs_dev *hdev)
532 {
533
534         struct device *dev = hdev->dev;
535         struct hccs_chip_info *chip;
536         struct hccs_die_info *die;
537         u8 i, j;
538         int ret;
539
540         for (i = 0; i < hdev->chip_num; i++) {
541                 chip = &hdev->chips[i];
542                 for (j = 0; j < chip->die_num; j++) {
543                         die = &chip->dies[j];
544                         if (!die->port_num)
545                                 continue;
546
547                         die->ports = devm_kzalloc(dev,
548                                 die->port_num * sizeof(struct hccs_port_info),
549                                 GFP_KERNEL);
550                         if (!die->ports) {
551                                 dev_err(dev, "allocate ports memory on chip%u/die%u failed.\n",
552                                         i, die->die_id);
553                                 return -ENOMEM;
554                         }
555
556                         ret = hccs_get_all_port_info_on_die(hdev, die);
557                         if (ret) {
558                                 dev_err(dev, "get all port info on chip%u/die%u failed, ret = %d.\n",
559                                         i, die->die_id, ret);
560                                 return ret;
561                         }
562                 }
563         }
564
565         return 0;
566 }
567
568 static int hccs_get_hw_info(struct hccs_dev *hdev)
569 {
570         int ret;
571
572         ret = hccs_query_chip_info_on_platform(hdev);
573         if (ret) {
574                 dev_err(hdev->dev, "query chip info on platform failed, ret = %d.\n",
575                         ret);
576                 return ret;
577         }
578
579         ret = hccs_query_all_die_info_on_platform(hdev);
580         if (ret) {
581                 dev_err(hdev->dev, "query all die info on platform failed, ret = %d.\n",
582                         ret);
583                 return ret;
584         }
585
586         ret = hccs_query_all_port_info_on_platform(hdev);
587         if (ret) {
588                 dev_err(hdev->dev, "query all port info on platform failed, ret = %d.\n",
589                         ret);
590                 return ret;
591         }
592
593         return 0;
594 }
595
596 static int hccs_query_port_link_status(struct hccs_dev *hdev,
597                                        const struct hccs_port_info *port,
598                                        struct hccs_link_status *link_status)
599 {
600         const struct hccs_die_info *die = port->die;
601         const struct hccs_chip_info *chip = die->chip;
602         struct hccs_port_comm_req_param *req_param;
603         struct hccs_desc desc;
604         int ret;
605
606         hccs_init_req_desc(&desc);
607         req_param = (struct hccs_port_comm_req_param *)desc.req.data;
608         req_param->chip_id = chip->chip_id;
609         req_param->die_id = die->die_id;
610         req_param->port_id = port->port_id;
611         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_LINK_STATUS, &desc);
612         if (ret) {
613                 dev_err(hdev->dev,
614                         "get port link status info failed, ret = %d.\n", ret);
615                 return ret;
616         }
617
618         *link_status = *((struct hccs_link_status *)desc.rsp.data);
619
620         return 0;
621 }
622
623 static int hccs_query_port_crc_err_cnt(struct hccs_dev *hdev,
624                                        const struct hccs_port_info *port,
625                                        u64 *crc_err_cnt)
626 {
627         const struct hccs_die_info *die = port->die;
628         const struct hccs_chip_info *chip = die->chip;
629         struct hccs_port_comm_req_param *req_param;
630         struct hccs_desc desc;
631         int ret;
632
633         hccs_init_req_desc(&desc);
634         req_param = (struct hccs_port_comm_req_param *)desc.req.data;
635         req_param->chip_id = chip->chip_id;
636         req_param->die_id = die->die_id;
637         req_param->port_id = port->port_id;
638         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_CRC_ERR_CNT, &desc);
639         if (ret) {
640                 dev_err(hdev->dev,
641                         "get port crc error count failed, ret = %d.\n", ret);
642                 return ret;
643         }
644
645         memcpy(crc_err_cnt, &desc.rsp.data, sizeof(u64));
646
647         return 0;
648 }
649
650 static int hccs_get_die_all_link_status(struct hccs_dev *hdev,
651                                         const struct hccs_die_info *die,
652                                         u8 *all_linked)
653 {
654         struct hccs_die_comm_req_param *req_param;
655         struct hccs_desc desc;
656         int ret;
657
658         if (die->port_num == 0) {
659                 *all_linked = 1;
660                 return 0;
661         }
662
663         hccs_init_req_desc(&desc);
664         req_param = (struct hccs_die_comm_req_param *)desc.req.data;
665         req_param->chip_id = die->chip->chip_id;
666         req_param->die_id = die->die_id;
667         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LINK_STA, &desc);
668         if (ret) {
669                 dev_err(hdev->dev,
670                         "get link status of all ports failed on die%u, ret = %d.\n",
671                         die->die_id, ret);
672                 return ret;
673         }
674
675         *all_linked = *((u8 *)&desc.rsp.data);
676
677         return 0;
678 }
679
680 static int hccs_get_die_all_port_lane_status(struct hccs_dev *hdev,
681                                              const struct hccs_die_info *die,
682                                              u8 *full_lane)
683 {
684         struct hccs_die_comm_req_param *req_param;
685         struct hccs_desc desc;
686         int ret;
687
688         if (die->port_num == 0) {
689                 *full_lane = 1;
690                 return 0;
691         }
692
693         hccs_init_req_desc(&desc);
694         req_param = (struct hccs_die_comm_req_param *)desc.req.data;
695         req_param->chip_id = die->chip->chip_id;
696         req_param->die_id = die->die_id;
697         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LANE_STA, &desc);
698         if (ret) {
699                 dev_err(hdev->dev, "get lane status of all ports failed on die%u, ret = %d.\n",
700                         die->die_id, ret);
701                 return ret;
702         }
703
704         *full_lane = *((u8 *)&desc.rsp.data);
705
706         return 0;
707 }
708
709 static int hccs_get_die_total_crc_err_cnt(struct hccs_dev *hdev,
710                                           const struct hccs_die_info *die,
711                                           u64 *total_crc_err_cnt)
712 {
713         struct hccs_die_comm_req_param *req_param;
714         struct hccs_desc desc;
715         int ret;
716
717         if (die->port_num == 0) {
718                 *total_crc_err_cnt = 0;
719                 return 0;
720         }
721
722         hccs_init_req_desc(&desc);
723         req_param = (struct hccs_die_comm_req_param *)desc.req.data;
724         req_param->chip_id = die->chip->chip_id;
725         req_param->die_id = die->die_id;
726         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_CRC_ERR_CNT, &desc);
727         if (ret) {
728                 dev_err(hdev->dev, "get crc error count sum failed on die%u, ret = %d.\n",
729                         die->die_id, ret);
730                 return ret;
731         }
732
733         memcpy(total_crc_err_cnt, &desc.rsp.data, sizeof(u64));
734
735         return 0;
736 }
737
738 static ssize_t hccs_show(struct kobject *k, struct attribute *attr, char *buf)
739 {
740         struct kobj_attribute *kobj_attr;
741
742         kobj_attr = container_of(attr, struct kobj_attribute, attr);
743
744         return kobj_attr->show(k, kobj_attr, buf);
745 }
746
747 static const struct sysfs_ops hccs_comm_ops = {
748         .show = hccs_show,
749 };
750
751 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
752                          char *buf)
753 {
754         const struct hccs_port_info *port = kobj_to_port_info(kobj);
755
756         return sysfs_emit(buf, "HCCS-v%u\n", port->port_type);
757 }
758 static struct kobj_attribute hccs_type_attr = __ATTR_RO(type);
759
760 static ssize_t lane_mode_show(struct kobject *kobj, struct kobj_attribute *attr,
761                               char *buf)
762 {
763         const struct hccs_port_info *port = kobj_to_port_info(kobj);
764
765         return sysfs_emit(buf, "x%u\n", port->lane_mode);
766 }
767 static struct kobj_attribute lane_mode_attr = __ATTR_RO(lane_mode);
768
769 static ssize_t enable_show(struct kobject *kobj,
770                                  struct kobj_attribute *attr, char *buf)
771 {
772         const struct hccs_port_info *port = kobj_to_port_info(kobj);
773
774         return sysfs_emit(buf, "%u\n", port->enable);
775 }
776 static struct kobj_attribute port_enable_attr = __ATTR_RO(enable);
777
778 static ssize_t cur_lane_num_show(struct kobject *kobj,
779                                  struct kobj_attribute *attr, char *buf)
780 {
781         const struct hccs_port_info *port = kobj_to_port_info(kobj);
782         struct hccs_dev *hdev = port->die->chip->hdev;
783         struct hccs_link_status link_status = {0};
784         int ret;
785
786         mutex_lock(&hdev->lock);
787         ret = hccs_query_port_link_status(hdev, port, &link_status);
788         mutex_unlock(&hdev->lock);
789         if (ret)
790                 return ret;
791
792         return sysfs_emit(buf, "%u\n", link_status.lane_num);
793 }
794 static struct kobj_attribute cur_lane_num_attr = __ATTR_RO(cur_lane_num);
795
796 static ssize_t link_fsm_show(struct kobject *kobj,
797                              struct kobj_attribute *attr, char *buf)
798 {
799         const struct hccs_port_info *port = kobj_to_port_info(kobj);
800         struct hccs_dev *hdev = port->die->chip->hdev;
801         struct hccs_link_status link_status = {0};
802         const struct {
803                 u8 link_fsm;
804                 char *str;
805         } link_fsm_map[] = {
806                 {HCCS_PORT_RESET, "reset"},
807                 {HCCS_PORT_SETUP, "setup"},
808                 {HCCS_PORT_CONFIG, "config"},
809                 {HCCS_PORT_READY, "link-up"},
810         };
811         const char *link_fsm_str = "unknown";
812         size_t i;
813         int ret;
814
815         mutex_lock(&hdev->lock);
816         ret = hccs_query_port_link_status(hdev, port, &link_status);
817         mutex_unlock(&hdev->lock);
818         if (ret)
819                 return ret;
820
821         for (i = 0; i < ARRAY_SIZE(link_fsm_map); i++) {
822                 if (link_fsm_map[i].link_fsm == link_status.link_fsm) {
823                         link_fsm_str = link_fsm_map[i].str;
824                         break;
825                 }
826         }
827
828         return sysfs_emit(buf, "%s\n", link_fsm_str);
829 }
830 static struct kobj_attribute link_fsm_attr = __ATTR_RO(link_fsm);
831
832 static ssize_t lane_mask_show(struct kobject *kobj,
833                               struct kobj_attribute *attr, char *buf)
834 {
835         const struct hccs_port_info *port = kobj_to_port_info(kobj);
836         struct hccs_dev *hdev = port->die->chip->hdev;
837         struct hccs_link_status link_status = {0};
838         int ret;
839
840         mutex_lock(&hdev->lock);
841         ret = hccs_query_port_link_status(hdev, port, &link_status);
842         mutex_unlock(&hdev->lock);
843         if (ret)
844                 return ret;
845
846         return sysfs_emit(buf, "0x%x\n", link_status.lane_mask);
847 }
848 static struct kobj_attribute lane_mask_attr = __ATTR_RO(lane_mask);
849
850 static ssize_t crc_err_cnt_show(struct kobject *kobj,
851                                 struct kobj_attribute *attr, char *buf)
852 {
853         const struct hccs_port_info *port = kobj_to_port_info(kobj);
854         struct hccs_dev *hdev = port->die->chip->hdev;
855         u64 crc_err_cnt;
856         int ret;
857
858         mutex_lock(&hdev->lock);
859         ret = hccs_query_port_crc_err_cnt(hdev, port, &crc_err_cnt);
860         mutex_unlock(&hdev->lock);
861         if (ret)
862                 return ret;
863
864         return sysfs_emit(buf, "%llu\n", crc_err_cnt);
865 }
866 static struct kobj_attribute crc_err_cnt_attr = __ATTR_RO(crc_err_cnt);
867
868 static struct attribute *hccs_port_default_attrs[] = {
869         &hccs_type_attr.attr,
870         &lane_mode_attr.attr,
871         &port_enable_attr.attr,
872         &cur_lane_num_attr.attr,
873         &link_fsm_attr.attr,
874         &lane_mask_attr.attr,
875         &crc_err_cnt_attr.attr,
876         NULL,
877 };
878 ATTRIBUTE_GROUPS(hccs_port_default);
879
880 static const struct kobj_type hccs_port_type = {
881         .sysfs_ops = &hccs_comm_ops,
882         .default_groups = hccs_port_default_groups,
883 };
884
885 static ssize_t all_linked_on_die_show(struct kobject *kobj,
886                                       struct kobj_attribute *attr, char *buf)
887 {
888         const struct hccs_die_info *die = kobj_to_die_info(kobj);
889         struct hccs_dev *hdev = die->chip->hdev;
890         u8 all_linked;
891         int ret;
892
893         mutex_lock(&hdev->lock);
894         ret = hccs_get_die_all_link_status(hdev, die, &all_linked);
895         mutex_unlock(&hdev->lock);
896         if (ret)
897                 return ret;
898
899         return sysfs_emit(buf, "%u\n", all_linked);
900 }
901 static struct kobj_attribute all_linked_on_die_attr =
902                 __ATTR(all_linked, 0444, all_linked_on_die_show, NULL);
903
904 static ssize_t linked_full_lane_on_die_show(struct kobject *kobj,
905                                             struct kobj_attribute *attr,
906                                             char *buf)
907 {
908         const struct hccs_die_info *die = kobj_to_die_info(kobj);
909         struct hccs_dev *hdev = die->chip->hdev;
910         u8 full_lane;
911         int ret;
912
913         mutex_lock(&hdev->lock);
914         ret = hccs_get_die_all_port_lane_status(hdev, die, &full_lane);
915         mutex_unlock(&hdev->lock);
916         if (ret)
917                 return ret;
918
919         return sysfs_emit(buf, "%u\n", full_lane);
920 }
921 static struct kobj_attribute linked_full_lane_on_die_attr =
922         __ATTR(linked_full_lane, 0444, linked_full_lane_on_die_show, NULL);
923
924 static ssize_t crc_err_cnt_sum_on_die_show(struct kobject *kobj,
925                                            struct kobj_attribute *attr,
926                                            char *buf)
927 {
928         const struct hccs_die_info *die = kobj_to_die_info(kobj);
929         struct hccs_dev *hdev = die->chip->hdev;
930         u64 total_crc_err_cnt;
931         int ret;
932
933         mutex_lock(&hdev->lock);
934         ret = hccs_get_die_total_crc_err_cnt(hdev, die, &total_crc_err_cnt);
935         mutex_unlock(&hdev->lock);
936         if (ret)
937                 return ret;
938
939         return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
940 }
941 static struct kobj_attribute crc_err_cnt_sum_on_die_attr =
942         __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_die_show, NULL);
943
944 static struct attribute *hccs_die_default_attrs[] = {
945         &all_linked_on_die_attr.attr,
946         &linked_full_lane_on_die_attr.attr,
947         &crc_err_cnt_sum_on_die_attr.attr,
948         NULL,
949 };
950 ATTRIBUTE_GROUPS(hccs_die_default);
951
952 static const struct kobj_type hccs_die_type = {
953         .sysfs_ops = &hccs_comm_ops,
954         .default_groups = hccs_die_default_groups,
955 };
956
957 static ssize_t all_linked_on_chip_show(struct kobject *kobj,
958                                        struct kobj_attribute *attr, char *buf)
959 {
960         const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
961         struct hccs_dev *hdev = chip->hdev;
962         const struct hccs_die_info *die;
963         u8 all_linked = 1;
964         u8 i, tmp;
965         int ret;
966
967         mutex_lock(&hdev->lock);
968         for (i = 0; i < chip->die_num; i++) {
969                 die = &chip->dies[i];
970                 ret = hccs_get_die_all_link_status(hdev, die, &tmp);
971                 if (ret) {
972                         mutex_unlock(&hdev->lock);
973                         return ret;
974                 }
975                 if (tmp != all_linked) {
976                         all_linked = 0;
977                         break;
978                 }
979         }
980         mutex_unlock(&hdev->lock);
981
982         return sysfs_emit(buf, "%u\n", all_linked);
983 }
984 static struct kobj_attribute all_linked_on_chip_attr =
985                 __ATTR(all_linked, 0444, all_linked_on_chip_show, NULL);
986
987 static ssize_t linked_full_lane_on_chip_show(struct kobject *kobj,
988                                              struct kobj_attribute *attr,
989                                              char *buf)
990 {
991         const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
992         struct hccs_dev *hdev = chip->hdev;
993         const struct hccs_die_info *die;
994         u8 full_lane = 1;
995         u8 i, tmp;
996         int ret;
997
998         mutex_lock(&hdev->lock);
999         for (i = 0; i < chip->die_num; i++) {
1000                 die = &chip->dies[i];
1001                 ret = hccs_get_die_all_port_lane_status(hdev, die, &tmp);
1002                 if (ret) {
1003                         mutex_unlock(&hdev->lock);
1004                         return ret;
1005                 }
1006                 if (tmp != full_lane) {
1007                         full_lane = 0;
1008                         break;
1009                 }
1010         }
1011         mutex_unlock(&hdev->lock);
1012
1013         return sysfs_emit(buf, "%u\n", full_lane);
1014 }
1015 static struct kobj_attribute linked_full_lane_on_chip_attr =
1016         __ATTR(linked_full_lane, 0444, linked_full_lane_on_chip_show, NULL);
1017
1018 static ssize_t crc_err_cnt_sum_on_chip_show(struct kobject *kobj,
1019                                             struct kobj_attribute *attr,
1020                                             char *buf)
1021 {
1022         const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1023         u64 crc_err_cnt, total_crc_err_cnt = 0;
1024         struct hccs_dev *hdev = chip->hdev;
1025         const struct hccs_die_info *die;
1026         int ret;
1027         u16 i;
1028
1029         mutex_lock(&hdev->lock);
1030         for (i = 0; i < chip->die_num; i++) {
1031                 die = &chip->dies[i];
1032                 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &crc_err_cnt);
1033                 if (ret) {
1034                         mutex_unlock(&hdev->lock);
1035                         return ret;
1036                 }
1037
1038                 total_crc_err_cnt += crc_err_cnt;
1039         }
1040         mutex_unlock(&hdev->lock);
1041
1042         return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
1043 }
1044 static struct kobj_attribute crc_err_cnt_sum_on_chip_attr =
1045                 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_chip_show, NULL);
1046
1047 static struct attribute *hccs_chip_default_attrs[] = {
1048         &all_linked_on_chip_attr.attr,
1049         &linked_full_lane_on_chip_attr.attr,
1050         &crc_err_cnt_sum_on_chip_attr.attr,
1051         NULL,
1052 };
1053 ATTRIBUTE_GROUPS(hccs_chip_default);
1054
1055 static const struct kobj_type hccs_chip_type = {
1056         .sysfs_ops = &hccs_comm_ops,
1057         .default_groups = hccs_chip_default_groups,
1058 };
1059
1060 static void hccs_remove_die_dir(struct hccs_die_info *die)
1061 {
1062         struct hccs_port_info *port;
1063         u8 i;
1064
1065         for (i = 0; i < die->port_num; i++) {
1066                 port = &die->ports[i];
1067                 if (port->dir_created)
1068                         kobject_put(&port->kobj);
1069         }
1070
1071         kobject_put(&die->kobj);
1072 }
1073
1074 static void hccs_remove_chip_dir(struct hccs_chip_info *chip)
1075 {
1076         struct hccs_die_info *die;
1077         u8 i;
1078
1079         for (i = 0; i < chip->die_num; i++) {
1080                 die = &chip->dies[i];
1081                 if (die->dir_created)
1082                         hccs_remove_die_dir(die);
1083         }
1084
1085         kobject_put(&chip->kobj);
1086 }
1087
1088 static void hccs_remove_topo_dirs(struct hccs_dev *hdev)
1089 {
1090         u8 i;
1091
1092         for (i = 0; i < hdev->chip_num; i++)
1093                 hccs_remove_chip_dir(&hdev->chips[i]);
1094 }
1095
1096 static int hccs_create_hccs_dir(struct hccs_dev *hdev,
1097                                 struct hccs_die_info *die,
1098                                 struct hccs_port_info *port)
1099 {
1100         int ret;
1101
1102         ret = kobject_init_and_add(&port->kobj, &hccs_port_type,
1103                                    &die->kobj, "hccs%d", port->port_id);
1104         if (ret) {
1105                 kobject_put(&port->kobj);
1106                 return ret;
1107         }
1108
1109         return 0;
1110 }
1111
1112 static int hccs_create_die_dir(struct hccs_dev *hdev,
1113                                struct hccs_chip_info *chip,
1114                                struct hccs_die_info *die)
1115 {
1116         struct hccs_port_info *port;
1117         int ret;
1118         u16 i;
1119
1120         ret = kobject_init_and_add(&die->kobj, &hccs_die_type,
1121                                    &chip->kobj, "die%d", die->die_id);
1122         if (ret) {
1123                 kobject_put(&die->kobj);
1124                 return ret;
1125         }
1126
1127         for (i = 0; i < die->port_num; i++) {
1128                 port = &die->ports[i];
1129                 ret = hccs_create_hccs_dir(hdev, die, port);
1130                 if (ret) {
1131                         dev_err(hdev->dev, "create hccs%d dir failed.\n",
1132                                 port->port_id);
1133                         goto err;
1134                 }
1135                 port->dir_created = true;
1136         }
1137
1138         return 0;
1139 err:
1140         hccs_remove_die_dir(die);
1141
1142         return ret;
1143 }
1144
1145 static int hccs_create_chip_dir(struct hccs_dev *hdev,
1146                                 struct hccs_chip_info *chip)
1147 {
1148         struct hccs_die_info *die;
1149         int ret;
1150         u16 id;
1151
1152         ret = kobject_init_and_add(&chip->kobj, &hccs_chip_type,
1153                                    &hdev->dev->kobj, "chip%d", chip->chip_id);
1154         if (ret) {
1155                 kobject_put(&chip->kobj);
1156                 return ret;
1157         }
1158
1159         for (id = 0; id < chip->die_num; id++) {
1160                 die = &chip->dies[id];
1161                 ret = hccs_create_die_dir(hdev, chip, die);
1162                 if (ret)
1163                         goto err;
1164                 die->dir_created = true;
1165         }
1166
1167         return 0;
1168 err:
1169         hccs_remove_chip_dir(chip);
1170
1171         return ret;
1172 }
1173
1174 static int hccs_create_topo_dirs(struct hccs_dev *hdev)
1175 {
1176         struct hccs_chip_info *chip;
1177         u8 id, k;
1178         int ret;
1179
1180         for (id = 0; id < hdev->chip_num; id++) {
1181                 chip = &hdev->chips[id];
1182                 ret = hccs_create_chip_dir(hdev, chip);
1183                 if (ret) {
1184                         dev_err(hdev->dev, "init chip%d dir failed!\n", id);
1185                         goto err;
1186                 }
1187         }
1188
1189         return 0;
1190 err:
1191         for (k = 0; k < id; k++)
1192                 hccs_remove_chip_dir(&hdev->chips[k]);
1193
1194         return ret;
1195 }
1196
1197 static int hccs_probe(struct platform_device *pdev)
1198 {
1199         struct acpi_device *acpi_dev;
1200         struct hccs_dev *hdev;
1201         int rc;
1202
1203         if (acpi_disabled) {
1204                 dev_err(&pdev->dev, "acpi is disabled.\n");
1205                 return -ENODEV;
1206         }
1207         acpi_dev = ACPI_COMPANION(&pdev->dev);
1208         if (!acpi_dev)
1209                 return -ENODEV;
1210
1211         hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
1212         if (!hdev)
1213                 return -ENOMEM;
1214         hdev->acpi_dev = acpi_dev;
1215         hdev->dev = &pdev->dev;
1216         platform_set_drvdata(pdev, hdev);
1217
1218         mutex_init(&hdev->lock);
1219         rc = hccs_get_pcc_chan_id(hdev);
1220         if (rc)
1221                 return rc;
1222         rc = hccs_register_pcc_channel(hdev);
1223         if (rc)
1224                 return rc;
1225
1226         rc = hccs_get_dev_caps(hdev);
1227         if (rc)
1228                 goto unregister_pcc_chan;
1229
1230         rc = hccs_get_hw_info(hdev);
1231         if (rc)
1232                 goto unregister_pcc_chan;
1233
1234         rc = hccs_create_topo_dirs(hdev);
1235         if (rc)
1236                 goto unregister_pcc_chan;
1237
1238         return 0;
1239
1240 unregister_pcc_chan:
1241         hccs_unregister_pcc_channel(hdev);
1242
1243         return rc;
1244 }
1245
1246 static int hccs_remove(struct platform_device *pdev)
1247 {
1248         struct hccs_dev *hdev = platform_get_drvdata(pdev);
1249
1250         hccs_remove_topo_dirs(hdev);
1251         hccs_unregister_pcc_channel(hdev);
1252
1253         return 0;
1254 }
1255
1256 static const struct acpi_device_id hccs_acpi_match[] = {
1257         { "HISI04B1"},
1258         { ""},
1259 };
1260 MODULE_DEVICE_TABLE(acpi, hccs_acpi_match);
1261
1262 static struct platform_driver hccs_driver = {
1263         .probe = hccs_probe,
1264         .remove = hccs_remove,
1265         .driver = {
1266                 .name = "kunpeng_hccs",
1267                 .acpi_match_table = hccs_acpi_match,
1268         },
1269 };
1270
1271 module_platform_driver(hccs_driver);
1272
1273 MODULE_DESCRIPTION("Kunpeng SoC HCCS driver");
1274 MODULE_LICENSE("GPL");
1275 MODULE_AUTHOR("Huisong Li <lihuisong@huawei.com>");