crypto4xx_core.c 39.6 KB
Newer Older
J
James Hsiao 已提交
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
/**
 * AMCC SoC PPC4xx Crypto Driver
 *
 * Copyright (c) 2008 Applied Micro Circuits Corporation.
 * All rights reserved. James Hsiao <jhsiao@amcc.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * This file implements AMCC crypto offload Linux device driver for use with
 * Linux CryptoAPI.
 */

#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/spinlock_types.h>
#include <linux/random.h>
#include <linux/scatterlist.h>
#include <linux/crypto.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
#include <linux/init.h>
30 31 32
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
J
James Hsiao 已提交
33
#include <linux/of_platform.h>
34
#include <linux/slab.h>
J
James Hsiao 已提交
35 36 37
#include <asm/dcr.h>
#include <asm/dcr-regs.h>
#include <asm/cacheflush.h>
38
#include <crypto/aead.h>
J
James Hsiao 已提交
39
#include <crypto/aes.h>
40
#include <crypto/ctr.h>
41
#include <crypto/gcm.h>
J
James Hsiao 已提交
42
#include <crypto/sha.h>
43
#include <crypto/scatterwalk.h>
44
#include <crypto/skcipher.h>
45
#include <crypto/internal/aead.h>
46
#include <crypto/internal/skcipher.h>
J
James Hsiao 已提交
47 48 49
#include "crypto4xx_reg_def.h"
#include "crypto4xx_core.h"
#include "crypto4xx_sa.h"
50
#include "crypto4xx_trng.h"
J
James Hsiao 已提交
51 52 53 54 55 56 57 58 59

#define PPC4XX_SEC_VERSION_STR			"0.5"

/**
 * PPC4xx Crypto Engine Initialization Routine
 */
static void crypto4xx_hw_init(struct crypto4xx_device *dev)
{
	union ce_ring_size ring_size;
60
	union ce_ring_control ring_ctrl;
J
James Hsiao 已提交
61 62 63 64
	union ce_part_ring_size part_ring_size;
	union ce_io_threshold io_threshold;
	u32 rand_num;
	union ce_pe_dma_cfg pe_dma_cfg;
65
	u32 device_ctrl;
J
James Hsiao 已提交
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

	writel(PPC4XX_BYTE_ORDER, dev->ce_base + CRYPTO4XX_BYTE_ORDER_CFG);
	/* setup pe dma, include reset sg, pdr and pe, then release reset */
	pe_dma_cfg.w = 0;
	pe_dma_cfg.bf.bo_sgpd_en = 1;
	pe_dma_cfg.bf.bo_data_en = 0;
	pe_dma_cfg.bf.bo_sa_en = 1;
	pe_dma_cfg.bf.bo_pd_en = 1;
	pe_dma_cfg.bf.dynamic_sa_en = 1;
	pe_dma_cfg.bf.reset_sg = 1;
	pe_dma_cfg.bf.reset_pdr = 1;
	pe_dma_cfg.bf.reset_pe = 1;
	writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
	/* un reset pe,sg and pdr */
	pe_dma_cfg.bf.pe_mode = 0;
	pe_dma_cfg.bf.reset_sg = 0;
	pe_dma_cfg.bf.reset_pdr = 0;
	pe_dma_cfg.bf.reset_pe = 0;
	pe_dma_cfg.bf.bo_td_en = 0;
	writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
	writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_PDR_BASE);
	writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_RDR_BASE);
	writel(PPC4XX_PRNG_CTRL_AUTO_EN, dev->ce_base + CRYPTO4XX_PRNG_CTRL);
	get_random_bytes(&rand_num, sizeof(rand_num));
	writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_L);
	get_random_bytes(&rand_num, sizeof(rand_num));
	writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_H);
	ring_size.w = 0;
	ring_size.bf.ring_offset = PPC4XX_PD_SIZE;
	ring_size.bf.ring_size   = PPC4XX_NUM_PD;
	writel(ring_size.w, dev->ce_base + CRYPTO4XX_RING_SIZE);
	ring_ctrl.w = 0;
	writel(ring_ctrl.w, dev->ce_base + CRYPTO4XX_RING_CTRL);
99 100 101
	device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
	device_ctrl |= PPC4XX_DC_3DES_EN;
	writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
J
James Hsiao 已提交
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
	writel(dev->gdr_pa, dev->ce_base + CRYPTO4XX_GATH_RING_BASE);
	writel(dev->sdr_pa, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE);
	part_ring_size.w = 0;
	part_ring_size.bf.sdr_size = PPC4XX_SDR_SIZE;
	part_ring_size.bf.gdr_size = PPC4XX_GDR_SIZE;
	writel(part_ring_size.w, dev->ce_base + CRYPTO4XX_PART_RING_SIZE);
	writel(PPC4XX_SD_BUFFER_SIZE, dev->ce_base + CRYPTO4XX_PART_RING_CFG);
	io_threshold.w = 0;
	io_threshold.bf.output_threshold = PPC4XX_OUTPUT_THRESHOLD;
	io_threshold.bf.input_threshold  = PPC4XX_INPUT_THRESHOLD;
	writel(io_threshold.w, dev->ce_base + CRYPTO4XX_IO_THRESHOLD);
	writel(0, dev->ce_base + CRYPTO4XX_PDR_BASE_UADDR);
	writel(0, dev->ce_base + CRYPTO4XX_RDR_BASE_UADDR);
	writel(0, dev->ce_base + CRYPTO4XX_PKT_SRC_UADDR);
	writel(0, dev->ce_base + CRYPTO4XX_PKT_DEST_UADDR);
	writel(0, dev->ce_base + CRYPTO4XX_SA_UADDR);
	writel(0, dev->ce_base + CRYPTO4XX_GATH_RING_BASE_UADDR);
	writel(0, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE_UADDR);
	/* un reset pe,sg and pdr */
	pe_dma_cfg.bf.pe_mode = 1;
	pe_dma_cfg.bf.reset_sg = 0;
	pe_dma_cfg.bf.reset_pdr = 0;
	pe_dma_cfg.bf.reset_pe = 0;
	pe_dma_cfg.bf.bo_td_en = 0;
	writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
	/*clear all pending interrupt*/
	writel(PPC4XX_INTERRUPT_CLR, dev->ce_base + CRYPTO4XX_INT_CLR);
	writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
	writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
	writel(PPC4XX_INT_CFG, dev->ce_base + CRYPTO4XX_INT_CFG);
132 133 134 135 136 137 138 139
	if (dev->is_revb) {
		writel(PPC4XX_INT_TIMEOUT_CNT_REVB << 10,
		       dev->ce_base + CRYPTO4XX_INT_TIMEOUT_CNT);
		writel(PPC4XX_PD_DONE_INT | PPC4XX_TMO_ERR_INT,
		       dev->ce_base + CRYPTO4XX_INT_EN);
	} else {
		writel(PPC4XX_PD_DONE_INT, dev->ce_base + CRYPTO4XX_INT_EN);
	}
J
James Hsiao 已提交
140 141 142 143
}

int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size)
{
K
Kees Cook 已提交
144
	ctx->sa_in = kcalloc(size, 4, GFP_ATOMIC);
J
James Hsiao 已提交
145 146 147
	if (ctx->sa_in == NULL)
		return -ENOMEM;

K
Kees Cook 已提交
148
	ctx->sa_out = kcalloc(size, 4, GFP_ATOMIC);
J
James Hsiao 已提交
149
	if (ctx->sa_out == NULL) {
150 151
		kfree(ctx->sa_in);
		ctx->sa_in = NULL;
J
James Hsiao 已提交
152 153 154 155 156 157 158 159 160 161
		return -ENOMEM;
	}

	ctx->sa_len = size;

	return 0;
}

void crypto4xx_free_sa(struct crypto4xx_ctx *ctx)
{
162 163 164 165
	kfree(ctx->sa_in);
	ctx->sa_in = NULL;
	kfree(ctx->sa_out);
	ctx->sa_out = NULL;
J
James Hsiao 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	ctx->sa_len = 0;
}

