3 * sep_driver.c - Security Processor Driver main group of functions
5 * Copyright(c) 2009,2010 Intel Corporation. All rights reserved.
6 * Contributions(c) 2009,2010 Discretix. All rights reserved.
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.
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
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.
23 * Mark Allyn mark.a.allyn@intel.com
24 * Jayant Mangalampalli jayant.mangalampalli@intel.com
28 * 2009.06.26 Initial publish
29 * 2010.09.14 Upgrade to Medfield
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/miscdevice.h>
37 #include <linux/cdev.h>
38 #include <linux/kdev_t.h>
39 #include <linux/mutex.h>
40 #include <linux/sched.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>
51 #include <linux/interrupt.h>
52 #include <linux/pagemap.h>
53 #include <asm/cacheflush.h>
54 #include <linux/sched.h>
55 #include <linux/delay.h>
56 #include <linux/jiffies.h>
57 #include <linux/rar_register.h>
59 #include "../memrar/memrar.h"
61 #include "sep_driver_hw_defs.h"
62 #include "sep_driver_config.h"
63 #include "sep_driver_api.h"
66 /*----------------------------------------
68 -----------------------------------------*/
70 #define SEP_RAR_IO_MEM_REGION_SIZE 0x40000
72 /*--------------------------------------------
74 --------------------------------------------*/
76 /* Keep this a single static object for now to keep the conversion easy */
78 static struct sep_device *sep_dev;
81 * sep_load_firmware - copy firmware cache/resident
82 * @sep: pointer to struct sep_device we are loading
84 * This functions copies the cache and resident from their source
85 * location into destination shared memory.
87 static int sep_load_firmware(struct sep_device *sep)
89 const struct firmware *fw;
90 char *cache_name = "cache.image.bin";
91 char *res_name = "resident.image.bin";
92 char *extapp_name = "extapp.image.bin";
94 unsigned long work1, work2, work3;
96 /* Set addresses and load resident */
97 sep->resident_bus = sep->rar_bus;
98 sep->resident_addr = sep->rar_addr;
100 error = request_firmware(&fw, res_name, &sep->pdev->dev);
102 dev_warn(&sep->pdev->dev, "can't request resident fw\n");
106 memcpy(sep->resident_addr, (void *)fw->data, fw->size);
107 sep->resident_size = fw->size;
108 release_firmware(fw);
110 dev_dbg(&sep->pdev->dev, "resident virtual is %p\n",
112 dev_dbg(&sep->pdev->dev, "resident bus is %lx\n",
113 (unsigned long)sep->resident_bus);
114 dev_dbg(&sep->pdev->dev, "resident size is %08zx\n",
117 /* Set addresses for dcache (no loading needed) */
118 work1 = (unsigned long)sep->resident_bus;
119 work2 = (unsigned long)sep->resident_size;
120 work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
121 sep->dcache_bus = (dma_addr_t)work3;
123 work1 = (unsigned long)sep->resident_addr;
124 work2 = (unsigned long)sep->resident_size;
125 work3 = (work1 + work2 + (1024 * 4)) & 0xfffff000;
126 sep->dcache_addr = (void *)work3;
128 sep->dcache_size = 1024 * 128;
130 /* Set addresses and load cache */
131 sep->cache_bus = sep->dcache_bus + sep->dcache_size;
132 sep->cache_addr = sep->dcache_addr + sep->dcache_size;
134 error = request_firmware(&fw, cache_name, &sep->pdev->dev);
136 dev_warn(&sep->pdev->dev, "Unable to request cache firmware\n");
140 memcpy(sep->cache_addr, (void *)fw->data, fw->size);
141 sep->cache_size = fw->size;
142 release_firmware(fw);
144 dev_dbg(&sep->pdev->dev, "cache virtual is %p\n",
146 dev_dbg(&sep->pdev->dev, "cache bus is %08lx\n",
147 (unsigned long)sep->cache_bus);
148 dev_dbg(&sep->pdev->dev, "cache size is %08zx\n",
151 /* Set addresses and load extapp */
152 sep->extapp_bus = sep->cache_bus + (1024 * 370);
153 sep->extapp_addr = sep->cache_addr + (1024 * 370);
155 error = request_firmware(&fw, extapp_name, &sep->pdev->dev);
157 dev_warn(&sep->pdev->dev, "Unable to request extapp firmware\n");
161 memcpy(sep->extapp_addr, (void *)fw->data, fw->size);
162 sep->extapp_size = fw->size;
163 release_firmware(fw);
165 dev_dbg(&sep->pdev->dev, "extapp virtual is %p\n",
167 dev_dbg(&sep->pdev->dev, "extapp bus is %08llx\n",
168 (unsigned long long)sep->extapp_bus);
169 dev_dbg(&sep->pdev->dev, "extapp size is %08zx\n",
175 MODULE_FIRMWARE("sep/cache.image.bin");
176 MODULE_FIRMWARE("sep/resident.image.bin");
177 MODULE_FIRMWARE("sep/extapp.image.bin");
180 * sep_dump_message - dump the message that is pending
183 static void sep_dump_message(struct sep_device *sep)
186 u32 *p = sep->shared_addr;
187 for (count = 0; count < 12 * 4; count += 4)
188 dev_dbg(&sep->pdev->dev, "Word %d of the message is %x\n",
193 * sep_map_and_alloc_shared_area - allocate shared block
194 * @sep: security processor
195 * @size: size of shared area
197 static int sep_map_and_alloc_shared_area(struct sep_device *sep)
199 sep->shared_addr = dma_alloc_coherent(&sep->pdev->dev,
201 &sep->shared_bus, GFP_KERNEL);
203 if (!sep->shared_addr) {
204 dev_warn(&sep->pdev->dev,
205 "shared memory dma_alloc_coherent failed\n");
208 dev_dbg(&sep->pdev->dev,
209 "shared_addr %zx bytes @%p (bus %llx)\n",
210 sep->shared_size, sep->shared_addr,
211 (unsigned long long)sep->shared_bus);
216 * sep_unmap_and_free_shared_area - free shared block
217 * @sep: security processor
219 static void sep_unmap_and_free_shared_area(struct sep_device *sep)
221 dev_dbg(&sep->pdev->dev, "shared area unmap and free\n");
222 dma_free_coherent(&sep->pdev->dev, sep->shared_size,
223 sep->shared_addr, sep->shared_bus);
227 * sep_shared_bus_to_virt - convert bus/virt addresses
228 * @sep: pointer to struct sep_device
229 * @bus_address: address to convert
231 * Returns virtual address inside the shared area according
232 * to the bus address.
234 static void *sep_shared_bus_to_virt(struct sep_device *sep,
235 dma_addr_t bus_address)
237 return sep->shared_addr + (bus_address - sep->shared_bus);
241 * open function for the singleton driver
242 * @inode_ptr struct inode *
243 * @file_ptr struct file *
245 * Called when the user opens the singleton device interface
247 static int sep_singleton_open(struct inode *inode_ptr, struct file *file_ptr)
250 struct sep_device *sep;
253 * Get the SEP device structure and use it for the
254 * private_data field in filp for other methods
258 file_ptr->private_data = sep;
260 dev_dbg(&sep->pdev->dev, "Singleton open for pid %d\n", current->pid);
262 dev_dbg(&sep->pdev->dev, "calling test and set for singleton 0\n");
263 if (test_and_set_bit(0, &sep->singleton_access_flag)) {
268 dev_dbg(&sep->pdev->dev, "sep_singleton_open end\n");
274 * sep_open - device open method
275 * @inode: inode of SEP device
276 * @filp: file handle to SEP device
278 * Open method for the SEP device. Called when userspace opens
279 * the SEP device node.
281 * Returns zero on success otherwise an error code.
283 static int sep_open(struct inode *inode, struct file *filp)
285 struct sep_device *sep;
288 * Get the SEP device structure and use it for the
289 * private_data field in filp for other methods
292 filp->private_data = sep;
294 dev_dbg(&sep->pdev->dev, "Open for pid %d\n", current->pid);
296 /* Anyone can open; locking takes place at transaction level */
301 * sep_singleton_release - close a SEP singleton device
302 * @inode: inode of SEP device
303 * @filp: file handle being closed
305 * Called on the final close of a SEP device. As the open protects against
306 * multiple simultaenous opens that means this method is called when the
307 * final reference to the open handle is dropped.
309 static int sep_singleton_release(struct inode *inode, struct file *filp)
311 struct sep_device *sep = filp->private_data;
313 dev_dbg(&sep->pdev->dev, "Singleton release for pid %d\n",
315 clear_bit(0, &sep->singleton_access_flag);
320 * sep_request_daemonopen - request daemon open method
321 * @inode: inode of SEP device
322 * @filp: file handle to SEP device
324 * Open method for the SEP request daemon. Called when
325 * request daemon in userspace opens the SEP device node.
327 * Returns zero on success otherwise an error code.
329 static int sep_request_daemon_open(struct inode *inode, struct file *filp)
331 struct sep_device *sep = sep_dev;
334 filp->private_data = sep;
336 dev_dbg(&sep->pdev->dev, "Request daemon open for pid %d\n",
339 /* There is supposed to be only one request daemon */
340 dev_dbg(&sep->pdev->dev, "calling test and set for req_dmon open 0\n");
341 if (test_and_set_bit(0, &sep->request_daemon_open))
347 * sep_request_daemon_release - close a SEP daemon
348 * @inode: inode of SEP device
349 * @filp: file handle being closed
351 * Called on the final close of a SEP daemon.
353 static int sep_request_daemon_release(struct inode *inode, struct file *filp)
355 struct sep_device *sep = filp->private_data;
357 dev_dbg(&sep->pdev->dev, "Reques daemon release for pid %d\n",
360 /* Clear the request_daemon_open flag */
361 clear_bit(0, &sep->request_daemon_open);
366 * sep_req_daemon_send_reply_command_handler - poke the SEP
367 * @sep: struct sep_device *
369 * This function raises interrupt to SEPm that signals that is has a
370 * new command from HOST
372 static int sep_req_daemon_send_reply_command_handler(struct sep_device *sep)
374 unsigned long lck_flags;
376 dev_dbg(&sep->pdev->dev,
377 "sep_req_daemon_send_reply_command_handler start\n");
379 sep_dump_message(sep);
381 /* Counters are lockable region */
382 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
386 /* Send the interrupt to SEP */
387 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR2_REG_ADDR, sep->send_ct);
390 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
392 dev_dbg(&sep->pdev->dev,
393 "sep_req_daemon_send_reply send_ct %lx reply_ct %lx\n",
394 sep->send_ct, sep->reply_ct);
396 dev_dbg(&sep->pdev->dev,
397 "sep_req_daemon_send_reply_command_handler end\n");
404 * sep_free_dma_table_data_handler - free DMA table
405 * @sep: pointere to struct sep_device
407 * Handles the request to free DMA table for synchronic actions
409 static int sep_free_dma_table_data_handler(struct sep_device *sep)
413 /* Pointer to the current dma_resource struct */
414 struct sep_dma_resource *dma;
416 dev_dbg(&sep->pdev->dev, "sep_free_dma_table_data_handler start\n");
418 for (dcb_counter = 0; dcb_counter < sep->nr_dcb_creat; dcb_counter++) {
419 dma = &sep->dma_res_arr[dcb_counter];
421 /* Unmap and free input map array */
422 if (dma->in_map_array) {
423 for (count = 0; count < dma->in_num_pages; count++) {
424 dma_unmap_page(&sep->pdev->dev,
425 dma->in_map_array[count].dma_addr,
426 dma->in_map_array[count].size,
429 kfree(dma->in_map_array);
432 /* Unmap output map array, DON'T free it yet */
433 if (dma->out_map_array) {
434 for (count = 0; count < dma->out_num_pages; count++) {
435 dma_unmap_page(&sep->pdev->dev,
436 dma->out_map_array[count].dma_addr,
437 dma->out_map_array[count].size,
440 kfree(dma->out_map_array);
443 /* Free page cache for output */
444 if (dma->in_page_array) {
445 for (count = 0; count < dma->in_num_pages; count++) {
446 flush_dcache_page(dma->in_page_array[count]);
447 page_cache_release(dma->in_page_array[count]);
449 kfree(dma->in_page_array);
452 if (dma->out_page_array) {
453 for (count = 0; count < dma->out_num_pages; count++) {
454 if (!PageReserved(dma->out_page_array[count]))
455 SetPageDirty(dma->out_page_array[count]);
456 flush_dcache_page(dma->out_page_array[count]);
457 page_cache_release(dma->out_page_array[count]);
459 kfree(dma->out_page_array);
462 /* Reset all the values */
463 dma->in_page_array = NULL;
464 dma->out_page_array = NULL;
465 dma->in_num_pages = 0;
466 dma->out_num_pages = 0;
467 dma->in_map_array = NULL;
468 dma->out_map_array = NULL;
469 dma->in_map_num_entries = 0;
470 dma->out_map_num_entries = 0;
473 sep->nr_dcb_creat = 0;
474 sep->num_lli_tables_created = 0;
476 dev_dbg(&sep->pdev->dev, "sep_free_dma_table_data_handler end\n");
481 * sep_request_daemon_mmap - maps the shared area to user space
482 * @filp: pointer to struct file
483 * @vma: pointer to vm_area_struct
485 * Called by the kernel when the daemon attempts an mmap() syscall
488 static int sep_request_daemon_mmap(struct file *filp,
489 struct vm_area_struct *vma)
491 struct sep_device *sep = filp->private_data;
492 dma_addr_t bus_address;
495 dev_dbg(&sep->pdev->dev, "daemon mmap start\n");
497 if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
502 /* Get physical address */
503 bus_address = sep->shared_bus;
505 dev_dbg(&sep->pdev->dev, "bus_address is %08lx\n",
506 (unsigned long)bus_address);
508 if (remap_pfn_range(vma, vma->vm_start, bus_address >> PAGE_SHIFT,
509 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
511 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
517 dev_dbg(&sep->pdev->dev, "daemon mmap end\n");
522 * sep_request_daemon_poll - poll implementation
523 * @sep: struct sep_device * for current SEP device
524 * @filp: struct file * for open file
525 * @wait: poll_table * for poll
527 * Called when our device is part of a poll() or select() syscall
529 static unsigned int sep_request_daemon_poll(struct file *filp,
535 unsigned long lck_flags;
536 struct sep_device *sep = filp->private_data;
538 dev_dbg(&sep->pdev->dev, "daemon poll: start\n");
540 poll_wait(filp, &sep->event_request_daemon, wait);
542 dev_dbg(&sep->pdev->dev, "daemon poll: send_ct is %lx reply ct is %lx\n",
543 sep->send_ct, sep->reply_ct);
545 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
546 /* Check if the data is ready */
547 if (sep->send_ct == sep->reply_ct) {
548 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
550 retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
551 dev_dbg(&sep->pdev->dev,
552 "daemon poll: data check (GPR2) is %x\n", retval2);
554 /* Check if PRINT request */
555 if ((retval2 >> 30) & 0x1) {
556 dev_dbg(&sep->pdev->dev, "daemon poll: PRINTF request in\n");
560 /* Check if NVS request */
562 dev_dbg(&sep->pdev->dev, "daemon poll: NVS request in\n");
563 mask |= POLLPRI | POLLWRNORM;
566 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
567 dev_dbg(&sep->pdev->dev,
568 "daemon poll: no reply received; returning 0\n");
572 dev_dbg(&sep->pdev->dev, "daemon poll: exit\n");
577 * sep_release - close a SEP device
578 * @inode: inode of SEP device
579 * @filp: file handle being closed
581 * Called on the final close of a SEP device.
583 static int sep_release(struct inode *inode, struct file *filp)
585 struct sep_device *sep = filp->private_data;
587 dev_dbg(&sep->pdev->dev, "Release for pid %d\n", current->pid);
589 mutex_lock(&sep->sep_mutex);
590 /* Is this the process that has a transaction open?
591 * If so, lets reset pid_doing_transaction to 0 and
592 * clear the in use flags, and then wake up sep_event
593 * so that other processes can do transactions
595 dev_dbg(&sep->pdev->dev, "waking up event and mmap_event\n");
596 if (sep->pid_doing_transaction == current->pid) {
597 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
598 clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
599 sep_free_dma_table_data_handler(sep);
600 wake_up(&sep->event);
601 sep->pid_doing_transaction = 0;
604 mutex_unlock(&sep->sep_mutex);
609 * sep_mmap - maps the shared area to user space
610 * @filp: pointer to struct file
611 * @vma: pointer to vm_area_struct
613 * Called on an mmap of our space via the normal SEP device
615 static int sep_mmap(struct file *filp, struct vm_area_struct *vma)
618 struct sep_device *sep = filp->private_data;
619 unsigned long error = 0;
621 dev_dbg(&sep->pdev->dev, "mmap start\n");
623 /* Set the transaction busy (own the device) */
624 wait_event_interruptible(sep->event,
625 test_and_set_bit(SEP_MMAP_LOCK_BIT,
626 &sep->in_use_flags) == 0);
628 if (signal_pending(current)) {
630 goto end_function_with_error;
633 * The pid_doing_transaction indicates that this process
634 * now owns the facilities to performa a transaction with
635 * the SEP. While this process is performing a transaction,
636 * no other process who has the SEP device open can perform
637 * any transactions. This method allows more than one process
638 * to have the device open at any given time, which provides
639 * finer granularity for device utilization by multiple
642 mutex_lock(&sep->sep_mutex);
643 sep->pid_doing_transaction = current->pid;
644 mutex_unlock(&sep->sep_mutex);
646 /* Zero the pools and the number of data pool alocation pointers */
647 sep->data_pool_bytes_allocated = 0;
648 sep->num_of_data_allocations = 0;
651 * Check that the size of the mapped range is as the size of the message
654 if ((vma->vm_end - vma->vm_start) > SEP_DRIVER_MMMAP_AREA_SIZE) {
656 goto end_function_with_error;
659 dev_dbg(&sep->pdev->dev, "shared_addr is %p\n", sep->shared_addr);
661 /* Get bus address */
662 bus_addr = sep->shared_bus;
664 dev_dbg(&sep->pdev->dev,
665 "bus_address is %lx\n", (unsigned long)bus_addr);
667 if (remap_pfn_range(vma, vma->vm_start, bus_addr >> PAGE_SHIFT,
668 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
669 dev_warn(&sep->pdev->dev, "remap_page_range failed\n");
671 goto end_function_with_error;
673 dev_dbg(&sep->pdev->dev, "mmap end\n");
676 end_function_with_error:
678 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
679 mutex_lock(&sep->sep_mutex);
680 sep->pid_doing_transaction = 0;
681 mutex_unlock(&sep->sep_mutex);
683 /* Raise event for stuck contextes */
685 dev_warn(&sep->pdev->dev, "mmap error - waking up event\n");
686 wake_up(&sep->event);
693 * sep_poll - poll handler
694 * @filp: pointer to struct file
695 * @wait: pointer to poll_table
697 * Called by the OS when the kernel is asked to do a poll on
700 static unsigned int sep_poll(struct file *filp, poll_table *wait)
705 unsigned long lck_flags;
707 struct sep_device *sep = filp->private_data;
709 dev_dbg(&sep->pdev->dev, "poll: start\n");
711 /* Am I the process that owns the transaction? */
712 mutex_lock(&sep->sep_mutex);
713 if (current->pid != sep->pid_doing_transaction) {
714 dev_warn(&sep->pdev->dev, "poll; wrong pid\n");
716 mutex_unlock(&sep->sep_mutex);
719 mutex_unlock(&sep->sep_mutex);
721 /* Check if send command or send_reply were activated previously */
722 if (!test_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
723 dev_warn(&sep->pdev->dev, "poll; lock bit set\n");
728 /* Add the event to the polling wait table */
729 dev_dbg(&sep->pdev->dev, "poll: calling wait sep_event\n");
731 poll_wait(filp, &sep->event, wait);
733 dev_dbg(&sep->pdev->dev, "poll: send_ct is %lx reply ct is %lx\n",
734 sep->send_ct, sep->reply_ct);
736 /* Check if error occured during poll */
737 retval2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
738 if (retval2 != 0x0) {
739 dev_warn(&sep->pdev->dev, "poll; poll error %x\n", retval2);
744 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
746 if (sep->send_ct == sep->reply_ct) {
747 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
748 retval = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
749 dev_dbg(&sep->pdev->dev, "poll: data ready check (GPR2) %x\n",
752 /* Check if printf request */
753 if ((retval >> 30) & 0x1) {
754 dev_dbg(&sep->pdev->dev, "poll: SEP printf request\n");
755 wake_up(&sep->event_request_daemon);
759 /* Check if the this is SEP reply or request */
761 dev_dbg(&sep->pdev->dev, "poll: SEP request\n");
762 wake_up(&sep->event_request_daemon);
764 dev_dbg(&sep->pdev->dev, "poll: normal return\n");
765 /* In case it is again by send_reply_comand */
766 clear_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags);
767 sep_dump_message(sep);
768 dev_dbg(&sep->pdev->dev,
769 "poll; SEP reply POLLIN | POLLRDNORM\n");
770 mask |= POLLIN | POLLRDNORM;
773 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
774 dev_dbg(&sep->pdev->dev,
775 "poll; no reply received; returning mask of 0\n");
780 dev_dbg(&sep->pdev->dev, "poll: end\n");
785 * sep_time_address - address in SEP memory of time
786 * @sep: SEP device we want the address from
788 * Return the address of the two dwords in memory used for time
791 static u32 *sep_time_address(struct sep_device *sep)
793 return sep->shared_addr + SEP_DRIVER_SYSTEM_TIME_MEMORY_OFFSET_IN_BYTES;
797 * sep_set_time - set the SEP time
798 * @sep: the SEP we are setting the time for
800 * Calculates time and sets it at the predefined address.
801 * Called with the SEP mutex held.
803 static unsigned long sep_set_time(struct sep_device *sep)
806 u32 *time_addr; /* Address of time as seen by the kernel */
809 dev_dbg(&sep->pdev->dev, "sep_set_time start\n");
811 do_gettimeofday(&time);
813 /* Set value in the SYSTEM MEMORY offset */
814 time_addr = sep_time_address(sep);
816 time_addr[0] = SEP_TIME_VAL_TOKEN;
817 time_addr[1] = time.tv_sec;
819 dev_dbg(&sep->pdev->dev, "time.tv_sec is %lu\n", time.tv_sec);
820 dev_dbg(&sep->pdev->dev, "time_addr is %p\n", time_addr);
821 dev_dbg(&sep->pdev->dev, "sep->shared_addr is %p\n", sep->shared_addr);
827 * sep_set_caller_id_handler - insert caller id entry
829 * @arg: pointer to struct caller_id_struct
831 * Inserts the data into the caller id table. Note that this function
832 * falls under the ioctl lock
834 static int sep_set_caller_id_handler(struct sep_device *sep, unsigned long arg)
839 struct caller_id_struct command_args;
841 dev_dbg(&sep->pdev->dev, "sep_set_caller_id_handler start\n");
843 for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
844 if (sep->caller_id_table[i].pid == 0)
848 if (i == SEP_CALLER_ID_TABLE_NUM_ENTRIES) {
849 dev_warn(&sep->pdev->dev, "no more caller id entries left\n");
850 dev_warn(&sep->pdev->dev, "maximum number is %d\n",
851 SEP_CALLER_ID_TABLE_NUM_ENTRIES);
857 if (copy_from_user(&command_args, (void __user *)arg,
858 sizeof(command_args))) {
863 hash = (void __user *)(unsigned long)command_args.callerIdAddress;
865 if (!command_args.pid || !command_args.callerIdSizeInBytes) {
870 dev_dbg(&sep->pdev->dev, "pid is %x\n", command_args.pid);
871 dev_dbg(&sep->pdev->dev, "callerIdSizeInBytes is %x\n",
872 command_args.callerIdSizeInBytes);
874 if (command_args.callerIdSizeInBytes >
875 SEP_CALLER_ID_HASH_SIZE_IN_BYTES) {
880 sep->caller_id_table[i].pid = command_args.pid;
882 if (copy_from_user(sep->caller_id_table[i].callerIdHash,
883 hash, command_args.callerIdSizeInBytes))
886 dev_dbg(&sep->pdev->dev, "sep_set_caller_id_handler end\n");
891 * sep_set_current_caller_id - set the caller id
892 * @sep: pointer to struct_sep_device
894 * Set the caller ID (if it exists) to the SEP. Note that this
895 * function falls under the ioctl lock
897 static int sep_set_current_caller_id(struct sep_device *sep)
901 dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id start\n");
902 dev_dbg(&sep->pdev->dev, "current process is %d\n", current->pid);
904 /* Zero the previous value */
905 memset(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
906 0, SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
908 for (i = 0; i < SEP_CALLER_ID_TABLE_NUM_ENTRIES; i++) {
909 if (sep->caller_id_table[i].pid == current->pid) {
910 dev_dbg(&sep->pdev->dev, "Caller Id found\n");
912 memcpy(sep->shared_addr + SEP_CALLER_ID_OFFSET_BYTES,
913 (void *)(sep->caller_id_table[i].callerIdHash),
914 SEP_CALLER_ID_HASH_SIZE_IN_BYTES);
918 dev_dbg(&sep->pdev->dev, "sep_set_current_caller_id end\n");
923 * sep_send_command_handler - kick off a command
924 * @sep: SEP being signalled
926 * This function raises interrupt to SEP that signals that is has a new
927 * command from the host
929 * Note that this function does fall under the ioctl lock
931 static int sep_send_command_handler(struct sep_device *sep)
933 unsigned long lck_flags;
936 dev_dbg(&sep->pdev->dev, "sep_send_command_handler start\n");
938 if (test_and_set_bit(SEP_SEND_MSG_LOCK_BIT, &sep->in_use_flags)) {
944 sep_set_current_caller_id(sep);
946 sep_dump_message(sep);
949 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
951 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
953 dev_dbg(&sep->pdev->dev,
954 "sep_send_command_handler send_ct %lx reply_ct %lx\n",
955 sep->send_ct, sep->reply_ct);
957 /* Send interrupt to SEP */
958 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x2);
961 dev_dbg(&sep->pdev->dev, "sep_send_command_handler end\n");
966 * sep_allocate_data_pool_memory_handler -allocate pool memory
967 * @sep: pointer to struct sep_device
968 * @arg: pointer to struct alloc_struct
970 * This function handles the allocate data pool memory request
971 * This function returns calculates the bus address of the
972 * allocated memory, and the offset of this area from the mapped address.
973 * Therefore, the FVOs in user space can calculate the exact virtual
974 * address of this allocated memory
976 static int sep_allocate_data_pool_memory_handler(struct sep_device *sep,
980 struct alloc_struct command_args;
982 /* Holds the allocated buffer address in the system memory pool */
985 dev_dbg(&sep->pdev->dev,
986 "sep_allocate_data_pool_memory_handler start\n");
988 if (copy_from_user(&command_args, (void __user *)arg,
989 sizeof(struct alloc_struct))) {
994 /* Allocate memory */
995 if ((sep->data_pool_bytes_allocated + command_args.num_bytes) >
996 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES) {
1001 dev_dbg(&sep->pdev->dev,
1002 "bytes_allocated: %x\n", (int)sep->data_pool_bytes_allocated);
1003 dev_dbg(&sep->pdev->dev,
1004 "offset: %x\n", SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES);
1005 /* Set the virtual and bus address */
1006 command_args.offset = SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
1007 sep->data_pool_bytes_allocated;
1009 dev_dbg(&sep->pdev->dev,
1010 "command_args.offset: %x\n", command_args.offset);
1012 /* Place in the shared area that is known by the SEP */
1013 token_addr = (u32 *)(sep->shared_addr +
1014 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES +
1015 (sep->num_of_data_allocations)*2*sizeof(u32));
1017 dev_dbg(&sep->pdev->dev, "allocation offset: %x\n",
1018 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES);
1019 dev_dbg(&sep->pdev->dev, "data pool token addr is %p\n", token_addr);
1021 token_addr[0] = SEP_DATA_POOL_POINTERS_VAL_TOKEN;
1022 token_addr[1] = (u32)sep->shared_bus +
1023 SEP_DRIVER_DATA_POOL_AREA_OFFSET_IN_BYTES +
1024 sep->data_pool_bytes_allocated;
1026 dev_dbg(&sep->pdev->dev, "data pool token [0] %x\n", token_addr[0]);
1027 dev_dbg(&sep->pdev->dev, "data pool token [1] %x\n", token_addr[1]);
1029 /* Write the memory back to the user space */
1030 error = copy_to_user((void *)arg, (void *)&command_args,
1031 sizeof(struct alloc_struct));
1037 /* Update the allocation */
1038 sep->data_pool_bytes_allocated += command_args.num_bytes;
1039 sep->num_of_data_allocations += 1;
1041 dev_dbg(&sep->pdev->dev, "data_allocations %d\n",
1042 sep->num_of_data_allocations);
1043 dev_dbg(&sep->pdev->dev, "bytes allocated %d\n",
1044 (int)sep->data_pool_bytes_allocated);
1047 dev_dbg(&sep->pdev->dev, "sep_allocate_data_pool_memory_handler end\n");
1052 * sep_lock_kernel_pages - map kernel pages for DMA
1053 * @sep: pointer to struct sep_device
1054 * @kernel_virt_addr: address of data buffer in kernel
1055 * @data_size: size of data
1056 * @lli_array_ptr: lli array
1057 * @in_out_flag: input into device or output from device
1059 * This function locks all the physical pages of the kernel virtual buffer
1060 * and construct a basic lli array, where each entry holds the physical
1061 * page address and the size that application data holds in this page
1062 * This function is used only during kernel crypto mod calls from within
1063 * the kernel (when ioctl is not used)
1065 static int sep_lock_kernel_pages(struct sep_device *sep,
1066 unsigned long kernel_virt_addr,
1068 struct sep_lli_entry **lli_array_ptr,
1074 struct sep_lli_entry *lli_array;
1076 struct sep_dma_map *map_array;
1078 dev_dbg(&sep->pdev->dev, "sep_lock_kernel_pages start\n");
1079 dev_dbg(&sep->pdev->dev, "kernel_virt_addr is %08lx\n",
1080 (unsigned long)kernel_virt_addr);
1081 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1083 lli_array = kmalloc(sizeof(struct sep_lli_entry), GFP_ATOMIC);
1088 map_array = kmalloc(sizeof(struct sep_dma_map), GFP_ATOMIC);
1091 goto end_function_with_error;
1094 map_array[0].dma_addr =
1095 dma_map_single(&sep->pdev->dev, (void *)kernel_virt_addr,
1096 data_size, DMA_BIDIRECTIONAL);
1097 map_array[0].size = data_size;
1101 * Set the start address of the first page - app data may start not at
1102 * the beginning of the page
1104 lli_array[0].bus_address = (u32)map_array[0].dma_addr;
1105 lli_array[0].block_size = map_array[0].size;
1107 dev_dbg(&sep->pdev->dev,
1108 "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1109 (unsigned long)lli_array[0].bus_address,
1110 lli_array[0].block_size);
1112 /* Set the output parameters */
1113 if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1114 *lli_array_ptr = lli_array;
1115 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 1;
1116 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1117 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1118 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries = 1;
1120 *lli_array_ptr = lli_array;
1121 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = 1;
1122 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
1123 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1124 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries = 1;
1128 end_function_with_error:
1132 dev_dbg(&sep->pdev->dev, "sep_lock_kernel_pages end\n");
1137 * sep_lock_user_pages - lock and map user pages for DMA
1138 * @sep: pointer to struct sep_device
1139 * @app_virt_addr: user memory data buffer
1140 * @data_size: size of data buffer
1141 * @lli_array_ptr: lli array
1142 * @in_out_flag: input or output to device
1144 * This function locks all the physical pages of the application
1145 * virtual buffer and construct a basic lli array, where each entry
1146 * holds the physical page address and the size that application
1147 * data holds in this physical pages
1149 static int sep_lock_user_pages(struct sep_device *sep,
1152 struct sep_lli_entry **lli_array_ptr,
1159 /* The the page of the end address of the user space buffer */
1161 /* The page of the start address of the user space buffer */
1163 /* The range in pages */
1165 /* Array of pointers to page */
1166 struct page **page_array;
1168 struct sep_lli_entry *lli_array;
1170 struct sep_dma_map *map_array;
1171 /* Direction of the DMA mapping for locked pages */
1172 enum dma_data_direction dir;
1174 dev_dbg(&sep->pdev->dev, "sep_lock_user_pages start\n");
1176 /* Set start and end pages and num pages */
1177 end_page = (app_virt_addr + data_size - 1) >> PAGE_SHIFT;
1178 start_page = app_virt_addr >> PAGE_SHIFT;
1179 num_pages = end_page - start_page + 1;
1181 dev_dbg(&sep->pdev->dev, "app_virt_addr is %x\n", app_virt_addr);
1182 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1183 dev_dbg(&sep->pdev->dev, "start_page is %x\n", start_page);
1184 dev_dbg(&sep->pdev->dev, "end_page is %x\n", end_page);
1185 dev_dbg(&sep->pdev->dev, "num_pages is %x\n", num_pages);
1187 dev_dbg(&sep->pdev->dev, "starting page_array malloc\n");
1189 /* Allocate array of pages structure pointers */
1190 page_array = kmalloc(sizeof(struct page *) * num_pages, GFP_ATOMIC);
1195 map_array = kmalloc(sizeof(struct sep_dma_map) * num_pages, GFP_ATOMIC);
1197 dev_warn(&sep->pdev->dev, "kmalloc for map_array failed\n");
1199 goto end_function_with_error1;
1202 lli_array = kmalloc(sizeof(struct sep_lli_entry) * num_pages,
1206 dev_warn(&sep->pdev->dev, "kmalloc for lli_array failed\n");
1208 goto end_function_with_error2;
1211 dev_dbg(&sep->pdev->dev, "starting get_user_pages\n");
1213 /* Convert the application virtual address into a set of physical */
1214 down_read(¤t->mm->mmap_sem);
1215 result = get_user_pages(current, current->mm, app_virt_addr,
1217 ((in_out_flag == SEP_DRIVER_IN_FLAG) ? 0 : 1),
1218 0, page_array, NULL);
1220 up_read(¤t->mm->mmap_sem);
1222 /* Check the number of pages locked - if not all then exit with error */
1223 if (result != num_pages) {
1224 dev_warn(&sep->pdev->dev,
1225 "not all pages locked by get_user_pages\n");
1227 goto end_function_with_error3;
1230 dev_dbg(&sep->pdev->dev, "get_user_pages succeeded\n");
1233 if (in_out_flag == SEP_DRIVER_IN_FLAG)
1234 dir = DMA_TO_DEVICE;
1236 dir = DMA_FROM_DEVICE;
1239 * Fill the array using page array data and
1240 * map the pages - this action will also flush the cache as needed
1242 for (count = 0; count < num_pages; count++) {
1243 /* Fill the map array */
1244 map_array[count].dma_addr =
1245 dma_map_page(&sep->pdev->dev, page_array[count],
1246 0, PAGE_SIZE, /*dir*/DMA_BIDIRECTIONAL);
1248 map_array[count].size = PAGE_SIZE;
1250 /* Fill the lli array entry */
1251 lli_array[count].bus_address = (u32)map_array[count].dma_addr;
1252 lli_array[count].block_size = PAGE_SIZE;
1254 dev_warn(&sep->pdev->dev, "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1255 count, (unsigned long)lli_array[count].bus_address,
1256 count, lli_array[count].block_size);
1259 /* Check the offset for the first page */
1260 lli_array[0].bus_address =
1261 lli_array[0].bus_address + (app_virt_addr & (~PAGE_MASK));
1263 /* Check that not all the data is in the first page only */
1264 if ((PAGE_SIZE - (app_virt_addr & (~PAGE_MASK))) >= data_size)
1265 lli_array[0].block_size = data_size;
1267 lli_array[0].block_size =
1268 PAGE_SIZE - (app_virt_addr & (~PAGE_MASK));
1270 dev_dbg(&sep->pdev->dev,
1271 "lli_array[0].bus_address is %08lx, lli_array[0].block_size is %x\n",
1272 (unsigned long)lli_array[count].bus_address,
1273 lli_array[count].block_size);
1275 /* Check the size of the last page */
1276 if (num_pages > 1) {
1277 lli_array[num_pages - 1].block_size =
1278 (app_virt_addr + data_size) & (~PAGE_MASK);
1280 dev_warn(&sep->pdev->dev,
1281 "lli_array[%x].bus_address is %08lx, lli_array[%x].block_size is %x\n",
1283 (unsigned long)lli_array[count].bus_address,
1285 lli_array[count].block_size);
1288 /* Set output params acording to the in_out flag */
1289 if (in_out_flag == SEP_DRIVER_IN_FLAG) {
1290 *lli_array_ptr = lli_array;
1291 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = num_pages;
1292 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = page_array;
1293 sep->dma_res_arr[sep->nr_dcb_creat].in_map_array = map_array;
1294 sep->dma_res_arr[sep->nr_dcb_creat].in_map_num_entries =
1297 *lli_array_ptr = lli_array;
1298 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages = num_pages;
1299 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array =
1301 sep->dma_res_arr[sep->nr_dcb_creat].out_map_array = map_array;
1302 sep->dma_res_arr[sep->nr_dcb_creat].out_map_num_entries =
1307 end_function_with_error3:
1308 /* Free lli array */
1311 end_function_with_error2:
1314 end_function_with_error1:
1315 /* Free page array */
1319 dev_dbg(&sep->pdev->dev, "sep_lock_user_pages end\n");
1324 * u32 sep_calculate_lli_table_max_size - size the LLI table
1325 * @sep: pointer to struct sep_device
1327 * @num_array_entries
1330 * This function calculates the size of data that can be inserted into
1331 * the lli table from this array, such that either the table is full
1332 * (all entries are entered), or there are no more entries in the
1335 static u32 sep_calculate_lli_table_max_size(struct sep_device *sep,
1336 struct sep_lli_entry *lli_in_array_ptr,
1337 u32 num_array_entries,
1338 u32 *last_table_flag)
1341 /* Table data size */
1342 u32 table_data_size = 0;
1343 /* Data size for the next table */
1344 u32 next_table_data_size;
1346 *last_table_flag = 0;
1349 * Calculate the data in the out lli table till we fill the whole
1350 * table or till the data has ended
1353 (counter < (SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP - 1)) &&
1354 (counter < num_array_entries); counter++)
1355 table_data_size += lli_in_array_ptr[counter].block_size;
1358 * Check if we reached the last entry,
1359 * meaning this ia the last table to build,
1360 * and no need to check the block alignment
1362 if (counter == num_array_entries) {
1363 /* Set the last table flag */
1364 *last_table_flag = 1;
1369 * Calculate the data size of the next table.
1370 * Stop if no entries left or if data size is more the DMA restriction
1372 next_table_data_size = 0;
1373 for (; counter < num_array_entries; counter++) {
1374 next_table_data_size += lli_in_array_ptr[counter].block_size;
1375 if (next_table_data_size >= SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1380 * Check if the next table data size is less then DMA rstriction.
1381 * if it is - recalculate the current table size, so that the next
1382 * table data size will be adaquete for DMA
1384 if (next_table_data_size &&
1385 next_table_data_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE)
1387 table_data_size -= (SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE -
1388 next_table_data_size);
1390 dev_dbg(&sep->pdev->dev, "table data size is %x\n",
1393 return table_data_size;
1397 * sep_build_lli_table - build an lli array for the given table
1398 * @sep: pointer to struct sep_device
1399 * @lli_array_ptr: pointer to lli array
1400 * @lli_table_ptr: pointer to lli table
1401 * @num_processed_entries_ptr: pointer to number of entries
1402 * @num_table_entries_ptr: pointer to number of tables
1403 * @table_data_size: total data size
1405 * Builds ant lli table from the lli_array according to
1406 * the given size of data
1408 static void sep_build_lli_table(struct sep_device *sep,
1409 struct sep_lli_entry *lli_array_ptr,
1410 struct sep_lli_entry *lli_table_ptr,
1411 u32 *num_processed_entries_ptr,
1412 u32 *num_table_entries_ptr,
1413 u32 table_data_size)
1415 /* Current table data size */
1416 u32 curr_table_data_size;
1417 /* Counter of lli array entry */
1420 dev_dbg(&sep->pdev->dev, "sep_build_lli_table start\n");
1422 /* Init currrent table data size and lli array entry counter */
1423 curr_table_data_size = 0;
1425 *num_table_entries_ptr = 1;
1427 dev_dbg(&sep->pdev->dev, "table_data_size is %x\n", table_data_size);
1429 /* Fill the table till table size reaches the needed amount */
1430 while (curr_table_data_size < table_data_size) {
1431 /* Update the number of entries in table */
1432 (*num_table_entries_ptr)++;
1434 lli_table_ptr->bus_address =
1435 cpu_to_le32(lli_array_ptr[array_counter].bus_address);
1437 lli_table_ptr->block_size =
1438 cpu_to_le32(lli_array_ptr[array_counter].block_size);
1440 curr_table_data_size += lli_array_ptr[array_counter].block_size;
1442 dev_dbg(&sep->pdev->dev, "lli_table_ptr is %p\n",
1444 dev_dbg(&sep->pdev->dev, "lli_table_ptr->bus_address is %08lx\n",
1445 (unsigned long)lli_table_ptr->bus_address);
1446 dev_dbg(&sep->pdev->dev, "lli_table_ptr->block_size is %x\n",
1447 lli_table_ptr->block_size);
1449 /* Check for overflow of the table data */
1450 if (curr_table_data_size > table_data_size) {
1451 dev_dbg(&sep->pdev->dev,
1452 "curr_table_data_size too large\n");
1454 /* Update the size of block in the table */
1455 lli_table_ptr->block_size -=
1456 cpu_to_le32((curr_table_data_size - table_data_size));
1458 /* Update the physical address in the lli array */
1459 lli_array_ptr[array_counter].bus_address +=
1460 cpu_to_le32(lli_table_ptr->block_size);
1462 /* Update the block size left in the lli array */
1463 lli_array_ptr[array_counter].block_size =
1464 (curr_table_data_size - table_data_size);
1466 /* Advance to the next entry in the lli_array */
1469 dev_dbg(&sep->pdev->dev,
1470 "lli_table_ptr->bus_address is %08lx\n",
1471 (unsigned long)lli_table_ptr->bus_address);
1472 dev_dbg(&sep->pdev->dev,
1473 "lli_table_ptr->block_size is %x\n",
1474 lli_table_ptr->block_size);
1476 /* Move to the next entry in table */
1480 /* Set the info entry to default */
1481 lli_table_ptr->bus_address = 0xffffffff;
1482 lli_table_ptr->block_size = 0;
1484 dev_dbg(&sep->pdev->dev, "lli_table_ptr is %p\n", lli_table_ptr);
1485 dev_dbg(&sep->pdev->dev, "lli_table_ptr->bus_address is %08lx\n",
1486 (unsigned long)lli_table_ptr->bus_address);
1487 dev_dbg(&sep->pdev->dev, "lli_table_ptr->block_size is %x\n",
1488 lli_table_ptr->block_size);
1490 /* Set the output parameter */
1491 *num_processed_entries_ptr += array_counter;
1493 dev_dbg(&sep->pdev->dev, "num_processed_entries_ptr is %x\n",
1494 *num_processed_entries_ptr);
1496 dev_dbg(&sep->pdev->dev, "sep_build_lli_table end\n");
1500 * sep_shared_area_virt_to_bus - map shared area to bus address
1501 * @sep: pointer to struct sep_device
1502 * @virt_address: virtual address to convert
1504 * This functions returns the physical address inside shared area according
1505 * to the virtual address. It can be either on the externa RAM device
1506 * (ioremapped), or on the system RAM
1507 * This implementation is for the external RAM
1509 static dma_addr_t sep_shared_area_virt_to_bus(struct sep_device *sep,
1512 dev_dbg(&sep->pdev->dev, "sh virt to phys v %p\n", virt_address);
1513 dev_dbg(&sep->pdev->dev, "sh virt to phys p %08lx\n",
1515 sep->shared_bus + (virt_address - sep->shared_addr));
1517 return sep->shared_bus + (size_t)(virt_address - sep->shared_addr);
1521 * sep_shared_area_bus_to_virt - map shared area bus address to kernel
1522 * @sep: pointer to struct sep_device
1523 * @bus_address: bus address to convert
1525 * This functions returns the virtual address inside shared area
1526 * according to the physical address. It can be either on the
1527 * externa RAM device (ioremapped), or on the system RAM
1528 * This implementation is for the external RAM
1530 static void *sep_shared_area_bus_to_virt(struct sep_device *sep,
1531 dma_addr_t bus_address)
1533 dev_dbg(&sep->pdev->dev, "shared bus to virt b=%lx v=%lx\n",
1534 (unsigned long)bus_address, (unsigned long)(sep->shared_addr +
1535 (size_t)(bus_address - sep->shared_bus)));
1537 return sep->shared_addr + (size_t)(bus_address - sep->shared_bus);
1541 * sep_debug_print_lli_tables - dump LLI table
1542 * @sep: pointer to struct sep_device
1543 * @lli_table_ptr: pointer to sep_lli_entry
1544 * @num_table_entries: number of entries
1545 * @table_data_size: total data size
1547 * Walk the the list of the print created tables and print all the data
1549 static void sep_debug_print_lli_tables(struct sep_device *sep,
1550 struct sep_lli_entry *lli_table_ptr,
1551 unsigned long num_table_entries,
1552 unsigned long table_data_size)
1554 unsigned long table_count = 1;
1555 unsigned long entries_count = 0;
1557 dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables start\n");
1559 while ((unsigned long) lli_table_ptr->bus_address != 0xffffffff) {
1560 dev_dbg(&sep->pdev->dev,
1561 "lli table %08lx, table_data_size is %lu\n",
1562 table_count, table_data_size);
1563 dev_dbg(&sep->pdev->dev, "num_table_entries is %lu\n",
1566 /* Print entries of the table (without info entry) */
1567 for (entries_count = 0; entries_count < num_table_entries;
1568 entries_count++, lli_table_ptr++) {
1570 dev_dbg(&sep->pdev->dev,
1571 "lli_table_ptr address is %08lx\n",
1572 (unsigned long) lli_table_ptr);
1574 dev_dbg(&sep->pdev->dev,
1575 "phys address is %08lx block size is %x\n",
1576 (unsigned long)lli_table_ptr->bus_address,
1577 lli_table_ptr->block_size);
1579 /* Point to the info entry */
1582 dev_dbg(&sep->pdev->dev,
1583 "phys lli_table_ptr->block_size is %x\n",
1584 lli_table_ptr->block_size);
1586 dev_dbg(&sep->pdev->dev,
1587 "phys lli_table_ptr->physical_address is %08lu\n",
1588 (unsigned long)lli_table_ptr->bus_address);
1591 table_data_size = lli_table_ptr->block_size & 0xffffff;
1592 num_table_entries = (lli_table_ptr->block_size >> 24) & 0xff;
1594 dev_dbg(&sep->pdev->dev,
1595 "phys table_data_size is %lu num_table_entries is"
1596 " %lu bus_address is%lu\n", table_data_size,
1597 num_table_entries, (unsigned long)lli_table_ptr->bus_address);
1599 if ((unsigned long)lli_table_ptr->bus_address != 0xffffffff)
1600 lli_table_ptr = (struct sep_lli_entry *)
1601 sep_shared_bus_to_virt(sep,
1602 (unsigned long)lli_table_ptr->bus_address);
1606 dev_dbg(&sep->pdev->dev, "sep_debug_print_lli_tables end\n");
1611 * sep_prepare_empty_lli_table - create a blank LLI table
1612 * @sep: pointer to struct sep_device
1613 * @lli_table_addr_ptr: pointer to lli table
1614 * @num_entries_ptr: pointer to number of entries
1615 * @table_data_size_ptr: point to table data size
1617 * This function creates empty lli tables when there is no data
1619 static void sep_prepare_empty_lli_table(struct sep_device *sep,
1620 dma_addr_t *lli_table_addr_ptr,
1621 u32 *num_entries_ptr,
1622 u32 *table_data_size_ptr)
1624 struct sep_lli_entry *lli_table_ptr;
1626 dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");
1628 /* Find the area for new table */
1630 (struct sep_lli_entry *)(sep->shared_addr +
1631 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1632 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1633 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1635 lli_table_ptr->bus_address = 0;
1636 lli_table_ptr->block_size = 0;
1639 lli_table_ptr->bus_address = 0xFFFFFFFF;
1640 lli_table_ptr->block_size = 0;
1642 /* Set the output parameter value */
1643 *lli_table_addr_ptr = sep->shared_bus +
1644 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1645 sep->num_lli_tables_created *
1646 sizeof(struct sep_lli_entry) *
1647 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1649 /* Set the num of entries and table data size for empty table */
1650 *num_entries_ptr = 2;
1651 *table_data_size_ptr = 0;
1653 /* Update the number of created tables */
1654 sep->num_lli_tables_created++;
1656 dev_dbg(&sep->pdev->dev, "sep_prepare_empty_lli_table start\n");
1661 * sep_prepare_input_dma_table - prepare input DMA mappings
1662 * @sep: pointer to struct sep_device
1667 * @table_data_size_ptr:
1668 * @is_kva: set for kernel data (kernel cryptio call)
1670 * This function prepares only input DMA table for synhronic symmetric
1672 * Note that all bus addresses that are passed to the SEP
1673 * are in 32 bit format; the SEP is a 32 bit device
1675 static int sep_prepare_input_dma_table(struct sep_device *sep,
1676 unsigned long app_virt_addr,
1679 dma_addr_t *lli_table_ptr,
1680 u32 *num_entries_ptr,
1681 u32 *table_data_size_ptr,
1685 /* Pointer to the info entry of the table - the last entry */
1686 struct sep_lli_entry *info_entry_ptr;
1687 /* Array of pointers to page */
1688 struct sep_lli_entry *lli_array_ptr;
1689 /* Points to the first entry to be processed in the lli_in_array */
1690 u32 current_entry = 0;
1691 /* Num entries in the virtual buffer */
1692 u32 sep_lli_entries = 0;
1693 /* Lli table pointer */
1694 struct sep_lli_entry *in_lli_table_ptr;
1695 /* The total data in one table */
1696 u32 table_data_size = 0;
1697 /* Flag for last table */
1698 u32 last_table_flag = 0;
1699 /* Number of entries in lli table */
1700 u32 num_entries_in_table = 0;
1701 /* Next table address */
1702 void *lli_table_alloc_addr = 0;
1704 dev_dbg(&sep->pdev->dev, "sep_prepare_input_dma_table start\n");
1705 dev_dbg(&sep->pdev->dev, "data_size is %x\n", data_size);
1706 dev_dbg(&sep->pdev->dev, "block_size is %x\n", block_size);
1708 /* Initialize the pages pointers */
1709 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
1710 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages = 0;
1712 /* Set the kernel address for first table to be allocated */
1713 lli_table_alloc_addr = (void *)(sep->shared_addr +
1714 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1715 sep->num_lli_tables_created * sizeof(struct sep_lli_entry) *
1716 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
1718 if (data_size == 0) {
1719 /* Special case - create meptu table - 2 entries, zero data */
1720 sep_prepare_empty_lli_table(sep, lli_table_ptr,
1721 num_entries_ptr, table_data_size_ptr);
1722 goto update_dcb_counter;
1725 /* Check if the pages are in Kernel Virtual Address layout */
1727 /* Lock the pages in the kernel */
1728 error = sep_lock_kernel_pages(sep, app_virt_addr,
1729 data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1732 * Lock the pages of the user buffer
1733 * and translate them to pages
1735 error = sep_lock_user_pages(sep, app_virt_addr,
1736 data_size, &lli_array_ptr, SEP_DRIVER_IN_FLAG);
1741 dev_dbg(&sep->pdev->dev, "output sep_in_num_pages is %x\n",
1742 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
1745 info_entry_ptr = NULL;
1747 sep_lli_entries = sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages;
1749 /* Loop till all the entries in in array are not processed */
1750 while (current_entry < sep_lli_entries) {
1752 /* Set the new input and output tables */
1754 (struct sep_lli_entry *)lli_table_alloc_addr;
1756 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1757 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1759 if (lli_table_alloc_addr >
1760 ((void *)sep->shared_addr +
1761 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1762 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1765 goto end_function_error;
1769 /* Update the number of created tables */
1770 sep->num_lli_tables_created++;
1772 /* Calculate the maximum size of data for input table */
1773 table_data_size = sep_calculate_lli_table_max_size(sep,
1774 &lli_array_ptr[current_entry],
1775 (sep_lli_entries - current_entry),
1779 * If this is not the last table -
1780 * then allign it to the block size
1782 if (!last_table_flag)
1784 (table_data_size / block_size) * block_size;
1786 dev_dbg(&sep->pdev->dev, "output table_data_size is %x\n",
1789 /* Construct input lli table */
1790 sep_build_lli_table(sep, &lli_array_ptr[current_entry],
1792 ¤t_entry, &num_entries_in_table, table_data_size);
1794 if (info_entry_ptr == NULL) {
1796 /* Set the output parameters to physical addresses */
1797 *lli_table_ptr = sep_shared_area_virt_to_bus(sep,
1799 *num_entries_ptr = num_entries_in_table;
1800 *table_data_size_ptr = table_data_size;
1802 dev_dbg(&sep->pdev->dev,
1803 "output lli_table_in_ptr is %08lx\n",
1804 (unsigned long)*lli_table_ptr);
1807 /* Update the info entry of the previous in table */
1808 info_entry_ptr->bus_address =
1809 sep_shared_area_virt_to_bus(sep,
1811 info_entry_ptr->block_size =
1812 ((num_entries_in_table) << 24) |
1815 /* Save the pointer to the info entry of the current tables */
1816 info_entry_ptr = in_lli_table_ptr + num_entries_in_table - 1;
1818 /* Print input tables */
1819 sep_debug_print_lli_tables(sep, (struct sep_lli_entry *)
1820 sep_shared_area_bus_to_virt(sep, *lli_table_ptr),
1821 *num_entries_ptr, *table_data_size_ptr);
1822 /* The array of the pages */
1823 kfree(lli_array_ptr);
1826 /* Update DCB counter */
1827 sep->nr_dcb_creat++;
1831 /* Free all the allocated resources */
1832 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
1833 kfree(lli_array_ptr);
1834 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
1837 dev_dbg(&sep->pdev->dev, "sep_prepare_input_dma_table end\n");
1842 * sep_construct_dma_tables_from_lli - prepare AES/DES mappings
1843 * @sep: pointer to struct sep_device
1845 * @sep_in_lli_entries:
1847 * @sep_out_lli_entries
1850 * @lli_table_out_ptr
1851 * @in_num_entries_ptr
1852 * @out_num_entries_ptr
1853 * @table_data_size_ptr
1855 * This function creates the input and output DMA tables for
1856 * symmetric operations (AES/DES) according to the block
1857 * size from LLI arays
1858 * Note that all bus addresses that are passed to the SEP
1859 * are in 32 bit format; the SEP is a 32 bit device
1861 static int sep_construct_dma_tables_from_lli(
1862 struct sep_device *sep,
1863 struct sep_lli_entry *lli_in_array,
1864 u32 sep_in_lli_entries,
1865 struct sep_lli_entry *lli_out_array,
1866 u32 sep_out_lli_entries,
1868 dma_addr_t *lli_table_in_ptr,
1869 dma_addr_t *lli_table_out_ptr,
1870 u32 *in_num_entries_ptr,
1871 u32 *out_num_entries_ptr,
1872 u32 *table_data_size_ptr)
1874 /* Points to the area where next lli table can be allocated */
1875 void *lli_table_alloc_addr = 0;
1876 /* Input lli table */
1877 struct sep_lli_entry *in_lli_table_ptr = NULL;
1878 /* Output lli table */
1879 struct sep_lli_entry *out_lli_table_ptr = NULL;
1880 /* Pointer to the info entry of the table - the last entry */
1881 struct sep_lli_entry *info_in_entry_ptr = NULL;
1882 /* Pointer to the info entry of the table - the last entry */
1883 struct sep_lli_entry *info_out_entry_ptr = NULL;
1884 /* Points to the first entry to be processed in the lli_in_array */
1885 u32 current_in_entry = 0;
1886 /* Points to the first entry to be processed in the lli_out_array */
1887 u32 current_out_entry = 0;
1888 /* Max size of the input table */
1889 u32 in_table_data_size = 0;
1890 /* Max size of the output table */
1891 u32 out_table_data_size = 0;
1892 /* Flag te signifies if this is the last tables build */
1893 u32 last_table_flag = 0;
1894 /* The data size that should be in table */
1895 u32 table_data_size = 0;
1896 /* Number of etnries in the input table */
1897 u32 num_entries_in_table = 0;
1898 /* Number of etnries in the output table */
1899 u32 num_entries_out_table = 0;
1901 dev_dbg(&sep->pdev->dev, "sep_construct_dma_tables_from_lli start\n");
1903 /* Initiate to point after the message area */
1904 lli_table_alloc_addr = (void *)(sep->shared_addr +
1905 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1906 (sep->num_lli_tables_created *
1907 (sizeof(struct sep_lli_entry) *
1908 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP)));
1910 /* Loop till all the entries in in array are not processed */
1911 while (current_in_entry < sep_in_lli_entries) {
1912 /* Set the new input and output tables */
1914 (struct sep_lli_entry *)lli_table_alloc_addr;
1916 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1917 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1919 /* Set the first output tables */
1921 (struct sep_lli_entry *)lli_table_alloc_addr;
1923 /* Check if the DMA table area limit was overrun */
1924 if ((lli_table_alloc_addr + sizeof(struct sep_lli_entry) *
1925 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP) >
1926 ((void *)sep->shared_addr +
1927 SYNCHRONIC_DMA_TABLES_AREA_OFFSET_BYTES +
1928 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES)) {
1930 dev_warn(&sep->pdev->dev, "dma table limit overrun\n");
1934 /* Update the number of the lli tables created */
1935 sep->num_lli_tables_created += 2;
1937 lli_table_alloc_addr += sizeof(struct sep_lli_entry) *
1938 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP;
1940 /* Calculate the maximum size of data for input table */
1941 in_table_data_size =
1942 sep_calculate_lli_table_max_size(sep,
1943 &lli_in_array[current_in_entry],
1944 (sep_in_lli_entries - current_in_entry),
1947 /* Calculate the maximum size of data for output table */
1948 out_table_data_size =
1949 sep_calculate_lli_table_max_size(sep,
1950 &lli_out_array[current_out_entry],
1951 (sep_out_lli_entries - current_out_entry),
1954 dev_dbg(&sep->pdev->dev,
1955 "in_table_data_size is %x\n",
1956 in_table_data_size);
1958 dev_dbg(&sep->pdev->dev,
1959 "out_table_data_size is %x\n",
1960 out_table_data_size);
1962 table_data_size = in_table_data_size;
1964 if (!last_table_flag) {
1966 * If this is not the last table,
1967 * then must check where the data is smallest
1968 * and then align it to the block size
1970 if (table_data_size > out_table_data_size)
1971 table_data_size = out_table_data_size;
1974 * Now calculate the table size so that
1975 * it will be module block size
1977 table_data_size = (table_data_size / block_size) *
1981 dev_dbg(&sep->pdev->dev, "table_data_size is %x\n",
1984 /* Construct input lli table */
1985 sep_build_lli_table(sep, &lli_in_array[current_in_entry],
1988 &num_entries_in_table,
1991 /* Construct output lli table */
1992 sep_build_lli_table(sep, &lli_out_array[current_out_entry],
1995 &num_entries_out_table,
1998 /* If info entry is null - this is the first table built */
1999 if (info_in_entry_ptr == NULL) {
2000 /* Set the output parameters to physical addresses */
2002 sep_shared_area_virt_to_bus(sep, in_lli_table_ptr);
2004 *in_num_entries_ptr = num_entries_in_table;
2006 *lli_table_out_ptr =
2007 sep_shared_area_virt_to_bus(sep,
2010 *out_num_entries_ptr = num_entries_out_table;
2011 *table_data_size_ptr = table_data_size;
2013 dev_dbg(&sep->pdev->dev,
2014 "output lli_table_in_ptr is %08lx\n",
2015 (unsigned long)*lli_table_in_ptr);
2016 dev_dbg(&sep->pdev->dev,
2017 "output lli_table_out_ptr is %08lx\n",
2018 (unsigned long)*lli_table_out_ptr);
2020 /* Update the info entry of the previous in table */
2021 info_in_entry_ptr->bus_address =
2022 sep_shared_area_virt_to_bus(sep,
2025 info_in_entry_ptr->block_size =
2026 ((num_entries_in_table) << 24) |
2029 /* Update the info entry of the previous in table */
2030 info_out_entry_ptr->bus_address =
2031 sep_shared_area_virt_to_bus(sep,
2034 info_out_entry_ptr->block_size =
2035 ((num_entries_out_table) << 24) |
2038 dev_dbg(&sep->pdev->dev,
2039 "output lli_table_in_ptr:%08lx %08x\n",
2040 (unsigned long)info_in_entry_ptr->bus_address,
2041 info_in_entry_ptr->block_size);
2043 dev_dbg(&sep->pdev->dev,
2044 "output lli_table_out_ptr:%08lx %08x\n",
2045 (unsigned long)info_out_entry_ptr->bus_address,
2046 info_out_entry_ptr->block_size);
2049 /* Save the pointer to the info entry of the current tables */
2050 info_in_entry_ptr = in_lli_table_ptr +
2051 num_entries_in_table - 1;
2052 info_out_entry_ptr = out_lli_table_ptr +
2053 num_entries_out_table - 1;
2055 dev_dbg(&sep->pdev->dev,
2056 "output num_entries_out_table is %x\n",
2057 (u32)num_entries_out_table);
2058 dev_dbg(&sep->pdev->dev,
2059 "output info_in_entry_ptr is %lx\n",
2060 (unsigned long)info_in_entry_ptr);
2061 dev_dbg(&sep->pdev->dev,
2062 "output info_out_entry_ptr is %lx\n",
2063 (unsigned long)info_out_entry_ptr);
2066 /* Print input tables */
2067 sep_debug_print_lli_tables(sep,
2068 (struct sep_lli_entry *)
2069 sep_shared_area_bus_to_virt(sep, *lli_table_in_ptr),
2070 *in_num_entries_ptr,
2071 *table_data_size_ptr);
2073 /* Print output tables */
2074 sep_debug_print_lli_tables(sep,
2075 (struct sep_lli_entry *)
2076 sep_shared_area_bus_to_virt(sep, *lli_table_out_ptr),
2077 *out_num_entries_ptr,
2078 *table_data_size_ptr);
2080 dev_dbg(&sep->pdev->dev, "sep_construct_dma_tables_from_lli end\n");
2085 * sep_prepare_input_output_dma_table - prepare DMA I/O table
2086 * @app_virt_in_addr:
2087 * @app_virt_out_addr:
2090 * @lli_table_in_ptr:
2091 * @lli_table_out_ptr:
2092 * @in_num_entries_ptr:
2093 * @out_num_entries_ptr:
2094 * @table_data_size_ptr:
2095 * @is_kva: set for kernel data; used only for kernel crypto module
2097 * This function builds input and output DMA tables for synhronic
2098 * symmetric operations (AES, DES, HASH). It also checks that each table
2099 * is of the modular block size
2100 * Note that all bus addresses that are passed to the SEP
2101 * are in 32 bit format; the SEP is a 32 bit device
2103 static int sep_prepare_input_output_dma_table(struct sep_device *sep,
2104 unsigned long app_virt_in_addr,
2105 unsigned long app_virt_out_addr,
2108 dma_addr_t *lli_table_in_ptr,
2109 dma_addr_t *lli_table_out_ptr,
2110 u32 *in_num_entries_ptr,
2111 u32 *out_num_entries_ptr,
2112 u32 *table_data_size_ptr,
2117 /* Array of pointers of page */
2118 struct sep_lli_entry *lli_in_array;
2119 /* Array of pointers of page */
2120 struct sep_lli_entry *lli_out_array;
2122 dev_dbg(&sep->pdev->dev, "sep_prepare_input_output_dma_table start\n");
2124 if (data_size == 0) {
2125 /* Prepare empty table for input and output */
2126 sep_prepare_empty_lli_table(sep, lli_table_in_ptr,
2127 in_num_entries_ptr, table_data_size_ptr);
2129 sep_prepare_empty_lli_table(sep, lli_table_out_ptr,
2130 out_num_entries_ptr, table_data_size_ptr);
2132 goto update_dcb_counter;
2135 /* Initialize the pages pointers */
2136 sep->dma_res_arr[sep->nr_dcb_creat].in_page_array = NULL;
2137 sep->dma_res_arr[sep->nr_dcb_creat].out_page_array = NULL;
2139 /* Lock the pages of the buffer and translate them to pages */
2140 if (is_kva == true) {
2141 error = sep_lock_kernel_pages(sep, app_virt_in_addr,
2142 data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2145 dev_warn(&sep->pdev->dev,
2146 "lock kernel for in failed\n");
2150 error = sep_lock_kernel_pages(sep, app_virt_out_addr,
2151 data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2154 dev_warn(&sep->pdev->dev,
2155 "lock kernel for out failed\n");
2161 error = sep_lock_user_pages(sep, app_virt_in_addr,
2162 data_size, &lli_in_array, SEP_DRIVER_IN_FLAG);
2164 dev_warn(&sep->pdev->dev,
2165 "sep_lock_user_pages for input virtual buffer failed\n");
2169 error = sep_lock_user_pages(sep, app_virt_out_addr,
2170 data_size, &lli_out_array, SEP_DRIVER_OUT_FLAG);
2173 dev_warn(&sep->pdev->dev,
2174 "sep_lock_user_pages for output virtual buffer failed\n");
2175 goto end_function_free_lli_in;
2179 dev_dbg(&sep->pdev->dev, "sep_in_num_pages is %x\n",
2180 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages);
2181 dev_dbg(&sep->pdev->dev, "sep_out_num_pages is %x\n",
2182 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages);
2183 dev_dbg(&sep->pdev->dev, "SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP is %x\n",
2184 SEP_DRIVER_ENTRIES_PER_TABLE_IN_SEP);
2186 /* Call the fucntion that creates table from the lli arrays */
2187 error = sep_construct_dma_tables_from_lli(sep, lli_in_array,
2188 sep->dma_res_arr[sep->nr_dcb_creat].in_num_pages,
2190 sep->dma_res_arr[sep->nr_dcb_creat].out_num_pages,
2191 block_size, lli_table_in_ptr, lli_table_out_ptr,
2192 in_num_entries_ptr, out_num_entries_ptr, table_data_size_ptr);
2195 dev_warn(&sep->pdev->dev,
2196 "sep_construct_dma_tables_from_lli failed\n");
2197 goto end_function_with_error;
2200 kfree(lli_out_array);
2201 kfree(lli_in_array);
2204 /* Update DCB counter */
2205 sep->nr_dcb_creat++;
2206 /* Fall through - free the lli entry arrays */
2207 dev_dbg(&sep->pdev->dev, "in_num_entries_ptr is %08x\n",
2208 *in_num_entries_ptr);
2209 dev_dbg(&sep->pdev->dev, "out_num_entries_ptr is %08x\n",
2210 *out_num_entries_ptr);
2211 dev_dbg(&sep->pdev->dev, "table_data_size_ptr is %08x\n",
2212 *table_data_size_ptr);
2216 end_function_with_error:
2217 kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_map_array);
2218 kfree(sep->dma_res_arr[sep->nr_dcb_creat].out_page_array);
2219 kfree(lli_out_array);
2222 end_function_free_lli_in:
2223 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_map_array);
2224 kfree(sep->dma_res_arr[sep->nr_dcb_creat].in_page_array);
2225 kfree(lli_in_array);
2228 dev_dbg(&sep->pdev->dev,
2229 "sep_prepare_input_output_dma_table end result = %d\n", error);
2236 * sep_prepare_input_output_dma_table_in_dcb - prepare control blocks
2237 * @app_in_address: unsigned long; for data buffer in (user space)
2238 * @app_out_address: unsigned long; for data buffer out (user space)
2239 * @data_in_size: u32; for size of data
2240 * @block_size: u32; for block size
2241 * @tail_block_size: u32; for size of tail block
2242 * @isapplet: bool; to indicate external app
2243 * @is_kva: bool; kernel buffer; only used for kernel crypto module
2245 * This function prepares the linked DMA tables and puts the
2246 * address for the linked list of tables inta a DCB (data control
2247 * block) the address of which is known by the SEP hardware
2248 * Note that all bus addresses that are passed to the SEP
2249 * are in 32 bit format; the SEP is a 32 bit device
2251 static int sep_prepare_input_output_dma_table_in_dcb(struct sep_device *sep,
2252 unsigned long app_in_address,
2253 unsigned long app_out_address,
2256 u32 tail_block_size,
2263 /* Address of the created DCB table */
2264 struct sep_dcblock *dcb_table_ptr = NULL;
2265 /* The physical address of the first input DMA table */
2266 dma_addr_t in_first_mlli_address = 0;
2267 /* Number of entries in the first input DMA table */
2268 u32 in_first_num_entries = 0;
2269 /* The physical address of the first output DMA table */
2270 dma_addr_t out_first_mlli_address = 0;
2271 /* Number of entries in the first output DMA table */
2272 u32 out_first_num_entries = 0;
2273 /* Data in the first input/output table */
2274 u32 first_data_size = 0;
2276 dev_dbg(&sep->pdev->dev, "prepare_input_output_dma_table_in_dcb start\n");
2278 if (sep->nr_dcb_creat == SEP_MAX_NUM_SYNC_DMA_OPS) {
2279 /* No more DCBs to allocate */
2280 dev_warn(&sep->pdev->dev, "no more DCBs available\n");
2285 /* Allocate new DCB */
2286 dcb_table_ptr = (struct sep_dcblock *)(sep->shared_addr +
2287 SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES +
2288 (sep->nr_dcb_creat * sizeof(struct sep_dcblock)));
2290 /* Set the default values in the DCB */
2291 dcb_table_ptr->input_mlli_address = 0;
2292 dcb_table_ptr->input_mlli_num_entries = 0;
2293 dcb_table_ptr->input_mlli_data_size = 0;
2294 dcb_table_ptr->output_mlli_address = 0;
2295 dcb_table_ptr->output_mlli_num_entries = 0;
2296 dcb_table_ptr->output_mlli_data_size = 0;
2297 dcb_table_ptr->tail_data_size = 0;
2298 dcb_table_ptr->out_vr_tail_pt = 0;
2300 if (isapplet == true) {
2301 tail_size = data_in_size % block_size;
2303 if (data_in_size < tail_block_size) {
2304 dev_warn(&sep->pdev->dev, "data in size smaller than tail block size\n");
2308 if (tail_block_size)
2310 * Case the tail size should be
2311 * bigger than the real block size
2313 tail_size = tail_block_size +
2315 tail_block_size) % block_size);
2318 /* Check if there is enough data for DMA operation */
2319 if (data_in_size < SEP_DRIVER_MIN_DATA_SIZE_PER_TABLE) {
2320 if (is_kva == true) {
2321 memcpy(dcb_table_ptr->tail_data,
2322 (void *)app_in_address, data_in_size);
2324 if (copy_from_user(dcb_table_ptr->tail_data,
2325 (void __user *)app_in_address,
2332 dcb_table_ptr->tail_data_size = data_in_size;
2334 /* Set the output user-space address for mem2mem op */
2335 if (app_out_address)
2336 dcb_table_ptr->out_vr_tail_pt =
2337 (u32)app_out_address;
2340 * Update both data length parameters in order to avoid
2341 * second data copy and allow building of empty mlli
2348 if (is_kva == true) {
2349 memcpy(dcb_table_ptr->tail_data,
2350 (void *)(app_in_address + data_in_size -
2351 tail_size), tail_size);
2353 /* We have tail data - copy it to DCB */
2354 if (copy_from_user(dcb_table_ptr->tail_data,
2355 (void *)(app_in_address +
2356 data_in_size - tail_size), tail_size)) {
2361 if (app_out_address)
2363 * Calculate the output address
2364 * according to tail data size
2366 dcb_table_ptr->out_vr_tail_pt =
2367 (u32)app_out_address + data_in_size
2370 /* Save the real tail data size */
2371 dcb_table_ptr->tail_data_size = tail_size;
2373 * Update the data size without the tail
2374 * data size AKA data for the dma
2376 data_in_size = (data_in_size - tail_size);
2379 /* Check if we need to build only input table or input/output */
2380 if (app_out_address) {
2381 /* Prepare input/output tables */
2382 error = sep_prepare_input_output_dma_table(sep,
2387 &in_first_mlli_address,
2388 &out_first_mlli_address,
2389 &in_first_num_entries,
2390 &out_first_num_entries,
2394 /* Prepare input tables */
2395 error = sep_prepare_input_dma_table(sep,
2399 &in_first_mlli_address,
2400 &in_first_num_entries,
2406 dev_warn(&sep->pdev->dev, "prepare DMA table call failed from prepare DCB call\n");
2410 /* Set the DCB values */
2411 dcb_table_ptr->input_mlli_address = in_first_mlli_address;
2412 dcb_table_ptr->input_mlli_num_entries = in_first_num_entries;
2413 dcb_table_ptr->input_mlli_data_size = first_data_size;
2414 dcb_table_ptr->output_mlli_address = out_first_mlli_address;
2415 dcb_table_ptr->output_mlli_num_entries = out_first_num_entries;
2416 dcb_table_ptr->output_mlli_data_size = first_data_size;
2419 dev_dbg(&sep->pdev->dev,
2420 "sep_prepare_input_output_dma_table_in_dcb end\n");
2427 * sep_create_sync_dma_tables_handler - create sync DMA tables
2428 * @sep: pointer to struct sep_device
2429 * @arg: pointer to struct bld_syn_tab_struct
2431 * Handle the request for creation of the DMA tables for the synchronic
2432 * symmetric operations (AES,DES). Note that all bus addresses that are
2433 * passed to the SEP are in 32 bit format; the SEP is a 32 bit device
2435 static int sep_create_sync_dma_tables_handler(struct sep_device *sep,
2440 /* Command arguments */
2441 struct bld_syn_tab_struct command_args;
2443 dev_dbg(&sep->pdev->dev,
2444 "sep_create_sync_dma_tables_handler start\n");
2446 if (copy_from_user(&command_args, (void __user *)arg,
2447 sizeof(struct bld_syn_tab_struct))) {
2452 dev_dbg(&sep->pdev->dev, "app_in_address is %08llx\n",
2453 command_args.app_in_address);
2454 dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2455 command_args.app_out_address);
2456 dev_dbg(&sep->pdev->dev, "data_size is %u\n",
2457 command_args.data_in_size);
2458 dev_dbg(&sep->pdev->dev, "block_size is %u\n",
2459 command_args.block_size);
2461 /* Validate user parameters */
2462 if (!command_args.app_in_address) {
2467 error = sep_prepare_input_output_dma_table_in_dcb(sep,
2468 (unsigned long)command_args.app_in_address,
2469 (unsigned long)command_args.app_out_address,
2470 command_args.data_in_size,
2471 command_args.block_size,
2477 dev_dbg(&sep->pdev->dev, "sep_create_sync_dma_tables_handler end\n");
2482 * sep_free_dma_tables_and_dcb - free DMA tables and DCBs
2483 * @sep: pointer to struct sep_device
2484 * @isapplet: indicates external application (used for kernel access)
2485 * @is_kva: indicates kernel addresses (only used for kernel crypto)
2487 * This function frees the DMA tables and DCB
2489 static int sep_free_dma_tables_and_dcb(struct sep_device *sep, bool isapplet,
2495 struct sep_dcblock *dcb_table_ptr;
2496 unsigned long pt_hold;
2499 dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb start\n");
2501 if (isapplet == true) {
2502 /* Set pointer to first DCB table */
2503 dcb_table_ptr = (struct sep_dcblock *)
2505 SEP_DRIVER_SYSTEM_DCB_MEMORY_OFFSET_IN_BYTES);
2507 /* Go over each DCB and see if tail pointer must be updated */
2508 for (i = 0; i < sep->nr_dcb_creat; i++, dcb_table_ptr++) {
2509 if (dcb_table_ptr->out_vr_tail_pt) {
2510 pt_hold = (unsigned long)dcb_table_ptr->out_vr_tail_pt;
2511 tail_pt = (void *)pt_hold;
2512 if (is_kva == true) {
2514 dcb_table_ptr->tail_data,
2515 dcb_table_ptr->tail_data_size);
2517 error_temp = copy_to_user(
2519 dcb_table_ptr->tail_data,
2520 dcb_table_ptr->tail_data_size);
2523 /* Release the DMA resource */
2530 /* Free the output pages, if any */
2531 sep_free_dma_table_data_handler(sep);
2533 dev_dbg(&sep->pdev->dev, "sep_free_dma_tables_and_dcb end\n");
2538 * sep_get_static_pool_addr_handler - get static pool address
2539 * @sep: pointer to struct sep_device
2540 * @arg: parameters from user space application
2542 * This function sets the bus and virtual addresses of the static pool
2543 * and returns the virtual address
2545 static int sep_get_static_pool_addr_handler(struct sep_device *sep)
2547 u32 *static_pool_addr = NULL;
2549 dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler start\n");
2551 static_pool_addr = (u32 *)(sep->shared_addr +
2552 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2554 static_pool_addr[0] = SEP_STATIC_POOL_VAL_TOKEN;
2555 static_pool_addr[1] = (u32)sep->shared_bus +
2556 SEP_DRIVER_STATIC_AREA_OFFSET_IN_BYTES;
2558 dev_dbg(&sep->pdev->dev, "static pool: physical %x\n",
2559 (u32)static_pool_addr[1]);
2561 dev_dbg(&sep->pdev->dev, "sep_get_static_pool_addr_handler end\n");
2567 * sep_start_handler - start device
2568 * @sep: pointer to struct sep_device
2570 static int sep_start_handler(struct sep_device *sep)
2572 unsigned long reg_val;
2573 unsigned long error = 0;
2575 dev_dbg(&sep->pdev->dev, "sep_start_handler start\n");
2577 /* Wait in polling for message from SEP */
2579 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2582 /* Check the value */
2584 /* Fatal error - read error status from GPRO */
2585 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2586 dev_dbg(&sep->pdev->dev, "sep_start_handler end\n");
2591 * ep_check_sum_calc - checksum messages
2592 * @data: buffer to checksum
2593 * @length: buffer size
2595 * This function performs a checksum for messages that are sent
2598 static u32 sep_check_sum_calc(u8 *data, u32 length)
2601 u16 *Tdata = (u16 *)data;
2603 while (length > 1) {
2604 /* This is the inner loop */
2609 /* Add left-over byte, if any */
2611 sum += *(u8 *)Tdata;
2613 /* Fold 32-bit sum to 16 bits */
2615 sum = (sum & 0xffff) + (sum >> 16);
2617 return ~sum & 0xFFFF;
2621 * sep_init_handler -
2622 * @sep: pointer to struct sep_device
2623 * @arg: parameters from user space application
2625 * Handles the request for SEP initialization
2626 * Note that this will go away for Medfield once the SCU
2627 * SEP initialization is complete
2628 * Also note that the message to the SEP has components
2629 * from user space as well as components written by the driver
2630 * This is becuase the portions of the message that pertain to
2631 * physical addresses must be set by the driver after the message
2632 * leaves custody of the user space application for security
2635 static int sep_init_handler(struct sep_device *sep, unsigned long arg)
2637 u32 message_buff[14];
2641 dma_addr_t new_base_addr;
2642 unsigned long addr_hold;
2643 struct init_struct command_args;
2645 dev_dbg(&sep->pdev->dev, "sep_init_handler start\n");
2647 /* Make sure that we have not initialized already */
2648 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2650 if (reg_val != 0x2) {
2651 error = SEP_ALREADY_INITIALIZED_ERR;
2652 dev_warn(&sep->pdev->dev, "init; device already initialized\n");
2656 /* Only root can initialize */
2657 if (!capable(CAP_SYS_ADMIN)) {
2662 /* Copy in the parameters */
2663 error = copy_from_user(&command_args, (void __user *)arg,
2664 sizeof(struct init_struct));
2671 /* Validate parameters */
2672 if (!command_args.message_addr || !command_args.sep_sram_addr ||
2673 command_args.message_size_in_words > 14) {
2678 /* Copy in the SEP init message */
2679 addr_hold = (unsigned long)command_args.message_addr;
2680 error = copy_from_user(message_buff,
2681 (void __user *)addr_hold,
2682 command_args.message_size_in_words*sizeof(u32));
2689 /* Load resident, cache, and extapp firmware */
2690 error = sep_load_firmware(sep);
2693 dev_warn(&sep->pdev->dev,
2694 "init; copy SEP init message failed %x\n", error);
2698 /* Compute the base address */
2699 new_base_addr = sep->shared_bus;
2701 if (sep->resident_bus < new_base_addr)
2702 new_base_addr = sep->resident_bus;
2704 if (sep->cache_bus < new_base_addr)
2705 new_base_addr = sep->cache_bus;
2707 if (sep->dcache_bus < new_base_addr)
2708 new_base_addr = sep->dcache_bus;
2710 /* Put physical addresses in SEP message */
2711 message_buff[3] = (u32)new_base_addr;
2712 message_buff[4] = (u32)sep->shared_bus;
2713 message_buff[6] = (u32)sep->resident_bus;
2714 message_buff[7] = (u32)sep->cache_bus;
2715 message_buff[8] = (u32)sep->dcache_bus;
2717 message_buff[command_args.message_size_in_words - 1] = 0x0;
2718 message_buff[command_args.message_size_in_words - 1] =
2719 sep_check_sum_calc((u8 *)message_buff,
2720 command_args.message_size_in_words*sizeof(u32));
2722 /* Debug print of message */
2723 for (counter = 0; counter < command_args.message_size_in_words;
2725 dev_dbg(&sep->pdev->dev, "init; SEP message word %d is %x\n",
2726 counter, message_buff[counter]);
2728 /* Tell the SEP the sram address */
2729 sep_write_reg(sep, HW_SRAM_ADDR_REG_ADDR, command_args.sep_sram_addr);
2731 /* Push the message to the SEP */
2732 for (counter = 0; counter < command_args.message_size_in_words;
2734 sep_write_reg(sep, HW_SRAM_DATA_REG_ADDR,
2735 message_buff[counter]);
2736 sep_wait_sram_write(sep);
2739 /* Signal SEP that message is ready and to init */
2740 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x1);
2742 /* Wait for acknowledge */
2743 dev_dbg(&sep->pdev->dev, "init; waiting for msg response\n");
2746 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2747 } while (!(reg_val & 0xFFFFFFFD));
2749 if (reg_val == 0x1) {
2750 dev_warn(&sep->pdev->dev, "init; device int failed\n");
2751 error = sep_read_reg(sep, 0x8060);
2752 dev_warn(&sep->pdev->dev, "init; sw monitor is %x\n", error);
2753 error = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
2754 dev_warn(&sep->pdev->dev, "init; error is %x\n", error);
2757 dev_dbg(&sep->pdev->dev, "init; end CC INIT, reg_val is %x\n", reg_val);
2759 /* Signal SEP to zero the GPR3 */
2760 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR0_REG_ADDR, 0x10);
2762 /* Wait for response */
2763 dev_dbg(&sep->pdev->dev, "init; waiting for zero set response\n");
2766 reg_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
2767 } while (reg_val != 0);
2770 dev_dbg(&sep->pdev->dev, "init is done\n");
2775 * sep_end_transaction_handler - end transaction
2776 * @sep: pointer to struct sep_device
2778 * This API handles the end transaction request
2780 static int sep_end_transaction_handler(struct sep_device *sep)
2782 dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler start\n");
2784 /* Clear the data pool pointers Token */
2785 memset((void *)(sep->shared_addr +
2786 SEP_DRIVER_DATA_POOL_ALLOCATION_OFFSET_IN_BYTES),
2787 0, sep->num_of_data_allocations*2*sizeof(u32));
2789 /* Check that all the DMA resources were freed */
2790 sep_free_dma_table_data_handler(sep);
2792 clear_bit(SEP_MMAP_LOCK_BIT, &sep->in_use_flags);
2795 * We are now through with the transaction. Let's
2796 * allow other processes who have the device open
2797 * to perform transactions
2799 mutex_lock(&sep->sep_mutex);
2800 sep->pid_doing_transaction = 0;
2801 mutex_unlock(&sep->sep_mutex);
2802 /* Raise event for stuck contextes */
2803 wake_up(&sep->event);
2805 dev_dbg(&sep->pdev->dev, "waking up event\n");
2806 dev_dbg(&sep->pdev->dev, "sep_end_transaction_handler end\n");
2812 * sep_prepare_dcb_handler - prepare a control block
2813 * @sep: pointer to struct sep_device
2814 * @arg: pointer to user parameters
2816 * This function will retrieve the RAR buffer physical addresses, type
2817 * & size corresponding to the RAR handles provided in the buffers vector.
2819 static int sep_prepare_dcb_handler(struct sep_device *sep, unsigned long arg)
2822 /* Command arguments */
2823 struct build_dcb_struct command_args;
2825 dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");
2827 /* Get the command arguments */
2828 if (copy_from_user(&command_args, (void __user *)arg,
2829 sizeof(struct build_dcb_struct))) {
2834 dev_dbg(&sep->pdev->dev, "app_in_address is %08llx\n",
2835 command_args.app_in_address);
2836 dev_dbg(&sep->pdev->dev, "app_out_address is %08llx\n",
2837 command_args.app_out_address);
2838 dev_dbg(&sep->pdev->dev, "data_size is %x\n",
2839 command_args.data_in_size);
2840 dev_dbg(&sep->pdev->dev, "block_size is %x\n",
2841 command_args.block_size);
2842 dev_dbg(&sep->pdev->dev, "tail block_size is %x\n",
2843 command_args.tail_block_size);
2845 error = sep_prepare_input_output_dma_table_in_dcb(sep,
2846 (unsigned long)command_args.app_in_address,
2847 (unsigned long)command_args.app_out_address,
2848 command_args.data_in_size, command_args.block_size,
2849 command_args.tail_block_size, true, false);
2852 dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler end\n");
2858 * sep_free_dcb_handler - free control block resources
2859 * @sep: pointer to struct sep_device
2861 * This function frees the DCB resources and updates the needed
2862 * user-space buffers.
2864 static int sep_free_dcb_handler(struct sep_device *sep)
2868 dev_dbg(&sep->pdev->dev, "sep_prepare_dcb_handler start\n");
2869 dev_dbg(&sep->pdev->dev, "num of DCBs %x\n", sep->nr_dcb_creat);
2871 error = sep_free_dma_tables_and_dcb(sep, false, false);
2873 dev_dbg(&sep->pdev->dev, "sep_free_dcb_handler end\n");
2878 * sep_rar_prepare_output_msg_handler - prepare an output message
2879 * @sep: pointer to struct sep_device
2880 * @arg: pointer to user parameters
2882 * This function will retrieve the RAR buffer physical addresses, type
2883 * & size corresponding to the RAR handles provided in the buffers vector.
2885 static int sep_rar_prepare_output_msg_handler(struct sep_device *sep,
2890 struct rar_hndl_to_bus_struct command_args;
2891 struct RAR_buffer rar_buf;
2893 dma_addr_t rar_bus = 0;
2894 /* Holds the RAR address in the system memory offset */
2897 dev_dbg(&sep->pdev->dev, "sep_rar_prepare_output_msg_handler start\n");
2900 if (copy_from_user(&command_args, (void __user *)arg,
2901 sizeof(command_args))) {
2906 /* Call to translation function only if user handle is not NULL */
2907 if (command_args.rar_handle) {
2908 memset(&rar_buf, 0, sizeof(rar_buf));
2909 rar_buf.info.handle = (u32)command_args.rar_handle;
2911 if (rar_handle_to_bus(&rar_buf, 1) != 1) {
2912 dev_dbg(&sep->pdev->dev, "rar_handle_to_bus failure\n");
2916 rar_bus = rar_buf.bus_address;
2918 dev_dbg(&sep->pdev->dev, "rar msg; rar_addr_bus = %x\n", (u32)rar_bus);
2920 /* Set value in the SYSTEM MEMORY offset */
2921 rar_addr = (u32 *)(sep->shared_addr +
2922 SEP_DRIVER_SYSTEM_RAR_MEMORY_OFFSET_IN_BYTES);
2924 /* Copy the physical address to the System Area for the SEP */
2925 rar_addr[0] = SEP_RAR_VAL_TOKEN;
2926 rar_addr[1] = rar_bus;
2929 dev_dbg(&sep->pdev->dev, "sep_rar_prepare_output_msg_handler start\n");
2934 * sep_realloc_ext_cache_handler - report location of extcache
2935 * @sep: pointer to struct sep_device
2936 * @arg: pointer to user parameters
2938 * This function tells the SEP where the extapp is located
2940 static int sep_realloc_ext_cache_handler(struct sep_device *sep,
2943 /* Holds the new ext cache address in the system memory offset */
2946 /* Set value in the SYSTEM MEMORY offset */
2947 system_addr = (u32 *)(sep->shared_addr +
2948 SEP_DRIVER_SYSTEM_EXT_CACHE_ADDR_OFFSET_IN_BYTES);
2950 /* Copy the physical address to the System Area for the SEP */
2951 system_addr[0] = SEP_EXT_CACHE_ADDR_VAL_TOKEN;
2952 dev_dbg(&sep->pdev->dev, "ext cache init; system addr 0 is %x\n",
2954 system_addr[1] = sep->extapp_bus;
2955 dev_dbg(&sep->pdev->dev, "ext cache init; system addr 1 is %x\n",
2962 * sep_ioctl - ioctl api
2963 * @filp: pointer to struct file
2965 * @arg: pointer to argument structure
2967 * Implement the ioctl methods availble on the SEP device.
2969 static long sep_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2972 struct sep_device *sep = filp->private_data;
2974 dev_dbg(&sep->pdev->dev, "ioctl start\n");
2976 dev_dbg(&sep->pdev->dev, "cmd is %x\n", cmd);
2978 /* Make sure we own this device */
2979 mutex_lock(&sep->sep_mutex);
2980 if ((current->pid != sep->pid_doing_transaction) &&
2981 (sep->pid_doing_transaction != 0)) {
2982 dev_dbg(&sep->pdev->dev, "ioctl pid is not owner\n");
2983 mutex_unlock(&sep->sep_mutex);
2988 mutex_unlock(&sep->sep_mutex);
2990 /* Check that the command is for SEP device */
2991 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
2996 /* Lock to prevent the daemon to interfere with operation */
2997 mutex_lock(&sep->ioctl_mutex);
3000 case SEP_IOCSENDSEPCOMMAND:
3001 /* Send command to SEP */
3002 error = sep_send_command_handler(sep);
3004 case SEP_IOCALLOCDATAPOLL:
3005 /* Allocate data pool */
3006 error = sep_allocate_data_pool_memory_handler(sep, arg);
3008 case SEP_IOCCREATESYMDMATABLE:
3009 /* Create DMA table for synhronic operation */
3010 error = sep_create_sync_dma_tables_handler(sep, arg);
3012 case SEP_IOCFREEDMATABLEDATA:
3013 /* Free the pages */
3014 error = sep_free_dma_table_data_handler(sep);
3016 case SEP_IOCSEPSTART:
3017 /* Start command to SEP */
3018 if (sep->pdev->revision == 0) /* Only for old chip */
3019 error = sep_start_handler(sep);
3021 error = -EPERM; /* Not permitted on new chip */
3023 case SEP_IOCSEPINIT:
3024 /* Init command to SEP */
3025 if (sep->pdev->revision == 0) /* Only for old chip */
3026 error = sep_init_handler(sep, arg);
3028 error = -EPERM; /* Not permitted on new chip */
3030 case SEP_IOCGETSTATICPOOLADDR:
3031 /* Get the physical and virtual addresses of the static pool */
3032 error = sep_get_static_pool_addr_handler(sep);
3034 case SEP_IOCENDTRANSACTION:
3035 error = sep_end_transaction_handler(sep);
3037 case SEP_IOCREALLOCEXTCACHE:
3038 if (sep->pdev->revision == 0) /* Only for old chip */
3039 error = sep_realloc_ext_cache_handler(sep, arg);
3041 error = -EPERM; /* Not permitted on new chip */
3043 case SEP_IOCRARPREPAREMESSAGE:
3044 error = sep_rar_prepare_output_msg_handler(sep, arg);
3046 case SEP_IOCPREPAREDCB:
3047 error = sep_prepare_dcb_handler(sep, arg);
3049 case SEP_IOCFREEDCB:
3050 error = sep_free_dcb_handler(sep);
3053 dev_dbg(&sep->pdev->dev, "invalid ioctl %x\n", cmd);
3057 mutex_unlock(&sep->ioctl_mutex);
3060 dev_dbg(&sep->pdev->dev, "ioctl end\n");
3065 * sep_singleton_ioctl - ioctl api for singleton interface
3066 * @filp: pointer to struct file
3068 * @arg: pointer to argument structure
3070 * Implement the additional ioctls for the singleton device
3072 static long sep_singleton_ioctl(struct file *filp, u32 cmd, unsigned long arg)
3075 struct sep_device *sep = filp->private_data;
3077 dev_dbg(&sep->pdev->dev, "singleton_ioctl start\n");
3078 dev_dbg(&sep->pdev->dev, "cmd is %x\n", cmd);
3080 /* Check that the command is for the SEP device */
3081 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
3086 /* Make sure we own this device */
3087 mutex_lock(&sep->sep_mutex);
3088 if ((current->pid != sep->pid_doing_transaction) &&
3089 (sep->pid_doing_transaction != 0)) {
3090 dev_dbg(&sep->pdev->dev, "singleton ioctl pid is not owner\n");
3091 mutex_unlock(&sep->sep_mutex);
3096 mutex_unlock(&sep->sep_mutex);
3099 case SEP_IOCTLSETCALLERID:
3100 mutex_lock(&sep->ioctl_mutex);
3101 error = sep_set_caller_id_handler(sep, arg);
3102 mutex_unlock(&sep->ioctl_mutex);
3105 error = sep_ioctl(filp, cmd, arg);
3110 dev_dbg(&sep->pdev->dev, "singleton ioctl end\n");
3115 * sep_request_daemon_ioctl - ioctl for daemon
3116 * @filp: pointer to struct file
3118 * @arg: pointer to argument structure
3120 * Called by the request daemon to perform ioctls on the daemon device
3122 static long sep_request_daemon_ioctl(struct file *filp, u32 cmd,
3127 struct sep_device *sep = filp->private_data;
3129 dev_dbg(&sep->pdev->dev, "daemon ioctl: start\n");
3130 dev_dbg(&sep->pdev->dev, "daemon ioctl: cmd is %x\n", cmd);
3132 /* Check that the command is for SEP device */
3133 if (_IOC_TYPE(cmd) != SEP_IOC_MAGIC_NUMBER) {
3138 /* Only one process can access ioctl at any given time */
3139 mutex_lock(&sep->ioctl_mutex);
3142 case SEP_IOCSENDSEPRPLYCOMMAND:
3143 /* Send reply command to SEP */
3144 error = sep_req_daemon_send_reply_command_handler(sep);
3146 case SEP_IOCENDTRANSACTION:
3148 * End req daemon transaction, do nothing
3149 * will be removed upon update in middleware
3155 dev_dbg(&sep->pdev->dev, "daemon ioctl: no such IOCTL\n");
3158 mutex_unlock(&sep->ioctl_mutex);
3161 dev_dbg(&sep->pdev->dev, "daemon ioctl: end\n");
3167 * sep_inthandler - interrupt handler
3169 * @dev_id: device id
3171 static irqreturn_t sep_inthandler(int irq, void *dev_id)
3173 irqreturn_t int_error = IRQ_HANDLED;
3174 unsigned long lck_flags;
3175 u32 reg_val, reg_val2 = 0;
3176 struct sep_device *sep = dev_id;
3178 /* Read the IRR register to check if this is SEP interrupt */
3179 reg_val = sep_read_reg(sep, HW_HOST_IRR_REG_ADDR);
3180 dev_dbg(&sep->pdev->dev, "SEP Interrupt - reg is %08x\n", reg_val);
3182 if (reg_val & (0x1 << 13)) {
3183 /* Lock and update the counter of reply messages */
3184 spin_lock_irqsave(&sep->snd_rply_lck, lck_flags);
3186 spin_unlock_irqrestore(&sep->snd_rply_lck, lck_flags);
3188 dev_dbg(&sep->pdev->dev, "sep int: send_ct %lx reply_ct %lx\n",
3189 sep->send_ct, sep->reply_ct);
3191 /* Is this printf or daemon request? */
3192 reg_val2 = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3193 dev_dbg(&sep->pdev->dev,
3194 "SEP Interrupt - reg2 is %08x\n", reg_val2);
3196 if ((reg_val2 >> 30) & 0x1) {
3197 dev_dbg(&sep->pdev->dev, "int: printf request\n");
3198 wake_up(&sep->event_request_daemon);
3199 } else if (reg_val2 >> 31) {
3200 dev_dbg(&sep->pdev->dev, "int: daemon request\n");
3201 wake_up(&sep->event_request_daemon);
3203 dev_dbg(&sep->pdev->dev, "int: SEP reply\n");
3204 wake_up(&sep->event);
3207 dev_dbg(&sep->pdev->dev, "int: not SEP interrupt\n");
3208 int_error = IRQ_NONE;
3210 if (int_error == IRQ_HANDLED)
3211 sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, reg_val);
3217 * sep_reconfig_shared_area - reconfigure shared area
3218 * @sep: pointer to struct sep_device
3220 * Reconfig the shared area between HOST and SEP - needed in case
3221 * the DX_CC_Init function was called before OS loading.
3223 static int sep_reconfig_shared_area(struct sep_device *sep)
3227 /* use to limit waiting for SEP */
3228 unsigned long end_time;
3230 dev_dbg(&sep->pdev->dev, "reconfig shared area start\n");
3232 /* Send the new SHARED MESSAGE AREA to the SEP */
3233 dev_dbg(&sep->pdev->dev, "sending %08llx to sep\n",
3234 (unsigned long long)sep->shared_bus);
3236 sep_write_reg(sep, HW_HOST_HOST_SEP_GPR1_REG_ADDR, sep->shared_bus);
3238 /* Poll for SEP response */
3239 ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3241 end_time = jiffies + (WAIT_TIME * HZ);
3243 while ((time_before(jiffies, end_time)) && (ret_val != 0xffffffff) &&
3244 (ret_val != sep->shared_bus))
3245 ret_val = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR1_REG_ADDR);
3247 /* Check the return value (register) */
3248 if (ret_val != sep->shared_bus) {
3249 dev_warn(&sep->pdev->dev, "could not reconfig shared area\n");
3250 dev_warn(&sep->pdev->dev, "result was %x\n", ret_val);
3255 dev_dbg(&sep->pdev->dev, "reconfig shared area end\n");
3259 /* File operation for singleton SEP operations */
3260 static const struct file_operations singleton_file_operations = {
3261 .owner = THIS_MODULE,
3262 .unlocked_ioctl = sep_singleton_ioctl,
3264 .open = sep_singleton_open,
3265 .release = sep_singleton_release,
3269 /* File operation for daemon operations */
3270 static const struct file_operations daemon_file_operations = {
3271 .owner = THIS_MODULE,
3272 .unlocked_ioctl = sep_request_daemon_ioctl,
3273 .poll = sep_request_daemon_poll,
3274 .open = sep_request_daemon_open,
3275 .release = sep_request_daemon_release,
3276 .mmap = sep_request_daemon_mmap,
3279 /* The files operations structure of the driver */
3280 static const struct file_operations sep_file_operations = {
3281 .owner = THIS_MODULE,
3282 .unlocked_ioctl = sep_ioctl,
3285 .release = sep_release,
3290 * sep_register_driver_with_fs - register misc devices
3291 * @sep: pointer to struct sep_device
3293 * This function registers the driver with the file system
3295 static int sep_register_driver_with_fs(struct sep_device *sep)
3299 sep->miscdev_sep.minor = MISC_DYNAMIC_MINOR;
3300 sep->miscdev_sep.name = SEP_DEV_NAME;
3301 sep->miscdev_sep.fops = &sep_file_operations;
3303 sep->miscdev_singleton.minor = MISC_DYNAMIC_MINOR;
3304 sep->miscdev_singleton.name = SEP_DEV_SINGLETON;
3305 sep->miscdev_singleton.fops = &singleton_file_operations;
3307 sep->miscdev_daemon.minor = MISC_DYNAMIC_MINOR;
3308 sep->miscdev_daemon.name = SEP_DEV_DAEMON;
3309 sep->miscdev_daemon.fops = &daemon_file_operations;
3311 ret_val = misc_register(&sep->miscdev_sep);
3313 dev_warn(&sep->pdev->dev, "misc reg fails for SEP %x\n",
3318 ret_val = misc_register(&sep->miscdev_singleton);
3320 dev_warn(&sep->pdev->dev, "misc reg fails for sing %x\n",
3322 misc_deregister(&sep->miscdev_sep);
3326 ret_val = misc_register(&sep->miscdev_daemon);
3328 dev_warn(&sep->pdev->dev, "misc reg fails for dmn %x\n",
3330 misc_deregister(&sep->miscdev_sep);
3331 misc_deregister(&sep->miscdev_singleton);
3340 * sep_probe - probe a matching PCI device
3342 * @end: pci_device_id
3344 * Attempt to set up and configure a SEP device that has been
3345 * discovered by the PCI layer.
3347 static int __devinit sep_probe(struct pci_dev *pdev,
3348 const struct pci_device_id *ent)
3351 struct sep_device *sep;
3353 pr_debug("SEP pci probe starting\n");
3354 if (sep_dev != NULL) {
3355 dev_warn(&pdev->dev, "only one SEP supported.\n");
3359 /* Enable the device */
3360 error = pci_enable_device(pdev);
3362 dev_warn(&pdev->dev, "error enabling pci device\n");
3366 /* Allocate the sep_device structure for this device */
3367 sep_dev = kzalloc(sizeof(struct sep_device), GFP_ATOMIC);
3368 if (sep_dev == NULL) {
3369 dev_warn(&pdev->dev,
3370 "can't kmalloc the sep_device structure\n");
3372 goto end_function_disable_device;
3376 * We're going to use another variable for actually
3377 * working with the device; this way, if we have
3378 * multiple devices in the future, it would be easier
3379 * to make appropriate changes
3383 sep->pdev = pci_dev_get(pdev);
3385 init_waitqueue_head(&sep->event);
3386 init_waitqueue_head(&sep->event_request_daemon);
3387 spin_lock_init(&sep->snd_rply_lck);
3388 mutex_init(&sep->sep_mutex);
3389 mutex_init(&sep->ioctl_mutex);
3391 dev_dbg(&sep->pdev->dev, "PCI obtained, device being prepared\n");
3392 dev_dbg(&sep->pdev->dev, "revision is %d\n", sep->pdev->revision);
3394 /* Set up our register area */
3395 sep->reg_physical_addr = pci_resource_start(sep->pdev, 0);
3396 if (!sep->reg_physical_addr) {
3397 dev_warn(&sep->pdev->dev, "Error getting register start\n");
3399 goto end_function_free_sep_dev;
3402 sep->reg_physical_end = pci_resource_end(sep->pdev, 0);
3403 if (!sep->reg_physical_end) {
3404 dev_warn(&sep->pdev->dev, "Error getting register end\n");
3406 goto end_function_free_sep_dev;
3409 sep->reg_addr = ioremap_nocache(sep->reg_physical_addr,
3410 (size_t)(sep->reg_physical_end - sep->reg_physical_addr + 1));
3411 if (!sep->reg_addr) {
3412 dev_warn(&sep->pdev->dev, "Error getting register virtual\n");
3414 goto end_function_free_sep_dev;
3417 dev_dbg(&sep->pdev->dev,
3418 "Register area start %llx end %llx virtual %p\n",
3419 (unsigned long long)sep->reg_physical_addr,
3420 (unsigned long long)sep->reg_physical_end,
3423 /* Allocate the shared area */
3424 sep->shared_size = SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES +
3425 SYNCHRONIC_DMA_TABLES_AREA_SIZE_BYTES +
3426 SEP_DRIVER_DATA_POOL_SHARED_AREA_SIZE_IN_BYTES +
3427 SEP_DRIVER_STATIC_AREA_SIZE_IN_BYTES +
3428 SEP_DRIVER_SYSTEM_DATA_MEMORY_SIZE_IN_BYTES;
3430 if (sep_map_and_alloc_shared_area(sep)) {
3432 /* Allocation failed */
3433 goto end_function_error;
3436 sep->rar_size = FAKE_RAR_SIZE;
3437 sep->rar_addr = dma_alloc_coherent(NULL,
3438 sep->rar_size, &sep->rar_bus, GFP_KERNEL);
3439 if (sep->rar_addr == NULL) {
3440 dev_warn(&sep->pdev->dev, "can't allocate mfld rar\n");
3442 goto end_function_deallocate_sep_shared_area;
3445 dev_dbg(&sep->pdev->dev, "rar start is %p, phy is %llx,"
3446 " size is %zx\n", sep->rar_addr,
3447 (unsigned long long)sep->rar_bus,
3450 dev_dbg(&sep->pdev->dev, "about to write IMR and ICR REG_ADDR\n");
3452 /* Clear ICR register */
3453 sep_write_reg(sep, HW_HOST_ICR_REG_ADDR, 0xFFFFFFFF);
3455 /* Set the IMR register - open only GPR 2 */
3456 sep_write_reg(sep, HW_HOST_IMR_REG_ADDR, (~(0x1 << 13)));
3458 /* Read send/receive counters from SEP */
3459 sep->reply_ct = sep_read_reg(sep, HW_HOST_SEP_HOST_GPR2_REG_ADDR);
3460 sep->reply_ct &= 0x3FFFFFFF;
3461 sep->send_ct = sep->reply_ct;
3463 dev_dbg(&sep->pdev->dev, "about to call request_irq\n");
3464 /* Get the interrupt line */
3465 error = request_irq(pdev->irq, sep_inthandler, IRQF_SHARED,
3469 goto end_function_dealloc_rar;
3471 /* The new chip requires ashared area reconfigure */
3472 if (sep->pdev->revision == 4) { /* Only for new chip */
3473 error = sep_reconfig_shared_area(sep);
3475 goto end_function_free_irq;
3477 /* Finally magic up the device nodes */
3478 /* Register driver with the fs */
3479 error = sep_register_driver_with_fs(sep);
3484 end_function_free_irq:
3485 free_irq(pdev->irq, sep);
3487 end_function_dealloc_rar:
3489 dma_free_coherent(&sep->pdev->dev, sep->rar_size,
3490 sep->rar_addr, sep->rar_bus);
3493 end_function_deallocate_sep_shared_area:
3494 /* De-allocate shared area */
3495 sep_unmap_and_free_shared_area(sep);
3498 iounmap(sep->reg_addr);
3500 end_function_free_sep_dev:
3501 pci_dev_put(sep_dev->pdev);
3505 end_function_disable_device:
3506 pci_disable_device(pdev);
3512 static void sep_remove(struct pci_dev *pdev)
3514 struct sep_device *sep = sep_dev;
3516 /* Unregister from fs */
3517 misc_deregister(&sep->miscdev_sep);
3518 misc_deregister(&sep->miscdev_singleton);
3519 misc_deregister(&sep->miscdev_daemon);
3522 free_irq(sep->pdev->irq, sep);
3524 /* Free the shared area */
3525 sep_unmap_and_free_shared_area(sep_dev);
3526 iounmap((void *) sep_dev->reg_addr);
3529 static DEFINE_PCI_DEVICE_TABLE(sep_pci_id_tbl) = {
3530 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MFLD_PCI_DEVICE_ID)},
3534 MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);
3536 /* Field for registering driver to PCI device */
3537 static struct pci_driver sep_pci_driver = {
3538 .name = "sep_sec_driver",
3539 .id_table = sep_pci_id_tbl,
3541 .remove = sep_remove
3546 * sep_init - init function
3548 * Module load time. Register the PCI device driver.
3550 static int __init sep_init(void)
3552 return pci_register_driver(&sep_pci_driver);
3557 * sep_exit - called to unload driver
3559 * Drop the misc devices then remove and unmap the various resources
3560 * that are not released by the driver remove method.
3562 static void __exit sep_exit(void)
3564 pci_unregister_driver(&sep_pci_driver);
3568 module_init(sep_init);
3569 module_exit(sep_exit);
3571 MODULE_LICENSE("GPL");