File: genwqe_cksum.c

package info (click to toggle)
genwqe-user 4.0.18-3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 1,440 kB
  • ctags: 2,978
  • sloc: ansic: 17,497; sh: 2,157; makefile: 406
file content (390 lines) | stat: -rw-r--r-- 10,236 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Copyright 2015, International Business Machines
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <malloc.h>
#include <time.h>
#include <signal.h>
#include <zlib.h>
#include <sys/time.h>
#include <asm/byteorder.h>

#include <sched.h>

#include "genwqe_tools.h"
#include "force_cpu.h"
#include "libcard.h"
#include "memcopy_ddcb.h"

int verbose_flag = 0;
static int debug_flag = 0;

static int DATA_BUF_SIZE = 4096 * 512;
static int use_sglist = 0;
static int use_adler32 = 0;
static int check_result = 0;
static const char *version = GENWQE_LIB_VERS_STRING;

/**
 * @brief	prints valid command line options
 *
 * @param prog	current program's name
 */
static void usage(const char *prog)
{
	printf("Usage: %s [-h] [-v, --verbose] [-C, --card <cardno>|RED]\n"
	       "\t[-V, --version]\n"
	       "\t[-X, --cpu <only run on this CPU number>]\n"
	       "\t[-D, --debug <create extended debug data on failure>]\n"
	       "\t[-G, --use-sglist use the scatter gather list support]\n"
	       "\t[-c, --check-result] check result against the software\n"
	       "\t[-s, --bufsize <bufsize/default is 4KiB>]\n"
	       "\t[-a, --adler32] use adler32 instead of crc32\n"
	       "\t[-i, --pgoffs_i <offs>] byte offset for input buffer\n"
	       "\t[FILE]...\n"
	       "\n"
	       "This utility sends memcopy/checksum DDCBs to the application\n"
	       "chip unit. The CRC32 is compatible to zlib. The UNIX program\n"
	       "cksum is using a different variation of the algorithm.\n\n",
	       prog);
}

/**
 * str_to_num - Convert string into number and cope with endings like
 *              KiB for kilobyte
 *              MiB for megabyte
 *              GiB for gigabyte
 */
static inline uint64_t str_to_num(char *str)
{
	char *s = str;
	uint64_t num = strtoull(s, &s, 0);

	if (*s == '\0')
		return num;

	if (strcmp(s, "KiB") == 0)
		num *= 1024;
	else if (strcmp(s, "MiB") == 0)
		num *= 1024 * 1024;
	else if (strcmp(s, "GiB") == 0)
		num *= 1024 * 1024 * 1024;

	return num;
}

static int genwqe_card_cksum(card_handle_t card,
			    struct genwqe_ddcb_cmd *cmd,
			    void *src, size_t n,
			    uint32_t *crc32,
			    uint32_t *adler32,
			    uint32_t *inp_processed,
			    struct genwqe_debug_data *debug_data)
{
	int rc;
	struct asiv_memcpy *asiv;
	struct asv_memcpy *asv;

	genwqe_ddcb_cmd_init(cmd);
	cmd->ddata_addr = (unsigned long)debug_data;
	cmd->acfunc	= DDCB_ACFUNC_APP;	/* goto accelerator */
	cmd->cmd	= ZCOMP_CMD_ZEDC_MEMCOPY;
	cmd->cmdopts	= 0x0001;		/* discard output for cksum */
	cmd->asiv_length= 0x40 - 0x20;
	cmd->asv_length	= 0xC0 - 0x80;		/* try to absorb all */

	/* setup ASIV part */
	asiv = (struct asiv_memcpy *)&cmd->asiv;
	asiv->inp_buff      = __cpu_to_be64((unsigned long)src);
	asiv->inp_buff_len  = __cpu_to_be32((uint32_t)n);
	asiv->outp_buff     = __cpu_to_be64(0);
	asiv->outp_buff_len = __cpu_to_be32(0);
	asiv->in_adler32    = __cpu_to_be32(*adler32);
	asiv->in_crc32      = __cpu_to_be32(*crc32);

	if (use_sglist) {
		cmd->ats = __cpu_to_be64(
			ATS_SET_FLAGS(struct asiv_memcpy, inp_buff,
				      ATS_TYPE_SGL_RD));
	} else {
		cmd->ats = __cpu_to_be64(
			ATS_SET_FLAGS(struct asiv_memcpy, inp_buff,
				      ATS_TYPE_FLAT_RD));
	}

	rc = genwqe_card_execute_ddcb(card, cmd);