/**
 * alloc memory for the gather ring
 * no need to alloc buf for the ring
 * gdr_tail, gdr_head and gdr_count are initialized by this function
 */
static u32 crypto4xx_build_pdr(struct crypto4xx_device *dev)
{
	int i;
	dev->pdr = dma_alloc_coherent(dev->core_dev->device,
				      sizeof(struct ce_pd) * PPC4XX_NUM_PD,
				      &dev->pdr_pa, GFP_ATOMIC);
	if (!dev->pdr)
		return -ENOMEM;

K
Kees Cook 已提交
183 184
	dev->pdr_uinfo = kcalloc(PPC4XX_NUM_PD, sizeof(struct pd_uinfo),
				 GFP_KERNEL);
J
James Hsiao 已提交
185 186 187 188 189 190 191
	if (!dev->pdr_uinfo) {
		dma_free_coherent(dev->core_dev->device,
				  sizeof(struct ce_pd) * PPC4XX_NUM_PD,
				  dev->pdr,
				  dev->pdr_pa);
		return -ENOMEM;
	}
192
	memset(dev->pdr, 0, sizeof(struct ce_pd) * PPC4XX_NUM_PD);
J
James Hsiao 已提交
193
	dev->shadow_sa_pool = dma_alloc_coherent(dev->core_dev->device,
194
				   sizeof(union shadow_sa_buf) * PPC4XX_NUM_PD,
J
James Hsiao 已提交
195 196 197 198 199 200 201 202 203 204 205
				   &dev->shadow_sa_pool_pa,
				   GFP_ATOMIC);
	if (!dev->shadow_sa_pool)
		return -ENOMEM;

	dev->shadow_sr_pool = dma_alloc_coherent(dev->core_dev->device,
			 sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
			 &dev->shadow_sr_pool_pa, GFP_ATOMIC);
	if (!dev->shadow_sr_pool)
		return -ENOMEM;
	for (i = 0; i < PPC4XX_NUM_PD; i++) {
206 207 208 209 210
		struct ce_pd *pd = &dev->pdr[i];
		struct pd_uinfo *pd_uinfo = &dev->pdr_uinfo[i];

		pd->sa = dev->shadow_sa_pool_pa +
			sizeof(union shadow_sa_buf) * i;
J
James Hsiao 已提交
211 212

		/* alloc 256 bytes which is enough for any kind of dynamic sa */
213
		pd_uinfo->sa_va = &dev->shadow_sa_pool[i].sa;
J
James Hsiao 已提交
214 215

		/* alloc state record */
216
		pd_uinfo->sr_va = &dev->shadow_sr_pool[i];
J
James Hsiao 已提交
217 218 219 220 221 222 223 224 225
		pd_uinfo->sr_pa = dev->shadow_sr_pool_pa +
		    sizeof(struct sa_state_record) * i;
	}

	return 0;
}

static void crypto4xx_destroy_pdr(struct crypto4xx_device *dev)
{
226
	if (dev->pdr)
J
James Hsiao 已提交
227 228 229
		dma_free_coherent(dev->core_dev->device,
				  sizeof(struct ce_pd) * PPC4XX_NUM_PD,
				  dev->pdr, dev->pdr_pa);
230

J
James Hsiao 已提交
231
	if (dev->shadow_sa_pool)
232 233 234
		dma_free_coherent(dev->core_dev->device,
			sizeof(union shadow_sa_buf) * PPC4XX_NUM_PD,
			dev->shadow_sa_pool, dev->shadow_sa_pool_pa);
235

J
James Hsiao 已提交
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
	if (dev->shadow_sr_pool)
		dma_free_coherent(dev->core_dev->device,
			sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
			dev->shadow_sr_pool, dev->shadow_sr_pool_pa);

	kfree(dev->pdr_uinfo);
}

static u32 crypto4xx_get_pd_from_pdr_nolock(struct crypto4xx_device *dev)
{
	u32 retval;
	u32 tmp;

	retval = dev->pdr_head;
	tmp = (dev->pdr_head + 1) % PPC4XX_NUM_PD;

	if (tmp == dev->pdr_tail)
		return ERING_WAS_FULL;

	dev->pdr_head = tmp;

	return retval;
}

static u32 crypto4xx_put_pd_to_pdr(struct crypto4xx_device *dev, u32 idx)
{
262
	struct pd_uinfo *pd_uinfo = &dev->pdr_uinfo[idx];
263
	u32 tail;
J
James Hsiao 已提交
264 265 266
	unsigned long flags;

	spin_lock_irqsave(&dev->core_dev->lock, flags);
267 268
	pd_uinfo->state = PD_ENTRY_FREE;

J
James Hsiao 已提交
269 270 271 272
	if (dev->pdr_tail != PPC4XX_LAST_PD)
		dev->pdr_tail++;
	else
		dev->pdr_tail = 0;
273
	tail = dev->pdr_tail;
J
James Hsiao 已提交
274 275
	spin_unlock_irqrestore(&dev->core_dev->lock, flags);

276
	return tail;
J
James Hsiao 已提交
277 278 279 280 281 282 283 284 285
}

/**
 * alloc memory for the gather ring
 * no need to alloc buf for the ring
 * gdr_tail, gdr_head and gdr_count are initialized by this function
 */
static u32 crypto4xx_build_gdr(struct crypto4xx_device *dev)
{
286 287 288
	dev->gdr = dma_zalloc_coherent(dev->core_dev->device,
				       sizeof(struct ce_gd) * PPC4XX_NUM_GD,
				       &dev->gdr_pa, GFP_ATOMIC);
J
James Hsiao 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	if (!dev->gdr)
		return -ENOMEM;

	return 0;
}

static inline void crypto4xx_destroy_gdr(struct crypto4xx_device *dev)
{
	dma_free_coherent(dev->core_dev->device,
			  sizeof(struct ce_gd) * PPC4XX_NUM_GD,
			  dev->gdr, dev->gdr_pa);
}

/*
 * when this function is called.
 * preemption or interrupt must be disabled
 */
306
static u32 crypto4xx_get_n_gd(struct crypto4xx_device *dev, int n)
J
James Hsiao 已提交
307 308 309
{
	u32 retval;
	u32 tmp;
310

J
James Hsiao 已提交
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
	if (n >= PPC4XX_NUM_GD)
		return ERING_WAS_FULL;

	retval = dev->gdr_head;
	tmp = (dev->gdr_head + n) % PPC4XX_NUM_GD;
	if (dev->gdr_head > dev->gdr_tail) {
		if (tmp < dev->gdr_head && tmp >= dev->gdr_tail)
			return ERING_WAS_FULL;
	} else if (dev->gdr_head < dev->gdr_tail) {
		if (tmp < dev->gdr_head || tmp >= dev->gdr_tail)
			return ERING_WAS_FULL;
	}
	dev->gdr_head = tmp;

	return retval;
}

static u32 crypto4xx_put_gd_to_gdr(struct crypto4xx_device *dev)
{
	unsigned long flags;

	spin_lock_irqsave(&dev->core_dev->lock, flags);
	if (dev->gdr_tail == dev->gdr_head) {
		spin_unlock_irqrestore(&dev->core_dev->lock, flags);
		return 0;
	}

	if (dev->gdr_tail != PPC4XX_LAST_GD)
		dev->gdr_tail++;
	else
		dev->gdr_tail = 0;

	spin_unlock_irqrestore(&dev->core_dev->lock, flags);

	return 0;
}

static inline struct ce_gd *crypto4xx_get_gdp(struct crypto4xx_device *dev,
					      dma_addr_t *gd_dma, u32 idx)
{
	*gd_dma = dev->gdr_pa + sizeof(struct ce_gd) * idx;

353
	return &dev->gdr[idx];
J
James Hsiao 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
}

/**
 * alloc memory for the scatter ring
 * need to alloc buf for the ring
 * sdr_tail, sdr_head and sdr_count are initialized by this function
 */
