talitos.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*
  2. * crypto/ocf/talitos/talitos.c
  3. *
  4. * An OCF-Linux module that uses Freescale's SEC to do the crypto.
  5. * Based on crypto/ocf/hifn and crypto/ocf/safe OCF drivers
  6. *
  7. * Copyright (c) 2006 Freescale Semiconductor, Inc.
  8. *
  9. * This code written by Kim A. B. Phillips <kim.phillips@freescale.com>
  10. * some code copied from files with the following:
  11. * Copyright (C) 2004-2007 David McCullough <david_mccullough@mcafee.com>
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. The name of the author may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  26. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  30. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * ---------------------------------------------------------------------------
  37. *
  38. * NOTES:
  39. *
  40. * The Freescale SEC (also known as 'talitos') resides on the
  41. * internal bus, and runs asynchronous to the processor core. It has
  42. * a wide gamut of cryptographic acceleration features, including single-
  43. * pass IPsec (also known as algorithm chaining). To properly utilize
  44. * all of the SEC's performance enhancing features, further reworking
  45. * of higher level code (framework, applications) will be necessary.
  46. *
  47. * The following table shows which SEC version is present in which devices:
  48. *
  49. * Devices SEC version
  50. *
  51. * 8272, 8248 SEC 1.0
  52. * 885, 875 SEC 1.2
  53. * 8555E, 8541E SEC 2.0
  54. * 8349E SEC 2.01
  55. * 8548E SEC 2.1
  56. *
  57. * The following table shows the features offered by each SEC version:
  58. *
  59. * Max. chan-
  60. * version Bus I/F Clock nels DEU AESU AFEU MDEU PKEU RNG KEU
  61. *
  62. * SEC 1.0 internal 64b 100MHz 4 1 1 1 1 1 1 0
  63. * SEC 1.2 internal 32b 66MHz 1 1 1 0 1 0 0 0
  64. * SEC 2.0 internal 64b 166MHz 4 1 1 1 1 1 1 0
  65. * SEC 2.01 internal 64b 166MHz 4 1 1 1 1 1 1 0
  66. * SEC 2.1 internal 64b 333MHz 4 1 1 1 1 1 1 1
  67. *
  68. * Each execution unit in the SEC has two modes of execution; channel and
  69. * slave/debug. This driver employs the channel infrastructure in the
  70. * device for convenience. Only the RNG is directly accessed due to the
  71. * convenience of its random fifo pool. The relationship between the
  72. * channels and execution units is depicted in the following diagram:
  73. *
  74. * ------- ------------
  75. * ---| ch0 |---| |
  76. * ------- | |
  77. * | |------+-------+-------+-------+------------
  78. * ------- | | | | | | |
  79. * ---| ch1 |---| | | | | | |
  80. * ------- | | ------ ------ ------ ------ ------
  81. * |controller| |DEU | |AESU| |MDEU| |PKEU| ... |RNG |
  82. * ------- | | ------ ------ ------ ------ ------
  83. * ---| ch2 |---| | | | | | |
  84. * ------- | | | | | | |
  85. * | |------+-------+-------+-------+------------
  86. * ------- | |
  87. * ---| ch3 |---| |
  88. * ------- ------------
  89. *
  90. * Channel ch0 may drive an aes operation to the aes unit (AESU),
  91. * and, at the same time, ch1 may drive a message digest operation
  92. * to the mdeu. Each channel has an input descriptor FIFO, and the
  93. * FIFO can contain, e.g. on the 8541E, up to 24 entries, before a
  94. * a buffer overrun error is triggered. The controller is responsible
  95. * for fetching the data from descriptor pointers, and passing the
  96. * data to the appropriate EUs. The controller also writes the
  97. * cryptographic operation's result to memory. The SEC notifies
  98. * completion by triggering an interrupt and/or setting the 1st byte
  99. * of the hdr field to 0xff.
  100. *
  101. * TODO:
  102. * o support more algorithms
  103. * o support more versions of the SEC
  104. * o add support for linux 2.4
  105. * o scatter-gather (sg) support
  106. * o add support for public key ops (PKEU)
  107. * o add statistics
  108. */
  109. #include <linux/version.h>
  110. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
  111. #include <linux/config.h>
  112. #endif
  113. #include <linux/module.h>
  114. #include <linux/init.h>
  115. #include <linux/interrupt.h>
  116. #include <linux/spinlock.h>
  117. #include <linux/random.h>
  118. #include <linux/skbuff.h>
  119. #include <asm/scatterlist.h>
  120. #include <linux/dma-mapping.h> /* dma_map_single() */
  121. #include <linux/moduleparam.h>
  122. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
  123. #include <linux/platform_device.h>
  124. #endif
  125. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  126. #include <linux/of_platform.h>
  127. #endif
  128. #include <cryptodev.h>
  129. #include <uio.h>
  130. #define DRV_NAME "talitos"
  131. #include "talitos_dev.h"
  132. #include "talitos_soft.h"
  133. #define read_random(p,l) get_random_bytes(p,l)
  134. const char talitos_driver_name[] = "Talitos OCF";
  135. const char talitos_driver_version[] = "0.2";
  136. static int talitos_newsession(device_t dev, u_int32_t *sidp,
  137. struct cryptoini *cri);
  138. static int talitos_freesession(device_t dev, u_int64_t tid);
  139. static int talitos_process(device_t dev, struct cryptop *crp, int hint);
  140. static void dump_talitos_status(struct talitos_softc *sc);
  141. static int talitos_submit(struct talitos_softc *sc, struct talitos_desc *td,
  142. int chsel);
  143. static void talitos_doneprocessing(struct talitos_softc *sc);
  144. static void talitos_init_device(struct talitos_softc *sc);
  145. static void talitos_reset_device_master(struct talitos_softc *sc);
  146. static void talitos_reset_device(struct talitos_softc *sc);
  147. static void talitos_errorprocessing(struct talitos_softc *sc);
  148. #ifdef CONFIG_PPC_MERGE
  149. static int talitos_probe(struct of_device *ofdev, const struct of_device_id *match);
  150. static int talitos_remove(struct of_device *ofdev);
  151. #else
  152. static int talitos_probe(struct platform_device *pdev);
  153. static int talitos_remove(struct platform_device *pdev);
  154. #endif
  155. #ifdef CONFIG_OCF_RANDOMHARVEST
  156. static int talitos_read_random(void *arg, u_int32_t *buf, int maxwords);
  157. static void talitos_rng_init(struct talitos_softc *sc);
  158. #endif
  159. static device_method_t talitos_methods = {
  160. /* crypto device methods */
  161. DEVMETHOD(cryptodev_newsession, talitos_newsession),
  162. DEVMETHOD(cryptodev_freesession,talitos_freesession),
  163. DEVMETHOD(cryptodev_process, talitos_process),
  164. };
  165. #define debug talitos_debug
  166. int talitos_debug = 0;
  167. module_param(talitos_debug, int, 0644);
  168. MODULE_PARM_DESC(talitos_debug, "Enable debug");
  169. static inline void talitos_write(volatile unsigned *addr, u32 val)
  170. {
  171. out_be32(addr, val);
  172. }
  173. static inline u32 talitos_read(volatile unsigned *addr)
  174. {
  175. u32 val;
  176. val = in_be32(addr);
  177. return val;
  178. }
  179. static void dump_talitos_status(struct talitos_softc *sc)
  180. {
  181. unsigned int v, v_hi, i, *ptr;
  182. v = talitos_read(sc->sc_base_addr + TALITOS_MCR);
  183. v_hi = talitos_read(sc->sc_base_addr + TALITOS_MCR_HI);
  184. printk(KERN_INFO "%s: MCR 0x%08x_%08x\n",
  185. device_get_nameunit(sc->sc_cdev), v, v_hi);
  186. v = talitos_read(sc->sc_base_addr + TALITOS_IMR);
  187. v_hi = talitos_read(sc->sc_base_addr + TALITOS_IMR_HI);
  188. printk(KERN_INFO "%s: IMR 0x%08x_%08x\n",
  189. device_get_nameunit(sc->sc_cdev), v, v_hi);
  190. v = talitos_read(sc->sc_base_addr + TALITOS_ISR);
  191. v_hi = talitos_read(sc->sc_base_addr + TALITOS_ISR_HI);
  192. printk(KERN_INFO "%s: ISR 0x%08x_%08x\n",
  193. device_get_nameunit(sc->sc_cdev), v, v_hi);
  194. for (i = 0; i < sc->sc_num_channels; i++) {
  195. v = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  196. TALITOS_CH_CDPR);
  197. v_hi = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  198. TALITOS_CH_CDPR_HI);
  199. printk(KERN_INFO "%s: CDPR ch%d 0x%08x_%08x\n",
  200. device_get_nameunit(sc->sc_cdev), i, v, v_hi);
  201. }
  202. for (i = 0; i < sc->sc_num_channels; i++) {
  203. v = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  204. TALITOS_CH_CCPSR);
  205. v_hi = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  206. TALITOS_CH_CCPSR_HI);
  207. printk(KERN_INFO "%s: CCPSR ch%d 0x%08x_%08x\n",
  208. device_get_nameunit(sc->sc_cdev), i, v, v_hi);
  209. }
  210. ptr = sc->sc_base_addr + TALITOS_CH_DESCBUF;
  211. for (i = 0; i < 16; i++) {
  212. v = talitos_read(ptr++); v_hi = talitos_read(ptr++);
  213. printk(KERN_INFO "%s: DESCBUF ch0 0x%08x_%08x (tdp%02d)\n",
  214. device_get_nameunit(sc->sc_cdev), v, v_hi, i);
  215. }
  216. return;
  217. }
  218. #ifdef CONFIG_OCF_RANDOMHARVEST
  219. /*
  220. * pull random numbers off the RNG FIFO, not exceeding amount available
  221. */
  222. static int
  223. talitos_read_random(void *arg, u_int32_t *buf, int maxwords)
  224. {
  225. struct talitos_softc *sc = (struct talitos_softc *) arg;
  226. int rc;
  227. u_int32_t v;
  228. DPRINTF("%s()\n", __FUNCTION__);
  229. /* check for things like FIFO underflow */
  230. v = talitos_read(sc->sc_base_addr + TALITOS_RNGISR_HI);
  231. if (unlikely(v)) {
  232. printk(KERN_ERR "%s: RNGISR_HI error %08x\n",
  233. device_get_nameunit(sc->sc_cdev), v);
  234. return 0;
  235. }
  236. /*
  237. * OFL is number of available 64-bit words,
  238. * shift and convert to a 32-bit word count
  239. */
  240. v = talitos_read(sc->sc_base_addr + TALITOS_RNGSR_HI);
  241. v = (v & TALITOS_RNGSR_HI_OFL) >> (16 - 1);
  242. if (maxwords > v)
  243. maxwords = v;
  244. for (rc = 0; rc < maxwords; rc++) {
  245. buf[rc] = talitos_read(sc->sc_base_addr +
  246. TALITOS_RNG_FIFO + rc*sizeof(u_int32_t));
  247. }
  248. if (maxwords & 1) {
  249. /*
  250. * RNG will complain with an AE in the RNGISR
  251. * if we don't complete the pairs of 32-bit reads
  252. * to its 64-bit register based FIFO
  253. */
  254. v = talitos_read(sc->sc_base_addr +
  255. TALITOS_RNG_FIFO + rc*sizeof(u_int32_t));
  256. }
  257. return rc;
  258. }
  259. static void
  260. talitos_rng_init(struct talitos_softc *sc)
  261. {
  262. u_int32_t v;
  263. DPRINTF("%s()\n", __FUNCTION__);
  264. /* reset RNG EU */
  265. v = talitos_read(sc->sc_base_addr + TALITOS_RNGRCR_HI);
  266. v |= TALITOS_RNGRCR_HI_SR;
  267. talitos_write(sc->sc_base_addr + TALITOS_RNGRCR_HI, v);
  268. while ((talitos_read(sc->sc_base_addr + TALITOS_RNGSR_HI)
  269. & TALITOS_RNGSR_HI_RD) == 0)
  270. cpu_relax();
  271. /*
  272. * we tell the RNG to start filling the RNG FIFO
  273. * by writing the RNGDSR
  274. */
  275. v = talitos_read(sc->sc_base_addr + TALITOS_RNGDSR_HI);
  276. talitos_write(sc->sc_base_addr + TALITOS_RNGDSR_HI, v);
  277. /*
  278. * 64 bits of data will be pushed onto the FIFO every
  279. * 256 SEC cycles until the FIFO is full. The RNG then
  280. * attempts to keep the FIFO full.
  281. */
  282. v = talitos_read(sc->sc_base_addr + TALITOS_RNGISR_HI);
  283. if (v) {
  284. printk(KERN_ERR "%s: RNGISR_HI error %08x\n",
  285. device_get_nameunit(sc->sc_cdev), v);
  286. return;
  287. }
  288. /*
  289. * n.b. we need to add a FIPS test here - if the RNG is going
  290. * to fail, it's going to fail at reset time
  291. */
  292. return;
  293. }
  294. #endif /* CONFIG_OCF_RANDOMHARVEST */
  295. /*
  296. * Generate a new software session.
  297. */
  298. static int
  299. talitos_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
  300. {
  301. struct cryptoini *c, *encini = NULL, *macini = NULL;
  302. struct talitos_softc *sc = device_get_softc(dev);
  303. struct talitos_session *ses = NULL;
  304. int sesn;
  305. DPRINTF("%s()\n", __FUNCTION__);
  306. if (sidp == NULL || cri == NULL || sc == NULL) {
  307. DPRINTF("%s,%d - EINVAL\n", __FILE__, __LINE__);
  308. return EINVAL;
  309. }
  310. for (c = cri; c != NULL; c = c->cri_next) {
  311. if (c->cri_alg == CRYPTO_MD5 ||
  312. c->cri_alg == CRYPTO_MD5_HMAC ||
  313. c->cri_alg == CRYPTO_SHA1 ||
  314. c->cri_alg == CRYPTO_SHA1_HMAC ||
  315. c->cri_alg == CRYPTO_NULL_HMAC) {
  316. if (macini)
  317. return EINVAL;
  318. macini = c;
  319. } else if (c->cri_alg == CRYPTO_DES_CBC ||
  320. c->cri_alg == CRYPTO_3DES_CBC ||
  321. c->cri_alg == CRYPTO_AES_CBC ||
  322. c->cri_alg == CRYPTO_NULL_CBC) {
  323. if (encini)
  324. return EINVAL;
  325. encini = c;
  326. } else {
  327. DPRINTF("UNKNOWN c->cri_alg %d\n", encini->cri_alg);
  328. return EINVAL;
  329. }
  330. }
  331. if (encini == NULL && macini == NULL)
  332. return EINVAL;
  333. if (encini) {
  334. /* validate key length */
  335. switch (encini->cri_alg) {
  336. case CRYPTO_DES_CBC:
  337. if (encini->cri_klen != 64)
  338. return EINVAL;
  339. break;
  340. case CRYPTO_3DES_CBC:
  341. if (encini->cri_klen != 192) {
  342. return EINVAL;
  343. }
  344. break;
  345. case CRYPTO_AES_CBC:
  346. if (encini->cri_klen != 128 &&
  347. encini->cri_klen != 192 &&
  348. encini->cri_klen != 256)
  349. return EINVAL;
  350. break;
  351. default:
  352. DPRINTF("UNKNOWN encini->cri_alg %d\n",
  353. encini->cri_alg);
  354. return EINVAL;
  355. }
  356. }
  357. if (sc->sc_sessions == NULL) {
  358. ses = sc->sc_sessions = (struct talitos_session *)
  359. kmalloc(sizeof(struct talitos_session), SLAB_ATOMIC);
  360. if (ses == NULL)
  361. return ENOMEM;
  362. memset(ses, 0, sizeof(struct talitos_session));
  363. sesn = 0;
  364. sc->sc_nsessions = 1;
  365. } else {
  366. for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
  367. if (sc->sc_sessions[sesn].ses_used == 0) {
  368. ses = &sc->sc_sessions[sesn];
  369. break;
  370. }
  371. }
  372. if (ses == NULL) {
  373. /* allocating session */
  374. sesn = sc->sc_nsessions;
  375. ses = (struct talitos_session *) kmalloc(
  376. (sesn + 1) * sizeof(struct talitos_session),
  377. SLAB_ATOMIC);
  378. if (ses == NULL)
  379. return ENOMEM;
  380. memset(ses, 0,
  381. (sesn + 1) * sizeof(struct talitos_session));
  382. memcpy(ses, sc->sc_sessions,
  383. sesn * sizeof(struct talitos_session));
  384. memset(sc->sc_sessions, 0,
  385. sesn * sizeof(struct talitos_session));
  386. kfree(sc->sc_sessions);
  387. sc->sc_sessions = ses;
  388. ses = &sc->sc_sessions[sesn];
  389. sc->sc_nsessions++;
  390. }
  391. }
  392. ses->ses_used = 1;
  393. if (encini) {
  394. ses->ses_klen = (encini->cri_klen + 7) / 8;
  395. memcpy(ses->ses_key, encini->cri_key, ses->ses_klen);
  396. if (macini) {
  397. /* doing hash on top of cipher */
  398. ses->ses_hmac_len = (macini->cri_klen + 7) / 8;
  399. memcpy(ses->ses_hmac, macini->cri_key,
  400. ses->ses_hmac_len);
  401. }
  402. } else if (macini) {
  403. /* doing hash */
  404. ses->ses_klen = (macini->cri_klen + 7) / 8;
  405. memcpy(ses->ses_key, macini->cri_key, ses->ses_klen);
  406. }
  407. /* back compat way of determining MSC result len */
  408. if (macini) {
  409. ses->ses_mlen = macini->cri_mlen;
  410. if (ses->ses_mlen == 0) {
  411. if (macini->cri_alg == CRYPTO_MD5_HMAC)
  412. ses->ses_mlen = MD5_HASH_LEN;
  413. else
  414. ses->ses_mlen = SHA1_HASH_LEN;
  415. }
  416. }
  417. /* really should make up a template td here,
  418. * and only fill things like i/o and direction in process() */
  419. /* assign session ID */
  420. *sidp = TALITOS_SID(sc->sc_num, sesn);
  421. return 0;
  422. }
  423. /*
  424. * Deallocate a session.
  425. */
  426. static int
  427. talitos_freesession(device_t dev, u_int64_t tid)
  428. {
  429. struct talitos_softc *sc = device_get_softc(dev);
  430. int session, ret;
  431. u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
  432. if (sc == NULL)
  433. return EINVAL;
  434. session = TALITOS_SESSION(sid);
  435. if (session < sc->sc_nsessions) {
  436. memset(&sc->sc_sessions[session], 0,
  437. sizeof(sc->sc_sessions[session]));
  438. ret = 0;
  439. } else
  440. ret = EINVAL;
  441. return ret;
  442. }
  443. /*
  444. * launch device processing - it will come back with done notification
  445. * in the form of an interrupt and/or HDR_DONE_BITS in header
  446. */
  447. static int
  448. talitos_submit(
  449. struct talitos_softc *sc,
  450. struct talitos_desc *td,
  451. int chsel)
  452. {
  453. u_int32_t v;
  454. v = dma_map_single(NULL, td, sizeof(*td), DMA_TO_DEVICE);
  455. talitos_write(sc->sc_base_addr +
  456. chsel*TALITOS_CH_OFFSET + TALITOS_CH_FF, 0);
  457. talitos_write(sc->sc_base_addr +
  458. chsel*TALITOS_CH_OFFSET + TALITOS_CH_FF_HI, v);
  459. return 0;
  460. }
  461. static int
  462. talitos_process(device_t dev, struct cryptop *crp, int hint)
  463. {
  464. int i, err = 0, ivsize;
  465. struct talitos_softc *sc = device_get_softc(dev);
  466. struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
  467. caddr_t iv;
  468. struct talitos_session *ses;
  469. struct talitos_desc *td;
  470. unsigned long flags;
  471. /* descriptor mappings */
  472. int hmac_key, hmac_data, cipher_iv, cipher_key,
  473. in_fifo, out_fifo, cipher_iv_out;
  474. static int chsel = -1;
  475. u_int32_t rand_iv[4];
  476. DPRINTF("%s()\n", __FUNCTION__);
  477. if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
  478. return EINVAL;
  479. }
  480. crp->crp_etype = 0;
  481. if (TALITOS_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
  482. return EINVAL;
  483. }
  484. ses = &sc->sc_sessions[TALITOS_SESSION(crp->crp_sid)];
  485. /* enter the channel scheduler */
  486. spin_lock_irqsave(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  487. /* reuse channel that already had/has requests for the required EU */
  488. for (i = 0; i < sc->sc_num_channels; i++) {
  489. if (sc->sc_chnlastalg[i] == crp->crp_desc->crd_alg)
  490. break;
  491. }
  492. if (i == sc->sc_num_channels) {
  493. /*
  494. * haven't seen this algo the last sc_num_channels or more
  495. * use round robin in this case
  496. * nb: sc->sc_num_channels must be power of 2
  497. */
  498. chsel = (chsel + 1) & (sc->sc_num_channels - 1);
  499. } else {
  500. /*
  501. * matches channel with same target execution unit;
  502. * use same channel in this case
  503. */
  504. chsel = i;
  505. }
  506. sc->sc_chnlastalg[chsel] = crp->crp_desc->crd_alg;
  507. /* release the channel scheduler lock */
  508. spin_unlock_irqrestore(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  509. /* acquire the selected channel fifo lock */
  510. spin_lock_irqsave(&sc->sc_chnfifolock[chsel], flags);
  511. /* find and reserve next available descriptor-cryptop pair */
  512. for (i = 0; i < sc->sc_chfifo_len; i++) {
  513. if (sc->sc_chnfifo[chsel][i].cf_desc.hdr == 0) {
  514. /*
  515. * ensure correct descriptor formation by
  516. * avoiding inadvertently setting "optional" entries
  517. * e.g. not using "optional" dptr2 for MD/HMAC descs
  518. */
  519. memset(&sc->sc_chnfifo[chsel][i].cf_desc,
  520. 0, sizeof(*td));
  521. /* reserve it with done notification request bit */
  522. sc->sc_chnfifo[chsel][i].cf_desc.hdr |=
  523. TALITOS_DONE_NOTIFY;
  524. break;
  525. }
  526. }
  527. spin_unlock_irqrestore(&sc->sc_chnfifolock[chsel], flags);
  528. if (i == sc->sc_chfifo_len) {
  529. /* fifo full */
  530. err = ERESTART;
  531. goto errout;
  532. }
  533. td = &sc->sc_chnfifo[chsel][i].cf_desc;
  534. sc->sc_chnfifo[chsel][i].cf_crp = crp;
  535. crd1 = crp->crp_desc;
  536. if (crd1 == NULL) {
  537. err = EINVAL;
  538. goto errout;
  539. }
  540. crd2 = crd1->crd_next;
  541. /* prevent compiler warning */
  542. hmac_key = 0;
  543. hmac_data = 0;
  544. if (crd2 == NULL) {
  545. td->hdr |= TD_TYPE_COMMON_NONSNOOP_NO_AFEU;
  546. /* assign descriptor dword ptr mappings for this desc. type */
  547. cipher_iv = 1;
  548. cipher_key = 2;
  549. in_fifo = 3;
  550. cipher_iv_out = 5;
  551. if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
  552. crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  553. crd1->crd_alg == CRYPTO_SHA1 ||
  554. crd1->crd_alg == CRYPTO_MD5) {
  555. out_fifo = 5;
  556. maccrd = crd1;
  557. enccrd = NULL;
  558. } else if (crd1->crd_alg == CRYPTO_DES_CBC ||
  559. crd1->crd_alg == CRYPTO_3DES_CBC ||
  560. crd1->crd_alg == CRYPTO_AES_CBC ||
  561. crd1->crd_alg == CRYPTO_ARC4) {
  562. out_fifo = 4;
  563. maccrd = NULL;
  564. enccrd = crd1;
  565. } else {
  566. DPRINTF("UNKNOWN crd1->crd_alg %d\n", crd1->crd_alg);
  567. err = EINVAL;
  568. goto errout;
  569. }
  570. } else {
  571. if (sc->sc_desc_types & TALITOS_HAS_DT_IPSEC_ESP) {
  572. td->hdr |= TD_TYPE_IPSEC_ESP;
  573. } else {
  574. DPRINTF("unimplemented: multiple descriptor ipsec\n");
  575. err = EINVAL;
  576. goto errout;
  577. }
  578. /* assign descriptor dword ptr mappings for this desc. type */
  579. hmac_key = 0;
  580. hmac_data = 1;
  581. cipher_iv = 2;
  582. cipher_key = 3;
  583. in_fifo = 4;
  584. out_fifo = 5;
  585. cipher_iv_out = 6;
  586. if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
  587. crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  588. crd1->crd_alg == CRYPTO_MD5 ||
  589. crd1->crd_alg == CRYPTO_SHA1) &&
  590. (crd2->crd_alg == CRYPTO_DES_CBC ||
  591. crd2->crd_alg == CRYPTO_3DES_CBC ||
  592. crd2->crd_alg == CRYPTO_AES_CBC ||
  593. crd2->crd_alg == CRYPTO_ARC4) &&
  594. ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
  595. maccrd = crd1;
  596. enccrd = crd2;
  597. } else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
  598. crd1->crd_alg == CRYPTO_ARC4 ||
  599. crd1->crd_alg == CRYPTO_3DES_CBC ||
  600. crd1->crd_alg == CRYPTO_AES_CBC) &&
  601. (crd2->crd_alg == CRYPTO_MD5_HMAC ||
  602. crd2->crd_alg == CRYPTO_SHA1_HMAC ||
  603. crd2->crd_alg == CRYPTO_MD5 ||
  604. crd2->crd_alg == CRYPTO_SHA1) &&
  605. (crd1->crd_flags & CRD_F_ENCRYPT)) {
  606. enccrd = crd1;
  607. maccrd = crd2;
  608. } else {
  609. /* We cannot order the SEC as requested */
  610. printk("%s: cannot do the order\n",
  611. device_get_nameunit(sc->sc_cdev));
  612. err = EINVAL;
  613. goto errout;
  614. }
  615. }
  616. /* assign in_fifo and out_fifo based on input/output struct type */
  617. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  618. /* using SKB buffers */
  619. struct sk_buff *skb = (struct sk_buff *)crp->crp_buf;
  620. if (skb_shinfo(skb)->nr_frags) {
  621. printk("%s: skb frags unimplemented\n",
  622. device_get_nameunit(sc->sc_cdev));
  623. err = EINVAL;
  624. goto errout;
  625. }
  626. td->ptr[in_fifo].ptr = dma_map_single(NULL, skb->data,
  627. skb->len, DMA_TO_DEVICE);
  628. td->ptr[in_fifo].len = skb->len;
  629. td->ptr[out_fifo].ptr = dma_map_single(NULL, skb->data,
  630. skb->len, DMA_TO_DEVICE);
  631. td->ptr[out_fifo].len = skb->len;
  632. td->ptr[hmac_data].ptr = dma_map_single(NULL, skb->data,
  633. skb->len, DMA_TO_DEVICE);
  634. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  635. /* using IOV buffers */
  636. struct uio *uiop = (struct uio *)crp->crp_buf;
  637. if (uiop->uio_iovcnt > 1) {
  638. printk("%s: iov frags unimplemented\n",
  639. device_get_nameunit(sc->sc_cdev));
  640. err = EINVAL;
  641. goto errout;
  642. }
  643. td->ptr[in_fifo].ptr = dma_map_single(NULL,
  644. uiop->uio_iov->iov_base, crp->crp_ilen, DMA_TO_DEVICE);
  645. td->ptr[in_fifo].len = crp->crp_ilen;
  646. /* crp_olen is never set; always use crp_ilen */
  647. td->ptr[out_fifo].ptr = dma_map_single(NULL,
  648. uiop->uio_iov->iov_base,
  649. crp->crp_ilen, DMA_TO_DEVICE);
  650. td->ptr[out_fifo].len = crp->crp_ilen;
  651. } else {
  652. /* using contig buffers */
  653. td->ptr[in_fifo].ptr = dma_map_single(NULL,
  654. crp->crp_buf, crp->crp_ilen, DMA_TO_DEVICE);
  655. td->ptr[in_fifo].len = crp->crp_ilen;
  656. td->ptr[out_fifo].ptr = dma_map_single(NULL,
  657. crp->crp_buf, crp->crp_ilen, DMA_TO_DEVICE);
  658. td->ptr[out_fifo].len = crp->crp_ilen;
  659. }
  660. if (enccrd) {
  661. switch (enccrd->crd_alg) {
  662. case CRYPTO_3DES_CBC:
  663. td->hdr |= TALITOS_MODE0_DEU_3DES;
  664. /* FALLTHROUGH */
  665. case CRYPTO_DES_CBC:
  666. td->hdr |= TALITOS_SEL0_DEU
  667. | TALITOS_MODE0_DEU_CBC;
  668. if (enccrd->crd_flags & CRD_F_ENCRYPT)
  669. td->hdr |= TALITOS_MODE0_DEU_ENC;
  670. ivsize = 2*sizeof(u_int32_t);
  671. DPRINTF("%cDES ses %d ch %d len %d\n",
  672. (td->hdr & TALITOS_MODE0_DEU_3DES)?'3':'1',
  673. (u32)TALITOS_SESSION(crp->crp_sid),
  674. chsel, td->ptr[in_fifo].len);
  675. break;
  676. case CRYPTO_AES_CBC:
  677. td->hdr |= TALITOS_SEL0_AESU
  678. | TALITOS_MODE0_AESU_CBC;
  679. if (enccrd->crd_flags & CRD_F_ENCRYPT)
  680. td->hdr |= TALITOS_MODE0_AESU_ENC;
  681. ivsize = 4*sizeof(u_int32_t);
  682. DPRINTF("AES ses %d ch %d len %d\n",
  683. (u32)TALITOS_SESSION(crp->crp_sid),
  684. chsel, td->ptr[in_fifo].len);
  685. break;
  686. default:
  687. printk("%s: unimplemented enccrd->crd_alg %d\n",
  688. device_get_nameunit(sc->sc_cdev), enccrd->crd_alg);
  689. err = EINVAL;
  690. goto errout;
  691. }
  692. /*
  693. * Setup encrypt/decrypt state. When using basic ops
  694. * we can't use an inline IV because hash/crypt offset
  695. * must be from the end of the IV to the start of the
  696. * crypt data and this leaves out the preceding header
  697. * from the hash calculation. Instead we place the IV
  698. * in the state record and set the hash/crypt offset to
  699. * copy both the header+IV.
  700. */
  701. if (enccrd->crd_flags & CRD_F_ENCRYPT) {
  702. td->hdr |= TALITOS_DIR_OUTBOUND;
  703. if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  704. iv = enccrd->crd_iv;
  705. else
  706. read_random((iv = (caddr_t) rand_iv), sizeof(rand_iv));
  707. if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
  708. crypto_copyback(crp->crp_flags, crp->crp_buf,
  709. enccrd->crd_inject, ivsize, iv);
  710. }
  711. } else {
  712. td->hdr |= TALITOS_DIR_INBOUND;
  713. if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
  714. iv = enccrd->crd_iv;
  715. } else {
  716. iv = (caddr_t) rand_iv;
  717. crypto_copydata(crp->crp_flags, crp->crp_buf,
  718. enccrd->crd_inject, ivsize, iv);
  719. }
  720. }
  721. td->ptr[cipher_iv].ptr = dma_map_single(NULL, iv, ivsize,
  722. DMA_TO_DEVICE);
  723. td->ptr[cipher_iv].len = ivsize;
  724. /*
  725. * we don't need the cipher iv out length/pointer
  726. * field to do ESP IPsec. Therefore we set the len field as 0,
  727. * which tells the SEC not to do anything with this len/ptr
  728. * field. Previously, when length/pointer as pointing to iv,
  729. * it gave us corruption of packets.
  730. */
  731. td->ptr[cipher_iv_out].len = 0;
  732. }
  733. if (enccrd && maccrd) {
  734. /* this is ipsec only for now */
  735. td->hdr |= TALITOS_SEL1_MDEU
  736. | TALITOS_MODE1_MDEU_INIT
  737. | TALITOS_MODE1_MDEU_PAD;
  738. switch (maccrd->crd_alg) {
  739. case CRYPTO_MD5:
  740. td->hdr |= TALITOS_MODE1_MDEU_MD5;
  741. break;
  742. case CRYPTO_MD5_HMAC:
  743. td->hdr |= TALITOS_MODE1_MDEU_MD5_HMAC;
  744. break;
  745. case CRYPTO_SHA1:
  746. td->hdr |= TALITOS_MODE1_MDEU_SHA1;
  747. break;
  748. case CRYPTO_SHA1_HMAC:
  749. td->hdr |= TALITOS_MODE1_MDEU_SHA1_HMAC;
  750. break;
  751. default:
  752. /* We cannot order the SEC as requested */
  753. printk("%s: cannot do the order\n",
  754. device_get_nameunit(sc->sc_cdev));
  755. err = EINVAL;
  756. goto errout;
  757. }
  758. if ((maccrd->crd_alg == CRYPTO_MD5_HMAC) ||
  759. (maccrd->crd_alg == CRYPTO_SHA1_HMAC)) {
  760. /*
  761. * The offset from hash data to the start of
  762. * crypt data is the difference in the skips.
  763. */
  764. /* ipsec only for now */
  765. td->ptr[hmac_key].ptr = dma_map_single(NULL,
  766. ses->ses_hmac, ses->ses_hmac_len, DMA_TO_DEVICE);
  767. td->ptr[hmac_key].len = ses->ses_hmac_len;
  768. td->ptr[in_fifo].ptr += enccrd->crd_skip;
  769. td->ptr[in_fifo].len = enccrd->crd_len;
  770. td->ptr[out_fifo].ptr += enccrd->crd_skip;
  771. td->ptr[out_fifo].len = enccrd->crd_len;
  772. /* bytes of HMAC to postpend to ciphertext */
  773. td->ptr[out_fifo].extent = ses->ses_mlen;
  774. td->ptr[hmac_data].ptr += maccrd->crd_skip;
  775. td->ptr[hmac_data].len = enccrd->crd_skip - maccrd->crd_skip;
  776. }
  777. if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
  778. printk("%s: CRD_F_KEY_EXPLICIT unimplemented\n",
  779. device_get_nameunit(sc->sc_cdev));
  780. }
  781. }
  782. if (!enccrd && maccrd) {
  783. /* single MD5 or SHA */
  784. td->hdr |= TALITOS_SEL0_MDEU
  785. | TALITOS_MODE0_MDEU_INIT
  786. | TALITOS_MODE0_MDEU_PAD;
  787. switch (maccrd->crd_alg) {
  788. case CRYPTO_MD5:
  789. td->hdr |= TALITOS_MODE0_MDEU_MD5;
  790. DPRINTF("MD5 ses %d ch %d len %d\n",
  791. (u32)TALITOS_SESSION(crp->crp_sid),
  792. chsel, td->ptr[in_fifo].len);
  793. break;
  794. case CRYPTO_MD5_HMAC:
  795. td->hdr |= TALITOS_MODE0_MDEU_MD5_HMAC;
  796. break;
  797. case CRYPTO_SHA1:
  798. td->hdr |= TALITOS_MODE0_MDEU_SHA1;
  799. DPRINTF("SHA1 ses %d ch %d len %d\n",
  800. (u32)TALITOS_SESSION(crp->crp_sid),
  801. chsel, td->ptr[in_fifo].len);
  802. break;
  803. case CRYPTO_SHA1_HMAC:
  804. td->hdr |= TALITOS_MODE0_MDEU_SHA1_HMAC;
  805. break;
  806. default:
  807. /* We cannot order the SEC as requested */
  808. DPRINTF("cannot do the order\n");
  809. err = EINVAL;
  810. goto errout;
  811. }
  812. if (crp->crp_flags & CRYPTO_F_IOV)
  813. td->ptr[out_fifo].ptr += maccrd->crd_inject;
  814. if ((maccrd->crd_alg == CRYPTO_MD5_HMAC) ||
  815. (maccrd->crd_alg == CRYPTO_SHA1_HMAC)) {
  816. td->ptr[hmac_key].ptr = dma_map_single(NULL,
  817. ses->ses_hmac, ses->ses_hmac_len,
  818. DMA_TO_DEVICE);
  819. td->ptr[hmac_key].len = ses->ses_hmac_len;
  820. }
  821. }
  822. else {
  823. /* using process key (session data has duplicate) */
  824. td->ptr[cipher_key].ptr = dma_map_single(NULL,
  825. enccrd->crd_key, (enccrd->crd_klen + 7) / 8,
  826. DMA_TO_DEVICE);
  827. td->ptr[cipher_key].len = (enccrd->crd_klen + 7) / 8;
  828. }
  829. /* descriptor complete - GO! */
  830. return talitos_submit(sc, td, chsel);
  831. errout:
  832. if (err != ERESTART) {
  833. crp->crp_etype = err;
  834. crypto_done(crp);
  835. }
  836. return err;
  837. }
  838. /* go through all channels descriptors, notifying OCF what has
  839. * _and_hasn't_ successfully completed and reset the device
  840. * (otherwise it's up to decoding desc hdrs!)
  841. */
  842. static void talitos_errorprocessing(struct talitos_softc *sc)
  843. {
  844. unsigned long flags;
  845. int i, j;
  846. /* disable further scheduling until under control */
  847. spin_lock_irqsave(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  848. if (debug) dump_talitos_status(sc);
  849. /* go through descriptors, try and salvage those successfully done,
  850. * and EIO those that weren't
  851. */
  852. for (i = 0; i < sc->sc_num_channels; i++) {
  853. spin_lock_irqsave(&sc->sc_chnfifolock[i], flags);
  854. for (j = 0; j < sc->sc_chfifo_len; j++) {
  855. if (sc->sc_chnfifo[i][j].cf_desc.hdr) {
  856. if ((sc->sc_chnfifo[i][j].cf_desc.hdr
  857. & TALITOS_HDR_DONE_BITS)
  858. != TALITOS_HDR_DONE_BITS) {
  859. /* this one didn't finish */
  860. /* signify in crp->etype */
  861. sc->sc_chnfifo[i][j].cf_crp->crp_etype
  862. = EIO;
  863. }
  864. } else
  865. continue; /* free entry */
  866. /* either way, notify ocf */
  867. crypto_done(sc->sc_chnfifo[i][j].cf_crp);
  868. /* and tag it available again
  869. *
  870. * memset to ensure correct descriptor formation by
  871. * avoiding inadvertently setting "optional" entries
  872. * e.g. not using "optional" dptr2 MD/HMAC processing
  873. */
  874. memset(&sc->sc_chnfifo[i][j].cf_desc,
  875. 0, sizeof(struct talitos_desc));
  876. }
  877. spin_unlock_irqrestore(&sc->sc_chnfifolock[i], flags);
  878. }
  879. /* reset and initialize the SEC h/w device */
  880. talitos_reset_device(sc);
  881. talitos_init_device(sc);
  882. #ifdef CONFIG_OCF_RANDOMHARVEST
  883. if (sc->sc_exec_units & TALITOS_HAS_EU_RNG)
  884. talitos_rng_init(sc);
  885. #endif
  886. /* Okay. Stand by. */
  887. spin_unlock_irqrestore(&sc->sc_chnfifolock[sc->sc_num_channels], flags);
  888. return;
  889. }
  890. /* go through all channels descriptors, notifying OCF what's been done */
  891. static void talitos_doneprocessing(struct talitos_softc *sc)
  892. {
  893. unsigned long flags;
  894. int i, j;
  895. /* go through descriptors looking for done bits */
  896. for (i = 0; i < sc->sc_num_channels; i++) {
  897. spin_lock_irqsave(&sc->sc_chnfifolock[i], flags);
  898. for (j = 0; j < sc->sc_chfifo_len; j++) {
  899. /* descriptor has done bits set? */
  900. if ((sc->sc_chnfifo[i][j].cf_desc.hdr
  901. & TALITOS_HDR_DONE_BITS)
  902. == TALITOS_HDR_DONE_BITS) {
  903. /* notify ocf */
  904. crypto_done(sc->sc_chnfifo[i][j].cf_crp);
  905. /* and tag it available again
  906. *
  907. * memset to ensure correct descriptor formation by
  908. * avoiding inadvertently setting "optional" entries
  909. * e.g. not using "optional" dptr2 MD/HMAC processing
  910. */
  911. memset(&sc->sc_chnfifo[i][j].cf_desc,
  912. 0, sizeof(struct talitos_desc));
  913. }
  914. }
  915. spin_unlock_irqrestore(&sc->sc_chnfifolock[i], flags);
  916. }
  917. return;
  918. }
  919. static irqreturn_t
  920. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  921. talitos_intr(int irq, void *arg)
  922. #else
  923. talitos_intr(int irq, void *arg, struct pt_regs *regs)
  924. #endif
  925. {
  926. struct talitos_softc *sc = arg;
  927. u_int32_t v, v_hi;
  928. /* ack */
  929. v = talitos_read(sc->sc_base_addr + TALITOS_ISR);
  930. v_hi = talitos_read(sc->sc_base_addr + TALITOS_ISR_HI);
  931. talitos_write(sc->sc_base_addr + TALITOS_ICR, v);
  932. talitos_write(sc->sc_base_addr + TALITOS_ICR_HI, v_hi);
  933. if (unlikely(v & TALITOS_ISR_ERROR)) {
  934. /* Okay, Houston, we've had a problem here. */
  935. printk(KERN_DEBUG "%s: got error interrupt - ISR 0x%08x_%08x\n",
  936. device_get_nameunit(sc->sc_cdev), v, v_hi);
  937. talitos_errorprocessing(sc);
  938. } else
  939. if (likely(v & TALITOS_ISR_DONE)) {
  940. talitos_doneprocessing(sc);
  941. }
  942. return IRQ_HANDLED;
  943. }
  944. /*
  945. * Initialize registers we need to touch only once.
  946. */
  947. static void
  948. talitos_init_device(struct talitos_softc *sc)
  949. {
  950. u_int32_t v;
  951. int i;
  952. DPRINTF("%s()\n", __FUNCTION__);
  953. /* init all channels */
  954. for (i = 0; i < sc->sc_num_channels; i++) {
  955. v = talitos_read(sc->sc_base_addr +
  956. i*TALITOS_CH_OFFSET + TALITOS_CH_CCCR_HI);
  957. v |= TALITOS_CH_CCCR_HI_CDWE
  958. | TALITOS_CH_CCCR_HI_CDIE; /* invoke interrupt if done */
  959. talitos_write(sc->sc_base_addr +
  960. i*TALITOS_CH_OFFSET + TALITOS_CH_CCCR_HI, v);
  961. }
  962. /* enable all interrupts */
  963. v = talitos_read(sc->sc_base_addr + TALITOS_IMR);
  964. v |= TALITOS_IMR_ALL;
  965. talitos_write(sc->sc_base_addr + TALITOS_IMR, v);
  966. v = talitos_read(sc->sc_base_addr + TALITOS_IMR_HI);
  967. v |= TALITOS_IMR_HI_ERRONLY;
  968. talitos_write(sc->sc_base_addr + TALITOS_IMR_HI, v);
  969. return;
  970. }
  971. /*
  972. * set the master reset bit on the device.
  973. */
  974. static void
  975. talitos_reset_device_master(struct talitos_softc *sc)
  976. {
  977. u_int32_t v;
  978. /* Reset the device by writing 1 to MCR:SWR and waiting 'til cleared */
  979. v = talitos_read(sc->sc_base_addr + TALITOS_MCR);
  980. talitos_write(sc->sc_base_addr + TALITOS_MCR, v | TALITOS_MCR_SWR);
  981. while (talitos_read(sc->sc_base_addr + TALITOS_MCR) & TALITOS_MCR_SWR)
  982. cpu_relax();
  983. return;
  984. }
  985. /*
  986. * Resets the device. Values in the registers are left as is
  987. * from the reset (i.e. initial values are assigned elsewhere).
  988. */
  989. static void
  990. talitos_reset_device(struct talitos_softc *sc)
  991. {
  992. u_int32_t v;
  993. int i;
  994. DPRINTF("%s()\n", __FUNCTION__);
  995. /*
  996. * Master reset
  997. * errata documentation: warning: certain SEC interrupts
  998. * are not fully cleared by writing the MCR:SWR bit,
  999. * set bit twice to completely reset
  1000. */
  1001. talitos_reset_device_master(sc); /* once */
  1002. talitos_reset_device_master(sc); /* and once again */
  1003. /* reset all channels */
  1004. for (i = 0; i < sc->sc_num_channels; i++) {
  1005. v = talitos_read(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  1006. TALITOS_CH_CCCR);
  1007. talitos_write(sc->sc_base_addr + i*TALITOS_CH_OFFSET +
  1008. TALITOS_CH_CCCR, v | TALITOS_CH_CCCR_RESET);
  1009. }
  1010. }
  1011. /* Set up the crypto device structure, private data,
  1012. * and anything else we need before we start */
  1013. #ifdef CONFIG_PPC_MERGE
  1014. static int talitos_probe(struct of_device *ofdev, const struct of_device_id *match)
  1015. #else
  1016. static int talitos_probe(struct platform_device *pdev)
  1017. #endif
  1018. {
  1019. struct talitos_softc *sc = NULL;
  1020. struct resource *r;
  1021. #ifdef CONFIG_PPC_MERGE
  1022. struct device *device = &ofdev->dev;
  1023. struct device_node *np = ofdev->node;
  1024. const unsigned int *prop;
  1025. int err;
  1026. struct resource res;
  1027. #endif
  1028. static int num_chips = 0;
  1029. int rc;
  1030. int i;
  1031. DPRINTF("%s()\n", __FUNCTION__);
  1032. sc = (struct talitos_softc *) kmalloc(sizeof(*sc), GFP_KERNEL);
  1033. if (!sc)
  1034. return -ENOMEM;
  1035. memset(sc, 0, sizeof(*sc));
  1036. softc_device_init(sc, DRV_NAME, num_chips, talitos_methods);
  1037. sc->sc_irq = -1;
  1038. sc->sc_cid = -1;
  1039. #ifndef CONFIG_PPC_MERGE
  1040. sc->sc_dev = pdev;
  1041. #endif
  1042. sc->sc_num = num_chips++;
  1043. #ifdef CONFIG_PPC_MERGE
  1044. dev_set_drvdata(device, sc);
  1045. #else
  1046. platform_set_drvdata(sc->sc_dev, sc);
  1047. #endif
  1048. /* get the irq line */
  1049. #ifdef CONFIG_PPC_MERGE
  1050. err = of_address_to_resource(np, 0, &res);
  1051. if (err)
  1052. return -EINVAL;
  1053. r = &res;
  1054. sc->sc_irq = irq_of_parse_and_map(np, 0);
  1055. #else
  1056. /* get a pointer to the register memory */
  1057. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1058. sc->sc_irq = platform_get_irq(pdev, 0);
  1059. #endif
  1060. rc = request_irq(sc->sc_irq, talitos_intr, 0,
  1061. device_get_nameunit(sc->sc_cdev), sc);
  1062. if (rc) {
  1063. printk(KERN_ERR "%s: failed to hook irq %d\n",
  1064. device_get_nameunit(sc->sc_cdev), sc->sc_irq);
  1065. sc->sc_irq = -1;
  1066. goto out;
  1067. }
  1068. sc->sc_base_addr = (ocf_iomem_t) ioremap(r->start, (r->end - r->start));
  1069. if (!sc->sc_base_addr) {
  1070. printk(KERN_ERR "%s: failed to ioremap\n",
  1071. device_get_nameunit(sc->sc_cdev));
  1072. goto out;
  1073. }
  1074. /* figure out our SEC's properties and capabilities */
  1075. sc->sc_chiprev = (u64)talitos_read(sc->sc_base_addr + TALITOS_ID) << 32
  1076. | talitos_read(sc->sc_base_addr + TALITOS_ID_HI);
  1077. DPRINTF("sec id 0x%llx\n", sc->sc_chiprev);
  1078. #ifdef CONFIG_PPC_MERGE
  1079. /* get SEC properties from device tree, defaulting to SEC 2.0 */
  1080. prop = of_get_property(np, "num-channels", NULL);
  1081. sc->sc_num_channels = prop ? *prop : TALITOS_NCHANNELS_SEC_2_0;
  1082. prop = of_get_property(np, "channel-fifo-len", NULL);
  1083. sc->sc_chfifo_len = prop ? *prop : TALITOS_CHFIFOLEN_SEC_2_0;
  1084. prop = of_get_property(np, "exec-units-mask", NULL);
  1085. sc->sc_exec_units = prop ? *prop : TALITOS_HAS_EUS_SEC_2_0;
  1086. prop = of_get_property(np, "descriptor-types-mask", NULL);
  1087. sc->sc_desc_types = prop ? *prop : TALITOS_HAS_DESCTYPES_SEC_2_0;
  1088. #else
  1089. /* bulk should go away with openfirmware flat device tree support */
  1090. if (sc->sc_chiprev & TALITOS_ID_SEC_2_0) {
  1091. sc->sc_num_channels = TALITOS_NCHANNELS_SEC_2_0;
  1092. sc->sc_chfifo_len = TALITOS_CHFIFOLEN_SEC_2_0;
  1093. sc->sc_exec_units = TALITOS_HAS_EUS_SEC_2_0;
  1094. sc->sc_desc_types = TALITOS_HAS_DESCTYPES_SEC_2_0;
  1095. } else {
  1096. printk(KERN_ERR "%s: failed to id device\n",
  1097. device_get_nameunit(sc->sc_cdev));
  1098. goto out;
  1099. }
  1100. #endif
  1101. /* + 1 is for the meta-channel lock used by the channel scheduler */
  1102. sc->sc_chnfifolock = (spinlock_t *) kmalloc(
  1103. (sc->sc_num_channels + 1) * sizeof(spinlock_t), GFP_KERNEL);
  1104. if (!sc->sc_chnfifolock)
  1105. goto out;
  1106. for (i = 0; i < sc->sc_num_channels + 1; i++) {
  1107. spin_lock_init(&sc->sc_chnfifolock[i]);
  1108. }
  1109. sc->sc_chnlastalg = (int *) kmalloc(
  1110. sc->sc_num_channels * sizeof(int), GFP_KERNEL);
  1111. if (!sc->sc_chnlastalg)
  1112. goto out;
  1113. memset(sc->sc_chnlastalg, 0, sc->sc_num_channels * sizeof(int));
  1114. sc->sc_chnfifo = (struct desc_cryptop_pair **) kmalloc(
  1115. sc->sc_num_channels * sizeof(struct desc_cryptop_pair *),
  1116. GFP_KERNEL);
  1117. if (!sc->sc_chnfifo)
  1118. goto out;
  1119. for (i = 0; i < sc->sc_num_channels; i++) {
  1120. sc->sc_chnfifo[i] = (struct desc_cryptop_pair *) kmalloc(
  1121. sc->sc_chfifo_len * sizeof(struct desc_cryptop_pair),
  1122. GFP_KERNEL);
  1123. if (!sc->sc_chnfifo[i])
  1124. goto out;
  1125. memset(sc->sc_chnfifo[i], 0,
  1126. sc->sc_chfifo_len * sizeof(struct desc_cryptop_pair));
  1127. }
  1128. /* reset and initialize the SEC h/w device */
  1129. talitos_reset_device(sc);
  1130. talitos_init_device(sc);
  1131. sc->sc_cid = crypto_get_driverid(softc_get_device(sc),CRYPTOCAP_F_HARDWARE);
  1132. if (sc->sc_cid < 0) {
  1133. printk(KERN_ERR "%s: could not get crypto driver id\n",
  1134. device_get_nameunit(sc->sc_cdev));
  1135. goto out;
  1136. }
  1137. /* register algorithms with the framework */
  1138. printk("%s:", device_get_nameunit(sc->sc_cdev));
  1139. if (sc->sc_exec_units & TALITOS_HAS_EU_RNG) {
  1140. printk(" rng");
  1141. #ifdef CONFIG_OCF_RANDOMHARVEST
  1142. talitos_rng_init(sc);
  1143. crypto_rregister(sc->sc_cid, talitos_read_random, sc);
  1144. #endif
  1145. }
  1146. if (sc->sc_exec_units & TALITOS_HAS_EU_DEU) {
  1147. printk(" des/3des");
  1148. crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
  1149. crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
  1150. }
  1151. if (sc->sc_exec_units & TALITOS_HAS_EU_AESU) {
  1152. printk(" aes");
  1153. crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
  1154. }
  1155. if (sc->sc_exec_units & TALITOS_HAS_EU_MDEU) {
  1156. printk(" md5");
  1157. crypto_register(sc->sc_cid, CRYPTO_MD5, 0, 0);
  1158. /* HMAC support only with IPsec for now */
  1159. crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
  1160. printk(" sha1");
  1161. crypto_register(sc->sc_cid, CRYPTO_SHA1, 0, 0);
  1162. /* HMAC support only with IPsec for now */
  1163. crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
  1164. }
  1165. printk("\n");
  1166. return 0;
  1167. out:
  1168. #ifndef CONFIG_PPC_MERGE
  1169. talitos_remove(pdev);
  1170. #endif
  1171. return -ENOMEM;
  1172. }
  1173. #ifdef CONFIG_PPC_MERGE
  1174. static int talitos_remove(struct of_device *ofdev)
  1175. #else
  1176. static int talitos_remove(struct platform_device *pdev)
  1177. #endif
  1178. {
  1179. #ifdef CONFIG_PPC_MERGE
  1180. struct talitos_softc *sc = dev_get_drvdata(&ofdev->dev);
  1181. #else
  1182. struct talitos_softc *sc = platform_get_drvdata(pdev);
  1183. #endif
  1184. int i;
  1185. DPRINTF("%s()\n", __FUNCTION__);
  1186. if (sc->sc_cid >= 0)
  1187. crypto_unregister_all(sc->sc_cid);
  1188. if (sc->sc_chnfifo) {
  1189. for (i = 0; i < sc->sc_num_channels; i++)
  1190. if (sc->sc_chnfifo[i])
  1191. kfree(sc->sc_chnfifo[i]);
  1192. kfree(sc->sc_chnfifo);
  1193. }
  1194. if (sc->sc_chnlastalg)
  1195. kfree(sc->sc_chnlastalg);
  1196. if (sc->sc_chnfifolock)
  1197. kfree(sc->sc_chnfifolock);
  1198. if (sc->sc_irq != -1)
  1199. free_irq(sc->sc_irq, sc);
  1200. if (sc->sc_base_addr)
  1201. iounmap((void *) sc->sc_base_addr);
  1202. kfree(sc);
  1203. return 0;
  1204. }
  1205. #ifdef CONFIG_PPC_MERGE
  1206. static struct of_device_id talitos_match[] = {
  1207. {
  1208. .type = "crypto",
  1209. .compatible = "talitos",
  1210. },
  1211. {},
  1212. };
  1213. MODULE_DEVICE_TABLE(of, talitos_match);
  1214. static struct of_platform_driver talitos_driver = {
  1215. .name = DRV_NAME,
  1216. .match_table = talitos_match,
  1217. .probe = talitos_probe,
  1218. .remove = talitos_remove,
  1219. };
  1220. static int __init talitos_init(void)
  1221. {
  1222. return of_register_platform_driver(&talitos_driver);
  1223. }
  1224. static void __exit talitos_exit(void)
  1225. {
  1226. of_unregister_platform_driver(&talitos_driver);
  1227. }
  1228. #else
  1229. /* Structure for a platform device driver */
  1230. static struct platform_driver talitos_driver = {
  1231. .probe = talitos_probe,
  1232. .remove = talitos_remove,
  1233. .driver = {
  1234. .name = "fsl-sec2",
  1235. }
  1236. };
  1237. static int __init talitos_init(void)
  1238. {
  1239. return platform_driver_register(&talitos_driver);
  1240. }
  1241. static void __exit talitos_exit(void)
  1242. {
  1243. platform_driver_unregister(&talitos_driver);
  1244. }
  1245. #endif
  1246. module_init(talitos_init);
  1247. module_exit(talitos_exit);
  1248. MODULE_LICENSE("Dual BSD/GPL");
  1249. MODULE_AUTHOR("kim.phillips@freescale.com");
  1250. MODULE_DESCRIPTION("OCF driver for Freescale SEC (talitos)");