	asv = (struct asv_memcpy *)&cmd->asv;
	*crc32	       = __be32_to_cpu(asv->out_crc32);
	*adler32       = __be32_to_cpu(asv->out_adler32);
	*inp_processed = __be32_to_cpu(asv->inp_processed);

	if (verbose_flag)
		fprintf(stderr, "  crc32=%u adler32=%u inp_processed=%u\n",
			*crc32, *adler32, *inp_processed);

	return rc;
}

static int process_in_file(card_handle_t card, const char *in_f,
			   uint8_t *ibuf, int ibuf_size)
{
	int rc, size_f;
	struct stat st;
	FILE *i_fp;
	uint32_t crc = 0, m_crc32 = 0; /* defined start value of 0 */
	uint32_t m_adler32 = 1;	       /* defined start value of 1 */
	uint32_t m_inp_processed;
	struct genwqe_ddcb_cmd cmd;
	struct genwqe_debug_data debug_data;
	int xerrno;

	if (check_result)
		crc = crc32(0L, Z_NULL, 0); /* start value */

	memset(&debug_data, 0, sizeof(debug_data));

	if (stat(in_f, &st) == -1) {
		fprintf(stderr, "err: stat on input file (%s)\n",
			strerror(errno));
		exit(EX_ERRNO);
	}
	size_f = st.st_size;

	i_fp = fopen(in_f, "r");
	if (!i_fp) {
		pr_err("err: can't open input file %s: %s\n", in_f,
		       strerror(errno));
		exit(EX_ERRNO);
	}

	while (size_f) {
		int tocopy = MIN(ibuf_size, size_f);

		rc = fread(ibuf, tocopy, 1, i_fp);
		if (rc != 1) {
			pr_err("err: can't read input file %s: %s\n", in_f,
			       strerror(errno));
			exit(EX_ERRNO);
		}

		if (check_result)
			crc = crc32(crc, ibuf, tocopy);	/* software */

		rc = genwqe_card_cksum(card, &cmd, ibuf, tocopy, /* hardware */
				      &m_crc32, &m_adler32, &m_inp_processed,
				      debug_flag ? &debug_data : NULL);
		xerrno = errno;

		if (debug_flag && verbose_flag)
			genwqe_print_debug_data(stdout, &debug_data,
						GENWQE_DD_ALL);

		/* Did the ioctl succeed? */
		if (rc != GENWQE_OK) {
			struct asv_runtime_dma_error *d;

			fprintf(stderr,
				"\nerr: CKSUM DDCB failed, %s (%d)\n"
				"     errno=%d %s\n", card_strerror(rc),
				rc, xerrno, strerror(xerrno));

			if (debug_flag && !verbose_flag)
				genwqe_print_debug_data(stdout, &debug_data,
							GENWQE_DD_ALL);

			fprintf(stderr, "  RETC: %03x %s ATTN: %x PROGR: %x\n"
				"  from card CRC32: %08x ADLER: %08x\n"
				"  DEQUEUE=%016llx CMPLT=%016llx DISP=%016llx\n",
				cmd.retc, retc_strerror(cmd.retc),
				cmd.attn, cmd.progress, m_crc32, m_adler32,
				(long long)cmd.deque_ts,
				(long long)cmd.cmplt_ts,
				(long long)cmd.disp_ts);

			if ((cmd.retc == DDCB_RETC_UNEXEC) &&
			    (cmd.attn == 0xe007)) {
				d = (struct asv_runtime_dma_error *)cmd.asv;
				fprintf(stderr,
					" raddr: %016llx rfmt/chan/disc: %08x "
					"rdmae: %04x rsge: %04x\n"
					" waddr: %016llx wfmt/chan/disc: %08x "
					"wdmae: %04x wsge: %04x\n",
					(long long)__be64_to_cpu(d->raddr_be64),
					__be32_to_cpu(d->rfmt_chan_disccnt_be32),
					__be16_to_cpu(d->rdmae_be16),
					__be16_to_cpu(d->rsge_be16),
					(long long)__be64_to_cpu(d->waddr_be64),
					__be32_to_cpu(d->wfmt_chan_disccnt_be32),
					__be16_to_cpu(d->wdmae_be16),
					__be16_to_cpu(d->wsge_be16));
			}
			genwqe_hexdump(stderr, cmd.asv, sizeof(cmd.asv));
			exit(EXIT_FAILURE);
		}

		size_f -= tocopy;
	}

	if (use_adler32)
		printf("%u %llu %s\n", m_adler32, (long long)st.st_size, in_f);
	else
		printf("%u %llu %s\n", m_crc32, (long long)st.st_size, in_f);

	if ((check_result) && (m_crc32 != crc)) {
		fprintf(stderr, "err: CRCs do not match %u != %u\n",
			m_crc32, crc);
	}

	fclose(i_fp);
	return 0;
}