static u32 crypto4xx_build_sdr(struct crypto4xx_device *dev)
{
	int i;

	/* alloc memory for scatter descriptor ring */
	dev->sdr = dma_alloc_coherent(dev->core_dev->device,
				      sizeof(struct ce_sd) * PPC4XX_NUM_SD,
				      &dev->sdr_pa, GFP_ATOMIC);
	if (!dev->sdr)
		return -ENOMEM;

	dev->scatter_buffer_va =
		dma_alloc_coherent(dev->core_dev->device,
374
			PPC4XX_SD_BUFFER_SIZE * PPC4XX_NUM_SD,
J
James Hsiao 已提交
375
			&dev->scatter_buffer_pa, GFP_ATOMIC);
376
	if (!dev->scatter_buffer_va)
J
James Hsiao 已提交
377 378 379
		return -ENOMEM;

	for (i = 0; i < PPC4XX_NUM_SD; i++) {
380
		dev->sdr[i].ptr = dev->scatter_buffer_pa +
381
				  PPC4XX_SD_BUFFER_SIZE * i;
J
James Hsiao 已提交
382 383 384 385 386 387 388
	}

	return 0;
}

static void crypto4xx_destroy_sdr(struct crypto4xx_device *dev)
{
389
	if (dev->sdr)
J
James Hsiao 已提交
390 391 392 393
		dma_free_coherent(dev->core_dev->device,
				  sizeof(struct ce_sd) * PPC4XX_NUM_SD,
				  dev->sdr, dev->sdr_pa);

394
	if (dev->scatter_buffer_va)
J
James Hsiao 已提交
395
		dma_free_coherent(dev->core_dev->device,
396
				  PPC4XX_SD_BUFFER_SIZE * PPC4XX_NUM_SD,
J
James Hsiao 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
				  dev->scatter_buffer_va,
				  dev->scatter_buffer_pa);
}

/*
 * when this function is called.
 * preemption or interrupt must be disabled
 */
static u32 crypto4xx_get_n_sd(struct crypto4xx_device *dev, int n)
{
	u32 retval;
	u32 tmp;

	if (n >= PPC4XX_NUM_SD)
		return ERING_WAS_FULL;

	retval = dev->sdr_head;
	tmp = (dev->sdr_head + n) % PPC4XX_NUM_SD;
	if (dev->sdr_head > dev->gdr_tail) {
		if (tmp < dev->sdr_head && tmp >= dev->sdr_tail)
			return ERING_WAS_FULL;
	} else if (dev->sdr_head < dev->sdr_tail) {
		if (tmp < dev->sdr_head || tmp >= dev->sdr_tail)
			return ERING_WAS_FULL;
	} /* the head = tail, or empty case is already take cared */
	dev->sdr_head = tmp;

	return retval;
}

static u32 crypto4xx_put_sd_to_sdr(struct crypto4xx_device *dev)
{
	unsigned long flags;

	spin_lock_irqsave(&dev->core_dev->lock, flags);
	if (dev->sdr_tail == dev->sdr_head) {
		spin_unlock_irqrestore(&dev->core_dev->lock, flags);
		return 0;
	}
	if (dev->sdr_tail != PPC4XX_LAST_SD)
		dev->sdr_tail++;
	else
		dev->sdr_tail = 0;
	spin_unlock_irqrestore(&dev->core_dev->lock, flags);

	return 0;
}

static inline struct ce_sd *crypto4xx_get_sdp(struct crypto4xx_device *dev,
					      dma_addr_t *sd_dma, u32 idx)
{
	*sd_dma = dev->sdr_pa + sizeof(struct ce_sd) * idx;

450
	return &dev->sdr[idx];
J
James Hsiao 已提交
451 452 453 454 455 456 457 458
}

static void crypto4xx_copy_pkt_to_dst(struct crypto4xx_device *dev,
				      struct ce_pd *pd,
				      struct pd_uinfo *pd_uinfo,
				      u32 nbytes,
				      struct scatterlist *dst)
{
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	unsigned int first_sd = pd_uinfo->first_sd;
	unsigned int last_sd;
	unsigned int overflow = 0;
	unsigned int to_copy;
	unsigned int dst_start = 0;

	/*
	 * Because the scatter buffers are all neatly organized in one
	 * big continuous ringbuffer; scatterwalk_map_and_copy() can
	 * be instructed to copy a range of buffers in one go.
	 */

	last_sd = (first_sd + pd_uinfo->num_sd);
	if (last_sd > PPC4XX_LAST_SD) {
		last_sd = PPC4XX_LAST_SD;
		overflow = last_sd % PPC4XX_NUM_SD;
	}
J
James Hsiao 已提交
476 477

	while (nbytes) {
478 479 480 481 482 483 484 485 486 487 488 489 490
		void *buf = dev->scatter_buffer_va +
			first_sd * PPC4XX_SD_BUFFER_SIZE;

		to_copy = min(nbytes, PPC4XX_SD_BUFFER_SIZE *
				      (1 + last_sd - first_sd));
		scatterwalk_map_and_copy(buf, dst, dst_start, to_copy, 1);
		nbytes -= to_copy;

		if (overflow) {
			first_sd = 0;
			last_sd = overflow;
			dst_start += to_copy;
			overflow = 0;
J
James Hsiao 已提交
491 492 493 494
		}
	}
}

495 496
static void crypto4xx_copy_digest_to_dst(void *dst,
					struct pd_uinfo *pd_uinfo,
J
James Hsiao 已提交
497 498 499 500 501
					struct crypto4xx_ctx *ctx)
{
	struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *) ctx->sa_in;

	if (sa->sa_command_0.bf.hash_alg == SA_HASH_ALG_SHA1) {
502
		memcpy(dst, pd_uinfo->sr_va->save_digest,
J
James Hsiao 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
		       SA_HASH_ALG_SHA1_DIGEST_SIZE);
	}
}

static void crypto4xx_ret_sg_desc(struct crypto4xx_device *dev,
				  struct pd_uinfo *pd_uinfo)
{
	int i;
	if (pd_uinfo->num_gd) {
		for (i = 0; i < pd_uinfo->num_gd; i++)
			crypto4xx_put_gd_to_gdr(dev);
		pd_uinfo->first_gd = 0xffffffff;
		pd_uinfo->num_gd = 0;
	}
	if (pd_uinfo->num_sd) {
		for (i = 0; i < pd_uinfo->num_sd; i++)
			crypto4xx_put_sd_to_sdr(dev);

		pd_uinfo->first_sd = 0xffffffff;
		pd_uinfo->num_sd = 0;
	}
}

526
static void crypto4xx_cipher_done(struct crypto4xx_device *dev,
J
James Hsiao 已提交
527 528 529
				     struct pd_uinfo *pd_uinfo,
				     struct ce_pd *pd)
{
530
	struct skcipher_request *req;
J
James Hsiao 已提交
531 532 533
	struct scatterlist *dst;
	dma_addr_t addr;

534
	req = skcipher_request_cast(pd_uinfo->async_req);
J
James Hsiao 已提交
535 536

	if (pd_uinfo->using_sd) {
537 538
		crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
					  req->cryptlen, req->dst);
J
James Hsiao 已提交
539 540 541 542 543
	} else {
		dst = pd_uinfo->dest_va;
		addr = dma_map_page(dev->core_dev->device, sg_page(dst),
				    dst->offset, dst->length, DMA_FROM_DEVICE);
	}
544 545 546 547 548 549 550 551 552

	if (pd_uinfo->sa_va->sa_command_0.bf.save_iv == SA_SAVE_IV) {
		struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);

		crypto4xx_memcpy_from_le32((u32 *)req->iv,
			pd_uinfo->sr_va->save_iv,
			crypto_skcipher_ivsize(skcipher));
	}

J
James Hsiao 已提交
553
	crypto4xx_ret_sg_desc(dev, pd_uinfo);
554 555

	if (pd_uinfo->state & PD_ENTRY_BUSY)
556 557
		skcipher_request_complete(req, -EINPROGRESS);
	skcipher_request_complete(req, 0);
J
James Hsiao 已提交
558 559
}

