18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * drivers/hyperhold/hp_iotab.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Technologies Co., Ltd.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "[HYPERHOLD]" fmt
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/mm.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "hp_iotab.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciatomic64_t hpio_mem = ATOMIC64_INIT(0);
168c2ecf20Sopenharmony_ciu64 hpio_memory(void)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	return atomic64_read(&hpio_mem);
198c2ecf20Sopenharmony_ci}
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct hp_iotab {
228c2ecf20Sopenharmony_ci	struct list_head io_list;
238c2ecf20Sopenharmony_ci	rwlock_t lock;
248c2ecf20Sopenharmony_ci	u32 io_cnt;
258c2ecf20Sopenharmony_ci	wait_queue_head_t empty_wq;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* store all inflight hpio in iotab */
298c2ecf20Sopenharmony_cistruct hp_iotab iotab = {
308c2ecf20Sopenharmony_ci	.io_list = LIST_HEAD_INIT(iotab.io_list),
318c2ecf20Sopenharmony_ci	.lock = __RW_LOCK_UNLOCKED(iotab.lock),
328c2ecf20Sopenharmony_ci	.io_cnt = 0,
338c2ecf20Sopenharmony_ci	.empty_wq = __WAIT_QUEUE_HEAD_INITIALIZER(iotab.empty_wq),
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic struct hpio *__iotab_search_get(struct hp_iotab *iotab, u32 eid)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct hpio *hpio = NULL;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	list_for_each_entry(hpio, &iotab->io_list, list)
418c2ecf20Sopenharmony_ci		if (hpio->eid == eid && kref_get_unless_zero(&hpio->refcnt))
428c2ecf20Sopenharmony_ci			return hpio;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return NULL;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic struct hpio *iotab_search_get(struct hp_iotab *iotab, u32 eid)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct hpio *hpio = NULL;
508c2ecf20Sopenharmony_ci	unsigned long flags;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	read_lock_irqsave(&iotab->lock, flags);
538c2ecf20Sopenharmony_ci	hpio = __iotab_search_get(iotab, eid);
548c2ecf20Sopenharmony_ci	read_unlock_irqrestore(&iotab->lock, flags);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	pr_info("find hpio %p for eid %u.\n", hpio, eid);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return hpio;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * insert @hpio into @iotab, cancel insertion if there is a hpio of the same
638c2ecf20Sopenharmony_ci * @eid, inc the refcnt of duplicated hpio and return it
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_cistatic struct hpio *iotab_insert(struct hp_iotab *iotab, struct hpio *hpio)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct hpio *dup = NULL;
688c2ecf20Sopenharmony_ci	unsigned long flags;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	write_lock_irqsave(&iotab->lock, flags);
718c2ecf20Sopenharmony_ci	dup = __iotab_search_get(iotab, hpio->eid);
728c2ecf20Sopenharmony_ci	if (dup) {
738c2ecf20Sopenharmony_ci		pr_info("find exist hpio %p for eid %u, insert hpio %p failed.\n",
748c2ecf20Sopenharmony_ci				dup, hpio->eid, hpio);
758c2ecf20Sopenharmony_ci		goto unlock;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci	list_add(&hpio->list, &iotab->io_list);
788c2ecf20Sopenharmony_ci	iotab->io_cnt++;
798c2ecf20Sopenharmony_ci	pr_info("insert new hpio %p for eid %u.\n", hpio, hpio->eid);
808c2ecf20Sopenharmony_ciunlock:
818c2ecf20Sopenharmony_ci	write_unlock_irqrestore(&iotab->lock, flags);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return dup;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic void iotab_delete(struct hp_iotab *iotab, struct hpio *hpio)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	unsigned long flags;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	write_lock_irqsave(&iotab->lock, flags);
918c2ecf20Sopenharmony_ci	list_del(&hpio->list);
928c2ecf20Sopenharmony_ci	iotab->io_cnt--;
938c2ecf20Sopenharmony_ci	if (!iotab->io_cnt)
948c2ecf20Sopenharmony_ci		wake_up(&iotab->empty_wq);
958c2ecf20Sopenharmony_ci	write_unlock_irqrestore(&iotab->lock, flags);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	pr_info("delete hpio %p for eid %u from iotab.\n", hpio, hpio->eid);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic void hpio_clear_pages(struct hpio *hpio)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	int i;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (!hpio->pages)
1058c2ecf20Sopenharmony_ci		return;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	for (i = 0; i < hpio->nr_page; i++)
1088c2ecf20Sopenharmony_ci		if (hpio->pages[i]) {
1098c2ecf20Sopenharmony_ci			put_page(hpio->pages[i]);
1108c2ecf20Sopenharmony_ci			atomic64_sub(PAGE_SIZE, &hpio_mem);
1118c2ecf20Sopenharmony_ci		}
1128c2ecf20Sopenharmony_ci	kfree(hpio->pages);
1138c2ecf20Sopenharmony_ci	atomic64_sub(sizeof(struct page *) * hpio->nr_page, &hpio_mem);
1148c2ecf20Sopenharmony_ci	hpio->nr_page = 0;
1158c2ecf20Sopenharmony_ci	hpio->pages = NULL;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/*
1198c2ecf20Sopenharmony_ci * alloc pages array for @hpio, fill in new alloced pages if @new_page
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_cistatic bool hpio_fill_pages(struct hpio *hpio, u32 nr_page, gfp_t gfp, bool new_page)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	int i;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	BUG_ON(hpio->pages);
1268c2ecf20Sopenharmony_ci	hpio->nr_page = nr_page;
1278c2ecf20Sopenharmony_ci	hpio->pages = kcalloc(hpio->nr_page, sizeof(struct page *), gfp);
1288c2ecf20Sopenharmony_ci	if (!hpio->pages)
1298c2ecf20Sopenharmony_ci		goto err;
1308c2ecf20Sopenharmony_ci	atomic64_add(sizeof(struct page *) * hpio->nr_page, &hpio_mem);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (!new_page)
1338c2ecf20Sopenharmony_ci		goto out;
1348c2ecf20Sopenharmony_ci	for (i = 0; i < hpio->nr_page; i++) {
1358c2ecf20Sopenharmony_ci		hpio->pages[i] = alloc_page(gfp);
1368c2ecf20Sopenharmony_ci		if (!hpio->pages[i])
1378c2ecf20Sopenharmony_ci			goto err;
1388c2ecf20Sopenharmony_ci		atomic64_add(PAGE_SIZE, &hpio_mem);
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ciout:
1418c2ecf20Sopenharmony_ci	return true;
1428c2ecf20Sopenharmony_cierr:
1438c2ecf20Sopenharmony_ci	hpio_clear_pages(hpio);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	return false;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_civoid hpio_free(struct hpio *hpio)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	if (!hpio)
1518c2ecf20Sopenharmony_ci		return;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	pr_info("free hpio = %p.\n", hpio);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	hpio_clear_pages(hpio);
1568c2ecf20Sopenharmony_ci	kfree(hpio);
1578c2ecf20Sopenharmony_ci	atomic64_sub(sizeof(struct hpio), &hpio_mem);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistruct hpio *hpio_alloc(u32 nr_page, gfp_t gfp, unsigned int op, bool new_page)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct hpio *hpio = NULL;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	hpio = kzalloc(sizeof(struct hpio), gfp);
1658c2ecf20Sopenharmony_ci	if (!hpio)
1668c2ecf20Sopenharmony_ci		goto err;
1678c2ecf20Sopenharmony_ci	atomic64_add(sizeof(struct hpio), &hpio_mem);
1688c2ecf20Sopenharmony_ci	if (!hpio_fill_pages(hpio, nr_page, gfp, new_page))
1698c2ecf20Sopenharmony_ci		goto err;
1708c2ecf20Sopenharmony_ci	hpio->op = op;
1718c2ecf20Sopenharmony_ci	atomic_set(&hpio->state, HPIO_INIT);
1728c2ecf20Sopenharmony_ci	kref_init(&hpio->refcnt);
1738c2ecf20Sopenharmony_ci	init_completion(&hpio->wait);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	return hpio;
1768c2ecf20Sopenharmony_cierr:
1778c2ecf20Sopenharmony_ci	hpio_free(hpio);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return NULL;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistruct hpio *hpio_get(u32 eid)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	return iotab_search_get(&iotab, eid);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistruct hpio *hpio_get_alloc(u32 eid, u32 nr_page, gfp_t gfp, unsigned int op)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct hpio *hpio = NULL;
1908c2ecf20Sopenharmony_ci	struct hpio *dup = NULL;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	hpio = iotab_search_get(&iotab, eid);
1938c2ecf20Sopenharmony_ci	if (hpio) {
1948c2ecf20Sopenharmony_ci		pr_info("find exist hpio %p for eid %u.\n", hpio, eid);
1958c2ecf20Sopenharmony_ci		goto out;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	hpio = hpio_alloc(nr_page, gfp, op, true);
1988c2ecf20Sopenharmony_ci	if (!hpio)
1998c2ecf20Sopenharmony_ci		goto out;
2008c2ecf20Sopenharmony_ci	hpio->eid = eid;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	pr_info("alloc hpio %p for eid %u.\n", hpio, eid);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	dup = iotab_insert(&iotab, hpio);
2058c2ecf20Sopenharmony_ci	if (dup) {
2068c2ecf20Sopenharmony_ci		hpio_free(hpio);
2078c2ecf20Sopenharmony_ci		hpio = dup;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ciout:
2108c2ecf20Sopenharmony_ci	return hpio;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic void hpio_release(struct kref *kref)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct hpio *hpio = container_of(kref, struct hpio, refcnt);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	iotab_delete(&iotab, hpio);
2188c2ecf20Sopenharmony_ci	if (hpio->free_extent)
2198c2ecf20Sopenharmony_ci		hpio->free_extent(hpio->eid);
2208c2ecf20Sopenharmony_ci	hpio_free(hpio);
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cibool hpio_put(struct hpio *hpio)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	pr_info("put hpio %p for eid %u, ref = %u.\n", hpio, hpio->eid, kref_read(&hpio->refcnt));
2268c2ecf20Sopenharmony_ci	return kref_put(&hpio->refcnt, hpio_release);
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_civoid hpio_complete(struct hpio *hpio)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	pr_info("complete hpio %p for eid %u.\n", hpio, hpio->eid);
2328c2ecf20Sopenharmony_ci	complete_all(&hpio->wait);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_civoid hpio_wait(struct hpio *hpio)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	wait_for_completion(&hpio->wait);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cienum hpio_state hpio_get_state(struct hpio *hpio)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	return atomic_read(&hpio->state);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_civoid hpio_set_state(struct hpio *hpio, enum hpio_state state)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	atomic_set(&hpio->state, state);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cibool hpio_change_state(struct hpio *hpio, enum hpio_state from, enum hpio_state to)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	return atomic_cmpxchg(&hpio->state, from, to) == from;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic void dump_iotab(struct hp_iotab *iotab)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct hpio *hpio = NULL;
2588c2ecf20Sopenharmony_ci	unsigned long flags;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	pr_info("dump inflight hpio in iotab.\n");
2618c2ecf20Sopenharmony_ci	read_lock_irqsave(&iotab->lock, flags);
2628c2ecf20Sopenharmony_ci	list_for_each_entry(hpio, &iotab->io_list, list)
2638c2ecf20Sopenharmony_ci		pr_info("hpio %p for eid %u is inflight.\n", hpio, hpio->eid);
2648c2ecf20Sopenharmony_ci	read_unlock_irqrestore(&iotab->lock, flags);
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_civoid wait_for_iotab_empty(void)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	dump_iotab(&iotab);
2708c2ecf20Sopenharmony_ci	wait_event(iotab.empty_wq, !iotab.io_cnt);
2718c2ecf20Sopenharmony_ci}
272