int main(int argc, char *argv[])
{
	int card_no = 0, err_code;
	card_handle_t card;
	uint8_t *ibuf, *ibuf4k;
	unsigned int page_size = sysconf(_SC_PAGESIZE);
	const char *in_f = NULL;
	int cpu = -1;
	int pgoffs_i = 0;

	while (1) {
		int ch;
		int option_index = 0;

		static struct option long_options[] = {
			/* functions */

			/* options */
			{ "card",	   required_argument, NULL, 'C' },
			{ "cpu",	   required_argument, NULL, 'X' },
			{ "use-sglist",	   no_argument,       NULL, 'G' },
			{ "use-adler32",   no_argument,       NULL, 'a' },
			{ "check-result",  no_argument,       NULL, 'c' },

			{ "bufsize",	   required_argument, NULL, 's' },
			{ "pgoffs_i",      required_argument, NULL, 'i' },

			/* misc/support */
			{ "version",       no_argument,       NULL, 'V' },
			{ "debug",	   no_argument,	      NULL, 'D' },
			{ "verbose",	   no_argument,       NULL, 'v' },
			{ "help",	   no_argument,       NULL, 'h' },
			{ 0,		   no_argument,       NULL, 0   },
		};

		ch = getopt_long(argc, argv, "acC:X:Gs:i:vDVh",
				 long_options, &option_index);
		if (ch == -1)	/* all params processed ? */
			break;

		switch (ch) {
		/* which card to use */
		case 'C':
			if (strcmp(optarg, "RED") == 0) {
				card_no = GENWQE_CARD_REDUNDANT;
				break;
			}
			card_no = strtol(optarg, (char **)NULL, 0);
			break;
		case 'X':
			cpu = strtoul(optarg, (char **)NULL, 0);
			break;
		case 'G':
			use_sglist++;
			break;
		case 'a':
			use_adler32 = 1;
			break;
		case 'c':
			check_result++;
			break;

		case 'i':
			pgoffs_i = strtol(optarg, (char **)NULL, 0);
			break;
		case 's':
			DATA_BUF_SIZE = str_to_num(optarg);
			break;

		case 'h':
			usage(argv[0]);
			exit(EXIT_SUCCESS);
			break;
		case 'V':
			printf("%s\n", version);
			exit(EXIT_SUCCESS);
		case 'D':
			debug_flag++;
			break;
		case 'v':
			verbose_flag++;
			break;
		default:
			usage(argv[0]);
			exit(EXIT_FAILURE);
		}
	}

	switch_cpu(cpu, verbose_flag);
	genwqe_card_lib_debug(verbose_flag);

	card = genwqe_card_open(card_no, GENWQE_MODE_RDWR, &err_code,
				0x475a4950, GENWQE_APPL_ID_MASK);
	if (card == NULL) {
		printf("err: genwqe card: %s/%d; %s\n",
		       card_strerror(err_code), err_code, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (use_sglist) {
		ibuf4k = memalign(page_size, DATA_BUF_SIZE + pgoffs_i);
		if (use_sglist > 1) {
			genwqe_pin_memory(card, ibuf4k, DATA_BUF_SIZE +
					 pgoffs_i, 0);
		}
	} else {
		ibuf4k = genwqe_card_malloc(card, DATA_BUF_SIZE + pgoffs_i);
	}
	if (DATA_BUF_SIZE != 0 && ibuf4k == NULL) {
		pr_err("cannot allocate memory\n");
		exit(EXIT_FAILURE);
	}
	ibuf = ibuf4k + pgoffs_i;

	while (optind < argc) {	/* input file */
		in_f = argv[optind++];
		process_in_file(card, in_f, ibuf, DATA_BUF_SIZE);
	}

	if (use_sglist) {
		if (use_sglist > 1) {
			genwqe_unpin_memory(card, ibuf4k, DATA_BUF_SIZE +
					   pgoffs_i);
		}
		free(ibuf4k);
	} else {
		genwqe_card_free(card, ibuf4k, DATA_BUF_SIZE + pgoffs_i);
	}

	genwqe_card_close(card);
	exit(EXIT_SUCCESS);
}