]> Pileus Git - ~andy/linux/blob - drivers/staging/sep/sep_driver.c
Staging: sep: reduce 64 and 32 bit compile warnings
[~andy/linux] / drivers / staging / sep / sep_driver.c
1 /*
2  *
3  *  sep_driver.c - Security Processor Driver main group of functions
4  *
5  *  Copyright(c) 2009,2010 Intel Corporation. All rights reserved.
6  *  Contributions(c) 2009,2010 Discretix. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under the terms of the GNU General Public License as published by the Free
10  *  Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful, but WITHOUT
13  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  *  more details.
16  *
17  *  You should have received a copy of the GNU General Public License along with
18  *  this program; if not, write to the Free Software Foundation, Inc., 59
19  *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  *  CONTACTS:
22  *
23  *  Mark Allyn          mark.a.allyn@intel.com
24  *  Jayant Mangalampalli jayant.mangalampalli@intel.com
25  *
26  *  CHANGES:
27  *
28  *  2009.06.26  Initial publish
29  *  2010.09.14  Upgrade to Medfield
30  *
31  */
32 #define DEBUG
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/miscdevice.h>
36 #include <linux/fs.h>
37 #include <linux/cdev.h>
38 #include <linux/kdev_t.h>
39 #include <linux/mutex.h>
40 #include <linux/sched.h>
41 #include <linux/mm.h>
42 #include <linux/poll.h>
43 #include <linux/wait.h>
44 #include <linux/pci.h>
45 #include <linux/firmware.h>
46 #include <linux/slab.h>
47 #include <linux/ioctl.h>
48 #include <asm/current.h>
49 #include <linux/ioport.h>
50 #include <linux/io.h>
51 #include <linux/interrupt.h>
52 #include <linux/pagemap.h>
53 #include <asm/cacheflush.h>
54 #include <linux/sched.h>
55 #include <linux/delay.h>
56 #include <linux/jiffies.h>
57 #include <linux/rar_register.h>
58
59 #include "../memrar/memrar.h"
60
61 #include "sep_driver_hw_defs.h"
62 #include "sep_driver_config.h"
63 #include "sep_driver_api.h"
64 #include "sep_dev.h"
65
66 /*----------------------------------------
67         DEFINES
68 -----------------------------------------*/
69
70 #define SEP_RAR_IO_MEM_REGION_SIZE 0x40000
71
72 /*--------------------------------------------
73         GLOBAL variables
74 --------------------------------------------*/
75
76 /* Keep this a single static object for now to keep the conversion easy */
77
78 static struct sep_device *sep_dev;
79
80 /**
81  *      sep_load_firmware - copy firmware cache/resident
82  *      @sep: pointer to struct sep_device we are loading
83  *
84  *      This functions copies the cache and resident from their source
85  *      location into destination shared memory.
86  */
87 static int sep_load_firmware(struct sep_device *sep)
88 {
89         const struct firmware *fw;
90         char *cache_name = "cache.image.bin";
91         char *res_name = "resident.image.bin";
92         char *extapp_name = "extapp.image.bin";
93         int error ;
94         unsigned long work1, work2, work3;
95
96         /* Set addresses and load resident */
97         sep->resident_bus = sep->rar_bus;
98         sep->resident_addr = sep->rar_addr;
99
100         error = request_firmware(&fw, res_name, &sep->pdev->dev);
101         if (error) {
102                 dev_warn(&sep->pdev->dev, "can't request resident fw\n");
103                 return error;
104         }
105
106         memcpy(sep->resident_addr, (void *)fw->data, fw->size);
107         sep->resident_size = fw->size;
108         release_firmware(fw);
109
110         dev_dbg(&sep->pdev->dev, "resident virtual is %p\n",
111                 sep->resident_addr);
112         dev_dbg(&sep->pdev->dev, "resident bus is %lx\n",
113                 (unsigned long)sep->resident_bus);
114         dev_dbg(&sep->pdev->dev, "resident size is %08zx\n",
115                 sep->resident_size);
116
117         /* Set addresses for dcache (no loading needed) */
118         work1 = (unsigned long)sep->resident_bus;
119         work2 = (unsigned long)sep->resident_size;
120         work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
121         sep->dcache_bus = (dma_addr_t)work3;
122
123         work1 = (unsigned long)sep->resident_addr;
124         work2 = (unsigned long)sep->resident_size;
125         work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
126         sep->dcache_addr = (void *)work3;
127
128         sep->dcache_size = 1024 * 128;
129
130         /* Set addresses and load cache */
131         sep->cache_bus = sep->dcache_bus + sep->dcache_size;
132         sep->cache_addr = sep->dcache_addr + sep->dcache_size;
133
134         error = request_firmware(&fw, cache_name, &sep->pdev->dev);
135         if (error) {
136                 dev_warn(&sep->pdev->dev, "Unable to request cache firmware\n");
137                 return error;
138         }
139
140         memcpy(sep->cache_addr, (void *)fw->data, fw->size);
141         sep->cache_size = fw->size;
142         release_firmware(fw);
143
144         dev_dbg(&sep->pdev->dev, "cache virtual is %p\n",
145                 sep->cache_addr);
146         dev_dbg(&sep->pdev->dev, "cache bus is %08lx\n",
147                 (unsigned long)sep->cache_bus);
148         dev_dbg(&sep->pdev->dev, "cache size is %08zx\n",
149                 sep->cache_size);
150
151         /* Set addresses and load extapp */
152         sep->extapp_bus = sep->cache_bus + (1024 * 370);
153         sep->extapp_addr = sep->cache_addr + (1024 * 370);
154
155         error = request_firmware(&fw, extapp_name, &sep->pdev->dev);
156         if (error) {
157                 dev_warn(&sep->pdev->dev, "Unable to request extapp firmware\n");
158                 return error;
159         }
160
161         memcpy(sep->extapp_addr, (void *)fw->data, fw->size);
162         sep->extapp_size = fw->size;
163         release_firmware(fw);
164
165         dev_dbg(&sep->pdev->dev, "extapp virtual is %p\n",
166                 sep->extapp_addr);
167         dev_dbg(&sep->pdev->dev, "extapp bus is %08llx\n",
168                 (unsigned long long)sep->extapp_bus);
169         dev_dbg(&sep->pdev->dev, "extapp size is %08zx\n",
170                 sep->extapp_size);
171
172         return error;
173 }
174
175 MODULE_FIRMWARE("sep/cache.image.bin");
176 MODULE_FIRMWARE("sep/resident.image.bin");
177 MODULE_FIRMWARE("sep/extapp.image.bin");
178
179 /**
180  *      sep_dump_message - dump the message that is pending
181  *      @sep: SEP device
182  */
183 static void sep_dump_message(struct sep_device *sep)
184 {
185         int count;
186         u32 *p = sep->shared_addr;
187         for (count = 0; count < 12 * 4; count += 4)
188                 dev_dbg(&sep->pdev->dev, "Word %d of the message is %x\n",
189                                                                 count, *p++);
190 }
191
192 /**
193  *      sep_map_and_alloc_shared_area - allocate shared block
194  *      @sep: security processor
195  *      @size: size of shared area
196  */
197 static int sep_map_and_alloc_shared_area(struct sep_device *sep)
198 {
199         sep->shared_addr = dma_alloc_coherent(&sep->pdev->dev,
200                 sep->shared_size,
201                 &sep->shared_bus, GFP_KERNEL);
202
203         if (!sep->shared_addr) {
204                 dev_warn(&sep->pdev->dev,
205                         "shared memory dma_alloc_coherent failed\n");
206                 return -ENOMEM;
207         }
208         dev_dbg(&sep->pdev->dev,
209                 "shared_addr %zx bytes @%p (bus %llx)\n",
210                                 sep->shared_size, sep->shared_addr,
211                                 (unsigned long long)sep->shared_bus);
212         return 0;
213 }
214
215 /**
216  *      sep_unmap_and_free_shared_area - free shared block
217  *      @sep: security processor
218  */
219 static void sep_unmap_and_free_shared_area(struct sep_device *sep)
220 {
221         dev_dbg(&sep->pdev->dev, "shared area unmap and free\n");
222         dma_free_coherent(&sep->pdev->dev, sep->shared_size,
223                                 sep->shared_addr, sep->shared_bus);
224 }
225
226 /**
227  *      sep_shared_bus_to_virt - convert bus/virt addresses
228  *      @sep: pointer to struct sep_device
229  *      @bus_address: address to convert
230  *
231  *      Returns virtual address inside the shared area according
232  *      to the bus address.
233  */
234 static void *sep_shared_bus_to_virt(struct sep_device *sep,
235                                                 dma_addr_t bus_address)
236 {
237         return sep->shared_addr + (bus_address - sep->shared_bus);
238 }
239
240 /**
241  *      open function for the singleton driver
242  *      @inode_ptr struct inode *
243  *      @file_ptr struct file *
244  *
245  *      Called when the user opens the singleton device interface
246  */
247 static int sep_singleton_open(struct inode *inode_ptr, struct file *file_ptr)
248 {
249         int error = 0;
250         struct sep_device *sep;
251
252         /*
253          * Get the SEP device structure and use it for the
254          * private_data field in filp for other methods
255          */
256         sep = sep_dev;
257
258         file_ptr->private_data = sep;
259
260         dev_dbg(&sep->pdev->dev, "Singleton open for pid %d\n", current->pid);
261
262         dev_dbg(&sep->pdev->dev, "calling test and set for singleton 0\n");
263         if (test_and_set_bit(0, &sep->singleton_access_flag)) {
264                 error = -EBUSY;
265                 goto end_function;
266         }
267
268         dev_dbg(&sep->pdev->dev, "sep_singleton_open end\n");
269 end_function:
270         return error;
271 }
272
273 /**
274  *      sep_open - device open method
275  *      @inode: inode of SEP device
276  *      @filp: file handle to SEP device
277  *
278  *      Open method for the SEP device. Called when userspace opens
279  *      the SEP device node.
280  *
281  *      Returns zero on success otherwise an error code.
282  */
283 static int sep_open(struct inode *inode, struct file *filp)
284 {
285         struct sep_device *sep;
286
287         /*
288          * Get the SEP device structure and use it for the
289          * private_data field in filp for other methods
290          */
291         sep = sep_dev;
292         filp->private_data = sep;
293
294         dev_dbg(&sep->pdev->dev, "Open for pid %d\n", current->pid);
295
296         /* Anyone can open; locking takes place at transaction level */
297         return 0;
298 }
299
300 /**
301  *      sep_singleton_release - close a SEP singleton device
302  *      @inode: inode of SEP device
303  *      @filp: file handle being closed
304  *
305  *      Called on the final close of a SEP device. As the open protects against
306  *      multiple simultaenous opens that means this method is called when the
307  *      final reference to the open handle is dropped.
308  */
309 static int sep_singleton_release(struct inode *inode, struct file *filp)
310 {
311         struct sep_device *sep = filp->private_data;
312
313         dev_dbg(&sep->pdev->dev, "Singleton release for pid %d\n",
314                                                         current->pid);
315         clear_bit(0, &sep->singleton_access_flag);
316         return 0;
317 }
318
319 /**
320  *      sep_request_daemonopen - request daemon open method
321  *      @inode: inode of SEP device
322  *      @filp: file handle to SEP device
323  *
324  *      Open method for the SEP request daemon. Called when
325  *      request daemon in userspace opens the SEP device node.
326  *
327  *      Returns zero on success otherwise an error code.
328  */
329 static int sep_request_daemon_open(struct inode *inode, struct file *filp)
330 {
331         struct sep_device *sep = sep_dev;
332         int error = 0;
333
334         filp->private_data = sep;
335
336         dev_dbg(&sep->pdev->dev, "Request daemon open for pid %d\n",
337                 current->pid);
338
339         /* There is supposed to be only one request daemon */
340         dev_dbg(&sep->pdev->dev, "calling test and set for req_dmon open 0\n");
341         if (test_and_set_bit(0, &sep->request_daemon_open))
342                 error = -EBUSY;
343         return error;
344 }
345
346 /**
347  *      sep_request_daemon_release - close a SEP daemon
348  *      @inode: inode of SEP device
349  *      @filp: file handle being closed
350  *
351  *      Called on the final close of a SEP daemon.
352  */
353 static int sep_request_daemon_release(struct inode *inode, struct file *filp)
354 {
355         struct sep_device *sep = filp->private_data;
356
357         dev_dbg(&sep->pdev->dev, "Reques daemon release for pid %d\n",
358                 current->pid);
359
360         /* Clear the request_daemon_open flag */
361         clear_bit(0, &sep->request_daemon_open);
362         return 0;
363 }
364
365 /**
366  *      sep_req_daemon_send_reply_command_handler - poke the SEP
367  *      @sep: struct sep_device *
368  *
369  *      This function raises interrupt to SEPm that signals that is has a
370  *      new command from HOST
371  */
372 static int sep_req_daemon_send_reply_command_handler(struct sep_device *sep)
373 {
374         unsigned long lck_flags;
375
376         dev_dbg(&sep->pdev->dev,
377                 "sep_req_daemon_send_reply_command_handler start\n");
378
379         sep_dump_message(sep);
380
381         /* Counters are lockable region */
382         spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
383         sep->send_ct++;
384         sep->reply_ct++;
385
386         /* Send the interrupt to SEP */
387         sep_write_reg(sep, HW_HOST_HOST_SEP_GPR2_REG_ADDR, sep->send_ct);
388         sep->send_ct++;
389
390         spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
391
392         dev_dbg(&sep->pdev->dev,
393                 "sep_req_daemon_send_reply send_ct %lx reply_ct %lx\n",
394                 sep->send_ct, sep->reply_ct);
395
396         dev_dbg(&sep->pdev->dev,
397                 "sep_req_daemon_send_reply_command_handler end\n");
398
399         return 0;
400 }
401
402
403 /**
404  *      sep_free_dma_table_data_handler - free DMA table
405  *      @sep: pointere to struct sep_device
406  *
407  *      Handles the request to  free DMA table for synchronic actions
408  */
409 static int sep_free_dma_table_data_handler(struct sep_device *sep)
410 {
411         int count;
412         int dcb_counter;
413         /* Pointer to the current dma_resource struct */
414         struct sep_dma_resource *dma;
415
416         dev_dbg(&sep->pdev->dev, "sep_free_dma_table_data_handler start\n");
417
418         for (dcb_counter = 0; dcb_counter < sep->nr_dcb_creat; dcb_counter++) {
419                 dma = &sep->dma_res_arr[dcb_counter];
420
421                 /* Unmap and free input map array */
422                 if (dma->in_map_array) {
423                         for (count = 0; count < dma->in_num_pages; count++) {
424                                 dma_unmap_page(&sep->pdev->dev,
425                                         dma->in_map_array[count].dma_addr,
426                                         dma->in_map_array[count].size,
427                                         DMA_TO_DEVICE);
428                         }
429                         kfree(dma->in_map_array);
430                 }
431
432                 /* Unmap output map array, DON'T free it yet */
433                 if (dma->out_map_array) {
434                         for (count = 0; count < dma->out_num_pages; count++) {
435                                 dma_unmap_page(&sep->pdev->dev,
436                                         dma->out_map_array[count].dma_addr,
437                                         dma->out_map_array[count].size,
438                                         DMA_FROM_DEVICE);
439                         }
440                         kfree(dma->out_map_array);
441                 }
442
443                 /* Free page cache for output */
444                 if (dma->in_page_array) {
445                         for (count = 0; count < dma->in_num_pages; count++) {
446                                 flush_dcache_page(dma->in_page_array[count]);
447                                 page_cache_release(dma->in_page_array[count]);
448                         }
449                         kfree(dma->in_page_array);
450                 }
451
452                 if (dma->out_page_array) {
453                         for (count = 0; count < dma->out_num_pages; count++) {
454                                 if (!PageReserved(dma->out_page_array[count]))
455                                         SetPageDirty(dma->out_page_array[count]);
456                                 flush_dcache_page(dma->out_page_array[count]);
457                                 page_cache_release(dma->out_page_array[count]);
458                         }
459                         kfree(dma->out_page_array);
460                 }
461
462                 /* Reset all the values */
463                 dma->in_page_array = NULL;
464                 dma->out_page_array = NULL;
465                 dma->in_num_pages = 0;
466                 dma->out_num_pages = 0;
467                 dma->in_map_array = NULL;
468                 dma->out_map_array = NULL;
469                 dma->in_map_num_entries = 0;
470                 dma->out_map_num_entries = 0;
471         }
472
473         sep->nr_dcb_creat = 0;
474         sep->num_lli_tables_created = 0;
475
476         dev_dbg(&sep->pdev->dev, "sep_free_dma_table_data_handler end\n");
477         return 0;
478 }
479
480 /**
481  *      sep_request_daemon_mmap - maps the shared area to user space
482  *      @filp: pointer to struct file
483  *      @vma: pointer to vm_area_struct
484  *
485  *      Called by the kernel when the daemon attempts an mmap() syscall
486  *      using our handle.
487  */
488 static int sep_request_daemon_mmap(struct file  *filp,
489         struct vm_area_struct  *vma)
490 {
491         struct sep_device *sep = filp->private_data;
492         dma_addr_t bus_address;
493         int error = 0;
494
495         dev_dbg(&sep->pdev->dev, "daemon mmap start\n");
496
497         if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
498                 error = -EINVAL;
499                 goto end_function;
500         }
501
502         /* Get physical address */
503         bus_address = sep->shared_bus;
504
505         dev_dbg(&sep->pdev->dev, "bus_address is %08lx\n",
506                                         (unsigned long)bus_address);
507
508         if (remap_pfn_range(vma, vma->vm_start, bus_address >> PAGE_SHIFT,
509                 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
510
511                 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
512                 error = -EAGAIN;
513                 goto end_function;
514         }
515
516 end_function:
517         dev_dbg(&sep->pdev->dev, "daemon mmap end\n");
518         return error;
519 }
520
521 /**
522  *      sep_request_daemon_poll - poll implementation
523  *      @sep: struct sep_device * for current SEP device
524  *      @filp: struct file * for open file
525  *      @wait: poll_table * for poll
526  *
527  *      Called when our device is part of a poll() or select() syscall
528  */
529 static unsigned int sep_request_daemon_poll(struct file *filp,
530         poll_table  *wait)
531 {
532         u32     mask = 0;
533         /* GPR2 register */
534         u32     retval2;
535         unsigned long lck_flags;
536         struct sep_device *sep = filp->private_data;
537
538         dev_dbg(&sep->pdev->dev, "daemon poll: start\n");
539
540         poll_wait(filp, &sep->event_request_daemon, wait);
541
542         dev_dbg(&sep->pdev->dev, "daemon poll: send_ct is %lx reply ct is %lx\n",
543                                                 sep->send_ct, sep->reply_ct);
544
545         spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
546         /* Check if the data is ready */
547         if (sep->send_ct == sep->reply_ct) {
548                 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
549
550                 retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
551                 dev_dbg(&sep->pdev->dev,
552                         "daemon poll: data check (GPR2) is %x\n", retval2);
553
554                 /* Check if PRINT request */
555                 if ((retval2 >> 30) & 0x1) {
556                         dev_dbg(&sep->pdev->dev, "daemon poll: PRINTF request in\n");
557                         mask |= POLLIN;
558                         goto end_function;
559                 }
560                 /* Check if NVS request */
561                 if (retval2 >> 31) {
562                         dev_dbg(&sep->pdev->dev, "daemon poll: NVS request in\n");
563                         mask |= POLLPRI | POLLWRNORM;
564                 }
565         } else {
566                 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
567                 dev_dbg(&sep->pdev->dev,
568                         "daemon poll: no reply received; returning 0\n");
569                 mask = 0;
570         }
571 end_function:
572         dev_dbg(&sep->pdev->dev, "daemon poll: exit\n");
573         return mask;
574 }
575
576 /**
577  *      sep_release - close a SEP device
578  *      @inode: inode of SEP device
579  *      @filp: file handle being closed
580  *
581  *      Called on the final close of a SEP device.
582  */
583 static int sep_release(struct inode *inode, struct file *filp)
584 {
585         struct sep_device *sep = filp->private_data;
586
587         dev_dbg(&sep->pdev->dev, "Release for pid %d\n", current->pid);
588
589         mutex_lock(&sep->sep_mutex);
590         /* Is this the process that has a transaction open?
591          * If so, lets reset pid_doing_transaction to 0 and
592          * clear the in use flags, and then wake up sep_event
593          * so that other processes can do transactions
594          */
595         dev_dbg(&sep->pdev->dev, "waking up event and mmap_event\n");
596         if (sep->pid_doing_transaction == current->pid) {
597                 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
598                 clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
599                 sep_free_dma_table_data_handler(sep);
600                 wake_up(&sep->event);
601                 sep->pid_doing_transaction = 0;
602         }
603
604         mutex_unlock(&sep->sep_mutex);
605         return 0;
606 }
607
608 /**
609  *      sep_mmap -  maps the shared area to user space
610  *      @filp: pointer to struct file
611  *      @vma: pointer to vm_area_struct
612  *
613  *      Called on an mmap of our space via the normal SEP device
614  */
615 static int sep_mmap(struct file *filp, struct vm_area_struct *vma)
616 {
617         dma_addr_t bus_addr;
618         struct sep_device *sep = filp->private_data;
619         unsigned long error = 0;
620
621         dev_dbg(&sep->pdev->dev, "mmap start\n");
622
623         /* Set the transaction busy (own the device) */
624         wait_event_interruptible(sep->event,
625                 test_and_set_bit(SEP_MMAP_LOCK_BIT,
626                 &sep->in_use_flags) == 0);
627
628         if (signal_pending(current)) {
629                 error = -EINTR;
630                 goto end_function_with_error;
631         }
632         /*
633          * The pid_doing_transaction indicates that this process
634          * now owns the facilities to performa a transaction with
635          * the SEP. While this process is performing a transaction,
636          * no other process who has the SEP device open can perform
637          * any transactions. This method allows more than one process
638          * to have the device open at any given time, which provides
639          * finer granularity for device utilization by multiple
640          * processes.
641          */
642         mutex_lock(&sep->sep_mutex);
643         sep->pid_doing_transaction = current->pid;
644         mutex_unlock(&sep->sep_mutex);
645
646         /* Zero the pools and the number of data pool alocation pointers */
647         sep->data_pool_bytes_allocated = 0;
648         sep->num_of_data_allocations = 0;
649
650         /*
651          * Check that the size of the mapped range is as the size of the message
652          * shared area
653          */
654         if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
655                 error = -EINVAL;
656                 goto end_function_with_error;
657         }
658
659         dev_dbg(&sep->pdev->dev, "shared_addr is %p\n", sep->shared_addr);
660
661         /* Get bus address */
662         bus_addr = sep->shared_bus;
663
664         dev_dbg(&sep->pdev->dev,
665                 "bus_address is %lx\n", (unsigned long)bus_addr);
666
667         if (remap_pfn_range(vma, vma->vm_start, bus_addr >> PAGE_SHIFT,
668                 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
669                 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
670                 error = -EAGAIN;
671                 goto end_function_with_error;
672         }
673         dev_dbg(&sep->pdev->dev, "mmap end\n");
674         goto end_function;
675
676 end_function_with_error:
677         /* Clear the bit */
678         clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
679         mutex_lock(&sep->sep_mutex);
680         sep->pid_doing_transaction = 0;
681         mutex_unlock(&sep->sep_mutex);
682
683         /* Raise event for stuck contextes */
684
685         dev_warn(&sep->pdev->dev, "mmap error - waking up event\n");
686         wake_up(&sep->event);
687
688 end_function:
689         return error;
690 }
691
692 /**
693  *      sep_poll - poll handler
694  *      @filp: pointer to struct file
695  *      @wait: pointer to poll_table
696  *
697  *      Called by the OS when the kernel is asked to do a poll on
698  *      a SEP file handle.
699  */
700 static unsigned int sep_poll(struct file *filp, poll_table *wait)
701 {
702         u32 mask = 0;
703         u32 retval = 0;
704         u32 retval2 = 0;
705         unsigned long lck_flags;
706
707         struct sep_device *sep = filp->private_data;
708
709         dev_dbg(&sep->pdev->dev, "poll: start\n");
710
711         /* Am I the process that owns the transaction? */
712         mutex_lock(&sep->sep_mutex);
713         if (current->pid != sep->pid_doing_transaction) {
714                 dev_warn(&sep->pdev->dev, "poll; wrong pid\n");
715                 mask = POLLERR;
716                 mutex_unlock(&sep->sep_mutex);
717                 goto end_function;
718         }
719         mutex_unlock(&sep->sep_mutex);
720
721         /* Check if send command or send_reply were activated previously */
722         if (!test_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
723                 dev_warn(&sep->pdev->dev, "poll; lock bit set\n");
724                 mask = POLLERR;
725                 goto end_function;
726         }
727
728         /* Add the event to the polling wait table */
729         dev_dbg(&sep->pdev->dev, "poll: calling wait sep_event\n");
730
731         poll_wait(filp, &sep->event, wait);
732
733         dev_dbg(&sep->pdev->dev, "poll: send_ct is %lx reply ct is %lx\n",
734                 sep->send_ct, sep->reply_ct);
735
736         /* Check if error occured during poll */
737         retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
738         if (retval2 != 0x0) {
739                 dev_warn(&sep->pdev->dev, "poll; poll error %x\n", retval2);
740                 mask |= POLLERR;
741                 goto end_function;
742         }
743
744         spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
745
746         if (sep->send_ct == sep->reply_ct) {
747                 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
748                 retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
749                 dev_dbg(&sep->pdev->dev, "poll: data ready check (GPR2)  %x\n",
750                         retval);
751
752                 /* Check if printf request  */
753                 if ((retval >> 30) & 0x1) {
754                         dev_dbg(&sep->pdev->dev, "poll: SEP printf request\n");
755                         wake_up(&sep->event_request_daemon);
756                         goto end_function;
757                 }
758
759                 /* Check if the this is SEP reply or request */
760                 if (retval >> 31) {
761                         dev_dbg(&sep->pdev->dev, "poll: SEP request\n");
762                         wake_up(&sep->event_request_daemon);
763                 } else {
764                         dev_dbg(&sep->pdev->dev, "poll: normal return\n");
765                         /* In case it is again by send_reply_comand */
766                         clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
767                         sep_dump_message(sep);
768                         dev_dbg(&sep->pdev->dev,
769                                 "poll; SEP reply POLLIN | POLLRDNORM\n");
770                         mask |= POLLIN | POLLRDNORM;
771                 }
772         } else {
773                 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
774                 dev_dbg(&sep->pdev->dev,
775                         "poll; no reply received; returning mask of 0\n");
776                 mask = 0;
777         }
778
779 end_function:
780         dev_dbg(&sep->pdev->dev, "poll: end\n");
781         return mask;
782 }
783
784 /**
785  *      sep_time_address - address in SEP memory of time
786  *      @sep: SEP device we want the address from
787  *
788  *      Return the address of the two dwords in memory used for time
789  *      setting.
790  */
791 static u32 *sep_time_address(struct sep_device *sep)
792 {
793         return sep->shared_addr + SEP_DRIVER_SYSTEM_TIME_MEMORY_OFFSET_IN_BYTES;
794 }
795
796 /**
797  *      sep_set_time - set the SEP time
798  *      @sep: the SEP we are setting the time for
799  *
800  *      Calculates time and sets it at the predefined address.
801  *      Called with the SEP mutex held.
802  */
803 static unsigned long sep_set_time(struct sep_device *sep)
804 {
805         struct timeval time;
806         u32 *time_addr; /* Address of time as seen by the kernel */
807
808
809         dev_dbg(&sep->pdev->dev, "sep_set_time start\n");
810
811         do_gettimeofday(&time);
812
813         /* Set value in the SYSTEM MEMORY offset */
814         time_addr = sep_time_address(sep);
815
816         time_addr[0] = SEP_TIME_VAL_TOKEN;
817         time_addr[1] = time.tv_sec;
818
819         dev_dbg(&sep->pdev->dev, "time.tv_sec is %lu\n", time.tv_sec);
820         dev_dbg(&sep->pdev->dev, "time_addr is %p\n", time_addr);
821         dev_dbg(&sep->pdev->dev, "sep->shared_addr is %p\n", sep->shared_addr);
822
823         return time.tv_sec;
824 }
825
826 /**
827  *      sep_set_caller_id_handler - insert caller id entry
828  *      @sep: SEP device
829  *      @arg: pointer to struct caller_id_struct
830  *
831  *      Inserts the data into the caller id table. Note that this function
832  *      falls under the ioctl lock
833  */
834 static int sep_set_caller_id_handler(struct sep_device *sep, unsigned long arg)
835 {
836         void __user *hash;
837         int   error = 0;
838         int   i;
839         struct caller_id_struct command_args;
840
841         dev_dbg(&sep->pdev->dev, "sep_set_caller_id_handler start\n");
842
843         for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
844                 if (sep->caller_id_table[i].pid == 0)
845                         break;
846         }
847
848         if (i == SEP_CALLER_ID_TABLE_NUM_ENTRIES) {
849                 dev_warn(&sep->pdev->dev, "no more caller id entries left\n");
850                 dev_warn(&sep->pdev->dev, "maximum number is %d\n",
851                                         SEP_CALLER_ID_TABLE_NUM_ENTRIES);
852                 error = -EUSERS;
853                 goto end_function;
854         }
855
856         /* Copy the data */
857         if (copy_from_user(&command_args, (void __user *)arg,
858                 sizeof(command_args))) {
859                 error = -EFAULT;
860                 goto end_function;
861         }
862
863         hash = (void __user *)(unsigned long)command_args.callerIdAddress;
864
865         if (!command_args.pid || !command_args.callerIdSizeInBytes) {
866                 error = -EINVAL;
867                 goto end_function;
868         }
869
870         dev_dbg(&sep->pdev->dev, "pid is %x\n", command_args.pid);
871         dev_dbg(&sep->pdev->dev, "callerIdSizeInBytes is %x\n",
872                 command_args.callerIdSizeInBytes);
873
874         if (command_args.callerIdSizeInBytes >
875                                         SEP_CALLER_ID_HASH_SIZE_IN_BYTES) {
876                 error = -EMSGSIZE;
877                 goto end_function;
878         }
879
880         sep->caller_id_table[i].pid = command_args.pid;
881
882         if (copy_from_user(sep->caller_id_table[i].callerIdHash,
883                 hash, command_args.callerIdSizeInBytes))
884                 error = -EFAULT;
885 end_function:
886         dev_dbg(&sep->pdev->dev, "sep_set_caller_id_handler end\n");
887         return error;
888 }
889
890 /**
891  *      sep_set_current_caller_id - set the caller id
892  *      @sep: pointer to struct_sep_device
893  *
894  *      Set the caller ID (if it exists) to the SEP. Note that this
895  *      function falls under the ioctl lock
896  */
897 static int sep_set_current_caller_id(struct sep_device *sep)
898 {
899         int i;
900
901         dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id start\n");
902         dev_dbg(&sep->pdev->dev, "current process is %d\n", current->pid);
903
904         /* Zero the previous value */
905         memset(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
906                                         0, SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
907
908         for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
909                 if (sep->caller_id_table[i].pid == current->pid) {
910                         dev_dbg(&sep->pdev->dev, "Caller Id found\n");
911
912                         memcpy(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
913                                 (void *)(sep->caller_id_table[i].callerIdHash),
914                                 SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
915                         break;
916                 }
917         }
918         dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id end\n");
919         return 0;
920 }
921
922 /**
923  *      sep_send_command_handler - kick off a command
924  *      @sep: SEP being signalled
925  *
926  *      This function raises interrupt to SEP that signals that is has a new
927  *      command from the host
928  *
929  *      Note that this function does fall under the ioctl lock
930  */
931 static int sep_send_command_handler(struct sep_device *sep)
932 {
933         unsigned long lck_flags;
934         int error = 0;
935
936         dev_dbg(&sep->pdev->dev, "sep_send_command_handler start\n");
937
938         if (test_and_set_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
939                 error = -EPROTO;
940                 goto end_function;
941         }
942         sep_set_time(sep);
943
944         sep_set_current_caller_id(sep);
945
946         sep_dump_message(sep);
947
948         /* Update counter */
949         spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
950         sep->send_ct++;
951         spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
952
953         dev_dbg(&sep->pdev->dev,
954                 "sep_send_command_handler send_ct %lx reply_ct %lx\n",
955                                                 sep->send_ct, sep->reply_ct);
956
957         /* Send interrupt to SEP */
958         sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
959
960 end_function:
961         dev_dbg(&sep->pdev->dev, "sep_send_command_handler end\n");
962         return error;
963 }
964
965 /**
966  *      sep_allocate_data_pool_memory_handler -allocate pool memory
967  *      @sep: pointer to struct sep_device
968  *      @arg: pointer to struct alloc_struct
969  *
970  *      This function handles the allocate data pool memory request
971  *      This function returns calculates the bus address of the
972  *      allocated memory, and the offset of this area from the mapped address.
973  *      Therefore, the FVOs in user space can calculate the exact virtual
974  *      address of this allocated memory
975  */
976 static int sep_allocate_data_pool_memory_handler(struct sep_device *sep,
977         unsigned long arg)
978 {
979         int error = 0;
980         struct alloc_struct command_args;
981
982         /* Holds the allocated buffer address in the system memory pool */
983         u32 *token_addr;
984
985         dev_dbg(&sep->pdev->dev,
986                 "sep_allocate_data_pool_memory_handler start\n");
987
988         if (copy_from_user(&command_args, (void __user *)arg,
989                                         sizeof(struct alloc_struct))) {
990                 error = -EFAULT;
991                 goto end_function;
992         }
993
994         /* Allocate memory */
995         if ((sep->data_pool_bytes_allocated + command_args.num_bytes) >
996                 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {
997                 error = -ENOMEM;
998                 goto end_function;
999         }
1000
1001         dev_dbg(&sep->pdev->dev,
1002                 "bytes_allocated: %x\n", (int)sep->data_pool_bytes_allocated);
1003         dev_dbg(&sep->pdev->dev,
1004                 "offset: %x\n", SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES);
1005         /* Set the virtual and bus address */
1006         command_args.offset = SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
1007                 sep->data_pool_bytes_allocated;
1008
1009         dev_dbg(&sep->pdev->dev,
1010                 "command_args.offset: %x\n", command_args.offset);
1011
1012         /* Place in the shared area that is known by the SEP */
1013         token_addr = (u32 *)(sep->shared_addr +
1014                 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES +
1015                 (sep->num_of_data_allocations)*2*sizeof(u32));
1016
1017         dev_dbg(&sep->pdev->dev, "allocation offset: %x\n",
1018                 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES);
1019         dev_dbg(&sep->pdev->dev, "data pool token addr is %p\n", token_addr);
1020
1021         token_addr[0] = SEP_DATA_POOL_POINTERS_VAL_TOKEN;
1022         token_addr[1] = (u32)sep->shared_bus +
1023                 SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
1024                 sep->data_pool_bytes_allocated;
1025
1026         dev_dbg(&sep->pdev->dev, "data pool token [0] %x\n", token_addr[0]);
1027         dev_dbg(&sep->pdev->dev, "data pool token [1] %x\n", token_addr[1]);
1028
1029         /* Write the memory back to the user space */
1030         error = copy_to_user((void *)arg, (void *)&command_args,
1031                 sizeof(struct alloc_struct));
1032         if (error) {
1033                 error = -EFAULT;
1034                 goto end_function;
1035         }
1036
1037         /* Update the allocation */
1038         sep->data_pool_bytes_allocated += command_args.num_bytes;
1039         sep->num_of_data_allocations += 1;
1040
1041         dev_dbg(&sep->pdev->dev, "data_allocations %d\n",
1042                 sep->num_of_data_allocations);
1043         dev_dbg(&sep->pdev->dev, "bytes allocated  %d\n",
1044                 (int)sep->data_pool_bytes_allocated);
1045
1046 end_function:
1047         dev_dbg(&sep->pdev->dev, "sep_allocate_data_pool_memory_handler end\n");
1048         return error;
1049 }
1050
1051 /**
1052  *      sep_lock_kernel_pages - map kernel pages for DMA
1053  *      @sep: pointer to struct sep_device
1054  *      @kernel_virt_addr: address of data buffer in kernel
1055  *      @data_size: size of data
1056  *      @lli_array_ptr: lli array
1057  *      @in_out_flag: input into device or output from device
1058  *
1059  *      This function locks all the physical pages of the kernel virtual buffer
1060  *      and construct a basic lli  array, where each entry holds the physical
1061  *      page address and the size that application data holds in this page
1062  *      This function is used only during kernel crypto mod calls from within
1063  *      the kernel (when ioctl is not used)
1064  */
1065 static int sep_lock_kernel_pages(struct sep_device *sep,
1066         unsigned long kernel_virt_addr,
1067         u32 data_size,
1068         struct sep_lli_entry **lli_array_ptr,
1069         int in_out_flag)
1070
1071 {
1072         int error = 0;
1073         /* Array of lli */
1074         struct sep_lli_entry *lli_array;
1075         /* Map array */
1076         struct sep_dma_map *map_array;
1077
1078         dev_dbg(&sep->pdev->dev, "sep_lock_kernel_pages start\n");
1079         dev_dbg(&sep->pdev->dev, "kernel_virt_addr is %08lx\n",
1080                                 (unsigned long)kernel_virt_addr);
1081         dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1082
1083         lli_array = kmalloc(sizeof(struct sep_lli_entry), GFP_ATOMIC);
1084         if (!lli_array) {
1085                 error = -ENOMEM;
1086                 goto end_function;
1087         }
1088         map_array = kmalloc(sizeof(struct sep_dma_map), GFP_ATOMIC);
1089         if (!map_array) {
1090                 error = -ENOMEM;
1091                 goto end_function_with_error;
1092         }
1093
1094         map_array[0].dma_addr =
1095                 dma_map_single(&sep->pdev->dev, (void *)kernel_virt_addr,
1096                 data_size, DMA_BIDIRECTIONAL);
1097         map_array[0].size = data_size;
1098
1099
1100         /*
1101          * Set the start address of the first page - app data may start not at
1102          * the beginning of the page
1103          */
1104         lli_array[0].bus_address = (u32)map_array[0].dma_addr;
1105         lli_array[0].block_size = map_array[0].size;
1106
1107         dev_dbg(&sep->pdev->dev,
1108         "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1109                 (unsigned long)lli_array[0].bus_address,
1110                 lli_array[0].block_size);
1111
1112         /* Set the output parameters */
1113         if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1114                 *lli_array_ptr = lli_array;
1115                 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 1;
1116                 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1117                 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1118                 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries = 1;
1119         } else {
1120                 *lli_array_ptr = lli_array;
1121                 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = 1;
1122                 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
1123                 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1124                 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries = 1;
1125         }
1126         goto end_function;
1127
1128 end_function_with_error:
1129         kfree(lli_array);
1130
1131 end_function:
1132         dev_dbg(&sep->pdev->dev, "sep_lock_kernel_pages end\n");
1133         return error;
1134 }
1135
1136 /**
1137  *      sep_lock_user_pages - lock and map user pages for DMA
1138  *      @sep: pointer to struct sep_device
1139  *      @app_virt_addr: user memory data buffer
1140  *      @data_size: size of data buffer
1141  *      @lli_array_ptr: lli array
1142  *      @in_out_flag: input or output to device
1143  *
1144  *      This function locks all the physical pages of the application
1145  *      virtual buffer and construct a basic lli  array, where each entry
1146  *      holds the physical page address and the size that application
1147  *      data holds in this physical pages
1148  */
1149 static int sep_lock_user_pages(struct sep_device *sep,
1150         u32 app_virt_addr,
1151         u32 data_size,
1152         struct sep_lli_entry **lli_array_ptr,
1153         int in_out_flag)
1154
1155 {
1156         int error = 0;
1157         u32 count;
1158         int result;
1159         /* The the page of the end address of the user space buffer */
1160         u32 end_page;
1161         /* The page of the start address of the user space buffer */
1162         u32 start_page;
1163         /* The range in pages */
1164         u32 num_pages;
1165         /* Array of pointers to page */
1166         struct page **page_array;
1167         /* Array of lli */
1168         struct sep_lli_entry *lli_array;
1169         /* Map array */
1170         struct sep_dma_map *map_array;
1171         /* Direction of the DMA mapping for locked pages */
1172         enum dma_data_direction dir;
1173
1174         dev_dbg(&sep->pdev->dev, "sep_lock_user_pages start\n");
1175
1176         /* Set start and end pages  and num pages */
1177         end_page = (app_virt_addr + data_size - 1) >> PAGE_SHIFT;
1178         start_page = app_virt_addr >> PAGE_SHIFT;
1179         num_pages = end_page - start_page + 1;
1180
1181         dev_dbg(&sep->pdev->dev, "app_virt_addr is %x\n", app_virt_addr);
1182         dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1183         dev_dbg(&sep->pdev->dev, "start_page is %x\n", start_page);
1184         dev_dbg(&sep->pdev->dev, "end_page is %x\n", end_page);
1185         dev_dbg(&sep->pdev->dev, "num_pages is %x\n", num_pages);
1186
1187         dev_dbg(&sep->pdev->dev, "starting page_array malloc\n");
1188
1189         /* Allocate array of pages structure pointers */
1190         page_array = kmalloc(sizeof(struct page *) * num_pages, GFP_ATOMIC);
1191         if (!page_array) {
1192                 error = -ENOMEM;
1193                 goto end_function;
1194         }
1195         map_array = kmalloc(sizeof(struct sep_dma_map) * num_pages, GFP_ATOMIC);
1196         if (!map_array) {
1197                 dev_warn(&sep->pdev->dev, "kmalloc for map_array failed\n");
1198                 error = -ENOMEM;
1199                 goto end_function_with_error1;
1200         }
1201
1202         lli_array = kmalloc(sizeof(struct sep_lli_entry) * num_pages,
1203                 GFP_ATOMIC);
1204
1205         if (!lli_array) {
1206                 dev_warn(&sep->pdev->dev, "kmalloc for lli_array failed\n");
1207                 error = -ENOMEM;
1208                 goto end_function_with_error2;
1209         }
1210
1211         dev_dbg(&sep->pdev->dev, "starting get_user_pages\n");
1212
1213         /* Convert the application virtual address into a set of physical */
1214         down_read(&current->mm->mmap_sem);
1215         result = get_user_pages(current, current->mm, app_virt_addr,
1216                 num_pages,
1217                 ((in_out_flag == SEP_DRIVER_IN_FLAG) ? 0 : 1),
1218                 0, page_array, NULL);
1219
1220         up_read(&current->mm->mmap_sem);
1221
1222         /* Check the number of pages locked - if not all then exit with error */
1223         if (result != num_pages) {
1224                 dev_warn(&sep->pdev->dev,
1225                         "not all pages locked by get_user_pages\n");
1226                 error = -ENOMEM;
1227                 goto end_function_with_error3;
1228         }
1229
1230         dev_dbg(&sep->pdev->dev, "get_user_pages succeeded\n");
1231
1232         /* Set direction */
1233         if (in_out_flag == SEP_DRIVER_IN_FLAG)
1234                 dir = DMA_TO_DEVICE;
1235         else
1236                 dir = DMA_FROM_DEVICE;
1237
1238         /*
1239          * Fill the array using page array data and
1240          * map the pages - this action will also flush the cache as needed
1241          */
1242         for (count = 0; count < num_pages; count++) {
1243                 /* Fill the map array */
1244                 map_array[count].dma_addr =
1245                         dma_map_page(&sep->pdev->dev, page_array[count],
1246                         0, PAGE_SIZE, /*dir*/DMA_BIDIRECTIONAL);
1247
1248                 map_array[count].size = PAGE_SIZE;
1249
1250                 /* Fill the lli array entry */
1251                 lli_array[count].bus_address = (u32)map_array[count].dma_addr;
1252                 lli_array[count].block_size = PAGE_SIZE;
1253
1254                 dev_warn(&sep->pdev->dev, "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1255                         count, (unsigned long)lli_array[count].bus_address,
1256                         count, lli_array[count].block_size);
1257         }
1258
1259         /* Check the offset for the first page */
1260         lli_array[0].bus_address =
1261                 lli_array[0].bus_address + (app_virt_addr & (~PAGE_MASK));
1262
1263         /* Check that not all the data is in the first page only */
1264         if ((PAGE_SIZE - (app_virt_addr & (~PAGE_MASK))) >= data_size)
1265                 lli_array[0].block_size = data_size;
1266         else
1267                 lli_array[0].block_size =
1268                         PAGE_SIZE - (app_virt_addr & (~PAGE_MASK));
1269
1270         dev_dbg(&sep->pdev->dev,
1271                 "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1272                 (unsigned long)lli_array[count].bus_address,
1273                 lli_array[count].block_size);
1274
1275         /* Check the size of the last page */
1276         if (num_pages > 1) {
1277                 lli_array[num_pages - 1].block_size =
1278                         (app_virt_addr + data_size) & (~PAGE_MASK);
1279
1280                 dev_warn(&sep->pdev->dev,
1281                         "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1282                         num_pages - 1,
1283                         (unsigned long)lli_array[count].bus_address,
1284                         num_pages - 1,
1285                         lli_array[count].block_size);
1286         }
1287
1288         /* Set output params acording to the in_out flag */
1289         if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1290                 *lli_array_ptr = lli_array;
1291                 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = num_pages;
1292                 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = page_array;
1293                 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1294                 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries =
1295                                                                 num_pages;
1296         } else {
1297                 *lli_array_ptr = lli_array;
1298                 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = num_pages;
1299                 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array =
1300                                                                 page_array;
1301                 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1302                 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries =
1303                                                                 num_pages;
1304         }
1305         goto end_function;
1306
1307 end_function_with_error3:
1308         /* Free lli array */
1309         kfree(lli_array);
1310
1311 end_function_with_error2:
1312         kfree(map_array);
1313
1314 end_function_with_error1:
1315         /* Free page array */
1316         kfree(page_array);
1317
1318 end_function:
1319         dev_dbg(&sep->pdev->dev, "sep_lock_user_pages end\n");
1320         return error;
1321 }
1322
1323 /**
1324  *      u32 sep_calculate_lli_table_max_size - size the LLI table
1325  *      @sep: pointer to struct sep_device
1326  *      @lli_in_array_ptr
1327  *      @num_array_entries
1328  *      @last_table_flag
1329  *
1330  *      This function calculates the size of data that can be inserted into
1331  *      the lli table from this array, such that either the table is full
1332  *      (all entries are entered), or there are no more entries in the
1333  *      lli array
1334  */
1335 static u32 sep_calculate_lli_table_max_size(struct sep_device *sep,
1336         struct sep_lli_entry *lli_in_array_ptr,
1337         u32 num_array_entries,
1338         u32 *last_table_flag)
1339 {
1340         u32 counter;
1341         /* Table data size */
1342         u32 table_data_size = 0;
1343         /* Data size for the next table */
1344         u32 next_table_data_size;
1345
1346         *last_table_flag = 0;
1347
1348         /*
1349          * Calculate the data in the out lli table till we fill the whole
1350          * table or till the data has ended
1351          */
1352         for (counter = 0;
1353                 (counter < (SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP - 1)) &&
1354                         (counter < num_array_entries); counter++)
1355                 table_data_size += lli_in_array_ptr[counter].block_size;
1356
1357         /*
1358          * Check if we reached the last entry,
1359          * meaning this ia the last table to build,
1360          * and no need to check the block alignment
1361          */
1362         if (counter == num_array_entries) {
1363                 /* Set the last table flag */
1364                 *last_table_flag = 1;
1365                 goto end_function;
1366         }
1367
1368         /*
1369          * Calculate the data size of the next table.
1370          * Stop if no entries left or if data size is more the DMA restriction
1371          */
1372         next_table_data_size = 0;
1373         for (; counter < num_array_entries; counter++) {
1374                 next_table_data_size += lli_in_array_ptr[counter].block_size;
1375                 if (next_table_data_size >= SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1376                         break;
1377         }
1378
1379         /*
1380          * Check if the next table data size is less then DMA rstriction.
1381          * if it is - recalculate the current table size, so that the next
1382          * table data size will be adaquete for DMA
1383          */
1384         if (next_table_data_size &&
1385                 next_table_data_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1386
1387                 table_data_size -= (SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE -
1388                         next_table_data_size);
1389
1390         dev_dbg(&sep->pdev->dev, "table data size is %x\n",
1391                                                         table_data_size);
1392 end_function:
1393         return table_data_size;
1394 }
1395
1396 /**
1397  *      sep_build_lli_table - build an lli array for the given table
1398  *      @sep: pointer to struct sep_device
1399  *      @lli_array_ptr: pointer to lli array
1400  *      @lli_table_ptr: pointer to lli table
1401  *      @num_processed_entries_ptr: pointer to number of entries
1402  *      @num_table_entries_ptr: pointer to number of tables
1403  *      @table_data_size: total data size
1404  *
1405  *      Builds ant lli table from the lli_array according to
1406  *      the given size of data
1407  */
1408 static void sep_build_lli_table(struct sep_device *sep,
1409         struct sep_lli_entry    *lli_array_ptr,
1410         struct sep_lli_entry    *lli_table_ptr,
1411         u32 *num_processed_entries_ptr,
1412         u32 *num_table_entries_ptr,
1413         u32 table_data_size)
1414 {
1415         /* Current table data size */
1416         u32 curr_table_data_size;
1417         /* Counter of lli array entry */
1418         u32 array_counter;
1419
1420         dev_dbg(&sep->pdev->dev, "sep_build_lli_table start\n");
1421
1422         /* Init currrent table data size and lli array entry counter */
1423         curr_table_data_size = 0;
1424         array_counter = 0;
1425         *num_table_entries_ptr = 1;
1426
1427         dev_dbg(&sep->pdev->dev, "table_data_size is %x\n", table_data_size);
1428
1429         /* Fill the table till table size reaches the needed amount */
1430         while (curr_table_data_size < table_data_size) {
1431                 /* Update the number of entries in table */
1432                 (*num_table_entries_ptr)++;
1433
1434                 lli_table_ptr->bus_address =
1435                         cpu_to_le32(lli_array_ptr[array_counter].bus_address);
1436
1437                 lli_table_ptr->block_size =
1438                         cpu_to_le32(lli_array_ptr[array_counter].block_size);
1439
1440                 curr_table_data_size += lli_array_ptr[array_counter].block_size;
1441
1442                 dev_dbg(&sep->pdev->dev, "lli_table_ptr is %p\n",
1443                                                                 lli_table_ptr);
1444                 dev_dbg(&sep->pdev->dev, "lli_table_ptr->bus_address is %08lx\n",
1445                                 (unsigned long)lli_table_ptr->bus_address);
1446                 dev_dbg(&sep->pdev->dev, "lli_table_ptr->block_size is %x\n",
1447                         lli_table_ptr->block_size);
1448
1449                 /* Check for overflow of the table data */
1450                 if (curr_table_data_size > table_data_size) {
1451                         dev_dbg(&sep->pdev->dev,
1452                                 "curr_table_data_size too large\n");
1453
1454                         /* Update the size of block in the table */
1455                         lli_table_ptr->block_size -=
1456                         cpu_to_le32((curr_table_data_size - table_data_size));
1457
1458                         /* Update the physical address in the lli array */
1459                         lli_array_ptr[array_counter].bus_address +=
1460                         cpu_to_le32(lli_table_ptr->block_size);
1461
1462                         /* Update the block size left in the lli array */
1463                         lli_array_ptr[array_counter].block_size =
1464                                 (curr_table_data_size - table_data_size);
1465                 } else
1466                         /* Advance to the next entry in the lli_array */
1467                         array_counter++;
1468
1469                 dev_dbg(&sep->pdev->dev,
1470                         "lli_table_ptr->bus_address is %08lx\n",
1471                                 (unsigned long)lli_table_ptr->bus_address);
1472                 dev_dbg(&sep->pdev->dev,
1473                         "lli_table_ptr->block_size is %x\n",
1474                         lli_table_ptr->block_size);
1475
1476                 /* Move to the next entry in table */
1477                 lli_table_ptr++;
1478         }
1479
1480         /* Set the info entry to default */
1481         lli_table_ptr->bus_address = 0xffffffff;
1482         lli_table_ptr->block_size = 0;
1483
1484         dev_dbg(&sep->pdev->dev, "lli_table_ptr is %p\n", lli_table_ptr);
1485         dev_dbg(&sep->pdev->dev, "lli_table_ptr->bus_address is %08lx\n",
1486                                 (unsigned long)lli_table_ptr->bus_address);
1487         dev_dbg(&sep->pdev->dev, "lli_table_ptr->block_size is %x\n",
1488                                                 lli_table_ptr->block_size);
1489
1490         /* Set the output parameter */
1491         *num_processed_entries_ptr += array_counter;
1492
1493         dev_dbg(&sep->pdev->dev, "num_processed_entries_ptr is %x\n",
1494                 *num_processed_entries_ptr);
1495
1496         dev_dbg(&sep->pdev->dev, "sep_build_lli_table end\n");
1497 }
1498
1499 /**
1500  *      sep_shared_area_virt_to_bus - map shared area to bus address
1501  *      @sep: pointer to struct sep_device
1502  *      @virt_address: virtual address to convert
1503  *
1504  *      This functions returns the physical address inside shared area according
1505  *      to the virtual address. It can be either on the externa RAM device
1506  *      (ioremapped), or on the system RAM
1507  *      This implementation is for the external RAM
1508  */
1509 static dma_addr_t sep_shared_area_virt_to_bus(struct sep_device *sep,
1510         void *virt_address)
1511 {
1512         dev_dbg(&sep->pdev->dev, "sh virt to phys v %p\n", virt_address);
1513         dev_dbg(&sep->pdev->dev, "sh virt to phys p %08lx\n",
1514                 (unsigned long)
1515                 sep->shared_bus + (virt_address - sep->shared_addr));
1516
1517         return sep->shared_bus + (size_t)(virt_address - sep->shared_addr);
1518 }
1519
1520 /**
1521  *      sep_shared_area_bus_to_virt - map shared area bus address to kernel
1522  *      @sep: pointer to struct sep_device
1523  *      @bus_address: bus address to convert
1524  *
1525  *      This functions returns the virtual address inside shared area
1526  *      according to the physical address. It can be either on the
1527  *      externa RAM device (ioremapped), or on the system RAM
1528  *      This implementation is for the external RAM
1529  */
1530 static void *sep_shared_area_bus_to_virt(struct sep_device *sep,
1531         dma_addr_t bus_address)
1532 {
1533         dev_dbg(&sep->pdev->dev, "shared bus to virt b=%lx v=%lx\n",
1534                 (unsigned long)bus_address, (unsigned long)(sep->shared_addr +
1535                         (size_t)(bus_address - sep->shared_bus)));
1536
1537         return sep->shared_addr + (size_t)(bus_address - sep->shared_bus);
1538 }
1539
1540 /**
1541  *      sep_debug_print_lli_tables - dump LLI table
1542  *      @sep: pointer to struct sep_device
1543  *      @lli_table_ptr: pointer to sep_lli_entry
1544  *      @num_table_entries: number of entries
1545  *      @table_data_size: total data size
1546  *
1547  *      Walk the the list of the print created tables and print all the data
1548  */
1549 static void sep_debug_print_lli_tables(struct sep_device *sep,
1550         struct sep_lli_entry *lli_table_ptr,
1551         unsigned long num_table_entries,
1552         unsigned long table_data_size)
1553 {
1554         unsigned long table_count = 1;
1555         unsigned long entries_count = 0;
1556
1557         dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables start\n");
1558
1559         while ((unsigned long) lli_table_ptr->bus_address != 0xffffffff) {
1560                 dev_dbg(&sep->pdev->dev,
1561                         "lli table %08lx, table_data_size is %lu\n",
1562                         table_count, table_data_size);
1563                 dev_dbg(&sep->pdev->dev, "num_table_entries is %lu\n",
1564                                                         num_table_entries);
1565
1566                 /* Print entries of the table (without info entry) */
1567                 for (entries_count = 0; entries_count < num_table_entries;
1568                         entries_count++, lli_table_ptr++) {
1569
1570                         dev_dbg(&sep->pdev->dev,
1571                                 "lli_table_ptr address is %08lx\n",
1572                                 (unsigned long) lli_table_ptr);
1573
1574                         dev_dbg(&sep->pdev->dev,
1575                                 "phys address is %08lx block size is %x\n",
1576                                 (unsigned long)lli_table_ptr->bus_address,
1577                                 lli_table_ptr->block_size);
1578                 }
1579                 /* Point to the info entry */
1580                 lli_table_ptr--;
1581
1582                 dev_dbg(&sep->pdev->dev,
1583                         "phys lli_table_ptr->block_size is %x\n",
1584                         lli_table_ptr->block_size);
1585
1586                 dev_dbg(&sep->pdev->dev,
1587                         "phys lli_table_ptr->physical_address is %08lu\n",
1588                         (unsigned long)lli_table_ptr->bus_address);
1589
1590
1591                 table_data_size = lli_table_ptr->block_size & 0xffffff;
1592                 num_table_entries = (lli_table_ptr->block_size >> 24) & 0xff;
1593
1594                 dev_dbg(&sep->pdev->dev,
1595                         "phys table_data_size is %lu num_table_entries is"
1596                         " %lu bus_address is%lu\n", table_data_size,
1597                         num_table_entries, (unsigned long)lli_table_ptr->bus_address);
1598
1599                 if ((unsigned long)lli_table_ptr->bus_address != 0xffffffff)
1600                         lli_table_ptr = (struct sep_lli_entry *)
1601                                 sep_shared_bus_to_virt(sep,
1602                                 (unsigned long)lli_table_ptr->bus_address);
1603
1604                 table_count++;
1605         }
1606         dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables end\n");
1607 }
1608
1609
1610 /**
1611  *      sep_prepare_empty_lli_table - create a blank LLI table
1612  *      @sep: pointer to struct sep_device
1613  *      @lli_table_addr_ptr: pointer to lli table
1614  *      @num_entries_ptr: pointer to number of entries
1615  *      @table_data_size_ptr: point to table data size
1616  *
1617  *      This function creates empty lli tables when there is no data
1618  */
1619 static void sep_prepare_empty_lli_table(struct sep_device *sep,
1620                 dma_addr_t *lli_table_addr_ptr,
1621                 u32 *num_entries_ptr,
1622                 u32 *table_data_size_ptr)
1623 {
1624         struct sep_lli_entry *lli_table_ptr;
1625
1626         dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");
1627
1628         /* Find the area for new table */
1629         lli_table_ptr =
1630                 (struct sep_lli_entry *)(sep->shared_addr +
1631                 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1632                 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1633                         SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1634
1635         lli_table_ptr->bus_address = 0;
1636         lli_table_ptr->block_size = 0;
1637
1638         lli_table_ptr++;
1639         lli_table_ptr->bus_address = 0xFFFFFFFF;
1640         lli_table_ptr->block_size = 0;
1641
1642         /* Set the output parameter value */
1643         *lli_table_addr_ptr = sep->shared_bus +
1644                 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1645                 sep->num_lli_tables_created *
1646                 sizeof(struct sep_lli_entry) *
1647                 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1648
1649         /* Set the num of entries and table data size for empty table */
1650         *num_entries_ptr = 2;
1651         *table_data_size_ptr = 0;
1652
1653         /* Update the number of created tables */
1654         sep->num_lli_tables_created++;
1655
1656         dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");
1657
1658 }
1659
1660 /**
1661  *      sep_prepare_input_dma_table - prepare input DMA mappings
1662  *      @sep: pointer to struct sep_device
1663  *      @data_size:
1664  *      @block_size:
1665  *      @lli_table_ptr:
1666  *      @num_entries_ptr:
1667  *      @table_data_size_ptr:
1668  *      @is_kva: set for kernel data (kernel cryptio call)
1669  *
1670  *      This function prepares only input DMA table for synhronic symmetric
1671  *      operations (HASH)
1672  *      Note that all bus addresses that are passed to the SEP
1673  *      are in 32 bit format; the SEP is a 32 bit device
1674  */
1675 static int sep_prepare_input_dma_table(struct sep_device *sep,
1676         unsigned long app_virt_addr,
1677         u32 data_size,
1678         u32 block_size,
1679         dma_addr_t *lli_table_ptr,
1680         u32 *num_entries_ptr,
1681         u32 *table_data_size_ptr,
1682         bool is_kva)
1683 {
1684         int error = 0;
1685         /* Pointer to the info entry of the table - the last entry */
1686         struct sep_lli_entry *info_entry_ptr;
1687         /* Array of pointers to page */
1688         struct sep_lli_entry *lli_array_ptr;
1689         /* Points to the first entry to be processed in the lli_in_array */
1690         u32 current_entry = 0;
1691         /* Num entries in the virtual buffer */
1692         u32 sep_lli_entries = 0;
1693         /* Lli table pointer */
1694         struct sep_lli_entry *in_lli_table_ptr;
1695         /* The total data in one table */
1696         u32 table_data_size = 0;
1697         /* Flag for last table */
1698         u32 last_table_flag = 0;
1699         /* Number of entries in lli table */
1700         u32 num_entries_in_table = 0;
1701         /* Next table address */
1702         void *lli_table_alloc_addr = 0;
1703
1704         dev_dbg(&sep->pdev->dev, "sep_prepare_input_dma_table start\n");
1705         dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1706         dev_dbg(&sep->pdev->dev, "block_size is %x\n", block_size);
1707
1708         /* Initialize the pages pointers */
1709         sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1710         sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 0;
1711
1712         /* Set the kernel address for first table to be allocated */
1713         lli_table_alloc_addr = (void *)(sep->shared_addr +
1714                 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1715                 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1716                 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1717
1718         if (data_size == 0) {
1719                 /* Special case  - create meptu table - 2 entries, zero data */
1720                 sep_prepare_empty_lli_table(sep, lli_table_ptr,
1721                                 num_entries_ptr, table_data_size_ptr);
1722                 goto update_dcb_counter;
1723         }
1724
1725         /* Check if the pages are in Kernel Virtual Address layout */
1726         if (is_kva == true)
1727                 /* Lock the pages in the kernel */
1728                 error = sep_lock_kernel_pages(sep, app_virt_addr,
1729                         data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1730         else
1731                 /*
1732                  * Lock the pages of the user buffer
1733                  * and translate them to pages
1734                  */
1735                 error = sep_lock_user_pages(sep, app_virt_addr,
1736                         data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1737
1738         if (error)
1739                 goto end_function;
1740
1741         dev_dbg(&sep->pdev->dev, "output sep_in_num_pages is %x\n",
1742                 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
1743
1744         current_entry = 0;
1745         info_entry_ptr = NULL;
1746
1747         sep_lli_entries = sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages;
1748
1749         /* Loop till all the entries in in array are not processed */
1750         while (current_entry < sep_lli_entries) {
1751
1752                 /* Set the new input and output tables */
1753                 in_lli_table_ptr =
1754                         (struct sep_lli_entry *)lli_table_alloc_addr;
1755
1756                 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1757                         SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1758
1759                 if (lli_table_alloc_addr >
1760                         ((void *)sep->shared_addr +
1761                         SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1762                         SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1763
1764                         error = -ENOMEM;
1765                         goto end_function_error;
1766
1767                 }
1768
1769                 /* Update the number of created tables */
1770                 sep->num_lli_tables_created++;
1771
1772                 /* Calculate the maximum size of data for input table */
1773                 table_data_size = sep_calculate_lli_table_max_size(sep,
1774                         &lli_array_ptr[current_entry],
1775                         (sep_lli_entries - current_entry),
1776                         &last_table_flag);
1777
1778                 /*
1779                  * If this is not the last table -
1780                  * then allign it to the block size
1781                  */
1782                 if (!last_table_flag)
1783                         table_data_size =
1784                                 (table_data_size / block_size) * block_size;
1785
1786                 dev_dbg(&sep->pdev->dev, "output table_data_size is %x\n",
1787                                                         table_data_size);
1788
1789                 /* Construct input lli table */
1790                 sep_build_lli_table(sep, &lli_array_ptr[current_entry],
1791                         in_lli_table_ptr,
1792                         &current_entry, &num_entries_in_table, table_data_size);
1793
1794                 if (info_entry_ptr == NULL) {
1795
1796                         /* Set the output parameters to physical addresses */
1797                         *lli_table_ptr = sep_shared_area_virt_to_bus(sep,
1798                                 in_lli_table_ptr);
1799                         *num_entries_ptr = num_entries_in_table;
1800                         *table_data_size_ptr = table_data_size;
1801
1802                         dev_dbg(&sep->pdev->dev,
1803                                 "output lli_table_in_ptr is %08lx\n",
1804                                 (unsigned long)*lli_table_ptr);
1805
1806                 } else {
1807                         /* Update the info entry of the previous in table */
1808                         info_entry_ptr->bus_address =
1809                                 sep_shared_area_virt_to_bus(sep,
1810                                                         in_lli_table_ptr);
1811                         info_entry_ptr->block_size =
1812                                 ((num_entries_in_table) << 24) |
1813                                 (table_data_size);
1814                 }
1815                 /* Save the pointer to the info entry of the current tables */
1816                 info_entry_ptr = in_lli_table_ptr + num_entries_in_table - 1;
1817         }
1818         /* Print input tables */
1819         sep_debug_print_lli_tables(sep, (struct sep_lli_entry *)
1820                 sep_shared_area_bus_to_virt(sep, *lli_table_ptr),
1821                 *num_entries_ptr, *table_data_size_ptr);
1822         /* The array of the pages */
1823         kfree(lli_array_ptr);
1824
1825 update_dcb_counter:
1826         /* Update DCB counter */
1827         sep->nr_dcb_creat++;
1828         goto end_function;
1829
1830 end_function_error:
1831         /* Free all the allocated resources */
1832         kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
1833         kfree(lli_array_ptr);
1834         kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
1835
1836 end_function:
1837         dev_dbg(&sep->pdev->dev, "sep_prepare_input_dma_table end\n");
1838         return error;
1839
1840 }
1841 /**
1842  *      sep_construct_dma_tables_from_lli - prepare AES/DES mappings
1843  *      @sep: pointer to struct sep_device
1844  *      @lli_in_array:
1845  *      @sep_in_lli_entries:
1846  *      @lli_out_array:
1847  *      @sep_out_lli_entries
1848  *      @block_size
1849  *      @lli_table_in_ptr
1850  *      @lli_table_out_ptr
1851  *      @in_num_entries_ptr
1852  *      @out_num_entries_ptr
1853  *      @table_data_size_ptr
1854  *
1855  *      This function creates the input and output DMA tables for
1856  *      symmetric operations (AES/DES) according to the block
1857  *      size from LLI arays
1858  *      Note that all bus addresses that are passed to the SEP
1859  *      are in 32 bit format; the SEP is a 32 bit device
1860  */
1861 static int sep_construct_dma_tables_from_lli(
1862         struct sep_device *sep,
1863         struct sep_lli_entry *lli_in_array,
1864         u32     sep_in_lli_entries,
1865         struct sep_lli_entry *lli_out_array,
1866         u32     sep_out_lli_entries,
1867         u32     block_size,
1868         dma_addr_t *lli_table_in_ptr,
1869         dma_addr_t *lli_table_out_ptr,
1870         u32     *in_num_entries_ptr,
1871         u32     *out_num_entries_ptr,
1872         u32     *table_data_size_ptr)
1873 {
1874         /* Points to the area where next lli table can be allocated */
1875         void *lli_table_alloc_addr = 0;
1876         /* Input lli table */
1877         struct sep_lli_entry *in_lli_table_ptr = NULL;
1878         /* Output lli table */
1879         struct sep_lli_entry *out_lli_table_ptr = NULL;
1880         /* Pointer to the info entry of the table - the last entry */
1881         struct sep_lli_entry *info_in_entry_ptr = NULL;
1882         /* Pointer to the info entry of the table - the last entry */
1883         struct sep_lli_entry *info_out_entry_ptr = NULL;
1884         /* Points to the first entry to be processed in the lli_in_array */
1885         u32 current_in_entry = 0;
1886         /* Points to the first entry to be processed in the lli_out_array */
1887         u32 current_out_entry = 0;
1888         /* Max size of the input table */
1889         u32 in_table_data_size = 0;
1890         /* Max size of the output table */
1891         u32 out_table_data_size = 0;
1892         /* Flag te signifies if this is the last tables build */
1893         u32 last_table_flag = 0;
1894         /* The data size that should be in table */
1895         u32 table_data_size = 0;
1896         /* Number of etnries in the input table */
1897         u32 num_entries_in_table = 0;
1898         /* Number of etnries in the output table */
1899         u32 num_entries_out_table = 0;
1900
1901         dev_dbg(&sep->pdev->dev, "sep_construct_dma_tables_from_lli start\n");
1902
1903         /* Initiate to point after the message area */
1904         lli_table_alloc_addr = (void *)(sep->shared_addr +
1905                 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1906                 (sep->num_lli_tables_created *
1907                 (sizeof(struct sep_lli_entry) *
1908                 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP)));
1909
1910         /* Loop till all the entries in in array are not processed */
1911         while (current_in_entry < sep_in_lli_entries) {
1912                 /* Set the new input and output tables */
1913                 in_lli_table_ptr =
1914                         (struct sep_lli_entry *)lli_table_alloc_addr;
1915
1916                 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1917                         SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1918
1919                 /* Set the first output tables */
1920                 out_lli_table_ptr =
1921                         (struct sep_lli_entry *)lli_table_alloc_addr;
1922
1923                 /* Check if the DMA table area limit was overrun */
1924                 if ((lli_table_alloc_addr + sizeof(struct sep_lli_entry) *
1925                         SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP) >
1926                         ((void *)sep->shared_addr +
1927                         SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1928                         SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1929
1930                         dev_warn(&sep->pdev->dev, "dma table limit overrun\n");
1931                         return -ENOMEM;
1932                 }
1933
1934                 /* Update the number of the lli tables created */
1935                 sep->num_lli_tables_created += 2;
1936
1937                 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1938                         SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1939
1940                 /* Calculate the maximum size of data for input table */
1941                 in_table_data_size =
1942                         sep_calculate_lli_table_max_size(sep,
1943                         &lli_in_array[current_in_entry],
1944                         (sep_in_lli_entries - current_in_entry),
1945                         &last_table_flag);
1946
1947                 /* Calculate the maximum size of data for output table */
1948                 out_table_data_size =
1949                         sep_calculate_lli_table_max_size(sep,
1950                         &lli_out_array[current_out_entry],
1951                         (sep_out_lli_entries - current_out_entry),
1952                         &last_table_flag);
1953
1954                 dev_dbg(&sep->pdev->dev,
1955                         "in_table_data_size is %x\n",
1956                         in_table_data_size);
1957
1958                 dev_dbg(&sep->pdev->dev,
1959                         "out_table_data_size is %x\n",
1960                         out_table_data_size);
1961
1962                 table_data_size = in_table_data_size;
1963
1964                 if (!last_table_flag) {
1965                         /*
1966                          * If this is not the last table,
1967                          * then must check where the data is smallest
1968                          * and then align it to the block size
1969                          */
1970                         if (table_data_size > out_table_data_size)
1971                                 table_data_size = out_table_data_size;
1972
1973                         /*
1974                          * Now calculate the table size so that
1975                          * it will be module block size
1976                          */
1977                         table_data_size = (table_data_size / block_size) *
1978                                 block_size;
1979                 }
1980
1981                 dev_dbg(&sep->pdev->dev, "table_data_size is %x\n",
1982                                                         table_data_size);
1983
1984                 /* Construct input lli table */
1985                 sep_build_lli_table(sep, &lli_in_array[current_in_entry],
1986                         in_lli_table_ptr,
1987                         &current_in_entry,
1988                         &num_entries_in_table,
1989                         table_data_size);
1990
1991                 /* Construct output lli table */
1992                 sep_build_lli_table(sep, &lli_out_array[current_out_entry],
1993                         out_lli_table_ptr,
1994                         &current_out_entry,
1995                         &num_entries_out_table,
1996                         table_data_size);
1997
1998                 /* If info entry is null - this is the first table built */
1999                 if (info_in_entry_ptr == NULL) {
2000                         /* Set the output parameters to physical addresses */
2001                         *lli_table_in_ptr =
2002                         sep_shared_area_virt_to_bus(sep, in_lli_table_ptr);
2003
2004                         *in_num_entries_ptr = num_entries_in_table;
2005
2006                         *lli_table_out_ptr =
2007                                 sep_shared_area_virt_to_bus(sep,
2008                                 out_lli_table_ptr);
2009
2010                         *out_num_entries_ptr = num_entries_out_table;
2011                         *table_data_size_ptr = table_data_size;
2012
2013                         dev_dbg(&sep->pdev->dev,
2014                         "output lli_table_in_ptr is %08lx\n",
2015                                 (unsigned long)*lli_table_in_ptr);
2016                         dev_dbg(&sep->pdev->dev,
2017                         "output lli_table_out_ptr is %08lx\n",
2018                                 (unsigned long)*lli_table_out_ptr);
2019                 } else {
2020                         /* Update the info entry of the previous in table */
2021                         info_in_entry_ptr->bus_address =
2022                                 sep_shared_area_virt_to_bus(sep,
2023                                 in_lli_table_ptr);
2024
2025                         info_in_entry_ptr->block_size =
2026                                 ((num_entries_in_table) << 24) |
2027                                 (table_data_size);
2028
2029                         /* Update the info entry of the previous in table */
2030                         info_out_entry_ptr->bus_address =
2031                                 sep_shared_area_virt_to_bus(sep,
2032                                 out_lli_table_ptr);
2033
2034                         info_out_entry_ptr->block_size =
2035                                 ((num_entries_out_table) << 24) |
2036                                 (table_data_size);
2037
2038                         dev_dbg(&sep->pdev->dev,
2039                                 "output lli_table_in_ptr:%08lx %08x\n",
2040                                 (unsigned long)info_in_entry_ptr->bus_address,
2041                                 info_in_entry_ptr->block_size);
2042
2043                         dev_dbg(&sep->pdev->dev,
2044                                 "output lli_table_out_ptr:%08lx  %08x\n",
2045                                 (unsigned long)info_out_entry_ptr->bus_address,
2046                                 info_out_entry_ptr->block_size);
2047                 }
2048
2049                 /* Save the pointer to the info entry of the current tables */
2050                 info_in_entry_ptr = in_lli_table_ptr +
2051                         num_entries_in_table - 1;
2052                 info_out_entry_ptr = out_lli_table_ptr +
2053                         num_entries_out_table - 1;
2054
2055                 dev_dbg(&sep->pdev->dev,
2056                         "output num_entries_out_table is %x\n",
2057                         (u32)num_entries_out_table);
2058                 dev_dbg(&sep->pdev->dev,
2059                         "output info_in_entry_ptr is %lx\n",
2060                         (unsigned long)info_in_entry_ptr);
2061                 dev_dbg(&sep->pdev->dev,
2062                         "output info_out_entry_ptr is %lx\n",
2063                         (unsigned long)info_out_entry_ptr);
2064         }
2065
2066         /* Print input tables */
2067         sep_debug_print_lli_tables(sep,
2068         (struct sep_lli_entry *)
2069         sep_shared_area_bus_to_virt(sep, *lli_table_in_ptr),
2070         *in_num_entries_ptr,
2071         *table_data_size_ptr);
2072
2073         /* Print output tables */
2074         sep_debug_print_lli_tables(sep,
2075         (struct sep_lli_entry *)
2076         sep_shared_area_bus_to_virt(sep, *lli_table_out_ptr),
2077         *out_num_entries_ptr,
2078         *table_data_size_ptr);
2079
2080         dev_dbg(&sep->pdev->dev, "sep_construct_dma_tables_from_lli end\n");
2081         return 0;
2082 }
2083
2084 /**
2085  *      sep_prepare_input_output_dma_table - prepare DMA I/O table
2086  *      @app_virt_in_addr:
2087  *      @app_virt_out_addr:
2088  *      @data_size:
2089  *      @block_size:
2090  *      @lli_table_in_ptr:
2091  *      @lli_table_out_ptr:
2092  *      @in_num_entries_ptr:
2093  *      @out_num_entries_ptr:
2094  *      @table_data_size_ptr:
2095  *      @is_kva: set for kernel data; used only for kernel crypto module
2096  *
2097  *      This function builds input and output DMA tables for synhronic
2098  *      symmetric operations (AES, DES, HASH). It also checks that each table
2099  *      is of the modular block size
2100  *      Note that all bus addresses that are passed to the SEP
2101  *      are in 32 bit format; the SEP is a 32 bit device
2102  */
2103 static int sep_prepare_input_output_dma_table(struct sep_device *sep,
2104         unsigned long app_virt_in_addr,
2105         unsigned long app_virt_out_addr,
2106         u32 data_size,
2107         u32 block_size,
2108         dma_addr_t *lli_table_in_ptr,
2109         dma_addr_t *lli_table_out_ptr,
2110         u32 *in_num_entries_ptr,
2111         u32 *out_num_entries_ptr,
2112         u32 *table_data_size_ptr,
2113         bool is_kva)
2114
2115 {
2116         int error = 0;
2117         /* Array of pointers of page */
2118         struct sep_lli_entry *lli_in_array;
2119         /* Array of pointers of page */
2120         struct sep_lli_entry *lli_out_array;
2121
2122         dev_dbg(&sep->pdev->dev, "sep_prepare_input_output_dma_table start\n");
2123
2124         if (data_size == 0) {
2125                 /* Prepare empty table for input and output */
2126                 sep_prepare_empty_lli_table(sep, lli_table_in_ptr,
2127                         in_num_entries_ptr, table_data_size_ptr);
2128
2129                 sep_prepare_empty_lli_table(sep, lli_table_out_ptr,
2130                         out_num_entries_ptr, table_data_size_ptr);
2131
2132                 goto update_dcb_counter;
2133         }
2134
2135         /* Initialize the pages pointers */
2136         sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
2137         sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
2138
2139         /* Lock the pages of the buffer and translate them to pages */
2140         if (is_kva == true) {
2141                 error = sep_lock_kernel_pages(sep, app_virt_in_addr,
2142                         data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2143
2144                 if (error) {
2145                         dev_warn(&sep->pdev->dev,
2146                                 "lock kernel for in failed\n");
2147                         goto end_function;
2148                 }
2149
2150                 error = sep_lock_kernel_pages(sep, app_virt_out_addr,
2151                         data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2152
2153                 if (error) {
2154                         dev_warn(&sep->pdev->dev,
2155                                 "lock kernel for out failed\n");
2156                         goto end_function;
2157                 }
2158         }
2159
2160         else {
2161                 error = sep_lock_user_pages(sep, app_virt_in_addr,
2162                                 data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2163                 if (error) {
2164                         dev_warn(&sep->pdev->dev,
2165                                 "sep_lock_user_pages for input virtual buffer failed\n");
2166                         goto end_function;
2167                 }
2168
2169                 error = sep_lock_user_pages(sep, app_virt_out_addr,
2170                         data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2171
2172                 if (error) {
2173                         dev_warn(&sep->pdev->dev,
2174                                 "sep_lock_user_pages for output virtual buffer failed\n");
2175                         goto end_function_free_lli_in;
2176                 }
2177         }
2178
2179         dev_dbg(&sep->pdev->dev, "sep_in_num_pages is %x\n",
2180                 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
2181         dev_dbg(&sep->pdev->dev, "sep_out_num_pages is %x\n",
2182                 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages);
2183         dev_dbg(&sep->pdev->dev, "SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP is %x\n",
2184                 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
2185
2186         /* Call the fucntion that creates table from the lli arrays */
2187         error = sep_construct_dma_tables_from_lli(sep, lli_in_array,
2188                 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages,
2189                 lli_out_array,
2190                 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages,
2191                 block_size, lli_table_in_ptr, lli_table_out_ptr,
2192                 in_num_entries_ptr, out_num_entries_ptr, table_data_size_ptr);
2193
2194         if (error) {
2195                 dev_warn(&sep->pdev->dev,
2196                         "sep_construct_dma_tables_from_lli failed\n");
2197                 goto end_function_with_error;
2198         }
2199
2200         kfree(lli_out_array);
2201         kfree(lli_in_array);
2202
2203 update_dcb_counter:
2204         /* Update DCB counter */
2205         sep->nr_dcb_creat++;
2206         /* Fall through - free the lli entry arrays */
2207         dev_dbg(&sep->pdev->dev, "in_num_entries_ptr is %08x\n",
2208                                                 *in_num_entries_ptr);
2209         dev_dbg(&sep->pdev->dev, "out_num_entries_ptr is %08x\n",
2210                                                 *out_num_entries_ptr);
2211         dev_dbg(&sep->pdev->dev, "table_data_size_ptr is %08x\n",
2212                                                 *table_data_size_ptr);
2213
2214         goto end_function;
2215
2216 end_function_with_error:
2217         kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_map_array);
2218         kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_page_array);
2219         kfree(lli_out_array);
2220
2221
2222 end_function_free_lli_in:
2223         kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
2224         kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
2225         kfree(lli_in_array);
2226
2227 end_function:
2228         dev_dbg(&sep->pdev->dev,
2229                 "sep_prepare_input_output_dma_table end result = %d\n", error);
2230
2231         return error;
2232
2233 }
2234
2235 /**
2236  *      sep_prepare_input_output_dma_table_in_dcb - prepare control blocks
2237  *      @app_in_address: unsigned long; for data buffer in (user space)
2238  *      @app_out_address: unsigned long; for data buffer out (user space)
2239  *      @data_in_size: u32; for size of data
2240  *      @block_size: u32; for block size
2241  *      @tail_block_size: u32; for size of tail block
2242  *      @isapplet: bool; to indicate external app
2243  *      @is_kva: bool; kernel buffer; only used for kernel crypto module
2244  *
2245  *      This function prepares the linked DMA tables and puts the
2246  *      address for the linked list of tables inta a DCB (data control
2247  *      block) the address of which is known by the SEP hardware
2248  *      Note that all bus addresses that are passed to the SEP
2249  *      are in 32 bit format; the SEP is a 32 bit device
2250  */
2251 static int sep_prepare_input_output_dma_table_in_dcb(struct sep_device *sep,
2252         unsigned long  app_in_address,
2253         unsigned long  app_out_address,
2254         u32  data_in_size,
2255         u32  block_size,
2256         u32  tail_block_size,
2257         bool isapplet,
2258         bool    is_kva)
2259 {
2260         int error = 0;
2261         /* Size of tail */
2262         u32 tail_size = 0;
2263         /* Address of the created DCB table */
2264         struct sep_dcblock *dcb_table_ptr = NULL;
2265         /* The physical address of the first input DMA table */
2266         dma_addr_t in_first_mlli_address = 0;
2267         /* Number of entries in the first input DMA table */
2268         u32  in_first_num_entries = 0;
2269         /* The physical address of the first output DMA table */
2270         dma_addr_t  out_first_mlli_address = 0;
2271         /* Number of entries in the first output DMA table */
2272         u32  out_first_num_entries = 0;
2273         /* Data in the first input/output table */
2274         u32  first_data_size = 0;
2275
2276         dev_dbg(&sep->pdev->dev, "prepare_input_output_dma_table_in_dcb start\n");
2277
2278         if (sep->nr_dcb_creat == SEP_MAX_NUM_SYNC_DMA_OPS) {
2279                 /* No more DCBs to allocate */
2280                 dev_warn(&sep->pdev->dev, "no more DCBs available\n");
2281                 error = -ENOSPC;
2282                 goto end_function;
2283         }
2284
2285         /* Allocate new DCB */
2286         dcb_table_ptr = (struct sep_dcblock *)(sep->shared_addr +
2287                 SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES +
2288                 (sep->nr_dcb_creat * sizeof(struct sep_dcblock)));
2289
2290         /* Set the default values in the DCB */
2291         dcb_table_ptr->input_mlli_address = 0;
2292         dcb_table_ptr->input_mlli_num_entries = 0;
2293         dcb_table_ptr->input_mlli_data_size = 0;
2294         dcb_table_ptr->output_mlli_address = 0;
2295         dcb_table_ptr->output_mlli_num_entries = 0;
2296         dcb_table_ptr->output_mlli_data_size = 0;
2297         dcb_table_ptr->tail_data_size = 0;
2298         dcb_table_ptr->out_vr_tail_pt = 0;
2299
2300         if (isapplet == true) {
2301                 tail_size = data_in_size % block_size;
2302                 if (tail_size) {
2303                         if (data_in_size < tail_block_size) {
2304                                 dev_warn(&sep->pdev->dev, "data in size smaller than tail block size\n");
2305                                 error = -ENOSPC;
2306                                 goto end_function;
2307                         }
2308                         if (tail_block_size)
2309                                 /*
2310                                  * Case the tail size should be
2311                                  * bigger than the real block size
2312                                  */
2313                                 tail_size = tail_block_size +
2314                                         ((data_in_size -
2315                                                 tail_block_size) % block_size);
2316                 }
2317
2318                 /* Check if there is enough data for DMA operation */
2319                 if (data_in_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE) {
2320                         if (is_kva == true) {
2321                                 memcpy(dcb_table_ptr->tail_data,
2322                                         (void *)app_in_address, data_in_size);
2323                         } else {
2324                                 if (copy_from_user(dcb_table_ptr->tail_data,
2325                                         (void __user *)app_in_address,
2326                                         data_in_size)) {
2327                                         error = -EFAULT;
2328                                         goto end_function;
2329                                 }
2330                         }
2331
2332                         dcb_table_ptr->tail_data_size = data_in_size;
2333
2334                         /* Set the output user-space address for mem2mem op */
2335                         if (app_out_address)
2336                                 dcb_table_ptr->out_vr_tail_pt =
2337                                                         (u32)app_out_address;
2338
2339                         /*
2340                          * Update both data length parameters in order to avoid
2341                          * second data copy and allow building of empty mlli
2342                          * tables
2343                          */
2344                         tail_size = 0x0;
2345                         data_in_size = 0x0;
2346                 }
2347                 if (tail_size) {
2348                         if (is_kva == true) {
2349                                 memcpy(dcb_table_ptr->tail_data,
2350                                         (void *)(app_in_address + data_in_size -
2351                                         tail_size), tail_size);
2352                         } else {
2353                                 /* We have tail data - copy it to DCB */
2354                                 if (copy_from_user(dcb_table_ptr->tail_data,
2355                                         (void *)(app_in_address +
2356                                         data_in_size - tail_size), tail_size)) {
2357                                         error = -EFAULT;
2358                                         goto end_function;
2359                                 }
2360                         }
2361                         if (app_out_address)
2362                                 /*
2363                                  * Calculate the output address
2364                                  * according to tail data size
2365                                  */
2366                                 dcb_table_ptr->out_vr_tail_pt =
2367                                         (u32)app_out_address + data_in_size
2368                                         - tail_size;
2369
2370                         /* Save the real tail data size */
2371                         dcb_table_ptr->tail_data_size = tail_size;
2372                         /*
2373                          * Update the data size without the tail
2374                          * data size AKA data for the dma
2375                          */
2376                         data_in_size = (data_in_size - tail_size);
2377                 }
2378         }
2379         /* Check if we need to build only input table or input/output */
2380         if (app_out_address) {
2381                 /* Prepare input/output tables */
2382                 error = sep_prepare_input_output_dma_table(sep,
2383                         app_in_address,
2384                         app_out_address,
2385                         data_in_size,
2386                         block_size,
2387                         &in_first_mlli_address,
2388                         &out_first_mlli_address,
2389                         &in_first_num_entries,
2390                         &out_first_num_entries,
2391                         &first_data_size,
2392                         is_kva);
2393         } else {
2394                 /* Prepare input tables */
2395                 error = sep_prepare_input_dma_table(sep,
2396                         app_in_address,
2397                         data_in_size,
2398                         block_size,
2399                         &in_first_mlli_address,
2400                         &in_first_num_entries,
2401                         &first_data_size,
2402                         is_kva);
2403         }
2404
2405         if (error) {
2406                 dev_warn(&sep->pdev->dev, "prepare DMA table call failed from prepare DCB call\n");
2407                 goto end_function;
2408         }
2409
2410         /* Set the DCB values */
2411         dcb_table_ptr->input_mlli_address = in_first_mlli_address;
2412         dcb_table_ptr->input_mlli_num_entries = in_first_num_entries;
2413         dcb_table_ptr->input_mlli_data_size = first_data_size;
2414         dcb_table_ptr->output_mlli_address = out_first_mlli_address;
2415         dcb_table_ptr->output_mlli_num_entries = out_first_num_entries;
2416         dcb_table_ptr->output_mlli_data_size = first_data_size;
2417
2418 end_function:
2419         dev_dbg(&sep->pdev->dev,
2420                 "sep_prepare_input_output_dma_table_in_dcb end\n");
2421         return error;
2422
2423 }
2424
2425
2426 /**
2427  *      sep_create_sync_dma_tables_handler - create sync DMA tables
2428  *      @sep: pointer to struct sep_device
2429  *      @arg: pointer to struct bld_syn_tab_struct
2430  *
2431  *      Handle the request for creation of the DMA tables for the synchronic
2432  *      symmetric operations (AES,DES). Note that all bus addresses that are
2433  *      passed to the SEP are in 32 bit format; the SEP is a 32 bit device
2434  */
2435 static int sep_create_sync_dma_tables_handler(struct sep_device *sep,
2436                                                 unsigned long arg)
2437 {
2438         int error = 0;
2439
2440         /* Command arguments */
2441         struct bld_syn_tab_struct command_args;
2442
2443         dev_dbg(&sep->pdev->dev,
2444                 "sep_create_sync_dma_tables_handler start\n");
2445
2446         if (copy_from_user(&command_args, (void __user *)arg,
2447                                         sizeof(struct bld_syn_tab_struct))) {
2448                 error = -EFAULT;
2449                 goto end_function;
2450         }
2451
2452         dev_dbg(&sep->pdev->dev, "app_in_address is %08llx\n",
2453                                                 command_args.app_in_address);
2454         dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2455                                                 command_args.app_out_address);
2456         dev_dbg(&sep->pdev->dev, "data_size is %u\n",
2457                                                 command_args.data_in_size);
2458         dev_dbg(&sep->pdev->dev, "block_size is %u\n",
2459                                                 command_args.block_size);
2460
2461         /* Validate user parameters */
2462         if (!command_args.app_in_address) {
2463                 error = -EINVAL;
2464                 goto end_function;
2465         }
2466
2467         error = sep_prepare_input_output_dma_table_in_dcb(sep,
2468                 (unsigned long)command_args.app_in_address,
2469                 (unsigned long)command_args.app_out_address,
2470                 command_args.data_in_size,
2471                 command_args.block_size,
2472                 0x0,
2473                 false,
2474                 false);
2475
2476 end_function:
2477         dev_dbg(&sep->pdev->dev, "sep_create_sync_dma_tables_handler end\n");
2478         return error;
2479 }
2480
2481 /**
2482  *      sep_free_dma_tables_and_dcb - free DMA tables and DCBs
2483  *      @sep: pointer to struct sep_device
2484  *      @isapplet: indicates external application (used for kernel access)
2485  *      @is_kva: indicates kernel addresses (only used for kernel crypto)
2486  *
2487  *      This function frees the DMA tables and DCB
2488  */
2489 static int sep_free_dma_tables_and_dcb(struct sep_device *sep, bool isapplet,
2490         bool is_kva)
2491 {
2492         int i = 0;
2493         int error = 0;
2494         int error_temp = 0;
2495         struct sep_dcblock *dcb_table_ptr;
2496         unsigned long pt_hold;
2497         void *tail_pt;
2498
2499         dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb start\n");
2500
2501         if (isapplet == true) {
2502                 /* Set pointer to first DCB table */
2503                 dcb_table_ptr = (struct sep_dcblock *)
2504                         (sep->shared_addr +
2505                         SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES);
2506
2507                 /* Go over each DCB and see if tail pointer must be updated */
2508                 for (i = 0; i < sep->nr_dcb_creat; i++, dcb_table_ptr++) {
2509                         if (dcb_table_ptr->out_vr_tail_pt) {
2510                                 pt_hold = (unsigned long)dcb_table_ptr->out_vr_tail_pt;
2511                                 tail_pt = (void *)pt_hold;
2512                                 if (is_kva == true) {
2513                                         memcpy(tail_pt,
2514                                                 dcb_table_ptr->tail_data,
2515                                                 dcb_table_ptr->tail_data_size);
2516                                 } else {
2517                                         error_temp = copy_to_user(
2518                                                 tail_pt,
2519                                                 dcb_table_ptr->tail_data,
2520                                                 dcb_table_ptr->tail_data_size);
2521                                 }
2522                                 if (error_temp) {
2523                                         /* Release the DMA resource */
2524                                         error = -EFAULT;
2525                                         break;
2526                                 }
2527                         }
2528                 }
2529         }
2530         /* Free the output pages, if any */
2531         sep_free_dma_table_data_handler(sep);
2532
2533         dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb end\n");
2534         return error;
2535 }
2536
2537 /**
2538  *      sep_get_static_pool_addr_handler - get static pool address
2539  *      @sep: pointer to struct sep_device
2540  *      @arg: parameters from user space application
2541  *
2542  *      This function sets the bus and virtual addresses of the static pool
2543  *      and returns the virtual address
2544  */
2545 static int sep_get_static_pool_addr_handler(struct sep_device *sep)
2546 {
2547         u32 *static_pool_addr = NULL;
2548
2549         dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler start\n");
2550
2551         static_pool_addr = (u32 *)(sep->shared_addr +
2552                 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2553
2554         static_pool_addr[0] = SEP_STATIC_POOL_VAL_TOKEN;
2555         static_pool_addr[1] = (u32)sep->shared_bus +
2556                 SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
2557
2558         dev_dbg(&sep->pdev->dev, "static pool: physical %x\n",
2559                 (u32)static_pool_addr[1]);
2560
2561         dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler end\n");
2562
2563         return 0;
2564 }
2565
2566 /**
2567  *      sep_start_handler - start device
2568  *      @sep: pointer to struct sep_device
2569  */
2570 static int sep_start_handler(struct sep_device *sep)
2571 {
2572         unsigned long reg_val;
2573         unsigned long error = 0;
2574
2575         dev_dbg(&sep->pdev->dev, "sep_start_handler start\n");
2576
2577         /* Wait in polling for message from SEP */
2578         do {
2579                 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2580         } while (!reg_val);
2581
2582         /* Check the value */
2583         if (reg_val == 0x1)
2584                 /* Fatal error - read error status from GPRO */
2585                 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2586         dev_dbg(&sep->pdev->dev, "sep_start_handler end\n");
2587         return error;
2588 }
2589
2590 /**
2591  *      ep_check_sum_calc - checksum messages
2592  *      @data: buffer to checksum
2593  *      @length: buffer size
2594  *
2595  *      This function performs a checksum for messages that are sent
2596  *      to the SEP.
2597  */
2598 static u32 sep_check_sum_calc(u8 *data, u32 length)
2599 {
2600         u32 sum = 0;
2601         u16 *Tdata = (u16 *)data;
2602
2603         while (length > 1) {
2604                 /*  This is the inner loop */
2605                 sum += *Tdata++;
2606                 length -= 2;
2607         }
2608
2609         /*  Add left-over byte, if any */
2610         if (length > 0)
2611                 sum += *(u8 *)Tdata;
2612
2613         /*  Fold 32-bit sum to 16 bits */
2614         while (sum>>16)
2615                 sum = (sum & 0xffff) + (sum >> 16);
2616
2617         return ~sum & 0xFFFF;
2618 }
2619
2620 /**
2621  *      sep_init_handler -
2622  *      @sep: pointer to struct sep_device
2623  *      @arg: parameters from user space application
2624  *
2625  *      Handles the request for SEP initialization
2626  *      Note that this will go away for Medfield once the SCU
2627  *      SEP initialization is complete
2628  *      Also note that the message to the SEP has components
2629  *      from user space as well as components written by the driver
2630  *      This is becuase the portions of the message that pertain to
2631  *      physical addresses must be set by the driver after the message
2632  *      leaves custody of the user space application for security
2633  *      reasons.
2634  */
2635 static int sep_init_handler(struct sep_device *sep, unsigned long arg)
2636 {
2637         u32 message_buff[14];
2638         u32 counter;
2639         int error = 0;
2640         u32 reg_val;
2641         dma_addr_t new_base_addr;
2642         unsigned long addr_hold;
2643         struct init_struct command_args;
2644
2645         dev_dbg(&sep->pdev->dev, "sep_init_handler start\n");
2646
2647         /* Make sure that we have not initialized already */
2648         reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2649
2650         if (reg_val != 0x2) {
2651                 error = SEP_ALREADY_INITIALIZED_ERR;
2652                 dev_warn(&sep->pdev->dev, "init; device already initialized\n");
2653                 goto end_function;
2654         }
2655
2656         /* Only root can initialize */
2657         if (!capable(CAP_SYS_ADMIN)) {
2658                 error = -EACCES;
2659                 goto end_function;
2660         }
2661
2662         /* Copy in the parameters */
2663         error = copy_from_user(&command_args, (void __user *)arg,
2664                 sizeof(struct init_struct));
2665
2666         if (error) {
2667                 error = -EFAULT;
2668                 goto end_function;
2669         }
2670
2671         /* Validate parameters */
2672         if (!command_args.message_addr || !command_args.sep_sram_addr ||
2673                 command_args.message_size_in_words > 14) {
2674                 error = -EINVAL;
2675                 goto end_function;
2676         }
2677
2678         /* Copy in the SEP init message */
2679         addr_hold = (unsigned long)command_args.message_addr;
2680         error = copy_from_user(message_buff,
2681                 (void __user *)addr_hold,
2682                 command_args.message_size_in_words*sizeof(u32));
2683
2684         if (error) {
2685                 error = -EFAULT;
2686                 goto end_function;
2687         }
2688
2689         /* Load resident, cache, and extapp firmware */
2690         error = sep_load_firmware(sep);
2691
2692         if (error) {
2693                 dev_warn(&sep->pdev->dev,
2694                         "init; copy SEP init message failed %x\n", error);
2695                 goto end_function;
2696         }
2697
2698         /* Compute the base address */
2699         new_base_addr = sep->shared_bus;
2700
2701         if (sep->resident_bus < new_base_addr)
2702                 new_base_addr = sep->resident_bus;
2703
2704         if (sep->cache_bus < new_base_addr)
2705                 new_base_addr = sep->cache_bus;
2706
2707         if (sep->dcache_bus < new_base_addr)
2708                 new_base_addr = sep->dcache_bus;
2709
2710         /* Put physical addresses in SEP message */
2711         message_buff[3] = (u32)new_base_addr;
2712         message_buff[4] = (u32)sep->shared_bus;
2713         message_buff[6] = (u32)sep->resident_bus;
2714         message_buff[7] = (u32)sep->cache_bus;
2715         message_buff[8] = (u32)sep->dcache_bus;
2716
2717         message_buff[command_args.message_size_in_words - 1] = 0x0;
2718         message_buff[command_args.message_size_in_words - 1] =
2719                 sep_check_sum_calc((u8 *)message_buff,
2720                 command_args.message_size_in_words*sizeof(u32));
2721
2722         /* Debug print of message */
2723         for (counter = 0; counter < command_args.message_size_in_words;
2724                                                                 counter++)
2725                 dev_dbg(&sep->pdev->dev, "init; SEP message word %d is %x\n",
2726                         counter, message_buff[counter]);
2727
2728         /* Tell the SEP the sram address */
2729         sep_write_reg(sep, HW_SRAM_ADDR_REG_ADDR, command_args.sep_sram_addr);
2730
2731         /* Push the message to the SEP */
2732         for (counter = 0; counter < command_args.message_size_in_words;
2733                                                                 counter++) {
2734                 sep_write_reg(sep, HW_SRAM_DATA_REG_ADDR,
2735                                                 message_buff[counter]);
2736                 sep_wait_sram_write(sep);
2737         }
2738
2739         /* Signal SEP that message is ready and to init */
2740         sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x1);
2741
2742         /* Wait for acknowledge */
2743         dev_dbg(&sep->pdev->dev, "init; waiting for msg response\n");
2744
2745         do {
2746                 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2747         } while (!(reg_val & 0xFFFFFFFD));
2748
2749         if (reg_val == 0x1) {
2750                 dev_warn(&sep->pdev->dev, "init; device int failed\n");
2751                 error = sep_read_reg(sep, 0x8060);
2752                 dev_warn(&sep->pdev->dev, "init; sw monitor is %x\n", error);
2753                 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2754                 dev_warn(&sep->pdev->dev, "init; error is %x\n", error);
2755                 goto end_function;
2756         }
2757         dev_dbg(&sep->pdev->dev, "init; end CC INIT, reg_val is %x\n", reg_val);
2758
2759         /* Signal SEP to zero the GPR3 */
2760         sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x10);
2761
2762         /* Wait for response */
2763         dev_dbg(&sep->pdev->dev, "init; waiting for zero set response\n");
2764
2765         do {
2766                 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2767         } while (reg_val != 0);
2768
2769 end_function:
2770         dev_dbg(&sep->pdev->dev, "init is done\n");
2771         return error;
2772 }
2773
2774 /**
2775  *      sep_end_transaction_handler - end transaction
2776  *      @sep: pointer to struct sep_device
2777  *
2778  *      This API handles the end transaction request
2779  */
2780 static int sep_end_transaction_handler(struct sep_device *sep)
2781 {
2782         dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler start\n");
2783
2784         /* Clear the data pool pointers Token */
2785         memset((void *)(sep->shared_addr +
2786                 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES),
2787                 0, sep->num_of_data_allocations*2*sizeof(u32));
2788
2789         /* Check that all the DMA resources were freed */
2790         sep_free_dma_table_data_handler(sep);
2791
2792         clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
2793
2794         /*
2795          * We are now through with the transaction. Let's
2796          * allow other processes who have the device open
2797          * to perform transactions
2798          */
2799         mutex_lock(&sep->sep_mutex);
2800         sep->pid_doing_transaction = 0;
2801         mutex_unlock(&sep->sep_mutex);
2802         /* Raise event for stuck contextes */
2803         wake_up(&sep->event);
2804
2805         dev_dbg(&sep->pdev->dev, "waking up event\n");
2806         dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler end\n");
2807
2808         return 0;
2809 }
2810
2811 /**
2812  *      sep_prepare_dcb_handler - prepare a control block
2813  *      @sep: pointer to struct sep_device
2814  *      @arg: pointer to user parameters
2815  *
2816  *      This function will retrieve the RAR buffer physical addresses, type
2817  *      & size corresponding to the RAR handles provided in the buffers vector.
2818  */
2819 static int sep_prepare_dcb_handler(struct sep_device *sep, unsigned long arg)
2820 {
2821         int error;
2822         /* Command arguments */
2823         struct build_dcb_struct command_args;
2824
2825         dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");
2826
2827         /* Get the command arguments */
2828         if (copy_from_user(&command_args, (void __user *)arg,
2829                                         sizeof(struct build_dcb_struct))) {
2830                 error = -EFAULT;
2831                 goto end_function;
2832         }
2833
2834         dev_dbg(&sep->pdev->dev, "app_in_address is %08llx\n",
2835                                                 command_args.app_in_address);
2836         dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2837                                                 command_args.app_out_address);
2838         dev_dbg(&sep->pdev->dev, "data_size is %x\n",
2839                                                 command_args.data_in_size);
2840         dev_dbg(&sep->pdev->dev, "block_size is %x\n",
2841                                                 command_args.block_size);
2842         dev_dbg(&sep->pdev->dev, "tail block_size is %x\n",
2843                                                 command_args.tail_block_size);
2844
2845         error = sep_prepare_input_output_dma_table_in_dcb(sep,
2846                 (unsigned long)command_args.app_in_address,
2847                 (unsigned long)command_args.app_out_address,
2848                 command_args.data_in_size, command_args.block_size,
2849                 command_args.tail_block_size, true, false);
2850
2851 end_function:
2852         dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler end\n");
2853         return error;
2854
2855 }
2856
2857 /**
2858  *      sep_free_dcb_handler - free control block resources
2859  *      @sep: pointer to struct sep_device
2860  *
2861  *      This function frees the DCB resources and updates the needed
2862  *      user-space buffers.
2863  */
2864 static int sep_free_dcb_handler(struct sep_device *sep)
2865 {
2866         int error ;
2867
2868         dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");
2869         dev_dbg(&sep->pdev->dev, "num of DCBs %x\n", sep->nr_dcb_creat);
2870
2871         error = sep_free_dma_tables_and_dcb(sep, false, false);
2872
2873         dev_dbg(&sep->pdev->dev, "sep_free_dcb_handler end\n");
2874         return error;
2875 }
2876
2877 /**
2878  *      sep_rar_prepare_output_msg_handler - prepare an output message
2879  *      @sep: pointer to struct sep_device
2880  *      @arg: pointer to user parameters
2881  *
2882  *      This function will retrieve the RAR buffer physical addresses, type
2883  *      & size corresponding to the RAR handles provided in the buffers vector.
2884  */
2885 static int sep_rar_prepare_output_msg_handler(struct sep_device *sep,
2886         unsigned long arg)
2887 {
2888         int error = 0;
2889         /* Command args */
2890         struct rar_hndl_to_bus_struct command_args;
2891         struct RAR_buffer rar_buf;
2892         /* Bus address */
2893         dma_addr_t  rar_bus = 0;
2894         /* Holds the RAR address in the system memory offset */
2895         u32 *rar_addr;
2896
2897         dev_dbg(&sep->pdev->dev, "sep_rar_prepare_output_msg_handler start\n");
2898
2899         /* Copy the data */
2900         if (copy_from_user(&command_args, (void __user *)arg,
2901                                                 sizeof(command_args))) {
2902                 error = -EFAULT;
2903                 goto end_function;
2904         }
2905
2906         /* Call to translation function only if user handle is not NULL */
2907         if (command_args.rar_handle) {
2908                 memset(&rar_buf, 0, sizeof(rar_buf));
2909                 rar_buf.info.handle = (u32)command_args.rar_handle;
2910
2911                 if (rar_handle_to_bus(&rar_buf, 1) != 1) {
2912                         dev_dbg(&sep->pdev->dev, "rar_handle_to_bus failure\n");
2913                         error = -EFAULT;
2914                         goto end_function;
2915                 }
2916                 rar_bus = rar_buf.bus_address;
2917         }
2918         dev_dbg(&sep->pdev->dev, "rar msg; rar_addr_bus = %x\n", (u32)rar_bus);
2919
2920         /* Set value in the SYSTEM MEMORY offset */
2921         rar_addr = (u32 *)(sep->shared_addr +
2922                 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2923
2924         /* Copy the physical address to the System Area for the SEP */
2925         rar_addr[0] = SEP_RAR_VAL_TOKEN;
2926         rar_addr[1] = rar_bus;
2927
2928 end_function:
2929         dev_dbg(&sep->pdev->dev, "sep_rar_prepare_output_msg_handler start\n");
2930         return error;
2931 }
2932
2933 /**
2934  *      sep_realloc_ext_cache_handler - report location of extcache
2935  *      @sep: pointer to struct sep_device
2936  *      @arg: pointer to user parameters
2937  *
2938  *      This function tells the SEP where the extapp is located
2939  */
2940 static int sep_realloc_ext_cache_handler(struct sep_device *sep,
2941         unsigned long arg)
2942 {
2943         /* Holds the new ext cache address in the system memory offset */
2944         u32 *system_addr;
2945
2946         /* Set value in the SYSTEM MEMORY offset */
2947         system_addr = (u32 *)(sep->shared_addr +
2948                 SEP_DRIVER_SYSTEM_EXT_CACHE_ADDR_OFFSET_IN_BYTES);
2949
2950         /* Copy the physical address to the System Area for the SEP */
2951         system_addr[0] = SEP_EXT_CACHE_ADDR_VAL_TOKEN;
2952         dev_dbg(&sep->pdev->dev, "ext cache init; system addr 0 is %x\n",
2953                                                         system_addr[0]);
2954         system_addr[1] = sep->extapp_bus;
2955         dev_dbg(&sep->pdev->dev, "ext cache init; system addr 1 is %x\n",
2956                                                         system_addr[1]);
2957
2958         return 0;
2959 }
2960
2961 /**
2962  *      sep_ioctl - ioctl api
2963  *      @filp: pointer to struct file
2964  *      @cmd: command
2965  *      @arg: pointer to argument structure
2966  *
2967  *      Implement the ioctl methods availble on the SEP device.
2968  */
2969 static long sep_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2970 {
2971         int error = 0;
2972         struct sep_device *sep = filp->private_data;
2973
2974         dev_dbg(&sep->pdev->dev, "ioctl start\n");
2975
2976         dev_dbg(&sep->pdev->dev, "cmd is %x\n", cmd);
2977
2978         /* Make sure we own this device */
2979         mutex_lock(&sep->sep_mutex);
2980         if ((current->pid != sep->pid_doing_transaction) &&
2981                                 (sep->pid_doing_transaction != 0)) {
2982                 dev_dbg(&sep->pdev->dev, "ioctl pid is not owner\n");
2983                 mutex_unlock(&sep->sep_mutex);
2984                 error = -EACCES;
2985                 goto end_function;
2986         }
2987
2988         mutex_unlock(&sep->sep_mutex);
2989
2990         /* Check that the command is for SEP device */
2991         if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
2992                 error = -ENOTTY;
2993                 goto end_function;
2994         }
2995
2996         /* Lock to prevent the daemon to interfere with operation */
2997         mutex_lock(&sep->ioctl_mutex);
2998
2999         switch (cmd) {
3000         case SEP_IOCSENDSEPCOMMAND:
3001                 /* Send command to SEP */
3002                 error = sep_send_command_handler(sep);
3003                 break;
3004         case SEP_IOCALLOCDATAPOLL:
3005                 /* Allocate data pool */
3006                 error = sep_allocate_data_pool_memory_handler(sep, arg);
3007                 break;
3008         case SEP_IOCCREATESYMDMATABLE:
3009                 /* Create DMA table for synhronic operation */
3010                 error = sep_create_sync_dma_tables_handler(sep, arg);
3011                 break;
3012         case SEP_IOCFREEDMATABLEDATA:
3013                 /* Free the pages */
3014                 error = sep_free_dma_table_data_handler(sep);
3015                 break;
3016         case SEP_IOCSEPSTART:
3017                 /* Start command to SEP */
3018                 if (sep->pdev->revision == 0) /* Only for old chip */
3019                         error = sep_start_handler(sep);
3020                 else
3021                         error = -EPERM; /* Not permitted on new chip */
3022                 break;
3023         case SEP_IOCSEPINIT:
3024                 /* Init command to SEP */
3025                 if (sep->pdev->revision == 0) /* Only for old chip */
3026                         error = sep_init_handler(sep, arg);
3027                 else
3028                         error = -EPERM; /* Not permitted on new chip */
3029                 break;
3030         case SEP_IOCGETSTATICPOOLADDR:
3031                 /* Get the physical and virtual addresses of the static pool */
3032                 error = sep_get_static_pool_addr_handler(sep);
3033                 break;
3034         case SEP_IOCENDTRANSACTION:
3035                 error = sep_end_transaction_handler(sep);
3036                 break;
3037         case SEP_IOCREALLOCEXTCACHE:
3038                 if (sep->pdev->revision == 0) /* Only for old chip */
3039                         error = sep_realloc_ext_cache_handler(sep, arg);
3040                 else
3041                         error = -EPERM; /* Not permitted on new chip */
3042                 break;
3043         case SEP_IOCRARPREPAREMESSAGE:
3044                 error = sep_rar_prepare_output_msg_handler(sep, arg);
3045                 break;
3046         case SEP_IOCPREPAREDCB:
3047                 error = sep_prepare_dcb_handler(sep, arg);
3048                 break;
3049         case SEP_IOCFREEDCB:
3050                 error = sep_free_dcb_handler(sep);
3051                 break;
3052         default:
3053                 dev_dbg(&sep->pdev->dev, "invalid ioctl %x\n", cmd);
3054                 error = -ENOTTY;
3055                 break;
3056         }
3057         mutex_unlock(&sep->ioctl_mutex);
3058
3059 end_function:
3060         dev_dbg(&sep->pdev->dev, "ioctl end\n");
3061         return error;
3062 }
3063
3064 /**
3065  *      sep_singleton_ioctl - ioctl api for singleton interface
3066  *      @filp: pointer to struct file
3067  *      @cmd: command
3068  *      @arg: pointer to argument structure
3069  *
3070  *      Implement the additional ioctls for the singleton device
3071  */
3072 static long sep_singleton_ioctl(struct file  *filp, u32 cmd, unsigned long arg)
3073 {
3074         long error = 0;
3075         struct sep_device *sep = filp->private_data;
3076
3077         dev_dbg(&sep->pdev->dev, "singleton_ioctl start\n");
3078         dev_dbg(&sep->pdev->dev, "cmd is %x\n", cmd);
3079
3080         /* Check that the command is for the SEP device */
3081         if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
3082                 error =  -ENOTTY;
3083                 goto end_function;
3084         }
3085
3086         /* Make sure we own this device */
3087         mutex_lock(&sep->sep_mutex);
3088         if ((current->pid != sep->pid_doing_transaction) &&
3089                                 (sep->pid_doing_transaction != 0)) {
3090                 dev_dbg(&sep->pdev->dev, "singleton ioctl pid is not owner\n");
3091                 mutex_unlock(&sep->sep_mutex);
3092                 error = -EACCES;
3093                 goto end_function;
3094         }
3095
3096         mutex_unlock(&sep->sep_mutex);
3097
3098         switch (cmd) {
3099         case SEP_IOCTLSETCALLERID:
3100                 mutex_lock(&sep->ioctl_mutex);
3101                 error = sep_set_caller_id_handler(sep, arg);
3102                 mutex_unlock(&sep->ioctl_mutex);
3103                 break;
3104         default:
3105                 error = sep_ioctl(filp, cmd, arg);
3106                 break;
3107         }
3108
3109 end_function:
3110         dev_dbg(&sep->pdev->dev, "singleton ioctl end\n");
3111         return error;
3112 }
3113
3114 /**
3115  *      sep_request_daemon_ioctl - ioctl for daemon
3116  *      @filp: pointer to struct file
3117  *      @cmd: command
3118  *      @arg: pointer to argument structure
3119  *
3120  *      Called by the request daemon to perform ioctls on the daemon device
3121  */
3122 static long sep_request_daemon_ioctl(struct file *filp, u32 cmd,
3123         unsigned long arg)
3124 {
3125
3126         long error;
3127         struct sep_device *sep = filp->private_data;
3128
3129         dev_dbg(&sep->pdev->dev, "daemon ioctl: start\n");
3130         dev_dbg(&sep->pdev->dev, "daemon ioctl: cmd is %x\n", cmd);
3131
3132         /* Check that the command is for SEP device */
3133         if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
3134                 error = -ENOTTY;
3135                 goto end_function;
3136         }
3137
3138         /* Only one process can access ioctl at any given time */
3139         mutex_lock(&sep->ioctl_mutex);
3140
3141         switch (cmd) {
3142         case SEP_IOCSENDSEPRPLYCOMMAND:
3143                 /* Send reply command to SEP */
3144                 error = sep_req_daemon_send_reply_command_handler(sep);
3145                 break;
3146         case SEP_IOCENDTRANSACTION:
3147                 /*
3148                  * End req daemon transaction, do nothing
3149                  * will be removed upon update in middleware
3150                  * API library
3151                  */
3152                 error = 0;
3153                 break;
3154         default:
3155                 dev_dbg(&sep->pdev->dev, "daemon ioctl: no such IOCTL\n");
3156                 error = -ENOTTY;
3157         }
3158         mutex_unlock(&sep->ioctl_mutex);
3159
3160 end_function:
3161         dev_dbg(&sep->pdev->dev, "daemon ioctl: end\n");
3162         return error;
3163
3164 }
3165
3166 /**
3167  *      sep_inthandler - interrupt handler
3168  *      @irq: interrupt
3169  *      @dev_id: device id
3170  */
3171 static irqreturn_t sep_inthandler(int irq, void *dev_id)
3172 {
3173         irqreturn_t int_error = IRQ_HANDLED;
3174         unsigned long lck_flags;
3175         u32 reg_val, reg_val2 = 0;
3176         struct sep_device *sep = dev_id;
3177
3178         /* Read the IRR register to check if this is SEP interrupt */
3179         reg_val = sep_read_reg(sep, HW_HOST_IRR_REG_ADDR);
3180         dev_dbg(&sep->pdev->dev, "SEP Interrupt - reg is %08x\n", reg_val);
3181
3182         if (reg_val & (0x1 << 13)) {
3183                 /* Lock and update the counter of reply messages */
3184                 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
3185                 sep->reply_ct++;
3186                 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
3187
3188                 dev_dbg(&sep->pdev->dev, "sep int: send_ct %lx reply_ct %lx\n",
3189                                         sep->send_ct, sep->reply_ct);
3190
3191                 /* Is this printf or daemon request? */
3192                 reg_val2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3193                 dev_dbg(&sep->pdev->dev,
3194                         "SEP Interrupt - reg2 is %08x\n", reg_val2);
3195
3196                 if ((reg_val2 >> 30) & 0x1) {
3197                         dev_dbg(&sep->pdev->dev, "int: printf request\n");
3198                         wake_up(&sep->event_request_daemon);
3199                 } else if (reg_val2 >> 31) {
3200                         dev_dbg(&sep->pdev->dev, "int: daemon request\n");
3201                         wake_up(&sep->event_request_daemon);
3202                 } else {
3203                         dev_dbg(&sep->pdev->dev, "int: SEP reply\n");
3204                         wake_up(&sep->event);
3205                 }
3206         } else {
3207                 dev_dbg(&sep->pdev->dev, "int: not SEP interrupt\n");
3208                 int_error = IRQ_NONE;
3209         }
3210         if (int_error == IRQ_HANDLED)
3211                 sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, reg_val);
3212
3213         return int_error;
3214 }
3215
3216 /**
3217  *      sep_reconfig_shared_area - reconfigure shared area
3218  *      @sep: pointer to struct sep_device
3219  *
3220  *      Reconfig the shared area between HOST and SEP - needed in case
3221  *      the DX_CC_Init function was called before OS loading.
3222  */
3223 static int sep_reconfig_shared_area(struct sep_device *sep)
3224 {
3225         int ret_val;
3226
3227         /* use to limit waiting for SEP */
3228         unsigned long end_time;
3229
3230         dev_dbg(&sep->pdev->dev, "reconfig shared area start\n");
3231
3232         /* Send the new SHARED MESSAGE AREA to the SEP */
3233         dev_dbg(&sep->pdev->dev, "sending %08llx to sep\n",
3234                                 (unsigned long long)sep->shared_bus);
3235
3236         sep_write_reg(sep, HW_HOST_HOST_SEP_GPR1_REG_ADDR, sep->shared_bus);
3237
3238         /* Poll for SEP response */
3239         ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3240
3241         end_time = jiffies + (WAIT_TIME * HZ);
3242
3243         while ((time_before(jiffies, end_time)) && (ret_val != 0xffffffff) &&
3244                 (ret_val != sep->shared_bus))
3245                 ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3246
3247         /* Check the return value (register) */
3248         if (ret_val != sep->shared_bus) {
3249                 dev_warn(&sep->pdev->dev, "could not reconfig shared area\n");
3250                 dev_warn(&sep->pdev->dev, "result was %x\n", ret_val);
3251                 ret_val = -ENOMEM;
3252         } else
3253                 ret_val = 0;
3254
3255         dev_dbg(&sep->pdev->dev, "reconfig shared area end\n");
3256         return ret_val;
3257 }
3258
3259 /* File operation for singleton SEP operations */
3260 static const struct file_operations singleton_file_operations = {
3261         .owner = THIS_MODULE,
3262         .unlocked_ioctl = sep_singleton_ioctl,
3263         .poll = sep_poll,
3264         .open = sep_singleton_open,
3265         .release = sep_singleton_release,
3266         .mmap = sep_mmap,
3267 };
3268
3269 /* File operation for daemon operations */
3270 static const struct file_operations daemon_file_operations = {
3271         .owner = THIS_MODULE,
3272         .unlocked_ioctl = sep_request_daemon_ioctl,
3273         .poll = sep_request_daemon_poll,
3274         .open = sep_request_daemon_open,
3275         .release = sep_request_daemon_release,
3276         .mmap = sep_request_daemon_mmap,
3277 };
3278
3279 /* The files operations structure of the driver */
3280 static const struct file_operations sep_file_operations = {
3281         .owner = THIS_MODULE,
3282         .unlocked_ioctl = sep_ioctl,
3283         .poll = sep_poll,
3284         .open = sep_open,
3285         .release = sep_release,
3286         .mmap = sep_mmap,
3287 };
3288
3289 /**
3290  *      sep_register_driver_with_fs - register misc devices
3291  *      @sep: pointer to struct sep_device
3292  *
3293  *      This function registers the driver with the file system
3294  */
3295 static int sep_register_driver_with_fs(struct sep_device *sep)
3296 {
3297         int ret_val;
3298
3299         sep->miscdev_sep.minor = MISC_DYNAMIC_MINOR;
3300         sep->miscdev_sep.name = SEP_DEV_NAME;
3301         sep->miscdev_sep.fops = &sep_file_operations;
3302
3303         sep->miscdev_singleton.minor = MISC_DYNAMIC_MINOR;
3304         sep->miscdev_singleton.name = SEP_DEV_SINGLETON;
3305         sep->miscdev_singleton.fops = &singleton_file_operations;
3306
3307         sep->miscdev_daemon.minor = MISC_DYNAMIC_MINOR;
3308         sep->miscdev_daemon.name = SEP_DEV_DAEMON;
3309         sep->miscdev_daemon.fops = &daemon_file_operations;
3310
3311         ret_val = misc_register(&sep->miscdev_sep);
3312         if (ret_val) {
3313                 dev_warn(&sep->pdev->dev, "misc reg fails for SEP %x\n",
3314                         ret_val);
3315                 return ret_val;
3316         }
3317
3318         ret_val = misc_register(&sep->miscdev_singleton);
3319         if (ret_val) {
3320                 dev_warn(&sep->pdev->dev, "misc reg fails for sing %x\n",
3321                         ret_val);
3322                 misc_deregister(&sep->miscdev_sep);
3323                 return ret_val;
3324         }
3325
3326         ret_val = misc_register(&sep->miscdev_daemon);
3327         if (ret_val) {
3328                 dev_warn(&sep->pdev->dev, "misc reg fails for dmn %x\n",
3329                         ret_val);
3330                 misc_deregister(&sep->miscdev_sep);
3331                 misc_deregister(&sep->miscdev_singleton);
3332
3333                 return ret_val;
3334         }
3335         return ret_val;
3336 }
3337
3338
3339 /**
3340  *      sep_probe - probe a matching PCI device
3341  *      @pdev: pci_device
3342  *      @end: pci_device_id
3343  *
3344  *      Attempt to set up and configure a SEP device that has been
3345  *      discovered by the PCI layer.
3346  */
3347 static int __devinit sep_probe(struct pci_dev *pdev,
3348         const struct pci_device_id *ent)
3349 {
3350         int error = 0;
3351         struct sep_device *sep;
3352
3353         pr_debug("SEP pci probe starting\n");
3354         if (sep_dev != NULL) {
3355                 dev_warn(&pdev->dev, "only one SEP supported.\n");
3356                 return -EBUSY;
3357         }
3358
3359         /* Enable the device */
3360         error = pci_enable_device(pdev);
3361         if (error) {
3362                 dev_warn(&pdev->dev, "error enabling pci device\n");
3363                 goto end_function;
3364         }
3365
3366         /* Allocate the sep_device structure for this device */
3367         sep_dev = kzalloc(sizeof(struct sep_device), GFP_ATOMIC);
3368         if (sep_dev == NULL) {
3369                 dev_warn(&pdev->dev,
3370                         "can't kmalloc the sep_device structure\n");
3371                 error = -ENOMEM;
3372                 goto end_function_disable_device;
3373         }
3374
3375         /*
3376          * We're going to use another variable for actually
3377          * working with the device; this way, if we have
3378          * multiple devices in the future, it would be easier
3379          * to make appropriate changes
3380          */
3381         sep = sep_dev;
3382
3383         sep->pdev = pci_dev_get(pdev);
3384
3385         init_waitqueue_head(&sep->event);
3386         init_waitqueue_head(&sep->event_request_daemon);
3387         spin_lock_init(&sep->snd_rply_lck);
3388         mutex_init(&sep->sep_mutex);
3389         mutex_init(&sep->ioctl_mutex);
3390
3391         dev_dbg(&sep->pdev->dev, "PCI obtained, device being prepared\n");
3392         dev_dbg(&sep->pdev->dev, "revision is %d\n", sep->pdev->revision);
3393
3394         /* Set up our register area */
3395         sep->reg_physical_addr = pci_resource_start(sep->pdev, 0);
3396         if (!sep->reg_physical_addr) {
3397                 dev_warn(&sep->pdev->dev, "Error getting register start\n");
3398                 error = -ENODEV;
3399                 goto end_function_free_sep_dev;
3400         }
3401
3402         sep->reg_physical_end = pci_resource_end(sep->pdev, 0);
3403         if (!sep->reg_physical_end) {
3404                 dev_warn(&sep->pdev->dev, "Error getting register end\n");
3405                 error = -ENODEV;
3406                 goto end_function_free_sep_dev;
3407         }
3408
3409         sep->reg_addr = ioremap_nocache(sep->reg_physical_addr,
3410                 (size_t)(sep->reg_physical_end - sep->reg_physical_addr + 1));
3411         if (!sep->reg_addr) {
3412                 dev_warn(&sep->pdev->dev, "Error getting register virtual\n");
3413                 error = -ENODEV;
3414                 goto end_function_free_sep_dev;
3415         }
3416
3417         dev_dbg(&sep->pdev->dev,
3418                 "Register area start %llx end %llx virtual %p\n",
3419                 (unsigned long long)sep->reg_physical_addr,
3420                 (unsigned long long)sep->reg_physical_end,
3421                 sep->reg_addr);
3422
3423         /* Allocate the shared area */
3424         sep->shared_size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
3425                 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES +
3426                 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES +
3427                 SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES +
3428                 SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;
3429
3430         if (sep_map_and_alloc_shared_area(sep)) {
3431                 error = -ENOMEM;
3432                 /* Allocation failed */
3433                 goto end_function_error;
3434         }
3435
3436         sep->rar_size = FAKE_RAR_SIZE;
3437         sep->rar_addr = dma_alloc_coherent(NULL,
3438                 sep->rar_size, &sep->rar_bus, GFP_KERNEL);
3439         if (sep->rar_addr == NULL) {
3440                 dev_warn(&sep->pdev->dev, "can't allocate mfld rar\n");
3441                 error = -ENOMEM;
3442                 goto end_function_deallocate_sep_shared_area;
3443         }
3444
3445         dev_dbg(&sep->pdev->dev, "rar start is %p, phy is %llx,"
3446                 " size is %zx\n", sep->rar_addr,
3447                 (unsigned long long)sep->rar_bus,
3448                 sep->rar_size);
3449
3450         dev_dbg(&sep->pdev->dev, "about to write IMR and ICR REG_ADDR\n");
3451
3452         /* Clear ICR register */
3453         sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, 0xFFFFFFFF);
3454
3455         /* Set the IMR register - open only GPR 2 */
3456         sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
3457
3458         /* Read send/receive counters from SEP */
3459         sep->reply_ct = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3460         sep->reply_ct &= 0x3FFFFFFF;
3461         sep->send_ct = sep->reply_ct;
3462
3463         dev_dbg(&sep->pdev->dev, "about to call request_irq\n");
3464         /* Get the interrupt line */
3465         error = request_irq(pdev->irq, sep_inthandler, IRQF_SHARED,
3466                 "sep_driver", sep);
3467
3468         if (error)
3469                 goto end_function_dealloc_rar;
3470
3471         /* The new chip requires ashared area reconfigure */
3472         if (sep->pdev->revision == 4) { /* Only for new chip */
3473                 error = sep_reconfig_shared_area(sep);
3474                 if (error)
3475                         goto end_function_free_irq;
3476         }
3477         /* Finally magic up the device nodes */
3478         /* Register driver with the fs */
3479         error = sep_register_driver_with_fs(sep);
3480         if (error == 0)
3481                 /* Success */
3482                 return 0;
3483
3484 end_function_free_irq:
3485         free_irq(pdev->irq, sep);
3486
3487 end_function_dealloc_rar:
3488         if (sep->rar_addr)
3489                 dma_free_coherent(&sep->pdev->dev, sep->rar_size,
3490                         sep->rar_addr, sep->rar_bus);
3491         goto end_function;
3492
3493 end_function_deallocate_sep_shared_area:
3494         /* De-allocate shared area */
3495         sep_unmap_and_free_shared_area(sep);
3496
3497 end_function_error:
3498         iounmap(sep->reg_addr);
3499
3500 end_function_free_sep_dev:
3501         pci_dev_put(sep_dev->pdev);
3502         kfree(sep_dev);
3503         sep_dev = NULL;
3504
3505 end_function_disable_device:
3506         pci_disable_device(pdev);
3507
3508 end_function:
3509         return error;
3510 }
3511
3512 static void sep_remove(struct pci_dev *pdev)
3513 {
3514         struct sep_device *sep = sep_dev;
3515
3516         /* Unregister from fs */
3517         misc_deregister(&sep->miscdev_sep);
3518         misc_deregister(&sep->miscdev_singleton);
3519         misc_deregister(&sep->miscdev_daemon);
3520
3521         /* Free the irq */
3522         free_irq(sep->pdev->irq, sep);
3523
3524         /* Free the shared area  */
3525         sep_unmap_and_free_shared_area(sep_dev);
3526         iounmap((void *) sep_dev->reg_addr);
3527 }
3528
3529 static DEFINE_PCI_DEVICE_TABLE(sep_pci_id_tbl) = {
3530         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MFLD_PCI_DEVICE_ID)},
3531         {0}
3532 };
3533
3534 MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);
3535
3536 /* Field for registering driver to PCI device */
3537 static struct pci_driver sep_pci_driver = {
3538         .name = "sep_sec_driver",
3539         .id_table = sep_pci_id_tbl,
3540         .probe = sep_probe,
3541         .remove = sep_remove
3542 };
3543
3544
3545 /**
3546  *      sep_init - init function
3547  *
3548  *      Module load time. Register the PCI device driver.
3549  */
3550 static int __init sep_init(void)
3551 {
3552         return pci_register_driver(&sep_pci_driver);
3553 }
3554
3555
3556 /**
3557  *      sep_exit - called to unload driver
3558  *
3559  *      Drop the misc devices then remove and unmap the various resources
3560  *      that are not released by the driver remove method.
3561  */
3562 static void __exit sep_exit(void)
3563 {
3564         pci_unregister_driver(&sep_pci_driver);
3565 }
3566
3567
3568 module_init(sep_init);
3569 module_exit(sep_exit);
3570
3571 MODULE_LICENSE("GPL");