No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / pci / n8 / common / n8_sksInit.c
blob69967b67c1e41b8d3db35b65cae3706ea331b6bb
1 /*-
2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
3 * All rights reserved.
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software. Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 static char const n8_id[] = "$Id: n8_sksInit.c,v 1.1 2008/10/30 12:02:14 darran Exp $";
37 /*****************************************************************************/
38 /** @file n8_sksInit.c
39 * @brief Handle SKS driver initialization.
41 * Defines functions that are used by the driver to initialize SKS for use.
43 *****************************************************************************/
45 /*****************************************************************************
46 * Revision history:
47 * 01/16/04 jpw Add N8_NO_64_BURST to prevent 64bit bursts to 32 bit regs
48 * during sequential slave io accesses.
49 * 10/25/02 brr Clean up function prototypes & include files.
50 * 06/07/02 brr Added function SKS_Remove.
51 * 05/02/02 brr Removed all references to queue structures.
52 * 04/29/02 spm Simplified n8_SKSInitialize. This fixed Bug #733: BSDi
53 * crash due to n8_daemon_sys_init program.
54 * 04/06/02 brr Do not define N8DEBUG.
55 * 04/05/02 bac Fixed typo in user-visible output message.
56 * 04/02/02 spm Changed proto for n8_SKSInitialize to support SKS
57 * re-architecture. This will allow the SKS allocation unit
58 * mapping to be accomplished by a normal (forward) ioctl,
59 * thus eliminating the need for a nasty blocking (reverse)
60 * ioctl.
61 * 02/22/02 spm Converted printk's to DBG's. Added include of n8_OS_intf.h
62 * 02/13/02 brr Removed reliance on QMgr.
63 * 01/21/02 spm Original version.
64 ****************************************************************************/
65 /** @defgroup subsystem_name Subsystem Title (not used for a header file)
67 #include "helper.h"
68 #include "n8_driver_main.h"
69 #include "n8_driver_parms.h"
70 #include "irq.h"
71 #include "displayRegs.h"
72 #include "n8_malloc_common.h"
73 #include "nsp_ioctl.h"
74 #include "n8_sks.h"
75 #include "n8_daemon_common.h"
76 #include "n8_daemon_sks.h"
77 #include "n8_OS_intf.h"
78 #include "n8_sksInit.h"
80 /* Values for Secure Key Storage control register */
81 #define PK_SKS_Go_Busy 0x80000000
82 #define PK_SKS_PROM_Error 0x40000000
83 #define PK_SKS_Access_Error 0x20000000
84 #define PK_SKS_Operation_Mask 0x00003000
85 #define PK_SKS_From_PROM_Mask 0x00002000
86 #define PK_SKS_Cache_Only_Mask 0x00001000
87 #define PK_SKS_Address_Mask 0x00000fff
88 #define PK_SKS_Op_Address_Mask 0x00003fff
89 #define PK_SKS_Any_Error_Mask 0x60000000
90 #define PK_SKS_Max_Length 0x00001000
92 #define PK_Cmd_SKS_Offset_Mask 0x00000fff
96 extern int NSPcount_g;
97 extern NspInstance_t NSPDeviceTable_g [];
99 int SKS_Prom2Cache(volatile NSP2000REGS_t *nsp);
100 /*****************************************************************************
101 * SKS_Prom2Cache
102 *****************************************************************************/
103 /** @ingroup NSP2000 Driver
104 * @brief This routine copies the entire 4K x 32-bit content of SKS PROM to
105 * the SKS cache
107 * TODO: What happens when there is no SKS PROM? Well, currently there is
108 * no way for software to detect this. If software could detect this
109 * situation, 1. the prom-to-cache copy done in this routine
110 * should be disabled and 2. the prom write performed by the
111 * SKS management application should be disabled, so that ONLY the
112 * cache write is performed.
114 * @param void
116 * @par Externals:
117 * None
119 * @return
120 * N8_STATUS_OK on success
122 * @par Errors:
123 * N8_UNEXPECTED_ERROR if there was an error writing to the SKS cache
125 * @par Locks:
126 * Requires that the caller (QMgrSetupQueue if not in driver)
127 * have the system semaphore.
129 * @par Assumptions: See Locks.
130 *****************************************************************************/
131 int SKS_Prom2Cache(volatile NSP2000REGS_t *nsp)
133 int i = 0;
135 for( i = 0; i < N8_SKS_PROM_SIZE; i++ )
138 /* Cannot access data register while PK_SKS_Go_Busy is on. */
139 while( ( nsp->pkh_secure_key_storage_control & PK_SKS_Go_Busy ) )
141 /* Wait until bit clears. */
144 /* Enable the SKS write. */
145 nsp->pkh_secure_key_storage_control =
146 (PK_SKS_Go_Busy | PK_SKS_From_PROM_Mask | ( i & PK_Cmd_SKS_Offset_Mask ));
147 N8_NO_64_BURST;
148 /* Check for errors. */
149 if ( ( nsp->pkh_secure_key_storage_control & PK_SKS_Access_Error ) |
150 ( nsp->pkh_secure_key_storage_control & PK_SKS_PROM_Error ) )
153 DBG(("NSP2000: Error writing to SKS PROM. Control Register = %08x",
154 nsp->pkh_secure_key_storage_control ));
155 return 0;
160 return 1;
162 } /* SKS_Prom2Cache */
164 /*****************************************************************************
165 * SKS_Init
166 *****************************************************************************/
167 /** @ingroup NSP2000 Driver
168 * @brief This routine performs the initialization required for the SKS.
170 * @param void
172 * @par Externals:
173 * None
175 * @return
176 * N8_STATUS_OK on success
178 * @par Errors:
179 * N8_UNEXPECTED_ERROR if there was an error writing to the SKS cache
181 * @par Locks:
183 * @par Assumptions: See Locks.
184 *****************************************************************************/
185 int SKS_Init(NspInstance_t *NSPinstance_p)
188 /* Handled in nsp.c */
189 N8_AtomicLockInit(NSPinstance_p->SKSSem);
190 memset(&NSPinstance_p->SKS_map, 0, N8_DEF_SKS_MEMSIZE);
191 NSPinstance_p->SKS_size = N8_DEF_SKS_MEMSIZE;
193 return(SKS_Prom2Cache((volatile NSP2000REGS_t *)NSPinstance_p->NSPregs_p));
196 /*****************************************************************************
197 * n8_SKSInitialize
198 *****************************************************************************/
199 /** @ingroup n8_sks
200 * @brief Initialize an instance of the SKS management interface API.
202 * This function is called once by the Queue Manager at startup. The purpose is
203 * to read in the persistent key handle files and set up the shared SKS
204 * management mapping structure on a per SKS basis.
206 * @par Externals:
207 * None
209 * @return
210 * void
212 *****************************************************************************/
213 void n8_SKSInitialize(n8_DaemonMsg_t *pParms)
216 int i;
217 NspInstance_t *NSPinstance_p;
220 DBG(( "n8_SKSInitialize entering...\n"));
222 /* the target SKS to initialize is the unit passed by the user
223 * initialization app
225 i = pParms->unit;
230 /* the SKS management structure is in shared memory accessible via each
231 * queue pointer. there is also an initialized flag which is shared
232 * across all processes.
235 /* do not call N8_preamble. it is unnecessary since the current function
236 * is called by the Queue Manager at startup. Also calling it results in
237 * deadlock as the preamble attempts to grab a system semaphore held by
238 * the QMgr. */
240 DBG(( "N8_sks: user app requested init of SKS unit %d\n",i));
243 if ((i < 0) || (i >= NSPcount_g))
245 DBG(("Failed to get control structure: \n"));
246 return;
249 /* assign the right control struct for the target HW */
250 NSPinstance_p = &NSPDeviceTable_g[i];
252 /* Entering critical section. */
253 N8_AtomicLock(NSPinstance_p->SKSSem);
255 /* copy descriptor table from user space */
256 N8_FROM_USER(NSPinstance_p->SKS_map,
257 pParms->SKS_Descriptor_p,
258 sizeof(N8_Buffer_t)*
259 SKS_ALLOC_UNITS_PER_PROM);
261 /* Count the first consecutive number of units
262 * allocated.
264 #if N8DEBUG
266 int count=0;
267 int m;
269 for (m=0; m<SKS_ALLOC_UNITS_PER_PROM; m++)
271 if (NSPinstance_p->SKS_map[m])
273 count++;
276 DBG(("n8_SKSInitialize: first %d units allocated in SKS_Memory_p[]\n", count));
278 #endif
280 N8_AtomicUnlock(NSPinstance_p->SKSSem);
282 DBG(( "released semaphore...\n"));
284 } while (FALSE);
286 DBG(( "n8_SKSInitialize leaving...\n"));
288 return;
290 } /* n8_SKSInitialize */
293 /*****************************************************************************
294 * SKS_Remove
295 *****************************************************************************/
296 /** @ingroup NSP2000 Driver
297 * @brief This routine deletes the resources allocated for the SKS.
299 * @param void
301 * @par Externals:
302 * None
304 * @return
305 * N8_STATUS_OK
307 * @par Errors:
309 * @par Locks:
311 * @par Assumptions: See Locks.
312 *****************************************************************************/
313 int SKS_Remove(NspInstance_t *NSPinstance_p)
316 N8_AtomicLockDel(NSPinstance_p->SKSSem);
317 return N8_STATUS_OK;