560
static void crypto4xx_ahash_done(struct crypto4xx_device *dev,
J
James Hsiao 已提交
561 562 563 564 565 566 567 568
				struct pd_uinfo *pd_uinfo)
{
	struct crypto4xx_ctx *ctx;
	struct ahash_request *ahash_req;

	ahash_req = ahash_request_cast(pd_uinfo->async_req);
	ctx  = crypto_tfm_ctx(ahash_req->base.tfm);

569
	crypto4xx_copy_digest_to_dst(ahash_req->result, pd_uinfo,
J
James Hsiao 已提交
570 571
				     crypto_tfm_ctx(ahash_req->base.tfm));
	crypto4xx_ret_sg_desc(dev, pd_uinfo);
572 573 574 575

	if (pd_uinfo->state & PD_ENTRY_BUSY)
		ahash_request_complete(ahash_req, -EINPROGRESS);
	ahash_request_complete(ahash_req, 0);
576
}
J
James Hsiao 已提交
577

578 579 580 581
static void crypto4xx_aead_done(struct crypto4xx_device *dev,
				struct pd_uinfo *pd_uinfo,
				struct ce_pd *pd)
{
582 583
	struct aead_request *aead_req = container_of(pd_uinfo->async_req,
		struct aead_request, base);
584
	struct scatterlist *dst = pd_uinfo->dest_va;
585 586
	size_t cp_len = crypto_aead_authsize(
		crypto_aead_reqtfm(aead_req));
587
	u32 icv[AES_BLOCK_SIZE];
588 589 590 591 592 593 594 595 596 597 598 599 600 601
	int err = 0;

	if (pd_uinfo->using_sd) {
		crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
					  pd->pd_ctl_len.bf.pkt_len,
					  dst);
	} else {
		__dma_sync_page(sg_page(dst), dst->offset, dst->length,
				DMA_FROM_DEVICE);
	}

	if (pd_uinfo->sa_va->sa_command_0.bf.dir == DIR_OUTBOUND) {
		/* append icv at the end */
		crypto4xx_memcpy_from_le32(icv, pd_uinfo->sr_va->save_digest,
602
					   sizeof(icv));
603 604 605

		scatterwalk_map_and_copy(icv, dst, aead_req->cryptlen,
					 cp_len, 1);
606 607 608 609 610 611
	} else {
		/* check icv at the end */
		scatterwalk_map_and_copy(icv, aead_req->src,
			aead_req->assoclen + aead_req->cryptlen -
			cp_len, cp_len, 0);

612
		crypto4xx_memcpy_from_le32(icv, icv, sizeof(icv));
613 614 615

		if (crypto_memneq(icv, pd_uinfo->sr_va->save_digest, cp_len))
			err = -EBADMSG;
616 617 618 619 620
	}

	crypto4xx_ret_sg_desc(dev, pd_uinfo);

	if (pd->pd_ctl.bf.status & 0xff) {
621 622 623 624 625 626 627 628 629 630 631
		if (!__ratelimit(&dev->aead_ratelimit)) {
			if (pd->pd_ctl.bf.status & 2)
				pr_err("pad fail error\n");
			if (pd->pd_ctl.bf.status & 4)
				pr_err("seqnum fail\n");
			if (pd->pd_ctl.bf.status & 8)
				pr_err("error _notify\n");
			pr_err("aead return err status = 0x%02x\n",
				pd->pd_ctl.bf.status & 0xff);
			pr_err("pd pad_ctl = 0x%08x\n",
				pd->pd_ctl.bf.pd_pad_ctl);
632
		}
633
		err = -EINVAL;
634 635 636 637 638 639
	}

	if (pd_uinfo->state & PD_ENTRY_BUSY)
		aead_request_complete(aead_req, -EINPROGRESS);

	aead_request_complete(aead_req, err);
J
James Hsiao 已提交
640 641
}

642
static void crypto4xx_pd_done(struct crypto4xx_device *dev, u32 idx)
J
James Hsiao 已提交
643
{
644 645
	struct ce_pd *pd = &dev->pdr[idx];
	struct pd_uinfo *pd_uinfo = &dev->pdr_uinfo[idx];
J
James Hsiao 已提交
646

647
	switch (crypto_tfm_alg_type(pd_uinfo->async_req->tfm)) {
648 649
	case CRYPTO_ALG_TYPE_SKCIPHER:
		crypto4xx_cipher_done(dev, pd_uinfo, pd);
650 651 652 653 654 655 656 657
		break;
	case CRYPTO_ALG_TYPE_AEAD:
		crypto4xx_aead_done(dev, pd_uinfo, pd);
		break;
	case CRYPTO_ALG_TYPE_AHASH:
		crypto4xx_ahash_done(dev, pd_uinfo);
		break;
	}
J
James Hsiao 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
}

static void crypto4xx_stop_all(struct crypto4xx_core_device *core_dev)
{
	crypto4xx_destroy_pdr(core_dev->dev);
	crypto4xx_destroy_gdr(core_dev->dev);
	crypto4xx_destroy_sdr(core_dev->dev);
	iounmap(core_dev->dev->ce_base);
	kfree(core_dev->dev);
	kfree(core_dev);
}

static u32 get_next_gd(u32 current)
{
	if (current != PPC4XX_LAST_GD)
		return current + 1;
	else
		return 0;
}

static u32 get_next_sd(u32 current)
{
	if (current != PPC4XX_LAST_SD)
		return current + 1;
	else
		return 0;
}

686
int crypto4xx_build_pd(struct crypto_async_request *req,
J
James Hsiao 已提交
687 688 689
		       struct crypto4xx_ctx *ctx,
		       struct scatterlist *src,
		       struct scatterlist *dst,
690 691 692
		       const unsigned int datalen,
		       const __le32 *iv, const u32 iv_len,
		       const struct dynamic_sa_ctl *req_sa,
693
		       const unsigned int sa_len,
694 695
		       const unsigned int assoclen,
		       struct scatterlist *_dst)
