xref: /kernel/linux/linux-6.6/drivers/s390/cio/fcx.c (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  Functions for assembling fcx enabled I/O control blocks.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *    Copyright IBM Corp. 2008
662306a36Sopenharmony_ci *    Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/kernel.h>
1062306a36Sopenharmony_ci#include <linux/types.h>
1162306a36Sopenharmony_ci#include <linux/string.h>
1262306a36Sopenharmony_ci#include <linux/io.h>
1362306a36Sopenharmony_ci#include <linux/errno.h>
1462306a36Sopenharmony_ci#include <linux/err.h>
1562306a36Sopenharmony_ci#include <linux/module.h>
1662306a36Sopenharmony_ci#include <asm/fcx.h>
1762306a36Sopenharmony_ci#include "cio.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/**
2062306a36Sopenharmony_ci * tcw_get_intrg - return pointer to associated interrogate tcw
2162306a36Sopenharmony_ci * @tcw: pointer to the original tcw
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * Return a pointer to the interrogate tcw associated with the specified tcw
2462306a36Sopenharmony_ci * or %NULL if there is no associated interrogate tcw.
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_cistruct tcw *tcw_get_intrg(struct tcw *tcw)
2762306a36Sopenharmony_ci{
2862306a36Sopenharmony_ci	return phys_to_virt(tcw->intrg);
2962306a36Sopenharmony_ci}
3062306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_get_intrg);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/**
3362306a36Sopenharmony_ci * tcw_get_data - return pointer to input/output data associated with tcw
3462306a36Sopenharmony_ci * @tcw: pointer to the tcw
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * Return the input or output data address specified in the tcw depending
3762306a36Sopenharmony_ci * on whether the r-bit or the w-bit is set. If neither bit is set, return
3862306a36Sopenharmony_ci * %NULL.
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_civoid *tcw_get_data(struct tcw *tcw)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	if (tcw->r)
4362306a36Sopenharmony_ci		return phys_to_virt(tcw->input);
4462306a36Sopenharmony_ci	if (tcw->w)
4562306a36Sopenharmony_ci		return phys_to_virt(tcw->output);
4662306a36Sopenharmony_ci	return NULL;
4762306a36Sopenharmony_ci}
4862306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_get_data);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/**
5162306a36Sopenharmony_ci * tcw_get_tccb - return pointer to tccb associated with tcw
5262306a36Sopenharmony_ci * @tcw: pointer to the tcw
5362306a36Sopenharmony_ci *
5462306a36Sopenharmony_ci * Return pointer to the tccb associated with this tcw.
5562306a36Sopenharmony_ci */
5662306a36Sopenharmony_cistruct tccb *tcw_get_tccb(struct tcw *tcw)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	return phys_to_virt(tcw->tccb);
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_get_tccb);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/**
6362306a36Sopenharmony_ci * tcw_get_tsb - return pointer to tsb associated with tcw
6462306a36Sopenharmony_ci * @tcw: pointer to the tcw
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci * Return pointer to the tsb associated with this tcw.
6762306a36Sopenharmony_ci */
6862306a36Sopenharmony_cistruct tsb *tcw_get_tsb(struct tcw *tcw)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	return phys_to_virt(tcw->tsb);
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_get_tsb);
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/**
7562306a36Sopenharmony_ci * tcw_init - initialize tcw data structure
7662306a36Sopenharmony_ci * @tcw: pointer to the tcw to be initialized
7762306a36Sopenharmony_ci * @r: initial value of the r-bit
7862306a36Sopenharmony_ci * @w: initial value of the w-bit
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * Initialize all fields of the specified tcw data structure with zero and
8162306a36Sopenharmony_ci * fill in the format, flags, r and w fields.
8262306a36Sopenharmony_ci */
8362306a36Sopenharmony_civoid tcw_init(struct tcw *tcw, int r, int w)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	memset(tcw, 0, sizeof(struct tcw));
8662306a36Sopenharmony_ci	tcw->format = TCW_FORMAT_DEFAULT;
8762306a36Sopenharmony_ci	tcw->flags = TCW_FLAGS_TIDAW_FORMAT(TCW_TIDAW_FORMAT_DEFAULT);
8862306a36Sopenharmony_ci	if (r)
8962306a36Sopenharmony_ci		tcw->r = 1;
9062306a36Sopenharmony_ci	if (w)
9162306a36Sopenharmony_ci		tcw->w = 1;
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_init);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic inline size_t tca_size(struct tccb *tccb)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	return tccb->tcah.tcal - 12;
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistatic u32 calc_dcw_count(struct tccb *tccb)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	int offset;
10362306a36Sopenharmony_ci	struct dcw *dcw;
10462306a36Sopenharmony_ci	u32 count = 0;
10562306a36Sopenharmony_ci	size_t size;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	size = tca_size(tccb);
10862306a36Sopenharmony_ci	for (offset = 0; offset < size;) {
10962306a36Sopenharmony_ci		dcw = (struct dcw *) &tccb->tca[offset];
11062306a36Sopenharmony_ci		count += dcw->count;
11162306a36Sopenharmony_ci		if (!(dcw->flags & DCW_FLAGS_CC))
11262306a36Sopenharmony_ci			break;
11362306a36Sopenharmony_ci		offset += sizeof(struct dcw) + ALIGN((int) dcw->cd_count, 4);
11462306a36Sopenharmony_ci	}
11562306a36Sopenharmony_ci	return count;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic u32 calc_cbc_size(struct tidaw *tidaw, int num)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	int i;
12162306a36Sopenharmony_ci	u32 cbc_data;
12262306a36Sopenharmony_ci	u32 cbc_count = 0;
12362306a36Sopenharmony_ci	u64 data_count = 0;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	for (i = 0; i < num; i++) {
12662306a36Sopenharmony_ci		if (tidaw[i].flags & TIDAW_FLAGS_LAST)
12762306a36Sopenharmony_ci			break;
12862306a36Sopenharmony_ci		/* TODO: find out if padding applies to total of data
12962306a36Sopenharmony_ci		 * transferred or data transferred by this tidaw. Assumption:
13062306a36Sopenharmony_ci		 * applies to total. */
13162306a36Sopenharmony_ci		data_count += tidaw[i].count;
13262306a36Sopenharmony_ci		if (tidaw[i].flags & TIDAW_FLAGS_INSERT_CBC) {
13362306a36Sopenharmony_ci			cbc_data = 4 + ALIGN(data_count, 4) - data_count;
13462306a36Sopenharmony_ci			cbc_count += cbc_data;
13562306a36Sopenharmony_ci			data_count += cbc_data;
13662306a36Sopenharmony_ci		}
13762306a36Sopenharmony_ci	}
13862306a36Sopenharmony_ci	return cbc_count;
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci/**
14262306a36Sopenharmony_ci * tcw_finalize - finalize tcw length fields and tidaw list
14362306a36Sopenharmony_ci * @tcw: pointer to the tcw
14462306a36Sopenharmony_ci * @num_tidaws: the number of tidaws used to address input/output data or zero
14562306a36Sopenharmony_ci * if no tida is used
14662306a36Sopenharmony_ci *
14762306a36Sopenharmony_ci * Calculate the input-/output-count and tccbl field in the tcw, add a
14862306a36Sopenharmony_ci * tcat the tccb and terminate the data tidaw list if used.
14962306a36Sopenharmony_ci *
15062306a36Sopenharmony_ci * Note: in case input- or output-tida is used, the tidaw-list must be stored
15162306a36Sopenharmony_ci * in contiguous storage (no ttic). The tcal field in the tccb must be
15262306a36Sopenharmony_ci * up-to-date.
15362306a36Sopenharmony_ci */
15462306a36Sopenharmony_civoid tcw_finalize(struct tcw *tcw, int num_tidaws)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	struct tidaw *tidaw;
15762306a36Sopenharmony_ci	struct tccb *tccb;
15862306a36Sopenharmony_ci	struct tccb_tcat *tcat;
15962306a36Sopenharmony_ci	u32 count;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	/* Terminate tidaw list. */
16262306a36Sopenharmony_ci	tidaw = tcw_get_data(tcw);
16362306a36Sopenharmony_ci	if (num_tidaws > 0)
16462306a36Sopenharmony_ci		tidaw[num_tidaws - 1].flags |= TIDAW_FLAGS_LAST;
16562306a36Sopenharmony_ci	/* Add tcat to tccb. */
16662306a36Sopenharmony_ci	tccb = tcw_get_tccb(tcw);
16762306a36Sopenharmony_ci	tcat = (struct tccb_tcat *) &tccb->tca[tca_size(tccb)];
16862306a36Sopenharmony_ci	memset(tcat, 0, sizeof(*tcat));
16962306a36Sopenharmony_ci	/* Calculate tcw input/output count and tcat transport count. */
17062306a36Sopenharmony_ci	count = calc_dcw_count(tccb);
17162306a36Sopenharmony_ci	if (tcw->w && (tcw->flags & TCW_FLAGS_OUTPUT_TIDA))
17262306a36Sopenharmony_ci		count += calc_cbc_size(tidaw, num_tidaws);
17362306a36Sopenharmony_ci	if (tcw->r)
17462306a36Sopenharmony_ci		tcw->input_count = count;
17562306a36Sopenharmony_ci	else if (tcw->w)
17662306a36Sopenharmony_ci		tcw->output_count = count;
17762306a36Sopenharmony_ci	tcat->count = ALIGN(count, 4) + 4;
17862306a36Sopenharmony_ci	/* Calculate tccbl. */
17962306a36Sopenharmony_ci	tcw->tccbl = (sizeof(struct tccb) + tca_size(tccb) +
18062306a36Sopenharmony_ci		      sizeof(struct tccb_tcat) - 20) >> 2;
18162306a36Sopenharmony_ci}
18262306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_finalize);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/**
18562306a36Sopenharmony_ci * tcw_set_intrg - set the interrogate tcw address of a tcw
18662306a36Sopenharmony_ci * @tcw: the tcw address
18762306a36Sopenharmony_ci * @intrg_tcw: the address of the interrogate tcw
18862306a36Sopenharmony_ci *
18962306a36Sopenharmony_ci * Set the address of the interrogate tcw in the specified tcw.
19062306a36Sopenharmony_ci */
19162306a36Sopenharmony_civoid tcw_set_intrg(struct tcw *tcw, struct tcw *intrg_tcw)
19262306a36Sopenharmony_ci{
19362306a36Sopenharmony_ci	tcw->intrg = (u32)virt_to_phys(intrg_tcw);
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_set_intrg);
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci/**
19862306a36Sopenharmony_ci * tcw_set_data - set data address and tida flag of a tcw
19962306a36Sopenharmony_ci * @tcw: the tcw address
20062306a36Sopenharmony_ci * @data: the data address
20162306a36Sopenharmony_ci * @use_tidal: zero of the data address specifies a contiguous block of data,
20262306a36Sopenharmony_ci * non-zero if it specifies a list if tidaws.
20362306a36Sopenharmony_ci *
20462306a36Sopenharmony_ci * Set the input/output data address of a tcw (depending on the value of the
20562306a36Sopenharmony_ci * r-flag and w-flag). If @use_tidal is non-zero, the corresponding tida flag
20662306a36Sopenharmony_ci * is set as well.
20762306a36Sopenharmony_ci */
20862306a36Sopenharmony_civoid tcw_set_data(struct tcw *tcw, void *data, int use_tidal)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	if (tcw->r) {
21162306a36Sopenharmony_ci		tcw->input = virt_to_phys(data);
21262306a36Sopenharmony_ci		if (use_tidal)
21362306a36Sopenharmony_ci			tcw->flags |= TCW_FLAGS_INPUT_TIDA;
21462306a36Sopenharmony_ci	} else if (tcw->w) {
21562306a36Sopenharmony_ci		tcw->output = virt_to_phys(data);
21662306a36Sopenharmony_ci		if (use_tidal)
21762306a36Sopenharmony_ci			tcw->flags |= TCW_FLAGS_OUTPUT_TIDA;
21862306a36Sopenharmony_ci	}
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_set_data);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/**
22362306a36Sopenharmony_ci * tcw_set_tccb - set tccb address of a tcw
22462306a36Sopenharmony_ci * @tcw: the tcw address
22562306a36Sopenharmony_ci * @tccb: the tccb address
22662306a36Sopenharmony_ci *
22762306a36Sopenharmony_ci * Set the address of the tccb in the specified tcw.
22862306a36Sopenharmony_ci */
22962306a36Sopenharmony_civoid tcw_set_tccb(struct tcw *tcw, struct tccb *tccb)
23062306a36Sopenharmony_ci{
23162306a36Sopenharmony_ci	tcw->tccb = virt_to_phys(tccb);
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_set_tccb);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci/**
23662306a36Sopenharmony_ci * tcw_set_tsb - set tsb address of a tcw
23762306a36Sopenharmony_ci * @tcw: the tcw address
23862306a36Sopenharmony_ci * @tsb: the tsb address
23962306a36Sopenharmony_ci *
24062306a36Sopenharmony_ci * Set the address of the tsb in the specified tcw.
24162306a36Sopenharmony_ci */
24262306a36Sopenharmony_civoid tcw_set_tsb(struct tcw *tcw, struct tsb *tsb)
24362306a36Sopenharmony_ci{
24462306a36Sopenharmony_ci	tcw->tsb = virt_to_phys(tsb);
24562306a36Sopenharmony_ci}
24662306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_set_tsb);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci/**
24962306a36Sopenharmony_ci * tccb_init - initialize tccb
25062306a36Sopenharmony_ci * @tccb: the tccb address
25162306a36Sopenharmony_ci * @size: the maximum size of the tccb
25262306a36Sopenharmony_ci * @sac: the service-action-code to be user
25362306a36Sopenharmony_ci *
25462306a36Sopenharmony_ci * Initialize the header of the specified tccb by resetting all values to zero
25562306a36Sopenharmony_ci * and filling in defaults for format, sac and initial tcal fields.
25662306a36Sopenharmony_ci */
25762306a36Sopenharmony_civoid tccb_init(struct tccb *tccb, size_t size, u32 sac)
25862306a36Sopenharmony_ci{
25962306a36Sopenharmony_ci	memset(tccb, 0, size);
26062306a36Sopenharmony_ci	tccb->tcah.format = TCCB_FORMAT_DEFAULT;
26162306a36Sopenharmony_ci	tccb->tcah.sac = sac;
26262306a36Sopenharmony_ci	tccb->tcah.tcal = 12;
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ciEXPORT_SYMBOL(tccb_init);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci/**
26762306a36Sopenharmony_ci * tsb_init - initialize tsb
26862306a36Sopenharmony_ci * @tsb: the tsb address
26962306a36Sopenharmony_ci *
27062306a36Sopenharmony_ci * Initialize the specified tsb by resetting all values to zero.
27162306a36Sopenharmony_ci */
27262306a36Sopenharmony_civoid tsb_init(struct tsb *tsb)
27362306a36Sopenharmony_ci{
27462306a36Sopenharmony_ci	memset(tsb, 0, sizeof(*tsb));
27562306a36Sopenharmony_ci}
27662306a36Sopenharmony_ciEXPORT_SYMBOL(tsb_init);
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci/**
27962306a36Sopenharmony_ci * tccb_add_dcw - add a dcw to the tccb
28062306a36Sopenharmony_ci * @tccb: the tccb address
28162306a36Sopenharmony_ci * @tccb_size: the maximum tccb size
28262306a36Sopenharmony_ci * @cmd: the dcw command
28362306a36Sopenharmony_ci * @flags: flags for the dcw
28462306a36Sopenharmony_ci * @cd: pointer to control data for this dcw or NULL if none is required
28562306a36Sopenharmony_ci * @cd_count: number of control data bytes for this dcw
28662306a36Sopenharmony_ci * @count: number of data bytes for this dcw
28762306a36Sopenharmony_ci *
28862306a36Sopenharmony_ci * Add a new dcw to the specified tccb by writing the dcw information specified
28962306a36Sopenharmony_ci * by @cmd, @flags, @cd, @cd_count and @count to the tca of the tccb. Return
29062306a36Sopenharmony_ci * a pointer to the newly added dcw on success or -%ENOSPC if the new dcw
29162306a36Sopenharmony_ci * would exceed the available space as defined by @tccb_size.
29262306a36Sopenharmony_ci *
29362306a36Sopenharmony_ci * Note: the tcal field of the tccb header will be updates to reflect added
29462306a36Sopenharmony_ci * content.
29562306a36Sopenharmony_ci */
29662306a36Sopenharmony_cistruct dcw *tccb_add_dcw(struct tccb *tccb, size_t tccb_size, u8 cmd, u8 flags,
29762306a36Sopenharmony_ci			 void *cd, u8 cd_count, u32 count)
29862306a36Sopenharmony_ci{
29962306a36Sopenharmony_ci	struct dcw *dcw;
30062306a36Sopenharmony_ci	int size;
30162306a36Sopenharmony_ci	int tca_offset;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	/* Check for space. */
30462306a36Sopenharmony_ci	tca_offset = tca_size(tccb);
30562306a36Sopenharmony_ci	size = ALIGN(sizeof(struct dcw) + cd_count, 4);
30662306a36Sopenharmony_ci	if (sizeof(struct tccb_tcah) + tca_offset + size +
30762306a36Sopenharmony_ci	    sizeof(struct tccb_tcat) > tccb_size)
30862306a36Sopenharmony_ci		return ERR_PTR(-ENOSPC);
30962306a36Sopenharmony_ci	/* Add dcw to tca. */
31062306a36Sopenharmony_ci	dcw = (struct dcw *) &tccb->tca[tca_offset];
31162306a36Sopenharmony_ci	memset(dcw, 0, size);
31262306a36Sopenharmony_ci	dcw->cmd = cmd;
31362306a36Sopenharmony_ci	dcw->flags = flags;
31462306a36Sopenharmony_ci	dcw->count = count;
31562306a36Sopenharmony_ci	dcw->cd_count = cd_count;
31662306a36Sopenharmony_ci	if (cd)
31762306a36Sopenharmony_ci		memcpy(&dcw->cd[0], cd, cd_count);
31862306a36Sopenharmony_ci	tccb->tcah.tcal += size;
31962306a36Sopenharmony_ci	return dcw;
32062306a36Sopenharmony_ci}
32162306a36Sopenharmony_ciEXPORT_SYMBOL(tccb_add_dcw);
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci/**
32462306a36Sopenharmony_ci * tcw_add_tidaw - add a tidaw to a tcw
32562306a36Sopenharmony_ci * @tcw: the tcw address
32662306a36Sopenharmony_ci * @num_tidaws: the current number of tidaws
32762306a36Sopenharmony_ci * @flags: flags for the new tidaw
32862306a36Sopenharmony_ci * @addr: address value for the new tidaw
32962306a36Sopenharmony_ci * @count: count value for the new tidaw
33062306a36Sopenharmony_ci *
33162306a36Sopenharmony_ci * Add a new tidaw to the input/output data tidaw-list of the specified tcw
33262306a36Sopenharmony_ci * (depending on the value of the r-flag and w-flag) and return a pointer to
33362306a36Sopenharmony_ci * the new tidaw.
33462306a36Sopenharmony_ci *
33562306a36Sopenharmony_ci * Note: the tidaw-list is assumed to be contiguous with no ttics. The caller
33662306a36Sopenharmony_ci * must ensure that there is enough space for the new tidaw. The last-tidaw
33762306a36Sopenharmony_ci * flag for the last tidaw in the list will be set by tcw_finalize.
33862306a36Sopenharmony_ci */
33962306a36Sopenharmony_cistruct tidaw *tcw_add_tidaw(struct tcw *tcw, int num_tidaws, u8 flags,
34062306a36Sopenharmony_ci			    void *addr, u32 count)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	struct tidaw *tidaw;
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	/* Add tidaw to tidaw-list. */
34562306a36Sopenharmony_ci	tidaw = ((struct tidaw *) tcw_get_data(tcw)) + num_tidaws;
34662306a36Sopenharmony_ci	memset(tidaw, 0, sizeof(struct tidaw));
34762306a36Sopenharmony_ci	tidaw->flags = flags;
34862306a36Sopenharmony_ci	tidaw->count = count;
34962306a36Sopenharmony_ci	tidaw->addr = virt_to_phys(addr);
35062306a36Sopenharmony_ci	return tidaw;
35162306a36Sopenharmony_ci}
35262306a36Sopenharmony_ciEXPORT_SYMBOL(tcw_add_tidaw);
353