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