J
James Hsiao 已提交
696 697 698 699 700 701 702 703 704 705
{
	struct crypto4xx_device *dev = ctx->dev;
	struct dynamic_sa_ctl *sa;
	struct ce_gd *gd;
	struct ce_pd *pd;
	u32 num_gd, num_sd;
	u32 fst_gd = 0xffffffff;
	u32 fst_sd = 0xffffffff;
	u32 pd_entry;
	unsigned long flags;
706 707 708
	struct pd_uinfo *pd_uinfo;
	unsigned int nbytes = datalen;
	size_t offset_to_sr_ptr;
J
James Hsiao 已提交
709
	u32 gd_idx = 0;
710
	int tmp;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	bool is_busy, force_sd;

	/*
	 * There's a very subtile/disguised "bug" in the hardware that
	 * gets indirectly mentioned in 18.1.3.5 Encryption/Decryption
	 * of the hardware spec:
	 * *drum roll* the AES/(T)DES OFB and CFB modes are listed as
	 * operation modes for >>> "Block ciphers" <<<.
	 *
	 * To workaround this issue and stop the hardware from causing
	 * "overran dst buffer" on crypttexts that are not a multiple
	 * of 16 (AES_BLOCK_SIZE), we force the driver to use the
	 * scatter buffers.
	 */
	force_sd = (req_sa->sa_command_1.bf.crypto_mode9_8 == CRYPTO_MODE_CFB
		|| req_sa->sa_command_1.bf.crypto_mode9_8 == CRYPTO_MODE_OFB)
		&& (datalen % AES_BLOCK_SIZE);
J
James Hsiao 已提交
728

729 730 731
	/* figure how many gd are needed */
	tmp = sg_nents_for_len(src, assoclen + datalen);
	if (tmp < 0) {
732
		dev_err(dev->core_dev->device, "Invalid number of src SG.\n");
733 734 735 736 737 738 739 740 741
		return tmp;
	}
	if (tmp == 1)
		tmp = 0;
	num_gd = tmp;

	if (assoclen) {
		nbytes += assoclen;
		dst = scatterwalk_ffwd(_dst, dst, assoclen);
742
	}
J
James Hsiao 已提交
743

744
	/* figure how many sd are needed */
745
	if (sg_is_last(dst) && force_sd == false) {
J
James Hsiao 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
		num_sd = 0;
	} else {
		if (datalen > PPC4XX_SD_BUFFER_SIZE) {
			num_sd = datalen / PPC4XX_SD_BUFFER_SIZE;
			if (datalen % PPC4XX_SD_BUFFER_SIZE)
				num_sd++;
		} else {
			num_sd = 1;
		}
	}

	/*
	 * The follow section of code needs to be protected
	 * The gather ring and scatter ring needs to be consecutive
	 * In case of run out of any kind of descriptor, the descriptor
	 * already got must be return the original place.
	 */
	spin_lock_irqsave(&dev->core_dev->lock, flags);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
	/*
	 * Let the caller know to slow down, once more than 13/16ths = 81%
	 * of the available data contexts are being used simultaneously.
	 *
	 * With PPC4XX_NUM_PD = 256, this will leave a "backlog queue" for
	 * 31 more contexts. Before new requests have to be rejected.
	 */
	if (req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG) {
		is_busy = ((dev->pdr_head - dev->pdr_tail) % PPC4XX_NUM_PD) >=
			((PPC4XX_NUM_PD * 13) / 16);
	} else {
		/*
		 * To fix contention issues between ipsec (no blacklog) and
		 * dm-crypto (backlog) reserve 32 entries for "no backlog"
		 * data contexts.
		 */
		is_busy = ((dev->pdr_head - dev->pdr_tail) % PPC4XX_NUM_PD) >=
			((PPC4XX_NUM_PD * 15) / 16);

		if (is_busy) {
			spin_unlock_irqrestore(&dev->core_dev->lock, flags);
			return -EBUSY;
		}
	}

J
James Hsiao 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
	if (num_gd) {
		fst_gd = crypto4xx_get_n_gd(dev, num_gd);
		if (fst_gd == ERING_WAS_FULL) {
			spin_unlock_irqrestore(&dev->core_dev->lock, flags);
			return -EAGAIN;
		}
	}
	if (num_sd) {
		fst_sd = crypto4xx_get_n_sd(dev, num_sd);
		if (fst_sd == ERING_WAS_FULL) {
			if (num_gd)
				dev->gdr_head = fst_gd;
			spin_unlock_irqrestore(&dev->core_dev->lock, flags);
			return -EAGAIN;
		}
	}
	pd_entry = crypto4xx_get_pd_from_pdr_nolock(dev);
	if (pd_entry == ERING_WAS_FULL) {
		if (num_gd)
			dev->gdr_head = fst_gd;
		if (num_sd)
			dev->sdr_head = fst_sd;
		spin_unlock_irqrestore(&dev->core_dev->lock, flags);
		return -EAGAIN;
	}
	spin_unlock_irqrestore(&dev->core_dev->lock, flags);

816 817 818
	pd = &dev->pdr[pd_entry];
	pd->sa_len = sa_len;

819
	pd_uinfo = &dev->pdr_uinfo[pd_entry];
J
James Hsiao 已提交
820 821
	pd_uinfo->num_gd = num_gd;
	pd_uinfo->num_sd = num_sd;
822 823
	pd_uinfo->dest_va = dst;
	pd_uinfo->async_req = req;
J
James Hsiao 已提交
824

825 826
	if (iv_len)
		memcpy(pd_uinfo->sr_va->save_iv, iv, iv_len);
J
James Hsiao 已提交
827

828 829 830
	sa = pd_uinfo->sa_va;
	memcpy(sa, req_sa, sa_len * 4);

831
	sa->sa_command_1.bf.hash_crypto_offset = (assoclen >> 2);
832 833
	offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(sa);
	*(u32 *)((unsigned long)sa + offset_to_sr_ptr) = pd_uinfo->sr_pa;
J
James Hsiao 已提交
834 835

	if (num_gd) {
836 837 838
		dma_addr_t gd_dma;
		struct scatterlist *sg;

J
James Hsiao 已提交
839 840 841 842 843 844 845 846
		/* get first gd we are going to use */
		gd_idx = fst_gd;
		pd_uinfo->first_gd = fst_gd;
		gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
		pd->src = gd_dma;
		/* enable gather */
		sa->sa_command_0.bf.gather = 1;
		/* walk the sg, and setup gather array */
847 848

		sg = src;
J
James Hsiao 已提交
849
		while (nbytes) {
850 851 852 853 854 855
			size_t len;

			len = min(sg->length, nbytes);
			gd->ptr = dma_map_page(dev->core_dev->device,
				sg_page(sg), sg->offset, len, DMA_TO_DEVICE);
			gd->ctl_len.len = len;
J
James Hsiao 已提交
856 857
			gd->ctl_len.done = 0;
			gd->ctl_len.ready = 1;
858
			if (len >= nbytes)
J
James Hsiao 已提交
859
				break;
860

J
James Hsiao 已提交
861 862 863
			nbytes -= sg->length;
			gd_idx = get_next_gd(gd_idx);
			gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
864
			sg = sg_next(sg);
J
James Hsiao 已提交
865 866 867
		}
	} else {
		pd->src = (u32)dma_map_page(dev->core_dev->device, sg_page(src),
868 869
				src->offset, min(nbytes, src->length),
				DMA_TO_DEVICE);
J
James Hsiao 已提交
870 871 872 873 874 875 876 877 878
		/*
		 * Disable gather in sa command
		 */
		sa->sa_command_0.bf.gather = 0;
		/*
		 * Indicate gather array is not used
		 */
		pd_uinfo->first_gd = 0xffffffff;
	}
879
	if (!num_sd) {
J
James Hsiao 已提交
880 881 882 883 884 885 886
		/*
		 * we know application give us dst a whole piece of memory
		 * no need to use scatter ring.
		 */
		pd_uinfo->using_sd = 0;
		pd_uinfo->first_sd = 0xffffffff;
		sa->sa_command_0.bf.scatter = 0;
887 888 889 890
		pd->dest = (u32)dma_map_page(dev->core_dev->device,
					     sg_page(dst), dst->offset,
					     min(datalen, dst->length),
					     DMA_TO_DEVICE);
J
James Hsiao 已提交
891
	} else {
892
		dma_addr_t sd_dma;
J
James Hsiao 已提交
893
		struct ce_sd *sd = NULL;
894

J
James Hsiao 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
		u32 sd_idx = fst_sd;
		nbytes = datalen;
		sa->sa_command_0.bf.scatter = 1;
		pd_uinfo->using_sd = 1;
		pd_uinfo->first_sd = fst_sd;
		sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
		pd->dest = sd_dma;
		/* setup scatter descriptor */
		sd->ctl.done = 0;
		sd->ctl.rdy = 1;
		/* sd->ptr should be setup by sd_init routine*/
		if (nbytes >= PPC4XX_SD_BUFFER_SIZE)
			nbytes -= PPC4XX_SD_BUFFER_SIZE;
		else
			nbytes = 0;
		while (nbytes) {
			sd_idx = get_next_sd(sd_idx);
			sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
			/* setup scatter descriptor */
			sd->ctl.done = 0;
			sd->ctl.rdy = 1;
916
			if (nbytes >= PPC4XX_SD_BUFFER_SIZE) {
J
James Hsiao 已提交
917
				nbytes -= PPC4XX_SD_BUFFER_SIZE;
918
			} else {
J
James Hsiao 已提交
919 920 921 922 923
				/*
				 * SD entry can hold PPC4XX_SD_BUFFER_SIZE,
				 * which is more than nbytes, so done.
				 */
				nbytes = 0;
924
			}
J
James Hsiao 已提交
925 926 927
		}
	}

928 929 930 931
	pd->pd_ctl.w = PD_CTL_HOST_READY |
		((crypto_tfm_alg_type(req->tfm) == CRYPTO_ALG_TYPE_AHASH) |
		 (crypto_tfm_alg_type(req->tfm) == CRYPTO_ALG_TYPE_AEAD) ?
			PD_CTL_HASH_FINAL : 0);
932
	pd->pd_ctl_len.w = 0x00400000 | (assoclen + datalen);
933 934
	pd_uinfo->state = PD_ENTRY_INUSE | (is_busy ? PD_ENTRY_BUSY : 0);

J
James Hsiao 已提交
935 936
	wmb();
	/* write any value to push engine to read a pd */
937
	writel(0, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
J
James Hsiao 已提交
938
	writel(1, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
939
	return is_busy ? -EBUSY : -EINPROGRESS;
J
James Hsiao 已提交
940 941 942 943 944
}

/**
 * Algorithm Registration Functions
 */
945 946
static void crypto4xx_ctx_init(struct crypto4xx_alg *amcc_alg,
			       struct crypto4xx_ctx *ctx)
J
James Hsiao 已提交
947 948 949 950 951
{
	ctx->dev = amcc_alg->dev;
	ctx->sa_in = NULL;
	ctx->sa_out = NULL;
	ctx->sa_len = 0;
952
}
J
James Hsiao 已提交
953

954
static int crypto4xx_sk_init(struct crypto_skcipher *sk)
955
{
956
	struct skcipher_alg *alg = crypto_skcipher_alg(sk);
957
	struct crypto4xx_alg *amcc_alg;
958
	struct crypto4xx_ctx *ctx =  crypto_skcipher_ctx(sk);
J
James Hsiao 已提交
959

960 961 962 963 964 965 966 967 968 969 970 971 972
	if (alg->base.cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
		ctx->sw_cipher.cipher =
			crypto_alloc_skcipher(alg->base.cra_name, 0,
					      CRYPTO_ALG_NEED_FALLBACK |
					      CRYPTO_ALG_ASYNC);
		if (IS_ERR(ctx->sw_cipher.cipher))
			return PTR_ERR(ctx->sw_cipher.cipher);

		crypto_skcipher_set_reqsize(sk,
			sizeof(struct skcipher_request) + 32 +
			crypto_skcipher_reqsize(ctx->sw_cipher.cipher));
	}

973 974
	amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.cipher);
	crypto4xx_ctx_init(amcc_alg, ctx);
J
James Hsiao 已提交
975 976 977
	return 0;
}

978
static void crypto4xx_common_exit(struct crypto4xx_ctx *ctx)
J
James Hsiao 已提交
979 980 981 982
{
	crypto4xx_free_sa(ctx);
}

983
static void crypto4xx_sk_exit(struct crypto_skcipher *sk)
984
{
985 986 987
	struct crypto4xx_ctx *ctx =  crypto_skcipher_ctx(sk);

	crypto4xx_common_exit(ctx);
988 989
	if (ctx->sw_cipher.cipher)
		crypto_free_skcipher(ctx->sw_cipher.cipher);
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
}

static int crypto4xx_aead_init(struct crypto_aead *tfm)
{
	struct aead_alg *alg = crypto_aead_alg(tfm);
	struct crypto4xx_ctx *ctx = crypto_aead_ctx(tfm);
	struct crypto4xx_alg *amcc_alg;

	ctx->sw_cipher.aead = crypto_alloc_aead(alg->base.cra_name, 0,
						CRYPTO_ALG_NEED_FALLBACK |
						CRYPTO_ALG_ASYNC);
	if (IS_ERR(ctx->sw_cipher.aead))
		return PTR_ERR(ctx->sw_cipher.aead);

	amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.aead);
	crypto4xx_ctx_init(amcc_alg, ctx);
1006 1007 1008
	crypto_aead_set_reqsize(tfm, max(sizeof(struct aead_request) + 32 +
				crypto_aead_reqsize(ctx->sw_cipher.aead),
				sizeof(struct crypto4xx_aead_reqctx)));
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	return 0;
}

