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