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