static void crypto4xx_aead_exit(struct crypto_aead *tfm)
{
	struct crypto4xx_ctx *ctx = crypto_aead_ctx(tfm);

	crypto4xx_common_exit(ctx);
	crypto_free_aead(ctx->sw_cipher.aead);
}

static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
				  struct crypto4xx_alg_common *crypto_alg,
				  int array_size)
J
James Hsiao 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
{
	struct crypto4xx_alg *alg;
	int i;
	int rc = 0;

	for (i = 0; i < array_size; i++) {
		alg = kzalloc(sizeof(struct crypto4xx_alg), GFP_KERNEL);
		if (!alg)
			return -ENOMEM;

		alg->alg = crypto_alg[i];
		alg->dev = sec_dev;
1035 1036

		switch (alg->alg.type) {
1037 1038 1039 1040
		case CRYPTO_ALG_TYPE_AEAD:
			rc = crypto_register_aead(&alg->alg.u.aead);
			break;

1041 1042 1043 1044 1045
		case CRYPTO_ALG_TYPE_AHASH:
			rc = crypto_register_ahash(&alg->alg.u.hash);
			break;

		default:
1046
			rc = crypto_register_skcipher(&alg->alg.u.cipher);
1047 1048 1049
			break;
		}

1050
		if (rc)
J
James Hsiao 已提交
1051
			kfree(alg);
1052
		else
J
James Hsiao 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
			list_add_tail(&alg->entry, &sec_dev->alg_list);
	}

	return 0;
}

static void crypto4xx_unregister_alg(struct crypto4xx_device *sec_dev)
{
	struct crypto4xx_alg *alg, *tmp;

	list_for_each_entry_safe(alg, tmp, &sec_dev->alg_list, entry) {
		list_del(&alg->entry);
1065 1066 1067 1068 1069
		switch (alg->alg.type) {
		case CRYPTO_ALG_TYPE_AHASH:
			crypto_unregister_ahash(&alg->alg.u.hash);
			break;

1070 1071 1072 1073
		case CRYPTO_ALG_TYPE_AEAD:
			crypto_unregister_aead(&alg->alg.u.aead);
			break;

1074
		default:
1075
			crypto_unregister_skcipher(&alg->alg.u.cipher);
1076
		}
J
James Hsiao 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
		kfree(alg);
	}
}

static void crypto4xx_bh_tasklet_cb(unsigned long data)
{
	struct device *dev = (struct device *)data;
	struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
	struct pd_uinfo *pd_uinfo;
	struct ce_pd *pd;
1087 1088
	u32 tail = core_dev->dev->pdr_tail;
	u32 head = core_dev->dev->pdr_head;
J
James Hsiao 已提交
1089

1090
	do {
1091 1092
		pd_uinfo = &core_dev->dev->pdr_uinfo[tail];
		pd = &core_dev->dev->pdr[tail];
1093
		if ((pd_uinfo->state & PD_ENTRY_INUSE) &&
1094 1095 1096
		     ((READ_ONCE(pd->pd_ctl.w) &
		       (PD_CTL_PE_DONE | PD_CTL_HOST_READY)) ==
		       PD_CTL_PE_DONE)) {
J
James Hsiao 已提交
1097
			crypto4xx_pd_done(core_dev->dev, tail);
1098
			tail = crypto4xx_put_pd_to_pdr(core_dev->dev, tail);
J
James Hsiao 已提交
1099 1100 1101 1102
		} else {
			/* if tail not done, break */
			break;
		}
1103
	} while (head != tail);
J
James Hsiao 已提交
1104 1105 1106 1107 1108
}

/**
 * Top Half of isr.
 */
1109 1110
static inline irqreturn_t crypto4xx_interrupt_handler(int irq, void *data,
						      u32 clr_val)
J
James Hsiao 已提交
1111 1112 1113 1114
{
	struct device *dev = (struct device *)data;
	struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);

1115
	writel(clr_val, core_dev->dev->ce_base + CRYPTO4XX_INT_CLR);
J
James Hsiao 已提交
1116 1117 1118 1119 1120
	tasklet_schedule(&core_dev->tasklet);

	return IRQ_HANDLED;
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
static irqreturn_t crypto4xx_ce_interrupt_handler(int irq, void *data)
{
	return crypto4xx_interrupt_handler(irq, data, PPC4XX_INTERRUPT_CLR);
}

static irqreturn_t crypto4xx_ce_interrupt_handler_revb(int irq, void *data)
{
	return crypto4xx_interrupt_handler(irq, data, PPC4XX_INTERRUPT_CLR |
		PPC4XX_TMO_ERR_INT);
}

