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