J
James Hsiao 已提交
1132 1133 1134
/**
 * Supported Crypto Algorithms
 */
1135
static struct crypto4xx_alg_common crypto4xx_alg[] = {
J
James Hsiao 已提交
1136
	/* Crypto AES modes */
1137 1138 1139 1140 1141
	{ .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
		.base = {
			.cra_name = "cbc(aes)",
			.cra_driver_name = "cbc-aes-ppc4xx",
			.cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
1142
			.cra_flags = CRYPTO_ALG_ASYNC |
1143 1144 1145 1146 1147 1148 1149 1150 1151
				CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct crypto4xx_ctx),
			.cra_module = THIS_MODULE,
		},
		.min_keysize = AES_MIN_KEY_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE,
		.ivsize	= AES_IV_SIZE,
		.setkey = crypto4xx_setkey_aes_cbc,
1152 1153
		.encrypt = crypto4xx_encrypt_iv_block,
		.decrypt = crypto4xx_decrypt_iv_block,
1154 1155
		.init = crypto4xx_sk_init,
		.exit = crypto4xx_sk_exit,
1156
	} },
1157 1158 1159 1160 1161
	{ .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
		.base = {
			.cra_name = "cfb(aes)",
			.cra_driver_name = "cfb-aes-ppc4xx",
			.cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
1162
			.cra_flags = CRYPTO_ALG_ASYNC |
1163 1164 1165 1166 1167 1168 1169 1170 1171
				CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct crypto4xx_ctx),
			.cra_module = THIS_MODULE,
		},
		.min_keysize = AES_MIN_KEY_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE,
		.ivsize	= AES_IV_SIZE,
		.setkey	= crypto4xx_setkey_aes_cfb,
1172 1173
		.encrypt = crypto4xx_encrypt_iv_stream,
		.decrypt = crypto4xx_decrypt_iv_stream,
1174 1175
		.init = crypto4xx_sk_init,
		.exit = crypto4xx_sk_exit,
1176
	} },
1177 1178 1179 1180 1181
	{ .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
		.base = {
			.cra_name = "ctr(aes)",
			.cra_driver_name = "ctr-aes-ppc4xx",
			.cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
1182
			.cra_flags = CRYPTO_ALG_NEED_FALLBACK |
1183 1184
				CRYPTO_ALG_ASYNC |
				CRYPTO_ALG_KERN_DRIVER_ONLY,
1185
			.cra_blocksize = 1,
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
			.cra_ctxsize = sizeof(struct crypto4xx_ctx),
			.cra_module = THIS_MODULE,
		},
		.min_keysize = AES_MIN_KEY_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE,
		.ivsize	= AES_IV_SIZE,
		.setkey	= crypto4xx_setkey_aes_ctr,
		.encrypt = crypto4xx_encrypt_ctr,
		.decrypt = crypto4xx_decrypt_ctr,
		.init = crypto4xx_sk_init,
		.exit = crypto4xx_sk_exit,
	} },
1198 1199 1200 1201 1202
	{ .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
		.base = {
			.cra_name = "rfc3686(ctr(aes))",
			.cra_driver_name = "rfc3686-ctr-aes-ppc4xx",
			.cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
1203
			.cra_flags = CRYPTO_ALG_ASYNC |
1204
				CRYPTO_ALG_KERN_DRIVER_ONLY,
1205
			.cra_blocksize = 1,
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
			.cra_ctxsize = sizeof(struct crypto4xx_ctx),
			.cra_module = THIS_MODULE,
		},
		.min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
		.ivsize	= CTR_RFC3686_IV_SIZE,
		.setkey = crypto4xx_setkey_rfc3686,
		.encrypt = crypto4xx_rfc3686_encrypt,
		.decrypt = crypto4xx_rfc3686_decrypt,
		.init = crypto4xx_sk_init,
		.exit = crypto4xx_sk_exit,
1217
	} },
1218 1219 1220 1221 1222
	{ .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
		.base = {
			.cra_name = "ecb(aes)",
			.cra_driver_name = "ecb-aes-ppc4xx",
			.cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
1223
			.cra_flags = CRYPTO_ALG_ASYNC |
1224
				CRYPTO_ALG_KERN_DRIVER_ONLY,
1225
			.cra_blocksize = 1,
1226 1227 1228 1229 1230 1231
			.cra_ctxsize = sizeof(struct crypto4xx_ctx),
			.cra_module = THIS_MODULE,
		},
		.min_keysize = AES_MIN_KEY_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE,
		.setkey	= crypto4xx_setkey_aes_ecb,
1232 1233
		.encrypt = crypto4xx_encrypt_noiv_block,
		.decrypt = crypto4xx_decrypt_noiv_block,
1234 1235 1236 1237 1238 1239 1240 1241
		.init = crypto4xx_sk_init,
		.exit = crypto4xx_sk_exit,
	} },
	{ .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
		.base = {
			.cra_name = "ofb(aes)",
			.cra_driver_name = "ofb-aes-ppc4xx",
			.cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
1242
			.cra_flags = CRYPTO_ALG_ASYNC |
1243
				CRYPTO_ALG_KERN_DRIVER_ONLY,
1244
			.cra_blocksize = 1,
1245 1246 1247 1248 1249 1250 1251
			.cra_ctxsize = sizeof(struct crypto4xx_ctx),
			.cra_module = THIS_MODULE,
		},
		.min_keysize = AES_MIN_KEY_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE,
		.ivsize	= AES_IV_SIZE,
		.setkey	= crypto4xx_setkey_aes_ofb,
1252 1253
		.encrypt = crypto4xx_encrypt_iv_stream,
		.decrypt = crypto4xx_decrypt_iv_stream,
1254 1255
		.init = crypto4xx_sk_init,
		.exit = crypto4xx_sk_exit,
1256
	} },
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

	/* AEAD */
	{ .type = CRYPTO_ALG_TYPE_AEAD, .u.aead = {
		.setkey		= crypto4xx_setkey_aes_ccm,
		.setauthsize	= crypto4xx_setauthsize_aead,
		.encrypt	= crypto4xx_encrypt_aes_ccm,
		.decrypt	= crypto4xx_decrypt_aes_ccm,
		.init		= crypto4xx_aead_init,
		.exit		= crypto4xx_aead_exit,
		.ivsize		= AES_BLOCK_SIZE,
		.maxauthsize    = 16,
		.base = {
			.cra_name	= "ccm(aes)",
			.cra_driver_name = "ccm-aes-ppc4xx",
			.cra_priority	= CRYPTO4XX_CRYPTO_PRIORITY,
			.cra_flags	= CRYPTO_ALG_ASYNC |
					  CRYPTO_ALG_NEED_FALLBACK |
					  CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize	= 1,
			.cra_ctxsize	= sizeof(struct crypto4xx_ctx),
			.cra_module	= THIS_MODULE,
		},
	} },
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	{ .type = CRYPTO_ALG_TYPE_AEAD, .u.aead = {
		.setkey		= crypto4xx_setkey_aes_gcm,
		.setauthsize	= crypto4xx_setauthsize_aead,
		.encrypt	= crypto4xx_encrypt_aes_gcm,
		.decrypt	= crypto4xx_decrypt_aes_gcm,
		.init		= crypto4xx_aead_init,
		.exit		= crypto4xx_aead_exit,
		.ivsize		= GCM_AES_IV_SIZE,
		.maxauthsize	= 16,
		.base = {
			.cra_name	= "gcm(aes)",
			.cra_driver_name = "gcm-aes-ppc4xx",
			.cra_priority	= CRYPTO4XX_CRYPTO_PRIORITY,
			.cra_flags	= CRYPTO_ALG_ASYNC |
					  CRYPTO_ALG_NEED_FALLBACK |
					  CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize	= 1,
			.cra_ctxsize	= sizeof(struct crypto4xx_ctx),
			.cra_module	= THIS_MODULE,
		},
	} },
J
James Hsiao 已提交
1301 1302 1303 1304 1305
};

/**
 * Module Initialization Routine
 */
1306
static int crypto4xx_probe(struct platform_device *ofdev)
J
James Hsiao 已提交
1307 1308 1309 1310 1311
{
	int rc;
	struct resource res;
	struct device *dev = &ofdev->dev;
	struct crypto4xx_core_device *core_dev;
1312 1313
	u32 pvr;
	bool is_revb = true;
J
James Hsiao 已提交
1314

1315
	rc = of_address_to_resource(ofdev->dev.of_node, 0, &res);
J
James Hsiao 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	if (rc)
		return -ENODEV;

	if (of_find_compatible_node(NULL, NULL, "amcc,ppc460ex-crypto")) {
		mtdcri(SDR0, PPC460EX_SDR0_SRST,
		       mfdcri(SDR0, PPC460EX_SDR0_SRST) | PPC460EX_CE_RESET);
		mtdcri(SDR0, PPC460EX_SDR0_SRST,
		       mfdcri(SDR0, PPC460EX_SDR0_SRST) & ~PPC460EX_CE_RESET);
	} else if (of_find_compatible_node(NULL, NULL,
			"amcc,ppc405ex-crypto")) {
		mtdcri(SDR0, PPC405EX_SDR0_SRST,
		       mfdcri(SDR0, PPC405EX_SDR0_SRST) | PPC405EX_CE_RESET);
		mtdcri(SDR0, PPC405EX_SDR0_SRST,
		       mfdcri(SDR0, PPC405EX_SDR0_SRST) & ~PPC405EX_CE_RESET);
1330
		is_revb = false;
J
James Hsiao 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	} else if (of_find_compatible_node(NULL, NULL,
			"amcc,ppc460sx-crypto")) {
		mtdcri(SDR0, PPC460SX_SDR0_SRST,
		       mfdcri(SDR0, PPC460SX_SDR0_SRST) | PPC460SX_CE_RESET);
		mtdcri(SDR0, PPC460SX_SDR0_SRST,
		       mfdcri(SDR0, PPC460SX_SDR0_SRST) & ~PPC460SX_CE_RESET);
	} else {
		printk(KERN_ERR "Crypto Function Not supported!\n");
		return -EINVAL;
	}

	core_dev = kzalloc(sizeof(struct crypto4xx_core_device), GFP_KERNEL);
	if (!core_dev)
		return -ENOMEM;

	dev_set_drvdata(dev, core_dev);
	core_dev->ofdev = ofdev;
	core_dev->dev = kzalloc(sizeof(struct crypto4xx_device), GFP_KERNEL);
1349
	rc = -ENOMEM;
J
James Hsiao 已提交
1350 1351 1352
	if (!core_dev->dev)
		goto err_alloc_dev;

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
	/*
	 * Older version of 460EX/GT have a hardware bug.
	 * Hence they do not support H/W based security intr coalescing
	 */
	pvr = mfspr(SPRN_PVR);
	if (is_revb && ((pvr >> 4) == 0x130218A)) {
		u32 min = PVR_MIN(pvr);

		if (min < 4) {
			dev_info(dev, "RevA detected - disable interrupt coalescing\n");
			is_revb = false;
		}
	}

J
James Hsiao 已提交
1367
	core_dev->dev->core_dev = core_dev;
1368
	core_dev->dev->is_revb = is_revb;
J
James Hsiao 已提交
1369 1370 1371
	core_dev->device = dev;
	spin_lock_init(&core_dev->lock);
	INIT_LIST_HEAD(&core_dev->dev->alg_list);
1372
	ratelimit_default_init(&core_dev->dev->aead_ratelimit);
J
James Hsiao 已提交
1373 1374 1375 1376 1377 1378
	rc = crypto4xx_build_pdr(core_dev->dev);
	if (rc)
		goto err_build_pdr;

	rc = crypto4xx_build_gdr(core_dev->dev);
	if (rc)
1379
		goto err_build_pdr;
J
James Hsiao 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388

	rc = crypto4xx_build_sdr(core_dev->dev);
	if (rc)
		goto err_build_sdr;

	/* Init tasklet for bottom half processing */
	tasklet_init(&core_dev->tasklet, crypto4xx_bh_tasklet_cb,
		     (unsigned long) dev);

1389
	core_dev->dev->ce_base = of_iomap(ofdev->dev.of_node, 0);
J
James Hsiao 已提交
1390 1391
	if (!core_dev->dev->ce_base) {
		dev_err(dev, "failed to of_iomap\n");
1392
		rc = -ENOMEM;
J
James Hsiao 已提交
1393 1394 1395
		goto err_iomap;
	}

1396 1397
	/* Register for Crypto isr, Crypto Engine IRQ */
	core_dev->irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
1398 1399 1400
	rc = request_irq(core_dev->irq, is_revb ?
			 crypto4xx_ce_interrupt_handler_revb :
			 crypto4xx_ce_interrupt_handler, 0,
1401
			 KBUILD_MODNAME, dev);
1402 1403 1404
	if (rc)
		goto err_request_irq;

J
James Hsiao 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413
	/* need to setup pdr, rdr, gdr and sdr before this */
	crypto4xx_hw_init(core_dev->dev);

	/* Register security algorithms with Linux CryptoAPI */
	rc = crypto4xx_register_alg(core_dev->dev, crypto4xx_alg,
			       ARRAY_SIZE(crypto4xx_alg));
	if (rc)
		goto err_start_dev;

1414
	ppc4xx_trng_probe(core_dev);
J
James Hsiao 已提交
1415 1416 1417 1418
	return 0;

err_start_dev:
	free_irq(core_dev->irq, dev);
1419
err_request_irq:
J
James Hsiao 已提交
1420
	irq_dispose_mapping(core_dev->irq);
1421 1422
	iounmap(core_dev->dev->ce_base);
err_iomap:
J
James Hsiao 已提交
1423 1424
	tasklet_kill(&core_dev->tasklet);
err_build_sdr:
1425
	crypto4xx_destroy_sdr(core_dev->dev);
J
James Hsiao 已提交
1426 1427
	crypto4xx_destroy_gdr(core_dev->dev);
err_build_pdr:
1428
	crypto4xx_destroy_pdr(core_dev->dev);
J
James Hsiao 已提交
1429 1430 1431 1432 1433 1434 1435
	kfree(core_dev->dev);
err_alloc_dev:
	kfree(core_dev);

	return rc;
}

1436
static int crypto4xx_remove(struct platform_device *ofdev)
J
James Hsiao 已提交
1437 1438 1439 1440
{
	struct device *dev = &ofdev->dev;
	struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);

1441 1442
	ppc4xx_trng_remove(core_dev);

J
James Hsiao 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	free_irq(core_dev->irq, dev);
	irq_dispose_mapping(core_dev->irq);

	tasklet_kill(&core_dev->tasklet);
	/* Un-register with Linux CryptoAPI */
	crypto4xx_unregister_alg(core_dev->dev);
	/* Free all allocated memory */
	crypto4xx_stop_all(core_dev);

	return 0;
}

1455
static const struct of_device_id crypto4xx_match[] = {
J
James Hsiao 已提交
1456 1457 1458
	{ .compatible      = "amcc,ppc4xx-crypto",},
	{ },
};
1459
MODULE_DEVICE_TABLE(of, crypto4xx_match);
J
James Hsiao 已提交
1460

1461
static struct platform_driver crypto4xx_driver = {
1462
	.driver = {
1463
		.name = KBUILD_MODNAME,
1464 1465
		.of_match_table = crypto4xx_match,
	},
J
James Hsiao 已提交
1466
	.probe		= crypto4xx_probe,
1467
	.remove		= crypto4xx_remove,
J
James Hsiao 已提交
1468 1469
};

1470
module_platform_driver(crypto4xx_driver);
J
James Hsiao 已提交
1471 1472 1473 1474

MODULE_LICENSE("GPL");
MODULE_AUTHOR("James Hsiao <jhsiao@amcc.com>");
MODULE_DESCRIPTION("Driver for AMCC PPC4xx crypto accelerator");